PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.21.x/net/tipc/socket.c

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