PageRenderTime 97ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/net/tipc/socket.c

https://bitbucket.org/emiliolopez/linux
C | 3359 lines | 2373 code | 409 blank | 577 comment | 495 complexity | a0b933d91793237934454f97aea48547 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, 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/rhashtable.h>
  37. #include <linux/sched/signal.h>
  38. #include "core.h"
  39. #include "name_table.h"
  40. #include "node.h"
  41. #include "link.h"
  42. #include "name_distr.h"
  43. #include "socket.h"
  44. #include "bcast.h"
  45. #include "netlink.h"
  46. #include "group.h"
  47. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  48. #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
  49. #define TIPC_FWD_MSG 1
  50. #define TIPC_MAX_PORT 0xffffffff
  51. #define TIPC_MIN_PORT 1
  52. #define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
  53. enum {
  54. TIPC_LISTEN = TCP_LISTEN,
  55. TIPC_ESTABLISHED = TCP_ESTABLISHED,
  56. TIPC_OPEN = TCP_CLOSE,
  57. TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
  58. TIPC_CONNECTING = TCP_SYN_SENT,
  59. };
  60. struct sockaddr_pair {
  61. struct sockaddr_tipc sock;
  62. struct sockaddr_tipc member;
  63. };
  64. /**
  65. * struct tipc_sock - TIPC socket structure
  66. * @sk: socket - interacts with 'port' and with user via the socket API
  67. * @conn_type: TIPC type used when connection was established
  68. * @conn_instance: TIPC instance used when connection was established
  69. * @published: non-zero if port has one or more associated names
  70. * @max_pkt: maximum packet size "hint" used when building messages sent by port
  71. * @portid: unique port identity in TIPC socket hash table
  72. * @phdr: preformatted message header used when sending messages
  73. * #cong_links: list of congested links
  74. * @publications: list of publications for port
  75. * @blocking_link: address of the congested link we are currently sleeping on
  76. * @pub_count: total # of publications port has made during its lifetime
  77. * @probing_state:
  78. * @conn_timeout: the time we can wait for an unresponded setup request
  79. * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
  80. * @cong_link_cnt: number of congested links
  81. * @snt_unacked: # messages sent by socket, and not yet acked by peer
  82. * @rcv_unacked: # messages read by user, but not yet acked back to peer
  83. * @peer: 'connected' peer for dgram/rdm
  84. * @node: hash table node
  85. * @mc_method: cookie for use between socket and broadcast layer
  86. * @rcu: rcu struct for tipc_sock
  87. */
  88. struct tipc_sock {
  89. struct sock sk;
  90. u32 conn_type;
  91. u32 conn_instance;
  92. int published;
  93. u32 max_pkt;
  94. u32 portid;
  95. struct tipc_msg phdr;
  96. struct list_head cong_links;
  97. struct list_head publications;
  98. u32 pub_count;
  99. uint conn_timeout;
  100. atomic_t dupl_rcvcnt;
  101. bool probe_unacked;
  102. u16 cong_link_cnt;
  103. u16 snt_unacked;
  104. u16 snd_win;
  105. u16 peer_caps;
  106. u16 rcv_unacked;
  107. u16 rcv_win;
  108. struct sockaddr_tipc peer;
  109. struct rhash_head node;
  110. struct tipc_mc_method mc_method;
  111. struct rcu_head rcu;
  112. struct tipc_group *group;
  113. };
  114. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  115. static void tipc_data_ready(struct sock *sk);
  116. static void tipc_write_space(struct sock *sk);
  117. static void tipc_sock_destruct(struct sock *sk);
  118. static int tipc_release(struct socket *sock);
  119. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  120. bool kern);
  121. static void tipc_sk_timeout(struct timer_list *t);
  122. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  123. struct tipc_name_seq const *seq);
  124. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  125. struct tipc_name_seq const *seq);
  126. static int tipc_sk_leave(struct tipc_sock *tsk);
  127. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
  128. static int tipc_sk_insert(struct tipc_sock *tsk);
  129. static void tipc_sk_remove(struct tipc_sock *tsk);
  130. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
  131. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
  132. static const struct proto_ops packet_ops;
  133. static const struct proto_ops stream_ops;
  134. static const struct proto_ops msg_ops;
  135. static struct proto tipc_proto;
  136. static const struct rhashtable_params tsk_rht_params;
  137. static u32 tsk_own_node(struct tipc_sock *tsk)
  138. {
  139. return msg_prevnode(&tsk->phdr);
  140. }
  141. static u32 tsk_peer_node(struct tipc_sock *tsk)
  142. {
  143. return msg_destnode(&tsk->phdr);
  144. }
  145. static u32 tsk_peer_port(struct tipc_sock *tsk)
  146. {
  147. return msg_destport(&tsk->phdr);
  148. }
  149. static bool tsk_unreliable(struct tipc_sock *tsk)
  150. {
  151. return msg_src_droppable(&tsk->phdr) != 0;
  152. }
  153. static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
  154. {
  155. msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
  156. }
  157. static bool tsk_unreturnable(struct tipc_sock *tsk)
  158. {
  159. return msg_dest_droppable(&tsk->phdr) != 0;
  160. }
  161. static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
  162. {
  163. msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
  164. }
  165. static int tsk_importance(struct tipc_sock *tsk)
  166. {
  167. return msg_importance(&tsk->phdr);
  168. }
  169. static int tsk_set_importance(struct tipc_sock *tsk, int imp)
  170. {
  171. if (imp > TIPC_CRITICAL_IMPORTANCE)
  172. return -EINVAL;
  173. msg_set_importance(&tsk->phdr, (u32)imp);
  174. return 0;
  175. }
  176. static struct tipc_sock *tipc_sk(const struct sock *sk)
  177. {
  178. return container_of(sk, struct tipc_sock, sk);
  179. }
  180. static bool tsk_conn_cong(struct tipc_sock *tsk)
  181. {
  182. return tsk->snt_unacked > tsk->snd_win;
  183. }
  184. static u16 tsk_blocks(int len)
  185. {
  186. return ((len / FLOWCTL_BLK_SZ) + 1);
  187. }
  188. /* tsk_blocks(): translate a buffer size in bytes to number of
  189. * advertisable blocks, taking into account the ratio truesize(len)/len
  190. * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
  191. */
  192. static u16 tsk_adv_blocks(int len)
  193. {
  194. return len / FLOWCTL_BLK_SZ / 4;
  195. }
  196. /* tsk_inc(): increment counter for sent or received data
  197. * - If block based flow control is not supported by peer we
  198. * fall back to message based ditto, incrementing the counter
  199. */
  200. static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
  201. {
  202. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  203. return ((msglen / FLOWCTL_BLK_SZ) + 1);
  204. return 1;
  205. }
  206. /**
  207. * tsk_advance_rx_queue - discard first buffer in socket receive queue
  208. *
  209. * Caller must hold socket lock
  210. */
  211. static void tsk_advance_rx_queue(struct sock *sk)
  212. {
  213. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  214. }
  215. /* tipc_sk_respond() : send response message back to sender
  216. */
  217. static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
  218. {
  219. u32 selector;
  220. u32 dnode;
  221. u32 onode = tipc_own_addr(sock_net(sk));
  222. if (!tipc_msg_reverse(onode, &skb, err))
  223. return;
  224. dnode = msg_destnode(buf_msg(skb));
  225. selector = msg_origport(buf_msg(skb));
  226. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  227. }
  228. /**
  229. * tsk_rej_rx_queue - reject all buffers in socket receive queue
  230. *
  231. * Caller must hold socket lock
  232. */
  233. static void tsk_rej_rx_queue(struct sock *sk)
  234. {
  235. struct sk_buff *skb;
  236. while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
  237. tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
  238. }
  239. static bool tipc_sk_connected(struct sock *sk)
  240. {
  241. return sk->sk_state == TIPC_ESTABLISHED;
  242. }
  243. /* tipc_sk_type_connectionless - check if the socket is datagram socket
  244. * @sk: socket
  245. *
  246. * Returns true if connection less, false otherwise
  247. */
  248. static bool tipc_sk_type_connectionless(struct sock *sk)
  249. {
  250. return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
  251. }
  252. /* tsk_peer_msg - verify if message was sent by connected port's peer
  253. *
  254. * Handles cases where the node's network address has changed from
  255. * the default of <0.0.0> to its configured setting.
  256. */
  257. static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
  258. {
  259. struct sock *sk = &tsk->sk;
  260. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  261. u32 peer_port = tsk_peer_port(tsk);
  262. u32 orig_node;
  263. u32 peer_node;
  264. if (unlikely(!tipc_sk_connected(sk)))
  265. return false;
  266. if (unlikely(msg_origport(msg) != peer_port))
  267. return false;
  268. orig_node = msg_orignode(msg);
  269. peer_node = tsk_peer_node(tsk);
  270. if (likely(orig_node == peer_node))
  271. return true;
  272. if (!orig_node && (peer_node == tn->own_addr))
  273. return true;
  274. if (!peer_node && (orig_node == tn->own_addr))
  275. return true;
  276. return false;
  277. }
  278. /* tipc_set_sk_state - set the sk_state of the socket
  279. * @sk: socket
  280. *
  281. * Caller must hold socket lock
  282. *
  283. * Returns 0 on success, errno otherwise
  284. */
  285. static int tipc_set_sk_state(struct sock *sk, int state)
  286. {
  287. int oldsk_state = sk->sk_state;
  288. int res = -EINVAL;
  289. switch (state) {
  290. case TIPC_OPEN:
  291. res = 0;
  292. break;
  293. case TIPC_LISTEN:
  294. case TIPC_CONNECTING:
  295. if (oldsk_state == TIPC_OPEN)
  296. res = 0;
  297. break;
  298. case TIPC_ESTABLISHED:
  299. if (oldsk_state == TIPC_CONNECTING ||
  300. oldsk_state == TIPC_OPEN)
  301. res = 0;
  302. break;
  303. case TIPC_DISCONNECTING:
  304. if (oldsk_state == TIPC_CONNECTING ||
  305. oldsk_state == TIPC_ESTABLISHED)
  306. res = 0;
  307. break;
  308. }
  309. if (!res)
  310. sk->sk_state = state;
  311. return res;
  312. }
  313. static int tipc_sk_sock_err(struct socket *sock, long *timeout)
  314. {
  315. struct sock *sk = sock->sk;
  316. int err = sock_error(sk);
  317. int typ = sock->type;
  318. if (err)
  319. return err;
  320. if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
  321. if (sk->sk_state == TIPC_DISCONNECTING)
  322. return -EPIPE;
  323. else if (!tipc_sk_connected(sk))
  324. return -ENOTCONN;
  325. }
  326. if (!*timeout)
  327. return -EAGAIN;
  328. if (signal_pending(current))
  329. return sock_intr_errno(*timeout);
  330. return 0;
  331. }
  332. #define tipc_wait_for_cond(sock_, timeo_, condition_) \
  333. ({ \
  334. struct sock *sk_; \
  335. int rc_; \
  336. \
  337. while ((rc_ = !(condition_))) { \
  338. DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
  339. sk_ = (sock_)->sk; \
  340. rc_ = tipc_sk_sock_err((sock_), timeo_); \
  341. if (rc_) \
  342. break; \
  343. prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
  344. release_sock(sk_); \
  345. *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
  346. sched_annotate_sleep(); \
  347. lock_sock(sk_); \
  348. remove_wait_queue(sk_sleep(sk_), &wait_); \
  349. } \
  350. rc_; \
  351. })
  352. /**
  353. * tipc_sk_create - create a TIPC socket
  354. * @net: network namespace (must be default network)
  355. * @sock: pre-allocated socket structure
  356. * @protocol: protocol indicator (must be 0)
  357. * @kern: caused by kernel or by userspace?
  358. *
  359. * This routine creates additional data structures used by the TIPC socket,
  360. * initializes them, and links them together.
  361. *
  362. * Returns 0 on success, errno otherwise
  363. */
  364. static int tipc_sk_create(struct net *net, struct socket *sock,
  365. int protocol, int kern)
  366. {
  367. struct tipc_net *tn;
  368. const struct proto_ops *ops;
  369. struct sock *sk;
  370. struct tipc_sock *tsk;
  371. struct tipc_msg *msg;
  372. /* Validate arguments */
  373. if (unlikely(protocol != 0))
  374. return -EPROTONOSUPPORT;
  375. switch (sock->type) {
  376. case SOCK_STREAM:
  377. ops = &stream_ops;
  378. break;
  379. case SOCK_SEQPACKET:
  380. ops = &packet_ops;
  381. break;
  382. case SOCK_DGRAM:
  383. case SOCK_RDM:
  384. ops = &msg_ops;
  385. break;
  386. default:
  387. return -EPROTOTYPE;
  388. }
  389. /* Allocate socket's protocol area */
  390. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
  391. if (sk == NULL)
  392. return -ENOMEM;
  393. tsk = tipc_sk(sk);
  394. tsk->max_pkt = MAX_PKT_DEFAULT;
  395. INIT_LIST_HEAD(&tsk->publications);
  396. INIT_LIST_HEAD(&tsk->cong_links);
  397. msg = &tsk->phdr;
  398. tn = net_generic(sock_net(sk), tipc_net_id);
  399. /* Finish initializing socket data structures */
  400. sock->ops = ops;
  401. sock_init_data(sock, sk);
  402. tipc_set_sk_state(sk, TIPC_OPEN);
  403. if (tipc_sk_insert(tsk)) {
  404. pr_warn("Socket create failed; port number exhausted\n");
  405. return -EINVAL;
  406. }
  407. /* Ensure tsk is visible before we read own_addr. */
  408. smp_mb();
  409. tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
  410. NAMED_H_SIZE, 0);
  411. msg_set_origport(msg, tsk->portid);
  412. timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
  413. sk->sk_shutdown = 0;
  414. sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
  415. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  416. sk->sk_data_ready = tipc_data_ready;
  417. sk->sk_write_space = tipc_write_space;
  418. sk->sk_destruct = tipc_sock_destruct;
  419. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  420. atomic_set(&tsk->dupl_rcvcnt, 0);
  421. /* Start out with safe limits until we receive an advertised window */
  422. tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
  423. tsk->rcv_win = tsk->snd_win;
  424. if (tipc_sk_type_connectionless(sk)) {
  425. tsk_set_unreturnable(tsk, true);
  426. if (sock->type == SOCK_DGRAM)
  427. tsk_set_unreliable(tsk, true);
  428. }
  429. return 0;
  430. }
  431. static void tipc_sk_callback(struct rcu_head *head)
  432. {
  433. struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
  434. sock_put(&tsk->sk);
  435. }
  436. /* Caller should hold socket lock for the socket. */
  437. static void __tipc_shutdown(struct socket *sock, int error)
  438. {
  439. struct sock *sk = sock->sk;
  440. struct tipc_sock *tsk = tipc_sk(sk);
  441. struct net *net = sock_net(sk);
  442. long timeout = CONN_TIMEOUT_DEFAULT;
  443. u32 dnode = tsk_peer_node(tsk);
  444. struct sk_buff *skb;
  445. /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
  446. tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
  447. !tsk_conn_cong(tsk)));
  448. /* Reject all unreceived messages, except on an active connection
  449. * (which disconnects locally & sends a 'FIN+' to peer).
  450. */
  451. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  452. if (TIPC_SKB_CB(skb)->bytes_read) {
  453. kfree_skb(skb);
  454. continue;
  455. }
  456. if (!tipc_sk_type_connectionless(sk) &&
  457. sk->sk_state != TIPC_DISCONNECTING) {
  458. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  459. tipc_node_remove_conn(net, dnode, tsk->portid);
  460. }
  461. tipc_sk_respond(sk, skb, error);
  462. }
  463. if (tipc_sk_type_connectionless(sk))
  464. return;
  465. if (sk->sk_state != TIPC_DISCONNECTING) {
  466. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  467. TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
  468. tsk_own_node(tsk), tsk_peer_port(tsk),
  469. tsk->portid, error);
  470. if (skb)
  471. tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
  472. tipc_node_remove_conn(net, dnode, tsk->portid);
  473. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  474. }
  475. }
  476. /**
  477. * tipc_release - destroy a TIPC socket
  478. * @sock: socket to destroy
  479. *
  480. * This routine cleans up any messages that are still queued on the socket.
  481. * For DGRAM and RDM socket types, all queued messages are rejected.
  482. * For SEQPACKET and STREAM socket types, the first message is rejected
  483. * and any others are discarded. (If the first message on a STREAM socket
  484. * is partially-read, it is discarded and the next one is rejected instead.)
  485. *
  486. * NOTE: Rejected messages are not necessarily returned to the sender! They
  487. * are returned or discarded according to the "destination droppable" setting
  488. * specified for the message by the sender.
  489. *
  490. * Returns 0 on success, errno otherwise
  491. */
  492. static int tipc_release(struct socket *sock)
  493. {
  494. struct sock *sk = sock->sk;
  495. struct tipc_sock *tsk;
  496. /*
  497. * Exit if socket isn't fully initialized (occurs when a failed accept()
  498. * releases a pre-allocated child socket that was never used)
  499. */
  500. if (sk == NULL)
  501. return 0;
  502. tsk = tipc_sk(sk);
  503. lock_sock(sk);
  504. __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
  505. sk->sk_shutdown = SHUTDOWN_MASK;
  506. tipc_sk_leave(tsk);
  507. tipc_sk_withdraw(tsk, 0, NULL);
  508. sk_stop_timer(sk, &sk->sk_timer);
  509. tipc_sk_remove(tsk);
  510. /* Reject any messages that accumulated in backlog queue */
  511. release_sock(sk);
  512. tipc_dest_list_purge(&tsk->cong_links);
  513. tsk->cong_link_cnt = 0;
  514. call_rcu(&tsk->rcu, tipc_sk_callback);
  515. sock->sk = NULL;
  516. return 0;
  517. }
  518. /**
  519. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  520. * @sock: socket structure
  521. * @uaddr: socket address describing name(s) and desired operation
  522. * @uaddr_len: size of socket address data structure
  523. *
  524. * Name and name sequence binding is indicated using a positive scope value;
  525. * a negative scope value unbinds the specified name. Specifying no name
  526. * (i.e. a socket address length of 0) unbinds all names from the socket.
  527. *
  528. * Returns 0 on success, errno otherwise
  529. *
  530. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  531. * access any non-constant socket information.
  532. */
  533. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  534. int uaddr_len)
  535. {
  536. struct sock *sk = sock->sk;
  537. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  538. struct tipc_sock *tsk = tipc_sk(sk);
  539. int res = -EINVAL;
  540. lock_sock(sk);
  541. if (unlikely(!uaddr_len)) {
  542. res = tipc_sk_withdraw(tsk, 0, NULL);
  543. goto exit;
  544. }
  545. if (tsk->group) {
  546. res = -EACCES;
  547. goto exit;
  548. }
  549. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  550. res = -EINVAL;
  551. goto exit;
  552. }
  553. if (addr->family != AF_TIPC) {
  554. res = -EAFNOSUPPORT;
  555. goto exit;
  556. }
  557. if (addr->addrtype == TIPC_ADDR_NAME)
  558. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  559. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  560. res = -EAFNOSUPPORT;
  561. goto exit;
  562. }
  563. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  564. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  565. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  566. res = -EACCES;
  567. goto exit;
  568. }
  569. res = (addr->scope > 0) ?
  570. tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
  571. tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
  572. exit:
  573. release_sock(sk);
  574. return res;
  575. }
  576. /**
  577. * tipc_getname - get port ID of socket or peer socket
  578. * @sock: socket structure
  579. * @uaddr: area for returned socket address
  580. * @uaddr_len: area for returned length of socket address
  581. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  582. *
  583. * Returns 0 on success, errno otherwise
  584. *
  585. * NOTE: This routine doesn't need to take the socket lock since it only
  586. * accesses socket information that is unchanging (or which changes in
  587. * a completely predictable manner).
  588. */
  589. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  590. int *uaddr_len, int peer)
  591. {
  592. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  593. struct sock *sk = sock->sk;
  594. struct tipc_sock *tsk = tipc_sk(sk);
  595. struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
  596. memset(addr, 0, sizeof(*addr));
  597. if (peer) {
  598. if ((!tipc_sk_connected(sk)) &&
  599. ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
  600. return -ENOTCONN;
  601. addr->addr.id.ref = tsk_peer_port(tsk);
  602. addr->addr.id.node = tsk_peer_node(tsk);
  603. } else {
  604. addr->addr.id.ref = tsk->portid;
  605. addr->addr.id.node = tn->own_addr;
  606. }
  607. *uaddr_len = sizeof(*addr);
  608. addr->addrtype = TIPC_ADDR_ID;
  609. addr->family = AF_TIPC;
  610. addr->scope = 0;
  611. addr->addr.name.domain = 0;
  612. return 0;
  613. }
  614. /**
  615. * tipc_poll - read and possibly block on pollmask
  616. * @file: file structure associated with the socket
  617. * @sock: socket for which to calculate the poll bits
  618. * @wait: ???
  619. *
  620. * Returns pollmask value
  621. *
  622. * COMMENTARY:
  623. * It appears that the usual socket locking mechanisms are not useful here
  624. * since the pollmask info is potentially out-of-date the moment this routine
  625. * exits. TCP and other protocols seem to rely on higher level poll routines
  626. * to handle any preventable race conditions, so TIPC will do the same ...
  627. *
  628. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  629. * imply that the operation will succeed, merely that it should be performed
  630. * and will not block.
  631. */
  632. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  633. poll_table *wait)
  634. {
  635. struct sock *sk = sock->sk;
  636. struct tipc_sock *tsk = tipc_sk(sk);
  637. struct tipc_group *grp = tsk->group;
  638. u32 revents = 0;
  639. sock_poll_wait(file, sk_sleep(sk), wait);
  640. if (sk->sk_shutdown & RCV_SHUTDOWN)
  641. revents |= POLLRDHUP | POLLIN | POLLRDNORM;
  642. if (sk->sk_shutdown == SHUTDOWN_MASK)
  643. revents |= POLLHUP;
  644. switch (sk->sk_state) {
  645. case TIPC_ESTABLISHED:
  646. if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
  647. revents |= POLLOUT;
  648. /* fall thru' */
  649. case TIPC_LISTEN:
  650. case TIPC_CONNECTING:
  651. if (!skb_queue_empty(&sk->sk_receive_queue))
  652. revents |= POLLIN | POLLRDNORM;
  653. break;
  654. case TIPC_OPEN:
  655. if (!grp || tipc_group_size(grp))
  656. if (!tsk->cong_link_cnt)
  657. revents |= POLLOUT;
  658. if (!tipc_sk_type_connectionless(sk))
  659. break;
  660. if (skb_queue_empty(&sk->sk_receive_queue))
  661. break;
  662. revents |= POLLIN | POLLRDNORM;
  663. break;
  664. case TIPC_DISCONNECTING:
  665. revents = POLLIN | POLLRDNORM | POLLHUP;
  666. break;
  667. }
  668. return revents;
  669. }
  670. /**
  671. * tipc_sendmcast - send multicast message
  672. * @sock: socket structure
  673. * @seq: destination address
  674. * @msg: message to send
  675. * @dlen: length of data to send
  676. * @timeout: timeout to wait for wakeup
  677. *
  678. * Called from function tipc_sendmsg(), which has done all sanity checks
  679. * Returns the number of bytes sent on success, or errno
  680. */
  681. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  682. struct msghdr *msg, size_t dlen, long timeout)
  683. {
  684. struct sock *sk = sock->sk;
  685. struct tipc_sock *tsk = tipc_sk(sk);
  686. struct tipc_msg *hdr = &tsk->phdr;
  687. struct net *net = sock_net(sk);
  688. int mtu = tipc_bcast_get_mtu(net);
  689. struct tipc_mc_method *method = &tsk->mc_method;
  690. u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
  691. struct sk_buff_head pkts;
  692. struct tipc_nlist dsts;
  693. int rc;
  694. if (tsk->group)
  695. return -EACCES;
  696. /* Block or return if any destination link is congested */
  697. rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
  698. if (unlikely(rc))
  699. return rc;
  700. /* Lookup destination nodes */
  701. tipc_nlist_init(&dsts, tipc_own_addr(net));
  702. tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
  703. seq->upper, domain, &dsts);
  704. if (!dsts.local && !dsts.remote)
  705. return -EHOSTUNREACH;
  706. /* Build message header */
  707. msg_set_type(hdr, TIPC_MCAST_MSG);
  708. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  709. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  710. msg_set_destport(hdr, 0);
  711. msg_set_destnode(hdr, 0);
  712. msg_set_nametype(hdr, seq->type);
  713. msg_set_namelower(hdr, seq->lower);
  714. msg_set_nameupper(hdr, seq->upper);
  715. /* Build message as chain of buffers */
  716. skb_queue_head_init(&pkts);
  717. rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
  718. /* Send message if build was successful */
  719. if (unlikely(rc == dlen))
  720. rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
  721. &tsk->cong_link_cnt);
  722. tipc_nlist_purge(&dsts);
  723. return rc ? rc : dlen;
  724. }
  725. /**
  726. * tipc_send_group_msg - send a message to a member in the group
  727. * @net: network namespace
  728. * @m: message to send
  729. * @mb: group member
  730. * @dnode: destination node
  731. * @dport: destination port
  732. * @dlen: total length of message data
  733. */
  734. static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
  735. struct msghdr *m, struct tipc_member *mb,
  736. u32 dnode, u32 dport, int dlen)
  737. {
  738. u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
  739. struct tipc_mc_method *method = &tsk->mc_method;
  740. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  741. struct tipc_msg *hdr = &tsk->phdr;
  742. struct sk_buff_head pkts;
  743. int mtu, rc;
  744. /* Complete message header */
  745. msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
  746. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  747. msg_set_destport(hdr, dport);
  748. msg_set_destnode(hdr, dnode);
  749. msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
  750. /* Build message as chain of buffers */
  751. skb_queue_head_init(&pkts);
  752. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  753. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  754. if (unlikely(rc != dlen))
  755. return rc;
  756. /* Send message */
  757. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  758. if (unlikely(rc == -ELINKCONG)) {
  759. tipc_dest_push(&tsk->cong_links, dnode, 0);
  760. tsk->cong_link_cnt++;
  761. }
  762. /* Update send window */
  763. tipc_group_update_member(mb, blks);
  764. /* A broadcast sent within next EXPIRE period must follow same path */
  765. method->rcast = true;
  766. method->mandatory = true;
  767. return dlen;
  768. }
  769. /**
  770. * tipc_send_group_unicast - send message to a member in the group
  771. * @sock: socket structure
  772. * @m: message to send
  773. * @dlen: total length of message data
  774. * @timeout: timeout to wait for wakeup
  775. *
  776. * Called from function tipc_sendmsg(), which has done all sanity checks
  777. * Returns the number of bytes sent on success, or errno
  778. */
  779. static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
  780. int dlen, long timeout)
  781. {
  782. struct sock *sk = sock->sk;
  783. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  784. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  785. struct tipc_sock *tsk = tipc_sk(sk);
  786. struct tipc_group *grp = tsk->group;
  787. struct net *net = sock_net(sk);
  788. struct tipc_member *mb = NULL;
  789. u32 node, port;
  790. int rc;
  791. node = dest->addr.id.node;
  792. port = dest->addr.id.ref;
  793. if (!port && !node)
  794. return -EHOSTUNREACH;
  795. /* Block or return if destination link or member is congested */
  796. rc = tipc_wait_for_cond(sock, &timeout,
  797. !tipc_dest_find(&tsk->cong_links, node, 0) &&
  798. !tipc_group_cong(grp, node, port, blks, &mb));
  799. if (unlikely(rc))
  800. return rc;
  801. if (unlikely(!mb))
  802. return -EHOSTUNREACH;
  803. rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
  804. return rc ? rc : dlen;
  805. }
  806. /**
  807. * tipc_send_group_anycast - send message to any member with given identity
  808. * @sock: socket structure
  809. * @m: message to send
  810. * @dlen: total length of message data
  811. * @timeout: timeout to wait for wakeup
  812. *
  813. * Called from function tipc_sendmsg(), which has done all sanity checks
  814. * Returns the number of bytes sent on success, or errno
  815. */
  816. static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
  817. int dlen, long timeout)
  818. {
  819. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  820. struct sock *sk = sock->sk;
  821. struct tipc_sock *tsk = tipc_sk(sk);
  822. struct list_head *cong_links = &tsk->cong_links;
  823. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  824. struct tipc_group *grp = tsk->group;
  825. struct tipc_member *first = NULL;
  826. struct tipc_member *mbr = NULL;
  827. struct net *net = sock_net(sk);
  828. u32 node, port, exclude;
  829. u32 type, inst, domain;
  830. struct list_head dsts;
  831. int lookups = 0;
  832. int dstcnt, rc;
  833. bool cong;
  834. INIT_LIST_HEAD(&dsts);
  835. type = dest->addr.name.name.type;
  836. inst = dest->addr.name.name.instance;
  837. domain = addr_domain(net, dest->scope);
  838. exclude = tipc_group_exclude(grp);
  839. while (++lookups < 4) {
  840. first = NULL;
  841. /* Look for a non-congested destination member, if any */
  842. while (1) {
  843. if (!tipc_nametbl_lookup(net, type, inst, domain, &dsts,
  844. &dstcnt, exclude, false))
  845. return -EHOSTUNREACH;
  846. tipc_dest_pop(&dsts, &node, &port);
  847. cong = tipc_group_cong(grp, node, port, blks, &mbr);
  848. if (!cong)
  849. break;
  850. if (mbr == first)
  851. break;
  852. if (!first)
  853. first = mbr;
  854. }
  855. /* Start over if destination was not in member list */
  856. if (unlikely(!mbr))
  857. continue;
  858. if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
  859. break;
  860. /* Block or return if destination link or member is congested */
  861. rc = tipc_wait_for_cond(sock, &timeout,
  862. !tipc_dest_find(cong_links, node, 0) &&
  863. !tipc_group_cong(grp, node, port,
  864. blks, &mbr));
  865. if (unlikely(rc))
  866. return rc;
  867. /* Send, unless destination disappeared while waiting */
  868. if (likely(mbr))
  869. break;
  870. }
  871. if (unlikely(lookups >= 4))
  872. return -EHOSTUNREACH;
  873. rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
  874. return rc ? rc : dlen;
  875. }
  876. /**
  877. * tipc_send_group_bcast - send message to all members in communication group
  878. * @sk: socket structure
  879. * @m: message to send
  880. * @dlen: total length of message data
  881. * @timeout: timeout to wait for wakeup
  882. *
  883. * Called from function tipc_sendmsg(), which has done all sanity checks
  884. * Returns the number of bytes sent on success, or errno
  885. */
  886. static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
  887. int dlen, long timeout)
  888. {
  889. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  890. struct sock *sk = sock->sk;
  891. struct net *net = sock_net(sk);
  892. struct tipc_sock *tsk = tipc_sk(sk);
  893. struct tipc_group *grp = tsk->group;
  894. struct tipc_nlist *dsts = tipc_group_dests(grp);
  895. struct tipc_mc_method *method = &tsk->mc_method;
  896. bool ack = method->mandatory && method->rcast;
  897. int blks = tsk_blocks(MCAST_H_SIZE + dlen);
  898. struct tipc_msg *hdr = &tsk->phdr;
  899. int mtu = tipc_bcast_get_mtu(net);
  900. struct sk_buff_head pkts;
  901. int rc = -EHOSTUNREACH;
  902. if (!dsts->local && !dsts->remote)
  903. return -EHOSTUNREACH;
  904. /* Block or return if any destination link or member is congested */
  905. rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt &&
  906. !tipc_group_bc_cong(grp, blks));
  907. if (unlikely(rc))
  908. return rc;
  909. /* Complete message header */
  910. if (dest) {
  911. msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
  912. msg_set_nameinst(hdr, dest->addr.name.name.instance);
  913. } else {
  914. msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
  915. msg_set_nameinst(hdr, 0);
  916. }
  917. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  918. msg_set_destport(hdr, 0);
  919. msg_set_destnode(hdr, 0);
  920. msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(grp));
  921. /* Avoid getting stuck with repeated forced replicasts */
  922. msg_set_grp_bc_ack_req(hdr, ack);
  923. /* Build message as chain of buffers */
  924. skb_queue_head_init(&pkts);
  925. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  926. if (unlikely(rc != dlen))
  927. return rc;
  928. /* Send message */
  929. rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
  930. if (unlikely(rc))
  931. return rc;
  932. /* Update broadcast sequence number and send windows */
  933. tipc_group_update_bc_members(tsk->group, blks, ack);
  934. /* Broadcast link is now free to choose method for next broadcast */
  935. method->mandatory = false;
  936. method->expires = jiffies;
  937. return dlen;
  938. }
  939. /**
  940. * tipc_send_group_mcast - send message to all members with given identity
  941. * @sock: socket structure
  942. * @m: message to send
  943. * @dlen: total length of message data
  944. * @timeout: timeout to wait for wakeup
  945. *
  946. * Called from function tipc_sendmsg(), which has done all sanity checks
  947. * Returns the number of bytes sent on success, or errno
  948. */
  949. static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
  950. int dlen, long timeout)
  951. {
  952. struct sock *sk = sock->sk;
  953. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  954. struct tipc_name_seq *seq = &dest->addr.nameseq;
  955. struct tipc_sock *tsk = tipc_sk(sk);
  956. struct tipc_group *grp = tsk->group;
  957. struct net *net = sock_net(sk);
  958. u32 domain, exclude, dstcnt;
  959. struct list_head dsts;
  960. INIT_LIST_HEAD(&dsts);
  961. if (seq->lower != seq->upper)
  962. return -ENOTSUPP;
  963. domain = addr_domain(net, dest->scope);
  964. exclude = tipc_group_exclude(grp);
  965. if (!tipc_nametbl_lookup(net, seq->type, seq->lower, domain,
  966. &dsts, &dstcnt, exclude, true))
  967. return -EHOSTUNREACH;
  968. if (dstcnt == 1) {
  969. tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
  970. return tipc_send_group_unicast(sock, m, dlen, timeout);
  971. }
  972. tipc_dest_list_purge(&dsts);
  973. return tipc_send_group_bcast(sock, m, dlen, timeout);
  974. }
  975. /**
  976. * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
  977. * @arrvq: queue with arriving messages, to be cloned after destination lookup
  978. * @inputq: queue with cloned messages, delivered to socket after dest lookup
  979. *
  980. * Multi-threaded: parallel calls with reference to same queues may occur
  981. */
  982. void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
  983. struct sk_buff_head *inputq)
  984. {
  985. u32 scope = TIPC_CLUSTER_SCOPE;
  986. u32 self = tipc_own_addr(net);
  987. struct sk_buff *skb, *_skb;
  988. u32 lower = 0, upper = ~0;
  989. struct sk_buff_head tmpq;
  990. u32 portid, oport, onode;
  991. struct list_head dports;
  992. struct tipc_msg *msg;
  993. int user, mtyp, hsz;
  994. __skb_queue_head_init(&tmpq);
  995. INIT_LIST_HEAD(&dports);
  996. skb = tipc_skb_peek(arrvq, &inputq->lock);
  997. for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
  998. msg = buf_msg(skb);
  999. user = msg_user(msg);
  1000. mtyp = msg_type(msg);
  1001. if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
  1002. spin_lock_bh(&inputq->lock);
  1003. if (skb_peek(arrvq) == skb) {
  1004. __skb_dequeue(arrvq);
  1005. __skb_queue_tail(inputq, skb);
  1006. }
  1007. refcount_dec(&skb->users);
  1008. spin_unlock_bh(&inputq->lock);
  1009. continue;
  1010. }
  1011. hsz = skb_headroom(skb) + msg_hdr_sz(msg);
  1012. oport = msg_origport(msg);
  1013. onode = msg_orignode(msg);
  1014. if (onode == self)
  1015. scope = TIPC_NODE_SCOPE;
  1016. /* Create destination port list and message clones: */
  1017. if (!msg_in_group(msg)) {
  1018. lower = msg_namelower(msg);
  1019. upper = msg_nameupper(msg);
  1020. }
  1021. tipc_nametbl_mc_translate(net, msg_nametype(msg), lower, upper,
  1022. scope, &dports);
  1023. while (tipc_dest_pop(&dports, NULL, &portid)) {
  1024. _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
  1025. if (_skb) {
  1026. msg_set_destport(buf_msg(_skb), portid);
  1027. __skb_queue_tail(&tmpq, _skb);
  1028. continue;
  1029. }
  1030. pr_warn("Failed to clone mcast rcv buffer\n");
  1031. }
  1032. /* Append to inputq if not already done by other thread */
  1033. spin_lock_bh(&inputq->lock);
  1034. if (skb_peek(arrvq) == skb) {
  1035. skb_queue_splice_tail_init(&tmpq, inputq);
  1036. kfree_skb(__skb_dequeue(arrvq));
  1037. }
  1038. spin_unlock_bh(&inputq->lock);
  1039. __skb_queue_purge(&tmpq);
  1040. kfree_skb(skb);
  1041. }
  1042. tipc_sk_rcv(net, inputq);
  1043. }
  1044. /**
  1045. * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
  1046. * @tsk: receiving socket
  1047. * @skb: pointer to message buffer.
  1048. */
  1049. static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
  1050. struct sk_buff_head *xmitq)
  1051. {
  1052. struct tipc_msg *hdr = buf_msg(skb);
  1053. u32 onode = tsk_own_node(tsk);
  1054. struct sock *sk = &tsk->sk;
  1055. int mtyp = msg_type(hdr);
  1056. bool conn_cong;
  1057. /* Ignore if connection cannot be validated: */
  1058. if (!tsk_peer_msg(tsk, hdr))
  1059. goto exit;
  1060. if (unlikely(msg_errcode(hdr))) {
  1061. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1062. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1063. tsk_peer_port(tsk));
  1064. sk->sk_state_change(sk);
  1065. goto exit;
  1066. }
  1067. tsk->probe_unacked = false;
  1068. if (mtyp == CONN_PROBE) {
  1069. msg_set_type(hdr, CONN_PROBE_REPLY);
  1070. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  1071. __skb_queue_tail(xmitq, skb);
  1072. return;
  1073. } else if (mtyp == CONN_ACK) {
  1074. conn_cong = tsk_conn_cong(tsk);
  1075. tsk->snt_unacked -= msg_conn_ack(hdr);
  1076. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1077. tsk->snd_win = msg_adv_win(hdr);
  1078. if (conn_cong)
  1079. sk->sk_write_space(sk);
  1080. } else if (mtyp != CONN_PROBE_REPLY) {
  1081. pr_warn("Received unknown CONN_PROTO msg\n");
  1082. }
  1083. exit:
  1084. kfree_skb(skb);
  1085. }
  1086. /**
  1087. * tipc_sendmsg - send message in connectionless manner
  1088. * @sock: socket structure
  1089. * @m: message to send
  1090. * @dsz: amount of user data to be sent
  1091. *
  1092. * Message must have an destination specified explicitly.
  1093. * Used for SOCK_RDM and SOCK_DGRAM messages,
  1094. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  1095. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  1096. *
  1097. * Returns the number of bytes sent on success, or errno otherwise
  1098. */
  1099. static int tipc_sendmsg(struct socket *sock,
  1100. struct msghdr *m, size_t dsz)
  1101. {
  1102. struct sock *sk = sock->sk;
  1103. int ret;
  1104. lock_sock(sk);
  1105. ret = __tipc_sendmsg(sock, m, dsz);
  1106. release_sock(sk);
  1107. return ret;
  1108. }
  1109. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
  1110. {
  1111. struct sock *sk = sock->sk;
  1112. struct net *net = sock_net(sk);
  1113. struct tipc_sock *tsk = tipc_sk(sk);
  1114. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1115. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1116. struct list_head *clinks = &tsk->cong_links;
  1117. bool syn = !tipc_sk_type_connectionless(sk);
  1118. struct tipc_group *grp = tsk->group;
  1119. struct tipc_msg *hdr = &tsk->phdr;
  1120. struct tipc_name_seq *seq;
  1121. struct sk_buff_head pkts;
  1122. u32 type, inst, domain;
  1123. u32 dnode, dport;
  1124. int mtu, rc;
  1125. if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
  1126. return -EMSGSIZE;
  1127. if (likely(dest)) {
  1128. if (unlikely(m->msg_namelen < sizeof(*dest)))
  1129. return -EINVAL;
  1130. if (unlikely(dest->family != AF_TIPC))
  1131. return -EINVAL;
  1132. }
  1133. if (grp) {
  1134. if (!dest)
  1135. return tipc_send_group_bcast(sock, m, dlen, timeout);
  1136. if (dest->addrtype == TIPC_ADDR_NAME)
  1137. return tipc_send_group_anycast(sock, m, dlen, timeout);
  1138. if (dest->addrtype == TIPC_ADDR_ID)
  1139. return tipc_send_group_unicast(sock, m, dlen, timeout);
  1140. if (dest->addrtype == TIPC_ADDR_MCAST)
  1141. return tipc_send_group_mcast(sock, m, dlen, timeout);
  1142. return -EINVAL;
  1143. }
  1144. if (unlikely(!dest)) {
  1145. dest = &tsk->peer;
  1146. if (!syn || dest->family != AF_TIPC)
  1147. return -EDESTADDRREQ;
  1148. }
  1149. if (unlikely(syn)) {
  1150. if (sk->sk_state == TIPC_LISTEN)
  1151. return -EPIPE;
  1152. if (sk->sk_state != TIPC_OPEN)
  1153. return -EISCONN;
  1154. if (tsk->published)
  1155. return -EOPNOTSUPP;
  1156. if (dest->addrtype == TIPC_ADDR_NAME) {
  1157. tsk->conn_type = dest->addr.name.name.type;
  1158. tsk->conn_instance = dest->addr.name.name.instance;
  1159. }
  1160. }
  1161. seq = &dest->addr.nameseq;
  1162. if (dest->addrtype == TIPC_ADDR_MCAST)
  1163. return tipc_sendmcast(sock, seq, m, dlen, timeout);
  1164. if (dest->addrtype == TIPC_ADDR_NAME) {
  1165. type = dest->addr.name.name.type;
  1166. inst = dest->addr.name.name.instance;
  1167. domain = dest->addr.name.domain;
  1168. dnode = domain;
  1169. msg_set_type(hdr, TIPC_NAMED_MSG);
  1170. msg_set_hdr_sz(hdr, NAMED_H_SIZE);
  1171. msg_set_nametype(hdr, type);
  1172. msg_set_nameinst(hdr, inst);
  1173. msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
  1174. dport = tipc_nametbl_translate(net, type, inst, &dnode);
  1175. msg_set_destnode(hdr, dnode);
  1176. msg_set_destport(hdr, dport);
  1177. if (unlikely(!dport && !dnode))
  1178. return -EHOSTUNREACH;
  1179. } else if (dest->addrtype == TIPC_ADDR_ID) {
  1180. dnode = dest->addr.id.node;
  1181. msg_set_type(hdr, TIPC_DIRECT_MSG);
  1182. msg_set_lookup_scope(hdr, 0);
  1183. msg_set_destnode(hdr, dnode);
  1184. msg_set_destport(hdr, dest->addr.id.ref);
  1185. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1186. }
  1187. /* Block or return if destination link is congested */
  1188. rc = tipc_wait_for_cond(sock, &timeout,
  1189. !tipc_dest_find(clinks, dnode, 0));
  1190. if (unlikely(rc))
  1191. return rc;
  1192. skb_queue_head_init(&pkts);
  1193. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  1194. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  1195. if (unlikely(rc != dlen))
  1196. return rc;
  1197. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1198. if (unlikely(rc == -ELINKCONG)) {
  1199. tipc_dest_push(clinks, dnode, 0);
  1200. tsk->cong_link_cnt++;
  1201. rc = 0;
  1202. }
  1203. if (unlikely(syn && !rc))
  1204. tipc_set_sk_state(sk, TIPC_CONNECTING);
  1205. return rc ? rc : dlen;
  1206. }
  1207. /**
  1208. * tipc_sendstream - send stream-oriented data
  1209. * @sock: socket structure
  1210. * @m: data to send
  1211. * @dsz: total length of data to be transmitted
  1212. *
  1213. * Used for SOCK_STREAM data.
  1214. *
  1215. * Returns the number of bytes sent on success (or partial success),
  1216. * or errno if no data sent
  1217. */
  1218. static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
  1219. {
  1220. struct sock *sk = sock->sk;
  1221. int ret;
  1222. lock_sock(sk);
  1223. ret = __tipc_sendstream(sock, m, dsz);
  1224. release_sock(sk);
  1225. return ret;
  1226. }
  1227. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
  1228. {
  1229. struct sock *sk = sock->sk;
  1230. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1231. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1232. struct tipc_sock *tsk = tipc_sk(sk);
  1233. struct tipc_msg *hdr = &tsk->phdr;
  1234. struct net *net = sock_net(sk);
  1235. struct sk_buff_head pkts;
  1236. u32 dnode = tsk_peer_node(tsk);
  1237. int send, sent = 0;
  1238. int rc = 0;
  1239. skb_queue_head_init(&pkts);
  1240. if (unlikely(dlen > INT_MAX))
  1241. return -EMSGSIZE;
  1242. /* Handle implicit connection setup */
  1243. if (unlikely(dest)) {
  1244. rc = __tipc_sendmsg(sock, m, dlen);
  1245. if (dlen && (dlen == rc))
  1246. tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
  1247. return rc;
  1248. }
  1249. do {
  1250. rc = tipc_wait_for_cond(sock, &timeout,
  1251. (!tsk->cong_link_cnt &&
  1252. !tsk_conn_cong(tsk) &&
  1253. tipc_sk_connected(sk)));
  1254. if (unlikely(rc))
  1255. break;
  1256. send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
  1257. rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
  1258. if (unlikely(rc != send))
  1259. break;
  1260. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1261. if (unlikely(rc == -ELINKCONG)) {
  1262. tsk->cong_link_cnt = 1;
  1263. rc = 0;
  1264. }
  1265. if (likely(!rc)) {
  1266. tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
  1267. sent += send;
  1268. }
  1269. } while (sent < dlen && !rc);
  1270. return sent ? sent : rc;
  1271. }
  1272. /**
  1273. * tipc_send_packet - send a connection-oriented message
  1274. * @sock: socket structure
  1275. * @m: message to send
  1276. * @dsz: length of data to be transmitted
  1277. *
  1278. * Used for SOCK_SEQPACKET messages.
  1279. *
  1280. * Returns the number of bytes sent on success, or errno otherwise
  1281. */
  1282. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  1283. {
  1284. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  1285. return -EMSGSIZE;
  1286. return tipc_sendstream(sock, m, dsz);
  1287. }
  1288. /* tipc_sk_finish_conn - complete the setup of a connection
  1289. */
  1290. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  1291. u32 peer_node)
  1292. {
  1293. struct sock *sk = &tsk->sk;
  1294. struct net *net = sock_net(sk);
  1295. struct tipc_msg *msg = &tsk->phdr;
  1296. msg_set_destnode(msg, peer_node);
  1297. msg_set_destport(msg, peer_port);
  1298. msg_set_type(msg, TIPC_CONN_MSG);
  1299. msg_set_lookup_scope(msg, 0);
  1300. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1301. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  1302. tipc_set_sk_state(sk, TIPC_ESTABLISHED);
  1303. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1304. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  1305. tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
  1306. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1307. return;
  1308. /* Fall back to message based flow control */
  1309. tsk->rcv_win = FLOWCTL_MSG_WIN;
  1310. tsk->snd_win = FLOWCTL_MSG_WIN;
  1311. }
  1312. /**
  1313. * tipc_sk_set_orig_addr - capture sender's address for received message
  1314. * @m: descriptor for message info
  1315. * @hdr: received message header
  1316. *
  1317. * Note: Address is not captured if not requested by receiver.
  1318. */
  1319. static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
  1320. {
  1321. DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
  1322. struct tipc_msg *hdr = buf_msg(skb);
  1323. if (!srcaddr)
  1324. return;
  1325. srcaddr->sock.family = AF_TIPC;
  1326. srcaddr->sock.addrtype = TIPC_ADDR_ID;
  1327. srcaddr->sock.addr.id.ref = msg_origport(hdr);
  1328. srcaddr->sock.addr.id.node = msg_orignode(hdr);
  1329. srcaddr->sock.addr.name.domain = 0;
  1330. srcaddr->sock.scope = 0;
  1331. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1332. if (!msg_in_group(hdr))
  1333. return;
  1334. /* Group message users may also want to know sending member's id */
  1335. srcaddr->member.family = AF_TIPC;
  1336. srcaddr->member.addrtype = TIPC_ADDR_NAME;
  1337. srcaddr->member.addr.name.name.type = msg_nametype(hdr);
  1338. srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
  1339. srcaddr->member.addr.name.domain = 0;
  1340. m->msg_namelen = sizeof(*srcaddr);
  1341. }
  1342. /**
  1343. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1344. * @m: descriptor for message info
  1345. * @msg: received message header
  1346. * @tsk: TIPC port associated with message
  1347. *
  1348. * Note: Ancillary data is not captured if not requested by receiver.
  1349. *
  1350. * Returns 0 if successful, otherwise errno
  1351. */
  1352. static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  1353. struct tipc_sock *tsk)
  1354. {
  1355. u32 anc_data[3];
  1356. u32 err;
  1357. u32 dest_type;
  1358. int has_name;
  1359. int res;
  1360. if (likely(m->msg_controllen == 0))
  1361. return 0;
  1362. /* Optionally capture errored message object(s) */
  1363. err = msg ? msg_errcode(msg) : 0;
  1364. if (unlikely(err)) {
  1365. anc_data[0] = err;
  1366. anc_data[1] = msg_data_sz(msg);
  1367. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1368. if (res)
  1369. return res;
  1370. if (anc_data[1]) {
  1371. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1372. msg_data(msg));
  1373. if (res)
  1374. return res;
  1375. }
  1376. }
  1377. /* Optionally capture message destination object */
  1378. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1379. switch (dest_type) {
  1380. case TIPC_NAMED_MSG:
  1381. has_name = 1;
  1382. anc_data[0] = msg_nametype(msg);
  1383. anc_data[1] = msg_namelower(msg);
  1384. anc_data[2] = msg_namelower(msg);
  1385. break;
  1386. case TIPC_MCAST_MSG:
  1387. has_name = 1;
  1388. anc_data[0] = msg_nametype(msg);
  1389. anc_data[1] = msg_namelower(msg);
  1390. anc_data[2] = msg_nameupper(msg);
  1391. break;
  1392. case TIPC_CONN_MSG:
  1393. has_name = (tsk->conn_type != 0);
  1394. anc_data[0] = tsk->conn_type;
  1395. anc_data[1] = tsk->conn_instance;
  1396. anc_data[2] = tsk->conn_instance;
  1397. break;
  1398. default:
  1399. has_name = 0;
  1400. }
  1401. if (has_name) {
  1402. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1403. if (res)
  1404. return res;
  1405. }
  1406. return 0;
  1407. }
  1408. static void tipc_sk_send_ack(struct tipc_sock *tsk)
  1409. {
  1410. struct sock *sk = &tsk->sk;
  1411. struct net *net = sock_net(sk);
  1412. struct sk_buff *skb = NULL;
  1413. struct tipc_msg *msg;
  1414. u32 peer_port = tsk_peer_port(tsk);
  1415. u32 dnode = tsk_peer_node(tsk);
  1416. if (!tipc_sk_connected(sk))
  1417. return;
  1418. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1419. dnode, tsk_own_node(tsk), peer_port,
  1420. tsk->portid, TIPC_OK);
  1421. if (!skb)
  1422. return;
  1423. msg = buf_msg(skb);
  1424. msg_set_conn_ack(msg, tsk->rcv_unacked);
  1425. tsk->rcv_unacked = 0;
  1426. /* Adjust to and advertize the correct window limit */
  1427. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
  1428. tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
  1429. msg_set_adv_win(msg, tsk->rcv_win);
  1430. }
  1431. tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1432. }
  1433. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1434. {
  1435. struct sock *sk = sock->sk;
  1436. DEFINE_WAIT(wait);
  1437. long timeo = *timeop;
  1438. int err = sock_error(sk);
  1439. if (err)
  1440. return err;
  1441. for (;;) {
  1442. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1443. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1444. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1445. err = -ENOTCONN;
  1446. break;
  1447. }
  1448. release_sock(sk);
  1449. timeo = schedule_timeout(timeo);
  1450. lock_sock(sk);
  1451. }
  1452. err = 0;
  1453. if (!skb_queue_empty(&sk->sk_receive_queue))
  1454. break;
  1455. err = -EAGAIN;
  1456. if (!timeo)
  1457. break;
  1458. err = sock_intr_errno(timeo);
  1459. if (signal_pending(current))
  1460. break;
  1461. err = sock_error(sk);
  1462. if (err)
  1463. break;
  1464. }
  1465. finish_wait(sk_sleep(sk), &wait);
  1466. *timeop = timeo;
  1467. return err;
  1468. }
  1469. /**
  1470. * tipc_recvmsg - receive packet-oriented message
  1471. * @m: descriptor for message info
  1472. * @buflen: length of user buffer area
  1473. * @flags: receive flags
  1474. *
  1475. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1476. * If the complete message doesn't fit in user area, truncate it.
  1477. *
  1478. * Returns size of returned message data, errno otherwise
  1479. */
  1480. static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
  1481. size_t buflen, int flags)
  1482. {
  1483. struct sock *sk = sock->sk;
  1484. bool connected = !tipc_sk_type_connectionless(sk);
  1485. struct tipc_sock *tsk = tipc_sk(sk);
  1486. int rc, err, hlen, dlen, copy;
  1487. struct sk_buff_head xmitq;
  1488. struct tipc_msg *hdr;
  1489. struct sk_buff *skb;
  1490. bool grp_evt;
  1491. long timeout;
  1492. /* Catch invalid receive requests */
  1493. if (unlikely(!buflen))
  1494. return -EINVAL;
  1495. lock_sock(sk);
  1496. if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
  1497. rc = -ENOTCONN;
  1498. goto exit;
  1499. }
  1500. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1501. /* Step rcv queue to first msg with data or error; wait if necessary */
  1502. do {
  1503. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1504. if (unlikely(rc))
  1505. goto exit;
  1506. skb = skb_peek(&sk->sk_receive_queue);
  1507. hdr = buf_msg(skb);
  1508. dlen = msg_data_sz(hdr);
  1509. hlen = msg_hdr_sz(hdr);
  1510. err = msg_errcode(hdr);
  1511. grp_evt = msg_is_grp_evt(hdr);
  1512. if (likely(dlen || err))
  1513. break;
  1514. tsk_advance_rx_queue(sk);
  1515. } while (1);
  1516. /* Collect msg meta data, including error code and rejected data */
  1517. tipc_sk_set_orig_addr(m, skb);
  1518. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1519. if (unlikely(rc))
  1520. goto exit;
  1521. /* Capture data if non-error msg, otherwise just set return value */
  1522. if (likely(!err)) {
  1523. copy = min_t(int, dlen, buflen);
  1524. if (unlikely(copy != dlen))
  1525. m->msg_flags |= MSG_TRUNC;
  1526. rc = skb_copy_datagram_msg(skb, hlen, m, copy);
  1527. } else {
  1528. copy = 0;
  1529. rc = 0;
  1530. if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
  1531. rc = -ECONNRESET;
  1532. }
  1533. if (unlikely(rc))
  1534. goto exit;
  1535. /* Mark message as group event if applicable */
  1536. if (unlikely(grp_evt)) {
  1537. if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
  1538. m->msg_flags |= MSG_EOR;
  1539. m->msg_flags |= MSG_OOB;
  1540. copy = 0;
  1541. }
  1542. /* Caption of data or error code/rejected data was successful */
  1543. if (unlikely(flags & MSG_PEEK))
  1544. goto exit;
  1545. /* Send group flow control advertisement when applicable */
  1546. if (tsk->group && msg_in_group(hdr) && !grp_evt) {
  1547. skb_queue_head_init(&xmitq);
  1548. tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
  1549. msg_orignode(hdr), msg_origport(hdr),
  1550. &xmitq);
  1551. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1552. }
  1553. tsk_advance_rx_queue(sk);
  1554. if (likely(!connected))
  1555. goto exit;
  1556. /* Send connection flow control advertisement when applicable */
  1557. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1558. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1559. tipc_sk_send_ack(tsk);
  1560. exit:
  1561. release_sock(sk);
  1562. return rc ? rc : copy;
  1563. }
  1564. /**
  1565. * tipc_recvstream - receive stream-oriented data
  1566. * @m: descriptor for message info
  1567. * @buflen: total size of user buffer area
  1568. * @flags: receive flags
  1569. *
  1570. * Used for SOCK_STREAM messages only. If not enough data is available
  1571. * will optionally wait for more; never truncates data.
  1572. *
  1573. * Returns size of returned message data, errno otherwise
  1574. */
  1575. static int tipc_recvstream(struct socket *sock, struct msghdr *m,
  1576. size_t buflen, int flags)
  1577. {
  1578. struct sock *sk = sock->sk;
  1579. struct tipc_sock *tsk = tipc_sk(sk);
  1580. struct sk_buff *skb;
  1581. struct tipc_msg *hdr;
  1582. struct tipc_skb_cb *skb_cb;
  1583. bool peek = flags & MSG_PEEK;
  1584. int offset, required, copy, copied = 0;
  1585. int hlen, dlen, err, rc;
  1586. long timeout;
  1587. /* Catch invalid receive attempts */
  1588. if (unlikely(!buflen))
  1589. return -EINVAL;
  1590. lock_sock(sk);
  1591. if (unlikely(sk->sk_state == TIPC_OPEN)) {
  1592. rc = -ENOTCONN;
  1593. goto exit;
  1594. }
  1595. required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
  1596. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1597. do {
  1598. /* Look at first msg in receive queue; wait if necessary */
  1599. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1600. if (unlikely(rc))
  1601. break;
  1602. skb = skb_peek(&sk->sk_receive_queue);
  1603. skb_cb = TIPC_SKB_CB(skb);
  1604. hdr = buf_msg(skb);
  1605. dlen = msg_data_sz(hdr);
  1606. hlen = msg_hdr_sz(hdr);
  1607. err = msg_errcode(hdr);
  1608. /* Discard any empty non-errored (SYN-) message */
  1609. if (unlikely(!dlen && !err)) {
  1610. tsk_advance_rx_queue(sk);
  1611. continue;
  1612. }
  1613. /* Collect msg meta data, incl. error code and rejected data */
  1614. if (!copied) {
  1615. tipc_sk_set_orig_addr(m, skb);
  1616. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1617. if (rc)
  1618. break;
  1619. }
  1620. /* Copy data if msg ok, otherwise return error/partial data */
  1621. if (likely(!err)) {
  1622. offset = skb_cb->bytes_read;
  1623. copy = min_t(int, dlen - offset, buflen - copied);
  1624. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1625. if (unlikely(rc))
  1626. break;
  1627. copied += copy;
  1628. offset += copy;
  1629. if (unlikely(offset < dlen)) {
  1630. if (!peek)
  1631. skb_cb->bytes_read = offset;
  1632. break;
  1633. }
  1634. } else {
  1635. rc = 0;
  1636. if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
  1637. rc = -ECONNRESET;
  1638. if (copied || rc)
  1639. break;
  1640. }
  1641. if (unlikely(peek))
  1642. break;
  1643. tsk_advance_rx_queue(sk);
  1644. /* Send connection flow control advertisement when applicable */
  1645. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1646. if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
  1647. tipc_sk_send_ack(tsk);
  1648. /* Exit if all requested data or FIN/error received */
  1649. if (copied == buflen || err)
  1650. break;
  1651. } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
  1652. exit:
  1653. release_sock(sk);
  1654. return copied ? copied : rc;
  1655. }
  1656. /**
  1657. * tipc_write_space - wake up thread if port congestion is released
  1658. * @sk: socket
  1659. */
  1660. static void tipc_write_space(struct sock *sk)
  1661. {
  1662. struct socket_wq *wq;
  1663. rcu_read_lock();
  1664. wq = rcu_dereference(sk->sk_wq);
  1665. if (skwq_has_sleeper(wq))
  1666. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1667. POLLWRNORM | POLLWRBAND);
  1668. rcu_read_unlock();
  1669. }
  1670. /**
  1671. * tipc_data_ready - wake up threads to indicate messages have been received
  1672. * @sk: socket
  1673. * @len: the length of messages
  1674. */
  1675. static void tipc_data_ready(struct sock *sk)
  1676. {
  1677. struct socket_wq *wq;
  1678. rcu_read_lock();
  1679. wq = rcu_dereference(sk->sk_wq);
  1680. if (skwq_has_sleeper(wq))
  1681. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1682. POLLRDNORM | POLLRDBAND);
  1683. rcu_read_unlock();
  1684. }
  1685. static void tipc_sock_destruct(struct sock *sk)
  1686. {
  1687. __skb_queue_purge(&sk->sk_receive_queue);
  1688. }
  1689. static void tipc_sk_proto_rcv(struct sock *sk,
  1690. struct sk_buff_head *inputq,
  1691. struct sk_buff_head *xmitq)
  1692. {
  1693. struct sk_buff *skb = __skb_dequeue(inputq);
  1694. struct tipc_sock *tsk = tipc_sk(sk);
  1695. struct tipc_msg *hdr = buf_msg(skb);
  1696. struct tipc_group *grp = tsk->group;
  1697. bool wakeup = false;
  1698. switch (msg_user(hdr)) {
  1699. case CONN_MANAGER:
  1700. tipc_sk_conn_proto_rcv(tsk, skb, xmitq);
  1701. return;
  1702. case SOCK_WAKEUP:
  1703. tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
  1704. tsk->cong_link_cnt--;
  1705. wakeup = true;
  1706. break;
  1707. case GROUP_PROTOCOL:
  1708. tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
  1709. break;
  1710. case TOP_SRV:
  1711. tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
  1712. skb, inputq, xmitq);
  1713. skb = NULL;
  1714. break;
  1715. default:
  1716. break;
  1717. }
  1718. if (wakeup)
  1719. sk->sk_write_space(sk);
  1720. kfree_skb(skb);
  1721. }
  1722. /**
  1723. * tipc_filter_connect - Handle incoming message for a connection-based socket
  1724. * @tsk: TIPC socket
  1725. * @skb: pointer to message buffer. Set to NULL if buffer is consumed
  1726. *
  1727. * Returns true if everything ok, false otherwise
  1728. */
  1729. static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
  1730. {
  1731. struct sock *sk = &tsk->sk;
  1732. struct net *net = sock_net(sk);
  1733. struct tipc_msg *hdr = buf_msg(skb);
  1734. u32 pport = msg_origport(hdr);
  1735. u32 pnode = msg_orignode(hdr);
  1736. if (unlikely(msg_mcast(hdr)))
  1737. return false;
  1738. switch (sk->sk_state) {
  1739. case TIPC_CONNECTING:
  1740. /* Accept only ACK or NACK message */
  1741. if (unlikely(!msg_connected(hdr))) {
  1742. if (pport != tsk_peer_port(tsk) ||
  1743. pnode != tsk_peer_node(tsk))
  1744. return false;
  1745. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1746. sk->sk_err = ECONNREFUSED;
  1747. sk->sk_state_change(sk);
  1748. return true;
  1749. }
  1750. if (unlikely(msg_errcode(hdr))) {
  1751. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1752. sk->sk_err = ECONNREFUSED;
  1753. sk->sk_state_change(sk);
  1754. return true;
  1755. }
  1756. if (unlikely(!msg_isdata(hdr))) {
  1757. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1758. sk->sk_err = EINVAL;
  1759. sk->sk_state_change(sk);
  1760. return true;
  1761. }
  1762. tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
  1763. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1764. /* If 'ACK+' message, add to socket receive queue */
  1765. if (msg_data_sz(hdr))
  1766. return true;
  1767. /* If empty 'ACK-' message, wake up sleeping connect() */
  1768. sk->sk_data_ready(sk);
  1769. /* 'ACK-' message is neither accepted nor rejected: */
  1770. msg_set_dest_droppable(hdr, 1);
  1771. return false;
  1772. case TIPC_OPEN:
  1773. case TIPC_DISCONNECTING:
  1774. break;
  1775. case TIPC_LISTEN:
  1776. /* Accept only SYN message */
  1777. if (!msg_connected(hdr) && !(msg_errcode(hdr)))
  1778. return true;
  1779. break;
  1780. case TIPC_ESTABLISHED:
  1781. /* Accept only connection-based messages sent by peer */
  1782. if (unlikely(!tsk_peer_msg(tsk, hdr)))
  1783. return false;
  1784. if (unlikely(msg_errcode(hdr))) {
  1785. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1786. /* Let timer expire on it's own */
  1787. tipc_node_remove_conn(net, tsk_peer_node(tsk),
  1788. tsk->portid);
  1789. sk->sk_state_change(sk);
  1790. }
  1791. return true;
  1792. default:
  1793. pr_err("Unknown sk_state %u\n", sk->sk_state);
  1794. }
  1795. return false;
  1796. }
  1797. /**
  1798. * rcvbuf_limit - get proper overload limit of socket receive queue
  1799. * @sk: socket
  1800. * @skb: message
  1801. *
  1802. * For connection oriented messages, irrespective of importance,
  1803. * default queue limit is 2 MB.
  1804. *
  1805. * For connectionless messages, queue limits are based on message
  1806. * importance as follows:
  1807. *
  1808. * TIPC_LOW_IMPORTANCE (2 MB)
  1809. * TIPC_MEDIUM_IMPORTANCE (4 MB)
  1810. * TIPC_HIGH_IMPORTANCE (8 MB)
  1811. * TIPC_CRITICAL_IMPORTANCE (16 MB)
  1812. *
  1813. * Returns overload limit according to corresponding message importance
  1814. */
  1815. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
  1816. {
  1817. struct tipc_sock *tsk = tipc_sk(sk);
  1818. struct tipc_msg *hdr = buf_msg(skb);
  1819. if (unlikely(msg_in_group(hdr)))
  1820. return sk->sk_rcvbuf;
  1821. if (unlikely(!msg_connected(hdr)))
  1822. return sk->sk_rcvbuf << msg_importance(hdr);
  1823. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  1824. return sk->sk_rcvbuf;
  1825. return FLOWCTL_MSG_LIM;
  1826. }
  1827. /**
  1828. * tipc_sk_filter_rcv - validate incoming message
  1829. * @sk: socket
  1830. * @skb: pointer to message.
  1831. *
  1832. * Enqueues message on receive queue if acceptable; optionally handles
  1833. * disconnect indication for a connected socket.
  1834. *
  1835. * Called with socket lock already taken
  1836. *
  1837. */
  1838. static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
  1839. struct sk_buff_head *xmitq)
  1840. {
  1841. bool sk_conn = !tipc_sk_type_connectionless(sk);
  1842. struct tipc_sock *tsk = tipc_sk(sk);
  1843. struct tipc_group *grp = tsk->group;
  1844. struct tipc_msg *hdr = buf_msg(skb);
  1845. struct net *net = sock_net(sk);
  1846. struct sk_buff_head inputq;
  1847. int limit, err = TIPC_OK;
  1848. TIPC_SKB_CB(skb)->bytes_read = 0;
  1849. __skb_queue_head_init(&inputq);
  1850. __skb_queue_tail(&inputq, skb);
  1851. if (unlikely(!msg_isdata(hdr)))
  1852. tipc_sk_proto_rcv(sk, &inputq, xmitq);
  1853. if (unlikely(grp))
  1854. tipc_group_filter_msg(grp, &inputq, xmitq);
  1855. /* Validate and add to receive buffer if there is space */
  1856. while ((skb = __skb_dequeue(&inputq))) {
  1857. hdr = buf_msg(skb);
  1858. limit = rcvbuf_limit(sk, skb);
  1859. if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
  1860. (!sk_conn && msg_connected(hdr)) ||
  1861. (!grp && msg_in_group(hdr)))
  1862. err = TIPC_ERR_NO_PORT;
  1863. else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit)
  1864. err = TIPC_ERR_OVERLOAD;
  1865. if (unlikely(err)) {
  1866. tipc_skb_reject(net, err, skb, xmitq);
  1867. err = TIPC_OK;
  1868. continue;
  1869. }
  1870. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1871. skb_set_owner_r(skb, sk);
  1872. sk->sk_data_ready(sk);
  1873. }
  1874. }
  1875. /**
  1876. * tipc_sk_backlog_rcv - handle incoming message from backlog queue
  1877. * @sk: socket
  1878. * @skb: message
  1879. *
  1880. * Caller must hold socket lock
  1881. */
  1882. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1883. {
  1884. unsigned int before = sk_rmem_alloc_get(sk);
  1885. struct sk_buff_head xmitq;
  1886. unsigned int added;
  1887. __skb_queue_head_init(&xmitq);
  1888. tipc_sk_filter_rcv(sk, skb, &xmitq);
  1889. added = sk_rmem_alloc_get(sk) - before;
  1890. atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
  1891. /* Send pending response/rejected messages, if any */
  1892. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1893. return 0;
  1894. }
  1895. /**
  1896. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1897. * inputq and try adding them to socket or backlog queue
  1898. * @inputq: list of incoming buffers with potentially different destinations
  1899. * @sk: socket where the buffers should be enqueued
  1900. * @dport: port number for the socket
  1901. *
  1902. * Caller must hold socket lock
  1903. */
  1904. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1905. u32 dport, struct sk_buff_head *xmitq)
  1906. {
  1907. unsigned long time_limit = jiffies + 2;
  1908. struct sk_buff *skb;
  1909. unsigned int lim;
  1910. atomic_t *dcnt;
  1911. u32 onode;
  1912. while (skb_queue_len(inputq)) {
  1913. if (unlikely(time_after_eq(jiffies, time_limit)))
  1914. return;
  1915. skb = tipc_skb_dequeue(inputq, dport);
  1916. if (unlikely(!skb))
  1917. return;
  1918. /* Add message directly to receive queue if possible */
  1919. if (!sock_owned_by_user(sk)) {
  1920. tipc_sk_filter_rcv(sk, skb, xmitq);
  1921. continue;
  1922. }
  1923. /* Try backlog, compensating for double-counted bytes */
  1924. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1925. if (!sk->sk_backlog.len)
  1926. atomic_set(dcnt, 0);
  1927. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1928. if (likely(!sk_add_backlog(sk, skb, lim)))
  1929. continue;
  1930. /* Overload => reject message back to sender */
  1931. onode = tipc_own_addr(sock_net(sk));
  1932. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
  1933. __skb_queue_tail(xmitq, skb);
  1934. break;
  1935. }
  1936. }
  1937. /**
  1938. * tipc_sk_rcv - handle a chain of incoming buffers
  1939. * @inputq: buffer list containing the buffers
  1940. * Consumes all buffers in list until inputq is empty
  1941. * Note: may be called in multiple threads referring to the same queue
  1942. */
  1943. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1944. {
  1945. struct sk_buff_head xmitq;
  1946. u32 dnode, dport = 0;
  1947. int err;
  1948. struct tipc_sock *tsk;
  1949. struct sock *sk;
  1950. struct sk_buff *skb;
  1951. __skb_queue_head_init(&xmitq);
  1952. while (skb_queue_len(inputq)) {
  1953. dport = tipc_skb_peek_port(inputq, dport);
  1954. tsk = tipc_sk_lookup(net, dport);
  1955. if (likely(tsk)) {
  1956. sk = &tsk->sk;
  1957. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1958. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  1959. spin_unlock_bh(&sk->sk_lock.slock);
  1960. }
  1961. /* Send pending response/rejected messages, if any */
  1962. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1963. sock_put(sk);
  1964. continue;
  1965. }
  1966. /* No destination socket => dequeue skb if still there */
  1967. skb = tipc_skb_dequeue(inputq, dport);
  1968. if (!skb)
  1969. return;
  1970. /* Try secondary lookup if unresolved named message */
  1971. err = TIPC_ERR_NO_PORT;
  1972. if (tipc_msg_lookup_dest(net, skb, &err))
  1973. goto xmit;
  1974. /* Prepare for message rejection */
  1975. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  1976. continue;
  1977. xmit:
  1978. dnode = msg_destnode(buf_msg(skb));
  1979. tipc_node_xmit_skb(net, skb, dnode, dport);
  1980. }
  1981. }
  1982. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1983. {
  1984. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1985. struct sock *sk = sock->sk;
  1986. int done;
  1987. do {
  1988. int err = sock_error(sk);
  1989. if (err)
  1990. return err;
  1991. if (!*timeo_p)
  1992. return -ETIMEDOUT;
  1993. if (signal_pending(current))
  1994. return sock_intr_errno(*timeo_p);
  1995. add_wait_queue(sk_sleep(sk), &wait);
  1996. done = sk_wait_event(sk, timeo_p,
  1997. sk->sk_state != TIPC_CONNECTING, &wait);
  1998. remove_wait_queue(sk_sleep(sk), &wait);
  1999. } while (!done);
  2000. return 0;
  2001. }
  2002. /**
  2003. * tipc_connect - establish a connection to another TIPC port
  2004. * @sock: socket structure
  2005. * @dest: socket address for destination port
  2006. * @destlen: size of socket address data structure
  2007. * @flags: file-related flags associated with socket
  2008. *
  2009. * Returns 0 on success, errno otherwise
  2010. */
  2011. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  2012. int destlen, int flags)
  2013. {
  2014. struct sock *sk = sock->sk;
  2015. struct tipc_sock *tsk = tipc_sk(sk);
  2016. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  2017. struct msghdr m = {NULL,};
  2018. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  2019. int previous;
  2020. int res = 0;
  2021. if (destlen != sizeof(struct sockaddr_tipc))
  2022. return -EINVAL;
  2023. lock_sock(sk);
  2024. if (tsk->group) {
  2025. res = -EINVAL;
  2026. goto exit;
  2027. }
  2028. if (dst->family == AF_UNSPEC) {
  2029. memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
  2030. if (!tipc_sk_type_connectionless(sk))
  2031. res = -EINVAL;
  2032. goto exit;
  2033. } else if (dst->family != AF_TIPC) {
  2034. res = -EINVAL;
  2035. }
  2036. if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
  2037. res = -EINVAL;
  2038. if (res)
  2039. goto exit;
  2040. /* DGRAM/RDM connect(), just save the destaddr */
  2041. if (tipc_sk_type_connectionless(sk)) {
  2042. memcpy(&tsk->peer, dest, destlen);
  2043. goto exit;
  2044. }
  2045. previous = sk->sk_state;
  2046. switch (sk->sk_state) {
  2047. case TIPC_OPEN:
  2048. /* Send a 'SYN-' to destination */
  2049. m.msg_name = dest;
  2050. m.msg_namelen = destlen;
  2051. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  2052. * indicate send_msg() is never blocked.
  2053. */
  2054. if (!timeout)
  2055. m.msg_flags = MSG_DONTWAIT;
  2056. res = __tipc_sendmsg(sock, &m, 0);
  2057. if ((res < 0) && (res != -EWOULDBLOCK))
  2058. goto exit;
  2059. /* Just entered TIPC_CONNECTING state; the only
  2060. * difference is that return value in non-blocking
  2061. * case is EINPROGRESS, rather than EALREADY.
  2062. */
  2063. res = -EINPROGRESS;
  2064. /* fall thru' */
  2065. case TIPC_CONNECTING:
  2066. if (!timeout) {
  2067. if (previous == TIPC_CONNECTING)
  2068. res = -EALREADY;
  2069. goto exit;
  2070. }
  2071. timeout = msecs_to_jiffies(timeout);
  2072. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  2073. res = tipc_wait_for_connect(sock, &timeout);
  2074. break;
  2075. case TIPC_ESTABLISHED:
  2076. res = -EISCONN;
  2077. break;
  2078. default:
  2079. res = -EINVAL;
  2080. }
  2081. exit:
  2082. release_sock(sk);
  2083. return res;
  2084. }
  2085. /**
  2086. * tipc_listen - allow socket to listen for incoming connections
  2087. * @sock: socket structure
  2088. * @len: (unused)
  2089. *
  2090. * Returns 0 on success, errno otherwise
  2091. */
  2092. static int tipc_listen(struct socket *sock, int len)
  2093. {
  2094. struct sock *sk = sock->sk;
  2095. int res;
  2096. lock_sock(sk);
  2097. res = tipc_set_sk_state(sk, TIPC_LISTEN);
  2098. release_sock(sk);
  2099. return res;
  2100. }
  2101. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  2102. {
  2103. struct sock *sk = sock->sk;
  2104. DEFINE_WAIT(wait);
  2105. int err;
  2106. /* True wake-one mechanism for incoming connections: only
  2107. * one process gets woken up, not the 'whole herd'.
  2108. * Since we do not 'race & poll' for established sockets
  2109. * anymore, the common case will execute the loop only once.
  2110. */
  2111. for (;;) {
  2112. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  2113. TASK_INTERRUPTIBLE);
  2114. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  2115. release_sock(sk);
  2116. timeo = schedule_timeout(timeo);
  2117. lock_sock(sk);
  2118. }
  2119. err = 0;
  2120. if (!skb_queue_empty(&sk->sk_receive_queue))
  2121. break;
  2122. err = -EAGAIN;
  2123. if (!timeo)
  2124. break;
  2125. err = sock_intr_errno(timeo);
  2126. if (signal_pending(current))
  2127. break;
  2128. }
  2129. finish_wait(sk_sleep(sk), &wait);
  2130. return err;
  2131. }
  2132. /**
  2133. * tipc_accept - wait for connection request
  2134. * @sock: listening socket
  2135. * @newsock: new socket that is to be connected
  2136. * @flags: file-related flags associated with socket
  2137. *
  2138. * Returns 0 on success, errno otherwise
  2139. */
  2140. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  2141. bool kern)
  2142. {
  2143. struct sock *new_sk, *sk = sock->sk;
  2144. struct sk_buff *buf;
  2145. struct tipc_sock *new_tsock;
  2146. struct tipc_msg *msg;
  2147. long timeo;
  2148. int res;
  2149. lock_sock(sk);
  2150. if (sk->sk_state != TIPC_LISTEN) {
  2151. res = -EINVAL;
  2152. goto exit;
  2153. }
  2154. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  2155. res = tipc_wait_for_accept(sock, timeo);
  2156. if (res)
  2157. goto exit;
  2158. buf = skb_peek(&sk->sk_receive_queue);
  2159. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
  2160. if (res)
  2161. goto exit;
  2162. security_sk_clone(sock->sk, new_sock->sk);
  2163. new_sk = new_sock->sk;
  2164. new_tsock = tipc_sk(new_sk);
  2165. msg = buf_msg(buf);
  2166. /* we lock on new_sk; but lockdep sees the lock on sk */
  2167. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  2168. /*
  2169. * Reject any stray messages received by new socket
  2170. * before the socket lock was taken (very, very unlikely)
  2171. */
  2172. tsk_rej_rx_queue(new_sk);
  2173. /* Connect new socket to it's peer */
  2174. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  2175. tsk_set_importance(new_tsock, msg_importance(msg));
  2176. if (msg_named(msg)) {
  2177. new_tsock->conn_type = msg_nametype(msg);
  2178. new_tsock->conn_instance = msg_nameinst(msg);
  2179. }
  2180. /*
  2181. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  2182. * Respond to 'SYN+' by queuing it on new socket.
  2183. */
  2184. if (!msg_data_sz(msg)) {
  2185. struct msghdr m = {NULL,};
  2186. tsk_advance_rx_queue(sk);
  2187. __tipc_sendstream(new_sock, &m, 0);
  2188. } else {
  2189. __skb_dequeue(&sk->sk_receive_queue);
  2190. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  2191. skb_set_owner_r(buf, new_sk);
  2192. }
  2193. release_sock(new_sk);
  2194. exit:
  2195. release_sock(sk);
  2196. return res;
  2197. }
  2198. /**
  2199. * tipc_shutdown - shutdown socket connection
  2200. * @sock: socket structure
  2201. * @how: direction to close (must be SHUT_RDWR)
  2202. *
  2203. * Terminates connection (if necessary), then purges socket's receive queue.
  2204. *
  2205. * Returns 0 on success, errno otherwise
  2206. */
  2207. static int tipc_shutdown(struct socket *sock, int how)
  2208. {
  2209. struct sock *sk = sock->sk;
  2210. int res;
  2211. if (how != SHUT_RDWR)
  2212. return -EINVAL;
  2213. lock_sock(sk);
  2214. __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
  2215. sk->sk_shutdown = SEND_SHUTDOWN;
  2216. if (sk->sk_state == TIPC_DISCONNECTING) {
  2217. /* Discard any unreceived messages */
  2218. __skb_queue_purge(&sk->sk_receive_queue);
  2219. /* Wake up anyone sleeping in poll */
  2220. sk->sk_state_change(sk);
  2221. res = 0;
  2222. } else {
  2223. res = -ENOTCONN;
  2224. }
  2225. release_sock(sk);
  2226. return res;
  2227. }
  2228. static void tipc_sk_timeout(struct timer_list *t)
  2229. {
  2230. struct sock *sk = from_timer(sk, t, sk_timer);
  2231. struct tipc_sock *tsk = tipc_sk(sk);
  2232. u32 peer_port = tsk_peer_port(tsk);
  2233. u32 peer_node = tsk_peer_node(tsk);
  2234. u32 own_node = tsk_own_node(tsk);
  2235. u32 own_port = tsk->portid;
  2236. struct net *net = sock_net(sk);
  2237. struct sk_buff *skb = NULL;
  2238. bh_lock_sock(sk);
  2239. if (!tipc_sk_connected(sk))
  2240. goto exit;
  2241. /* Try again later if socket is busy */
  2242. if (sock_owned_by_user(sk)) {
  2243. sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
  2244. goto exit;
  2245. }
  2246. if (tsk->probe_unacked) {
  2247. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2248. tipc_node_remove_conn(net, peer_node, peer_port);
  2249. sk->sk_state_change(sk);
  2250. goto exit;
  2251. }
  2252. /* Send new probe */
  2253. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
  2254. peer_node, own_node, peer_port, own_port,
  2255. TIPC_OK);
  2256. tsk->probe_unacked = true;
  2257. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  2258. exit:
  2259. bh_unlock_sock(sk);
  2260. if (skb)
  2261. tipc_node_xmit_skb(net, skb, peer_node, own_port);
  2262. sock_put(sk);
  2263. }
  2264. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  2265. struct tipc_name_seq const *seq)
  2266. {
  2267. struct sock *sk = &tsk->sk;
  2268. struct net *net = sock_net(sk);
  2269. struct publication *publ;
  2270. u32 key;
  2271. if (tipc_sk_connected(sk))
  2272. return -EINVAL;
  2273. key = tsk->portid + tsk->pub_count + 1;
  2274. if (key == tsk->portid)
  2275. return -EADDRINUSE;
  2276. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  2277. scope, tsk->portid, key);
  2278. if (unlikely(!publ))
  2279. return -EINVAL;
  2280. list_add(&publ->pport_list, &tsk->publications);
  2281. tsk->pub_count++;
  2282. tsk->published = 1;
  2283. return 0;
  2284. }
  2285. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  2286. struct tipc_name_seq const *seq)
  2287. {
  2288. struct net *net = sock_net(&tsk->sk);
  2289. struct publication *publ;
  2290. struct publication *safe;
  2291. int rc = -EINVAL;
  2292. list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
  2293. if (seq) {
  2294. if (publ->scope != scope)
  2295. continue;
  2296. if (publ->type != seq->type)
  2297. continue;
  2298. if (publ->lower != seq->lower)
  2299. continue;
  2300. if (publ->upper != seq->upper)
  2301. break;
  2302. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2303. publ->ref, publ->key);
  2304. rc = 0;
  2305. break;
  2306. }
  2307. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2308. publ->ref, publ->key);
  2309. rc = 0;
  2310. }
  2311. if (list_empty(&tsk->publications))
  2312. tsk->published = 0;
  2313. return rc;
  2314. }
  2315. /* tipc_sk_reinit: set non-zero address in all existing sockets
  2316. * when we go from standalone to network mode.
  2317. */
  2318. void tipc_sk_reinit(struct net *net)
  2319. {
  2320. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2321. struct rhashtable_iter iter;
  2322. struct tipc_sock *tsk;
  2323. struct tipc_msg *msg;
  2324. rhashtable_walk_enter(&tn->sk_rht, &iter);
  2325. do {
  2326. tsk = ERR_PTR(rhashtable_walk_start(&iter));
  2327. if (IS_ERR(tsk))
  2328. goto walk_stop;
  2329. while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
  2330. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2331. msg = &tsk->phdr;
  2332. msg_set_prevnode(msg, tn->own_addr);
  2333. msg_set_orignode(msg, tn->own_addr);
  2334. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2335. }
  2336. walk_stop:
  2337. rhashtable_walk_stop(&iter);
  2338. } while (tsk == ERR_PTR(-EAGAIN));
  2339. }
  2340. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2341. {
  2342. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2343. struct tipc_sock *tsk;
  2344. rcu_read_lock();
  2345. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  2346. if (tsk)
  2347. sock_hold(&tsk->sk);
  2348. rcu_read_unlock();
  2349. return tsk;
  2350. }
  2351. static int tipc_sk_insert(struct tipc_sock *tsk)
  2352. {
  2353. struct sock *sk = &tsk->sk;
  2354. struct net *net = sock_net(sk);
  2355. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2356. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2357. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  2358. while (remaining--) {
  2359. portid++;
  2360. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2361. portid = TIPC_MIN_PORT;
  2362. tsk->portid = portid;
  2363. sock_hold(&tsk->sk);
  2364. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2365. tsk_rht_params))
  2366. return 0;
  2367. sock_put(&tsk->sk);
  2368. }
  2369. return -1;
  2370. }
  2371. static void tipc_sk_remove(struct tipc_sock *tsk)
  2372. {
  2373. struct sock *sk = &tsk->sk;
  2374. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2375. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2376. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  2377. __sock_put(sk);
  2378. }
  2379. }
  2380. static const struct rhashtable_params tsk_rht_params = {
  2381. .nelem_hint = 192,
  2382. .head_offset = offsetof(struct tipc_sock, node),
  2383. .key_offset = offsetof(struct tipc_sock, portid),
  2384. .key_len = sizeof(u32), /* portid */
  2385. .max_size = 1048576,
  2386. .min_size = 256,
  2387. .automatic_shrinking = true,
  2388. };
  2389. int tipc_sk_rht_init(struct net *net)
  2390. {
  2391. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2392. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2393. }
  2394. void tipc_sk_rht_destroy(struct net *net)
  2395. {
  2396. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2397. /* Wait for socket readers to complete */
  2398. synchronize_net();
  2399. rhashtable_destroy(&tn->sk_rht);
  2400. }
  2401. static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
  2402. {
  2403. struct net *net = sock_net(&tsk->sk);
  2404. u32 domain = addr_domain(net, mreq->scope);
  2405. struct tipc_group *grp = tsk->group;
  2406. struct tipc_msg *hdr = &tsk->phdr;
  2407. struct tipc_name_seq seq;
  2408. int rc;
  2409. if (mreq->type < TIPC_RESERVED_TYPES)
  2410. return -EACCES;
  2411. if (grp)
  2412. return -EACCES;
  2413. grp = tipc_group_create(net, tsk->portid, mreq);
  2414. if (!grp)
  2415. return -ENOMEM;
  2416. tsk->group = grp;
  2417. msg_set_lookup_scope(hdr, mreq->scope);
  2418. msg_set_nametype(hdr, mreq->type);
  2419. msg_set_dest_droppable(hdr, true);
  2420. seq.type = mreq->type;
  2421. seq.lower = mreq->instance;
  2422. seq.upper = seq.lower;
  2423. tipc_nametbl_build_group(net, grp, mreq->type, domain);
  2424. rc = tipc_sk_publish(tsk, mreq->scope, &seq);
  2425. if (rc) {
  2426. tipc_group_delete(net, grp);
  2427. tsk->group = NULL;
  2428. }
  2429. /* Eliminate any risk that a broadcast overtakes the sent JOIN */
  2430. tsk->mc_method.rcast = true;
  2431. tsk->mc_method.mandatory = true;
  2432. return rc;
  2433. }
  2434. static int tipc_sk_leave(struct tipc_sock *tsk)
  2435. {
  2436. struct net *net = sock_net(&tsk->sk);
  2437. struct tipc_group *grp = tsk->group;
  2438. struct tipc_name_seq seq;
  2439. int scope;
  2440. if (!grp)
  2441. return -EINVAL;
  2442. tipc_group_self(grp, &seq, &scope);
  2443. tipc_group_delete(net, grp);
  2444. tsk->group = NULL;
  2445. tipc_sk_withdraw(tsk, scope, &seq);
  2446. return 0;
  2447. }
  2448. /**
  2449. * tipc_setsockopt - set socket option
  2450. * @sock: socket structure
  2451. * @lvl: option level
  2452. * @opt: option identifier
  2453. * @ov: pointer to new option value
  2454. * @ol: length of option value
  2455. *
  2456. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2457. * (to ease compatibility).
  2458. *
  2459. * Returns 0 on success, errno otherwise
  2460. */
  2461. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2462. char __user *ov, unsigned int ol)
  2463. {
  2464. struct sock *sk = sock->sk;
  2465. struct tipc_sock *tsk = tipc_sk(sk);
  2466. struct tipc_group_req mreq;
  2467. u32 value = 0;
  2468. int res = 0;
  2469. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2470. return 0;
  2471. if (lvl != SOL_TIPC)
  2472. return -ENOPROTOOPT;
  2473. switch (opt) {
  2474. case TIPC_IMPORTANCE:
  2475. case TIPC_SRC_DROPPABLE:
  2476. case TIPC_DEST_DROPPABLE:
  2477. case TIPC_CONN_TIMEOUT:
  2478. if (ol < sizeof(value))
  2479. return -EINVAL;
  2480. if (get_user(value, (u32 __user *)ov))
  2481. return -EFAULT;
  2482. break;
  2483. case TIPC_GROUP_JOIN:
  2484. if (ol < sizeof(mreq))
  2485. return -EINVAL;
  2486. if (copy_from_user(&mreq, ov, sizeof(mreq)))
  2487. return -EFAULT;
  2488. break;
  2489. default:
  2490. if (ov || ol)
  2491. return -EINVAL;
  2492. }
  2493. lock_sock(sk);
  2494. switch (opt) {
  2495. case TIPC_IMPORTANCE:
  2496. res = tsk_set_importance(tsk, value);
  2497. break;
  2498. case TIPC_SRC_DROPPABLE:
  2499. if (sock->type != SOCK_STREAM)
  2500. tsk_set_unreliable(tsk, value);
  2501. else
  2502. res = -ENOPROTOOPT;
  2503. break;
  2504. case TIPC_DEST_DROPPABLE:
  2505. tsk_set_unreturnable(tsk, value);
  2506. break;
  2507. case TIPC_CONN_TIMEOUT:
  2508. tipc_sk(sk)->conn_timeout = value;
  2509. break;
  2510. case TIPC_MCAST_BROADCAST:
  2511. tsk->mc_method.rcast = false;
  2512. tsk->mc_method.mandatory = true;
  2513. break;
  2514. case TIPC_MCAST_REPLICAST:
  2515. tsk->mc_method.rcast = true;
  2516. tsk->mc_method.mandatory = true;
  2517. break;
  2518. case TIPC_GROUP_JOIN:
  2519. res = tipc_sk_join(tsk, &mreq);
  2520. break;
  2521. case TIPC_GROUP_LEAVE:
  2522. res = tipc_sk_leave(tsk);
  2523. break;
  2524. default:
  2525. res = -EINVAL;
  2526. }
  2527. release_sock(sk);
  2528. return res;
  2529. }
  2530. /**
  2531. * tipc_getsockopt - get socket option
  2532. * @sock: socket structure
  2533. * @lvl: option level
  2534. * @opt: option identifier
  2535. * @ov: receptacle for option value
  2536. * @ol: receptacle for length of option value
  2537. *
  2538. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2539. * (to ease compatibility).
  2540. *
  2541. * Returns 0 on success, errno otherwise
  2542. */
  2543. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2544. char __user *ov, int __user *ol)
  2545. {
  2546. struct sock *sk = sock->sk;
  2547. struct tipc_sock *tsk = tipc_sk(sk);
  2548. struct tipc_name_seq seq;
  2549. int len, scope;
  2550. u32 value;
  2551. int res;
  2552. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2553. return put_user(0, ol);
  2554. if (lvl != SOL_TIPC)
  2555. return -ENOPROTOOPT;
  2556. res = get_user(len, ol);
  2557. if (res)
  2558. return res;
  2559. lock_sock(sk);
  2560. switch (opt) {
  2561. case TIPC_IMPORTANCE:
  2562. value = tsk_importance(tsk);
  2563. break;
  2564. case TIPC_SRC_DROPPABLE:
  2565. value = tsk_unreliable(tsk);
  2566. break;
  2567. case TIPC_DEST_DROPPABLE:
  2568. value = tsk_unreturnable(tsk);
  2569. break;
  2570. case TIPC_CONN_TIMEOUT:
  2571. value = tsk->conn_timeout;
  2572. /* no need to set "res", since already 0 at this point */
  2573. break;
  2574. case TIPC_NODE_RECVQ_DEPTH:
  2575. value = 0; /* was tipc_queue_size, now obsolete */
  2576. break;
  2577. case TIPC_SOCK_RECVQ_DEPTH:
  2578. value = skb_queue_len(&sk->sk_receive_queue);
  2579. break;
  2580. case TIPC_GROUP_JOIN:
  2581. seq.type = 0;
  2582. if (tsk->group)
  2583. tipc_group_self(tsk->group, &seq, &scope);
  2584. value = seq.type;
  2585. break;
  2586. default:
  2587. res = -EINVAL;
  2588. }
  2589. release_sock(sk);
  2590. if (res)
  2591. return res; /* "get" failed */
  2592. if (len < sizeof(value))
  2593. return -EINVAL;
  2594. if (copy_to_user(ov, &value, sizeof(value)))
  2595. return -EFAULT;
  2596. return put_user(sizeof(value), ol);
  2597. }
  2598. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2599. {
  2600. struct sock *sk = sock->sk;
  2601. struct tipc_sioc_ln_req lnr;
  2602. void __user *argp = (void __user *)arg;
  2603. switch (cmd) {
  2604. case SIOCGETLINKNAME:
  2605. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2606. return -EFAULT;
  2607. if (!tipc_node_get_linkname(sock_net(sk),
  2608. lnr.bearer_id & 0xffff, lnr.peer,
  2609. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2610. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2611. return -EFAULT;
  2612. return 0;
  2613. }
  2614. return -EADDRNOTAVAIL;
  2615. default:
  2616. return -ENOIOCTLCMD;
  2617. }
  2618. }
  2619. static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
  2620. {
  2621. struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
  2622. struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
  2623. u32 onode = tipc_own_addr(sock_net(sock1->sk));
  2624. tsk1->peer.family = AF_TIPC;
  2625. tsk1->peer.addrtype = TIPC_ADDR_ID;
  2626. tsk1->peer.scope = TIPC_NODE_SCOPE;
  2627. tsk1->peer.addr.id.ref = tsk2->portid;
  2628. tsk1->peer.addr.id.node = onode;
  2629. tsk2->peer.family = AF_TIPC;
  2630. tsk2->peer.addrtype = TIPC_ADDR_ID;
  2631. tsk2->peer.scope = TIPC_NODE_SCOPE;
  2632. tsk2->peer.addr.id.ref = tsk1->portid;
  2633. tsk2->peer.addr.id.node = onode;
  2634. tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
  2635. tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
  2636. return 0;
  2637. }
  2638. /* Protocol switches for the various types of TIPC sockets */
  2639. static const struct proto_ops msg_ops = {
  2640. .owner = THIS_MODULE,
  2641. .family = AF_TIPC,
  2642. .release = tipc_release,
  2643. .bind = tipc_bind,
  2644. .connect = tipc_connect,
  2645. .socketpair = tipc_socketpair,
  2646. .accept = sock_no_accept,
  2647. .getname = tipc_getname,
  2648. .poll = tipc_poll,
  2649. .ioctl = tipc_ioctl,
  2650. .listen = sock_no_listen,
  2651. .shutdown = tipc_shutdown,
  2652. .setsockopt = tipc_setsockopt,
  2653. .getsockopt = tipc_getsockopt,
  2654. .sendmsg = tipc_sendmsg,
  2655. .recvmsg = tipc_recvmsg,
  2656. .mmap = sock_no_mmap,
  2657. .sendpage = sock_no_sendpage
  2658. };
  2659. static const struct proto_ops packet_ops = {
  2660. .owner = THIS_MODULE,
  2661. .family = AF_TIPC,
  2662. .release = tipc_release,
  2663. .bind = tipc_bind,
  2664. .connect = tipc_connect,
  2665. .socketpair = tipc_socketpair,
  2666. .accept = tipc_accept,
  2667. .getname = tipc_getname,
  2668. .poll = tipc_poll,
  2669. .ioctl = tipc_ioctl,
  2670. .listen = tipc_listen,
  2671. .shutdown = tipc_shutdown,
  2672. .setsockopt = tipc_setsockopt,
  2673. .getsockopt = tipc_getsockopt,
  2674. .sendmsg = tipc_send_packet,
  2675. .recvmsg = tipc_recvmsg,
  2676. .mmap = sock_no_mmap,
  2677. .sendpage = sock_no_sendpage
  2678. };
  2679. static const struct proto_ops stream_ops = {
  2680. .owner = THIS_MODULE,
  2681. .family = AF_TIPC,
  2682. .release = tipc_release,
  2683. .bind = tipc_bind,
  2684. .connect = tipc_connect,
  2685. .socketpair = tipc_socketpair,
  2686. .accept = tipc_accept,
  2687. .getname = tipc_getname,
  2688. .poll = tipc_poll,
  2689. .ioctl = tipc_ioctl,
  2690. .listen = tipc_listen,
  2691. .shutdown = tipc_shutdown,
  2692. .setsockopt = tipc_setsockopt,
  2693. .getsockopt = tipc_getsockopt,
  2694. .sendmsg = tipc_sendstream,
  2695. .recvmsg = tipc_recvstream,
  2696. .mmap = sock_no_mmap,
  2697. .sendpage = sock_no_sendpage
  2698. };
  2699. static const struct net_proto_family tipc_family_ops = {
  2700. .owner = THIS_MODULE,
  2701. .family = AF_TIPC,
  2702. .create = tipc_sk_create
  2703. };
  2704. static struct proto tipc_proto = {
  2705. .name = "TIPC",
  2706. .owner = THIS_MODULE,
  2707. .obj_size = sizeof(struct tipc_sock),
  2708. .sysctl_rmem = sysctl_tipc_rmem
  2709. };
  2710. /**
  2711. * tipc_socket_init - initialize TIPC socket interface
  2712. *
  2713. * Returns 0 on success, errno otherwise
  2714. */
  2715. int tipc_socket_init(void)
  2716. {
  2717. int res;
  2718. res = proto_register(&tipc_proto, 1);
  2719. if (res) {
  2720. pr_err("Failed to register TIPC protocol type\n");
  2721. goto out;
  2722. }
  2723. res = sock_register(&tipc_family_ops);
  2724. if (res) {
  2725. pr_err("Failed to register TIPC socket type\n");
  2726. proto_unregister(&tipc_proto);
  2727. goto out;
  2728. }
  2729. out:
  2730. return res;
  2731. }
  2732. /**
  2733. * tipc_socket_stop - stop TIPC socket interface
  2734. */
  2735. void tipc_socket_stop(void)
  2736. {
  2737. sock_unregister(tipc_family_ops.family);
  2738. proto_unregister(&tipc_proto);
  2739. }
  2740. /* Caller should hold socket lock for the passed tipc socket. */
  2741. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2742. {
  2743. u32 peer_node;
  2744. u32 peer_port;
  2745. struct nlattr *nest;
  2746. peer_node = tsk_peer_node(tsk);
  2747. peer_port = tsk_peer_port(tsk);
  2748. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2749. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2750. goto msg_full;
  2751. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2752. goto msg_full;
  2753. if (tsk->conn_type != 0) {
  2754. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2755. goto msg_full;
  2756. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2757. goto msg_full;
  2758. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2759. goto msg_full;
  2760. }
  2761. nla_nest_end(skb, nest);
  2762. return 0;
  2763. msg_full:
  2764. nla_nest_cancel(skb, nest);
  2765. return -EMSGSIZE;
  2766. }
  2767. /* Caller should hold socket lock for the passed tipc socket. */
  2768. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2769. struct tipc_sock *tsk)
  2770. {
  2771. int err;
  2772. void *hdr;
  2773. struct nlattr *attrs;
  2774. struct net *net = sock_net(skb->sk);
  2775. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2776. struct sock *sk = &tsk->sk;
  2777. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2778. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2779. if (!hdr)
  2780. goto msg_cancel;
  2781. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2782. if (!attrs)
  2783. goto genlmsg_cancel;
  2784. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
  2785. goto attr_msg_cancel;
  2786. if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
  2787. goto attr_msg_cancel;
  2788. if (tipc_sk_connected(sk)) {
  2789. err = __tipc_nl_add_sk_con(skb, tsk);
  2790. if (err)
  2791. goto attr_msg_cancel;
  2792. } else if (!list_empty(&tsk->publications)) {
  2793. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2794. goto attr_msg_cancel;
  2795. }
  2796. nla_nest_end(skb, attrs);
  2797. genlmsg_end(skb, hdr);
  2798. return 0;
  2799. attr_msg_cancel:
  2800. nla_nest_cancel(skb, attrs);
  2801. genlmsg_cancel:
  2802. genlmsg_cancel(skb, hdr);
  2803. msg_cancel:
  2804. return -EMSGSIZE;
  2805. }
  2806. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2807. {
  2808. int err;
  2809. struct tipc_sock *tsk;
  2810. const struct bucket_table *tbl;
  2811. struct rhash_head *pos;
  2812. struct net *net = sock_net(skb->sk);
  2813. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2814. u32 tbl_id = cb->args[0];
  2815. u32 prev_portid = cb->args[1];
  2816. rcu_read_lock();
  2817. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2818. for (; tbl_id < tbl->size; tbl_id++) {
  2819. rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
  2820. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2821. if (prev_portid && prev_portid != tsk->portid) {
  2822. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2823. continue;
  2824. }
  2825. err = __tipc_nl_add_sk(skb, cb, tsk);
  2826. if (err) {
  2827. prev_portid = tsk->portid;
  2828. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2829. goto out;
  2830. }
  2831. prev_portid = 0;
  2832. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2833. }
  2834. }
  2835. out:
  2836. rcu_read_unlock();
  2837. cb->args[0] = tbl_id;
  2838. cb->args[1] = prev_portid;
  2839. return skb->len;
  2840. }
  2841. /* Caller should hold socket lock for the passed tipc socket. */
  2842. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  2843. struct netlink_callback *cb,
  2844. struct publication *publ)
  2845. {
  2846. void *hdr;
  2847. struct nlattr *attrs;
  2848. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2849. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  2850. if (!hdr)
  2851. goto msg_cancel;
  2852. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  2853. if (!attrs)
  2854. goto genlmsg_cancel;
  2855. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  2856. goto attr_msg_cancel;
  2857. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  2858. goto attr_msg_cancel;
  2859. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  2860. goto attr_msg_cancel;
  2861. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  2862. goto attr_msg_cancel;
  2863. nla_nest_end(skb, attrs);
  2864. genlmsg_end(skb, hdr);
  2865. return 0;
  2866. attr_msg_cancel:
  2867. nla_nest_cancel(skb, attrs);
  2868. genlmsg_cancel:
  2869. genlmsg_cancel(skb, hdr);
  2870. msg_cancel:
  2871. return -EMSGSIZE;
  2872. }
  2873. /* Caller should hold socket lock for the passed tipc socket. */
  2874. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  2875. struct netlink_callback *cb,
  2876. struct tipc_sock *tsk, u32 *last_publ)
  2877. {
  2878. int err;
  2879. struct publication *p;
  2880. if (*last_publ) {
  2881. list_for_each_entry(p, &tsk->publications, pport_list) {
  2882. if (p->key == *last_publ)
  2883. break;
  2884. }
  2885. if (p->key != *last_publ) {
  2886. /* We never set seq or call nl_dump_check_consistent()
  2887. * this means that setting prev_seq here will cause the
  2888. * consistence check to fail in the netlink callback
  2889. * handler. Resulting in the last NLMSG_DONE message
  2890. * having the NLM_F_DUMP_INTR flag set.
  2891. */
  2892. cb->prev_seq = 1;
  2893. *last_publ = 0;
  2894. return -EPIPE;
  2895. }
  2896. } else {
  2897. p = list_first_entry(&tsk->publications, struct publication,
  2898. pport_list);
  2899. }
  2900. list_for_each_entry_from(p, &tsk->publications, pport_list) {
  2901. err = __tipc_nl_add_sk_publ(skb, cb, p);
  2902. if (err) {
  2903. *last_publ = p->key;
  2904. return err;
  2905. }
  2906. }
  2907. *last_publ = 0;
  2908. return 0;
  2909. }
  2910. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2911. {
  2912. int err;
  2913. u32 tsk_portid = cb->args[0];
  2914. u32 last_publ = cb->args[1];
  2915. u32 done = cb->args[2];
  2916. struct net *net = sock_net(skb->sk);
  2917. struct tipc_sock *tsk;
  2918. if (!tsk_portid) {
  2919. struct nlattr **attrs;
  2920. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  2921. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  2922. if (err)
  2923. return err;
  2924. if (!attrs[TIPC_NLA_SOCK])
  2925. return -EINVAL;
  2926. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  2927. attrs[TIPC_NLA_SOCK],
  2928. tipc_nl_sock_policy, NULL);
  2929. if (err)
  2930. return err;
  2931. if (!sock[TIPC_NLA_SOCK_REF])
  2932. return -EINVAL;
  2933. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  2934. }
  2935. if (done)
  2936. return 0;
  2937. tsk = tipc_sk_lookup(net, tsk_portid);
  2938. if (!tsk)
  2939. return -EINVAL;
  2940. lock_sock(&tsk->sk);
  2941. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  2942. if (!err)
  2943. done = 1;
  2944. release_sock(&tsk->sk);
  2945. sock_put(&tsk->sk);
  2946. cb->args[0] = tsk_portid;
  2947. cb->args[1] = last_publ;
  2948. cb->args[2] = done;
  2949. return skb->len;
  2950. }