diff options
Diffstat (limited to 'net/tipc/socket.c')
-rw-r--r-- | net/tipc/socket.c | 884 |
1 files changed, 740 insertions, 144 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c index 7d423ee10897..75275c5cf929 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 | */ | ||
78 | struct 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 | ||
50 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); | 100 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
51 | static void tipc_data_ready(struct sock *sk); | 101 | static void tipc_data_ready(struct sock *sk); |
@@ -53,6 +103,16 @@ static void tipc_write_space(struct sock *sk); | |||
53 | static int tipc_release(struct socket *sock); | 103 | static int tipc_release(struct socket *sock); |
54 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); | 104 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); |
55 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); | 105 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); |
106 | static void tipc_sk_timeout(unsigned long ref); | ||
107 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, | ||
108 | struct tipc_name_seq const *seq); | ||
109 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, | ||
110 | struct tipc_name_seq const *seq); | ||
111 | static u32 tipc_sk_ref_acquire(struct tipc_sock *tsk); | ||
112 | static void tipc_sk_ref_discard(u32 ref); | ||
113 | static struct tipc_sock *tipc_sk_get(u32 ref); | ||
114 | static struct tipc_sock *tipc_sk_get_next(u32 *ref); | ||
115 | static void tipc_sk_put(struct tipc_sock *tsk); | ||
56 | 116 | ||
57 | static const struct proto_ops packet_ops; | 117 | static const struct proto_ops packet_ops; |
58 | static const struct proto_ops stream_ops; | 118 | static const struct proto_ops stream_ops; |
@@ -106,24 +166,75 @@ static struct proto tipc_proto_kern; | |||
106 | * - port reference | 166 | * - port reference |
107 | */ | 167 | */ |
108 | 168 | ||
109 | #include "socket.h" | 169 | static u32 tsk_peer_node(struct tipc_sock *tsk) |
170 | { | ||
171 | return msg_destnode(&tsk->phdr); | ||
172 | } | ||
173 | |||
174 | static u32 tsk_peer_port(struct tipc_sock *tsk) | ||
175 | { | ||
176 | return msg_destport(&tsk->phdr); | ||
177 | } | ||
178 | |||
179 | static bool tsk_unreliable(struct tipc_sock *tsk) | ||
180 | { | ||
181 | return msg_src_droppable(&tsk->phdr) != 0; | ||
182 | } | ||
183 | |||
184 | static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable) | ||
185 | { | ||
186 | msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0); | ||
187 | } | ||
188 | |||
189 | static bool tsk_unreturnable(struct tipc_sock *tsk) | ||
190 | { | ||
191 | return msg_dest_droppable(&tsk->phdr) != 0; | ||
192 | } | ||
193 | |||
194 | static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable) | ||
195 | { | ||
196 | msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0); | ||
197 | } | ||
198 | |||
199 | static int tsk_importance(struct tipc_sock *tsk) | ||
200 | { | ||
201 | return msg_importance(&tsk->phdr); | ||
202 | } | ||
203 | |||
204 | static 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 | |||
212 | static struct tipc_sock *tipc_sk(const struct sock *sk) | ||
213 | { | ||
214 | return container_of(sk, struct tipc_sock, sk); | ||
215 | } | ||
216 | |||
217 | static int tsk_conn_cong(struct tipc_sock *tsk) | ||
218 | { | ||
219 | return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN; | ||
220 | } | ||
110 | 221 | ||
111 | /** | 222 | /** |
112 | * advance_rx_queue - discard first buffer in socket receive queue | 223 | * tsk_advance_rx_queue - discard first buffer in socket receive queue |
113 | * | 224 | * |
114 | * Caller must hold socket lock | 225 | * Caller must hold socket lock |
115 | */ | 226 | */ |
116 | static void advance_rx_queue(struct sock *sk) | 227 | static void tsk_advance_rx_queue(struct sock *sk) |
117 | { | 228 | { |
118 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); | 229 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
119 | } | 230 | } |
120 | 231 | ||
121 | /** | 232 | /** |
122 | * reject_rx_queue - reject all buffers in socket receive queue | 233 | * tsk_rej_rx_queue - reject all buffers in socket receive queue |
123 | * | 234 | * |
124 | * Caller must hold socket lock | 235 | * Caller must hold socket lock |
125 | */ | 236 | */ |
126 | static void reject_rx_queue(struct sock *sk) | 237 | static void tsk_rej_rx_queue(struct sock *sk) |
127 | { | 238 | { |
128 | struct sk_buff *buf; | 239 | struct sk_buff *buf; |
129 | u32 dnode; | 240 | u32 dnode; |
@@ -134,6 +245,38 @@ static void reject_rx_queue(struct sock *sk) | |||
134 | } | 245 | } |
135 | } | 246 | } |
136 | 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 | */ | ||
253 | static 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; | ||
261 | |||
262 | if (unlikely(msg_origport(msg) != peer_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; | ||
278 | } | ||
279 | |||
137 | /** | 280 | /** |
138 | * tipc_sk_create - create a TIPC socket | 281 | * tipc_sk_create - create a TIPC socket |
139 | * @net: network namespace (must be default network) | 282 | * @net: network namespace (must be default network) |
@@ -153,7 +296,7 @@ static int tipc_sk_create(struct net *net, struct socket *sock, | |||
153 | socket_state state; | 296 | socket_state state; |
154 | struct sock *sk; | 297 | struct sock *sk; |
155 | struct tipc_sock *tsk; | 298 | struct tipc_sock *tsk; |
156 | struct tipc_port *port; | 299 | struct tipc_msg *msg; |
157 | u32 ref; | 300 | u32 ref; |
158 | 301 | ||
159 | /* Validate arguments */ | 302 | /* Validate arguments */ |
@@ -188,20 +331,24 @@ static int tipc_sk_create(struct net *net, struct socket *sock, | |||
188 | return -ENOMEM; | 331 | return -ENOMEM; |
189 | 332 | ||
190 | tsk = tipc_sk(sk); | 333 | tsk = tipc_sk(sk); |
191 | port = &tsk->port; | 334 | ref = tipc_sk_ref_acquire(tsk); |
192 | |||
193 | ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE); | ||
194 | if (!ref) { | 335 | if (!ref) { |
195 | pr_warn("Socket registration failed, ref. table exhausted\n"); | 336 | pr_warn("Socket create failed; reference table exhausted\n"); |
196 | sk_free(sk); | ||
197 | return -ENOMEM; | 337 | return -ENOMEM; |
198 | } | 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); | ||
199 | 346 | ||
200 | /* Finish initializing socket data structures */ | 347 | /* Finish initializing socket data structures */ |
201 | sock->ops = ops; | 348 | sock->ops = ops; |
202 | sock->state = state; | 349 | sock->state = state; |
203 | |||
204 | sock_init_data(sock, sk); | 350 | sock_init_data(sock, sk); |
351 | k_init_timer(&tsk->timer, (Handler)tipc_sk_timeout, ref); | ||
205 | sk->sk_backlog_rcv = tipc_backlog_rcv; | 352 | sk->sk_backlog_rcv = tipc_backlog_rcv; |
206 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; | 353 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
207 | sk->sk_data_ready = tipc_data_ready; | 354 | sk->sk_data_ready = tipc_data_ready; |
@@ -209,12 +356,11 @@ static int tipc_sk_create(struct net *net, struct socket *sock, | |||
209 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; | 356 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
210 | tsk->sent_unacked = 0; | 357 | tsk->sent_unacked = 0; |
211 | atomic_set(&tsk->dupl_rcvcnt, 0); | 358 | atomic_set(&tsk->dupl_rcvcnt, 0); |
212 | tipc_port_unlock(port); | ||
213 | 359 | ||
214 | if (sock->state == SS_READY) { | 360 | if (sock->state == SS_READY) { |
215 | tipc_port_set_unreturnable(port, true); | 361 | tsk_set_unreturnable(tsk, true); |
216 | if (sock->type == SOCK_DGRAM) | 362 | if (sock->type == SOCK_DGRAM) |
217 | tipc_port_set_unreliable(port, true); | 363 | tsk_set_unreliable(tsk, true); |
218 | } | 364 | } |
219 | return 0; | 365 | return 0; |
220 | } | 366 | } |
@@ -308,7 +454,6 @@ static int tipc_release(struct socket *sock) | |||
308 | { | 454 | { |
309 | struct sock *sk = sock->sk; | 455 | struct sock *sk = sock->sk; |
310 | struct tipc_sock *tsk; | 456 | struct tipc_sock *tsk; |
311 | struct tipc_port *port; | ||
312 | struct sk_buff *buf; | 457 | struct sk_buff *buf; |
313 | u32 dnode; | 458 | u32 dnode; |
314 | 459 | ||
@@ -320,13 +465,13 @@ static int tipc_release(struct socket *sock) | |||
320 | return 0; | 465 | return 0; |
321 | 466 | ||
322 | tsk = tipc_sk(sk); | 467 | tsk = tipc_sk(sk); |
323 | port = &tsk->port; | ||
324 | lock_sock(sk); | 468 | lock_sock(sk); |
325 | 469 | ||
326 | /* | 470 | /* |
327 | * Reject all unreceived messages, except on an active connection | 471 | * Reject all unreceived messages, except on an active connection |
328 | * (which disconnects locally & sends a 'FIN+' to peer) | 472 | * (which disconnects locally & sends a 'FIN+' to peer) |
329 | */ | 473 | */ |
474 | dnode = tsk_peer_node(tsk); | ||
330 | while (sock->state != SS_DISCONNECTING) { | 475 | while (sock->state != SS_DISCONNECTING) { |
331 | buf = __skb_dequeue(&sk->sk_receive_queue); | 476 | buf = __skb_dequeue(&sk->sk_receive_queue); |
332 | if (buf == NULL) | 477 | if (buf == NULL) |
@@ -337,17 +482,27 @@ static int tipc_release(struct socket *sock) | |||
337 | if ((sock->state == SS_CONNECTING) || | 482 | if ((sock->state == SS_CONNECTING) || |
338 | (sock->state == SS_CONNECTED)) { | 483 | (sock->state == SS_CONNECTED)) { |
339 | sock->state = SS_DISCONNECTING; | 484 | sock->state = SS_DISCONNECTING; |
340 | tipc_port_disconnect(port->ref); | 485 | tsk->connected = 0; |
486 | tipc_node_remove_conn(dnode, tsk->ref); | ||
341 | } | 487 | } |
342 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) | 488 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) |
343 | tipc_link_xmit(buf, dnode, 0); | 489 | tipc_link_xmit(buf, dnode, 0); |
344 | } | 490 | } |
345 | } | 491 | } |
346 | 492 | ||
347 | /* Destroy TIPC port; also disconnects an active connection and | 493 | tipc_sk_withdraw(tsk, 0, NULL); |
348 | * sends a 'FIN-' to peer. | 494 | tipc_sk_ref_discard(tsk->ref); |
349 | */ | 495 | k_cancel_timer(&tsk->timer); |
350 | 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); | ||
351 | 506 | ||
352 | /* Discard any remaining (connection-based) messages in receive queue */ | 507 | /* Discard any remaining (connection-based) messages in receive queue */ |
353 | __skb_queue_purge(&sk->sk_receive_queue); | 508 | __skb_queue_purge(&sk->sk_receive_queue); |
@@ -355,7 +510,6 @@ static int tipc_release(struct socket *sock) | |||
355 | /* Reject any messages that accumulated in backlog queue */ | 510 | /* Reject any messages that accumulated in backlog queue */ |
356 | sock->state = SS_DISCONNECTING; | 511 | sock->state = SS_DISCONNECTING; |
357 | release_sock(sk); | 512 | release_sock(sk); |
358 | |||
359 | sock_put(sk); | 513 | sock_put(sk); |
360 | sock->sk = NULL; | 514 | sock->sk = NULL; |
361 | 515 | ||
@@ -387,7 +541,7 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, | |||
387 | 541 | ||
388 | lock_sock(sk); | 542 | lock_sock(sk); |
389 | if (unlikely(!uaddr_len)) { | 543 | if (unlikely(!uaddr_len)) { |
390 | res = tipc_withdraw(&tsk->port, 0, NULL); | 544 | res = tipc_sk_withdraw(tsk, 0, NULL); |
391 | goto exit; | 545 | goto exit; |
392 | } | 546 | } |
393 | 547 | ||
@@ -415,8 +569,8 @@ static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, | |||
415 | } | 569 | } |
416 | 570 | ||
417 | res = (addr->scope > 0) ? | 571 | res = (addr->scope > 0) ? |
418 | tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) : | 572 | tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) : |
419 | tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq); | 573 | tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq); |
420 | exit: | 574 | exit: |
421 | release_sock(sk); | 575 | release_sock(sk); |
422 | return res; | 576 | return res; |
@@ -446,10 +600,10 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, | |||
446 | if ((sock->state != SS_CONNECTED) && | 600 | if ((sock->state != SS_CONNECTED) && |
447 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) | 601 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) |
448 | return -ENOTCONN; | 602 | return -ENOTCONN; |
449 | addr->addr.id.ref = tipc_port_peerport(&tsk->port); | 603 | addr->addr.id.ref = tsk_peer_port(tsk); |
450 | addr->addr.id.node = tipc_port_peernode(&tsk->port); | 604 | addr->addr.id.node = tsk_peer_node(tsk); |
451 | } else { | 605 | } else { |
452 | addr->addr.id.ref = tsk->port.ref; | 606 | addr->addr.id.ref = tsk->ref; |
453 | addr->addr.id.node = tipc_own_addr; | 607 | addr->addr.id.node = tipc_own_addr; |
454 | } | 608 | } |
455 | 609 | ||
@@ -518,7 +672,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock, | |||
518 | break; | 672 | break; |
519 | case SS_READY: | 673 | case SS_READY: |
520 | case SS_CONNECTED: | 674 | case SS_CONNECTED: |
521 | if (!tsk->link_cong && !tipc_sk_conn_cong(tsk)) | 675 | if (!tsk->link_cong && !tsk_conn_cong(tsk)) |
522 | mask |= POLLOUT; | 676 | mask |= POLLOUT; |
523 | /* fall thru' */ | 677 | /* fall thru' */ |
524 | case SS_CONNECTING: | 678 | case SS_CONNECTING: |
@@ -549,7 +703,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, | |||
549 | struct iovec *iov, size_t dsz, long timeo) | 703 | struct iovec *iov, size_t dsz, long timeo) |
550 | { | 704 | { |
551 | struct sock *sk = sock->sk; | 705 | struct sock *sk = sock->sk; |
552 | struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr; | 706 | struct tipc_msg *mhdr = &tipc_sk(sk)->phdr; |
553 | struct sk_buff *buf; | 707 | struct sk_buff *buf; |
554 | uint mtu; | 708 | uint mtu; |
555 | int rc; | 709 | int rc; |
@@ -579,6 +733,7 @@ new_mtu: | |||
579 | goto new_mtu; | 733 | goto new_mtu; |
580 | if (rc != -ELINKCONG) | 734 | if (rc != -ELINKCONG) |
581 | break; | 735 | break; |
736 | tipc_sk(sk)->link_cong = 1; | ||
582 | rc = tipc_wait_for_sndmsg(sock, &timeo); | 737 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
583 | if (rc) | 738 | if (rc) |
584 | kfree_skb_list(buf); | 739 | kfree_skb_list(buf); |
@@ -638,20 +793,19 @@ static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, | |||
638 | struct sk_buff *buf) | 793 | struct sk_buff *buf) |
639 | { | 794 | { |
640 | struct tipc_msg *msg = buf_msg(buf); | 795 | struct tipc_msg *msg = buf_msg(buf); |
641 | struct tipc_port *port = &tsk->port; | ||
642 | int conn_cong; | 796 | int conn_cong; |
643 | 797 | ||
644 | /* Ignore if connection cannot be validated: */ | 798 | /* Ignore if connection cannot be validated: */ |
645 | if (!port->connected || !tipc_port_peer_msg(port, msg)) | 799 | if (!tsk_peer_msg(tsk, msg)) |
646 | goto exit; | 800 | goto exit; |
647 | 801 | ||
648 | port->probing_state = TIPC_CONN_OK; | 802 | tsk->probing_state = TIPC_CONN_OK; |
649 | 803 | ||
650 | if (msg_type(msg) == CONN_ACK) { | 804 | if (msg_type(msg) == CONN_ACK) { |
651 | conn_cong = tipc_sk_conn_cong(tsk); | 805 | conn_cong = tsk_conn_cong(tsk); |
652 | tsk->sent_unacked -= msg_msgcnt(msg); | 806 | tsk->sent_unacked -= msg_msgcnt(msg); |
653 | if (conn_cong) | 807 | if (conn_cong) |
654 | tipc_sock_wakeup(tsk); | 808 | tsk->sk.sk_write_space(&tsk->sk); |
655 | } else if (msg_type(msg) == CONN_PROBE) { | 809 | } else if (msg_type(msg) == CONN_PROBE) { |
656 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) | 810 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) |
657 | return TIPC_OK; | 811 | return TIPC_OK; |
@@ -742,8 +896,7 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, | |||
742 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); | 896 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
743 | struct sock *sk = sock->sk; | 897 | struct sock *sk = sock->sk; |
744 | struct tipc_sock *tsk = tipc_sk(sk); | 898 | struct tipc_sock *tsk = tipc_sk(sk); |
745 | struct tipc_port *port = &tsk->port; | 899 | struct tipc_msg *mhdr = &tsk->phdr; |
746 | struct tipc_msg *mhdr = &port->phdr; | ||
747 | struct iovec *iov = m->msg_iov; | 900 | struct iovec *iov = m->msg_iov; |
748 | u32 dnode, dport; | 901 | u32 dnode, dport; |
749 | struct sk_buff *buf; | 902 | struct sk_buff *buf; |
@@ -774,13 +927,13 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, | |||
774 | rc = -EISCONN; | 927 | rc = -EISCONN; |
775 | goto exit; | 928 | goto exit; |
776 | } | 929 | } |
777 | if (tsk->port.published) { | 930 | if (tsk->published) { |
778 | rc = -EOPNOTSUPP; | 931 | rc = -EOPNOTSUPP; |
779 | goto exit; | 932 | goto exit; |
780 | } | 933 | } |
781 | if (dest->addrtype == TIPC_ADDR_NAME) { | 934 | if (dest->addrtype == TIPC_ADDR_NAME) { |
782 | tsk->port.conn_type = dest->addr.name.name.type; | 935 | tsk->conn_type = dest->addr.name.name.type; |
783 | tsk->port.conn_instance = dest->addr.name.name.instance; | 936 | tsk->conn_instance = dest->addr.name.name.instance; |
784 | } | 937 | } |
785 | } | 938 | } |
786 | rc = dest_name_check(dest, m); | 939 | rc = dest_name_check(dest, m); |
@@ -820,13 +973,14 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, | |||
820 | } | 973 | } |
821 | 974 | ||
822 | new_mtu: | 975 | new_mtu: |
823 | mtu = tipc_node_get_mtu(dnode, tsk->port.ref); | 976 | mtu = tipc_node_get_mtu(dnode, tsk->ref); |
824 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); | 977 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); |
825 | if (rc < 0) | 978 | if (rc < 0) |
826 | goto exit; | 979 | goto exit; |
827 | 980 | ||
828 | do { | 981 | do { |
829 | rc = tipc_link_xmit(buf, dnode, tsk->port.ref); | 982 | TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong; |
983 | rc = tipc_link_xmit(buf, dnode, tsk->ref); | ||
830 | if (likely(rc >= 0)) { | 984 | if (likely(rc >= 0)) { |
831 | if (sock->state != SS_READY) | 985 | if (sock->state != SS_READY) |
832 | sock->state = SS_CONNECTING; | 986 | sock->state = SS_CONNECTING; |
@@ -835,10 +989,9 @@ new_mtu: | |||
835 | } | 989 | } |
836 | if (rc == -EMSGSIZE) | 990 | if (rc == -EMSGSIZE) |
837 | goto new_mtu; | 991 | goto new_mtu; |
838 | |||
839 | if (rc != -ELINKCONG) | 992 | if (rc != -ELINKCONG) |
840 | break; | 993 | break; |
841 | 994 | tsk->link_cong = 1; | |
842 | rc = tipc_wait_for_sndmsg(sock, &timeo); | 995 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
843 | if (rc) | 996 | if (rc) |
844 | kfree_skb_list(buf); | 997 | kfree_skb_list(buf); |
@@ -873,8 +1026,8 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) | |||
873 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 1026 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
874 | done = sk_wait_event(sk, timeo_p, | 1027 | done = sk_wait_event(sk, timeo_p, |
875 | (!tsk->link_cong && | 1028 | (!tsk->link_cong && |
876 | !tipc_sk_conn_cong(tsk)) || | 1029 | !tsk_conn_cong(tsk)) || |
877 | !tsk->port.connected); | 1030 | !tsk->connected); |
878 | finish_wait(sk_sleep(sk), &wait); | 1031 | finish_wait(sk_sleep(sk), &wait); |
879 | } while (!done); | 1032 | } while (!done); |
880 | return 0; | 1033 | return 0; |
@@ -897,11 +1050,10 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, | |||
897 | { | 1050 | { |
898 | struct sock *sk = sock->sk; | 1051 | struct sock *sk = sock->sk; |
899 | struct tipc_sock *tsk = tipc_sk(sk); | 1052 | struct tipc_sock *tsk = tipc_sk(sk); |
900 | struct tipc_port *port = &tsk->port; | 1053 | struct tipc_msg *mhdr = &tsk->phdr; |
901 | struct tipc_msg *mhdr = &port->phdr; | ||
902 | struct sk_buff *buf; | 1054 | struct sk_buff *buf; |
903 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); | 1055 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
904 | u32 ref = port->ref; | 1056 | u32 ref = tsk->ref; |
905 | int rc = -EINVAL; | 1057 | int rc = -EINVAL; |
906 | long timeo; | 1058 | long timeo; |
907 | u32 dnode; | 1059 | u32 dnode; |
@@ -929,16 +1081,16 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, | |||
929 | } | 1081 | } |
930 | 1082 | ||
931 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); | 1083 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
932 | dnode = tipc_port_peernode(port); | 1084 | dnode = tsk_peer_node(tsk); |
933 | 1085 | ||
934 | next: | 1086 | next: |
935 | mtu = port->max_pkt; | 1087 | mtu = tsk->max_pkt; |
936 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); | 1088 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); |
937 | rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf); | 1089 | rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf); |
938 | if (unlikely(rc < 0)) | 1090 | if (unlikely(rc < 0)) |
939 | goto exit; | 1091 | goto exit; |
940 | do { | 1092 | do { |
941 | if (likely(!tipc_sk_conn_cong(tsk))) { | 1093 | if (likely(!tsk_conn_cong(tsk))) { |
942 | rc = tipc_link_xmit(buf, dnode, ref); | 1094 | rc = tipc_link_xmit(buf, dnode, ref); |
943 | if (likely(!rc)) { | 1095 | if (likely(!rc)) { |
944 | tsk->sent_unacked++; | 1096 | tsk->sent_unacked++; |
@@ -948,11 +1100,12 @@ next: | |||
948 | goto next; | 1100 | goto next; |
949 | } | 1101 | } |
950 | if (rc == -EMSGSIZE) { | 1102 | if (rc == -EMSGSIZE) { |
951 | port->max_pkt = tipc_node_get_mtu(dnode, ref); | 1103 | tsk->max_pkt = tipc_node_get_mtu(dnode, ref); |
952 | goto next; | 1104 | goto next; |
953 | } | 1105 | } |
954 | if (rc != -ELINKCONG) | 1106 | if (rc != -ELINKCONG) |
955 | break; | 1107 | break; |
1108 | tsk->link_cong = 1; | ||
956 | } | 1109 | } |
957 | rc = tipc_wait_for_sndpkt(sock, &timeo); | 1110 | rc = tipc_wait_for_sndpkt(sock, &timeo); |
958 | if (rc) | 1111 | if (rc) |
@@ -984,29 +1137,25 @@ static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, | |||
984 | return tipc_send_stream(iocb, sock, m, dsz); | 1137 | return tipc_send_stream(iocb, sock, m, dsz); |
985 | } | 1138 | } |
986 | 1139 | ||
987 | /** | 1140 | /* 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 | */ | 1141 | */ |
994 | static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg) | 1142 | static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, |
1143 | u32 peer_node) | ||
995 | { | 1144 | { |
996 | struct tipc_port *port = &tsk->port; | 1145 | struct tipc_msg *msg = &tsk->phdr; |
997 | struct socket *sock = tsk->sk.sk_socket; | 1146 | |
998 | struct tipc_portid peer; | 1147 | msg_set_destnode(msg, peer_node); |
999 | 1148 | msg_set_destport(msg, peer_port); | |
1000 | peer.ref = msg_origport(msg); | 1149 | msg_set_type(msg, TIPC_CONN_MSG); |
1001 | peer.node = msg_orignode(msg); | 1150 | msg_set_lookup_scope(msg, 0); |
1002 | 1151 | msg_set_hdr_sz(msg, SHORT_H_SIZE); | |
1003 | __tipc_port_connect(port->ref, port, &peer); | 1152 | |
1004 | 1153 | tsk->probing_interval = CONN_PROBING_INTERVAL; | |
1005 | if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE) | 1154 | tsk->probing_state = TIPC_CONN_OK; |
1006 | return -EINVAL; | 1155 | tsk->connected = 1; |
1007 | msg_set_importance(&port->phdr, (u32)msg_importance(msg)); | 1156 | k_start_timer(&tsk->timer, tsk->probing_interval); |
1008 | sock->state = SS_CONNECTED; | 1157 | tipc_node_add_conn(peer_node, tsk->ref, peer_port); |
1009 | return 0; | 1158 | tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref); |
1010 | } | 1159 | } |
1011 | 1160 | ||
1012 | /** | 1161 | /** |
@@ -1033,17 +1182,17 @@ static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) | |||
1033 | } | 1182 | } |
1034 | 1183 | ||
1035 | /** | 1184 | /** |
1036 | * anc_data_recv - optionally capture ancillary data for received message | 1185 | * tipc_sk_anc_data_recv - optionally capture ancillary data for received message |
1037 | * @m: descriptor for message info | 1186 | * @m: descriptor for message info |
1038 | * @msg: received message header | 1187 | * @msg: received message header |
1039 | * @tport: TIPC port associated with message | 1188 | * @tsk: TIPC port associated with message |
1040 | * | 1189 | * |
1041 | * Note: Ancillary data is not captured if not requested by receiver. | 1190 | * Note: Ancillary data is not captured if not requested by receiver. |
1042 | * | 1191 | * |
1043 | * Returns 0 if successful, otherwise errno | 1192 | * Returns 0 if successful, otherwise errno |
1044 | */ | 1193 | */ |
1045 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, | 1194 | static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
1046 | struct tipc_port *tport) | 1195 | struct tipc_sock *tsk) |
1047 | { | 1196 | { |
1048 | u32 anc_data[3]; | 1197 | u32 anc_data[3]; |
1049 | u32 err; | 1198 | u32 err; |
@@ -1086,10 +1235,10 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, | |||
1086 | anc_data[2] = msg_nameupper(msg); | 1235 | anc_data[2] = msg_nameupper(msg); |
1087 | break; | 1236 | break; |
1088 | case TIPC_CONN_MSG: | 1237 | case TIPC_CONN_MSG: |
1089 | has_name = (tport->conn_type != 0); | 1238 | has_name = (tsk->conn_type != 0); |
1090 | anc_data[0] = tport->conn_type; | 1239 | anc_data[0] = tsk->conn_type; |
1091 | anc_data[1] = tport->conn_instance; | 1240 | anc_data[1] = tsk->conn_instance; |
1092 | anc_data[2] = tport->conn_instance; | 1241 | anc_data[2] = tsk->conn_instance; |
1093 | break; | 1242 | break; |
1094 | default: | 1243 | default: |
1095 | has_name = 0; | 1244 | has_name = 0; |
@@ -1103,6 +1252,24 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, | |||
1103 | return 0; | 1252 | return 0; |
1104 | } | 1253 | } |
1105 | 1254 | ||
1255 | static 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 | |||
1106 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) | 1273 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
1107 | { | 1274 | { |
1108 | struct sock *sk = sock->sk; | 1275 | struct sock *sk = sock->sk; |
@@ -1153,7 +1320,6 @@ static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, | |||
1153 | { | 1320 | { |
1154 | struct sock *sk = sock->sk; | 1321 | struct sock *sk = sock->sk; |
1155 | struct tipc_sock *tsk = tipc_sk(sk); | 1322 | struct tipc_sock *tsk = tipc_sk(sk); |
1156 | struct tipc_port *port = &tsk->port; | ||
1157 | struct sk_buff *buf; | 1323 | struct sk_buff *buf; |
1158 | struct tipc_msg *msg; | 1324 | struct tipc_msg *msg; |
1159 | long timeo; | 1325 | long timeo; |
@@ -1188,7 +1354,7 @@ restart: | |||
1188 | 1354 | ||
1189 | /* Discard an empty non-errored message & try again */ | 1355 | /* Discard an empty non-errored message & try again */ |
1190 | if ((!sz) && (!err)) { | 1356 | if ((!sz) && (!err)) { |
1191 | advance_rx_queue(sk); | 1357 | tsk_advance_rx_queue(sk); |
1192 | goto restart; | 1358 | goto restart; |
1193 | } | 1359 | } |
1194 | 1360 | ||
@@ -1196,7 +1362,7 @@ restart: | |||
1196 | set_orig_addr(m, msg); | 1362 | set_orig_addr(m, msg); |
1197 | 1363 | ||
1198 | /* Capture ancillary data (optional) */ | 1364 | /* Capture ancillary data (optional) */ |
1199 | res = anc_data_recv(m, msg, port); | 1365 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
1200 | if (res) | 1366 | if (res) |
1201 | goto exit; | 1367 | goto exit; |
1202 | 1368 | ||
@@ -1223,10 +1389,10 @@ restart: | |||
1223 | if (likely(!(flags & MSG_PEEK))) { | 1389 | if (likely(!(flags & MSG_PEEK))) { |
1224 | if ((sock->state != SS_READY) && | 1390 | if ((sock->state != SS_READY) && |
1225 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { | 1391 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
1226 | tipc_acknowledge(port->ref, tsk->rcv_unacked); | 1392 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
1227 | tsk->rcv_unacked = 0; | 1393 | tsk->rcv_unacked = 0; |
1228 | } | 1394 | } |
1229 | advance_rx_queue(sk); | 1395 | tsk_advance_rx_queue(sk); |
1230 | } | 1396 | } |
1231 | exit: | 1397 | exit: |
1232 | release_sock(sk); | 1398 | release_sock(sk); |
@@ -1250,7 +1416,6 @@ static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, | |||
1250 | { | 1416 | { |
1251 | struct sock *sk = sock->sk; | 1417 | struct sock *sk = sock->sk; |
1252 | struct tipc_sock *tsk = tipc_sk(sk); | 1418 | struct tipc_sock *tsk = tipc_sk(sk); |
1253 | struct tipc_port *port = &tsk->port; | ||
1254 | struct sk_buff *buf; | 1419 | struct sk_buff *buf; |
1255 | struct tipc_msg *msg; | 1420 | struct tipc_msg *msg; |
1256 | long timeo; | 1421 | long timeo; |
@@ -1288,14 +1453,14 @@ restart: | |||
1288 | 1453 | ||
1289 | /* Discard an empty non-errored message & try again */ | 1454 | /* Discard an empty non-errored message & try again */ |
1290 | if ((!sz) && (!err)) { | 1455 | if ((!sz) && (!err)) { |
1291 | advance_rx_queue(sk); | 1456 | tsk_advance_rx_queue(sk); |
1292 | goto restart; | 1457 | goto restart; |
1293 | } | 1458 | } |
1294 | 1459 | ||
1295 | /* Optionally capture sender's address & ancillary data of first msg */ | 1460 | /* Optionally capture sender's address & ancillary data of first msg */ |
1296 | if (sz_copied == 0) { | 1461 | if (sz_copied == 0) { |
1297 | set_orig_addr(m, msg); | 1462 | set_orig_addr(m, msg); |
1298 | res = anc_data_recv(m, msg, port); | 1463 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
1299 | if (res) | 1464 | if (res) |
1300 | goto exit; | 1465 | goto exit; |
1301 | } | 1466 | } |
@@ -1334,10 +1499,10 @@ restart: | |||
1334 | /* Consume received message (optional) */ | 1499 | /* Consume received message (optional) */ |
1335 | if (likely(!(flags & MSG_PEEK))) { | 1500 | if (likely(!(flags & MSG_PEEK))) { |
1336 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { | 1501 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
1337 | tipc_acknowledge(port->ref, tsk->rcv_unacked); | 1502 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
1338 | tsk->rcv_unacked = 0; | 1503 | tsk->rcv_unacked = 0; |
1339 | } | 1504 | } |
1340 | advance_rx_queue(sk); | 1505 | tsk_advance_rx_queue(sk); |
1341 | } | 1506 | } |
1342 | 1507 | ||
1343 | /* Loop around if more data is required */ | 1508 | /* Loop around if more data is required */ |
@@ -1396,12 +1561,9 @@ static void tipc_data_ready(struct sock *sk) | |||
1396 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) | 1561 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
1397 | { | 1562 | { |
1398 | struct sock *sk = &tsk->sk; | 1563 | struct sock *sk = &tsk->sk; |
1399 | struct tipc_port *port = &tsk->port; | ||
1400 | struct socket *sock = sk->sk_socket; | 1564 | struct socket *sock = sk->sk_socket; |
1401 | struct tipc_msg *msg = buf_msg(*buf); | 1565 | struct tipc_msg *msg = buf_msg(*buf); |
1402 | |||
1403 | int retval = -TIPC_ERR_NO_PORT; | 1566 | int retval = -TIPC_ERR_NO_PORT; |
1404 | int res; | ||
1405 | 1567 | ||
1406 | if (msg_mcast(msg)) | 1568 | if (msg_mcast(msg)) |
1407 | return retval; | 1569 | return retval; |
@@ -1409,16 +1571,23 @@ static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) | |||
1409 | switch ((int)sock->state) { | 1571 | switch ((int)sock->state) { |
1410 | case SS_CONNECTED: | 1572 | case SS_CONNECTED: |
1411 | /* Accept only connection-based messages sent by peer */ | 1573 | /* Accept only connection-based messages sent by peer */ |
1412 | if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) { | 1574 | if (tsk_peer_msg(tsk, msg)) { |
1413 | if (unlikely(msg_errcode(msg))) { | 1575 | if (unlikely(msg_errcode(msg))) { |
1414 | sock->state = SS_DISCONNECTING; | 1576 | sock->state = SS_DISCONNECTING; |
1415 | __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); | ||
1416 | } | 1581 | } |
1417 | retval = TIPC_OK; | 1582 | retval = TIPC_OK; |
1418 | } | 1583 | } |
1419 | break; | 1584 | break; |
1420 | case SS_CONNECTING: | 1585 | case SS_CONNECTING: |
1421 | /* Accept only ACK or NACK message */ | 1586 | /* Accept only ACK or NACK message */ |
1587 | |||
1588 | if (unlikely(!msg_connected(msg))) | ||
1589 | break; | ||
1590 | |||
1422 | if (unlikely(msg_errcode(msg))) { | 1591 | if (unlikely(msg_errcode(msg))) { |
1423 | sock->state = SS_DISCONNECTING; | 1592 | sock->state = SS_DISCONNECTING; |
1424 | sk->sk_err = ECONNREFUSED; | 1593 | sk->sk_err = ECONNREFUSED; |
@@ -1426,17 +1595,17 @@ static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) | |||
1426 | break; | 1595 | break; |
1427 | } | 1596 | } |
1428 | 1597 | ||
1429 | if (unlikely(!msg_connected(msg))) | 1598 | 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; | 1599 | sock->state = SS_DISCONNECTING; |
1435 | sk->sk_err = -res; | 1600 | sk->sk_err = EINVAL; |
1436 | retval = TIPC_OK; | 1601 | retval = TIPC_OK; |
1437 | break; | 1602 | break; |
1438 | } | 1603 | } |
1439 | 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 | |||
1440 | /* If an incoming message is an 'ACK-', it should be | 1609 | /* If an incoming message is an 'ACK-', it should be |
1441 | * discarded here because it doesn't contain useful | 1610 | * discarded here because it doesn't contain useful |
1442 | * data. In addition, we should try to wake up | 1611 | * data. In addition, we should try to wake up |
@@ -1518,6 +1687,13 @@ static int filter_rcv(struct sock *sk, struct sk_buff *buf) | |||
1518 | if (unlikely(msg_user(msg) == CONN_MANAGER)) | 1687 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
1519 | return tipc_sk_proto_rcv(tsk, &onode, buf); | 1688 | return tipc_sk_proto_rcv(tsk, &onode, buf); |
1520 | 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 | } | ||
1696 | |||
1521 | /* Reject message if it is wrong sort of message for socket */ | 1697 | /* Reject message if it is wrong sort of message for socket */ |
1522 | if (msg_type(msg) > TIPC_DIRECT_MSG) | 1698 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
1523 | return -TIPC_ERR_NO_PORT; | 1699 | return -TIPC_ERR_NO_PORT; |
@@ -1585,7 +1761,6 @@ static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) | |||
1585 | int tipc_sk_rcv(struct sk_buff *buf) | 1761 | int tipc_sk_rcv(struct sk_buff *buf) |
1586 | { | 1762 | { |
1587 | struct tipc_sock *tsk; | 1763 | struct tipc_sock *tsk; |
1588 | struct tipc_port *port; | ||
1589 | struct sock *sk; | 1764 | struct sock *sk; |
1590 | u32 dport = msg_destport(buf_msg(buf)); | 1765 | u32 dport = msg_destport(buf_msg(buf)); |
1591 | int rc = TIPC_OK; | 1766 | int rc = TIPC_OK; |
@@ -1593,13 +1768,11 @@ int tipc_sk_rcv(struct sk_buff *buf) | |||
1593 | u32 dnode; | 1768 | u32 dnode; |
1594 | 1769 | ||
1595 | /* Validate destination and message */ | 1770 | /* Validate destination and message */ |
1596 | port = tipc_port_lock(dport); | 1771 | tsk = tipc_sk_get(dport); |
1597 | if (unlikely(!port)) { | 1772 | if (unlikely(!tsk)) { |
1598 | rc = tipc_msg_eval(buf, &dnode); | 1773 | rc = tipc_msg_eval(buf, &dnode); |
1599 | goto exit; | 1774 | goto exit; |
1600 | } | 1775 | } |
1601 | |||
1602 | tsk = tipc_port_to_sock(port); | ||
1603 | sk = &tsk->sk; | 1776 | sk = &tsk->sk; |
1604 | 1777 | ||
1605 | /* Queue message */ | 1778 | /* Queue message */ |
@@ -1615,8 +1788,7 @@ int tipc_sk_rcv(struct sk_buff *buf) | |||
1615 | rc = -TIPC_ERR_OVERLOAD; | 1788 | rc = -TIPC_ERR_OVERLOAD; |
1616 | } | 1789 | } |
1617 | bh_unlock_sock(sk); | 1790 | bh_unlock_sock(sk); |
1618 | tipc_port_unlock(port); | 1791 | tipc_sk_put(tsk); |
1619 | |||
1620 | if (likely(!rc)) | 1792 | if (likely(!rc)) |
1621 | return 0; | 1793 | return 0; |
1622 | exit: | 1794 | exit: |
@@ -1803,10 +1975,8 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) | |||
1803 | { | 1975 | { |
1804 | struct sock *new_sk, *sk = sock->sk; | 1976 | struct sock *new_sk, *sk = sock->sk; |
1805 | struct sk_buff *buf; | 1977 | struct sk_buff *buf; |
1806 | struct tipc_port *new_port; | 1978 | struct tipc_sock *new_tsock; |
1807 | struct tipc_msg *msg; | 1979 | struct tipc_msg *msg; |
1808 | struct tipc_portid peer; | ||
1809 | u32 new_ref; | ||
1810 | long timeo; | 1980 | long timeo; |
1811 | int res; | 1981 | int res; |
1812 | 1982 | ||
@@ -1828,8 +1998,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) | |||
1828 | goto exit; | 1998 | goto exit; |
1829 | 1999 | ||
1830 | new_sk = new_sock->sk; | 2000 | new_sk = new_sock->sk; |
1831 | new_port = &tipc_sk(new_sk)->port; | 2001 | new_tsock = tipc_sk(new_sk); |
1832 | new_ref = new_port->ref; | ||
1833 | msg = buf_msg(buf); | 2002 | msg = buf_msg(buf); |
1834 | 2003 | ||
1835 | /* 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 */ |
@@ -1839,18 +2008,16 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) | |||
1839 | * Reject any stray messages received by new socket | 2008 | * Reject any stray messages received by new socket |
1840 | * before the socket lock was taken (very, very unlikely) | 2009 | * before the socket lock was taken (very, very unlikely) |
1841 | */ | 2010 | */ |
1842 | reject_rx_queue(new_sk); | 2011 | tsk_rej_rx_queue(new_sk); |
1843 | 2012 | ||
1844 | /* Connect new socket to it's peer */ | 2013 | /* Connect new socket to it's peer */ |
1845 | peer.ref = msg_origport(msg); | 2014 | 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; | 2015 | new_sock->state = SS_CONNECTED; |
1849 | 2016 | ||
1850 | tipc_port_set_importance(new_port, msg_importance(msg)); | 2017 | tsk_set_importance(new_tsock, msg_importance(msg)); |
1851 | if (msg_named(msg)) { | 2018 | if (msg_named(msg)) { |
1852 | new_port->conn_type = msg_nametype(msg); | 2019 | new_tsock->conn_type = msg_nametype(msg); |
1853 | new_port->conn_instance = msg_nameinst(msg); | 2020 | new_tsock->conn_instance = msg_nameinst(msg); |
1854 | } | 2021 | } |
1855 | 2022 | ||
1856 | /* | 2023 | /* |
@@ -1860,7 +2027,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) | |||
1860 | if (!msg_data_sz(msg)) { | 2027 | if (!msg_data_sz(msg)) { |
1861 | struct msghdr m = {NULL,}; | 2028 | struct msghdr m = {NULL,}; |
1862 | 2029 | ||
1863 | advance_rx_queue(sk); | 2030 | tsk_advance_rx_queue(sk); |
1864 | tipc_send_packet(NULL, new_sock, &m, 0); | 2031 | tipc_send_packet(NULL, new_sock, &m, 0); |
1865 | } else { | 2032 | } else { |
1866 | __skb_dequeue(&sk->sk_receive_queue); | 2033 | __skb_dequeue(&sk->sk_receive_queue); |
@@ -1886,9 +2053,8 @@ static int tipc_shutdown(struct socket *sock, int how) | |||
1886 | { | 2053 | { |
1887 | struct sock *sk = sock->sk; | 2054 | struct sock *sk = sock->sk; |
1888 | struct tipc_sock *tsk = tipc_sk(sk); | 2055 | struct tipc_sock *tsk = tipc_sk(sk); |
1889 | struct tipc_port *port = &tsk->port; | ||
1890 | struct sk_buff *buf; | 2056 | struct sk_buff *buf; |
1891 | u32 peer; | 2057 | u32 dnode; |
1892 | int res; | 2058 | int res; |
1893 | 2059 | ||
1894 | if (how != SHUT_RDWR) | 2060 | if (how != SHUT_RDWR) |
@@ -1908,15 +2074,21 @@ restart: | |||
1908 | kfree_skb(buf); | 2074 | kfree_skb(buf); |
1909 | goto restart; | 2075 | goto restart; |
1910 | } | 2076 | } |
1911 | tipc_port_disconnect(port->ref); | 2077 | if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN)) |
1912 | if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN)) | 2078 | tipc_link_xmit(buf, dnode, tsk->ref); |
1913 | tipc_link_xmit(buf, peer, 0); | 2079 | tipc_node_remove_conn(dnode, tsk->ref); |
1914 | } else { | 2080 | } else { |
1915 | 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); | ||
1916 | } | 2088 | } |
1917 | 2089 | tsk->connected = 0; | |
1918 | sock->state = SS_DISCONNECTING; | 2090 | sock->state = SS_DISCONNECTING; |
1919 | 2091 | tipc_node_remove_conn(dnode, tsk->ref); | |
1920 | /* fall through */ | 2092 | /* fall through */ |
1921 | 2093 | ||
1922 | case SS_DISCONNECTING: | 2094 | case SS_DISCONNECTING: |
@@ -1937,6 +2109,432 @@ restart: | |||
1937 | return res; | 2109 | return res; |
1938 | } | 2110 | } |
1939 | 2111 | ||
2112 | static 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); | ||
2148 | exit: | ||
2149 | tipc_sk_put(tsk); | ||
2150 | } | ||
2151 | |||
2152 | static 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 | |||
2175 | static 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 | |||
2206 | static 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 | |||
2250 | struct 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 | */ | ||
2285 | void 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 | */ | ||
2306 | struct 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 | */ | ||
2321 | struct 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 | |||
2348 | static struct ref_table tipc_ref_table; | ||
2349 | |||
2350 | static DEFINE_RWLOCK(ref_table_lock); | ||
2351 | |||
2352 | /** | ||
2353 | * tipc_ref_table_init - create reference table for sockets | ||
2354 | */ | ||
2355 | int 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 | */ | ||
2386 | void 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 | */ | ||
2400 | u32 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 | */ | ||
2448 | void 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; | ||
2487 | exit: | ||
2488 | write_unlock_bh(&ref_table_lock); | ||
2489 | } | ||
2490 | |||
2491 | /* tipc_sk_get - find referenced socket and return pointer to it | ||
2492 | */ | ||
2493 | struct 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 | */ | ||
2513 | struct 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 | |||
2533 | static void tipc_sk_put(struct tipc_sock *tsk) | ||
2534 | { | ||
2535 | sock_put(&tsk->sk); | ||
2536 | } | ||
2537 | |||
1940 | /** | 2538 | /** |
1941 | * tipc_setsockopt - set socket option | 2539 | * tipc_setsockopt - set socket option |
1942 | * @sock: socket structure | 2540 | * @sock: socket structure |
@@ -1955,7 +2553,6 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt, | |||
1955 | { | 2553 | { |
1956 | struct sock *sk = sock->sk; | 2554 | struct sock *sk = sock->sk; |
1957 | struct tipc_sock *tsk = tipc_sk(sk); | 2555 | struct tipc_sock *tsk = tipc_sk(sk); |
1958 | struct tipc_port *port = &tsk->port; | ||
1959 | u32 value; | 2556 | u32 value; |
1960 | int res; | 2557 | int res; |
1961 | 2558 | ||
@@ -1973,16 +2570,16 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt, | |||
1973 | 2570 | ||
1974 | switch (opt) { | 2571 | switch (opt) { |
1975 | case TIPC_IMPORTANCE: | 2572 | case TIPC_IMPORTANCE: |
1976 | tipc_port_set_importance(port, value); | 2573 | res = tsk_set_importance(tsk, value); |
1977 | break; | 2574 | break; |
1978 | case TIPC_SRC_DROPPABLE: | 2575 | case TIPC_SRC_DROPPABLE: |
1979 | if (sock->type != SOCK_STREAM) | 2576 | if (sock->type != SOCK_STREAM) |
1980 | tipc_port_set_unreliable(port, value); | 2577 | tsk_set_unreliable(tsk, value); |
1981 | else | 2578 | else |
1982 | res = -ENOPROTOOPT; | 2579 | res = -ENOPROTOOPT; |
1983 | break; | 2580 | break; |
1984 | case TIPC_DEST_DROPPABLE: | 2581 | case TIPC_DEST_DROPPABLE: |
1985 | tipc_port_set_unreturnable(port, value); | 2582 | tsk_set_unreturnable(tsk, value); |
1986 | break; | 2583 | break; |
1987 | case TIPC_CONN_TIMEOUT: | 2584 | case TIPC_CONN_TIMEOUT: |
1988 | tipc_sk(sk)->conn_timeout = value; | 2585 | tipc_sk(sk)->conn_timeout = value; |
@@ -2015,7 +2612,6 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt, | |||
2015 | { | 2612 | { |
2016 | struct sock *sk = sock->sk; | 2613 | struct sock *sk = sock->sk; |
2017 | struct tipc_sock *tsk = tipc_sk(sk); | 2614 | struct tipc_sock *tsk = tipc_sk(sk); |
2018 | struct tipc_port *port = &tsk->port; | ||
2019 | int len; | 2615 | int len; |
2020 | u32 value; | 2616 | u32 value; |
2021 | int res; | 2617 | int res; |
@@ -2032,16 +2628,16 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt, | |||
2032 | 2628 | ||
2033 | switch (opt) { | 2629 | switch (opt) { |
2034 | case TIPC_IMPORTANCE: | 2630 | case TIPC_IMPORTANCE: |
2035 | value = tipc_port_importance(port); | 2631 | value = tsk_importance(tsk); |
2036 | break; | 2632 | break; |
2037 | case TIPC_SRC_DROPPABLE: | 2633 | case TIPC_SRC_DROPPABLE: |
2038 | value = tipc_port_unreliable(port); | 2634 | value = tsk_unreliable(tsk); |
2039 | break; | 2635 | break; |
2040 | case TIPC_DEST_DROPPABLE: | 2636 | case TIPC_DEST_DROPPABLE: |
2041 | value = tipc_port_unreturnable(port); | 2637 | value = tsk_unreturnable(tsk); |
2042 | break; | 2638 | break; |
2043 | case TIPC_CONN_TIMEOUT: | 2639 | case TIPC_CONN_TIMEOUT: |
2044 | value = tipc_sk(sk)->conn_timeout; | 2640 | value = tsk->conn_timeout; |
2045 | /* no need to set "res", since already 0 at this point */ | 2641 | /* no need to set "res", since already 0 at this point */ |
2046 | break; | 2642 | break; |
2047 | case TIPC_NODE_RECVQ_DEPTH: | 2643 | case TIPC_NODE_RECVQ_DEPTH: |