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.c1383
1 files changed, 1055 insertions, 328 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index ef0475568f9e..75275c5cf929 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -35,21 +35,84 @@
35 */ 35 */
36 36
37#include "core.h" 37#include "core.h"
38#include "port.h" 38#include "name_table.h"
39#include "node.h" 39#include "node.h"
40 40#include "link.h"
41#include <linux/export.h> 41#include <linux/export.h>
42#include "config.h"
43#include "socket.h"
42 44
43#define SS_LISTENING -1 /* socket is listening */ 45#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */ 46#define SS_READY -2 /* socket is connectionless */
45 47
46#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ 48#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
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};
47 99
48static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); 100static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
49static void tipc_data_ready(struct sock *sk); 101static void tipc_data_ready(struct sock *sk);
50static void tipc_write_space(struct sock *sk); 102static void tipc_write_space(struct sock *sk);
51static int tipc_release(struct socket *sock); 103static int tipc_release(struct socket *sock);
52static 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);
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);
53 116
54static const struct proto_ops packet_ops; 117static const struct proto_ops packet_ops;
55static const struct proto_ops stream_ops; 118static const struct proto_ops stream_ops;
@@ -103,29 +166,115 @@ static struct proto tipc_proto_kern;
103 * - port reference 166 * - port reference
104 */ 167 */
105 168
106#include "socket.h" 169static u32 tsk_peer_node(struct tipc_sock *tsk)
170{
171 return msg_destnode(&tsk->phdr);
172}
173
174static u32 tsk_peer_port(struct tipc_sock *tsk)
175{
176 return msg_destport(&tsk->phdr);
177}
178
179static bool tsk_unreliable(struct tipc_sock *tsk)
180{
181 return msg_src_droppable(&tsk->phdr) != 0;
182}
183
184static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
185{
186 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
187}
188
189static bool tsk_unreturnable(struct tipc_sock *tsk)
190{
191 return msg_dest_droppable(&tsk->phdr) != 0;
192}
193
194static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
195{
196 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
197}
198
199static int tsk_importance(struct tipc_sock *tsk)
200{
201 return msg_importance(&tsk->phdr);
202}
203
204static int tsk_set_importance(struct tipc_sock *tsk, int imp)
205{
206 if (imp > TIPC_CRITICAL_IMPORTANCE)
207 return -EINVAL;
208 msg_set_importance(&tsk->phdr, (u32)imp);
209 return 0;
210}
211
212static struct tipc_sock *tipc_sk(const struct sock *sk)
213{
214 return container_of(sk, struct tipc_sock, sk);
215}
216
217static int tsk_conn_cong(struct tipc_sock *tsk)
218{
219 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
220}
107 221
108/** 222/**
109 * advance_rx_queue - discard first buffer in socket receive queue 223 * tsk_advance_rx_queue - discard first buffer in socket receive queue
110 * 224 *
111 * Caller must hold socket lock 225 * Caller must hold socket lock
112 */ 226 */
113static void advance_rx_queue(struct sock *sk) 227static void tsk_advance_rx_queue(struct sock *sk)
114{ 228{
115 kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); 229 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
116} 230}
117 231
118/** 232/**
119 * reject_rx_queue - reject all buffers in socket receive queue 233 * tsk_rej_rx_queue - reject all buffers in socket receive queue
120 * 234 *
121 * Caller must hold socket lock 235 * Caller must hold socket lock
122 */ 236 */
123static void reject_rx_queue(struct sock *sk) 237static void tsk_rej_rx_queue(struct sock *sk)
124{ 238{
125 struct sk_buff *buf; 239 struct sk_buff *buf;
240 u32 dnode;
241
242 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
243 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
244 tipc_link_xmit(buf, dnode, 0);
245 }
246}
247
248/* tsk_peer_msg - verify if message was sent by connected port's peer
249 *
250 * Handles cases where the node's network address has changed from
251 * the default of <0.0.0> to its configured setting.
252 */
253static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
254{
255 u32 peer_port = tsk_peer_port(tsk);
256 u32 orig_node;
257 u32 peer_node;
258
259 if (unlikely(!tsk->connected))
260 return false;
126 261
127 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) 262 if (unlikely(msg_origport(msg) != peer_port))
128 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 263 return false;
264
265 orig_node = msg_orignode(msg);
266 peer_node = tsk_peer_node(tsk);
267
268 if (likely(orig_node == peer_node))
269 return true;
270
271 if (!orig_node && (peer_node == tipc_own_addr))
272 return true;
273
274 if (!peer_node && (orig_node == tipc_own_addr))
275 return true;
276
277 return false;
129} 278}
130 279
131/** 280/**
@@ -147,7 +296,7 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
147 socket_state state; 296 socket_state state;
148 struct sock *sk; 297 struct sock *sk;
149 struct tipc_sock *tsk; 298 struct tipc_sock *tsk;
150 struct tipc_port *port; 299 struct tipc_msg *msg;
151 u32 ref; 300 u32 ref;
152 301
153 /* Validate arguments */ 302 /* Validate arguments */
@@ -182,32 +331,36 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
182 return -ENOMEM; 331 return -ENOMEM;
183 332
184 tsk = tipc_sk(sk); 333 tsk = tipc_sk(sk);
185 port = &tsk->port; 334 ref = tipc_sk_ref_acquire(tsk);
186
187 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
188 if (!ref) { 335 if (!ref) {
189 pr_warn("Socket registration failed, ref. table exhausted\n"); 336 pr_warn("Socket create failed; reference table exhausted\n");
190 sk_free(sk);
191 return -ENOMEM; 337 return -ENOMEM;
192 } 338 }
339 tsk->max_pkt = MAX_PKT_DEFAULT;
340 tsk->ref = ref;
341 INIT_LIST_HEAD(&tsk->publications);
342 msg = &tsk->phdr;
343 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
344 NAMED_H_SIZE, 0);
345 msg_set_origport(msg, ref);
193 346
194 /* Finish initializing socket data structures */ 347 /* Finish initializing socket data structures */
195 sock->ops = ops; 348 sock->ops = ops;
196 sock->state = state; 349 sock->state = state;
197
198 sock_init_data(sock, sk); 350 sock_init_data(sock, sk);
351 k_init_timer(&tsk->timer, (Handler)tipc_sk_timeout, ref);
199 sk->sk_backlog_rcv = tipc_backlog_rcv; 352 sk->sk_backlog_rcv = tipc_backlog_rcv;
200 sk->sk_rcvbuf = sysctl_tipc_rmem[1]; 353 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
201 sk->sk_data_ready = tipc_data_ready; 354 sk->sk_data_ready = tipc_data_ready;
202 sk->sk_write_space = tipc_write_space; 355 sk->sk_write_space = tipc_write_space;
203 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; 356 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
357 tsk->sent_unacked = 0;
204 atomic_set(&tsk->dupl_rcvcnt, 0); 358 atomic_set(&tsk->dupl_rcvcnt, 0);
205 tipc_port_unlock(port);
206 359
207 if (sock->state == SS_READY) { 360 if (sock->state == SS_READY) {
208 tipc_port_set_unreturnable(port, true); 361 tsk_set_unreturnable(tsk, true);
209 if (sock->type == SOCK_DGRAM) 362 if (sock->type == SOCK_DGRAM)
210 tipc_port_set_unreliable(port, true); 363 tsk_set_unreliable(tsk, true);
211 } 364 }
212 return 0; 365 return 0;
213} 366}
@@ -301,8 +454,8 @@ static int tipc_release(struct socket *sock)
301{ 454{
302 struct sock *sk = sock->sk; 455 struct sock *sk = sock->sk;
303 struct tipc_sock *tsk; 456 struct tipc_sock *tsk;
304 struct tipc_port *port;
305 struct sk_buff *buf; 457 struct sk_buff *buf;
458 u32 dnode;
306 459
307 /* 460 /*
308 * Exit if socket isn't fully initialized (occurs when a failed accept() 461 * Exit if socket isn't fully initialized (occurs when a failed accept()
@@ -312,13 +465,13 @@ static int tipc_release(struct socket *sock)
312 return 0; 465 return 0;
313 466
314 tsk = tipc_sk(sk); 467 tsk = tipc_sk(sk);
315 port = &tsk->port;
316 lock_sock(sk); 468 lock_sock(sk);
317 469
318 /* 470 /*
319 * Reject all unreceived messages, except on an active connection 471 * Reject all unreceived messages, except on an active connection
320 * (which disconnects locally & sends a 'FIN+' to peer) 472 * (which disconnects locally & sends a 'FIN+' to peer)
321 */ 473 */
474 dnode = tsk_peer_node(tsk);
322 while (sock->state != SS_DISCONNECTING) { 475 while (sock->state != SS_DISCONNECTING) {
323 buf = __skb_dequeue(&sk->sk_receive_queue); 476 buf = __skb_dequeue(&sk->sk_receive_queue);
324 if (buf == NULL) 477 if (buf == NULL)
@@ -329,16 +482,27 @@ static int tipc_release(struct socket *sock)
329 if ((sock->state == SS_CONNECTING) || 482 if ((sock->state == SS_CONNECTING) ||
330 (sock->state == SS_CONNECTED)) { 483 (sock->state == SS_CONNECTED)) {
331 sock->state = SS_DISCONNECTING; 484 sock->state = SS_DISCONNECTING;
332 tipc_port_disconnect(port->ref); 485 tsk->connected = 0;
486 tipc_node_remove_conn(dnode, tsk->ref);
333 } 487 }
334 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 488 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
489 tipc_link_xmit(buf, dnode, 0);
335 } 490 }
336 } 491 }
337 492
338 /* Destroy TIPC port; also disconnects an active connection and 493 tipc_sk_withdraw(tsk, 0, NULL);
339 * sends a 'FIN-' to peer. 494 tipc_sk_ref_discard(tsk->ref);
340 */ 495 k_cancel_timer(&tsk->timer);
341 tipc_port_destroy(port); 496 if (tsk->connected) {
497 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
498 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
499 tsk_peer_port(tsk),
500 tsk->ref, TIPC_ERR_NO_PORT);
501 if (buf)
502 tipc_link_xmit(buf, dnode, tsk->ref);
503 tipc_node_remove_conn(dnode, tsk->ref);
504 }
505 k_term_timer(&tsk->timer);
342 506
343 /* Discard any remaining (connection-based) messages in receive queue */ 507 /* Discard any remaining (connection-based) messages in receive queue */
344 __skb_queue_purge(&sk->sk_receive_queue); 508 __skb_queue_purge(&sk->sk_receive_queue);
@@ -346,7 +510,6 @@ static int tipc_release(struct socket *sock)
346 /* Reject any messages that accumulated in backlog queue */ 510 /* Reject any messages that accumulated in backlog queue */
347 sock->state = SS_DISCONNECTING; 511 sock->state = SS_DISCONNECTING;
348 release_sock(sk); 512 release_sock(sk);
349
350 sock_put(sk); 513 sock_put(sk);
351 sock->sk = NULL; 514 sock->sk = NULL;
352 515
@@ -378,7 +541,7 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
378 541
379 lock_sock(sk); 542 lock_sock(sk);
380 if (unlikely(!uaddr_len)) { 543 if (unlikely(!uaddr_len)) {
381 res = tipc_withdraw(&tsk->port, 0, NULL); 544 res = tipc_sk_withdraw(tsk, 0, NULL);
382 goto exit; 545 goto exit;
383 } 546 }
384 547
@@ -406,8 +569,8 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
406 } 569 }
407 570
408 res = (addr->scope > 0) ? 571 res = (addr->scope > 0) ?
409 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) : 572 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
410 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq); 573 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
411exit: 574exit:
412 release_sock(sk); 575 release_sock(sk);
413 return res; 576 return res;
@@ -437,10 +600,10 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
437 if ((sock->state != SS_CONNECTED) && 600 if ((sock->state != SS_CONNECTED) &&
438 ((peer != 2) || (sock->state != SS_DISCONNECTING))) 601 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
439 return -ENOTCONN; 602 return -ENOTCONN;
440 addr->addr.id.ref = tipc_port_peerport(&tsk->port); 603 addr->addr.id.ref = tsk_peer_port(tsk);
441 addr->addr.id.node = tipc_port_peernode(&tsk->port); 604 addr->addr.id.node = tsk_peer_node(tsk);
442 } else { 605 } else {
443 addr->addr.id.ref = tsk->port.ref; 606 addr->addr.id.ref = tsk->ref;
444 addr->addr.id.node = tipc_own_addr; 607 addr->addr.id.node = tipc_own_addr;
445 } 608 }
446 609
@@ -504,12 +667,12 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
504 667
505 switch ((int)sock->state) { 668 switch ((int)sock->state) {
506 case SS_UNCONNECTED: 669 case SS_UNCONNECTED:
507 if (!tsk->port.congested) 670 if (!tsk->link_cong)
508 mask |= POLLOUT; 671 mask |= POLLOUT;
509 break; 672 break;
510 case SS_READY: 673 case SS_READY:
511 case SS_CONNECTED: 674 case SS_CONNECTED:
512 if (!tsk->port.congested) 675 if (!tsk->link_cong && !tsk_conn_cong(tsk))
513 mask |= POLLOUT; 676 mask |= POLLOUT;
514 /* fall thru' */ 677 /* fall thru' */
515 case SS_CONNECTING: 678 case SS_CONNECTING:
@@ -526,6 +689,136 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
526} 689}
527 690
528/** 691/**
692 * tipc_sendmcast - send multicast message
693 * @sock: socket structure
694 * @seq: destination address
695 * @iov: message data to send
696 * @dsz: total length of message data
697 * @timeo: timeout to wait for wakeup
698 *
699 * Called from function tipc_sendmsg(), which has done all sanity checks
700 * Returns the number of bytes sent on success, or errno
701 */
702static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
703 struct iovec *iov, size_t dsz, long timeo)
704{
705 struct sock *sk = sock->sk;
706 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
707 struct sk_buff *buf;
708 uint mtu;
709 int rc;
710
711 msg_set_type(mhdr, TIPC_MCAST_MSG);
712 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
713 msg_set_destport(mhdr, 0);
714 msg_set_destnode(mhdr, 0);
715 msg_set_nametype(mhdr, seq->type);
716 msg_set_namelower(mhdr, seq->lower);
717 msg_set_nameupper(mhdr, seq->upper);
718 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
719
720new_mtu:
721 mtu = tipc_bclink_get_mtu();
722 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
723 if (unlikely(rc < 0))
724 return rc;
725
726 do {
727 rc = tipc_bclink_xmit(buf);
728 if (likely(rc >= 0)) {
729 rc = dsz;
730 break;
731 }
732 if (rc == -EMSGSIZE)
733 goto new_mtu;
734 if (rc != -ELINKCONG)
735 break;
736 tipc_sk(sk)->link_cong = 1;
737 rc = tipc_wait_for_sndmsg(sock, &timeo);
738 if (rc)
739 kfree_skb_list(buf);
740 } while (!rc);
741 return rc;
742}
743
744/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
745 */
746void tipc_sk_mcast_rcv(struct sk_buff *buf)
747{
748 struct tipc_msg *msg = buf_msg(buf);
749 struct tipc_port_list dports = {0, NULL, };
750 struct tipc_port_list *item;
751 struct sk_buff *b;
752 uint i, last, dst = 0;
753 u32 scope = TIPC_CLUSTER_SCOPE;
754
755 if (in_own_node(msg_orignode(msg)))
756 scope = TIPC_NODE_SCOPE;
757
758 /* Create destination port list: */
759 tipc_nametbl_mc_translate(msg_nametype(msg),
760 msg_namelower(msg),
761 msg_nameupper(msg),
762 scope,
763 &dports);
764 last = dports.count;
765 if (!last) {
766 kfree_skb(buf);
767 return;
768 }
769
770 for (item = &dports; item; item = item->next) {
771 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
772 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
773 if (!b) {
774 pr_warn("Failed do clone mcast rcv buffer\n");
775 continue;
776 }
777 msg_set_destport(msg, item->ports[i]);
778 tipc_sk_rcv(b);
779 }
780 }
781 tipc_port_list_free(&dports);
782}
783
784/**
785 * tipc_sk_proto_rcv - receive a connection mng protocol message
786 * @tsk: receiving socket
787 * @dnode: node to send response message to, if any
788 * @buf: buffer containing protocol message
789 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
790 * (CONN_PROBE_REPLY) message should be forwarded.
791 */
792static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
793 struct sk_buff *buf)
794{
795 struct tipc_msg *msg = buf_msg(buf);
796 int conn_cong;
797
798 /* Ignore if connection cannot be validated: */
799 if (!tsk_peer_msg(tsk, msg))
800 goto exit;
801
802 tsk->probing_state = TIPC_CONN_OK;
803
804 if (msg_type(msg) == CONN_ACK) {
805 conn_cong = tsk_conn_cong(tsk);
806 tsk->sent_unacked -= msg_msgcnt(msg);
807 if (conn_cong)
808 tsk->sk.sk_write_space(&tsk->sk);
809 } else if (msg_type(msg) == CONN_PROBE) {
810 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
811 return TIPC_OK;
812 msg_set_type(msg, CONN_PROBE_REPLY);
813 return TIPC_FWD_MSG;
814 }
815 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
816exit:
817 kfree_skb(buf);
818 return TIPC_OK;
819}
820
821/**
529 * dest_name_check - verify user is permitted to send to specified port name 822 * dest_name_check - verify user is permitted to send to specified port name
530 * @dest: destination address 823 * @dest: destination address
531 * @m: descriptor for message to be sent 824 * @m: descriptor for message to be sent
@@ -539,6 +832,8 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
539{ 832{
540 struct tipc_cfg_msg_hdr hdr; 833 struct tipc_cfg_msg_hdr hdr;
541 834
835 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
836 return 0;
542 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) 837 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
543 return 0; 838 return 0;
544 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) 839 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
@@ -575,19 +870,18 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
575 return sock_intr_errno(*timeo_p); 870 return sock_intr_errno(*timeo_p);
576 871
577 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 872 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
578 done = sk_wait_event(sk, timeo_p, !tsk->port.congested); 873 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
579 finish_wait(sk_sleep(sk), &wait); 874 finish_wait(sk_sleep(sk), &wait);
580 } while (!done); 875 } while (!done);
581 return 0; 876 return 0;
582} 877}
583 878
584
585/** 879/**
586 * tipc_sendmsg - send message in connectionless manner 880 * tipc_sendmsg - send message in connectionless manner
587 * @iocb: if NULL, indicates that socket lock is already held 881 * @iocb: if NULL, indicates that socket lock is already held
588 * @sock: socket structure 882 * @sock: socket structure
589 * @m: message to send 883 * @m: message to send
590 * @total_len: length of message 884 * @dsz: amount of user data to be sent
591 * 885 *
592 * Message must have an destination specified explicitly. 886 * Message must have an destination specified explicitly.
593 * Used for SOCK_RDM and SOCK_DGRAM messages, 887 * Used for SOCK_RDM and SOCK_DGRAM messages,
@@ -597,100 +891,122 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
597 * Returns the number of bytes sent on success, or errno otherwise 891 * Returns the number of bytes sent on success, or errno otherwise
598 */ 892 */
599static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, 893static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
600 struct msghdr *m, size_t total_len) 894 struct msghdr *m, size_t dsz)
601{ 895{
896 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
602 struct sock *sk = sock->sk; 897 struct sock *sk = sock->sk;
603 struct tipc_sock *tsk = tipc_sk(sk); 898 struct tipc_sock *tsk = tipc_sk(sk);
604 struct tipc_port *port = &tsk->port; 899 struct tipc_msg *mhdr = &tsk->phdr;
605 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 900 struct iovec *iov = m->msg_iov;
606 int needs_conn; 901 u32 dnode, dport;
902 struct sk_buff *buf;
903 struct tipc_name_seq *seq = &dest->addr.nameseq;
904 u32 mtu;
607 long timeo; 905 long timeo;
608 int res = -EINVAL; 906 int rc = -EINVAL;
609 907
610 if (unlikely(!dest)) 908 if (unlikely(!dest))
611 return -EDESTADDRREQ; 909 return -EDESTADDRREQ;
910
612 if (unlikely((m->msg_namelen < sizeof(*dest)) || 911 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
613 (dest->family != AF_TIPC))) 912 (dest->family != AF_TIPC)))
614 return -EINVAL; 913 return -EINVAL;
615 if (total_len > TIPC_MAX_USER_MSG_SIZE) 914
915 if (dsz > TIPC_MAX_USER_MSG_SIZE)
616 return -EMSGSIZE; 916 return -EMSGSIZE;
617 917
618 if (iocb) 918 if (iocb)
619 lock_sock(sk); 919 lock_sock(sk);
620 920
621 needs_conn = (sock->state != SS_READY); 921 if (unlikely(sock->state != SS_READY)) {
622 if (unlikely(needs_conn)) {
623 if (sock->state == SS_LISTENING) { 922 if (sock->state == SS_LISTENING) {
624 res = -EPIPE; 923 rc = -EPIPE;
625 goto exit; 924 goto exit;
626 } 925 }
627 if (sock->state != SS_UNCONNECTED) { 926 if (sock->state != SS_UNCONNECTED) {
628 res = -EISCONN; 927 rc = -EISCONN;
629 goto exit; 928 goto exit;
630 } 929 }
631 if (tsk->port.published) { 930 if (tsk->published) {
632 res = -EOPNOTSUPP; 931 rc = -EOPNOTSUPP;
633 goto exit; 932 goto exit;
634 } 933 }
635 if (dest->addrtype == TIPC_ADDR_NAME) { 934 if (dest->addrtype == TIPC_ADDR_NAME) {
636 tsk->port.conn_type = dest->addr.name.name.type; 935 tsk->conn_type = dest->addr.name.name.type;
637 tsk->port.conn_instance = dest->addr.name.name.instance; 936 tsk->conn_instance = dest->addr.name.name.instance;
638 } 937 }
639
640 /* Abort any pending connection attempts (very unlikely) */
641 reject_rx_queue(sk);
642 } 938 }
939 rc = dest_name_check(dest, m);
940 if (rc)
941 goto exit;
643 942
644 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 943 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
645 do { 944
646 if (dest->addrtype == TIPC_ADDR_NAME) { 945 if (dest->addrtype == TIPC_ADDR_MCAST) {
647 res = dest_name_check(dest, m); 946 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
648 if (res) 947 goto exit;
649 break; 948 } else if (dest->addrtype == TIPC_ADDR_NAME) {
650 res = tipc_send2name(port, 949 u32 type = dest->addr.name.name.type;
651 &dest->addr.name.name, 950 u32 inst = dest->addr.name.name.instance;
652 dest->addr.name.domain, 951 u32 domain = dest->addr.name.domain;
653 m->msg_iov, 952
654 total_len); 953 dnode = domain;
655 } else if (dest->addrtype == TIPC_ADDR_ID) { 954 msg_set_type(mhdr, TIPC_NAMED_MSG);
656 res = tipc_send2port(port, 955 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
657 &dest->addr.id, 956 msg_set_nametype(mhdr, type);
658 m->msg_iov, 957 msg_set_nameinst(mhdr, inst);
659 total_len); 958 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
660 } else if (dest->addrtype == TIPC_ADDR_MCAST) { 959 dport = tipc_nametbl_translate(type, inst, &dnode);
661 if (needs_conn) { 960 msg_set_destnode(mhdr, dnode);
662 res = -EOPNOTSUPP; 961 msg_set_destport(mhdr, dport);
663 break; 962 if (unlikely(!dport && !dnode)) {
664 } 963 rc = -EHOSTUNREACH;
665 res = dest_name_check(dest, m); 964 goto exit;
666 if (res)
667 break;
668 res = tipc_port_mcast_xmit(port,
669 &dest->addr.nameseq,
670 m->msg_iov,
671 total_len);
672 } 965 }
673 if (likely(res != -ELINKCONG)) { 966 } else if (dest->addrtype == TIPC_ADDR_ID) {
674 if (needs_conn && (res >= 0)) 967 dnode = dest->addr.id.node;
968 msg_set_type(mhdr, TIPC_DIRECT_MSG);
969 msg_set_lookup_scope(mhdr, 0);
970 msg_set_destnode(mhdr, dnode);
971 msg_set_destport(mhdr, dest->addr.id.ref);
972 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
973 }
974
975new_mtu:
976 mtu = tipc_node_get_mtu(dnode, tsk->ref);
977 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
978 if (rc < 0)
979 goto exit;
980
981 do {
982 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
983 rc = tipc_link_xmit(buf, dnode, tsk->ref);
984 if (likely(rc >= 0)) {
985 if (sock->state != SS_READY)
675 sock->state = SS_CONNECTING; 986 sock->state = SS_CONNECTING;
987 rc = dsz;
676 break; 988 break;
677 } 989 }
678 res = tipc_wait_for_sndmsg(sock, &timeo); 990 if (rc == -EMSGSIZE)
679 if (res) 991 goto new_mtu;
992 if (rc != -ELINKCONG)
680 break; 993 break;
681 } while (1); 994 tsk->link_cong = 1;
682 995 rc = tipc_wait_for_sndmsg(sock, &timeo);
996 if (rc)
997 kfree_skb_list(buf);
998 } while (!rc);
683exit: 999exit:
684 if (iocb) 1000 if (iocb)
685 release_sock(sk); 1001 release_sock(sk);
686 return res; 1002
1003 return rc;
687} 1004}
688 1005
689static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) 1006static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
690{ 1007{
691 struct sock *sk = sock->sk; 1008 struct sock *sk = sock->sk;
692 struct tipc_sock *tsk = tipc_sk(sk); 1009 struct tipc_sock *tsk = tipc_sk(sk);
693 struct tipc_port *port = &tsk->port;
694 DEFINE_WAIT(wait); 1010 DEFINE_WAIT(wait);
695 int done; 1011 int done;
696 1012
@@ -709,37 +1025,48 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
709 1025
710 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1026 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
711 done = sk_wait_event(sk, timeo_p, 1027 done = sk_wait_event(sk, timeo_p,
712 (!port->congested || !port->connected)); 1028 (!tsk->link_cong &&
1029 !tsk_conn_cong(tsk)) ||
1030 !tsk->connected);
713 finish_wait(sk_sleep(sk), &wait); 1031 finish_wait(sk_sleep(sk), &wait);
714 } while (!done); 1032 } while (!done);
715 return 0; 1033 return 0;
716} 1034}
717 1035
718/** 1036/**
719 * tipc_send_packet - send a connection-oriented message 1037 * tipc_send_stream - send stream-oriented data
720 * @iocb: if NULL, indicates that socket lock is already held 1038 * @iocb: (unused)
721 * @sock: socket structure 1039 * @sock: socket structure
722 * @m: message to send 1040 * @m: data to send
723 * @total_len: length of message 1041 * @dsz: total length of data to be transmitted
724 * 1042 *
725 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. 1043 * Used for SOCK_STREAM data.
726 * 1044 *
727 * Returns the number of bytes sent on success, or errno otherwise 1045 * Returns the number of bytes sent on success (or partial success),
1046 * or errno if no data sent
728 */ 1047 */
729static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, 1048static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
730 struct msghdr *m, size_t total_len) 1049 struct msghdr *m, size_t dsz)
731{ 1050{
732 struct sock *sk = sock->sk; 1051 struct sock *sk = sock->sk;
733 struct tipc_sock *tsk = tipc_sk(sk); 1052 struct tipc_sock *tsk = tipc_sk(sk);
1053 struct tipc_msg *mhdr = &tsk->phdr;
1054 struct sk_buff *buf;
734 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 1055 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
735 int res = -EINVAL; 1056 u32 ref = tsk->ref;
1057 int rc = -EINVAL;
736 long timeo; 1058 long timeo;
1059 u32 dnode;
1060 uint mtu, send, sent = 0;
737 1061
738 /* Handle implied connection establishment */ 1062 /* Handle implied connection establishment */
739 if (unlikely(dest)) 1063 if (unlikely(dest)) {
740 return tipc_sendmsg(iocb, sock, m, total_len); 1064 rc = tipc_sendmsg(iocb, sock, m, dsz);
741 1065 if (dsz && (dsz == rc))
742 if (total_len > TIPC_MAX_USER_MSG_SIZE) 1066 tsk->sent_unacked = 1;
1067 return rc;
1068 }
1069 if (dsz > (uint)INT_MAX)
743 return -EMSGSIZE; 1070 return -EMSGSIZE;
744 1071
745 if (iocb) 1072 if (iocb)
@@ -747,148 +1074,88 @@ static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
747 1074
748 if (unlikely(sock->state != SS_CONNECTED)) { 1075 if (unlikely(sock->state != SS_CONNECTED)) {
749 if (sock->state == SS_DISCONNECTING) 1076 if (sock->state == SS_DISCONNECTING)
750 res = -EPIPE; 1077 rc = -EPIPE;
751 else 1078 else
752 res = -ENOTCONN; 1079 rc = -ENOTCONN;
753 goto exit; 1080 goto exit;
754 } 1081 }
755 1082
756 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 1083 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1084 dnode = tsk_peer_node(tsk);
1085
1086next:
1087 mtu = tsk->max_pkt;
1088 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1089 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
1090 if (unlikely(rc < 0))
1091 goto exit;
757 do { 1092 do {
758 res = tipc_send(&tsk->port, m->msg_iov, total_len); 1093 if (likely(!tsk_conn_cong(tsk))) {
759 if (likely(res != -ELINKCONG)) 1094 rc = tipc_link_xmit(buf, dnode, ref);
760 break; 1095 if (likely(!rc)) {
761 res = tipc_wait_for_sndpkt(sock, &timeo); 1096 tsk->sent_unacked++;
762 if (res) 1097 sent += send;
763 break; 1098 if (sent == dsz)
764 } while (1); 1099 break;
1100 goto next;
1101 }
1102 if (rc == -EMSGSIZE) {
1103 tsk->max_pkt = tipc_node_get_mtu(dnode, ref);
1104 goto next;
1105 }
1106 if (rc != -ELINKCONG)
1107 break;
1108 tsk->link_cong = 1;
1109 }
1110 rc = tipc_wait_for_sndpkt(sock, &timeo);
1111 if (rc)
1112 kfree_skb_list(buf);
1113 } while (!rc);
765exit: 1114exit:
766 if (iocb) 1115 if (iocb)
767 release_sock(sk); 1116 release_sock(sk);
768 return res; 1117 return sent ? sent : rc;
769} 1118}
770 1119
771/** 1120/**
772 * tipc_send_stream - send stream-oriented data 1121 * tipc_send_packet - send a connection-oriented message
773 * @iocb: (unused) 1122 * @iocb: if NULL, indicates that socket lock is already held
774 * @sock: socket structure 1123 * @sock: socket structure
775 * @m: data to send 1124 * @m: message to send
776 * @total_len: total length of data to be sent 1125 * @dsz: length of data to be transmitted
777 * 1126 *
778 * Used for SOCK_STREAM data. 1127 * Used for SOCK_SEQPACKET messages.
779 * 1128 *
780 * Returns the number of bytes sent on success (or partial success), 1129 * Returns the number of bytes sent on success, or errno otherwise
781 * or errno if no data sent
782 */ 1130 */
783static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, 1131static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
784 struct msghdr *m, size_t total_len) 1132 struct msghdr *m, size_t dsz)
785{ 1133{
786 struct sock *sk = sock->sk; 1134 if (dsz > TIPC_MAX_USER_MSG_SIZE)
787 struct tipc_sock *tsk = tipc_sk(sk); 1135 return -EMSGSIZE;
788 struct msghdr my_msg;
789 struct iovec my_iov;
790 struct iovec *curr_iov;
791 int curr_iovlen;
792 char __user *curr_start;
793 u32 hdr_size;
794 int curr_left;
795 int bytes_to_send;
796 int bytes_sent;
797 int res;
798
799 lock_sock(sk);
800
801 /* Handle special cases where there is no connection */
802 if (unlikely(sock->state != SS_CONNECTED)) {
803 if (sock->state == SS_UNCONNECTED)
804 res = tipc_send_packet(NULL, sock, m, total_len);
805 else
806 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
807 goto exit;
808 }
809
810 if (unlikely(m->msg_name)) {
811 res = -EISCONN;
812 goto exit;
813 }
814
815 if (total_len > (unsigned int)INT_MAX) {
816 res = -EMSGSIZE;
817 goto exit;
818 }
819
820 /*
821 * Send each iovec entry using one or more messages
822 *
823 * Note: This algorithm is good for the most likely case
824 * (i.e. one large iovec entry), but could be improved to pass sets
825 * of small iovec entries into send_packet().
826 */
827 curr_iov = m->msg_iov;
828 curr_iovlen = m->msg_iovlen;
829 my_msg.msg_iov = &my_iov;
830 my_msg.msg_iovlen = 1;
831 my_msg.msg_flags = m->msg_flags;
832 my_msg.msg_name = NULL;
833 bytes_sent = 0;
834
835 hdr_size = msg_hdr_sz(&tsk->port.phdr);
836
837 while (curr_iovlen--) {
838 curr_start = curr_iov->iov_base;
839 curr_left = curr_iov->iov_len;
840
841 while (curr_left) {
842 bytes_to_send = tsk->port.max_pkt - hdr_size;
843 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
844 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
845 if (curr_left < bytes_to_send)
846 bytes_to_send = curr_left;
847 my_iov.iov_base = curr_start;
848 my_iov.iov_len = bytes_to_send;
849 res = tipc_send_packet(NULL, sock, &my_msg,
850 bytes_to_send);
851 if (res < 0) {
852 if (bytes_sent)
853 res = bytes_sent;
854 goto exit;
855 }
856 curr_left -= bytes_to_send;
857 curr_start += bytes_to_send;
858 bytes_sent += bytes_to_send;
859 }
860 1136
861 curr_iov++; 1137 return tipc_send_stream(iocb, sock, m, dsz);
862 }
863 res = bytes_sent;
864exit:
865 release_sock(sk);
866 return res;
867} 1138}
868 1139
869/** 1140/* tipc_sk_finish_conn - complete the setup of a connection
870 * auto_connect - complete connection setup to a remote port
871 * @tsk: tipc socket structure
872 * @msg: peer's response message
873 *
874 * Returns 0 on success, errno otherwise
875 */ 1141 */
876static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg) 1142static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1143 u32 peer_node)
877{ 1144{
878 struct tipc_port *port = &tsk->port; 1145 struct tipc_msg *msg = &tsk->phdr;
879 struct socket *sock = tsk->sk.sk_socket; 1146
880 struct tipc_portid peer; 1147 msg_set_destnode(msg, peer_node);
881 1148 msg_set_destport(msg, peer_port);
882 peer.ref = msg_origport(msg); 1149 msg_set_type(msg, TIPC_CONN_MSG);
883 peer.node = msg_orignode(msg); 1150 msg_set_lookup_scope(msg, 0);
884 1151 msg_set_hdr_sz(msg, SHORT_H_SIZE);
885 __tipc_port_connect(port->ref, port, &peer); 1152
886 1153 tsk->probing_interval = CONN_PROBING_INTERVAL;
887 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE) 1154 tsk->probing_state = TIPC_CONN_OK;
888 return -EINVAL; 1155 tsk->connected = 1;
889 msg_set_importance(&port->phdr, (u32)msg_importance(msg)); 1156 k_start_timer(&tsk->timer, tsk->probing_interval);
890 sock->state = SS_CONNECTED; 1157 tipc_node_add_conn(peer_node, tsk->ref, peer_port);
891 return 0; 1158 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref);
892} 1159}
893 1160
894/** 1161/**
@@ -915,17 +1182,17 @@ static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
915} 1182}
916 1183
917/** 1184/**
918 * anc_data_recv - optionally capture ancillary data for received message 1185 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
919 * @m: descriptor for message info 1186 * @m: descriptor for message info
920 * @msg: received message header 1187 * @msg: received message header
921 * @tport: TIPC port associated with message 1188 * @tsk: TIPC port associated with message
922 * 1189 *
923 * Note: Ancillary data is not captured if not requested by receiver. 1190 * Note: Ancillary data is not captured if not requested by receiver.
924 * 1191 *
925 * Returns 0 if successful, otherwise errno 1192 * Returns 0 if successful, otherwise errno
926 */ 1193 */
927static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, 1194static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
928 struct tipc_port *tport) 1195 struct tipc_sock *tsk)
929{ 1196{
930 u32 anc_data[3]; 1197 u32 anc_data[3];
931 u32 err; 1198 u32 err;
@@ -968,10 +1235,10 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
968 anc_data[2] = msg_nameupper(msg); 1235 anc_data[2] = msg_nameupper(msg);
969 break; 1236 break;
970 case TIPC_CONN_MSG: 1237 case TIPC_CONN_MSG:
971 has_name = (tport->conn_type != 0); 1238 has_name = (tsk->conn_type != 0);
972 anc_data[0] = tport->conn_type; 1239 anc_data[0] = tsk->conn_type;
973 anc_data[1] = tport->conn_instance; 1240 anc_data[1] = tsk->conn_instance;
974 anc_data[2] = tport->conn_instance; 1241 anc_data[2] = tsk->conn_instance;
975 break; 1242 break;
976 default: 1243 default:
977 has_name = 0; 1244 has_name = 0;
@@ -985,6 +1252,24 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
985 return 0; 1252 return 0;
986} 1253}
987 1254
1255static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
1256{
1257 struct sk_buff *buf = NULL;
1258 struct tipc_msg *msg;
1259 u32 peer_port = tsk_peer_port(tsk);
1260 u32 dnode = tsk_peer_node(tsk);
1261
1262 if (!tsk->connected)
1263 return;
1264 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1265 tipc_own_addr, peer_port, tsk->ref, TIPC_OK);
1266 if (!buf)
1267 return;
1268 msg = buf_msg(buf);
1269 msg_set_msgcnt(msg, ack);
1270 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1271}
1272
988static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) 1273static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
989{ 1274{
990 struct sock *sk = sock->sk; 1275 struct sock *sk = sock->sk;
@@ -1035,7 +1320,6 @@ static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1035{ 1320{
1036 struct sock *sk = sock->sk; 1321 struct sock *sk = sock->sk;
1037 struct tipc_sock *tsk = tipc_sk(sk); 1322 struct tipc_sock *tsk = tipc_sk(sk);
1038 struct tipc_port *port = &tsk->port;
1039 struct sk_buff *buf; 1323 struct sk_buff *buf;
1040 struct tipc_msg *msg; 1324 struct tipc_msg *msg;
1041 long timeo; 1325 long timeo;
@@ -1070,7 +1354,7 @@ restart:
1070 1354
1071 /* Discard an empty non-errored message & try again */ 1355 /* Discard an empty non-errored message & try again */
1072 if ((!sz) && (!err)) { 1356 if ((!sz) && (!err)) {
1073 advance_rx_queue(sk); 1357 tsk_advance_rx_queue(sk);
1074 goto restart; 1358 goto restart;
1075 } 1359 }
1076 1360
@@ -1078,7 +1362,7 @@ restart:
1078 set_orig_addr(m, msg); 1362 set_orig_addr(m, msg);
1079 1363
1080 /* Capture ancillary data (optional) */ 1364 /* Capture ancillary data (optional) */
1081 res = anc_data_recv(m, msg, port); 1365 res = tipc_sk_anc_data_recv(m, msg, tsk);
1082 if (res) 1366 if (res)
1083 goto exit; 1367 goto exit;
1084 1368
@@ -1104,9 +1388,11 @@ restart:
1104 /* Consume received message (optional) */ 1388 /* Consume received message (optional) */
1105 if (likely(!(flags & MSG_PEEK))) { 1389 if (likely(!(flags & MSG_PEEK))) {
1106 if ((sock->state != SS_READY) && 1390 if ((sock->state != SS_READY) &&
1107 (++port->conn_unacked >= TIPC_CONNACK_INTV)) 1391 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1108 tipc_acknowledge(port->ref, port->conn_unacked); 1392 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1109 advance_rx_queue(sk); 1393 tsk->rcv_unacked = 0;
1394 }
1395 tsk_advance_rx_queue(sk);
1110 } 1396 }
1111exit: 1397exit:
1112 release_sock(sk); 1398 release_sock(sk);
@@ -1130,7 +1416,6 @@ static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1130{ 1416{
1131 struct sock *sk = sock->sk; 1417 struct sock *sk = sock->sk;
1132 struct tipc_sock *tsk = tipc_sk(sk); 1418 struct tipc_sock *tsk = tipc_sk(sk);
1133 struct tipc_port *port = &tsk->port;
1134 struct sk_buff *buf; 1419 struct sk_buff *buf;
1135 struct tipc_msg *msg; 1420 struct tipc_msg *msg;
1136 long timeo; 1421 long timeo;
@@ -1168,14 +1453,14 @@ restart:
1168 1453
1169 /* Discard an empty non-errored message & try again */ 1454 /* Discard an empty non-errored message & try again */
1170 if ((!sz) && (!err)) { 1455 if ((!sz) && (!err)) {
1171 advance_rx_queue(sk); 1456 tsk_advance_rx_queue(sk);
1172 goto restart; 1457 goto restart;
1173 } 1458 }
1174 1459
1175 /* Optionally capture sender's address & ancillary data of first msg */ 1460 /* Optionally capture sender's address & ancillary data of first msg */
1176 if (sz_copied == 0) { 1461 if (sz_copied == 0) {
1177 set_orig_addr(m, msg); 1462 set_orig_addr(m, msg);
1178 res = anc_data_recv(m, msg, port); 1463 res = tipc_sk_anc_data_recv(m, msg, tsk);
1179 if (res) 1464 if (res)
1180 goto exit; 1465 goto exit;
1181 } 1466 }
@@ -1213,9 +1498,11 @@ restart:
1213 1498
1214 /* Consume received message (optional) */ 1499 /* Consume received message (optional) */
1215 if (likely(!(flags & MSG_PEEK))) { 1500 if (likely(!(flags & MSG_PEEK))) {
1216 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV)) 1501 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1217 tipc_acknowledge(port->ref, port->conn_unacked); 1502 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1218 advance_rx_queue(sk); 1503 tsk->rcv_unacked = 0;
1504 }
1505 tsk_advance_rx_queue(sk);
1219 } 1506 }
1220 1507
1221 /* Loop around if more data is required */ 1508 /* Loop around if more data is required */
@@ -1269,18 +1556,14 @@ static void tipc_data_ready(struct sock *sk)
1269 * @tsk: TIPC socket 1556 * @tsk: TIPC socket
1270 * @msg: message 1557 * @msg: message
1271 * 1558 *
1272 * Returns TIPC error status code and socket error status code 1559 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
1273 * once it encounters some errors
1274 */ 1560 */
1275static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) 1561static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1276{ 1562{
1277 struct sock *sk = &tsk->sk; 1563 struct sock *sk = &tsk->sk;
1278 struct tipc_port *port = &tsk->port;
1279 struct socket *sock = sk->sk_socket; 1564 struct socket *sock = sk->sk_socket;
1280 struct tipc_msg *msg = buf_msg(*buf); 1565 struct tipc_msg *msg = buf_msg(*buf);
1281 1566 int retval = -TIPC_ERR_NO_PORT;
1282 u32 retval = TIPC_ERR_NO_PORT;
1283 int res;
1284 1567
1285 if (msg_mcast(msg)) 1568 if (msg_mcast(msg))
1286 return retval; 1569 return retval;
@@ -1288,16 +1571,23 @@ static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1288 switch ((int)sock->state) { 1571 switch ((int)sock->state) {
1289 case SS_CONNECTED: 1572 case SS_CONNECTED:
1290 /* Accept only connection-based messages sent by peer */ 1573 /* Accept only connection-based messages sent by peer */
1291 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) { 1574 if (tsk_peer_msg(tsk, msg)) {
1292 if (unlikely(msg_errcode(msg))) { 1575 if (unlikely(msg_errcode(msg))) {
1293 sock->state = SS_DISCONNECTING; 1576 sock->state = SS_DISCONNECTING;
1294 __tipc_port_disconnect(port); 1577 tsk->connected = 0;
1578 /* let timer expire on it's own */
1579 tipc_node_remove_conn(tsk_peer_node(tsk),
1580 tsk->ref);
1295 } 1581 }
1296 retval = TIPC_OK; 1582 retval = TIPC_OK;
1297 } 1583 }
1298 break; 1584 break;
1299 case SS_CONNECTING: 1585 case SS_CONNECTING:
1300 /* Accept only ACK or NACK message */ 1586 /* Accept only ACK or NACK message */
1587
1588 if (unlikely(!msg_connected(msg)))
1589 break;
1590
1301 if (unlikely(msg_errcode(msg))) { 1591 if (unlikely(msg_errcode(msg))) {
1302 sock->state = SS_DISCONNECTING; 1592 sock->state = SS_DISCONNECTING;
1303 sk->sk_err = ECONNREFUSED; 1593 sk->sk_err = ECONNREFUSED;
@@ -1305,17 +1595,17 @@ static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1305 break; 1595 break;
1306 } 1596 }
1307 1597
1308 if (unlikely(!msg_connected(msg))) 1598 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
1309 break;
1310
1311 res = auto_connect(tsk, msg);
1312 if (res) {
1313 sock->state = SS_DISCONNECTING; 1599 sock->state = SS_DISCONNECTING;
1314 sk->sk_err = -res; 1600 sk->sk_err = EINVAL;
1315 retval = TIPC_OK; 1601 retval = TIPC_OK;
1316 break; 1602 break;
1317 } 1603 }
1318 1604
1605 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1606 msg_set_importance(&tsk->phdr, msg_importance(msg));
1607 sock->state = SS_CONNECTED;
1608
1319 /* If an incoming message is an 'ACK-', it should be 1609 /* If an incoming message is an 'ACK-', it should be
1320 * discarded here because it doesn't contain useful 1610 * discarded here because it doesn't contain useful
1321 * data. In addition, we should try to wake up 1611 * data. In addition, we should try to wake up
@@ -1382,32 +1672,44 @@ static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1382 * 1672 *
1383 * Called with socket lock already taken; port lock may also be taken. 1673 * Called with socket lock already taken; port lock may also be taken.
1384 * 1674 *
1385 * Returns TIPC error status code (TIPC_OK if message is not to be rejected) 1675 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1676 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
1386 */ 1677 */
1387static u32 filter_rcv(struct sock *sk, struct sk_buff *buf) 1678static int filter_rcv(struct sock *sk, struct sk_buff *buf)
1388{ 1679{
1389 struct socket *sock = sk->sk_socket; 1680 struct socket *sock = sk->sk_socket;
1390 struct tipc_sock *tsk = tipc_sk(sk); 1681 struct tipc_sock *tsk = tipc_sk(sk);
1391 struct tipc_msg *msg = buf_msg(buf); 1682 struct tipc_msg *msg = buf_msg(buf);
1392 unsigned int limit = rcvbuf_limit(sk, buf); 1683 unsigned int limit = rcvbuf_limit(sk, buf);
1393 u32 res = TIPC_OK; 1684 u32 onode;
1685 int rc = TIPC_OK;
1686
1687 if (unlikely(msg_user(msg) == CONN_MANAGER))
1688 return tipc_sk_proto_rcv(tsk, &onode, buf);
1689
1690 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1691 kfree_skb(buf);
1692 tsk->link_cong = 0;
1693 sk->sk_write_space(sk);
1694 return TIPC_OK;
1695 }
1394 1696
1395 /* Reject message if it is wrong sort of message for socket */ 1697 /* Reject message if it is wrong sort of message for socket */
1396 if (msg_type(msg) > TIPC_DIRECT_MSG) 1698 if (msg_type(msg) > TIPC_DIRECT_MSG)
1397 return TIPC_ERR_NO_PORT; 1699 return -TIPC_ERR_NO_PORT;
1398 1700
1399 if (sock->state == SS_READY) { 1701 if (sock->state == SS_READY) {
1400 if (msg_connected(msg)) 1702 if (msg_connected(msg))
1401 return TIPC_ERR_NO_PORT; 1703 return -TIPC_ERR_NO_PORT;
1402 } else { 1704 } else {
1403 res = filter_connect(tsk, &buf); 1705 rc = filter_connect(tsk, &buf);
1404 if (res != TIPC_OK || buf == NULL) 1706 if (rc != TIPC_OK || buf == NULL)
1405 return res; 1707 return rc;
1406 } 1708 }
1407 1709
1408 /* Reject message if there isn't room to queue it */ 1710 /* Reject message if there isn't room to queue it */
1409 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) 1711 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1410 return TIPC_ERR_OVERLOAD; 1712 return -TIPC_ERR_OVERLOAD;
1411 1713
1412 /* Enqueue message */ 1714 /* Enqueue message */
1413 TIPC_SKB_CB(buf)->handle = NULL; 1715 TIPC_SKB_CB(buf)->handle = NULL;
@@ -1429,16 +1731,23 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1429 */ 1731 */
1430static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) 1732static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
1431{ 1733{
1432 u32 res; 1734 int rc;
1735 u32 onode;
1433 struct tipc_sock *tsk = tipc_sk(sk); 1736 struct tipc_sock *tsk = tipc_sk(sk);
1434 uint truesize = buf->truesize; 1737 uint truesize = buf->truesize;
1435 1738
1436 res = filter_rcv(sk, buf); 1739 rc = filter_rcv(sk, buf);
1437 if (unlikely(res)) 1740
1438 tipc_reject_msg(buf, res); 1741 if (likely(!rc)) {
1742 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1743 atomic_add(truesize, &tsk->dupl_rcvcnt);
1744 return 0;
1745 }
1746
1747 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1748 return 0;
1439 1749
1440 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) 1750 tipc_link_xmit(buf, onode, 0);
1441 atomic_add(truesize, &tsk->dupl_rcvcnt);
1442 1751
1443 return 0; 1752 return 0;
1444} 1753}
@@ -1452,49 +1761,42 @@ static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
1452int tipc_sk_rcv(struct sk_buff *buf) 1761int tipc_sk_rcv(struct sk_buff *buf)
1453{ 1762{
1454 struct tipc_sock *tsk; 1763 struct tipc_sock *tsk;
1455 struct tipc_port *port;
1456 struct sock *sk; 1764 struct sock *sk;
1457 u32 dport = msg_destport(buf_msg(buf)); 1765 u32 dport = msg_destport(buf_msg(buf));
1458 int err = TIPC_OK; 1766 int rc = TIPC_OK;
1459 uint limit; 1767 uint limit;
1768 u32 dnode;
1460 1769
1461 /* Forward unresolved named message */ 1770 /* Validate destination and message */
1462 if (unlikely(!dport)) { 1771 tsk = tipc_sk_get(dport);
1463 tipc_net_route_msg(buf); 1772 if (unlikely(!tsk)) {
1464 return 0; 1773 rc = tipc_msg_eval(buf, &dnode);
1465 }
1466
1467 /* Validate destination */
1468 port = tipc_port_lock(dport);
1469 if (unlikely(!port)) {
1470 err = TIPC_ERR_NO_PORT;
1471 goto exit; 1774 goto exit;
1472 } 1775 }
1473
1474 tsk = tipc_port_to_sock(port);
1475 sk = &tsk->sk; 1776 sk = &tsk->sk;
1476 1777
1477 /* Queue message */ 1778 /* Queue message */
1478 bh_lock_sock(sk); 1779 bh_lock_sock(sk);
1479 1780
1480 if (!sock_owned_by_user(sk)) { 1781 if (!sock_owned_by_user(sk)) {
1481 err = filter_rcv(sk, buf); 1782 rc = filter_rcv(sk, buf);
1482 } else { 1783 } else {
1483 if (sk->sk_backlog.len == 0) 1784 if (sk->sk_backlog.len == 0)
1484 atomic_set(&tsk->dupl_rcvcnt, 0); 1785 atomic_set(&tsk->dupl_rcvcnt, 0);
1485 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt); 1786 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1486 if (sk_add_backlog(sk, buf, limit)) 1787 if (sk_add_backlog(sk, buf, limit))
1487 err = TIPC_ERR_OVERLOAD; 1788 rc = -TIPC_ERR_OVERLOAD;
1488 } 1789 }
1489
1490 bh_unlock_sock(sk); 1790 bh_unlock_sock(sk);
1491 tipc_port_unlock(port); 1791 tipc_sk_put(tsk);
1492 1792 if (likely(!rc))
1493 if (likely(!err))
1494 return 0; 1793 return 0;
1495exit: 1794exit:
1496 tipc_reject_msg(buf, err); 1795 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
1497 return -EHOSTUNREACH; 1796 return -EHOSTUNREACH;
1797
1798 tipc_link_xmit(buf, dnode, 0);
1799 return (rc < 0) ? -EHOSTUNREACH : 0;
1498} 1800}
1499 1801
1500static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) 1802static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
@@ -1673,10 +1975,8 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1673{ 1975{
1674 struct sock *new_sk, *sk = sock->sk; 1976 struct sock *new_sk, *sk = sock->sk;
1675 struct sk_buff *buf; 1977 struct sk_buff *buf;
1676 struct tipc_port *new_port; 1978 struct tipc_sock *new_tsock;
1677 struct tipc_msg *msg; 1979 struct tipc_msg *msg;
1678 struct tipc_portid peer;
1679 u32 new_ref;
1680 long timeo; 1980 long timeo;
1681 int res; 1981 int res;
1682 1982
@@ -1698,8 +1998,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1698 goto exit; 1998 goto exit;
1699 1999
1700 new_sk = new_sock->sk; 2000 new_sk = new_sock->sk;
1701 new_port = &tipc_sk(new_sk)->port; 2001 new_tsock = tipc_sk(new_sk);
1702 new_ref = new_port->ref;
1703 msg = buf_msg(buf); 2002 msg = buf_msg(buf);
1704 2003
1705 /* we lock on new_sk; but lockdep sees the lock on sk */ 2004 /* we lock on new_sk; but lockdep sees the lock on sk */
@@ -1709,18 +2008,16 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1709 * Reject any stray messages received by new socket 2008 * Reject any stray messages received by new socket
1710 * before the socket lock was taken (very, very unlikely) 2009 * before the socket lock was taken (very, very unlikely)
1711 */ 2010 */
1712 reject_rx_queue(new_sk); 2011 tsk_rej_rx_queue(new_sk);
1713 2012
1714 /* Connect new socket to it's peer */ 2013 /* Connect new socket to it's peer */
1715 peer.ref = msg_origport(msg); 2014 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
1716 peer.node = msg_orignode(msg);
1717 tipc_port_connect(new_ref, &peer);
1718 new_sock->state = SS_CONNECTED; 2015 new_sock->state = SS_CONNECTED;
1719 2016
1720 tipc_port_set_importance(new_port, msg_importance(msg)); 2017 tsk_set_importance(new_tsock, msg_importance(msg));
1721 if (msg_named(msg)) { 2018 if (msg_named(msg)) {
1722 new_port->conn_type = msg_nametype(msg); 2019 new_tsock->conn_type = msg_nametype(msg);
1723 new_port->conn_instance = msg_nameinst(msg); 2020 new_tsock->conn_instance = msg_nameinst(msg);
1724 } 2021 }
1725 2022
1726 /* 2023 /*
@@ -1730,7 +2027,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1730 if (!msg_data_sz(msg)) { 2027 if (!msg_data_sz(msg)) {
1731 struct msghdr m = {NULL,}; 2028 struct msghdr m = {NULL,};
1732 2029
1733 advance_rx_queue(sk); 2030 tsk_advance_rx_queue(sk);
1734 tipc_send_packet(NULL, new_sock, &m, 0); 2031 tipc_send_packet(NULL, new_sock, &m, 0);
1735 } else { 2032 } else {
1736 __skb_dequeue(&sk->sk_receive_queue); 2033 __skb_dequeue(&sk->sk_receive_queue);
@@ -1756,8 +2053,8 @@ static int tipc_shutdown(struct socket *sock, int how)
1756{ 2053{
1757 struct sock *sk = sock->sk; 2054 struct sock *sk = sock->sk;
1758 struct tipc_sock *tsk = tipc_sk(sk); 2055 struct tipc_sock *tsk = tipc_sk(sk);
1759 struct tipc_port *port = &tsk->port;
1760 struct sk_buff *buf; 2056 struct sk_buff *buf;
2057 u32 dnode;
1761 int res; 2058 int res;
1762 2059
1763 if (how != SHUT_RDWR) 2060 if (how != SHUT_RDWR)
@@ -1777,14 +2074,21 @@ restart:
1777 kfree_skb(buf); 2074 kfree_skb(buf);
1778 goto restart; 2075 goto restart;
1779 } 2076 }
1780 tipc_port_disconnect(port->ref); 2077 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1781 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); 2078 tipc_link_xmit(buf, dnode, tsk->ref);
2079 tipc_node_remove_conn(dnode, tsk->ref);
1782 } else { 2080 } else {
1783 tipc_port_shutdown(port->ref); 2081 dnode = tsk_peer_node(tsk);
2082 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2083 TIPC_CONN_MSG, SHORT_H_SIZE,
2084 0, dnode, tipc_own_addr,
2085 tsk_peer_port(tsk),
2086 tsk->ref, TIPC_CONN_SHUTDOWN);
2087 tipc_link_xmit(buf, dnode, tsk->ref);
1784 } 2088 }
1785 2089 tsk->connected = 0;
1786 sock->state = SS_DISCONNECTING; 2090 sock->state = SS_DISCONNECTING;
1787 2091 tipc_node_remove_conn(dnode, tsk->ref);
1788 /* fall through */ 2092 /* fall through */
1789 2093
1790 case SS_DISCONNECTING: 2094 case SS_DISCONNECTING:
@@ -1805,6 +2109,432 @@ restart:
1805 return res; 2109 return res;
1806} 2110}
1807 2111
2112static void tipc_sk_timeout(unsigned long ref)
2113{
2114 struct tipc_sock *tsk;
2115 struct sock *sk;
2116 struct sk_buff *buf = NULL;
2117 u32 peer_port, peer_node;
2118
2119 tsk = tipc_sk_get(ref);
2120 if (!tsk)
2121 return;
2122
2123 sk = &tsk->sk;
2124 bh_lock_sock(sk);
2125 if (!tsk->connected) {
2126 bh_unlock_sock(sk);
2127 goto exit;
2128 }
2129 peer_port = tsk_peer_port(tsk);
2130 peer_node = tsk_peer_node(tsk);
2131
2132 if (tsk->probing_state == TIPC_CONN_PROBING) {
2133 /* Previous probe not answered -> self abort */
2134 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2135 SHORT_H_SIZE, 0, tipc_own_addr,
2136 peer_node, ref, peer_port,
2137 TIPC_ERR_NO_PORT);
2138 } else {
2139 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2140 0, peer_node, tipc_own_addr,
2141 peer_port, ref, TIPC_OK);
2142 tsk->probing_state = TIPC_CONN_PROBING;
2143 k_start_timer(&tsk->timer, tsk->probing_interval);
2144 }
2145 bh_unlock_sock(sk);
2146 if (buf)
2147 tipc_link_xmit(buf, peer_node, ref);
2148exit:
2149 tipc_sk_put(tsk);
2150}
2151
2152static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2153 struct tipc_name_seq const *seq)
2154{
2155 struct publication *publ;
2156 u32 key;
2157
2158 if (tsk->connected)
2159 return -EINVAL;
2160 key = tsk->ref + tsk->pub_count + 1;
2161 if (key == tsk->ref)
2162 return -EADDRINUSE;
2163
2164 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
2165 scope, tsk->ref, key);
2166 if (unlikely(!publ))
2167 return -EINVAL;
2168
2169 list_add(&publ->pport_list, &tsk->publications);
2170 tsk->pub_count++;
2171 tsk->published = 1;
2172 return 0;
2173}
2174
2175static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2176 struct tipc_name_seq const *seq)
2177{
2178 struct publication *publ;
2179 struct publication *safe;
2180 int rc = -EINVAL;
2181
2182 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2183 if (seq) {
2184 if (publ->scope != scope)
2185 continue;
2186 if (publ->type != seq->type)
2187 continue;
2188 if (publ->lower != seq->lower)
2189 continue;
2190 if (publ->upper != seq->upper)
2191 break;
2192 tipc_nametbl_withdraw(publ->type, publ->lower,
2193 publ->ref, publ->key);
2194 rc = 0;
2195 break;
2196 }
2197 tipc_nametbl_withdraw(publ->type, publ->lower,
2198 publ->ref, publ->key);
2199 rc = 0;
2200 }
2201 if (list_empty(&tsk->publications))
2202 tsk->published = 0;
2203 return rc;
2204}
2205
2206static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
2207 int len, int full_id)
2208{
2209 struct publication *publ;
2210 int ret;
2211
2212 if (full_id)
2213 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2214 tipc_zone(tipc_own_addr),
2215 tipc_cluster(tipc_own_addr),
2216 tipc_node(tipc_own_addr), tsk->ref);
2217 else
2218 ret = tipc_snprintf(buf, len, "%-10u:", tsk->ref);
2219
2220 if (tsk->connected) {
2221 u32 dport = tsk_peer_port(tsk);
2222 u32 destnode = tsk_peer_node(tsk);
2223
2224 ret += tipc_snprintf(buf + ret, len - ret,
2225 " connected to <%u.%u.%u:%u>",
2226 tipc_zone(destnode),
2227 tipc_cluster(destnode),
2228 tipc_node(destnode), dport);
2229 if (tsk->conn_type != 0)
2230 ret += tipc_snprintf(buf + ret, len - ret,
2231 " via {%u,%u}", tsk->conn_type,
2232 tsk->conn_instance);
2233 } else if (tsk->published) {
2234 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2235 list_for_each_entry(publ, &tsk->publications, pport_list) {
2236 if (publ->lower == publ->upper)
2237 ret += tipc_snprintf(buf + ret, len - ret,
2238 " {%u,%u}", publ->type,
2239 publ->lower);
2240 else
2241 ret += tipc_snprintf(buf + ret, len - ret,
2242 " {%u,%u,%u}", publ->type,
2243 publ->lower, publ->upper);
2244 }
2245 }
2246 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2247 return ret;
2248}
2249
2250struct sk_buff *tipc_sk_socks_show(void)
2251{
2252 struct sk_buff *buf;
2253 struct tlv_desc *rep_tlv;
2254 char *pb;
2255 int pb_len;
2256 struct tipc_sock *tsk;
2257 int str_len = 0;
2258 u32 ref = 0;
2259
2260 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2261 if (!buf)
2262 return NULL;
2263 rep_tlv = (struct tlv_desc *)buf->data;
2264 pb = TLV_DATA(rep_tlv);
2265 pb_len = ULTRA_STRING_MAX_LEN;
2266
2267 tsk = tipc_sk_get_next(&ref);
2268 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2269 lock_sock(&tsk->sk);
2270 str_len += tipc_sk_show(tsk, pb + str_len,
2271 pb_len - str_len, 0);
2272 release_sock(&tsk->sk);
2273 tipc_sk_put(tsk);
2274 }
2275 str_len += 1; /* for "\0" */
2276 skb_put(buf, TLV_SPACE(str_len));
2277 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2278
2279 return buf;
2280}
2281
2282/* tipc_sk_reinit: set non-zero address in all existing sockets
2283 * when we go from standalone to network mode.
2284 */
2285void tipc_sk_reinit(void)
2286{
2287 struct tipc_msg *msg;
2288 u32 ref = 0;
2289 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
2290
2291 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2292 lock_sock(&tsk->sk);
2293 msg = &tsk->phdr;
2294 msg_set_prevnode(msg, tipc_own_addr);
2295 msg_set_orignode(msg, tipc_own_addr);
2296 release_sock(&tsk->sk);
2297 tipc_sk_put(tsk);
2298 }
2299}
2300
2301/**
2302 * struct reference - TIPC socket reference entry
2303 * @tsk: pointer to socket associated with reference entry
2304 * @ref: reference value for socket (combines instance & array index info)
2305 */
2306struct reference {
2307 struct tipc_sock *tsk;
2308 u32 ref;
2309};
2310
2311/**
2312 * struct tipc_ref_table - table of TIPC socket reference entries
2313 * @entries: pointer to array of reference entries
2314 * @capacity: array index of first unusable entry
2315 * @init_point: array index of first uninitialized entry
2316 * @first_free: array index of first unused socket reference entry
2317 * @last_free: array index of last unused socket reference entry
2318 * @index_mask: bitmask for array index portion of reference values
2319 * @start_mask: initial value for instance value portion of reference values
2320 */
2321struct ref_table {
2322 struct reference *entries;
2323 u32 capacity;
2324 u32 init_point;
2325 u32 first_free;
2326 u32 last_free;
2327 u32 index_mask;
2328 u32 start_mask;
2329};
2330
2331/* Socket reference table consists of 2**N entries.
2332 *
2333 * State Socket ptr Reference
2334 * ----- ---------- ---------
2335 * In use non-NULL XXXX|own index
2336 * (XXXX changes each time entry is acquired)
2337 * Free NULL YYYY|next free index
2338 * (YYYY is one more than last used XXXX)
2339 * Uninitialized NULL 0
2340 *
2341 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
2342 *
2343 * Note that a reference value of 0 does not necessarily indicate that an
2344 * entry is uninitialized, since the last entry in the free list could also
2345 * have a reference value of 0 (although this is unlikely).
2346 */
2347
2348static struct ref_table tipc_ref_table;
2349
2350static DEFINE_RWLOCK(ref_table_lock);
2351
2352/**
2353 * tipc_ref_table_init - create reference table for sockets
2354 */
2355int tipc_sk_ref_table_init(u32 req_sz, u32 start)
2356{
2357 struct reference *table;
2358 u32 actual_sz;
2359
2360 /* account for unused entry, then round up size to a power of 2 */
2361
2362 req_sz++;
2363 for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) {
2364 /* do nothing */
2365 };
2366
2367 /* allocate table & mark all entries as uninitialized */
2368 table = vzalloc(actual_sz * sizeof(struct reference));
2369 if (table == NULL)
2370 return -ENOMEM;
2371
2372 tipc_ref_table.entries = table;
2373 tipc_ref_table.capacity = req_sz;
2374 tipc_ref_table.init_point = 1;
2375 tipc_ref_table.first_free = 0;
2376 tipc_ref_table.last_free = 0;
2377 tipc_ref_table.index_mask = actual_sz - 1;
2378 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
2379
2380 return 0;
2381}
2382
2383/**
2384 * tipc_ref_table_stop - destroy reference table for sockets
2385 */
2386void tipc_sk_ref_table_stop(void)
2387{
2388 if (!tipc_ref_table.entries)
2389 return;
2390 vfree(tipc_ref_table.entries);
2391 tipc_ref_table.entries = NULL;
2392}
2393
2394/* tipc_ref_acquire - create reference to a socket
2395 *
2396 * Register an socket pointer in the reference table.
2397 * Returns a unique reference value that is used from then on to retrieve the
2398 * socket pointer, or to determine if the socket has been deregistered.
2399 */
2400u32 tipc_sk_ref_acquire(struct tipc_sock *tsk)
2401{
2402 u32 index;
2403 u32 index_mask;
2404 u32 next_plus_upper;
2405 u32 ref = 0;
2406 struct reference *entry;
2407
2408 if (unlikely(!tsk)) {
2409 pr_err("Attempt to acquire ref. to non-existent obj\n");
2410 return 0;
2411 }
2412 if (unlikely(!tipc_ref_table.entries)) {
2413 pr_err("Ref. table not found in acquisition attempt\n");
2414 return 0;
2415 }
2416
2417 /* Take a free entry, if available; otherwise initialize a new one */
2418 write_lock_bh(&ref_table_lock);
2419 index = tipc_ref_table.first_free;
2420 entry = &tipc_ref_table.entries[index];
2421
2422 if (likely(index)) {
2423 index = tipc_ref_table.first_free;
2424 entry = &tipc_ref_table.entries[index];
2425 index_mask = tipc_ref_table.index_mask;
2426 next_plus_upper = entry->ref;
2427 tipc_ref_table.first_free = next_plus_upper & index_mask;
2428 ref = (next_plus_upper & ~index_mask) + index;
2429 entry->tsk = tsk;
2430 } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
2431 index = tipc_ref_table.init_point++;
2432 entry = &tipc_ref_table.entries[index];
2433 ref = tipc_ref_table.start_mask + index;
2434 }
2435
2436 if (ref) {
2437 entry->ref = ref;
2438 entry->tsk = tsk;
2439 }
2440 write_unlock_bh(&ref_table_lock);
2441 return ref;
2442}
2443
2444/* tipc_sk_ref_discard - invalidate reference to an socket
2445 *
2446 * Disallow future references to an socket and free up the entry for re-use.
2447 */
2448void tipc_sk_ref_discard(u32 ref)
2449{
2450 struct reference *entry;
2451 u32 index;
2452 u32 index_mask;
2453
2454 if (unlikely(!tipc_ref_table.entries)) {
2455 pr_err("Ref. table not found during discard attempt\n");
2456 return;
2457 }
2458
2459 index_mask = tipc_ref_table.index_mask;
2460 index = ref & index_mask;
2461 entry = &tipc_ref_table.entries[index];
2462
2463 write_lock_bh(&ref_table_lock);
2464
2465 if (unlikely(!entry->tsk)) {
2466 pr_err("Attempt to discard ref. to non-existent socket\n");
2467 goto exit;
2468 }
2469 if (unlikely(entry->ref != ref)) {
2470 pr_err("Attempt to discard non-existent reference\n");
2471 goto exit;
2472 }
2473
2474 /* Mark entry as unused; increment instance part of entry's
2475 * reference to invalidate any subsequent references
2476 */
2477
2478 entry->tsk = NULL;
2479 entry->ref = (ref & ~index_mask) + (index_mask + 1);
2480
2481 /* Append entry to free entry list */
2482 if (unlikely(tipc_ref_table.first_free == 0))
2483 tipc_ref_table.first_free = index;
2484 else
2485 tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
2486 tipc_ref_table.last_free = index;
2487exit:
2488 write_unlock_bh(&ref_table_lock);
2489}
2490
2491/* tipc_sk_get - find referenced socket and return pointer to it
2492 */
2493struct tipc_sock *tipc_sk_get(u32 ref)
2494{
2495 struct reference *entry;
2496 struct tipc_sock *tsk;
2497
2498 if (unlikely(!tipc_ref_table.entries))
2499 return NULL;
2500 read_lock_bh(&ref_table_lock);
2501 entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
2502 tsk = entry->tsk;
2503 if (likely(tsk && (entry->ref == ref)))
2504 sock_hold(&tsk->sk);
2505 else
2506 tsk = NULL;
2507 read_unlock_bh(&ref_table_lock);
2508 return tsk;
2509}
2510
2511/* tipc_sk_get_next - lock & return next socket after referenced one
2512*/
2513struct tipc_sock *tipc_sk_get_next(u32 *ref)
2514{
2515 struct reference *entry;
2516 struct tipc_sock *tsk = NULL;
2517 uint index = *ref & tipc_ref_table.index_mask;
2518
2519 read_lock_bh(&ref_table_lock);
2520 while (++index < tipc_ref_table.capacity) {
2521 entry = &tipc_ref_table.entries[index];
2522 if (!entry->tsk)
2523 continue;
2524 tsk = entry->tsk;
2525 sock_hold(&tsk->sk);
2526 *ref = entry->ref;
2527 break;
2528 }
2529 read_unlock_bh(&ref_table_lock);
2530 return tsk;
2531}
2532
2533static void tipc_sk_put(struct tipc_sock *tsk)
2534{
2535 sock_put(&tsk->sk);
2536}
2537
1808/** 2538/**
1809 * tipc_setsockopt - set socket option 2539 * tipc_setsockopt - set socket option
1810 * @sock: socket structure 2540 * @sock: socket structure
@@ -1823,7 +2553,6 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1823{ 2553{
1824 struct sock *sk = sock->sk; 2554 struct sock *sk = sock->sk;
1825 struct tipc_sock *tsk = tipc_sk(sk); 2555 struct tipc_sock *tsk = tipc_sk(sk);
1826 struct tipc_port *port = &tsk->port;
1827 u32 value; 2556 u32 value;
1828 int res; 2557 int res;
1829 2558
@@ -1841,16 +2570,16 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1841 2570
1842 switch (opt) { 2571 switch (opt) {
1843 case TIPC_IMPORTANCE: 2572 case TIPC_IMPORTANCE:
1844 tipc_port_set_importance(port, value); 2573 res = tsk_set_importance(tsk, value);
1845 break; 2574 break;
1846 case TIPC_SRC_DROPPABLE: 2575 case TIPC_SRC_DROPPABLE:
1847 if (sock->type != SOCK_STREAM) 2576 if (sock->type != SOCK_STREAM)
1848 tipc_port_set_unreliable(port, value); 2577 tsk_set_unreliable(tsk, value);
1849 else 2578 else
1850 res = -ENOPROTOOPT; 2579 res = -ENOPROTOOPT;
1851 break; 2580 break;
1852 case TIPC_DEST_DROPPABLE: 2581 case TIPC_DEST_DROPPABLE:
1853 tipc_port_set_unreturnable(port, value); 2582 tsk_set_unreturnable(tsk, value);
1854 break; 2583 break;
1855 case TIPC_CONN_TIMEOUT: 2584 case TIPC_CONN_TIMEOUT:
1856 tipc_sk(sk)->conn_timeout = value; 2585 tipc_sk(sk)->conn_timeout = value;
@@ -1883,7 +2612,6 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1883{ 2612{
1884 struct sock *sk = sock->sk; 2613 struct sock *sk = sock->sk;
1885 struct tipc_sock *tsk = tipc_sk(sk); 2614 struct tipc_sock *tsk = tipc_sk(sk);
1886 struct tipc_port *port = &tsk->port;
1887 int len; 2615 int len;
1888 u32 value; 2616 u32 value;
1889 int res; 2617 int res;
@@ -1900,16 +2628,16 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1900 2628
1901 switch (opt) { 2629 switch (opt) {
1902 case TIPC_IMPORTANCE: 2630 case TIPC_IMPORTANCE:
1903 value = tipc_port_importance(port); 2631 value = tsk_importance(tsk);
1904 break; 2632 break;
1905 case TIPC_SRC_DROPPABLE: 2633 case TIPC_SRC_DROPPABLE:
1906 value = tipc_port_unreliable(port); 2634 value = tsk_unreliable(tsk);
1907 break; 2635 break;
1908 case TIPC_DEST_DROPPABLE: 2636 case TIPC_DEST_DROPPABLE:
1909 value = tipc_port_unreturnable(port); 2637 value = tsk_unreturnable(tsk);
1910 break; 2638 break;
1911 case TIPC_CONN_TIMEOUT: 2639 case TIPC_CONN_TIMEOUT:
1912 value = tipc_sk(sk)->conn_timeout; 2640 value = tsk->conn_timeout;
1913 /* no need to set "res", since already 0 at this point */ 2641 /* no need to set "res", since already 0 at this point */
1914 break; 2642 break;
1915 case TIPC_NODE_RECVQ_DEPTH: 2643 case TIPC_NODE_RECVQ_DEPTH:
@@ -1936,7 +2664,7 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1936 return put_user(sizeof(value), ol); 2664 return put_user(sizeof(value), ol);
1937} 2665}
1938 2666
1939int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg) 2667static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1940{ 2668{
1941 struct tipc_sioc_ln_req lnr; 2669 struct tipc_sioc_ln_req lnr;
1942 void __user *argp = (void __user *)arg; 2670 void __user *argp = (void __user *)arg;
@@ -1952,7 +2680,6 @@ int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1952 return 0; 2680 return 0;
1953 } 2681 }
1954 return -EADDRNOTAVAIL; 2682 return -EADDRNOTAVAIL;
1955 break;
1956 default: 2683 default:
1957 return -ENOIOCTLCMD; 2684 return -ENOIOCTLCMD;
1958 } 2685 }