aboutsummaryrefslogtreecommitdiffstats
path: root/net/tipc/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/tipc/socket.c')
-rw-r--r--net/tipc/socket.c1271
1 files changed, 1036 insertions, 235 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index ff8c8118d56e..4731cad99d1c 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -35,17 +35,67 @@
35 */ 35 */
36 36
37#include "core.h" 37#include "core.h"
38#include "port.h"
39#include "name_table.h" 38#include "name_table.h"
40#include "node.h" 39#include "node.h"
41#include "link.h" 40#include "link.h"
42#include <linux/export.h> 41#include <linux/export.h>
42#include "config.h"
43#include "socket.h"
43 44
44#define SS_LISTENING -1 /* socket is listening */ 45#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */ 46#define SS_READY -2 /* socket is connectionless */
46 47
47#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ 48#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
48#define TIPC_FWD_MSG 1 49#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
50#define TIPC_FWD_MSG 1
51#define TIPC_CONN_OK 0
52#define TIPC_CONN_PROBING 1
53
54/**
55 * struct tipc_sock - TIPC socket structure
56 * @sk: socket - interacts with 'port' and with user via the socket API
57 * @connected: non-zero if port is currently connected to a peer port
58 * @conn_type: TIPC type used when connection was established
59 * @conn_instance: TIPC instance used when connection was established
60 * @published: non-zero if port has one or more associated names
61 * @max_pkt: maximum packet size "hint" used when building messages sent by port
62 * @ref: unique reference to port in TIPC object registry
63 * @phdr: preformatted message header used when sending messages
64 * @port_list: adjacent ports in TIPC's global list of ports
65 * @publications: list of publications for port
66 * @pub_count: total # of publications port has made during its lifetime
67 * @probing_state:
68 * @probing_interval:
69 * @timer:
70 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
71 * @peer_name: the peer of the connection, if any
72 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
77 */
78struct tipc_sock {
79 struct sock sk;
80 int connected;
81 u32 conn_type;
82 u32 conn_instance;
83 int published;
84 u32 max_pkt;
85 u32 ref;
86 struct tipc_msg phdr;
87 struct list_head sock_list;
88 struct list_head publications;
89 u32 pub_count;
90 u32 probing_state;
91 u32 probing_interval;
92 struct timer_list timer;
93 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
95 bool link_cong;
96 uint sent_unacked;
97 uint rcv_unacked;
98};
49 99
50static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); 100static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
51static void tipc_data_ready(struct sock *sk); 101static void tipc_data_ready(struct sock *sk);
@@ -53,6 +103,16 @@ static void tipc_write_space(struct sock *sk);
53static int tipc_release(struct socket *sock); 103static int tipc_release(struct socket *sock);
54static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); 104static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
55static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); 105static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
106static void tipc_sk_timeout(unsigned long ref);
107static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
108 struct tipc_name_seq const *seq);
109static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
110 struct tipc_name_seq const *seq);
111static u32 tipc_sk_ref_acquire(struct tipc_sock *tsk);
112static void tipc_sk_ref_discard(u32 ref);
113static struct tipc_sock *tipc_sk_get(u32 ref);
114static struct tipc_sock *tipc_sk_get_next(u32 *ref);
115static void tipc_sk_put(struct tipc_sock *tsk);
56 116
57static const struct proto_ops packet_ops; 117static const struct proto_ops packet_ops;
58static const struct proto_ops stream_ops; 118static const struct proto_ops stream_ops;
@@ -61,6 +121,14 @@ static const struct proto_ops msg_ops;
61static struct proto tipc_proto; 121static struct proto tipc_proto;
62static struct proto tipc_proto_kern; 122static struct proto tipc_proto_kern;
63 123
124static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
125 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
126 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
127 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
128 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
129 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
130};
131
64/* 132/*
65 * Revised TIPC socket locking policy: 133 * Revised TIPC socket locking policy:
66 * 134 *
@@ -106,34 +174,117 @@ static struct proto tipc_proto_kern;
106 * - port reference 174 * - port reference
107 */ 175 */
108 176
109#include "socket.h" 177static u32 tsk_peer_node(struct tipc_sock *tsk)
178{
179 return msg_destnode(&tsk->phdr);
180}
181
182static u32 tsk_peer_port(struct tipc_sock *tsk)
183{
184 return msg_destport(&tsk->phdr);
185}
186
187static bool tsk_unreliable(struct tipc_sock *tsk)
188{
189 return msg_src_droppable(&tsk->phdr) != 0;
190}
191
192static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
193{
194 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
195}
196
197static bool tsk_unreturnable(struct tipc_sock *tsk)
198{
199 return msg_dest_droppable(&tsk->phdr) != 0;
200}
201
202static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
203{
204 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
205}
206
207static int tsk_importance(struct tipc_sock *tsk)
208{
209 return msg_importance(&tsk->phdr);
210}
211
212static int tsk_set_importance(struct tipc_sock *tsk, int imp)
213{
214 if (imp > TIPC_CRITICAL_IMPORTANCE)
215 return -EINVAL;
216 msg_set_importance(&tsk->phdr, (u32)imp);
217 return 0;
218}
219
220static struct tipc_sock *tipc_sk(const struct sock *sk)
221{
222 return container_of(sk, struct tipc_sock, sk);
223}
224
225static int tsk_conn_cong(struct tipc_sock *tsk)
226{
227 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
228}
110 229
111/** 230/**
112 * advance_rx_queue - discard first buffer in socket receive queue 231 * tsk_advance_rx_queue - discard first buffer in socket receive queue
113 * 232 *
114 * Caller must hold socket lock 233 * Caller must hold socket lock
115 */ 234 */
116static void advance_rx_queue(struct sock *sk) 235static void tsk_advance_rx_queue(struct sock *sk)
117{ 236{
118 kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); 237 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
119} 238}
120 239
121/** 240/**
122 * reject_rx_queue - reject all buffers in socket receive queue 241 * tsk_rej_rx_queue - reject all buffers in socket receive queue
123 * 242 *
124 * Caller must hold socket lock 243 * Caller must hold socket lock
125 */ 244 */
126static void reject_rx_queue(struct sock *sk) 245static void tsk_rej_rx_queue(struct sock *sk)
127{ 246{
128 struct sk_buff *buf; 247 struct sk_buff *skb;
129 u32 dnode; 248 u32 dnode;
130 249
131 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { 250 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
132 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) 251 if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT))
133 tipc_link_xmit(buf, dnode, 0); 252 tipc_link_xmit_skb(skb, dnode, 0);
134 } 253 }
135} 254}
136 255
256/* tsk_peer_msg - verify if message was sent by connected port's peer
257 *
258 * Handles cases where the node's network address has changed from
259 * the default of <0.0.0> to its configured setting.
260 */
261static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
262{
263 u32 peer_port = tsk_peer_port(tsk);
264 u32 orig_node;
265 u32 peer_node;
266
267 if (unlikely(!tsk->connected))
268 return false;
269
270 if (unlikely(msg_origport(msg) != peer_port))
271 return false;
272
273 orig_node = msg_orignode(msg);
274 peer_node = tsk_peer_node(tsk);
275
276 if (likely(orig_node == peer_node))
277 return true;
278
279 if (!orig_node && (peer_node == tipc_own_addr))
280 return true;
281
282 if (!peer_node && (orig_node == tipc_own_addr))
283 return true;
284
285 return false;
286}
287
137/** 288/**
138 * tipc_sk_create - create a TIPC socket 289 * tipc_sk_create - create a TIPC socket
139 * @net: network namespace (must be default network) 290 * @net: network namespace (must be default network)
@@ -153,7 +304,7 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
153 socket_state state; 304 socket_state state;
154 struct sock *sk; 305 struct sock *sk;
155 struct tipc_sock *tsk; 306 struct tipc_sock *tsk;
156 struct tipc_port *port; 307 struct tipc_msg *msg;
157 u32 ref; 308 u32 ref;
158 309
159 /* Validate arguments */ 310 /* Validate arguments */
@@ -188,20 +339,24 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
188 return -ENOMEM; 339 return -ENOMEM;
189 340
190 tsk = tipc_sk(sk); 341 tsk = tipc_sk(sk);
191 port = &tsk->port; 342 ref = tipc_sk_ref_acquire(tsk);
192
193 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
194 if (!ref) { 343 if (!ref) {
195 pr_warn("Socket registration failed, ref. table exhausted\n"); 344 pr_warn("Socket create failed; reference table exhausted\n");
196 sk_free(sk);
197 return -ENOMEM; 345 return -ENOMEM;
198 } 346 }
347 tsk->max_pkt = MAX_PKT_DEFAULT;
348 tsk->ref = ref;
349 INIT_LIST_HEAD(&tsk->publications);
350 msg = &tsk->phdr;
351 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
352 NAMED_H_SIZE, 0);
353 msg_set_origport(msg, ref);
199 354
200 /* Finish initializing socket data structures */ 355 /* Finish initializing socket data structures */
201 sock->ops = ops; 356 sock->ops = ops;
202 sock->state = state; 357 sock->state = state;
203
204 sock_init_data(sock, sk); 358 sock_init_data(sock, sk);
359 k_init_timer(&tsk->timer, (Handler)tipc_sk_timeout, ref);
205 sk->sk_backlog_rcv = tipc_backlog_rcv; 360 sk->sk_backlog_rcv = tipc_backlog_rcv;
206 sk->sk_rcvbuf = sysctl_tipc_rmem[1]; 361 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
207 sk->sk_data_ready = tipc_data_ready; 362 sk->sk_data_ready = tipc_data_ready;
@@ -209,12 +364,11 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
209 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; 364 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
210 tsk->sent_unacked = 0; 365 tsk->sent_unacked = 0;
211 atomic_set(&tsk->dupl_rcvcnt, 0); 366 atomic_set(&tsk->dupl_rcvcnt, 0);
212 tipc_port_unlock(port);
213 367
214 if (sock->state == SS_READY) { 368 if (sock->state == SS_READY) {
215 tipc_port_set_unreturnable(port, true); 369 tsk_set_unreturnable(tsk, true);
216 if (sock->type == SOCK_DGRAM) 370 if (sock->type == SOCK_DGRAM)
217 tipc_port_set_unreliable(port, true); 371 tsk_set_unreliable(tsk, true);
218 } 372 }
219 return 0; 373 return 0;
220} 374}
@@ -308,8 +462,7 @@ static int tipc_release(struct socket *sock)
308{ 462{
309 struct sock *sk = sock->sk; 463 struct sock *sk = sock->sk;
310 struct tipc_sock *tsk; 464 struct tipc_sock *tsk;
311 struct tipc_port *port; 465 struct sk_buff *skb;
312 struct sk_buff *buf;
313 u32 dnode; 466 u32 dnode;
314 467
315 /* 468 /*
@@ -320,34 +473,44 @@ static int tipc_release(struct socket *sock)
320 return 0; 473 return 0;
321 474
322 tsk = tipc_sk(sk); 475 tsk = tipc_sk(sk);
323 port = &tsk->port;
324 lock_sock(sk); 476 lock_sock(sk);
325 477
326 /* 478 /*
327 * Reject all unreceived messages, except on an active connection 479 * Reject all unreceived messages, except on an active connection
328 * (which disconnects locally & sends a 'FIN+' to peer) 480 * (which disconnects locally & sends a 'FIN+' to peer)
329 */ 481 */
482 dnode = tsk_peer_node(tsk);
330 while (sock->state != SS_DISCONNECTING) { 483 while (sock->state != SS_DISCONNECTING) {
331 buf = __skb_dequeue(&sk->sk_receive_queue); 484 skb = __skb_dequeue(&sk->sk_receive_queue);
332 if (buf == NULL) 485 if (skb == NULL)
333 break; 486 break;
334 if (TIPC_SKB_CB(buf)->handle != NULL) 487 if (TIPC_SKB_CB(skb)->handle != NULL)
335 kfree_skb(buf); 488 kfree_skb(skb);
336 else { 489 else {
337 if ((sock->state == SS_CONNECTING) || 490 if ((sock->state == SS_CONNECTING) ||
338 (sock->state == SS_CONNECTED)) { 491 (sock->state == SS_CONNECTED)) {
339 sock->state = SS_DISCONNECTING; 492 sock->state = SS_DISCONNECTING;
340 tipc_port_disconnect(port->ref); 493 tsk->connected = 0;
494 tipc_node_remove_conn(dnode, tsk->ref);
341 } 495 }
342 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) 496 if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT))
343 tipc_link_xmit(buf, dnode, 0); 497 tipc_link_xmit_skb(skb, dnode, 0);
344 } 498 }
345 } 499 }
346 500
347 /* Destroy TIPC port; also disconnects an active connection and 501 tipc_sk_withdraw(tsk, 0, NULL);
348 * sends a 'FIN-' to peer. 502 tipc_sk_ref_discard(tsk->ref);
349 */ 503 k_cancel_timer(&tsk->timer);
350 tipc_port_destroy(port); 504 if (tsk->connected) {
505 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
506 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
507 tsk_peer_port(tsk),
508 tsk->ref, TIPC_ERR_NO_PORT);
509 if (skb)
510 tipc_link_xmit_skb(skb, dnode, tsk->ref);
511 tipc_node_remove_conn(dnode, tsk->ref);
512 }
513 k_term_timer(&tsk->timer);
351 514
352 /* Discard any remaining (connection-based) messages in receive queue */ 515 /* Discard any remaining (connection-based) messages in receive queue */
353 __skb_queue_purge(&sk->sk_receive_queue); 516 __skb_queue_purge(&sk->sk_receive_queue);
@@ -355,7 +518,6 @@ static int tipc_release(struct socket *sock)
355 /* Reject any messages that accumulated in backlog queue */ 518 /* Reject any messages that accumulated in backlog queue */
356 sock->state = SS_DISCONNECTING; 519 sock->state = SS_DISCONNECTING;
357 release_sock(sk); 520 release_sock(sk);
358
359 sock_put(sk); 521 sock_put(sk);
360 sock->sk = NULL; 522 sock->sk = NULL;
361 523
@@ -387,7 +549,7 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
387 549
388 lock_sock(sk); 550 lock_sock(sk);
389 if (unlikely(!uaddr_len)) { 551 if (unlikely(!uaddr_len)) {
390 res = tipc_withdraw(&tsk->port, 0, NULL); 552 res = tipc_sk_withdraw(tsk, 0, NULL);
391 goto exit; 553 goto exit;
392 } 554 }
393 555
@@ -415,8 +577,8 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
415 } 577 }
416 578
417 res = (addr->scope > 0) ? 579 res = (addr->scope > 0) ?
418 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) : 580 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
419 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq); 581 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
420exit: 582exit:
421 release_sock(sk); 583 release_sock(sk);
422 return res; 584 return res;
@@ -446,10 +608,10 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
446 if ((sock->state != SS_CONNECTED) && 608 if ((sock->state != SS_CONNECTED) &&
447 ((peer != 2) || (sock->state != SS_DISCONNECTING))) 609 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
448 return -ENOTCONN; 610 return -ENOTCONN;
449 addr->addr.id.ref = tipc_port_peerport(&tsk->port); 611 addr->addr.id.ref = tsk_peer_port(tsk);
450 addr->addr.id.node = tipc_port_peernode(&tsk->port); 612 addr->addr.id.node = tsk_peer_node(tsk);
451 } else { 613 } else {
452 addr->addr.id.ref = tsk->port.ref; 614 addr->addr.id.ref = tsk->ref;
453 addr->addr.id.node = tipc_own_addr; 615 addr->addr.id.node = tipc_own_addr;
454 } 616 }
455 617
@@ -518,7 +680,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
518 break; 680 break;
519 case SS_READY: 681 case SS_READY:
520 case SS_CONNECTED: 682 case SS_CONNECTED:
521 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk)) 683 if (!tsk->link_cong && !tsk_conn_cong(tsk))
522 mask |= POLLOUT; 684 mask |= POLLOUT;
523 /* fall thru' */ 685 /* fall thru' */
524 case SS_CONNECTING: 686 case SS_CONNECTING:
@@ -538,7 +700,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
538 * tipc_sendmcast - send multicast message 700 * tipc_sendmcast - send multicast message
539 * @sock: socket structure 701 * @sock: socket structure
540 * @seq: destination address 702 * @seq: destination address
541 * @iov: message data to send 703 * @msg: message to send
542 * @dsz: total length of message data 704 * @dsz: total length of message data
543 * @timeo: timeout to wait for wakeup 705 * @timeo: timeout to wait for wakeup
544 * 706 *
@@ -546,11 +708,11 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
546 * Returns the number of bytes sent on success, or errno 708 * Returns the number of bytes sent on success, or errno
547 */ 709 */
548static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, 710static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
549 struct iovec *iov, size_t dsz, long timeo) 711 struct msghdr *msg, size_t dsz, long timeo)
550{ 712{
551 struct sock *sk = sock->sk; 713 struct sock *sk = sock->sk;
552 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr; 714 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
553 struct sk_buff *buf; 715 struct sk_buff_head head;
554 uint mtu; 716 uint mtu;
555 int rc; 717 int rc;
556 718
@@ -565,12 +727,13 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
565 727
566new_mtu: 728new_mtu:
567 mtu = tipc_bclink_get_mtu(); 729 mtu = tipc_bclink_get_mtu();
568 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); 730 __skb_queue_head_init(&head);
731 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head);
569 if (unlikely(rc < 0)) 732 if (unlikely(rc < 0))
570 return rc; 733 return rc;
571 734
572 do { 735 do {
573 rc = tipc_bclink_xmit(buf); 736 rc = tipc_bclink_xmit(&head);
574 if (likely(rc >= 0)) { 737 if (likely(rc >= 0)) {
575 rc = dsz; 738 rc = dsz;
576 break; 739 break;
@@ -579,9 +742,10 @@ new_mtu:
579 goto new_mtu; 742 goto new_mtu;
580 if (rc != -ELINKCONG) 743 if (rc != -ELINKCONG)
581 break; 744 break;
745 tipc_sk(sk)->link_cong = 1;
582 rc = tipc_wait_for_sndmsg(sock, &timeo); 746 rc = tipc_wait_for_sndmsg(sock, &timeo);
583 if (rc) 747 if (rc)
584 kfree_skb_list(buf); 748 __skb_queue_purge(&head);
585 } while (!rc); 749 } while (!rc);
586 return rc; 750 return rc;
587} 751}
@@ -638,20 +802,19 @@ static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
638 struct sk_buff *buf) 802 struct sk_buff *buf)
639{ 803{
640 struct tipc_msg *msg = buf_msg(buf); 804 struct tipc_msg *msg = buf_msg(buf);
641 struct tipc_port *port = &tsk->port;
642 int conn_cong; 805 int conn_cong;
643 806
644 /* Ignore if connection cannot be validated: */ 807 /* Ignore if connection cannot be validated: */
645 if (!port->connected || !tipc_port_peer_msg(port, msg)) 808 if (!tsk_peer_msg(tsk, msg))
646 goto exit; 809 goto exit;
647 810
648 port->probing_state = TIPC_CONN_OK; 811 tsk->probing_state = TIPC_CONN_OK;
649 812
650 if (msg_type(msg) == CONN_ACK) { 813 if (msg_type(msg) == CONN_ACK) {
651 conn_cong = tipc_sk_conn_cong(tsk); 814 conn_cong = tsk_conn_cong(tsk);
652 tsk->sent_unacked -= msg_msgcnt(msg); 815 tsk->sent_unacked -= msg_msgcnt(msg);
653 if (conn_cong) 816 if (conn_cong)
654 tipc_sock_wakeup(tsk); 817 tsk->sk.sk_write_space(&tsk->sk);
655 } else if (msg_type(msg) == CONN_PROBE) { 818 } else if (msg_type(msg) == CONN_PROBE) {
656 if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) 819 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
657 return TIPC_OK; 820 return TIPC_OK;
@@ -664,39 +827,6 @@ exit:
664 return TIPC_OK; 827 return TIPC_OK;
665} 828}
666 829
667/**
668 * dest_name_check - verify user is permitted to send to specified port name
669 * @dest: destination address
670 * @m: descriptor for message to be sent
671 *
672 * Prevents restricted configuration commands from being issued by
673 * unauthorized users.
674 *
675 * Returns 0 if permission is granted, otherwise errno
676 */
677static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
678{
679 struct tipc_cfg_msg_hdr hdr;
680
681 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
682 return 0;
683 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
684 return 0;
685 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
686 return 0;
687 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
688 return -EACCES;
689
690 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
691 return -EMSGSIZE;
692 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
693 return -EFAULT;
694 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
695 return -EACCES;
696
697 return 0;
698}
699
700static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) 830static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
701{ 831{
702 struct sock *sk = sock->sk; 832 struct sock *sk = sock->sk;
@@ -742,15 +872,14 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
742 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 872 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
743 struct sock *sk = sock->sk; 873 struct sock *sk = sock->sk;
744 struct tipc_sock *tsk = tipc_sk(sk); 874 struct tipc_sock *tsk = tipc_sk(sk);
745 struct tipc_port *port = &tsk->port; 875 struct tipc_msg *mhdr = &tsk->phdr;
746 struct tipc_msg *mhdr = &port->phdr;
747 struct iovec *iov = m->msg_iov;
748 u32 dnode, dport; 876 u32 dnode, dport;
749 struct sk_buff *buf; 877 struct sk_buff_head head;
878 struct sk_buff *skb;
750 struct tipc_name_seq *seq = &dest->addr.nameseq; 879 struct tipc_name_seq *seq = &dest->addr.nameseq;
751 u32 mtu; 880 u32 mtu;
752 long timeo; 881 long timeo;
753 int rc = -EINVAL; 882 int rc;
754 883
755 if (unlikely(!dest)) 884 if (unlikely(!dest))
756 return -EDESTADDRREQ; 885 return -EDESTADDRREQ;
@@ -774,23 +903,20 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
774 rc = -EISCONN; 903 rc = -EISCONN;
775 goto exit; 904 goto exit;
776 } 905 }
777 if (tsk->port.published) { 906 if (tsk->published) {
778 rc = -EOPNOTSUPP; 907 rc = -EOPNOTSUPP;
779 goto exit; 908 goto exit;
780 } 909 }
781 if (dest->addrtype == TIPC_ADDR_NAME) { 910 if (dest->addrtype == TIPC_ADDR_NAME) {
782 tsk->port.conn_type = dest->addr.name.name.type; 911 tsk->conn_type = dest->addr.name.name.type;
783 tsk->port.conn_instance = dest->addr.name.name.instance; 912 tsk->conn_instance = dest->addr.name.name.instance;
784 } 913 }
785 } 914 }
786 rc = dest_name_check(dest, m);
787 if (rc)
788 goto exit;
789 915
790 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 916 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
791 917
792 if (dest->addrtype == TIPC_ADDR_MCAST) { 918 if (dest->addrtype == TIPC_ADDR_MCAST) {
793 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo); 919 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
794 goto exit; 920 goto exit;
795 } else if (dest->addrtype == TIPC_ADDR_NAME) { 921 } else if (dest->addrtype == TIPC_ADDR_NAME) {
796 u32 type = dest->addr.name.name.type; 922 u32 type = dest->addr.name.name.type;
@@ -820,13 +946,16 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
820 } 946 }
821 947
822new_mtu: 948new_mtu:
823 mtu = tipc_node_get_mtu(dnode, tsk->port.ref); 949 mtu = tipc_node_get_mtu(dnode, tsk->ref);
824 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); 950 __skb_queue_head_init(&head);
951 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head);
825 if (rc < 0) 952 if (rc < 0)
826 goto exit; 953 goto exit;
827 954
828 do { 955 do {
829 rc = tipc_link_xmit(buf, dnode, tsk->port.ref); 956 skb = skb_peek(&head);
957 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
958 rc = tipc_link_xmit(&head, dnode, tsk->ref);
830 if (likely(rc >= 0)) { 959 if (likely(rc >= 0)) {
831 if (sock->state != SS_READY) 960 if (sock->state != SS_READY)
832 sock->state = SS_CONNECTING; 961 sock->state = SS_CONNECTING;
@@ -835,13 +964,12 @@ new_mtu:
835 } 964 }
836 if (rc == -EMSGSIZE) 965 if (rc == -EMSGSIZE)
837 goto new_mtu; 966 goto new_mtu;
838
839 if (rc != -ELINKCONG) 967 if (rc != -ELINKCONG)
840 break; 968 break;
841 969 tsk->link_cong = 1;
842 rc = tipc_wait_for_sndmsg(sock, &timeo); 970 rc = tipc_wait_for_sndmsg(sock, &timeo);
843 if (rc) 971 if (rc)
844 kfree_skb_list(buf); 972 __skb_queue_purge(&head);
845 } while (!rc); 973 } while (!rc);
846exit: 974exit:
847 if (iocb) 975 if (iocb)
@@ -873,8 +1001,8 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
873 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1001 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
874 done = sk_wait_event(sk, timeo_p, 1002 done = sk_wait_event(sk, timeo_p,
875 (!tsk->link_cong && 1003 (!tsk->link_cong &&
876 !tipc_sk_conn_cong(tsk)) || 1004 !tsk_conn_cong(tsk)) ||
877 !tsk->port.connected); 1005 !tsk->connected);
878 finish_wait(sk_sleep(sk), &wait); 1006 finish_wait(sk_sleep(sk), &wait);
879 } while (!done); 1007 } while (!done);
880 return 0; 1008 return 0;
@@ -897,11 +1025,10 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
897{ 1025{
898 struct sock *sk = sock->sk; 1026 struct sock *sk = sock->sk;
899 struct tipc_sock *tsk = tipc_sk(sk); 1027 struct tipc_sock *tsk = tipc_sk(sk);
900 struct tipc_port *port = &tsk->port; 1028 struct tipc_msg *mhdr = &tsk->phdr;
901 struct tipc_msg *mhdr = &port->phdr; 1029 struct sk_buff_head head;
902 struct sk_buff *buf;
903 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 1030 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
904 u32 ref = port->ref; 1031 u32 ref = tsk->ref;
905 int rc = -EINVAL; 1032 int rc = -EINVAL;
906 long timeo; 1033 long timeo;
907 u32 dnode; 1034 u32 dnode;
@@ -929,17 +1056,18 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
929 } 1056 }
930 1057
931 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 1058 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
932 dnode = tipc_port_peernode(port); 1059 dnode = tsk_peer_node(tsk);
933 1060
934next: 1061next:
935 mtu = port->max_pkt; 1062 mtu = tsk->max_pkt;
936 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); 1063 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
937 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf); 1064 __skb_queue_head_init(&head);
1065 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head);
938 if (unlikely(rc < 0)) 1066 if (unlikely(rc < 0))
939 goto exit; 1067 goto exit;
940 do { 1068 do {
941 if (likely(!tipc_sk_conn_cong(tsk))) { 1069 if (likely(!tsk_conn_cong(tsk))) {
942 rc = tipc_link_xmit(buf, dnode, ref); 1070 rc = tipc_link_xmit(&head, dnode, ref);
943 if (likely(!rc)) { 1071 if (likely(!rc)) {
944 tsk->sent_unacked++; 1072 tsk->sent_unacked++;
945 sent += send; 1073 sent += send;
@@ -948,15 +1076,16 @@ next:
948 goto next; 1076 goto next;
949 } 1077 }
950 if (rc == -EMSGSIZE) { 1078 if (rc == -EMSGSIZE) {
951 port->max_pkt = tipc_node_get_mtu(dnode, ref); 1079 tsk->max_pkt = tipc_node_get_mtu(dnode, ref);
952 goto next; 1080 goto next;
953 } 1081 }
954 if (rc != -ELINKCONG) 1082 if (rc != -ELINKCONG)
955 break; 1083 break;
1084 tsk->link_cong = 1;
956 } 1085 }
957 rc = tipc_wait_for_sndpkt(sock, &timeo); 1086 rc = tipc_wait_for_sndpkt(sock, &timeo);
958 if (rc) 1087 if (rc)
959 kfree_skb_list(buf); 1088 __skb_queue_purge(&head);
960 } while (!rc); 1089 } while (!rc);
961exit: 1090exit:
962 if (iocb) 1091 if (iocb)
@@ -984,29 +1113,25 @@ static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
984 return tipc_send_stream(iocb, sock, m, dsz); 1113 return tipc_send_stream(iocb, sock, m, dsz);
985} 1114}
986 1115
987/** 1116/* tipc_sk_finish_conn - complete the setup of a connection
988 * auto_connect - complete connection setup to a remote port
989 * @tsk: tipc socket structure
990 * @msg: peer's response message
991 *
992 * Returns 0 on success, errno otherwise
993 */ 1117 */
994static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg) 1118static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1119 u32 peer_node)
995{ 1120{
996 struct tipc_port *port = &tsk->port; 1121 struct tipc_msg *msg = &tsk->phdr;
997 struct socket *sock = tsk->sk.sk_socket; 1122
998 struct tipc_portid peer; 1123 msg_set_destnode(msg, peer_node);
999 1124 msg_set_destport(msg, peer_port);
1000 peer.ref = msg_origport(msg); 1125 msg_set_type(msg, TIPC_CONN_MSG);
1001 peer.node = msg_orignode(msg); 1126 msg_set_lookup_scope(msg, 0);
1002 1127 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1003 __tipc_port_connect(port->ref, port, &peer); 1128
1004 1129 tsk->probing_interval = CONN_PROBING_INTERVAL;
1005 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE) 1130 tsk->probing_state = TIPC_CONN_OK;
1006 return -EINVAL; 1131 tsk->connected = 1;
1007 msg_set_importance(&port->phdr, (u32)msg_importance(msg)); 1132 k_start_timer(&tsk->timer, tsk->probing_interval);
1008 sock->state = SS_CONNECTED; 1133 tipc_node_add_conn(peer_node, tsk->ref, peer_port);
1009 return 0; 1134 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref);
1010} 1135}
1011 1136
1012/** 1137/**
@@ -1033,17 +1158,17 @@ static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1033} 1158}
1034 1159
1035/** 1160/**
1036 * anc_data_recv - optionally capture ancillary data for received message 1161 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1037 * @m: descriptor for message info 1162 * @m: descriptor for message info
1038 * @msg: received message header 1163 * @msg: received message header
1039 * @tport: TIPC port associated with message 1164 * @tsk: TIPC port associated with message
1040 * 1165 *
1041 * Note: Ancillary data is not captured if not requested by receiver. 1166 * Note: Ancillary data is not captured if not requested by receiver.
1042 * 1167 *
1043 * Returns 0 if successful, otherwise errno 1168 * Returns 0 if successful, otherwise errno
1044 */ 1169 */
1045static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, 1170static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1046 struct tipc_port *tport) 1171 struct tipc_sock *tsk)
1047{ 1172{
1048 u32 anc_data[3]; 1173 u32 anc_data[3];
1049 u32 err; 1174 u32 err;
@@ -1086,10 +1211,10 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1086 anc_data[2] = msg_nameupper(msg); 1211 anc_data[2] = msg_nameupper(msg);
1087 break; 1212 break;
1088 case TIPC_CONN_MSG: 1213 case TIPC_CONN_MSG:
1089 has_name = (tport->conn_type != 0); 1214 has_name = (tsk->conn_type != 0);
1090 anc_data[0] = tport->conn_type; 1215 anc_data[0] = tsk->conn_type;
1091 anc_data[1] = tport->conn_instance; 1216 anc_data[1] = tsk->conn_instance;
1092 anc_data[2] = tport->conn_instance; 1217 anc_data[2] = tsk->conn_instance;
1093 break; 1218 break;
1094 default: 1219 default:
1095 has_name = 0; 1220 has_name = 0;
@@ -1103,6 +1228,24 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1103 return 0; 1228 return 0;
1104} 1229}
1105 1230
1231static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
1232{
1233 struct sk_buff *skb = NULL;
1234 struct tipc_msg *msg;
1235 u32 peer_port = tsk_peer_port(tsk);
1236 u32 dnode = tsk_peer_node(tsk);
1237
1238 if (!tsk->connected)
1239 return;
1240 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1241 tipc_own_addr, peer_port, tsk->ref, TIPC_OK);
1242 if (!skb)
1243 return;
1244 msg = buf_msg(skb);
1245 msg_set_msgcnt(msg, ack);
1246 tipc_link_xmit_skb(skb, dnode, msg_link_selector(msg));
1247}
1248
1106static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) 1249static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1107{ 1250{
1108 struct sock *sk = sock->sk; 1251 struct sock *sk = sock->sk;
@@ -1153,7 +1296,6 @@ static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1153{ 1296{
1154 struct sock *sk = sock->sk; 1297 struct sock *sk = sock->sk;
1155 struct tipc_sock *tsk = tipc_sk(sk); 1298 struct tipc_sock *tsk = tipc_sk(sk);
1156 struct tipc_port *port = &tsk->port;
1157 struct sk_buff *buf; 1299 struct sk_buff *buf;
1158 struct tipc_msg *msg; 1300 struct tipc_msg *msg;
1159 long timeo; 1301 long timeo;
@@ -1188,7 +1330,7 @@ restart:
1188 1330
1189 /* Discard an empty non-errored message & try again */ 1331 /* Discard an empty non-errored message & try again */
1190 if ((!sz) && (!err)) { 1332 if ((!sz) && (!err)) {
1191 advance_rx_queue(sk); 1333 tsk_advance_rx_queue(sk);
1192 goto restart; 1334 goto restart;
1193 } 1335 }
1194 1336
@@ -1196,7 +1338,7 @@ restart:
1196 set_orig_addr(m, msg); 1338 set_orig_addr(m, msg);
1197 1339
1198 /* Capture ancillary data (optional) */ 1340 /* Capture ancillary data (optional) */
1199 res = anc_data_recv(m, msg, port); 1341 res = tipc_sk_anc_data_recv(m, msg, tsk);
1200 if (res) 1342 if (res)
1201 goto exit; 1343 goto exit;
1202 1344
@@ -1206,8 +1348,7 @@ restart:
1206 sz = buf_len; 1348 sz = buf_len;
1207 m->msg_flags |= MSG_TRUNC; 1349 m->msg_flags |= MSG_TRUNC;
1208 } 1350 }
1209 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg), 1351 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
1210 m->msg_iov, sz);
1211 if (res) 1352 if (res)
1212 goto exit; 1353 goto exit;
1213 res = sz; 1354 res = sz;
@@ -1223,10 +1364,10 @@ restart:
1223 if (likely(!(flags & MSG_PEEK))) { 1364 if (likely(!(flags & MSG_PEEK))) {
1224 if ((sock->state != SS_READY) && 1365 if ((sock->state != SS_READY) &&
1225 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { 1366 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1226 tipc_acknowledge(port->ref, tsk->rcv_unacked); 1367 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1227 tsk->rcv_unacked = 0; 1368 tsk->rcv_unacked = 0;
1228 } 1369 }
1229 advance_rx_queue(sk); 1370 tsk_advance_rx_queue(sk);
1230 } 1371 }
1231exit: 1372exit:
1232 release_sock(sk); 1373 release_sock(sk);
@@ -1250,7 +1391,6 @@ static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1250{ 1391{
1251 struct sock *sk = sock->sk; 1392 struct sock *sk = sock->sk;
1252 struct tipc_sock *tsk = tipc_sk(sk); 1393 struct tipc_sock *tsk = tipc_sk(sk);
1253 struct tipc_port *port = &tsk->port;
1254 struct sk_buff *buf; 1394 struct sk_buff *buf;
1255 struct tipc_msg *msg; 1395 struct tipc_msg *msg;
1256 long timeo; 1396 long timeo;
@@ -1288,14 +1428,14 @@ restart:
1288 1428
1289 /* Discard an empty non-errored message & try again */ 1429 /* Discard an empty non-errored message & try again */
1290 if ((!sz) && (!err)) { 1430 if ((!sz) && (!err)) {
1291 advance_rx_queue(sk); 1431 tsk_advance_rx_queue(sk);
1292 goto restart; 1432 goto restart;
1293 } 1433 }
1294 1434
1295 /* Optionally capture sender's address & ancillary data of first msg */ 1435 /* Optionally capture sender's address & ancillary data of first msg */
1296 if (sz_copied == 0) { 1436 if (sz_copied == 0) {
1297 set_orig_addr(m, msg); 1437 set_orig_addr(m, msg);
1298 res = anc_data_recv(m, msg, port); 1438 res = tipc_sk_anc_data_recv(m, msg, tsk);
1299 if (res) 1439 if (res)
1300 goto exit; 1440 goto exit;
1301 } 1441 }
@@ -1308,8 +1448,8 @@ restart:
1308 needed = (buf_len - sz_copied); 1448 needed = (buf_len - sz_copied);
1309 sz_to_copy = (sz <= needed) ? sz : needed; 1449 sz_to_copy = (sz <= needed) ? sz : needed;
1310 1450
1311 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset, 1451 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1312 m->msg_iov, sz_to_copy); 1452 m, sz_to_copy);
1313 if (res) 1453 if (res)
1314 goto exit; 1454 goto exit;
1315 1455
@@ -1334,10 +1474,10 @@ restart:
1334 /* Consume received message (optional) */ 1474 /* Consume received message (optional) */
1335 if (likely(!(flags & MSG_PEEK))) { 1475 if (likely(!(flags & MSG_PEEK))) {
1336 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { 1476 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1337 tipc_acknowledge(port->ref, tsk->rcv_unacked); 1477 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1338 tsk->rcv_unacked = 0; 1478 tsk->rcv_unacked = 0;
1339 } 1479 }
1340 advance_rx_queue(sk); 1480 tsk_advance_rx_queue(sk);
1341 } 1481 }
1342 1482
1343 /* Loop around if more data is required */ 1483 /* Loop around if more data is required */
@@ -1391,17 +1531,14 @@ static void tipc_data_ready(struct sock *sk)
1391 * @tsk: TIPC socket 1531 * @tsk: TIPC socket
1392 * @msg: message 1532 * @msg: message
1393 * 1533 *
1394 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise 1534 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
1395 */ 1535 */
1396static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) 1536static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1397{ 1537{
1398 struct sock *sk = &tsk->sk; 1538 struct sock *sk = &tsk->sk;
1399 struct tipc_port *port = &tsk->port;
1400 struct socket *sock = sk->sk_socket; 1539 struct socket *sock = sk->sk_socket;
1401 struct tipc_msg *msg = buf_msg(*buf); 1540 struct tipc_msg *msg = buf_msg(*buf);
1402
1403 int retval = -TIPC_ERR_NO_PORT; 1541 int retval = -TIPC_ERR_NO_PORT;
1404 int res;
1405 1542
1406 if (msg_mcast(msg)) 1543 if (msg_mcast(msg))
1407 return retval; 1544 return retval;
@@ -1409,16 +1546,23 @@ static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1409 switch ((int)sock->state) { 1546 switch ((int)sock->state) {
1410 case SS_CONNECTED: 1547 case SS_CONNECTED:
1411 /* Accept only connection-based messages sent by peer */ 1548 /* Accept only connection-based messages sent by peer */
1412 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) { 1549 if (tsk_peer_msg(tsk, msg)) {
1413 if (unlikely(msg_errcode(msg))) { 1550 if (unlikely(msg_errcode(msg))) {
1414 sock->state = SS_DISCONNECTING; 1551 sock->state = SS_DISCONNECTING;
1415 __tipc_port_disconnect(port); 1552 tsk->connected = 0;
1553 /* let timer expire on it's own */
1554 tipc_node_remove_conn(tsk_peer_node(tsk),
1555 tsk->ref);
1416 } 1556 }
1417 retval = TIPC_OK; 1557 retval = TIPC_OK;
1418 } 1558 }
1419 break; 1559 break;
1420 case SS_CONNECTING: 1560 case SS_CONNECTING:
1421 /* Accept only ACK or NACK message */ 1561 /* Accept only ACK or NACK message */
1562
1563 if (unlikely(!msg_connected(msg)))
1564 break;
1565
1422 if (unlikely(msg_errcode(msg))) { 1566 if (unlikely(msg_errcode(msg))) {
1423 sock->state = SS_DISCONNECTING; 1567 sock->state = SS_DISCONNECTING;
1424 sk->sk_err = ECONNREFUSED; 1568 sk->sk_err = ECONNREFUSED;
@@ -1426,17 +1570,17 @@ static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1426 break; 1570 break;
1427 } 1571 }
1428 1572
1429 if (unlikely(!msg_connected(msg))) 1573 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
1430 break;
1431
1432 res = auto_connect(tsk, msg);
1433 if (res) {
1434 sock->state = SS_DISCONNECTING; 1574 sock->state = SS_DISCONNECTING;
1435 sk->sk_err = -res; 1575 sk->sk_err = EINVAL;
1436 retval = TIPC_OK; 1576 retval = TIPC_OK;
1437 break; 1577 break;
1438 } 1578 }
1439 1579
1580 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1581 msg_set_importance(&tsk->phdr, msg_importance(msg));
1582 sock->state = SS_CONNECTED;
1583
1440 /* If an incoming message is an 'ACK-', it should be 1584 /* If an incoming message is an 'ACK-', it should be
1441 * discarded here because it doesn't contain useful 1585 * discarded here because it doesn't contain useful
1442 * data. In addition, we should try to wake up 1586 * data. In addition, we should try to wake up
@@ -1518,6 +1662,13 @@ static int filter_rcv(struct sock *sk, struct sk_buff *buf)
1518 if (unlikely(msg_user(msg) == CONN_MANAGER)) 1662 if (unlikely(msg_user(msg) == CONN_MANAGER))
1519 return tipc_sk_proto_rcv(tsk, &onode, buf); 1663 return tipc_sk_proto_rcv(tsk, &onode, buf);
1520 1664
1665 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1666 kfree_skb(buf);
1667 tsk->link_cong = 0;
1668 sk->sk_write_space(sk);
1669 return TIPC_OK;
1670 }
1671
1521 /* Reject message if it is wrong sort of message for socket */ 1672 /* Reject message if it is wrong sort of message for socket */
1522 if (msg_type(msg) > TIPC_DIRECT_MSG) 1673 if (msg_type(msg) > TIPC_DIRECT_MSG)
1523 return -TIPC_ERR_NO_PORT; 1674 return -TIPC_ERR_NO_PORT;
@@ -1547,20 +1698,20 @@ static int filter_rcv(struct sock *sk, struct sk_buff *buf)
1547/** 1698/**
1548 * tipc_backlog_rcv - handle incoming message from backlog queue 1699 * tipc_backlog_rcv - handle incoming message from backlog queue
1549 * @sk: socket 1700 * @sk: socket
1550 * @buf: message 1701 * @skb: message
1551 * 1702 *
1552 * Caller must hold socket lock, but not port lock. 1703 * Caller must hold socket lock, but not port lock.
1553 * 1704 *
1554 * Returns 0 1705 * Returns 0
1555 */ 1706 */
1556static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) 1707static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1557{ 1708{
1558 int rc; 1709 int rc;
1559 u32 onode; 1710 u32 onode;
1560 struct tipc_sock *tsk = tipc_sk(sk); 1711 struct tipc_sock *tsk = tipc_sk(sk);
1561 uint truesize = buf->truesize; 1712 uint truesize = skb->truesize;
1562 1713
1563 rc = filter_rcv(sk, buf); 1714 rc = filter_rcv(sk, skb);
1564 1715
1565 if (likely(!rc)) { 1716 if (likely(!rc)) {
1566 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) 1717 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
@@ -1568,62 +1719,58 @@ static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
1568 return 0; 1719 return 0;
1569 } 1720 }
1570 1721
1571 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc)) 1722 if ((rc < 0) && !tipc_msg_reverse(skb, &onode, -rc))
1572 return 0; 1723 return 0;
1573 1724
1574 tipc_link_xmit(buf, onode, 0); 1725 tipc_link_xmit_skb(skb, onode, 0);
1575 1726
1576 return 0; 1727 return 0;
1577} 1728}
1578 1729
1579/** 1730/**
1580 * tipc_sk_rcv - handle incoming message 1731 * tipc_sk_rcv - handle incoming message
1581 * @buf: buffer containing arriving message 1732 * @skb: buffer containing arriving message
1582 * Consumes buffer 1733 * Consumes buffer
1583 * Returns 0 if success, or errno: -EHOSTUNREACH 1734 * Returns 0 if success, or errno: -EHOSTUNREACH
1584 */ 1735 */
1585int tipc_sk_rcv(struct sk_buff *buf) 1736int tipc_sk_rcv(struct sk_buff *skb)
1586{ 1737{
1587 struct tipc_sock *tsk; 1738 struct tipc_sock *tsk;
1588 struct tipc_port *port;
1589 struct sock *sk; 1739 struct sock *sk;
1590 u32 dport = msg_destport(buf_msg(buf)); 1740 u32 dport = msg_destport(buf_msg(skb));
1591 int rc = TIPC_OK; 1741 int rc = TIPC_OK;
1592 uint limit; 1742 uint limit;
1593 u32 dnode; 1743 u32 dnode;
1594 1744
1595 /* Validate destination and message */ 1745 /* Validate destination and message */
1596 port = tipc_port_lock(dport); 1746 tsk = tipc_sk_get(dport);
1597 if (unlikely(!port)) { 1747 if (unlikely(!tsk)) {
1598 rc = tipc_msg_eval(buf, &dnode); 1748 rc = tipc_msg_eval(skb, &dnode);
1599 goto exit; 1749 goto exit;
1600 } 1750 }
1601
1602 tsk = tipc_port_to_sock(port);
1603 sk = &tsk->sk; 1751 sk = &tsk->sk;
1604 1752
1605 /* Queue message */ 1753 /* Queue message */
1606 bh_lock_sock(sk); 1754 spin_lock_bh(&sk->sk_lock.slock);
1607 1755
1608 if (!sock_owned_by_user(sk)) { 1756 if (!sock_owned_by_user(sk)) {
1609 rc = filter_rcv(sk, buf); 1757 rc = filter_rcv(sk, skb);
1610 } else { 1758 } else {
1611 if (sk->sk_backlog.len == 0) 1759 if (sk->sk_backlog.len == 0)
1612 atomic_set(&tsk->dupl_rcvcnt, 0); 1760 atomic_set(&tsk->dupl_rcvcnt, 0);
1613 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt); 1761 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1614 if (sk_add_backlog(sk, buf, limit)) 1762 if (sk_add_backlog(sk, skb, limit))
1615 rc = -TIPC_ERR_OVERLOAD; 1763 rc = -TIPC_ERR_OVERLOAD;
1616 } 1764 }
1617 bh_unlock_sock(sk); 1765 spin_unlock_bh(&sk->sk_lock.slock);
1618 tipc_port_unlock(port); 1766 tipc_sk_put(tsk);
1619
1620 if (likely(!rc)) 1767 if (likely(!rc))
1621 return 0; 1768 return 0;
1622exit: 1769exit:
1623 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc)) 1770 if ((rc < 0) && !tipc_msg_reverse(skb, &dnode, -rc))
1624 return -EHOSTUNREACH; 1771 return -EHOSTUNREACH;
1625 1772
1626 tipc_link_xmit(buf, dnode, 0); 1773 tipc_link_xmit_skb(skb, dnode, 0);
1627 return (rc < 0) ? -EHOSTUNREACH : 0; 1774 return (rc < 0) ? -EHOSTUNREACH : 0;
1628} 1775}
1629 1776
@@ -1803,10 +1950,8 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1803{ 1950{
1804 struct sock *new_sk, *sk = sock->sk; 1951 struct sock *new_sk, *sk = sock->sk;
1805 struct sk_buff *buf; 1952 struct sk_buff *buf;
1806 struct tipc_port *new_port; 1953 struct tipc_sock *new_tsock;
1807 struct tipc_msg *msg; 1954 struct tipc_msg *msg;
1808 struct tipc_portid peer;
1809 u32 new_ref;
1810 long timeo; 1955 long timeo;
1811 int res; 1956 int res;
1812 1957
@@ -1828,8 +1973,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1828 goto exit; 1973 goto exit;
1829 1974
1830 new_sk = new_sock->sk; 1975 new_sk = new_sock->sk;
1831 new_port = &tipc_sk(new_sk)->port; 1976 new_tsock = tipc_sk(new_sk);
1832 new_ref = new_port->ref;
1833 msg = buf_msg(buf); 1977 msg = buf_msg(buf);
1834 1978
1835 /* we lock on new_sk; but lockdep sees the lock on sk */ 1979 /* we lock on new_sk; but lockdep sees the lock on sk */
@@ -1839,18 +1983,16 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1839 * Reject any stray messages received by new socket 1983 * Reject any stray messages received by new socket
1840 * before the socket lock was taken (very, very unlikely) 1984 * before the socket lock was taken (very, very unlikely)
1841 */ 1985 */
1842 reject_rx_queue(new_sk); 1986 tsk_rej_rx_queue(new_sk);
1843 1987
1844 /* Connect new socket to it's peer */ 1988 /* Connect new socket to it's peer */
1845 peer.ref = msg_origport(msg); 1989 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
1846 peer.node = msg_orignode(msg);
1847 tipc_port_connect(new_ref, &peer);
1848 new_sock->state = SS_CONNECTED; 1990 new_sock->state = SS_CONNECTED;
1849 1991
1850 tipc_port_set_importance(new_port, msg_importance(msg)); 1992 tsk_set_importance(new_tsock, msg_importance(msg));
1851 if (msg_named(msg)) { 1993 if (msg_named(msg)) {
1852 new_port->conn_type = msg_nametype(msg); 1994 new_tsock->conn_type = msg_nametype(msg);
1853 new_port->conn_instance = msg_nameinst(msg); 1995 new_tsock->conn_instance = msg_nameinst(msg);
1854 } 1996 }
1855 1997
1856 /* 1998 /*
@@ -1860,7 +2002,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1860 if (!msg_data_sz(msg)) { 2002 if (!msg_data_sz(msg)) {
1861 struct msghdr m = {NULL,}; 2003 struct msghdr m = {NULL,};
1862 2004
1863 advance_rx_queue(sk); 2005 tsk_advance_rx_queue(sk);
1864 tipc_send_packet(NULL, new_sock, &m, 0); 2006 tipc_send_packet(NULL, new_sock, &m, 0);
1865 } else { 2007 } else {
1866 __skb_dequeue(&sk->sk_receive_queue); 2008 __skb_dequeue(&sk->sk_receive_queue);
@@ -1886,9 +2028,8 @@ static int tipc_shutdown(struct socket *sock, int how)
1886{ 2028{
1887 struct sock *sk = sock->sk; 2029 struct sock *sk = sock->sk;
1888 struct tipc_sock *tsk = tipc_sk(sk); 2030 struct tipc_sock *tsk = tipc_sk(sk);
1889 struct tipc_port *port = &tsk->port; 2031 struct sk_buff *skb;
1890 struct sk_buff *buf; 2032 u32 dnode;
1891 u32 peer;
1892 int res; 2033 int res;
1893 2034
1894 if (how != SHUT_RDWR) 2035 if (how != SHUT_RDWR)
@@ -1902,21 +2043,27 @@ static int tipc_shutdown(struct socket *sock, int how)
1902 2043
1903restart: 2044restart:
1904 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ 2045 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1905 buf = __skb_dequeue(&sk->sk_receive_queue); 2046 skb = __skb_dequeue(&sk->sk_receive_queue);
1906 if (buf) { 2047 if (skb) {
1907 if (TIPC_SKB_CB(buf)->handle != NULL) { 2048 if (TIPC_SKB_CB(skb)->handle != NULL) {
1908 kfree_skb(buf); 2049 kfree_skb(skb);
1909 goto restart; 2050 goto restart;
1910 } 2051 }
1911 tipc_port_disconnect(port->ref); 2052 if (tipc_msg_reverse(skb, &dnode, TIPC_CONN_SHUTDOWN))
1912 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN)) 2053 tipc_link_xmit_skb(skb, dnode, tsk->ref);
1913 tipc_link_xmit(buf, peer, 0); 2054 tipc_node_remove_conn(dnode, tsk->ref);
1914 } else { 2055 } else {
1915 tipc_port_shutdown(port->ref); 2056 dnode = tsk_peer_node(tsk);
2057 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2058 TIPC_CONN_MSG, SHORT_H_SIZE,
2059 0, dnode, tipc_own_addr,
2060 tsk_peer_port(tsk),
2061 tsk->ref, TIPC_CONN_SHUTDOWN);
2062 tipc_link_xmit_skb(skb, dnode, tsk->ref);
1916 } 2063 }
1917 2064 tsk->connected = 0;
1918 sock->state = SS_DISCONNECTING; 2065 sock->state = SS_DISCONNECTING;
1919 2066 tipc_node_remove_conn(dnode, tsk->ref);
1920 /* fall through */ 2067 /* fall through */
1921 2068
1922 case SS_DISCONNECTING: 2069 case SS_DISCONNECTING:
@@ -1937,6 +2084,432 @@ restart:
1937 return res; 2084 return res;
1938} 2085}
1939 2086
2087static void tipc_sk_timeout(unsigned long ref)
2088{
2089 struct tipc_sock *tsk;
2090 struct sock *sk;
2091 struct sk_buff *skb = NULL;
2092 u32 peer_port, peer_node;
2093
2094 tsk = tipc_sk_get(ref);
2095 if (!tsk)
2096 return;
2097
2098 sk = &tsk->sk;
2099 bh_lock_sock(sk);
2100 if (!tsk->connected) {
2101 bh_unlock_sock(sk);
2102 goto exit;
2103 }
2104 peer_port = tsk_peer_port(tsk);
2105 peer_node = tsk_peer_node(tsk);
2106
2107 if (tsk->probing_state == TIPC_CONN_PROBING) {
2108 /* Previous probe not answered -> self abort */
2109 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2110 SHORT_H_SIZE, 0, tipc_own_addr,
2111 peer_node, ref, peer_port,
2112 TIPC_ERR_NO_PORT);
2113 } else {
2114 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2115 0, peer_node, tipc_own_addr,
2116 peer_port, ref, TIPC_OK);
2117 tsk->probing_state = TIPC_CONN_PROBING;
2118 k_start_timer(&tsk->timer, tsk->probing_interval);
2119 }
2120 bh_unlock_sock(sk);
2121 if (skb)
2122 tipc_link_xmit_skb(skb, peer_node, ref);
2123exit:
2124 tipc_sk_put(tsk);
2125}
2126
2127static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2128 struct tipc_name_seq const *seq)
2129{
2130 struct publication *publ;
2131 u32 key;
2132
2133 if (tsk->connected)
2134 return -EINVAL;
2135 key = tsk->ref + tsk->pub_count + 1;
2136 if (key == tsk->ref)
2137 return -EADDRINUSE;
2138
2139 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
2140 scope, tsk->ref, key);
2141 if (unlikely(!publ))
2142 return -EINVAL;
2143
2144 list_add(&publ->pport_list, &tsk->publications);
2145 tsk->pub_count++;
2146 tsk->published = 1;
2147 return 0;
2148}
2149
2150static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2151 struct tipc_name_seq const *seq)
2152{
2153 struct publication *publ;
2154 struct publication *safe;
2155 int rc = -EINVAL;
2156
2157 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2158 if (seq) {
2159 if (publ->scope != scope)
2160 continue;
2161 if (publ->type != seq->type)
2162 continue;
2163 if (publ->lower != seq->lower)
2164 continue;
2165 if (publ->upper != seq->upper)
2166 break;
2167 tipc_nametbl_withdraw(publ->type, publ->lower,
2168 publ->ref, publ->key);
2169 rc = 0;
2170 break;
2171 }
2172 tipc_nametbl_withdraw(publ->type, publ->lower,
2173 publ->ref, publ->key);
2174 rc = 0;
2175 }
2176 if (list_empty(&tsk->publications))
2177 tsk->published = 0;
2178 return rc;
2179}
2180
2181static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
2182 int len, int full_id)
2183{
2184 struct publication *publ;
2185 int ret;
2186
2187 if (full_id)
2188 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2189 tipc_zone(tipc_own_addr),
2190 tipc_cluster(tipc_own_addr),
2191 tipc_node(tipc_own_addr), tsk->ref);
2192 else
2193 ret = tipc_snprintf(buf, len, "%-10u:", tsk->ref);
2194
2195 if (tsk->connected) {
2196 u32 dport = tsk_peer_port(tsk);
2197 u32 destnode = tsk_peer_node(tsk);
2198
2199 ret += tipc_snprintf(buf + ret, len - ret,
2200 " connected to <%u.%u.%u:%u>",
2201 tipc_zone(destnode),
2202 tipc_cluster(destnode),
2203 tipc_node(destnode), dport);
2204 if (tsk->conn_type != 0)
2205 ret += tipc_snprintf(buf + ret, len - ret,
2206 " via {%u,%u}", tsk->conn_type,
2207 tsk->conn_instance);
2208 } else if (tsk->published) {
2209 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2210 list_for_each_entry(publ, &tsk->publications, pport_list) {
2211 if (publ->lower == publ->upper)
2212 ret += tipc_snprintf(buf + ret, len - ret,
2213 " {%u,%u}", publ->type,
2214 publ->lower);
2215 else
2216 ret += tipc_snprintf(buf + ret, len - ret,
2217 " {%u,%u,%u}", publ->type,
2218 publ->lower, publ->upper);
2219 }
2220 }
2221 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2222 return ret;
2223}
2224
2225struct sk_buff *tipc_sk_socks_show(void)
2226{
2227 struct sk_buff *buf;
2228 struct tlv_desc *rep_tlv;
2229 char *pb;
2230 int pb_len;
2231 struct tipc_sock *tsk;
2232 int str_len = 0;
2233 u32 ref = 0;
2234
2235 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2236 if (!buf)
2237 return NULL;
2238 rep_tlv = (struct tlv_desc *)buf->data;
2239 pb = TLV_DATA(rep_tlv);
2240 pb_len = ULTRA_STRING_MAX_LEN;
2241
2242 tsk = tipc_sk_get_next(&ref);
2243 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2244 lock_sock(&tsk->sk);
2245 str_len += tipc_sk_show(tsk, pb + str_len,
2246 pb_len - str_len, 0);
2247 release_sock(&tsk->sk);
2248 tipc_sk_put(tsk);
2249 }
2250 str_len += 1; /* for "\0" */
2251 skb_put(buf, TLV_SPACE(str_len));
2252 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2253
2254 return buf;
2255}
2256
2257/* tipc_sk_reinit: set non-zero address in all existing sockets
2258 * when we go from standalone to network mode.
2259 */
2260void tipc_sk_reinit(void)
2261{
2262 struct tipc_msg *msg;
2263 u32 ref = 0;
2264 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
2265
2266 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2267 lock_sock(&tsk->sk);
2268 msg = &tsk->phdr;
2269 msg_set_prevnode(msg, tipc_own_addr);
2270 msg_set_orignode(msg, tipc_own_addr);
2271 release_sock(&tsk->sk);
2272 tipc_sk_put(tsk);
2273 }
2274}
2275
2276/**
2277 * struct reference - TIPC socket reference entry
2278 * @tsk: pointer to socket associated with reference entry
2279 * @ref: reference value for socket (combines instance & array index info)
2280 */
2281struct reference {
2282 struct tipc_sock *tsk;
2283 u32 ref;
2284};
2285
2286/**
2287 * struct tipc_ref_table - table of TIPC socket reference entries
2288 * @entries: pointer to array of reference entries
2289 * @capacity: array index of first unusable entry
2290 * @init_point: array index of first uninitialized entry
2291 * @first_free: array index of first unused socket reference entry
2292 * @last_free: array index of last unused socket reference entry
2293 * @index_mask: bitmask for array index portion of reference values
2294 * @start_mask: initial value for instance value portion of reference values
2295 */
2296struct ref_table {
2297 struct reference *entries;
2298 u32 capacity;
2299 u32 init_point;
2300 u32 first_free;
2301 u32 last_free;
2302 u32 index_mask;
2303 u32 start_mask;
2304};
2305
2306/* Socket reference table consists of 2**N entries.
2307 *
2308 * State Socket ptr Reference
2309 * ----- ---------- ---------
2310 * In use non-NULL XXXX|own index
2311 * (XXXX changes each time entry is acquired)
2312 * Free NULL YYYY|next free index
2313 * (YYYY is one more than last used XXXX)
2314 * Uninitialized NULL 0
2315 *
2316 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
2317 *
2318 * Note that a reference value of 0 does not necessarily indicate that an
2319 * entry is uninitialized, since the last entry in the free list could also
2320 * have a reference value of 0 (although this is unlikely).
2321 */
2322
2323static struct ref_table tipc_ref_table;
2324
2325static DEFINE_RWLOCK(ref_table_lock);
2326
2327/**
2328 * tipc_ref_table_init - create reference table for sockets
2329 */
2330int tipc_sk_ref_table_init(u32 req_sz, u32 start)
2331{
2332 struct reference *table;
2333 u32 actual_sz;
2334
2335 /* account for unused entry, then round up size to a power of 2 */
2336
2337 req_sz++;
2338 for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) {
2339 /* do nothing */
2340 };
2341
2342 /* allocate table & mark all entries as uninitialized */
2343 table = vzalloc(actual_sz * sizeof(struct reference));
2344 if (table == NULL)
2345 return -ENOMEM;
2346
2347 tipc_ref_table.entries = table;
2348 tipc_ref_table.capacity = req_sz;
2349 tipc_ref_table.init_point = 1;
2350 tipc_ref_table.first_free = 0;
2351 tipc_ref_table.last_free = 0;
2352 tipc_ref_table.index_mask = actual_sz - 1;
2353 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
2354
2355 return 0;
2356}
2357
2358/**
2359 * tipc_ref_table_stop - destroy reference table for sockets
2360 */
2361void tipc_sk_ref_table_stop(void)
2362{
2363 if (!tipc_ref_table.entries)
2364 return;
2365 vfree(tipc_ref_table.entries);
2366 tipc_ref_table.entries = NULL;
2367}
2368
2369/* tipc_ref_acquire - create reference to a socket
2370 *
2371 * Register an socket pointer in the reference table.
2372 * Returns a unique reference value that is used from then on to retrieve the
2373 * socket pointer, or to determine if the socket has been deregistered.
2374 */
2375u32 tipc_sk_ref_acquire(struct tipc_sock *tsk)
2376{
2377 u32 index;
2378 u32 index_mask;
2379 u32 next_plus_upper;
2380 u32 ref = 0;
2381 struct reference *entry;
2382
2383 if (unlikely(!tsk)) {
2384 pr_err("Attempt to acquire ref. to non-existent obj\n");
2385 return 0;
2386 }
2387 if (unlikely(!tipc_ref_table.entries)) {
2388 pr_err("Ref. table not found in acquisition attempt\n");
2389 return 0;
2390 }
2391
2392 /* Take a free entry, if available; otherwise initialize a new one */
2393 write_lock_bh(&ref_table_lock);
2394 index = tipc_ref_table.first_free;
2395 entry = &tipc_ref_table.entries[index];
2396
2397 if (likely(index)) {
2398 index = tipc_ref_table.first_free;
2399 entry = &tipc_ref_table.entries[index];
2400 index_mask = tipc_ref_table.index_mask;
2401 next_plus_upper = entry->ref;
2402 tipc_ref_table.first_free = next_plus_upper & index_mask;
2403 ref = (next_plus_upper & ~index_mask) + index;
2404 entry->tsk = tsk;
2405 } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
2406 index = tipc_ref_table.init_point++;
2407 entry = &tipc_ref_table.entries[index];
2408 ref = tipc_ref_table.start_mask + index;
2409 }
2410
2411 if (ref) {
2412 entry->ref = ref;
2413 entry->tsk = tsk;
2414 }
2415 write_unlock_bh(&ref_table_lock);
2416 return ref;
2417}
2418
2419/* tipc_sk_ref_discard - invalidate reference to an socket
2420 *
2421 * Disallow future references to an socket and free up the entry for re-use.
2422 */
2423void tipc_sk_ref_discard(u32 ref)
2424{
2425 struct reference *entry;
2426 u32 index;
2427 u32 index_mask;
2428
2429 if (unlikely(!tipc_ref_table.entries)) {
2430 pr_err("Ref. table not found during discard attempt\n");
2431 return;
2432 }
2433
2434 index_mask = tipc_ref_table.index_mask;
2435 index = ref & index_mask;
2436 entry = &tipc_ref_table.entries[index];
2437
2438 write_lock_bh(&ref_table_lock);
2439
2440 if (unlikely(!entry->tsk)) {
2441 pr_err("Attempt to discard ref. to non-existent socket\n");
2442 goto exit;
2443 }
2444 if (unlikely(entry->ref != ref)) {
2445 pr_err("Attempt to discard non-existent reference\n");
2446 goto exit;
2447 }
2448
2449 /* Mark entry as unused; increment instance part of entry's
2450 * reference to invalidate any subsequent references
2451 */
2452
2453 entry->tsk = NULL;
2454 entry->ref = (ref & ~index_mask) + (index_mask + 1);
2455
2456 /* Append entry to free entry list */
2457 if (unlikely(tipc_ref_table.first_free == 0))
2458 tipc_ref_table.first_free = index;
2459 else
2460 tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
2461 tipc_ref_table.last_free = index;
2462exit:
2463 write_unlock_bh(&ref_table_lock);
2464}
2465
2466/* tipc_sk_get - find referenced socket and return pointer to it
2467 */
2468struct tipc_sock *tipc_sk_get(u32 ref)
2469{
2470 struct reference *entry;
2471 struct tipc_sock *tsk;
2472
2473 if (unlikely(!tipc_ref_table.entries))
2474 return NULL;
2475 read_lock_bh(&ref_table_lock);
2476 entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
2477 tsk = entry->tsk;
2478 if (likely(tsk && (entry->ref == ref)))
2479 sock_hold(&tsk->sk);
2480 else
2481 tsk = NULL;
2482 read_unlock_bh(&ref_table_lock);
2483 return tsk;
2484}
2485
2486/* tipc_sk_get_next - lock & return next socket after referenced one
2487*/
2488struct tipc_sock *tipc_sk_get_next(u32 *ref)
2489{
2490 struct reference *entry;
2491 struct tipc_sock *tsk = NULL;
2492 uint index = *ref & tipc_ref_table.index_mask;
2493
2494 read_lock_bh(&ref_table_lock);
2495 while (++index < tipc_ref_table.capacity) {
2496 entry = &tipc_ref_table.entries[index];
2497 if (!entry->tsk)
2498 continue;
2499 tsk = entry->tsk;
2500 sock_hold(&tsk->sk);
2501 *ref = entry->ref;
2502 break;
2503 }
2504 read_unlock_bh(&ref_table_lock);
2505 return tsk;
2506}
2507
2508static void tipc_sk_put(struct tipc_sock *tsk)
2509{
2510 sock_put(&tsk->sk);
2511}
2512
1940/** 2513/**
1941 * tipc_setsockopt - set socket option 2514 * tipc_setsockopt - set socket option
1942 * @sock: socket structure 2515 * @sock: socket structure
@@ -1955,7 +2528,6 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1955{ 2528{
1956 struct sock *sk = sock->sk; 2529 struct sock *sk = sock->sk;
1957 struct tipc_sock *tsk = tipc_sk(sk); 2530 struct tipc_sock *tsk = tipc_sk(sk);
1958 struct tipc_port *port = &tsk->port;
1959 u32 value; 2531 u32 value;
1960 int res; 2532 int res;
1961 2533
@@ -1973,16 +2545,16 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1973 2545
1974 switch (opt) { 2546 switch (opt) {
1975 case TIPC_IMPORTANCE: 2547 case TIPC_IMPORTANCE:
1976 res = tipc_port_set_importance(port, value); 2548 res = tsk_set_importance(tsk, value);
1977 break; 2549 break;
1978 case TIPC_SRC_DROPPABLE: 2550 case TIPC_SRC_DROPPABLE:
1979 if (sock->type != SOCK_STREAM) 2551 if (sock->type != SOCK_STREAM)
1980 tipc_port_set_unreliable(port, value); 2552 tsk_set_unreliable(tsk, value);
1981 else 2553 else
1982 res = -ENOPROTOOPT; 2554 res = -ENOPROTOOPT;
1983 break; 2555 break;
1984 case TIPC_DEST_DROPPABLE: 2556 case TIPC_DEST_DROPPABLE:
1985 tipc_port_set_unreturnable(port, value); 2557 tsk_set_unreturnable(tsk, value);
1986 break; 2558 break;
1987 case TIPC_CONN_TIMEOUT: 2559 case TIPC_CONN_TIMEOUT:
1988 tipc_sk(sk)->conn_timeout = value; 2560 tipc_sk(sk)->conn_timeout = value;
@@ -2015,7 +2587,6 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2015{ 2587{
2016 struct sock *sk = sock->sk; 2588 struct sock *sk = sock->sk;
2017 struct tipc_sock *tsk = tipc_sk(sk); 2589 struct tipc_sock *tsk = tipc_sk(sk);
2018 struct tipc_port *port = &tsk->port;
2019 int len; 2590 int len;
2020 u32 value; 2591 u32 value;
2021 int res; 2592 int res;
@@ -2032,16 +2603,16 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2032 2603
2033 switch (opt) { 2604 switch (opt) {
2034 case TIPC_IMPORTANCE: 2605 case TIPC_IMPORTANCE:
2035 value = tipc_port_importance(port); 2606 value = tsk_importance(tsk);
2036 break; 2607 break;
2037 case TIPC_SRC_DROPPABLE: 2608 case TIPC_SRC_DROPPABLE:
2038 value = tipc_port_unreliable(port); 2609 value = tsk_unreliable(tsk);
2039 break; 2610 break;
2040 case TIPC_DEST_DROPPABLE: 2611 case TIPC_DEST_DROPPABLE:
2041 value = tipc_port_unreturnable(port); 2612 value = tsk_unreturnable(tsk);
2042 break; 2613 break;
2043 case TIPC_CONN_TIMEOUT: 2614 case TIPC_CONN_TIMEOUT:
2044 value = tipc_sk(sk)->conn_timeout; 2615 value = tsk->conn_timeout;
2045 /* no need to set "res", since already 0 at this point */ 2616 /* no need to set "res", since already 0 at this point */
2046 break; 2617 break;
2047 case TIPC_NODE_RECVQ_DEPTH: 2618 case TIPC_NODE_RECVQ_DEPTH:
@@ -2077,7 +2648,7 @@ static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2077 case SIOCGETLINKNAME: 2648 case SIOCGETLINKNAME:
2078 if (copy_from_user(&lnr, argp, sizeof(lnr))) 2649 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2079 return -EFAULT; 2650 return -EFAULT;
2080 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer, 2651 if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer,
2081 lnr.linkname, TIPC_MAX_LINK_NAME)) { 2652 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2082 if (copy_to_user(argp, &lnr, sizeof(lnr))) 2653 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2083 return -EFAULT; 2654 return -EFAULT;
@@ -2206,3 +2777,233 @@ void tipc_socket_stop(void)
2206 sock_unregister(tipc_family_ops.family); 2777 sock_unregister(tipc_family_ops.family);
2207 proto_unregister(&tipc_proto); 2778 proto_unregister(&tipc_proto);
2208} 2779}
2780
2781/* Caller should hold socket lock for the passed tipc socket. */
2782static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2783{
2784 u32 peer_node;
2785 u32 peer_port;
2786 struct nlattr *nest;
2787
2788 peer_node = tsk_peer_node(tsk);
2789 peer_port = tsk_peer_port(tsk);
2790
2791 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2792
2793 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2794 goto msg_full;
2795 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2796 goto msg_full;
2797
2798 if (tsk->conn_type != 0) {
2799 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2800 goto msg_full;
2801 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2802 goto msg_full;
2803 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2804 goto msg_full;
2805 }
2806 nla_nest_end(skb, nest);
2807
2808 return 0;
2809
2810msg_full:
2811 nla_nest_cancel(skb, nest);
2812
2813 return -EMSGSIZE;
2814}
2815
2816/* Caller should hold socket lock for the passed tipc socket. */
2817static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2818 struct tipc_sock *tsk)
2819{
2820 int err;
2821 void *hdr;
2822 struct nlattr *attrs;
2823
2824 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2825 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2826 if (!hdr)
2827 goto msg_cancel;
2828
2829 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2830 if (!attrs)
2831 goto genlmsg_cancel;
2832 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->ref))
2833 goto attr_msg_cancel;
2834 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
2835 goto attr_msg_cancel;
2836
2837 if (tsk->connected) {
2838 err = __tipc_nl_add_sk_con(skb, tsk);
2839 if (err)
2840 goto attr_msg_cancel;
2841 } else if (!list_empty(&tsk->publications)) {
2842 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2843 goto attr_msg_cancel;
2844 }
2845 nla_nest_end(skb, attrs);
2846 genlmsg_end(skb, hdr);
2847
2848 return 0;
2849
2850attr_msg_cancel:
2851 nla_nest_cancel(skb, attrs);
2852genlmsg_cancel:
2853 genlmsg_cancel(skb, hdr);
2854msg_cancel:
2855 return -EMSGSIZE;
2856}
2857
2858int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2859{
2860 int err;
2861 struct tipc_sock *tsk;
2862 u32 prev_ref = cb->args[0];
2863 u32 ref = prev_ref;
2864
2865 tsk = tipc_sk_get_next(&ref);
2866 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2867 lock_sock(&tsk->sk);
2868 err = __tipc_nl_add_sk(skb, cb, tsk);
2869 release_sock(&tsk->sk);
2870 tipc_sk_put(tsk);
2871 if (err)
2872 break;
2873
2874 prev_ref = ref;
2875 }
2876
2877 cb->args[0] = prev_ref;
2878
2879 return skb->len;
2880}
2881
2882/* Caller should hold socket lock for the passed tipc socket. */
2883static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2884 struct netlink_callback *cb,
2885 struct publication *publ)
2886{
2887 void *hdr;
2888 struct nlattr *attrs;
2889
2890 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2891 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2892 if (!hdr)
2893 goto msg_cancel;
2894
2895 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2896 if (!attrs)
2897 goto genlmsg_cancel;
2898
2899 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2900 goto attr_msg_cancel;
2901 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2902 goto attr_msg_cancel;
2903 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2904 goto attr_msg_cancel;
2905 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2906 goto attr_msg_cancel;
2907
2908 nla_nest_end(skb, attrs);
2909 genlmsg_end(skb, hdr);
2910
2911 return 0;
2912
2913attr_msg_cancel:
2914 nla_nest_cancel(skb, attrs);
2915genlmsg_cancel:
2916 genlmsg_cancel(skb, hdr);
2917msg_cancel:
2918 return -EMSGSIZE;
2919}
2920
2921/* Caller should hold socket lock for the passed tipc socket. */
2922static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2923 struct netlink_callback *cb,
2924 struct tipc_sock *tsk, u32 *last_publ)
2925{
2926 int err;
2927 struct publication *p;
2928
2929 if (*last_publ) {
2930 list_for_each_entry(p, &tsk->publications, pport_list) {
2931 if (p->key == *last_publ)
2932 break;
2933 }
2934 if (p->key != *last_publ) {
2935 /* We never set seq or call nl_dump_check_consistent()
2936 * this means that setting prev_seq here will cause the
2937 * consistence check to fail in the netlink callback
2938 * handler. Resulting in the last NLMSG_DONE message
2939 * having the NLM_F_DUMP_INTR flag set.
2940 */
2941 cb->prev_seq = 1;
2942 *last_publ = 0;
2943 return -EPIPE;
2944 }
2945 } else {
2946 p = list_first_entry(&tsk->publications, struct publication,
2947 pport_list);
2948 }
2949
2950 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2951 err = __tipc_nl_add_sk_publ(skb, cb, p);
2952 if (err) {
2953 *last_publ = p->key;
2954 return err;
2955 }
2956 }
2957 *last_publ = 0;
2958
2959 return 0;
2960}
2961
2962int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2963{
2964 int err;
2965 u32 tsk_ref = cb->args[0];
2966 u32 last_publ = cb->args[1];
2967 u32 done = cb->args[2];
2968 struct tipc_sock *tsk;
2969
2970 if (!tsk_ref) {
2971 struct nlattr **attrs;
2972 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2973
2974 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2975 if (err)
2976 return err;
2977
2978 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2979 attrs[TIPC_NLA_SOCK],
2980 tipc_nl_sock_policy);
2981 if (err)
2982 return err;
2983
2984 if (!sock[TIPC_NLA_SOCK_REF])
2985 return -EINVAL;
2986
2987 tsk_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2988 }
2989
2990 if (done)
2991 return 0;
2992
2993 tsk = tipc_sk_get(tsk_ref);
2994 if (!tsk)
2995 return -EINVAL;
2996
2997 lock_sock(&tsk->sk);
2998 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2999 if (!err)
3000 done = 1;
3001 release_sock(&tsk->sk);
3002 tipc_sk_put(tsk);
3003
3004 cb->args[0] = tsk_ref;
3005 cb->args[1] = last_publ;
3006 cb->args[2] = done;
3007
3008 return skb->len;
3009}