1 /* 2 * net/tipc/socket.c: TIPC socket API 3 * 4 * Copyright (c) 2001-2006, Ericsson AB 5 * Copyright (c) 2004-2005, 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/slab.h> 44 #include <linux/poll.h> 45 #include <linux/fcntl.h> 46 #include <asm/semaphore.h> 47 #include <asm/string.h> 48 #include <asm/atomic.h> 49 #include <net/sock.h> 50 51 #include <linux/tipc.h> 52 #include <linux/tipc_config.h> 53 #include <net/tipc/tipc_msg.h> 54 #include <net/tipc/tipc_port.h> 55 56 #include "core.h" 57 58 #define SS_LISTENING -1 /* socket is listening */ 59 #define SS_READY -2 /* socket is connectionless */ 60 61 #define OVERLOAD_LIMIT_BASE 5000 62 63 struct tipc_sock { 64 struct sock sk; 65 struct tipc_port *p; 66 struct semaphore sem; 67 }; 68 69 #define tipc_sk(sk) ((struct tipc_sock*)sk) 70 71 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); 72 static void wakeupdispatch(struct tipc_port *tport); 73 74 static struct proto_ops packet_ops; 75 static struct proto_ops stream_ops; 76 static struct proto_ops msg_ops; 77 78 static struct proto tipc_proto; 79 80 static int sockets_enabled = 0; 81 82 static atomic_t tipc_queue_size = ATOMIC_INIT(0); 83 84 85 /* 86 * sock_lock(): Lock a port/socket pair. lock_sock() can 87 * not be used here, since the same lock must protect ports 88 * with non-socket interfaces. 89 * See net.c for description of locking policy. 90 */ 91 static void sock_lock(struct tipc_sock* tsock) 92 { 93 spin_lock_bh(tsock->p->lock); 94 } 95 96 /* 97 * sock_unlock(): Unlock a port/socket pair 98 */ 99 static void sock_unlock(struct tipc_sock* tsock) 100 { 101 spin_unlock_bh(tsock->p->lock); 102 } 103 104 /** 105 * pollmask - determine the current set of poll() events for a socket 106 * @sock: socket structure 107 * 108 * TIPC sets the returned events as follows: 109 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty 110 * or if a connection-oriented socket is does not have an active connection 111 * (i.e. a read operation will not block). 112 * b) POLLOUT is set except when a socket's connection has been terminated 113 * (i.e. a write operation will not block). 114 * c) POLLHUP is set when a socket's connection has been terminated. 115 * 116 * IMPORTANT: The fact that a read or write operation will not block does NOT 117 * imply that the operation will succeed! 118 * 119 * Returns pollmask value 120 */ 121 122 static u32 pollmask(struct socket *sock) 123 { 124 u32 mask; 125 126 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) || 127 (sock->state == SS_UNCONNECTED) || 128 (sock->state == SS_DISCONNECTING)) 129 mask = (POLLRDNORM | POLLIN); 130 else 131 mask = 0; 132 133 if (sock->state == SS_DISCONNECTING) 134 mask |= POLLHUP; 135 else 136 mask |= POLLOUT; 137 138 return mask; 139 } 140 141 142 /** 143 * advance_queue - discard first buffer in queue 144 * @tsock: TIPC socket 145 */ 146 147 static void advance_queue(struct tipc_sock *tsock) 148 { 149 sock_lock(tsock); 150 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue)); 151 sock_unlock(tsock); 152 atomic_dec(&tipc_queue_size); 153 } 154 155 /** 156 * tipc_create - create a TIPC socket 157 * @sock: pre-allocated socket structure 158 * @protocol: protocol indicator (must be 0) 159 * 160 * This routine creates and attaches a 'struct sock' to the 'struct socket', 161 * then create and attaches a TIPC port to the 'struct sock' part. 162 * 163 * Returns 0 on success, errno otherwise 164 */ 165 static int tipc_create(struct socket *sock, int protocol) 166 { 167 struct tipc_sock *tsock; 168 struct tipc_port *port; 169 struct sock *sk; 170 u32 ref; 171 172 if (unlikely(protocol != 0)) 173 return -EPROTONOSUPPORT; 174 175 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE); 176 if (unlikely(!ref)) 177 return -ENOMEM; 178 179 sock->state = SS_UNCONNECTED; 180 181 switch (sock->type) { 182 case SOCK_STREAM: 183 sock->ops = &stream_ops; 184 break; 185 case SOCK_SEQPACKET: 186 sock->ops = &packet_ops; 187 break; 188 case SOCK_DGRAM: 189 tipc_set_portunreliable(ref, 1); 190 /* fall through */ 191 case SOCK_RDM: 192 tipc_set_portunreturnable(ref, 1); 193 sock->ops = &msg_ops; 194 sock->state = SS_READY; 195 break; 196 default: 197 tipc_deleteport(ref); 198 return -EPROTOTYPE; 199 } 200 201 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1); 202 if (!sk) { 203 tipc_deleteport(ref); 204 return -ENOMEM; 205 } 206 207 sock_init_data(sock, sk); 208 init_waitqueue_head(sk->sk_sleep); 209 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */ 210 211 tsock = tipc_sk(sk); 212 port = tipc_get_port(ref); 213 214 tsock->p = port; 215 port->usr_handle = tsock; 216 217 init_MUTEX(&tsock->sem); 218 219 dbg("sock_create: %x\n",tsock); 220 221 atomic_inc(&tipc_user_count); 222 223 return 0; 224 } 225 226 /** 227 * release - destroy a TIPC socket 228 * @sock: socket to destroy 229 * 230 * This routine cleans up any messages that are still queued on the socket. 231 * For DGRAM and RDM socket types, all queued messages are rejected. 232 * For SEQPACKET and STREAM socket types, the first message is rejected 233 * and any others are discarded. (If the first message on a STREAM socket 234 * is partially-read, it is discarded and the next one is rejected instead.) 235 * 236 * NOTE: Rejected messages are not necessarily returned to the sender! They 237 * are returned or discarded according to the "destination droppable" setting 238 * specified for the message by the sender. 239 * 240 * Returns 0 on success, errno otherwise 241 */ 242 243 static int release(struct socket *sock) 244 { 245 struct tipc_sock *tsock = tipc_sk(sock->sk); 246 struct sock *sk = sock->sk; 247 int res = TIPC_OK; 248 struct sk_buff *buf; 249 250 dbg("sock_delete: %x\n",tsock); 251 if (!tsock) 252 return 0; 253 down_interruptible(&tsock->sem); 254 if (!sock->sk) { 255 up(&tsock->sem); 256 return 0; 257 } 258 259 /* Reject unreceived messages, unless no longer connected */ 260 261 while (sock->state != SS_DISCONNECTING) { 262 sock_lock(tsock); 263 buf = skb_dequeue(&sk->sk_receive_queue); 264 if (!buf) 265 tsock->p->usr_handle = NULL; 266 sock_unlock(tsock); 267 if (!buf) 268 break; 269 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) 270 buf_discard(buf); 271 else 272 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 273 atomic_dec(&tipc_queue_size); 274 } 275 276 /* Delete TIPC port */ 277 278 res = tipc_deleteport(tsock->p->ref); 279 sock->sk = NULL; 280 281 /* Discard any remaining messages */ 282 283 while ((buf = skb_dequeue(&sk->sk_receive_queue))) { 284 buf_discard(buf); 285 atomic_dec(&tipc_queue_size); 286 } 287 288 up(&tsock->sem); 289 290 sock_put(sk); 291 292 atomic_dec(&tipc_user_count); 293 return res; 294 } 295 296 /** 297 * bind - associate or disassocate TIPC name(s) with a socket 298 * @sock: socket structure 299 * @uaddr: socket address describing name(s) and desired operation 300 * @uaddr_len: size of socket address data structure 301 * 302 * Name and name sequence binding is indicated using a positive scope value; 303 * a negative scope value unbinds the specified name. Specifying no name 304 * (i.e. a socket address length of 0) unbinds all names from the socket. 305 * 306 * Returns 0 on success, errno otherwise 307 */ 308 309 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) 310 { 311 struct tipc_sock *tsock = tipc_sk(sock->sk); 312 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 313 int res; 314 315 if (down_interruptible(&tsock->sem)) 316 return -ERESTARTSYS; 317 318 if (unlikely(!uaddr_len)) { 319 res = tipc_withdraw(tsock->p->ref, 0, NULL); 320 goto exit; 321 } 322 323 if (uaddr_len < sizeof(struct sockaddr_tipc)) { 324 res = -EINVAL; 325 goto exit; 326 } 327 328 if (addr->family != AF_TIPC) { 329 res = -EAFNOSUPPORT; 330 goto exit; 331 } 332 if (addr->addrtype == TIPC_ADDR_NAME) 333 addr->addr.nameseq.upper = addr->addr.nameseq.lower; 334 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { 335 res = -EAFNOSUPPORT; 336 goto exit; 337 } 338 339 if (addr->scope > 0) 340 res = tipc_publish(tsock->p->ref, addr->scope, 341 &addr->addr.nameseq); 342 else 343 res = tipc_withdraw(tsock->p->ref, -addr->scope, 344 &addr->addr.nameseq); 345 exit: 346 up(&tsock->sem); 347 return res; 348 } 349 350 /** 351 * get_name - get port ID of socket or peer socket 352 * @sock: socket structure 353 * @uaddr: area for returned socket address 354 * @uaddr_len: area for returned length of socket address 355 * @peer: 0 to obtain socket name, 1 to obtain peer socket name 356 * 357 * Returns 0 on success, errno otherwise 358 */ 359 360 static int get_name(struct socket *sock, struct sockaddr *uaddr, 361 int *uaddr_len, int peer) 362 { 363 struct tipc_sock *tsock = tipc_sk(sock->sk); 364 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 365 u32 res; 366 367 if (down_interruptible(&tsock->sem)) 368 return -ERESTARTSYS; 369 370 *uaddr_len = sizeof(*addr); 371 addr->addrtype = TIPC_ADDR_ID; 372 addr->family = AF_TIPC; 373 addr->scope = 0; 374 if (peer) 375 res = tipc_peer(tsock->p->ref, &addr->addr.id); 376 else 377 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id); 378 addr->addr.name.domain = 0; 379 380 up(&tsock->sem); 381 return res; 382 } 383 384 /** 385 * poll - read and possibly block on pollmask 386 * @file: file structure associated with the socket 387 * @sock: socket for which to calculate the poll bits 388 * @wait: ??? 389 * 390 * Returns the pollmask 391 */ 392 393 static unsigned int poll(struct file *file, struct socket *sock, 394 poll_table *wait) 395 { 396 poll_wait(file, sock->sk->sk_sleep, wait); 397 /* NEED LOCK HERE? */ 398 return pollmask(sock); 399 } 400 401 /** 402 * dest_name_check - verify user is permitted to send to specified port name 403 * @dest: destination address 404 * @m: descriptor for message to be sent 405 * 406 * Prevents restricted configuration commands from being issued by 407 * unauthorized users. 408 * 409 * Returns 0 if permission is granted, otherwise errno 410 */ 411 412 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) 413 { 414 struct tipc_cfg_msg_hdr hdr; 415 416 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) 417 return 0; 418 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) 419 return 0; 420 421 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) 422 return -EACCES; 423 424 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) 425 return -EFAULT; 426 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) 427 return -EACCES; 428 429 return 0; 430 } 431 432 /** 433 * send_msg - send message in connectionless manner 434 * @iocb: (unused) 435 * @sock: socket structure 436 * @m: message to send 437 * @total_len: length of message 438 * 439 * Message must have an destination specified explicitly. 440 * Used for SOCK_RDM and SOCK_DGRAM messages, 441 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. 442 * (Note: 'SYN+' is prohibited on SOCK_STREAM.) 443 * 444 * Returns the number of bytes sent on success, or errno otherwise 445 */ 446 447 static int send_msg(struct kiocb *iocb, struct socket *sock, 448 struct msghdr *m, size_t total_len) 449 { 450 struct tipc_sock *tsock = tipc_sk(sock->sk); 451 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; 452 struct sk_buff *buf; 453 int needs_conn; 454 int res = -EINVAL; 455 456 if (unlikely(!dest)) 457 return -EDESTADDRREQ; 458 if (unlikely((m->msg_namelen < sizeof(*dest)) || 459 (dest->family != AF_TIPC))) 460 return -EINVAL; 461 462 needs_conn = (sock->state != SS_READY); 463 if (unlikely(needs_conn)) { 464 if (sock->state == SS_LISTENING) 465 return -EPIPE; 466 if (sock->state != SS_UNCONNECTED) 467 return -EISCONN; 468 if ((tsock->p->published) || 469 ((sock->type == SOCK_STREAM) && (total_len != 0))) 470 return -EOPNOTSUPP; 471 if (dest->addrtype == TIPC_ADDR_NAME) { 472 tsock->p->conn_type = dest->addr.name.name.type; 473 tsock->p->conn_instance = dest->addr.name.name.instance; 474 } 475 } 476 477 if (down_interruptible(&tsock->sem)) 478 return -ERESTARTSYS; 479 480 if (needs_conn) { 481 482 /* Abort any pending connection attempts (very unlikely) */ 483 484 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { 485 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 486 atomic_dec(&tipc_queue_size); 487 } 488 489 sock->state = SS_CONNECTING; 490 } 491 492 do { 493 if (dest->addrtype == TIPC_ADDR_NAME) { 494 if ((res = dest_name_check(dest, m))) 495 goto exit; 496 res = tipc_send2name(tsock->p->ref, 497 &dest->addr.name.name, 498 dest->addr.name.domain, 499 m->msg_iovlen, 500 m->msg_iov); 501 } 502 else if (dest->addrtype == TIPC_ADDR_ID) { 503 res = tipc_send2port(tsock->p->ref, 504 &dest->addr.id, 505 m->msg_iovlen, 506 m->msg_iov); 507 } 508 else if (dest->addrtype == TIPC_ADDR_MCAST) { 509 if (needs_conn) { 510 res = -EOPNOTSUPP; 511 goto exit; 512 } 513 if ((res = dest_name_check(dest, m))) 514 goto exit; 515 res = tipc_multicast(tsock->p->ref, 516 &dest->addr.nameseq, 517 0, 518 m->msg_iovlen, 519 m->msg_iov); 520 } 521 if (likely(res != -ELINKCONG)) { 522 exit: 523 up(&tsock->sem); 524 return res; 525 } 526 if (m->msg_flags & MSG_DONTWAIT) { 527 res = -EWOULDBLOCK; 528 goto exit; 529 } 530 if (wait_event_interruptible(*sock->sk->sk_sleep, 531 !tsock->p->congested)) { 532 res = -ERESTARTSYS; 533 goto exit; 534 } 535 } while (1); 536 } 537 538 /** 539 * send_packet - send a connection-oriented message 540 * @iocb: (unused) 541 * @sock: socket structure 542 * @m: message to send 543 * @total_len: length of message 544 * 545 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. 546 * 547 * Returns the number of bytes sent on success, or errno otherwise 548 */ 549 550 static int send_packet(struct kiocb *iocb, struct socket *sock, 551 struct msghdr *m, size_t total_len) 552 { 553 struct tipc_sock *tsock = tipc_sk(sock->sk); 554 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; 555 int res; 556 557 /* Handle implied connection establishment */ 558 559 if (unlikely(dest)) 560 return send_msg(iocb, sock, m, total_len); 561 562 if (down_interruptible(&tsock->sem)) { 563 return -ERESTARTSYS; 564 } 565 566 do { 567 if (unlikely(sock->state != SS_CONNECTED)) { 568 if (sock->state == SS_DISCONNECTING) 569 res = -EPIPE; 570 else 571 res = -ENOTCONN; 572 goto exit; 573 } 574 575 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov); 576 if (likely(res != -ELINKCONG)) { 577 exit: 578 up(&tsock->sem); 579 return res; 580 } 581 if (m->msg_flags & MSG_DONTWAIT) { 582 res = -EWOULDBLOCK; 583 goto exit; 584 } 585 if (wait_event_interruptible(*sock->sk->sk_sleep, 586 !tsock->p->congested)) { 587 res = -ERESTARTSYS; 588 goto exit; 589 } 590 } while (1); 591 } 592 593 /** 594 * send_stream - send stream-oriented data 595 * @iocb: (unused) 596 * @sock: socket structure 597 * @m: data to send 598 * @total_len: total length of data to be sent 599 * 600 * Used for SOCK_STREAM data. 601 * 602 * Returns the number of bytes sent on success (or partial success), 603 * or errno if no data sent 604 */ 605 606 607 static int send_stream(struct kiocb *iocb, struct socket *sock, 608 struct msghdr *m, size_t total_len) 609 { 610 struct msghdr my_msg; 611 struct iovec my_iov; 612 struct iovec *curr_iov; 613 int curr_iovlen; 614 char __user *curr_start; 615 int curr_left; 616 int bytes_to_send; 617 int bytes_sent; 618 int res; 619 620 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE)) 621 return send_packet(iocb, sock, m, total_len); 622 623 /* Can only send large data streams if already connected */ 624 625 if (unlikely(sock->state != SS_CONNECTED)) { 626 if (sock->state == SS_DISCONNECTING) 627 return -EPIPE; 628 else 629 return -ENOTCONN; 630 } 631 632 /* 633 * Send each iovec entry using one or more messages 634 * 635 * Note: This algorithm is good for the most likely case 636 * (i.e. one large iovec entry), but could be improved to pass sets 637 * of small iovec entries into send_packet(). 638 */ 639 640 curr_iov = m->msg_iov; 641 curr_iovlen = m->msg_iovlen; 642 my_msg.msg_iov = &my_iov; 643 my_msg.msg_iovlen = 1; 644 bytes_sent = 0; 645 646 while (curr_iovlen--) { 647 curr_start = curr_iov->iov_base; 648 curr_left = curr_iov->iov_len; 649 650 while (curr_left) { 651 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE) 652 ? curr_left : TIPC_MAX_USER_MSG_SIZE; 653 my_iov.iov_base = curr_start; 654 my_iov.iov_len = bytes_to_send; 655 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) { 656 return bytes_sent ? bytes_sent : res; 657 } 658 curr_left -= bytes_to_send; 659 curr_start += bytes_to_send; 660 bytes_sent += bytes_to_send; 661 } 662 663 curr_iov++; 664 } 665 666 return bytes_sent; 667 } 668 669 /** 670 * auto_connect - complete connection setup to a remote port 671 * @sock: socket structure 672 * @tsock: TIPC-specific socket structure 673 * @msg: peer's response message 674 * 675 * Returns 0 on success, errno otherwise 676 */ 677 678 static int auto_connect(struct socket *sock, struct tipc_sock *tsock, 679 struct tipc_msg *msg) 680 { 681 struct tipc_portid peer; 682 683 if (msg_errcode(msg)) { 684 sock->state = SS_DISCONNECTING; 685 return -ECONNREFUSED; 686 } 687 688 peer.ref = msg_origport(msg); 689 peer.node = msg_orignode(msg); 690 tipc_connect2port(tsock->p->ref, &peer); 691 tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); 692 sock->state = SS_CONNECTED; 693 return 0; 694 } 695 696 /** 697 * set_orig_addr - capture sender's address for received message 698 * @m: descriptor for message info 699 * @msg: received message header 700 * 701 * Note: Address is not captured if not requested by receiver. 702 */ 703 704 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) 705 { 706 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name; 707 708 if (addr) { 709 addr->family = AF_TIPC; 710 addr->addrtype = TIPC_ADDR_ID; 711 addr->addr.id.ref = msg_origport(msg); 712 addr->addr.id.node = msg_orignode(msg); 713 addr->addr.name.domain = 0; /* could leave uninitialized */ 714 addr->scope = 0; /* could leave uninitialized */ 715 m->msg_namelen = sizeof(struct sockaddr_tipc); 716 } 717 } 718 719 /** 720 * anc_data_recv - optionally capture ancillary data for received message 721 * @m: descriptor for message info 722 * @msg: received message header 723 * @tport: TIPC port associated with message 724 * 725 * Note: Ancillary data is not captured if not requested by receiver. 726 * 727 * Returns 0 if successful, otherwise errno 728 */ 729 730 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, 731 struct tipc_port *tport) 732 { 733 u32 anc_data[3]; 734 u32 err; 735 u32 dest_type; 736 int has_name; 737 int res; 738 739 if (likely(m->msg_controllen == 0)) 740 return 0; 741 742 /* Optionally capture errored message object(s) */ 743 744 err = msg ? msg_errcode(msg) : 0; 745 if (unlikely(err)) { 746 anc_data[0] = err; 747 anc_data[1] = msg_data_sz(msg); 748 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data))) 749 return res; 750 if (anc_data[1] && 751 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], 752 msg_data(msg)))) 753 return res; 754 } 755 756 /* Optionally capture message destination object */ 757 758 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; 759 switch (dest_type) { 760 case TIPC_NAMED_MSG: 761 has_name = 1; 762 anc_data[0] = msg_nametype(msg); 763 anc_data[1] = msg_namelower(msg); 764 anc_data[2] = msg_namelower(msg); 765 break; 766 case TIPC_MCAST_MSG: 767 has_name = 1; 768 anc_data[0] = msg_nametype(msg); 769 anc_data[1] = msg_namelower(msg); 770 anc_data[2] = msg_nameupper(msg); 771 break; 772 case TIPC_CONN_MSG: 773 has_name = (tport->conn_type != 0); 774 anc_data[0] = tport->conn_type; 775 anc_data[1] = tport->conn_instance; 776 anc_data[2] = tport->conn_instance; 777 break; 778 default: 779 has_name = 0; 780 } 781 if (has_name && 782 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data))) 783 return res; 784 785 return 0; 786 } 787 788 /** 789 * recv_msg - receive packet-oriented message 790 * @iocb: (unused) 791 * @m: descriptor for message info 792 * @buf_len: total size of user buffer area 793 * @flags: receive flags 794 * 795 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. 796 * If the complete message doesn't fit in user area, truncate it. 797 * 798 * Returns size of returned message data, errno otherwise 799 */ 800 801 static int recv_msg(struct kiocb *iocb, struct socket *sock, 802 struct msghdr *m, size_t buf_len, int flags) 803 { 804 struct tipc_sock *tsock = tipc_sk(sock->sk); 805 struct sk_buff *buf; 806 struct tipc_msg *msg; 807 unsigned int q_len; 808 unsigned int sz; 809 u32 err; 810 int res; 811 812 /* Currently doesn't support receiving into multiple iovec entries */ 813 814 if (m->msg_iovlen != 1) 815 return -EOPNOTSUPP; 816 817 /* Catch invalid receive attempts */ 818 819 if (unlikely(!buf_len)) 820 return -EINVAL; 821 822 if (sock->type == SOCK_SEQPACKET) { 823 if (unlikely(sock->state == SS_UNCONNECTED)) 824 return -ENOTCONN; 825 if (unlikely((sock->state == SS_DISCONNECTING) && 826 (skb_queue_len(&sock->sk->sk_receive_queue) == 0))) 827 return -ENOTCONN; 828 } 829 830 /* Look for a message in receive queue; wait if necessary */ 831 832 if (unlikely(down_interruptible(&tsock->sem))) 833 return -ERESTARTSYS; 834 835 restart: 836 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && 837 (flags & MSG_DONTWAIT))) { 838 res = -EWOULDBLOCK; 839 goto exit; 840 } 841 842 if ((res = wait_event_interruptible( 843 *sock->sk->sk_sleep, 844 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || 845 (sock->state == SS_DISCONNECTING))) )) { 846 goto exit; 847 } 848 849 /* Catch attempt to receive on an already terminated connection */ 850 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ 851 852 if (!q_len) { 853 res = -ENOTCONN; 854 goto exit; 855 } 856 857 /* Get access to first message in receive queue */ 858 859 buf = skb_peek(&sock->sk->sk_receive_queue); 860 msg = buf_msg(buf); 861 sz = msg_data_sz(msg); 862 err = msg_errcode(msg); 863 864 /* Complete connection setup for an implied connect */ 865 866 if (unlikely(sock->state == SS_CONNECTING)) { 867 if ((res = auto_connect(sock, tsock, msg))) 868 goto exit; 869 } 870 871 /* Discard an empty non-errored message & try again */ 872 873 if ((!sz) && (!err)) { 874 advance_queue(tsock); 875 goto restart; 876 } 877 878 /* Capture sender's address (optional) */ 879 880 set_orig_addr(m, msg); 881 882 /* Capture ancillary data (optional) */ 883 884 if ((res = anc_data_recv(m, msg, tsock->p))) 885 goto exit; 886 887 /* Capture message data (if valid) & compute return value (always) */ 888 889 if (!err) { 890 if (unlikely(buf_len < sz)) { 891 sz = buf_len; 892 m->msg_flags |= MSG_TRUNC; 893 } 894 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg), 895 sz))) { 896 res = -EFAULT; 897 goto exit; 898 } 899 res = sz; 900 } else { 901 if ((sock->state == SS_READY) || 902 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) 903 res = 0; 904 else 905 res = -ECONNRESET; 906 } 907 908 /* Consume received message (optional) */ 909 910 if (likely(!(flags & MSG_PEEK))) { 911 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) 912 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); 913 advance_queue(tsock); 914 } 915 exit: 916 up(&tsock->sem); 917 return res; 918 } 919 920 /** 921 * recv_stream - receive stream-oriented data 922 * @iocb: (unused) 923 * @m: descriptor for message info 924 * @buf_len: total size of user buffer area 925 * @flags: receive flags 926 * 927 * Used for SOCK_STREAM messages only. If not enough data is available 928 * will optionally wait for more; never truncates data. 929 * 930 * Returns size of returned message data, errno otherwise 931 */ 932 933 static int recv_stream(struct kiocb *iocb, struct socket *sock, 934 struct msghdr *m, size_t buf_len, int flags) 935 { 936 struct tipc_sock *tsock = tipc_sk(sock->sk); 937 struct sk_buff *buf; 938 struct tipc_msg *msg; 939 unsigned int q_len; 940 unsigned int sz; 941 int sz_to_copy; 942 int sz_copied = 0; 943 int needed; 944 char *crs = m->msg_iov->iov_base; 945 unsigned char *buf_crs; 946 u32 err; 947 int res; 948 949 /* Currently doesn't support receiving into multiple iovec entries */ 950 951 if (m->msg_iovlen != 1) 952 return -EOPNOTSUPP; 953 954 /* Catch invalid receive attempts */ 955 956 if (unlikely(!buf_len)) 957 return -EINVAL; 958 959 if (unlikely(sock->state == SS_DISCONNECTING)) { 960 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0) 961 return -ENOTCONN; 962 } else if (unlikely(sock->state != SS_CONNECTED)) 963 return -ENOTCONN; 964 965 /* Look for a message in receive queue; wait if necessary */ 966 967 if (unlikely(down_interruptible(&tsock->sem))) 968 return -ERESTARTSYS; 969 970 restart: 971 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && 972 (flags & MSG_DONTWAIT))) { 973 res = -EWOULDBLOCK; 974 goto exit; 975 } 976 977 if ((res = wait_event_interruptible( 978 *sock->sk->sk_sleep, 979 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || 980 (sock->state == SS_DISCONNECTING))) )) { 981 goto exit; 982 } 983 984 /* Catch attempt to receive on an already terminated connection */ 985 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ 986 987 if (!q_len) { 988 res = -ENOTCONN; 989 goto exit; 990 } 991 992 /* Get access to first message in receive queue */ 993 994 buf = skb_peek(&sock->sk->sk_receive_queue); 995 msg = buf_msg(buf); 996 sz = msg_data_sz(msg); 997 err = msg_errcode(msg); 998 999 /* Discard an empty non-errored message & try again */ 1000 1001 if ((!sz) && (!err)) { 1002 advance_queue(tsock); 1003 goto restart; 1004 } 1005 1006 /* Optionally capture sender's address & ancillary data of first msg */ 1007 1008 if (sz_copied == 0) { 1009 set_orig_addr(m, msg); 1010 if ((res = anc_data_recv(m, msg, tsock->p))) 1011 goto exit; 1012 } 1013 1014 /* Capture message data (if valid) & compute return value (always) */ 1015 1016 if (!err) { 1017 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle); 1018 sz = buf->tail - buf_crs; 1019 1020 needed = (buf_len - sz_copied); 1021 sz_to_copy = (sz <= needed) ? sz : needed; 1022 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) { 1023 res = -EFAULT; 1024 goto exit; 1025 } 1026 sz_copied += sz_to_copy; 1027 1028 if (sz_to_copy < sz) { 1029 if (!(flags & MSG_PEEK)) 1030 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy; 1031 goto exit; 1032 } 1033 1034 crs += sz_to_copy; 1035 } else { 1036 if (sz_copied != 0) 1037 goto exit; /* can't add error msg to valid data */ 1038 1039 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) 1040 res = 0; 1041 else 1042 res = -ECONNRESET; 1043 } 1044 1045 /* Consume received message (optional) */ 1046 1047 if (likely(!(flags & MSG_PEEK))) { 1048 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) 1049 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); 1050 advance_queue(tsock); 1051 } 1052 1053 /* Loop around if more data is required */ 1054 1055 if ((sz_copied < buf_len) /* didn't get all requested data */ 1056 && (flags & MSG_WAITALL) /* ... and need to wait for more */ 1057 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */ 1058 && (!err) /* ... and haven't reached a FIN */ 1059 ) 1060 goto restart; 1061 1062 exit: 1063 up(&tsock->sem); 1064 return sz_copied ? sz_copied : res; 1065 } 1066 1067 /** 1068 * queue_overloaded - test if queue overload condition exists 1069 * @queue_size: current size of queue 1070 * @base: nominal maximum size of queue 1071 * @msg: message to be added to queue 1072 * 1073 * Returns 1 if queue is currently overloaded, 0 otherwise 1074 */ 1075 1076 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg) 1077 { 1078 u32 threshold; 1079 u32 imp = msg_importance(msg); 1080 1081 if (imp == TIPC_LOW_IMPORTANCE) 1082 threshold = base; 1083 else if (imp == TIPC_MEDIUM_IMPORTANCE) 1084 threshold = base * 2; 1085 else if (imp == TIPC_HIGH_IMPORTANCE) 1086 threshold = base * 100; 1087 else 1088 return 0; 1089 1090 if (msg_connected(msg)) 1091 threshold *= 4; 1092 1093 return (queue_size > threshold); 1094 } 1095 1096 /** 1097 * async_disconnect - wrapper function used to disconnect port 1098 * @portref: TIPC port reference (passed as pointer-sized value) 1099 */ 1100 1101 static void async_disconnect(unsigned long portref) 1102 { 1103 tipc_disconnect((u32)portref); 1104 } 1105 1106 /** 1107 * dispatch - handle arriving message 1108 * @tport: TIPC port that received message 1109 * @buf: message 1110 * 1111 * Called with port locked. Must not take socket lock to avoid deadlock risk. 1112 * 1113 * Returns TIPC error status code (TIPC_OK if message is not to be rejected) 1114 */ 1115 1116 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) 1117 { 1118 struct tipc_msg *msg = buf_msg(buf); 1119 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; 1120 struct socket *sock; 1121 u32 recv_q_len; 1122 1123 /* Reject message if socket is closing */ 1124 1125 if (!tsock) 1126 return TIPC_ERR_NO_PORT; 1127 1128 /* Reject message if it is wrong sort of message for socket */ 1129 1130 /* 1131 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD? 1132 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY 1133 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC 1134 */ 1135 sock = tsock->sk.sk_socket; 1136 if (sock->state == SS_READY) { 1137 if (msg_connected(msg)) { 1138 msg_dbg(msg, "dispatch filter 1\n"); 1139 return TIPC_ERR_NO_PORT; 1140 } 1141 } else { 1142 if (msg_mcast(msg)) { 1143 msg_dbg(msg, "dispatch filter 2\n"); 1144 return TIPC_ERR_NO_PORT; 1145 } 1146 if (sock->state == SS_CONNECTED) { 1147 if (!msg_connected(msg)) { 1148 msg_dbg(msg, "dispatch filter 3\n"); 1149 return TIPC_ERR_NO_PORT; 1150 } 1151 } 1152 else if (sock->state == SS_CONNECTING) { 1153 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) { 1154 msg_dbg(msg, "dispatch filter 4\n"); 1155 return TIPC_ERR_NO_PORT; 1156 } 1157 } 1158 else if (sock->state == SS_LISTENING) { 1159 if (msg_connected(msg) || msg_errcode(msg)) { 1160 msg_dbg(msg, "dispatch filter 5\n"); 1161 return TIPC_ERR_NO_PORT; 1162 } 1163 } 1164 else if (sock->state == SS_DISCONNECTING) { 1165 msg_dbg(msg, "dispatch filter 6\n"); 1166 return TIPC_ERR_NO_PORT; 1167 } 1168 else /* (sock->state == SS_UNCONNECTED) */ { 1169 if (msg_connected(msg) || msg_errcode(msg)) { 1170 msg_dbg(msg, "dispatch filter 7\n"); 1171 return TIPC_ERR_NO_PORT; 1172 } 1173 } 1174 } 1175 1176 /* Reject message if there isn't room to queue it */ 1177 1178 if (unlikely((u32)atomic_read(&tipc_queue_size) > 1179 OVERLOAD_LIMIT_BASE)) { 1180 if (queue_overloaded(atomic_read(&tipc_queue_size), 1181 OVERLOAD_LIMIT_BASE, msg)) 1182 return TIPC_ERR_OVERLOAD; 1183 } 1184 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue); 1185 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) { 1186 if (queue_overloaded(recv_q_len, 1187 OVERLOAD_LIMIT_BASE / 2, msg)) 1188 return TIPC_ERR_OVERLOAD; 1189 } 1190 1191 /* Initiate connection termination for an incoming 'FIN' */ 1192 1193 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { 1194 sock->state = SS_DISCONNECTING; 1195 /* Note: Use signal since port lock is already taken! */ 1196 tipc_k_signal((Handler)async_disconnect, tport->ref); 1197 } 1198 1199 /* Enqueue message (finally!) */ 1200 1201 msg_dbg(msg,"<DISP<: "); 1202 TIPC_SKB_CB(buf)->handle = msg_data(msg); 1203 atomic_inc(&tipc_queue_size); 1204 skb_queue_tail(&sock->sk->sk_receive_queue, buf); 1205 1206 wake_up_interruptible(sock->sk->sk_sleep); 1207 return TIPC_OK; 1208 } 1209 1210 /** 1211 * wakeupdispatch - wake up port after congestion 1212 * @tport: port to wakeup 1213 * 1214 * Called with port lock on. 1215 */ 1216 1217 static void wakeupdispatch(struct tipc_port *tport) 1218 { 1219 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; 1220 1221 wake_up_interruptible(tsock->sk.sk_sleep); 1222 } 1223 1224 /** 1225 * connect - establish a connection to another TIPC port 1226 * @sock: socket structure 1227 * @dest: socket address for destination port 1228 * @destlen: size of socket address data structure 1229 * @flags: (unused) 1230 * 1231 * Returns 0 on success, errno otherwise 1232 */ 1233 1234 static int connect(struct socket *sock, struct sockaddr *dest, int destlen, 1235 int flags) 1236 { 1237 struct tipc_sock *tsock = tipc_sk(sock->sk); 1238 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; 1239 struct msghdr m = {NULL,}; 1240 struct sk_buff *buf; 1241 struct tipc_msg *msg; 1242 int res; 1243 1244 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */ 1245 1246 if (sock->state == SS_READY) 1247 return -EOPNOTSUPP; 1248 1249 /* Issue Posix-compliant error code if socket is in the wrong state */ 1250 1251 if (sock->state == SS_LISTENING) 1252 return -EOPNOTSUPP; 1253 if (sock->state == SS_CONNECTING) 1254 return -EALREADY; 1255 if (sock->state != SS_UNCONNECTED) 1256 return -EISCONN; 1257 1258 /* 1259 * Reject connection attempt using multicast address 1260 * 1261 * Note: send_msg() validates the rest of the address fields, 1262 * so there's no need to do it here 1263 */ 1264 1265 if (dst->addrtype == TIPC_ADDR_MCAST) 1266 return -EINVAL; 1267 1268 /* Send a 'SYN-' to destination */ 1269 1270 m.msg_name = dest; 1271 m.msg_namelen = destlen; 1272 if ((res = send_msg(NULL, sock, &m, 0)) < 0) { 1273 sock->state = SS_DISCONNECTING; 1274 return res; 1275 } 1276 1277 if (down_interruptible(&tsock->sem)) 1278 return -ERESTARTSYS; 1279 1280 /* Wait for destination's 'ACK' response */ 1281 1282 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep, 1283 skb_queue_len(&sock->sk->sk_receive_queue), 1284 sock->sk->sk_rcvtimeo); 1285 buf = skb_peek(&sock->sk->sk_receive_queue); 1286 if (res > 0) { 1287 msg = buf_msg(buf); 1288 res = auto_connect(sock, tsock, msg); 1289 if (!res) { 1290 if (!msg_data_sz(msg)) 1291 advance_queue(tsock); 1292 } 1293 } else { 1294 if (res == 0) { 1295 res = -ETIMEDOUT; 1296 } else 1297 { /* leave "res" unchanged */ } 1298 sock->state = SS_DISCONNECTING; 1299 } 1300 1301 up(&tsock->sem); 1302 return res; 1303 } 1304 1305 /** 1306 * listen - allow socket to listen for incoming connections 1307 * @sock: socket structure 1308 * @len: (unused) 1309 * 1310 * Returns 0 on success, errno otherwise 1311 */ 1312 1313 static int listen(struct socket *sock, int len) 1314 { 1315 /* REQUIRES SOCKET LOCKING OF SOME SORT? */ 1316 1317 if (sock->state == SS_READY) 1318 return -EOPNOTSUPP; 1319 if (sock->state != SS_UNCONNECTED) 1320 return -EINVAL; 1321 sock->state = SS_LISTENING; 1322 return 0; 1323 } 1324 1325 /** 1326 * accept - wait for connection request 1327 * @sock: listening socket 1328 * @newsock: new socket that is to be connected 1329 * @flags: file-related flags associated with socket 1330 * 1331 * Returns 0 on success, errno otherwise 1332 */ 1333 1334 static int accept(struct socket *sock, struct socket *newsock, int flags) 1335 { 1336 struct tipc_sock *tsock = tipc_sk(sock->sk); 1337 struct sk_buff *buf; 1338 int res = -EFAULT; 1339 1340 if (sock->state == SS_READY) 1341 return -EOPNOTSUPP; 1342 if (sock->state != SS_LISTENING) 1343 return -EINVAL; 1344 1345 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && 1346 (flags & O_NONBLOCK))) 1347 return -EWOULDBLOCK; 1348 1349 if (down_interruptible(&tsock->sem)) 1350 return -ERESTARTSYS; 1351 1352 if (wait_event_interruptible(*sock->sk->sk_sleep, 1353 skb_queue_len(&sock->sk->sk_receive_queue))) { 1354 res = -ERESTARTSYS; 1355 goto exit; 1356 } 1357 buf = skb_peek(&sock->sk->sk_receive_queue); 1358 1359 res = tipc_create(newsock, 0); 1360 if (!res) { 1361 struct tipc_sock *new_tsock = tipc_sk(newsock->sk); 1362 struct tipc_portid id; 1363 struct tipc_msg *msg = buf_msg(buf); 1364 u32 new_ref = new_tsock->p->ref; 1365 1366 id.ref = msg_origport(msg); 1367 id.node = msg_orignode(msg); 1368 tipc_connect2port(new_ref, &id); 1369 newsock->state = SS_CONNECTED; 1370 1371 tipc_set_portimportance(new_ref, msg_importance(msg)); 1372 if (msg_named(msg)) { 1373 new_tsock->p->conn_type = msg_nametype(msg); 1374 new_tsock->p->conn_instance = msg_nameinst(msg); 1375 } 1376 1377 /* 1378 * Respond to 'SYN-' by discarding it & returning 'ACK'-. 1379 * Respond to 'SYN+' by queuing it on new socket. 1380 */ 1381 1382 msg_dbg(msg,"<ACC<: "); 1383 if (!msg_data_sz(msg)) { 1384 struct msghdr m = {NULL,}; 1385 1386 send_packet(NULL, newsock, &m, 0); 1387 advance_queue(tsock); 1388 } else { 1389 sock_lock(tsock); 1390 skb_dequeue(&sock->sk->sk_receive_queue); 1391 sock_unlock(tsock); 1392 skb_queue_head(&newsock->sk->sk_receive_queue, buf); 1393 } 1394 } 1395 exit: 1396 up(&tsock->sem); 1397 return res; 1398 } 1399 1400 /** 1401 * shutdown - shutdown socket connection 1402 * @sock: socket structure 1403 * @how: direction to close (unused; always treated as read + write) 1404 * 1405 * Terminates connection (if necessary), then purges socket's receive queue. 1406 * 1407 * Returns 0 on success, errno otherwise 1408 */ 1409 1410 static int shutdown(struct socket *sock, int how) 1411 { 1412 struct tipc_sock* tsock = tipc_sk(sock->sk); 1413 struct sk_buff *buf; 1414 int res; 1415 1416 /* Could return -EINVAL for an invalid "how", but why bother? */ 1417 1418 if (down_interruptible(&tsock->sem)) 1419 return -ERESTARTSYS; 1420 1421 sock_lock(tsock); 1422 1423 switch (sock->state) { 1424 case SS_CONNECTED: 1425 1426 /* Send 'FIN+' or 'FIN-' message to peer */ 1427 1428 sock_unlock(tsock); 1429 restart: 1430 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { 1431 atomic_dec(&tipc_queue_size); 1432 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { 1433 buf_discard(buf); 1434 goto restart; 1435 } 1436 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); 1437 } 1438 else { 1439 tipc_shutdown(tsock->p->ref); 1440 } 1441 sock_lock(tsock); 1442 1443 /* fall through */ 1444 1445 case SS_DISCONNECTING: 1446 1447 /* Discard any unreceived messages */ 1448 1449 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { 1450 atomic_dec(&tipc_queue_size); 1451 buf_discard(buf); 1452 } 1453 tsock->p->conn_unacked = 0; 1454 1455 /* fall through */ 1456 1457 case SS_CONNECTING: 1458 sock->state = SS_DISCONNECTING; 1459 res = 0; 1460 break; 1461 1462 default: 1463 res = -ENOTCONN; 1464 } 1465 1466 sock_unlock(tsock); 1467 1468 up(&tsock->sem); 1469 return res; 1470 } 1471 1472 /** 1473 * setsockopt - set socket option 1474 * @sock: socket structure 1475 * @lvl: option level 1476 * @opt: option identifier 1477 * @ov: pointer to new option value 1478 * @ol: length of option value 1479 * 1480 * For stream sockets only, accepts and ignores all IPPROTO_TCP options 1481 * (to ease compatibility). 1482 * 1483 * Returns 0 on success, errno otherwise 1484 */ 1485 1486 static int setsockopt(struct socket *sock, 1487 int lvl, int opt, char __user *ov, int ol) 1488 { 1489 struct tipc_sock *tsock = tipc_sk(sock->sk); 1490 u32 value; 1491 int res; 1492 1493 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 1494 return 0; 1495 if (lvl != SOL_TIPC) 1496 return -ENOPROTOOPT; 1497 if (ol < sizeof(value)) 1498 return -EINVAL; 1499 if ((res = get_user(value, (u32 *)ov))) 1500 return res; 1501 1502 if (down_interruptible(&tsock->sem)) 1503 return -ERESTARTSYS; 1504 1505 switch (opt) { 1506 case TIPC_IMPORTANCE: 1507 res = tipc_set_portimportance(tsock->p->ref, value); 1508 break; 1509 case TIPC_SRC_DROPPABLE: 1510 if (sock->type != SOCK_STREAM) 1511 res = tipc_set_portunreliable(tsock->p->ref, value); 1512 else 1513 res = -ENOPROTOOPT; 1514 break; 1515 case TIPC_DEST_DROPPABLE: 1516 res = tipc_set_portunreturnable(tsock->p->ref, value); 1517 break; 1518 case TIPC_CONN_TIMEOUT: 1519 sock->sk->sk_rcvtimeo = (value * HZ / 1000); 1520 break; 1521 default: 1522 res = -EINVAL; 1523 } 1524 1525 up(&tsock->sem); 1526 return res; 1527 } 1528 1529 /** 1530 * getsockopt - get socket option 1531 * @sock: socket structure 1532 * @lvl: option level 1533 * @opt: option identifier 1534 * @ov: receptacle for option value 1535 * @ol: receptacle for length of option value 1536 * 1537 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options 1538 * (to ease compatibility). 1539 * 1540 * Returns 0 on success, errno otherwise 1541 */ 1542 1543 static int getsockopt(struct socket *sock, 1544 int lvl, int opt, char __user *ov, int *ol) 1545 { 1546 struct tipc_sock *tsock = tipc_sk(sock->sk); 1547 int len; 1548 u32 value; 1549 int res; 1550 1551 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 1552 return put_user(0, ol); 1553 if (lvl != SOL_TIPC) 1554 return -ENOPROTOOPT; 1555 if ((res = get_user(len, ol))) 1556 return res; 1557 1558 if (down_interruptible(&tsock->sem)) 1559 return -ERESTARTSYS; 1560 1561 switch (opt) { 1562 case TIPC_IMPORTANCE: 1563 res = tipc_portimportance(tsock->p->ref, &value); 1564 break; 1565 case TIPC_SRC_DROPPABLE: 1566 res = tipc_portunreliable(tsock->p->ref, &value); 1567 break; 1568 case TIPC_DEST_DROPPABLE: 1569 res = tipc_portunreturnable(tsock->p->ref, &value); 1570 break; 1571 case TIPC_CONN_TIMEOUT: 1572 value = (sock->sk->sk_rcvtimeo * 1000) / HZ; 1573 break; 1574 default: 1575 res = -EINVAL; 1576 } 1577 1578 if (res) { 1579 /* "get" failed */ 1580 } 1581 else if (len < sizeof(value)) { 1582 res = -EINVAL; 1583 } 1584 else if ((res = copy_to_user(ov, &value, sizeof(value)))) { 1585 /* couldn't return value */ 1586 } 1587 else { 1588 res = put_user(sizeof(value), ol); 1589 } 1590 1591 up(&tsock->sem); 1592 return res; 1593 } 1594 1595 /** 1596 * Placeholders for non-implemented functionality 1597 * 1598 * Returns error code (POSIX-compliant where defined) 1599 */ 1600 1601 static int ioctl(struct socket *s, u32 cmd, unsigned long arg) 1602 { 1603 return -EINVAL; 1604 } 1605 1606 static int no_mmap(struct file *file, struct socket *sock, 1607 struct vm_area_struct *vma) 1608 { 1609 return -EINVAL; 1610 } 1611 static ssize_t no_sendpage(struct socket *sock, struct page *page, 1612 int offset, size_t size, int flags) 1613 { 1614 return -EINVAL; 1615 } 1616 1617 static int no_skpair(struct socket *s1, struct socket *s2) 1618 { 1619 return -EOPNOTSUPP; 1620 } 1621 1622 /** 1623 * Protocol switches for the various types of TIPC sockets 1624 */ 1625 1626 static struct proto_ops msg_ops = { 1627 .owner = THIS_MODULE, 1628 .family = AF_TIPC, 1629 .release = release, 1630 .bind = bind, 1631 .connect = connect, 1632 .socketpair = no_skpair, 1633 .accept = accept, 1634 .getname = get_name, 1635 .poll = poll, 1636 .ioctl = ioctl, 1637 .listen = listen, 1638 .shutdown = shutdown, 1639 .setsockopt = setsockopt, 1640 .getsockopt = getsockopt, 1641 .sendmsg = send_msg, 1642 .recvmsg = recv_msg, 1643 .mmap = no_mmap, 1644 .sendpage = no_sendpage 1645 }; 1646 1647 static struct proto_ops packet_ops = { 1648 .owner = THIS_MODULE, 1649 .family = AF_TIPC, 1650 .release = release, 1651 .bind = bind, 1652 .connect = connect, 1653 .socketpair = no_skpair, 1654 .accept = accept, 1655 .getname = get_name, 1656 .poll = poll, 1657 .ioctl = ioctl, 1658 .listen = listen, 1659 .shutdown = shutdown, 1660 .setsockopt = setsockopt, 1661 .getsockopt = getsockopt, 1662 .sendmsg = send_packet, 1663 .recvmsg = recv_msg, 1664 .mmap = no_mmap, 1665 .sendpage = no_sendpage 1666 }; 1667 1668 static struct proto_ops stream_ops = { 1669 .owner = THIS_MODULE, 1670 .family = AF_TIPC, 1671 .release = release, 1672 .bind = bind, 1673 .connect = connect, 1674 .socketpair = no_skpair, 1675 .accept = accept, 1676 .getname = get_name, 1677 .poll = poll, 1678 .ioctl = ioctl, 1679 .listen = listen, 1680 .shutdown = shutdown, 1681 .setsockopt = setsockopt, 1682 .getsockopt = getsockopt, 1683 .sendmsg = send_stream, 1684 .recvmsg = recv_stream, 1685 .mmap = no_mmap, 1686 .sendpage = no_sendpage 1687 }; 1688 1689 static struct net_proto_family tipc_family_ops = { 1690 .owner = THIS_MODULE, 1691 .family = AF_TIPC, 1692 .create = tipc_create 1693 }; 1694 1695 static struct proto tipc_proto = { 1696 .name = "TIPC", 1697 .owner = THIS_MODULE, 1698 .obj_size = sizeof(struct tipc_sock) 1699 }; 1700 1701 /** 1702 * tipc_socket_init - initialize TIPC socket interface 1703 * 1704 * Returns 0 on success, errno otherwise 1705 */ 1706 int tipc_socket_init(void) 1707 { 1708 int res; 1709 1710 res = proto_register(&tipc_proto, 1); 1711 if (res) { 1712 err("Failed to register TIPC protocol type\n"); 1713 goto out; 1714 } 1715 1716 res = sock_register(&tipc_family_ops); 1717 if (res) { 1718 err("Failed to register TIPC socket type\n"); 1719 proto_unregister(&tipc_proto); 1720 goto out; 1721 } 1722 1723 sockets_enabled = 1; 1724 out: 1725 return res; 1726 } 1727 1728 /** 1729 * tipc_socket_stop - stop TIPC socket interface 1730 */ 1731 void tipc_socket_stop(void) 1732 { 1733 if (!sockets_enabled) 1734 return; 1735 1736 sockets_enabled = 0; 1737 sock_unregister(tipc_family_ops.family); 1738 proto_unregister(&tipc_proto); 1739 } 1740 1741