Commit | Line | Data |
---|---|---|
b97bf3fd | 1 | /* |
02c00c2a | 2 | * net/tipc/socket.c: TIPC socket API |
c4307285 | 3 | * |
8826cde6 | 4 | * Copyright (c) 2001-2007, 2012-2014, Ericsson AB |
c5fa7b3c | 5 | * Copyright (c) 2004-2008, 2010-2013, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the names of the copyright holders nor the names of its | |
17 | * contributors may be used to endorse or promote products derived from | |
18 | * this software without specific prior written permission. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * Alternatively, this software may be distributed under the terms of the |
21 | * GNU General Public License ("GPL") version 2 as published by the Free | |
22 | * Software Foundation. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
b97bf3fd | 37 | #include "core.h" |
e2dafe87 | 38 | #include "name_table.h" |
78acb1f9 | 39 | #include "node.h" |
e2dafe87 | 40 | #include "link.h" |
2cf8aa19 | 41 | #include <linux/export.h> |
5a9ee0be | 42 | #include "config.h" |
2e84c60b | 43 | #include "socket.h" |
2cf8aa19 | 44 | |
b97bf3fd PL |
45 | #define SS_LISTENING -1 /* socket is listening */ |
46 | #define SS_READY -2 /* socket is connectionless */ | |
47 | ||
3654ea02 | 48 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
dadebc00 | 49 | #define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */ |
ac0074ee | 50 | #define TIPC_FWD_MSG 1 |
b97bf3fd | 51 | |
4f4482dc | 52 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
676d2369 | 53 | static void tipc_data_ready(struct sock *sk); |
f288bef4 | 54 | static void tipc_write_space(struct sock *sk); |
247f0f3c YX |
55 | static int tipc_release(struct socket *sock); |
56 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); | |
0abd8ff2 | 57 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); |
57289015 | 58 | static void tipc_sk_timeout(unsigned long ref); |
0fc87aae JPM |
59 | static int tipc_sk_publish(struct tipc_port *port, uint scope, |
60 | struct tipc_name_seq const *seq); | |
61 | static int tipc_sk_withdraw(struct tipc_port *port, uint scope, | |
62 | struct tipc_name_seq const *seq); | |
808d90f9 JPM |
63 | static u32 tipc_sk_ref_acquire(struct tipc_sock *tsk); |
64 | static void tipc_sk_ref_discard(u32 ref); | |
65 | static struct tipc_sock *tipc_sk_get(u32 ref); | |
66 | static struct tipc_sock *tipc_sk_get_next(u32 *ref); | |
67 | static void tipc_sk_put(struct tipc_sock *tsk); | |
b97bf3fd | 68 | |
bca65eae FW |
69 | static const struct proto_ops packet_ops; |
70 | static const struct proto_ops stream_ops; | |
71 | static const struct proto_ops msg_ops; | |
b97bf3fd PL |
72 | |
73 | static struct proto tipc_proto; | |
c5fa7b3c | 74 | static struct proto tipc_proto_kern; |
b97bf3fd | 75 | |
c4307285 | 76 | /* |
0c3141e9 AS |
77 | * Revised TIPC socket locking policy: |
78 | * | |
79 | * Most socket operations take the standard socket lock when they start | |
80 | * and hold it until they finish (or until they need to sleep). Acquiring | |
81 | * this lock grants the owner exclusive access to the fields of the socket | |
82 | * data structures, with the exception of the backlog queue. A few socket | |
83 | * operations can be done without taking the socket lock because they only | |
84 | * read socket information that never changes during the life of the socket. | |
85 | * | |
86 | * Socket operations may acquire the lock for the associated TIPC port if they | |
87 | * need to perform an operation on the port. If any routine needs to acquire | |
88 | * both the socket lock and the port lock it must take the socket lock first | |
89 | * to avoid the risk of deadlock. | |
90 | * | |
91 | * The dispatcher handling incoming messages cannot grab the socket lock in | |
92 | * the standard fashion, since invoked it runs at the BH level and cannot block. | |
93 | * Instead, it checks to see if the socket lock is currently owned by someone, | |
94 | * and either handles the message itself or adds it to the socket's backlog | |
95 | * queue; in the latter case the queued message is processed once the process | |
96 | * owning the socket lock releases it. | |
97 | * | |
98 | * NOTE: Releasing the socket lock while an operation is sleeping overcomes | |
99 | * the problem of a blocked socket operation preventing any other operations | |
100 | * from occurring. However, applications must be careful if they have | |
101 | * multiple threads trying to send (or receive) on the same socket, as these | |
102 | * operations might interfere with each other. For example, doing a connect | |
103 | * and a receive at the same time might allow the receive to consume the | |
104 | * ACK message meant for the connect. While additional work could be done | |
105 | * to try and overcome this, it doesn't seem to be worthwhile at the present. | |
106 | * | |
107 | * NOTE: Releasing the socket lock while an operation is sleeping also ensures | |
108 | * that another operation that must be performed in a non-blocking manner is | |
109 | * not delayed for very long because the lock has already been taken. | |
110 | * | |
111 | * NOTE: This code assumes that certain fields of a port/socket pair are | |
112 | * constant over its lifetime; such fields can be examined without taking | |
113 | * the socket lock and/or port lock, and do not need to be re-read even | |
114 | * after resuming processing after waiting. These fields include: | |
115 | * - socket type | |
116 | * - pointer to socket sk structure (aka tipc_sock structure) | |
117 | * - pointer to port structure | |
118 | * - port reference | |
119 | */ | |
120 | ||
2e84c60b JPM |
121 | static u32 tsk_peer_node(struct tipc_port *p_ptr) |
122 | { | |
123 | return msg_destnode(&p_ptr->phdr); | |
124 | } | |
125 | ||
126 | static u32 tsk_peer_port(struct tipc_port *p_ptr) | |
127 | { | |
128 | return msg_destport(&p_ptr->phdr); | |
129 | } | |
130 | ||
131 | static bool tsk_unreliable(struct tipc_port *port) | |
132 | { | |
133 | return msg_src_droppable(&port->phdr) != 0; | |
134 | } | |
135 | ||
136 | static void tsk_set_unreliable(struct tipc_port *port, bool unreliable) | |
137 | { | |
138 | msg_set_src_droppable(&port->phdr, unreliable ? 1 : 0); | |
139 | } | |
140 | ||
141 | static bool tsk_unreturnable(struct tipc_port *port) | |
142 | { | |
143 | return msg_dest_droppable(&port->phdr) != 0; | |
144 | } | |
145 | ||
146 | static void tsk_set_unreturnable(struct tipc_port *port, bool unreturnable) | |
147 | { | |
148 | msg_set_dest_droppable(&port->phdr, unreturnable ? 1 : 0); | |
149 | } | |
150 | ||
151 | static int tsk_importance(struct tipc_port *port) | |
152 | { | |
153 | return msg_importance(&port->phdr); | |
154 | } | |
155 | ||
156 | static int tsk_set_importance(struct tipc_port *port, int imp) | |
157 | { | |
158 | if (imp > TIPC_CRITICAL_IMPORTANCE) | |
159 | return -EINVAL; | |
160 | msg_set_importance(&port->phdr, (u32)imp); | |
161 | return 0; | |
162 | } | |
8826cde6 | 163 | |
0c3141e9 | 164 | /** |
2e84c60b | 165 | * tsk_advance_rx_queue - discard first buffer in socket receive queue |
0c3141e9 AS |
166 | * |
167 | * Caller must hold socket lock | |
b97bf3fd | 168 | */ |
2e84c60b | 169 | static void tsk_advance_rx_queue(struct sock *sk) |
b97bf3fd | 170 | { |
5f6d9123 | 171 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
b97bf3fd PL |
172 | } |
173 | ||
b97bf3fd | 174 | /** |
2e84c60b | 175 | * tsk_rej_rx_queue - reject all buffers in socket receive queue |
0c3141e9 AS |
176 | * |
177 | * Caller must hold socket lock | |
b97bf3fd | 178 | */ |
2e84c60b | 179 | static void tsk_rej_rx_queue(struct sock *sk) |
b97bf3fd | 180 | { |
0c3141e9 | 181 | struct sk_buff *buf; |
8db1bae3 | 182 | u32 dnode; |
0c3141e9 | 183 | |
8db1bae3 JPM |
184 | while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { |
185 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) | |
9fbfb8b1 | 186 | tipc_link_xmit(buf, dnode, 0); |
8db1bae3 | 187 | } |
b97bf3fd PL |
188 | } |
189 | ||
2e84c60b | 190 | /* tsk_peer_msg - verify if message was sent by connected port's peer |
0fc87aae JPM |
191 | * |
192 | * Handles cases where the node's network address has changed from | |
193 | * the default of <0.0.0> to its configured setting. | |
194 | */ | |
2e84c60b | 195 | static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) |
0fc87aae | 196 | { |
2e84c60b | 197 | u32 peer_port = tsk_peer_port(&tsk->port); |
0fc87aae JPM |
198 | u32 orig_node; |
199 | u32 peer_node; | |
200 | ||
201 | if (unlikely(!tsk->port.connected)) | |
202 | return false; | |
203 | ||
204 | if (unlikely(msg_origport(msg) != peer_port)) | |
205 | return false; | |
206 | ||
207 | orig_node = msg_orignode(msg); | |
2e84c60b | 208 | peer_node = tsk_peer_node(&tsk->port); |
0fc87aae JPM |
209 | |
210 | if (likely(orig_node == peer_node)) | |
211 | return true; | |
212 | ||
213 | if (!orig_node && (peer_node == tipc_own_addr)) | |
214 | return true; | |
215 | ||
216 | if (!peer_node && (orig_node == tipc_own_addr)) | |
217 | return true; | |
218 | ||
219 | return false; | |
220 | } | |
221 | ||
b97bf3fd | 222 | /** |
c5fa7b3c | 223 | * tipc_sk_create - create a TIPC socket |
0c3141e9 | 224 | * @net: network namespace (must be default network) |
b97bf3fd PL |
225 | * @sock: pre-allocated socket structure |
226 | * @protocol: protocol indicator (must be 0) | |
3f378b68 | 227 | * @kern: caused by kernel or by userspace? |
c4307285 | 228 | * |
0c3141e9 AS |
229 | * This routine creates additional data structures used by the TIPC socket, |
230 | * initializes them, and links them together. | |
b97bf3fd PL |
231 | * |
232 | * Returns 0 on success, errno otherwise | |
233 | */ | |
58ed9442 JPM |
234 | static int tipc_sk_create(struct net *net, struct socket *sock, |
235 | int protocol, int kern) | |
b97bf3fd | 236 | { |
0c3141e9 AS |
237 | const struct proto_ops *ops; |
238 | socket_state state; | |
b97bf3fd | 239 | struct sock *sk; |
58ed9442 JPM |
240 | struct tipc_sock *tsk; |
241 | struct tipc_port *port; | |
5b8fa7ce | 242 | struct tipc_msg *msg; |
58ed9442 | 243 | u32 ref; |
0c3141e9 AS |
244 | |
245 | /* Validate arguments */ | |
b97bf3fd PL |
246 | if (unlikely(protocol != 0)) |
247 | return -EPROTONOSUPPORT; | |
248 | ||
b97bf3fd PL |
249 | switch (sock->type) { |
250 | case SOCK_STREAM: | |
0c3141e9 AS |
251 | ops = &stream_ops; |
252 | state = SS_UNCONNECTED; | |
b97bf3fd PL |
253 | break; |
254 | case SOCK_SEQPACKET: | |
0c3141e9 AS |
255 | ops = &packet_ops; |
256 | state = SS_UNCONNECTED; | |
b97bf3fd PL |
257 | break; |
258 | case SOCK_DGRAM: | |
b97bf3fd | 259 | case SOCK_RDM: |
0c3141e9 AS |
260 | ops = &msg_ops; |
261 | state = SS_READY; | |
b97bf3fd | 262 | break; |
49978651 | 263 | default: |
49978651 | 264 | return -EPROTOTYPE; |
b97bf3fd PL |
265 | } |
266 | ||
0c3141e9 | 267 | /* Allocate socket's protocol area */ |
c5fa7b3c YX |
268 | if (!kern) |
269 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); | |
270 | else | |
271 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern); | |
272 | ||
0c3141e9 | 273 | if (sk == NULL) |
b97bf3fd | 274 | return -ENOMEM; |
b97bf3fd | 275 | |
58ed9442 JPM |
276 | tsk = tipc_sk(sk); |
277 | port = &tsk->port; | |
808d90f9 | 278 | ref = tipc_sk_ref_acquire(tsk); |
58ed9442 | 279 | if (!ref) { |
5b8fa7ce | 280 | pr_warn("Socket create failed; reference table exhausted\n"); |
0c3141e9 AS |
281 | return -ENOMEM; |
282 | } | |
5b8fa7ce JPM |
283 | port->max_pkt = MAX_PKT_DEFAULT; |
284 | port->ref = ref; | |
285 | INIT_LIST_HEAD(&port->publications); | |
5b8fa7ce | 286 | |
5b8fa7ce JPM |
287 | msg = &port->phdr; |
288 | tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, | |
289 | NAMED_H_SIZE, 0); | |
290 | msg_set_origport(msg, ref); | |
b97bf3fd | 291 | |
0c3141e9 | 292 | /* Finish initializing socket data structures */ |
0c3141e9 AS |
293 | sock->ops = ops; |
294 | sock->state = state; | |
0c3141e9 | 295 | sock_init_data(sock, sk); |
57289015 | 296 | k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref); |
4f4482dc | 297 | sk->sk_backlog_rcv = tipc_backlog_rcv; |
cc79dd1b | 298 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
f288bef4 YX |
299 | sk->sk_data_ready = tipc_data_ready; |
300 | sk->sk_write_space = tipc_write_space; | |
4f4482dc | 301 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
60120526 | 302 | tsk->sent_unacked = 0; |
4f4482dc | 303 | atomic_set(&tsk->dupl_rcvcnt, 0); |
7ef43eba | 304 | |
0c3141e9 | 305 | if (sock->state == SS_READY) { |
2e84c60b | 306 | tsk_set_unreturnable(port, true); |
0c3141e9 | 307 | if (sock->type == SOCK_DGRAM) |
2e84c60b | 308 | tsk_set_unreliable(port, true); |
0c3141e9 | 309 | } |
b97bf3fd PL |
310 | return 0; |
311 | } | |
312 | ||
c5fa7b3c YX |
313 | /** |
314 | * tipc_sock_create_local - create TIPC socket from inside TIPC module | |
315 | * @type: socket type - SOCK_RDM or SOCK_SEQPACKET | |
316 | * | |
317 | * We cannot use sock_creat_kern here because it bumps module user count. | |
318 | * Since socket owner and creator is the same module we must make sure | |
319 | * that module count remains zero for module local sockets, otherwise | |
320 | * we cannot do rmmod. | |
321 | * | |
322 | * Returns 0 on success, errno otherwise | |
323 | */ | |
324 | int tipc_sock_create_local(int type, struct socket **res) | |
325 | { | |
326 | int rc; | |
c5fa7b3c YX |
327 | |
328 | rc = sock_create_lite(AF_TIPC, type, 0, res); | |
329 | if (rc < 0) { | |
330 | pr_err("Failed to create kernel socket\n"); | |
331 | return rc; | |
332 | } | |
333 | tipc_sk_create(&init_net, *res, 0, 1); | |
334 | ||
c5fa7b3c YX |
335 | return 0; |
336 | } | |
337 | ||
338 | /** | |
339 | * tipc_sock_release_local - release socket created by tipc_sock_create_local | |
340 | * @sock: the socket to be released. | |
341 | * | |
342 | * Module reference count is not incremented when such sockets are created, | |
343 | * so we must keep it from being decremented when they are released. | |
344 | */ | |
345 | void tipc_sock_release_local(struct socket *sock) | |
346 | { | |
247f0f3c | 347 | tipc_release(sock); |
c5fa7b3c YX |
348 | sock->ops = NULL; |
349 | sock_release(sock); | |
350 | } | |
351 | ||
352 | /** | |
353 | * tipc_sock_accept_local - accept a connection on a socket created | |
354 | * with tipc_sock_create_local. Use this function to avoid that | |
355 | * module reference count is inadvertently incremented. | |
356 | * | |
357 | * @sock: the accepting socket | |
358 | * @newsock: reference to the new socket to be created | |
359 | * @flags: socket flags | |
360 | */ | |
361 | ||
362 | int tipc_sock_accept_local(struct socket *sock, struct socket **newsock, | |
ae8509c4 | 363 | int flags) |
c5fa7b3c YX |
364 | { |
365 | struct sock *sk = sock->sk; | |
366 | int ret; | |
367 | ||
368 | ret = sock_create_lite(sk->sk_family, sk->sk_type, | |
369 | sk->sk_protocol, newsock); | |
370 | if (ret < 0) | |
371 | return ret; | |
372 | ||
247f0f3c | 373 | ret = tipc_accept(sock, *newsock, flags); |
c5fa7b3c YX |
374 | if (ret < 0) { |
375 | sock_release(*newsock); | |
376 | return ret; | |
377 | } | |
378 | (*newsock)->ops = sock->ops; | |
379 | return ret; | |
380 | } | |
381 | ||
b97bf3fd | 382 | /** |
247f0f3c | 383 | * tipc_release - destroy a TIPC socket |
b97bf3fd PL |
384 | * @sock: socket to destroy |
385 | * | |
386 | * This routine cleans up any messages that are still queued on the socket. | |
387 | * For DGRAM and RDM socket types, all queued messages are rejected. | |
388 | * For SEQPACKET and STREAM socket types, the first message is rejected | |
389 | * and any others are discarded. (If the first message on a STREAM socket | |
390 | * is partially-read, it is discarded and the next one is rejected instead.) | |
c4307285 | 391 | * |
b97bf3fd PL |
392 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
393 | * are returned or discarded according to the "destination droppable" setting | |
394 | * specified for the message by the sender. | |
395 | * | |
396 | * Returns 0 on success, errno otherwise | |
397 | */ | |
247f0f3c | 398 | static int tipc_release(struct socket *sock) |
b97bf3fd | 399 | { |
b97bf3fd | 400 | struct sock *sk = sock->sk; |
58ed9442 JPM |
401 | struct tipc_sock *tsk; |
402 | struct tipc_port *port; | |
b97bf3fd | 403 | struct sk_buff *buf; |
8db1bae3 | 404 | u32 dnode; |
b97bf3fd | 405 | |
0c3141e9 AS |
406 | /* |
407 | * Exit if socket isn't fully initialized (occurs when a failed accept() | |
408 | * releases a pre-allocated child socket that was never used) | |
409 | */ | |
0c3141e9 | 410 | if (sk == NULL) |
b97bf3fd | 411 | return 0; |
c4307285 | 412 | |
58ed9442 JPM |
413 | tsk = tipc_sk(sk); |
414 | port = &tsk->port; | |
0c3141e9 AS |
415 | lock_sock(sk); |
416 | ||
417 | /* | |
418 | * Reject all unreceived messages, except on an active connection | |
419 | * (which disconnects locally & sends a 'FIN+' to peer) | |
420 | */ | |
2e84c60b | 421 | dnode = tsk_peer_node(port); |
b97bf3fd | 422 | while (sock->state != SS_DISCONNECTING) { |
0c3141e9 AS |
423 | buf = __skb_dequeue(&sk->sk_receive_queue); |
424 | if (buf == NULL) | |
b97bf3fd | 425 | break; |
40682432 | 426 | if (TIPC_SKB_CB(buf)->handle != NULL) |
5f6d9123 | 427 | kfree_skb(buf); |
0c3141e9 AS |
428 | else { |
429 | if ((sock->state == SS_CONNECTING) || | |
430 | (sock->state == SS_CONNECTED)) { | |
431 | sock->state = SS_DISCONNECTING; | |
dadebc00 | 432 | port->connected = 0; |
5b8fa7ce | 433 | tipc_node_remove_conn(dnode, port->ref); |
0c3141e9 | 434 | } |
8db1bae3 | 435 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) |
9fbfb8b1 | 436 | tipc_link_xmit(buf, dnode, 0); |
0c3141e9 | 437 | } |
b97bf3fd PL |
438 | } |
439 | ||
0fc87aae | 440 | tipc_sk_withdraw(port, 0, NULL); |
808d90f9 | 441 | tipc_sk_ref_discard(port->ref); |
5b8fa7ce JPM |
442 | k_cancel_timer(&port->timer); |
443 | if (port->connected) { | |
444 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, | |
445 | SHORT_H_SIZE, 0, dnode, tipc_own_addr, | |
2e84c60b | 446 | tsk_peer_port(port), |
5b8fa7ce JPM |
447 | port->ref, TIPC_ERR_NO_PORT); |
448 | if (buf) | |
449 | tipc_link_xmit(buf, dnode, port->ref); | |
450 | tipc_node_remove_conn(dnode, port->ref); | |
451 | } | |
5b8fa7ce | 452 | k_term_timer(&port->timer); |
b97bf3fd | 453 | |
0c3141e9 | 454 | /* Discard any remaining (connection-based) messages in receive queue */ |
57467e56 | 455 | __skb_queue_purge(&sk->sk_receive_queue); |
b97bf3fd | 456 | |
0c3141e9 | 457 | /* Reject any messages that accumulated in backlog queue */ |
0c3141e9 AS |
458 | sock->state = SS_DISCONNECTING; |
459 | release_sock(sk); | |
b97bf3fd | 460 | sock_put(sk); |
0c3141e9 | 461 | sock->sk = NULL; |
b97bf3fd | 462 | |
065d7e39 | 463 | return 0; |
b97bf3fd PL |
464 | } |
465 | ||
466 | /** | |
247f0f3c | 467 | * tipc_bind - associate or disassocate TIPC name(s) with a socket |
b97bf3fd PL |
468 | * @sock: socket structure |
469 | * @uaddr: socket address describing name(s) and desired operation | |
470 | * @uaddr_len: size of socket address data structure | |
c4307285 | 471 | * |
b97bf3fd PL |
472 | * Name and name sequence binding is indicated using a positive scope value; |
473 | * a negative scope value unbinds the specified name. Specifying no name | |
474 | * (i.e. a socket address length of 0) unbinds all names from the socket. | |
c4307285 | 475 | * |
b97bf3fd | 476 | * Returns 0 on success, errno otherwise |
0c3141e9 AS |
477 | * |
478 | * NOTE: This routine doesn't need to take the socket lock since it doesn't | |
479 | * access any non-constant socket information. | |
b97bf3fd | 480 | */ |
247f0f3c YX |
481 | static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, |
482 | int uaddr_len) | |
b97bf3fd | 483 | { |
84602761 | 484 | struct sock *sk = sock->sk; |
b97bf3fd | 485 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
58ed9442 | 486 | struct tipc_sock *tsk = tipc_sk(sk); |
84602761 | 487 | int res = -EINVAL; |
b97bf3fd | 488 | |
84602761 YX |
489 | lock_sock(sk); |
490 | if (unlikely(!uaddr_len)) { | |
0fc87aae | 491 | res = tipc_sk_withdraw(&tsk->port, 0, NULL); |
84602761 YX |
492 | goto exit; |
493 | } | |
c4307285 | 494 | |
84602761 YX |
495 | if (uaddr_len < sizeof(struct sockaddr_tipc)) { |
496 | res = -EINVAL; | |
497 | goto exit; | |
498 | } | |
499 | if (addr->family != AF_TIPC) { | |
500 | res = -EAFNOSUPPORT; | |
501 | goto exit; | |
502 | } | |
b97bf3fd | 503 | |
b97bf3fd PL |
504 | if (addr->addrtype == TIPC_ADDR_NAME) |
505 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; | |
84602761 YX |
506 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { |
507 | res = -EAFNOSUPPORT; | |
508 | goto exit; | |
509 | } | |
c4307285 | 510 | |
13a2e898 | 511 | if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && |
7d0ab17b | 512 | (addr->addr.nameseq.type != TIPC_TOP_SRV) && |
84602761 YX |
513 | (addr->addr.nameseq.type != TIPC_CFG_SRV)) { |
514 | res = -EACCES; | |
515 | goto exit; | |
516 | } | |
c422f1bd | 517 | |
84602761 | 518 | res = (addr->scope > 0) ? |
0fc87aae JPM |
519 | tipc_sk_publish(&tsk->port, addr->scope, &addr->addr.nameseq) : |
520 | tipc_sk_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq); | |
84602761 YX |
521 | exit: |
522 | release_sock(sk); | |
523 | return res; | |
b97bf3fd PL |
524 | } |
525 | ||
c4307285 | 526 | /** |
247f0f3c | 527 | * tipc_getname - get port ID of socket or peer socket |
b97bf3fd PL |
528 | * @sock: socket structure |
529 | * @uaddr: area for returned socket address | |
530 | * @uaddr_len: area for returned length of socket address | |
2da59918 | 531 | * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID |
c4307285 | 532 | * |
b97bf3fd | 533 | * Returns 0 on success, errno otherwise |
0c3141e9 | 534 | * |
2da59918 AS |
535 | * NOTE: This routine doesn't need to take the socket lock since it only |
536 | * accesses socket information that is unchanging (or which changes in | |
0e65967e | 537 | * a completely predictable manner). |
b97bf3fd | 538 | */ |
247f0f3c YX |
539 | static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, |
540 | int *uaddr_len, int peer) | |
b97bf3fd | 541 | { |
b97bf3fd | 542 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
58ed9442 | 543 | struct tipc_sock *tsk = tipc_sk(sock->sk); |
b97bf3fd | 544 | |
88f8a5e3 | 545 | memset(addr, 0, sizeof(*addr)); |
0c3141e9 | 546 | if (peer) { |
2da59918 AS |
547 | if ((sock->state != SS_CONNECTED) && |
548 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) | |
549 | return -ENOTCONN; | |
2e84c60b JPM |
550 | addr->addr.id.ref = tsk_peer_port(&tsk->port); |
551 | addr->addr.id.node = tsk_peer_node(&tsk->port); | |
0c3141e9 | 552 | } else { |
58ed9442 | 553 | addr->addr.id.ref = tsk->port.ref; |
b924dcf0 | 554 | addr->addr.id.node = tipc_own_addr; |
0c3141e9 | 555 | } |
b97bf3fd PL |
556 | |
557 | *uaddr_len = sizeof(*addr); | |
558 | addr->addrtype = TIPC_ADDR_ID; | |
559 | addr->family = AF_TIPC; | |
560 | addr->scope = 0; | |
b97bf3fd PL |
561 | addr->addr.name.domain = 0; |
562 | ||
0c3141e9 | 563 | return 0; |
b97bf3fd PL |
564 | } |
565 | ||
566 | /** | |
247f0f3c | 567 | * tipc_poll - read and possibly block on pollmask |
b97bf3fd PL |
568 | * @file: file structure associated with the socket |
569 | * @sock: socket for which to calculate the poll bits | |
570 | * @wait: ??? | |
571 | * | |
9b674e82 AS |
572 | * Returns pollmask value |
573 | * | |
574 | * COMMENTARY: | |
575 | * It appears that the usual socket locking mechanisms are not useful here | |
576 | * since the pollmask info is potentially out-of-date the moment this routine | |
577 | * exits. TCP and other protocols seem to rely on higher level poll routines | |
578 | * to handle any preventable race conditions, so TIPC will do the same ... | |
579 | * | |
580 | * TIPC sets the returned events as follows: | |
f662c070 AS |
581 | * |
582 | * socket state flags set | |
583 | * ------------ --------- | |
584 | * unconnected no read flags | |
c4fc298a | 585 | * POLLOUT if port is not congested |
f662c070 AS |
586 | * |
587 | * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue | |
588 | * no write flags | |
589 | * | |
590 | * connected POLLIN/POLLRDNORM if data in rx queue | |
591 | * POLLOUT if port is not congested | |
592 | * | |
593 | * disconnecting POLLIN/POLLRDNORM/POLLHUP | |
594 | * no write flags | |
595 | * | |
596 | * listening POLLIN if SYN in rx queue | |
597 | * no write flags | |
598 | * | |
599 | * ready POLLIN/POLLRDNORM if data in rx queue | |
600 | * [connectionless] POLLOUT (since port cannot be congested) | |
601 | * | |
602 | * IMPORTANT: The fact that a read or write operation is indicated does NOT | |
603 | * imply that the operation will succeed, merely that it should be performed | |
604 | * and will not block. | |
b97bf3fd | 605 | */ |
247f0f3c YX |
606 | static unsigned int tipc_poll(struct file *file, struct socket *sock, |
607 | poll_table *wait) | |
b97bf3fd | 608 | { |
9b674e82 | 609 | struct sock *sk = sock->sk; |
58ed9442 | 610 | struct tipc_sock *tsk = tipc_sk(sk); |
f662c070 | 611 | u32 mask = 0; |
9b674e82 | 612 | |
f288bef4 | 613 | sock_poll_wait(file, sk_sleep(sk), wait); |
9b674e82 | 614 | |
f662c070 | 615 | switch ((int)sock->state) { |
c4fc298a | 616 | case SS_UNCONNECTED: |
60120526 | 617 | if (!tsk->link_cong) |
c4fc298a EH |
618 | mask |= POLLOUT; |
619 | break; | |
f662c070 AS |
620 | case SS_READY: |
621 | case SS_CONNECTED: | |
60120526 | 622 | if (!tsk->link_cong && !tipc_sk_conn_cong(tsk)) |
f662c070 AS |
623 | mask |= POLLOUT; |
624 | /* fall thru' */ | |
625 | case SS_CONNECTING: | |
626 | case SS_LISTENING: | |
627 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
628 | mask |= (POLLIN | POLLRDNORM); | |
629 | break; | |
630 | case SS_DISCONNECTING: | |
631 | mask = (POLLIN | POLLRDNORM | POLLHUP); | |
632 | break; | |
633 | } | |
9b674e82 AS |
634 | |
635 | return mask; | |
b97bf3fd PL |
636 | } |
637 | ||
0abd8ff2 JPM |
638 | /** |
639 | * tipc_sendmcast - send multicast message | |
640 | * @sock: socket structure | |
641 | * @seq: destination address | |
642 | * @iov: message data to send | |
643 | * @dsz: total length of message data | |
644 | * @timeo: timeout to wait for wakeup | |
645 | * | |
646 | * Called from function tipc_sendmsg(), which has done all sanity checks | |
647 | * Returns the number of bytes sent on success, or errno | |
648 | */ | |
649 | static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, | |
650 | struct iovec *iov, size_t dsz, long timeo) | |
651 | { | |
652 | struct sock *sk = sock->sk; | |
653 | struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr; | |
654 | struct sk_buff *buf; | |
655 | uint mtu; | |
656 | int rc; | |
657 | ||
658 | msg_set_type(mhdr, TIPC_MCAST_MSG); | |
659 | msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE); | |
660 | msg_set_destport(mhdr, 0); | |
661 | msg_set_destnode(mhdr, 0); | |
662 | msg_set_nametype(mhdr, seq->type); | |
663 | msg_set_namelower(mhdr, seq->lower); | |
664 | msg_set_nameupper(mhdr, seq->upper); | |
665 | msg_set_hdr_sz(mhdr, MCAST_H_SIZE); | |
666 | ||
667 | new_mtu: | |
668 | mtu = tipc_bclink_get_mtu(); | |
9fbfb8b1 | 669 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); |
0abd8ff2 JPM |
670 | if (unlikely(rc < 0)) |
671 | return rc; | |
672 | ||
673 | do { | |
674 | rc = tipc_bclink_xmit(buf); | |
675 | if (likely(rc >= 0)) { | |
676 | rc = dsz; | |
677 | break; | |
678 | } | |
679 | if (rc == -EMSGSIZE) | |
680 | goto new_mtu; | |
681 | if (rc != -ELINKCONG) | |
682 | break; | |
50100a5e | 683 | tipc_sk(sk)->link_cong = 1; |
0abd8ff2 JPM |
684 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
685 | if (rc) | |
686 | kfree_skb_list(buf); | |
687 | } while (!rc); | |
688 | return rc; | |
689 | } | |
690 | ||
078bec82 JPM |
691 | /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets |
692 | */ | |
693 | void tipc_sk_mcast_rcv(struct sk_buff *buf) | |
694 | { | |
695 | struct tipc_msg *msg = buf_msg(buf); | |
696 | struct tipc_port_list dports = {0, NULL, }; | |
697 | struct tipc_port_list *item; | |
698 | struct sk_buff *b; | |
699 | uint i, last, dst = 0; | |
700 | u32 scope = TIPC_CLUSTER_SCOPE; | |
701 | ||
702 | if (in_own_node(msg_orignode(msg))) | |
703 | scope = TIPC_NODE_SCOPE; | |
704 | ||
705 | /* Create destination port list: */ | |
706 | tipc_nametbl_mc_translate(msg_nametype(msg), | |
707 | msg_namelower(msg), | |
708 | msg_nameupper(msg), | |
709 | scope, | |
710 | &dports); | |
711 | last = dports.count; | |
712 | if (!last) { | |
713 | kfree_skb(buf); | |
714 | return; | |
715 | } | |
716 | ||
717 | for (item = &dports; item; item = item->next) { | |
718 | for (i = 0; i < PLSIZE && ++dst <= last; i++) { | |
719 | b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf; | |
720 | if (!b) { | |
721 | pr_warn("Failed do clone mcast rcv buffer\n"); | |
722 | continue; | |
723 | } | |
724 | msg_set_destport(msg, item->ports[i]); | |
725 | tipc_sk_rcv(b); | |
726 | } | |
727 | } | |
728 | tipc_port_list_free(&dports); | |
729 | } | |
730 | ||
ac0074ee JPM |
731 | /** |
732 | * tipc_sk_proto_rcv - receive a connection mng protocol message | |
733 | * @tsk: receiving socket | |
734 | * @dnode: node to send response message to, if any | |
735 | * @buf: buffer containing protocol message | |
736 | * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if | |
737 | * (CONN_PROBE_REPLY) message should be forwarded. | |
738 | */ | |
52f50ce5 WY |
739 | static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, |
740 | struct sk_buff *buf) | |
ac0074ee JPM |
741 | { |
742 | struct tipc_msg *msg = buf_msg(buf); | |
743 | struct tipc_port *port = &tsk->port; | |
60120526 | 744 | int conn_cong; |
ac0074ee JPM |
745 | |
746 | /* Ignore if connection cannot be validated: */ | |
2e84c60b | 747 | if (!tsk_peer_msg(tsk, msg)) |
ac0074ee JPM |
748 | goto exit; |
749 | ||
750 | port->probing_state = TIPC_CONN_OK; | |
751 | ||
752 | if (msg_type(msg) == CONN_ACK) { | |
60120526 JPM |
753 | conn_cong = tipc_sk_conn_cong(tsk); |
754 | tsk->sent_unacked -= msg_msgcnt(msg); | |
755 | if (conn_cong) | |
50100a5e | 756 | tsk->sk.sk_write_space(&tsk->sk); |
ac0074ee JPM |
757 | } else if (msg_type(msg) == CONN_PROBE) { |
758 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) | |
759 | return TIPC_OK; | |
760 | msg_set_type(msg, CONN_PROBE_REPLY); | |
761 | return TIPC_FWD_MSG; | |
762 | } | |
763 | /* Do nothing if msg_type() == CONN_PROBE_REPLY */ | |
764 | exit: | |
765 | kfree_skb(buf); | |
766 | return TIPC_OK; | |
767 | } | |
768 | ||
c4307285 | 769 | /** |
b97bf3fd PL |
770 | * dest_name_check - verify user is permitted to send to specified port name |
771 | * @dest: destination address | |
772 | * @m: descriptor for message to be sent | |
c4307285 | 773 | * |
b97bf3fd PL |
774 | * Prevents restricted configuration commands from being issued by |
775 | * unauthorized users. | |
c4307285 | 776 | * |
b97bf3fd PL |
777 | * Returns 0 if permission is granted, otherwise errno |
778 | */ | |
05790c64 | 779 | static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) |
b97bf3fd PL |
780 | { |
781 | struct tipc_cfg_msg_hdr hdr; | |
782 | ||
e2dafe87 JPM |
783 | if (unlikely(dest->addrtype == TIPC_ADDR_ID)) |
784 | return 0; | |
c4307285 YH |
785 | if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) |
786 | return 0; | |
787 | if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) | |
788 | return 0; | |
c4307285 YH |
789 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
790 | return -EACCES; | |
b97bf3fd | 791 | |
3f8dd944 AS |
792 | if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr))) |
793 | return -EMSGSIZE; | |
c4307285 | 794 | if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) |
b97bf3fd | 795 | return -EFAULT; |
70cb2347 | 796 | if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) |
b97bf3fd | 797 | return -EACCES; |
c4307285 | 798 | |
b97bf3fd PL |
799 | return 0; |
800 | } | |
801 | ||
3f40504f YX |
802 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) |
803 | { | |
804 | struct sock *sk = sock->sk; | |
58ed9442 | 805 | struct tipc_sock *tsk = tipc_sk(sk); |
3f40504f YX |
806 | DEFINE_WAIT(wait); |
807 | int done; | |
808 | ||
809 | do { | |
810 | int err = sock_error(sk); | |
811 | if (err) | |
812 | return err; | |
813 | if (sock->state == SS_DISCONNECTING) | |
814 | return -EPIPE; | |
815 | if (!*timeo_p) | |
816 | return -EAGAIN; | |
817 | if (signal_pending(current)) | |
818 | return sock_intr_errno(*timeo_p); | |
819 | ||
820 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
60120526 | 821 | done = sk_wait_event(sk, timeo_p, !tsk->link_cong); |
3f40504f YX |
822 | finish_wait(sk_sleep(sk), &wait); |
823 | } while (!done); | |
824 | return 0; | |
825 | } | |
826 | ||
b97bf3fd | 827 | /** |
247f0f3c | 828 | * tipc_sendmsg - send message in connectionless manner |
0c3141e9 | 829 | * @iocb: if NULL, indicates that socket lock is already held |
b97bf3fd PL |
830 | * @sock: socket structure |
831 | * @m: message to send | |
e2dafe87 | 832 | * @dsz: amount of user data to be sent |
c4307285 | 833 | * |
b97bf3fd | 834 | * Message must have an destination specified explicitly. |
c4307285 | 835 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
b97bf3fd PL |
836 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
837 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) | |
c4307285 | 838 | * |
b97bf3fd PL |
839 | * Returns the number of bytes sent on success, or errno otherwise |
840 | */ | |
247f0f3c | 841 | static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, |
e2dafe87 | 842 | struct msghdr *m, size_t dsz) |
b97bf3fd | 843 | { |
e2dafe87 | 844 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
0c3141e9 | 845 | struct sock *sk = sock->sk; |
58ed9442 | 846 | struct tipc_sock *tsk = tipc_sk(sk); |
5c311421 | 847 | struct tipc_port *port = &tsk->port; |
e2dafe87 JPM |
848 | struct tipc_msg *mhdr = &port->phdr; |
849 | struct iovec *iov = m->msg_iov; | |
850 | u32 dnode, dport; | |
851 | struct sk_buff *buf; | |
852 | struct tipc_name_seq *seq = &dest->addr.nameseq; | |
853 | u32 mtu; | |
3f40504f | 854 | long timeo; |
e2dafe87 | 855 | int rc = -EINVAL; |
b97bf3fd PL |
856 | |
857 | if (unlikely(!dest)) | |
858 | return -EDESTADDRREQ; | |
e2dafe87 | 859 | |
51f9cc1f AS |
860 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
861 | (dest->family != AF_TIPC))) | |
b97bf3fd | 862 | return -EINVAL; |
e2dafe87 JPM |
863 | |
864 | if (dsz > TIPC_MAX_USER_MSG_SIZE) | |
c29c3f70 | 865 | return -EMSGSIZE; |
b97bf3fd | 866 | |
0c3141e9 AS |
867 | if (iocb) |
868 | lock_sock(sk); | |
869 | ||
e2dafe87 | 870 | if (unlikely(sock->state != SS_READY)) { |
0c3141e9 | 871 | if (sock->state == SS_LISTENING) { |
e2dafe87 | 872 | rc = -EPIPE; |
0c3141e9 AS |
873 | goto exit; |
874 | } | |
875 | if (sock->state != SS_UNCONNECTED) { | |
e2dafe87 | 876 | rc = -EISCONN; |
0c3141e9 AS |
877 | goto exit; |
878 | } | |
58ed9442 | 879 | if (tsk->port.published) { |
e2dafe87 | 880 | rc = -EOPNOTSUPP; |
0c3141e9 AS |
881 | goto exit; |
882 | } | |
3388007b | 883 | if (dest->addrtype == TIPC_ADDR_NAME) { |
58ed9442 JPM |
884 | tsk->port.conn_type = dest->addr.name.name.type; |
885 | tsk->port.conn_instance = dest->addr.name.name.instance; | |
3388007b | 886 | } |
b97bf3fd | 887 | } |
e2dafe87 JPM |
888 | rc = dest_name_check(dest, m); |
889 | if (rc) | |
890 | goto exit; | |
b97bf3fd | 891 | |
3f40504f | 892 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
e2dafe87 JPM |
893 | |
894 | if (dest->addrtype == TIPC_ADDR_MCAST) { | |
895 | rc = tipc_sendmcast(sock, seq, iov, dsz, timeo); | |
896 | goto exit; | |
897 | } else if (dest->addrtype == TIPC_ADDR_NAME) { | |
898 | u32 type = dest->addr.name.name.type; | |
899 | u32 inst = dest->addr.name.name.instance; | |
900 | u32 domain = dest->addr.name.domain; | |
901 | ||
902 | dnode = domain; | |
903 | msg_set_type(mhdr, TIPC_NAMED_MSG); | |
904 | msg_set_hdr_sz(mhdr, NAMED_H_SIZE); | |
905 | msg_set_nametype(mhdr, type); | |
906 | msg_set_nameinst(mhdr, inst); | |
907 | msg_set_lookup_scope(mhdr, tipc_addr_scope(domain)); | |
908 | dport = tipc_nametbl_translate(type, inst, &dnode); | |
909 | msg_set_destnode(mhdr, dnode); | |
910 | msg_set_destport(mhdr, dport); | |
911 | if (unlikely(!dport && !dnode)) { | |
912 | rc = -EHOSTUNREACH; | |
913 | goto exit; | |
c4307285 | 914 | } |
e2dafe87 JPM |
915 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
916 | dnode = dest->addr.id.node; | |
917 | msg_set_type(mhdr, TIPC_DIRECT_MSG); | |
918 | msg_set_lookup_scope(mhdr, 0); | |
919 | msg_set_destnode(mhdr, dnode); | |
920 | msg_set_destport(mhdr, dest->addr.id.ref); | |
921 | msg_set_hdr_sz(mhdr, BASIC_H_SIZE); | |
922 | } | |
923 | ||
924 | new_mtu: | |
925 | mtu = tipc_node_get_mtu(dnode, tsk->port.ref); | |
9fbfb8b1 | 926 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); |
e2dafe87 JPM |
927 | if (rc < 0) |
928 | goto exit; | |
929 | ||
930 | do { | |
50100a5e | 931 | TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong; |
9fbfb8b1 | 932 | rc = tipc_link_xmit(buf, dnode, tsk->port.ref); |
e2dafe87 JPM |
933 | if (likely(rc >= 0)) { |
934 | if (sock->state != SS_READY) | |
0c3141e9 | 935 | sock->state = SS_CONNECTING; |
e2dafe87 | 936 | rc = dsz; |
0c3141e9 | 937 | break; |
c4307285 | 938 | } |
e2dafe87 JPM |
939 | if (rc == -EMSGSIZE) |
940 | goto new_mtu; | |
e2dafe87 | 941 | if (rc != -ELINKCONG) |
0c3141e9 | 942 | break; |
50100a5e | 943 | tsk->link_cong = 1; |
e2dafe87 | 944 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
70452dcb EH |
945 | if (rc) |
946 | kfree_skb_list(buf); | |
e2dafe87 | 947 | } while (!rc); |
0c3141e9 AS |
948 | exit: |
949 | if (iocb) | |
950 | release_sock(sk); | |
e2dafe87 JPM |
951 | |
952 | return rc; | |
b97bf3fd PL |
953 | } |
954 | ||
391a6dd1 YX |
955 | static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) |
956 | { | |
957 | struct sock *sk = sock->sk; | |
58ed9442 | 958 | struct tipc_sock *tsk = tipc_sk(sk); |
391a6dd1 YX |
959 | DEFINE_WAIT(wait); |
960 | int done; | |
961 | ||
962 | do { | |
963 | int err = sock_error(sk); | |
964 | if (err) | |
965 | return err; | |
966 | if (sock->state == SS_DISCONNECTING) | |
967 | return -EPIPE; | |
968 | else if (sock->state != SS_CONNECTED) | |
969 | return -ENOTCONN; | |
970 | if (!*timeo_p) | |
971 | return -EAGAIN; | |
972 | if (signal_pending(current)) | |
973 | return sock_intr_errno(*timeo_p); | |
974 | ||
975 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
976 | done = sk_wait_event(sk, timeo_p, | |
60120526 JPM |
977 | (!tsk->link_cong && |
978 | !tipc_sk_conn_cong(tsk)) || | |
979 | !tsk->port.connected); | |
391a6dd1 YX |
980 | finish_wait(sk_sleep(sk), &wait); |
981 | } while (!done); | |
982 | return 0; | |
983 | } | |
984 | ||
c4307285 | 985 | /** |
4ccfe5e0 JPM |
986 | * tipc_send_stream - send stream-oriented data |
987 | * @iocb: (unused) | |
b97bf3fd | 988 | * @sock: socket structure |
4ccfe5e0 JPM |
989 | * @m: data to send |
990 | * @dsz: total length of data to be transmitted | |
c4307285 | 991 | * |
4ccfe5e0 | 992 | * Used for SOCK_STREAM data. |
c4307285 | 993 | * |
4ccfe5e0 JPM |
994 | * Returns the number of bytes sent on success (or partial success), |
995 | * or errno if no data sent | |
b97bf3fd | 996 | */ |
4ccfe5e0 JPM |
997 | static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, |
998 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 999 | { |
0c3141e9 | 1000 | struct sock *sk = sock->sk; |
58ed9442 | 1001 | struct tipc_sock *tsk = tipc_sk(sk); |
4ccfe5e0 JPM |
1002 | struct tipc_port *port = &tsk->port; |
1003 | struct tipc_msg *mhdr = &port->phdr; | |
1004 | struct sk_buff *buf; | |
342dfc30 | 1005 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
4ccfe5e0 JPM |
1006 | u32 ref = port->ref; |
1007 | int rc = -EINVAL; | |
391a6dd1 | 1008 | long timeo; |
4ccfe5e0 JPM |
1009 | u32 dnode; |
1010 | uint mtu, send, sent = 0; | |
b97bf3fd PL |
1011 | |
1012 | /* Handle implied connection establishment */ | |
4ccfe5e0 JPM |
1013 | if (unlikely(dest)) { |
1014 | rc = tipc_sendmsg(iocb, sock, m, dsz); | |
1015 | if (dsz && (dsz == rc)) | |
60120526 | 1016 | tsk->sent_unacked = 1; |
4ccfe5e0 JPM |
1017 | return rc; |
1018 | } | |
1019 | if (dsz > (uint)INT_MAX) | |
c29c3f70 AS |
1020 | return -EMSGSIZE; |
1021 | ||
0c3141e9 AS |
1022 | if (iocb) |
1023 | lock_sock(sk); | |
b97bf3fd | 1024 | |
391a6dd1 YX |
1025 | if (unlikely(sock->state != SS_CONNECTED)) { |
1026 | if (sock->state == SS_DISCONNECTING) | |
4ccfe5e0 | 1027 | rc = -EPIPE; |
391a6dd1 | 1028 | else |
4ccfe5e0 | 1029 | rc = -ENOTCONN; |
391a6dd1 YX |
1030 | goto exit; |
1031 | } | |
1d835874 | 1032 | |
391a6dd1 | 1033 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
2e84c60b | 1034 | dnode = tsk_peer_node(port); |
4ccfe5e0 JPM |
1035 | |
1036 | next: | |
1037 | mtu = port->max_pkt; | |
1038 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); | |
9fbfb8b1 | 1039 | rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf); |
4ccfe5e0 JPM |
1040 | if (unlikely(rc < 0)) |
1041 | goto exit; | |
c4307285 | 1042 | do { |
60120526 | 1043 | if (likely(!tipc_sk_conn_cong(tsk))) { |
9fbfb8b1 | 1044 | rc = tipc_link_xmit(buf, dnode, ref); |
4ccfe5e0 | 1045 | if (likely(!rc)) { |
60120526 | 1046 | tsk->sent_unacked++; |
4ccfe5e0 JPM |
1047 | sent += send; |
1048 | if (sent == dsz) | |
1049 | break; | |
1050 | goto next; | |
1051 | } | |
1052 | if (rc == -EMSGSIZE) { | |
1053 | port->max_pkt = tipc_node_get_mtu(dnode, ref); | |
1054 | goto next; | |
1055 | } | |
1056 | if (rc != -ELINKCONG) | |
1057 | break; | |
50100a5e | 1058 | tsk->link_cong = 1; |
4ccfe5e0 JPM |
1059 | } |
1060 | rc = tipc_wait_for_sndpkt(sock, &timeo); | |
70452dcb EH |
1061 | if (rc) |
1062 | kfree_skb_list(buf); | |
4ccfe5e0 | 1063 | } while (!rc); |
391a6dd1 | 1064 | exit: |
0c3141e9 AS |
1065 | if (iocb) |
1066 | release_sock(sk); | |
4ccfe5e0 | 1067 | return sent ? sent : rc; |
b97bf3fd PL |
1068 | } |
1069 | ||
c4307285 | 1070 | /** |
4ccfe5e0 JPM |
1071 | * tipc_send_packet - send a connection-oriented message |
1072 | * @iocb: if NULL, indicates that socket lock is already held | |
b97bf3fd | 1073 | * @sock: socket structure |
4ccfe5e0 JPM |
1074 | * @m: message to send |
1075 | * @dsz: length of data to be transmitted | |
c4307285 | 1076 | * |
4ccfe5e0 | 1077 | * Used for SOCK_SEQPACKET messages. |
c4307285 | 1078 | * |
4ccfe5e0 | 1079 | * Returns the number of bytes sent on success, or errno otherwise |
b97bf3fd | 1080 | */ |
4ccfe5e0 JPM |
1081 | static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, |
1082 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 1083 | { |
4ccfe5e0 JPM |
1084 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
1085 | return -EMSGSIZE; | |
b97bf3fd | 1086 | |
4ccfe5e0 | 1087 | return tipc_send_stream(iocb, sock, m, dsz); |
b97bf3fd PL |
1088 | } |
1089 | ||
dadebc00 | 1090 | /* tipc_sk_finish_conn - complete the setup of a connection |
b97bf3fd | 1091 | */ |
dadebc00 JPM |
1092 | static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port, |
1093 | u32 peer_node) | |
b97bf3fd | 1094 | { |
dadebc00 | 1095 | struct tipc_msg *msg = &port->phdr; |
b97bf3fd | 1096 | |
dadebc00 JPM |
1097 | msg_set_destnode(msg, peer_node); |
1098 | msg_set_destport(msg, peer_port); | |
1099 | msg_set_type(msg, TIPC_CONN_MSG); | |
1100 | msg_set_lookup_scope(msg, 0); | |
1101 | msg_set_hdr_sz(msg, SHORT_H_SIZE); | |
584d24b3 | 1102 | |
dadebc00 JPM |
1103 | port->probing_interval = CONN_PROBING_INTERVAL; |
1104 | port->probing_state = TIPC_CONN_OK; | |
1105 | port->connected = 1; | |
1106 | k_start_timer(&port->timer, port->probing_interval); | |
1107 | tipc_node_add_conn(peer_node, port->ref, peer_port); | |
1108 | port->max_pkt = tipc_node_get_mtu(peer_node, port->ref); | |
b97bf3fd PL |
1109 | } |
1110 | ||
1111 | /** | |
1112 | * set_orig_addr - capture sender's address for received message | |
1113 | * @m: descriptor for message info | |
1114 | * @msg: received message header | |
c4307285 | 1115 | * |
b97bf3fd PL |
1116 | * Note: Address is not captured if not requested by receiver. |
1117 | */ | |
05790c64 | 1118 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
b97bf3fd | 1119 | { |
342dfc30 | 1120 | DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); |
b97bf3fd | 1121 | |
c4307285 | 1122 | if (addr) { |
b97bf3fd PL |
1123 | addr->family = AF_TIPC; |
1124 | addr->addrtype = TIPC_ADDR_ID; | |
60085c3d | 1125 | memset(&addr->addr, 0, sizeof(addr->addr)); |
b97bf3fd PL |
1126 | addr->addr.id.ref = msg_origport(msg); |
1127 | addr->addr.id.node = msg_orignode(msg); | |
0e65967e AS |
1128 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
1129 | addr->scope = 0; /* could leave uninitialized */ | |
b97bf3fd PL |
1130 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
1131 | } | |
1132 | } | |
1133 | ||
1134 | /** | |
c4307285 | 1135 | * anc_data_recv - optionally capture ancillary data for received message |
b97bf3fd PL |
1136 | * @m: descriptor for message info |
1137 | * @msg: received message header | |
1138 | * @tport: TIPC port associated with message | |
c4307285 | 1139 | * |
b97bf3fd | 1140 | * Note: Ancillary data is not captured if not requested by receiver. |
c4307285 | 1141 | * |
b97bf3fd PL |
1142 | * Returns 0 if successful, otherwise errno |
1143 | */ | |
05790c64 | 1144 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
ae8509c4 | 1145 | struct tipc_port *tport) |
b97bf3fd PL |
1146 | { |
1147 | u32 anc_data[3]; | |
1148 | u32 err; | |
1149 | u32 dest_type; | |
3546c750 | 1150 | int has_name; |
b97bf3fd PL |
1151 | int res; |
1152 | ||
1153 | if (likely(m->msg_controllen == 0)) | |
1154 | return 0; | |
1155 | ||
1156 | /* Optionally capture errored message object(s) */ | |
b97bf3fd PL |
1157 | err = msg ? msg_errcode(msg) : 0; |
1158 | if (unlikely(err)) { | |
1159 | anc_data[0] = err; | |
1160 | anc_data[1] = msg_data_sz(msg); | |
2db9983a AS |
1161 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
1162 | if (res) | |
b97bf3fd | 1163 | return res; |
2db9983a AS |
1164 | if (anc_data[1]) { |
1165 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], | |
1166 | msg_data(msg)); | |
1167 | if (res) | |
1168 | return res; | |
1169 | } | |
b97bf3fd PL |
1170 | } |
1171 | ||
1172 | /* Optionally capture message destination object */ | |
b97bf3fd PL |
1173 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
1174 | switch (dest_type) { | |
1175 | case TIPC_NAMED_MSG: | |
3546c750 | 1176 | has_name = 1; |
b97bf3fd PL |
1177 | anc_data[0] = msg_nametype(msg); |
1178 | anc_data[1] = msg_namelower(msg); | |
1179 | anc_data[2] = msg_namelower(msg); | |
1180 | break; | |
1181 | case TIPC_MCAST_MSG: | |
3546c750 | 1182 | has_name = 1; |
b97bf3fd PL |
1183 | anc_data[0] = msg_nametype(msg); |
1184 | anc_data[1] = msg_namelower(msg); | |
1185 | anc_data[2] = msg_nameupper(msg); | |
1186 | break; | |
1187 | case TIPC_CONN_MSG: | |
3546c750 | 1188 | has_name = (tport->conn_type != 0); |
b97bf3fd PL |
1189 | anc_data[0] = tport->conn_type; |
1190 | anc_data[1] = tport->conn_instance; | |
1191 | anc_data[2] = tport->conn_instance; | |
1192 | break; | |
1193 | default: | |
3546c750 | 1194 | has_name = 0; |
b97bf3fd | 1195 | } |
2db9983a AS |
1196 | if (has_name) { |
1197 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); | |
1198 | if (res) | |
1199 | return res; | |
1200 | } | |
b97bf3fd PL |
1201 | |
1202 | return 0; | |
1203 | } | |
1204 | ||
739f5e4e JPM |
1205 | static void tipc_sk_send_ack(struct tipc_port *port, uint ack) |
1206 | { | |
1207 | struct sk_buff *buf = NULL; | |
1208 | struct tipc_msg *msg; | |
2e84c60b JPM |
1209 | u32 peer_port = tsk_peer_port(port); |
1210 | u32 dnode = tsk_peer_node(port); | |
739f5e4e JPM |
1211 | |
1212 | if (!port->connected) | |
1213 | return; | |
1214 | buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode, | |
1215 | tipc_own_addr, peer_port, port->ref, TIPC_OK); | |
1216 | if (!buf) | |
1217 | return; | |
1218 | msg = buf_msg(buf); | |
1219 | msg_set_msgcnt(msg, ack); | |
1220 | tipc_link_xmit(buf, dnode, msg_link_selector(msg)); | |
1221 | } | |
1222 | ||
85d3fc94 | 1223 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
9bbb4ecc YX |
1224 | { |
1225 | struct sock *sk = sock->sk; | |
1226 | DEFINE_WAIT(wait); | |
85d3fc94 | 1227 | long timeo = *timeop; |
9bbb4ecc YX |
1228 | int err; |
1229 | ||
1230 | for (;;) { | |
1231 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
fe8e4649 | 1232 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
9bbb4ecc YX |
1233 | if (sock->state == SS_DISCONNECTING) { |
1234 | err = -ENOTCONN; | |
1235 | break; | |
1236 | } | |
1237 | release_sock(sk); | |
1238 | timeo = schedule_timeout(timeo); | |
1239 | lock_sock(sk); | |
1240 | } | |
1241 | err = 0; | |
1242 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1243 | break; | |
1244 | err = sock_intr_errno(timeo); | |
1245 | if (signal_pending(current)) | |
1246 | break; | |
1247 | err = -EAGAIN; | |
1248 | if (!timeo) | |
1249 | break; | |
1250 | } | |
1251 | finish_wait(sk_sleep(sk), &wait); | |
85d3fc94 | 1252 | *timeop = timeo; |
9bbb4ecc YX |
1253 | return err; |
1254 | } | |
1255 | ||
c4307285 | 1256 | /** |
247f0f3c | 1257 | * tipc_recvmsg - receive packet-oriented message |
b97bf3fd PL |
1258 | * @iocb: (unused) |
1259 | * @m: descriptor for message info | |
1260 | * @buf_len: total size of user buffer area | |
1261 | * @flags: receive flags | |
c4307285 | 1262 | * |
b97bf3fd PL |
1263 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
1264 | * If the complete message doesn't fit in user area, truncate it. | |
1265 | * | |
1266 | * Returns size of returned message data, errno otherwise | |
1267 | */ | |
247f0f3c YX |
1268 | static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, |
1269 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1270 | { |
0c3141e9 | 1271 | struct sock *sk = sock->sk; |
58ed9442 JPM |
1272 | struct tipc_sock *tsk = tipc_sk(sk); |
1273 | struct tipc_port *port = &tsk->port; | |
b97bf3fd PL |
1274 | struct sk_buff *buf; |
1275 | struct tipc_msg *msg; | |
9bbb4ecc | 1276 | long timeo; |
b97bf3fd PL |
1277 | unsigned int sz; |
1278 | u32 err; | |
1279 | int res; | |
1280 | ||
0c3141e9 | 1281 | /* Catch invalid receive requests */ |
b97bf3fd PL |
1282 | if (unlikely(!buf_len)) |
1283 | return -EINVAL; | |
1284 | ||
0c3141e9 | 1285 | lock_sock(sk); |
b97bf3fd | 1286 | |
0c3141e9 AS |
1287 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
1288 | res = -ENOTCONN; | |
b97bf3fd PL |
1289 | goto exit; |
1290 | } | |
1291 | ||
9bbb4ecc | 1292 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
0c3141e9 | 1293 | restart: |
b97bf3fd | 1294 | |
0c3141e9 | 1295 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1296 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1297 | if (res) |
1298 | goto exit; | |
b97bf3fd | 1299 | |
0c3141e9 | 1300 | /* Look at first message in receive queue */ |
0c3141e9 | 1301 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1302 | msg = buf_msg(buf); |
1303 | sz = msg_data_sz(msg); | |
1304 | err = msg_errcode(msg); | |
1305 | ||
b97bf3fd | 1306 | /* Discard an empty non-errored message & try again */ |
b97bf3fd | 1307 | if ((!sz) && (!err)) { |
2e84c60b | 1308 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1309 | goto restart; |
1310 | } | |
1311 | ||
1312 | /* Capture sender's address (optional) */ | |
b97bf3fd PL |
1313 | set_orig_addr(m, msg); |
1314 | ||
1315 | /* Capture ancillary data (optional) */ | |
58ed9442 | 1316 | res = anc_data_recv(m, msg, port); |
0c3141e9 | 1317 | if (res) |
b97bf3fd PL |
1318 | goto exit; |
1319 | ||
1320 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd PL |
1321 | if (!err) { |
1322 | if (unlikely(buf_len < sz)) { | |
1323 | sz = buf_len; | |
1324 | m->msg_flags |= MSG_TRUNC; | |
1325 | } | |
0232fd0a AS |
1326 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg), |
1327 | m->msg_iov, sz); | |
1328 | if (res) | |
b97bf3fd | 1329 | goto exit; |
b97bf3fd PL |
1330 | res = sz; |
1331 | } else { | |
1332 | if ((sock->state == SS_READY) || | |
1333 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) | |
1334 | res = 0; | |
1335 | else | |
1336 | res = -ECONNRESET; | |
1337 | } | |
1338 | ||
1339 | /* Consume received message (optional) */ | |
b97bf3fd | 1340 | if (likely(!(flags & MSG_PEEK))) { |
99009806 | 1341 | if ((sock->state != SS_READY) && |
60120526 | 1342 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
739f5e4e | 1343 | tipc_sk_send_ack(port, tsk->rcv_unacked); |
60120526 JPM |
1344 | tsk->rcv_unacked = 0; |
1345 | } | |
2e84c60b | 1346 | tsk_advance_rx_queue(sk); |
c4307285 | 1347 | } |
b97bf3fd | 1348 | exit: |
0c3141e9 | 1349 | release_sock(sk); |
b97bf3fd PL |
1350 | return res; |
1351 | } | |
1352 | ||
c4307285 | 1353 | /** |
247f0f3c | 1354 | * tipc_recv_stream - receive stream-oriented data |
b97bf3fd PL |
1355 | * @iocb: (unused) |
1356 | * @m: descriptor for message info | |
1357 | * @buf_len: total size of user buffer area | |
1358 | * @flags: receive flags | |
c4307285 YH |
1359 | * |
1360 | * Used for SOCK_STREAM messages only. If not enough data is available | |
b97bf3fd PL |
1361 | * will optionally wait for more; never truncates data. |
1362 | * | |
1363 | * Returns size of returned message data, errno otherwise | |
1364 | */ | |
247f0f3c YX |
1365 | static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, |
1366 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1367 | { |
0c3141e9 | 1368 | struct sock *sk = sock->sk; |
58ed9442 JPM |
1369 | struct tipc_sock *tsk = tipc_sk(sk); |
1370 | struct tipc_port *port = &tsk->port; | |
b97bf3fd PL |
1371 | struct sk_buff *buf; |
1372 | struct tipc_msg *msg; | |
9bbb4ecc | 1373 | long timeo; |
b97bf3fd | 1374 | unsigned int sz; |
3720d40b | 1375 | int sz_to_copy, target, needed; |
b97bf3fd | 1376 | int sz_copied = 0; |
b97bf3fd | 1377 | u32 err; |
0c3141e9 | 1378 | int res = 0; |
b97bf3fd | 1379 | |
0c3141e9 | 1380 | /* Catch invalid receive attempts */ |
b97bf3fd PL |
1381 | if (unlikely(!buf_len)) |
1382 | return -EINVAL; | |
1383 | ||
0c3141e9 | 1384 | lock_sock(sk); |
b97bf3fd | 1385 | |
9bbb4ecc | 1386 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
0c3141e9 | 1387 | res = -ENOTCONN; |
b97bf3fd PL |
1388 | goto exit; |
1389 | } | |
1390 | ||
3720d40b | 1391 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
9bbb4ecc | 1392 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
b97bf3fd | 1393 | |
617d3c7a | 1394 | restart: |
0c3141e9 | 1395 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1396 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1397 | if (res) |
1398 | goto exit; | |
b97bf3fd | 1399 | |
0c3141e9 | 1400 | /* Look at first message in receive queue */ |
0c3141e9 | 1401 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1402 | msg = buf_msg(buf); |
1403 | sz = msg_data_sz(msg); | |
1404 | err = msg_errcode(msg); | |
1405 | ||
1406 | /* Discard an empty non-errored message & try again */ | |
b97bf3fd | 1407 | if ((!sz) && (!err)) { |
2e84c60b | 1408 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1409 | goto restart; |
1410 | } | |
1411 | ||
1412 | /* Optionally capture sender's address & ancillary data of first msg */ | |
b97bf3fd PL |
1413 | if (sz_copied == 0) { |
1414 | set_orig_addr(m, msg); | |
58ed9442 | 1415 | res = anc_data_recv(m, msg, port); |
0c3141e9 | 1416 | if (res) |
b97bf3fd PL |
1417 | goto exit; |
1418 | } | |
1419 | ||
1420 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd | 1421 | if (!err) { |
0232fd0a | 1422 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
b97bf3fd | 1423 | |
0232fd0a | 1424 | sz -= offset; |
b97bf3fd PL |
1425 | needed = (buf_len - sz_copied); |
1426 | sz_to_copy = (sz <= needed) ? sz : needed; | |
0232fd0a AS |
1427 | |
1428 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset, | |
1429 | m->msg_iov, sz_to_copy); | |
1430 | if (res) | |
b97bf3fd | 1431 | goto exit; |
0232fd0a | 1432 | |
b97bf3fd PL |
1433 | sz_copied += sz_to_copy; |
1434 | ||
1435 | if (sz_to_copy < sz) { | |
1436 | if (!(flags & MSG_PEEK)) | |
0232fd0a AS |
1437 | TIPC_SKB_CB(buf)->handle = |
1438 | (void *)(unsigned long)(offset + sz_to_copy); | |
b97bf3fd PL |
1439 | goto exit; |
1440 | } | |
b97bf3fd PL |
1441 | } else { |
1442 | if (sz_copied != 0) | |
1443 | goto exit; /* can't add error msg to valid data */ | |
1444 | ||
1445 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) | |
1446 | res = 0; | |
1447 | else | |
1448 | res = -ECONNRESET; | |
1449 | } | |
1450 | ||
1451 | /* Consume received message (optional) */ | |
b97bf3fd | 1452 | if (likely(!(flags & MSG_PEEK))) { |
60120526 | 1453 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
739f5e4e | 1454 | tipc_sk_send_ack(port, tsk->rcv_unacked); |
60120526 JPM |
1455 | tsk->rcv_unacked = 0; |
1456 | } | |
2e84c60b | 1457 | tsk_advance_rx_queue(sk); |
c4307285 | 1458 | } |
b97bf3fd PL |
1459 | |
1460 | /* Loop around if more data is required */ | |
f64f9e71 JP |
1461 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
1462 | (!skb_queue_empty(&sk->sk_receive_queue) || | |
3720d40b | 1463 | (sz_copied < target)) && /* and more is ready or required */ |
f64f9e71 JP |
1464 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
1465 | (!err)) /* and haven't reached a FIN */ | |
b97bf3fd PL |
1466 | goto restart; |
1467 | ||
1468 | exit: | |
0c3141e9 | 1469 | release_sock(sk); |
a3b0a5a9 | 1470 | return sz_copied ? sz_copied : res; |
b97bf3fd PL |
1471 | } |
1472 | ||
f288bef4 YX |
1473 | /** |
1474 | * tipc_write_space - wake up thread if port congestion is released | |
1475 | * @sk: socket | |
1476 | */ | |
1477 | static void tipc_write_space(struct sock *sk) | |
1478 | { | |
1479 | struct socket_wq *wq; | |
1480 | ||
1481 | rcu_read_lock(); | |
1482 | wq = rcu_dereference(sk->sk_wq); | |
1483 | if (wq_has_sleeper(wq)) | |
1484 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | | |
1485 | POLLWRNORM | POLLWRBAND); | |
1486 | rcu_read_unlock(); | |
1487 | } | |
1488 | ||
1489 | /** | |
1490 | * tipc_data_ready - wake up threads to indicate messages have been received | |
1491 | * @sk: socket | |
1492 | * @len: the length of messages | |
1493 | */ | |
676d2369 | 1494 | static void tipc_data_ready(struct sock *sk) |
f288bef4 YX |
1495 | { |
1496 | struct socket_wq *wq; | |
1497 | ||
1498 | rcu_read_lock(); | |
1499 | wq = rcu_dereference(sk->sk_wq); | |
1500 | if (wq_has_sleeper(wq)) | |
1501 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | | |
1502 | POLLRDNORM | POLLRDBAND); | |
1503 | rcu_read_unlock(); | |
1504 | } | |
1505 | ||
7e6c131e YX |
1506 | /** |
1507 | * filter_connect - Handle all incoming messages for a connection-based socket | |
58ed9442 | 1508 | * @tsk: TIPC socket |
7e6c131e YX |
1509 | * @msg: message |
1510 | * | |
e4de5fab | 1511 | * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise |
7e6c131e | 1512 | */ |
e4de5fab | 1513 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
7e6c131e | 1514 | { |
58ed9442 JPM |
1515 | struct sock *sk = &tsk->sk; |
1516 | struct tipc_port *port = &tsk->port; | |
8826cde6 | 1517 | struct socket *sock = sk->sk_socket; |
7e6c131e | 1518 | struct tipc_msg *msg = buf_msg(*buf); |
8826cde6 | 1519 | |
e4de5fab | 1520 | int retval = -TIPC_ERR_NO_PORT; |
7e6c131e YX |
1521 | |
1522 | if (msg_mcast(msg)) | |
1523 | return retval; | |
1524 | ||
1525 | switch ((int)sock->state) { | |
1526 | case SS_CONNECTED: | |
1527 | /* Accept only connection-based messages sent by peer */ | |
2e84c60b | 1528 | if (tsk_peer_msg(tsk, msg)) { |
7e6c131e YX |
1529 | if (unlikely(msg_errcode(msg))) { |
1530 | sock->state = SS_DISCONNECTING; | |
dadebc00 JPM |
1531 | port->connected = 0; |
1532 | /* let timer expire on it's own */ | |
2e84c60b | 1533 | tipc_node_remove_conn(tsk_peer_node(port), |
dadebc00 | 1534 | port->ref); |
7e6c131e YX |
1535 | } |
1536 | retval = TIPC_OK; | |
1537 | } | |
1538 | break; | |
1539 | case SS_CONNECTING: | |
1540 | /* Accept only ACK or NACK message */ | |
dadebc00 JPM |
1541 | |
1542 | if (unlikely(!msg_connected(msg))) | |
1543 | break; | |
1544 | ||
584d24b3 YX |
1545 | if (unlikely(msg_errcode(msg))) { |
1546 | sock->state = SS_DISCONNECTING; | |
2c8d8518 | 1547 | sk->sk_err = ECONNREFUSED; |
584d24b3 YX |
1548 | retval = TIPC_OK; |
1549 | break; | |
1550 | } | |
1551 | ||
dadebc00 | 1552 | if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) { |
584d24b3 | 1553 | sock->state = SS_DISCONNECTING; |
dadebc00 | 1554 | sk->sk_err = EINVAL; |
7e6c131e | 1555 | retval = TIPC_OK; |
584d24b3 YX |
1556 | break; |
1557 | } | |
1558 | ||
dadebc00 JPM |
1559 | tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg)); |
1560 | msg_set_importance(&port->phdr, msg_importance(msg)); | |
1561 | sock->state = SS_CONNECTED; | |
1562 | ||
584d24b3 YX |
1563 | /* If an incoming message is an 'ACK-', it should be |
1564 | * discarded here because it doesn't contain useful | |
1565 | * data. In addition, we should try to wake up | |
1566 | * connect() routine if sleeping. | |
1567 | */ | |
1568 | if (msg_data_sz(msg) == 0) { | |
1569 | kfree_skb(*buf); | |
1570 | *buf = NULL; | |
1571 | if (waitqueue_active(sk_sleep(sk))) | |
1572 | wake_up_interruptible(sk_sleep(sk)); | |
1573 | } | |
1574 | retval = TIPC_OK; | |
7e6c131e YX |
1575 | break; |
1576 | case SS_LISTENING: | |
1577 | case SS_UNCONNECTED: | |
1578 | /* Accept only SYN message */ | |
1579 | if (!msg_connected(msg) && !(msg_errcode(msg))) | |
1580 | retval = TIPC_OK; | |
1581 | break; | |
1582 | case SS_DISCONNECTING: | |
1583 | break; | |
1584 | default: | |
1585 | pr_err("Unknown socket state %u\n", sock->state); | |
1586 | } | |
1587 | return retval; | |
1588 | } | |
1589 | ||
aba79f33 YX |
1590 | /** |
1591 | * rcvbuf_limit - get proper overload limit of socket receive queue | |
1592 | * @sk: socket | |
1593 | * @buf: message | |
1594 | * | |
1595 | * For all connection oriented messages, irrespective of importance, | |
1596 | * the default overload value (i.e. 67MB) is set as limit. | |
1597 | * | |
1598 | * For all connectionless messages, by default new queue limits are | |
1599 | * as belows: | |
1600 | * | |
cc79dd1b YX |
1601 | * TIPC_LOW_IMPORTANCE (4 MB) |
1602 | * TIPC_MEDIUM_IMPORTANCE (8 MB) | |
1603 | * TIPC_HIGH_IMPORTANCE (16 MB) | |
1604 | * TIPC_CRITICAL_IMPORTANCE (32 MB) | |
aba79f33 YX |
1605 | * |
1606 | * Returns overload limit according to corresponding message importance | |
1607 | */ | |
1608 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf) | |
1609 | { | |
1610 | struct tipc_msg *msg = buf_msg(buf); | |
aba79f33 YX |
1611 | |
1612 | if (msg_connected(msg)) | |
0cee6bbe | 1613 | return sysctl_tipc_rmem[2]; |
1614 | ||
1615 | return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE << | |
1616 | msg_importance(msg); | |
aba79f33 YX |
1617 | } |
1618 | ||
c4307285 | 1619 | /** |
0c3141e9 AS |
1620 | * filter_rcv - validate incoming message |
1621 | * @sk: socket | |
b97bf3fd | 1622 | * @buf: message |
c4307285 | 1623 | * |
0c3141e9 AS |
1624 | * Enqueues message on receive queue if acceptable; optionally handles |
1625 | * disconnect indication for a connected socket. | |
1626 | * | |
1627 | * Called with socket lock already taken; port lock may also be taken. | |
c4307285 | 1628 | * |
e4de5fab | 1629 | * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message |
ac0074ee | 1630 | * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded |
b97bf3fd | 1631 | */ |
e4de5fab | 1632 | static int filter_rcv(struct sock *sk, struct sk_buff *buf) |
b97bf3fd | 1633 | { |
0c3141e9 | 1634 | struct socket *sock = sk->sk_socket; |
58ed9442 | 1635 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd | 1636 | struct tipc_msg *msg = buf_msg(buf); |
aba79f33 | 1637 | unsigned int limit = rcvbuf_limit(sk, buf); |
ac0074ee | 1638 | u32 onode; |
e4de5fab | 1639 | int rc = TIPC_OK; |
b97bf3fd | 1640 | |
ac0074ee JPM |
1641 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
1642 | return tipc_sk_proto_rcv(tsk, &onode, buf); | |
ec8a2e56 | 1643 | |
50100a5e JPM |
1644 | if (unlikely(msg_user(msg) == SOCK_WAKEUP)) { |
1645 | kfree_skb(buf); | |
1646 | tsk->link_cong = 0; | |
1647 | sk->sk_write_space(sk); | |
1648 | return TIPC_OK; | |
1649 | } | |
1650 | ||
b97bf3fd | 1651 | /* Reject message if it is wrong sort of message for socket */ |
aad58547 | 1652 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
e4de5fab | 1653 | return -TIPC_ERR_NO_PORT; |
0c3141e9 | 1654 | |
b97bf3fd | 1655 | if (sock->state == SS_READY) { |
b29f1428 | 1656 | if (msg_connected(msg)) |
e4de5fab | 1657 | return -TIPC_ERR_NO_PORT; |
b97bf3fd | 1658 | } else { |
e4de5fab JPM |
1659 | rc = filter_connect(tsk, &buf); |
1660 | if (rc != TIPC_OK || buf == NULL) | |
1661 | return rc; | |
b97bf3fd PL |
1662 | } |
1663 | ||
1664 | /* Reject message if there isn't room to queue it */ | |
aba79f33 | 1665 | if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) |
e4de5fab | 1666 | return -TIPC_ERR_OVERLOAD; |
b97bf3fd | 1667 | |
aba79f33 | 1668 | /* Enqueue message */ |
40682432 | 1669 | TIPC_SKB_CB(buf)->handle = NULL; |
0c3141e9 | 1670 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
aba79f33 | 1671 | skb_set_owner_r(buf, sk); |
0c3141e9 | 1672 | |
676d2369 | 1673 | sk->sk_data_ready(sk); |
0c3141e9 AS |
1674 | return TIPC_OK; |
1675 | } | |
b97bf3fd | 1676 | |
0c3141e9 | 1677 | /** |
4f4482dc | 1678 | * tipc_backlog_rcv - handle incoming message from backlog queue |
0c3141e9 AS |
1679 | * @sk: socket |
1680 | * @buf: message | |
1681 | * | |
1682 | * Caller must hold socket lock, but not port lock. | |
1683 | * | |
1684 | * Returns 0 | |
1685 | */ | |
4f4482dc | 1686 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) |
0c3141e9 | 1687 | { |
e4de5fab | 1688 | int rc; |
8db1bae3 | 1689 | u32 onode; |
4f4482dc | 1690 | struct tipc_sock *tsk = tipc_sk(sk); |
02c00c2a | 1691 | uint truesize = buf->truesize; |
0c3141e9 | 1692 | |
e4de5fab | 1693 | rc = filter_rcv(sk, buf); |
4f4482dc | 1694 | |
ac0074ee JPM |
1695 | if (likely(!rc)) { |
1696 | if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) | |
1697 | atomic_add(truesize, &tsk->dupl_rcvcnt); | |
1698 | return 0; | |
1699 | } | |
1700 | ||
1701 | if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc)) | |
1702 | return 0; | |
1703 | ||
9fbfb8b1 | 1704 | tipc_link_xmit(buf, onode, 0); |
4f4482dc | 1705 | |
0c3141e9 AS |
1706 | return 0; |
1707 | } | |
1708 | ||
1709 | /** | |
24be34b5 | 1710 | * tipc_sk_rcv - handle incoming message |
9816f061 JPM |
1711 | * @buf: buffer containing arriving message |
1712 | * Consumes buffer | |
1713 | * Returns 0 if success, or errno: -EHOSTUNREACH | |
0c3141e9 | 1714 | */ |
9816f061 | 1715 | int tipc_sk_rcv(struct sk_buff *buf) |
0c3141e9 | 1716 | { |
9816f061 JPM |
1717 | struct tipc_sock *tsk; |
1718 | struct tipc_port *port; | |
1719 | struct sock *sk; | |
1720 | u32 dport = msg_destport(buf_msg(buf)); | |
e4de5fab | 1721 | int rc = TIPC_OK; |
4f4482dc | 1722 | uint limit; |
8db1bae3 | 1723 | u32 dnode; |
9816f061 | 1724 | |
5a379074 | 1725 | /* Validate destination and message */ |
6c9808ce | 1726 | tsk = tipc_sk_get(dport); |
9b50fd08 | 1727 | if (unlikely(!tsk)) { |
5a379074 | 1728 | rc = tipc_msg_eval(buf, &dnode); |
9816f061 JPM |
1729 | goto exit; |
1730 | } | |
9b50fd08 | 1731 | port = &tsk->port; |
9816f061 JPM |
1732 | sk = &tsk->sk; |
1733 | ||
1734 | /* Queue message */ | |
0c3141e9 | 1735 | bh_lock_sock(sk); |
9816f061 | 1736 | |
0c3141e9 | 1737 | if (!sock_owned_by_user(sk)) { |
e4de5fab | 1738 | rc = filter_rcv(sk, buf); |
0c3141e9 | 1739 | } else { |
4f4482dc JPM |
1740 | if (sk->sk_backlog.len == 0) |
1741 | atomic_set(&tsk->dupl_rcvcnt, 0); | |
1742 | limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt); | |
1743 | if (sk_add_backlog(sk, buf, limit)) | |
e4de5fab | 1744 | rc = -TIPC_ERR_OVERLOAD; |
0c3141e9 AS |
1745 | } |
1746 | bh_unlock_sock(sk); | |
6c9808ce | 1747 | tipc_sk_put(tsk); |
e4de5fab | 1748 | if (likely(!rc)) |
9816f061 JPM |
1749 | return 0; |
1750 | exit: | |
5a379074 | 1751 | if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc)) |
8db1bae3 | 1752 | return -EHOSTUNREACH; |
5a379074 | 1753 | |
9fbfb8b1 | 1754 | tipc_link_xmit(buf, dnode, 0); |
5a379074 | 1755 | return (rc < 0) ? -EHOSTUNREACH : 0; |
b97bf3fd PL |
1756 | } |
1757 | ||
78eb3a53 YX |
1758 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
1759 | { | |
1760 | struct sock *sk = sock->sk; | |
1761 | DEFINE_WAIT(wait); | |
1762 | int done; | |
1763 | ||
1764 | do { | |
1765 | int err = sock_error(sk); | |
1766 | if (err) | |
1767 | return err; | |
1768 | if (!*timeo_p) | |
1769 | return -ETIMEDOUT; | |
1770 | if (signal_pending(current)) | |
1771 | return sock_intr_errno(*timeo_p); | |
1772 | ||
1773 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1774 | done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); | |
1775 | finish_wait(sk_sleep(sk), &wait); | |
1776 | } while (!done); | |
1777 | return 0; | |
1778 | } | |
1779 | ||
b97bf3fd | 1780 | /** |
247f0f3c | 1781 | * tipc_connect - establish a connection to another TIPC port |
b97bf3fd PL |
1782 | * @sock: socket structure |
1783 | * @dest: socket address for destination port | |
1784 | * @destlen: size of socket address data structure | |
0c3141e9 | 1785 | * @flags: file-related flags associated with socket |
b97bf3fd PL |
1786 | * |
1787 | * Returns 0 on success, errno otherwise | |
1788 | */ | |
247f0f3c YX |
1789 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
1790 | int destlen, int flags) | |
b97bf3fd | 1791 | { |
0c3141e9 | 1792 | struct sock *sk = sock->sk; |
b89741a0 AS |
1793 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
1794 | struct msghdr m = {NULL,}; | |
78eb3a53 YX |
1795 | long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; |
1796 | socket_state previous; | |
b89741a0 AS |
1797 | int res; |
1798 | ||
0c3141e9 AS |
1799 | lock_sock(sk); |
1800 | ||
b89741a0 | 1801 | /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ |
0c3141e9 AS |
1802 | if (sock->state == SS_READY) { |
1803 | res = -EOPNOTSUPP; | |
1804 | goto exit; | |
1805 | } | |
b89741a0 | 1806 | |
b89741a0 AS |
1807 | /* |
1808 | * Reject connection attempt using multicast address | |
1809 | * | |
1810 | * Note: send_msg() validates the rest of the address fields, | |
1811 | * so there's no need to do it here | |
1812 | */ | |
0c3141e9 AS |
1813 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
1814 | res = -EINVAL; | |
1815 | goto exit; | |
1816 | } | |
1817 | ||
78eb3a53 | 1818 | previous = sock->state; |
584d24b3 YX |
1819 | switch (sock->state) { |
1820 | case SS_UNCONNECTED: | |
1821 | /* Send a 'SYN-' to destination */ | |
1822 | m.msg_name = dest; | |
1823 | m.msg_namelen = destlen; | |
1824 | ||
1825 | /* If connect is in non-blocking case, set MSG_DONTWAIT to | |
1826 | * indicate send_msg() is never blocked. | |
1827 | */ | |
1828 | if (!timeout) | |
1829 | m.msg_flags = MSG_DONTWAIT; | |
1830 | ||
247f0f3c | 1831 | res = tipc_sendmsg(NULL, sock, &m, 0); |
584d24b3 YX |
1832 | if ((res < 0) && (res != -EWOULDBLOCK)) |
1833 | goto exit; | |
1834 | ||
1835 | /* Just entered SS_CONNECTING state; the only | |
1836 | * difference is that return value in non-blocking | |
1837 | * case is EINPROGRESS, rather than EALREADY. | |
1838 | */ | |
1839 | res = -EINPROGRESS; | |
584d24b3 | 1840 | case SS_CONNECTING: |
78eb3a53 YX |
1841 | if (previous == SS_CONNECTING) |
1842 | res = -EALREADY; | |
1843 | if (!timeout) | |
1844 | goto exit; | |
1845 | timeout = msecs_to_jiffies(timeout); | |
1846 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ | |
1847 | res = tipc_wait_for_connect(sock, &timeout); | |
584d24b3 YX |
1848 | break; |
1849 | case SS_CONNECTED: | |
1850 | res = -EISCONN; | |
1851 | break; | |
1852 | default: | |
1853 | res = -EINVAL; | |
78eb3a53 | 1854 | break; |
b89741a0 | 1855 | } |
0c3141e9 AS |
1856 | exit: |
1857 | release_sock(sk); | |
b89741a0 | 1858 | return res; |
b97bf3fd PL |
1859 | } |
1860 | ||
c4307285 | 1861 | /** |
247f0f3c | 1862 | * tipc_listen - allow socket to listen for incoming connections |
b97bf3fd PL |
1863 | * @sock: socket structure |
1864 | * @len: (unused) | |
c4307285 | 1865 | * |
b97bf3fd PL |
1866 | * Returns 0 on success, errno otherwise |
1867 | */ | |
247f0f3c | 1868 | static int tipc_listen(struct socket *sock, int len) |
b97bf3fd | 1869 | { |
0c3141e9 AS |
1870 | struct sock *sk = sock->sk; |
1871 | int res; | |
1872 | ||
1873 | lock_sock(sk); | |
b97bf3fd | 1874 | |
245f3d34 | 1875 | if (sock->state != SS_UNCONNECTED) |
0c3141e9 AS |
1876 | res = -EINVAL; |
1877 | else { | |
1878 | sock->state = SS_LISTENING; | |
1879 | res = 0; | |
1880 | } | |
1881 | ||
1882 | release_sock(sk); | |
1883 | return res; | |
b97bf3fd PL |
1884 | } |
1885 | ||
6398e23c YX |
1886 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
1887 | { | |
1888 | struct sock *sk = sock->sk; | |
1889 | DEFINE_WAIT(wait); | |
1890 | int err; | |
1891 | ||
1892 | /* True wake-one mechanism for incoming connections: only | |
1893 | * one process gets woken up, not the 'whole herd'. | |
1894 | * Since we do not 'race & poll' for established sockets | |
1895 | * anymore, the common case will execute the loop only once. | |
1896 | */ | |
1897 | for (;;) { | |
1898 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, | |
1899 | TASK_INTERRUPTIBLE); | |
fe8e4649 | 1900 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
6398e23c YX |
1901 | release_sock(sk); |
1902 | timeo = schedule_timeout(timeo); | |
1903 | lock_sock(sk); | |
1904 | } | |
1905 | err = 0; | |
1906 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1907 | break; | |
1908 | err = -EINVAL; | |
1909 | if (sock->state != SS_LISTENING) | |
1910 | break; | |
1911 | err = sock_intr_errno(timeo); | |
1912 | if (signal_pending(current)) | |
1913 | break; | |
1914 | err = -EAGAIN; | |
1915 | if (!timeo) | |
1916 | break; | |
1917 | } | |
1918 | finish_wait(sk_sleep(sk), &wait); | |
1919 | return err; | |
1920 | } | |
1921 | ||
c4307285 | 1922 | /** |
247f0f3c | 1923 | * tipc_accept - wait for connection request |
b97bf3fd PL |
1924 | * @sock: listening socket |
1925 | * @newsock: new socket that is to be connected | |
1926 | * @flags: file-related flags associated with socket | |
c4307285 | 1927 | * |
b97bf3fd PL |
1928 | * Returns 0 on success, errno otherwise |
1929 | */ | |
247f0f3c | 1930 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) |
b97bf3fd | 1931 | { |
0fef8f20 | 1932 | struct sock *new_sk, *sk = sock->sk; |
b97bf3fd | 1933 | struct sk_buff *buf; |
8826cde6 | 1934 | struct tipc_port *new_port; |
0fef8f20 | 1935 | struct tipc_msg *msg; |
6398e23c | 1936 | long timeo; |
0c3141e9 | 1937 | int res; |
b97bf3fd | 1938 | |
0c3141e9 | 1939 | lock_sock(sk); |
b97bf3fd | 1940 | |
0c3141e9 AS |
1941 | if (sock->state != SS_LISTENING) { |
1942 | res = -EINVAL; | |
b97bf3fd PL |
1943 | goto exit; |
1944 | } | |
6398e23c YX |
1945 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
1946 | res = tipc_wait_for_accept(sock, timeo); | |
1947 | if (res) | |
1948 | goto exit; | |
0c3141e9 AS |
1949 | |
1950 | buf = skb_peek(&sk->sk_receive_queue); | |
1951 | ||
c5fa7b3c | 1952 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); |
0fef8f20 PG |
1953 | if (res) |
1954 | goto exit; | |
b97bf3fd | 1955 | |
0fef8f20 | 1956 | new_sk = new_sock->sk; |
58ed9442 | 1957 | new_port = &tipc_sk(new_sk)->port; |
0fef8f20 | 1958 | msg = buf_msg(buf); |
b97bf3fd | 1959 | |
0fef8f20 PG |
1960 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
1961 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); | |
1962 | ||
1963 | /* | |
1964 | * Reject any stray messages received by new socket | |
1965 | * before the socket lock was taken (very, very unlikely) | |
1966 | */ | |
2e84c60b | 1967 | tsk_rej_rx_queue(new_sk); |
0fef8f20 PG |
1968 | |
1969 | /* Connect new socket to it's peer */ | |
dadebc00 | 1970 | tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg)); |
0fef8f20 PG |
1971 | new_sock->state = SS_CONNECTED; |
1972 | ||
2e84c60b | 1973 | tsk_set_importance(new_port, msg_importance(msg)); |
0fef8f20 | 1974 | if (msg_named(msg)) { |
8826cde6 JPM |
1975 | new_port->conn_type = msg_nametype(msg); |
1976 | new_port->conn_instance = msg_nameinst(msg); | |
b97bf3fd | 1977 | } |
0fef8f20 PG |
1978 | |
1979 | /* | |
1980 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. | |
1981 | * Respond to 'SYN+' by queuing it on new socket. | |
1982 | */ | |
1983 | if (!msg_data_sz(msg)) { | |
1984 | struct msghdr m = {NULL,}; | |
1985 | ||
2e84c60b | 1986 | tsk_advance_rx_queue(sk); |
247f0f3c | 1987 | tipc_send_packet(NULL, new_sock, &m, 0); |
0fef8f20 PG |
1988 | } else { |
1989 | __skb_dequeue(&sk->sk_receive_queue); | |
1990 | __skb_queue_head(&new_sk->sk_receive_queue, buf); | |
aba79f33 | 1991 | skb_set_owner_r(buf, new_sk); |
0fef8f20 PG |
1992 | } |
1993 | release_sock(new_sk); | |
b97bf3fd | 1994 | exit: |
0c3141e9 | 1995 | release_sock(sk); |
b97bf3fd PL |
1996 | return res; |
1997 | } | |
1998 | ||
1999 | /** | |
247f0f3c | 2000 | * tipc_shutdown - shutdown socket connection |
b97bf3fd | 2001 | * @sock: socket structure |
e247a8f5 | 2002 | * @how: direction to close (must be SHUT_RDWR) |
b97bf3fd PL |
2003 | * |
2004 | * Terminates connection (if necessary), then purges socket's receive queue. | |
c4307285 | 2005 | * |
b97bf3fd PL |
2006 | * Returns 0 on success, errno otherwise |
2007 | */ | |
247f0f3c | 2008 | static int tipc_shutdown(struct socket *sock, int how) |
b97bf3fd | 2009 | { |
0c3141e9 | 2010 | struct sock *sk = sock->sk; |
58ed9442 JPM |
2011 | struct tipc_sock *tsk = tipc_sk(sk); |
2012 | struct tipc_port *port = &tsk->port; | |
b97bf3fd | 2013 | struct sk_buff *buf; |
80e44c22 | 2014 | u32 dnode; |
b97bf3fd PL |
2015 | int res; |
2016 | ||
e247a8f5 AS |
2017 | if (how != SHUT_RDWR) |
2018 | return -EINVAL; | |
b97bf3fd | 2019 | |
0c3141e9 | 2020 | lock_sock(sk); |
b97bf3fd PL |
2021 | |
2022 | switch (sock->state) { | |
0c3141e9 | 2023 | case SS_CONNECTING: |
b97bf3fd PL |
2024 | case SS_CONNECTED: |
2025 | ||
b97bf3fd | 2026 | restart: |
617d3c7a | 2027 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
0c3141e9 AS |
2028 | buf = __skb_dequeue(&sk->sk_receive_queue); |
2029 | if (buf) { | |
40682432 | 2030 | if (TIPC_SKB_CB(buf)->handle != NULL) { |
5f6d9123 | 2031 | kfree_skb(buf); |
b97bf3fd PL |
2032 | goto restart; |
2033 | } | |
80e44c22 JPM |
2034 | if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN)) |
2035 | tipc_link_xmit(buf, dnode, port->ref); | |
dadebc00 | 2036 | tipc_node_remove_conn(dnode, port->ref); |
0c3141e9 | 2037 | } else { |
2e84c60b | 2038 | dnode = tsk_peer_node(port); |
80e44c22 JPM |
2039 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
2040 | TIPC_CONN_MSG, SHORT_H_SIZE, | |
2041 | 0, dnode, tipc_own_addr, | |
2e84c60b | 2042 | tsk_peer_port(port), |
80e44c22 JPM |
2043 | port->ref, TIPC_CONN_SHUTDOWN); |
2044 | tipc_link_xmit(buf, dnode, port->ref); | |
b97bf3fd | 2045 | } |
dadebc00 | 2046 | port->connected = 0; |
0c3141e9 | 2047 | sock->state = SS_DISCONNECTING; |
dadebc00 | 2048 | tipc_node_remove_conn(dnode, port->ref); |
b97bf3fd PL |
2049 | /* fall through */ |
2050 | ||
2051 | case SS_DISCONNECTING: | |
2052 | ||
75031151 | 2053 | /* Discard any unreceived messages */ |
57467e56 | 2054 | __skb_queue_purge(&sk->sk_receive_queue); |
75031151 YX |
2055 | |
2056 | /* Wake up anyone sleeping in poll */ | |
2057 | sk->sk_state_change(sk); | |
b97bf3fd PL |
2058 | res = 0; |
2059 | break; | |
2060 | ||
2061 | default: | |
2062 | res = -ENOTCONN; | |
2063 | } | |
2064 | ||
0c3141e9 | 2065 | release_sock(sk); |
b97bf3fd PL |
2066 | return res; |
2067 | } | |
2068 | ||
57289015 JPM |
2069 | static void tipc_sk_timeout(unsigned long ref) |
2070 | { | |
6c9808ce | 2071 | struct tipc_sock *tsk; |
9b50fd08 | 2072 | struct tipc_port *port; |
57289015 JPM |
2073 | struct sock *sk; |
2074 | struct sk_buff *buf = NULL; | |
57289015 JPM |
2075 | u32 peer_port, peer_node; |
2076 | ||
6c9808ce | 2077 | tsk = tipc_sk_get(ref); |
9b50fd08 | 2078 | if (!tsk) |
6c9808ce JPM |
2079 | goto exit; |
2080 | sk = &tsk->sk; | |
9b50fd08 | 2081 | port = &tsk->port; |
6c9808ce JPM |
2082 | |
2083 | bh_lock_sock(sk); | |
57289015 | 2084 | if (!port->connected) { |
6c9808ce JPM |
2085 | bh_unlock_sock(sk); |
2086 | goto exit; | |
57289015 | 2087 | } |
2e84c60b JPM |
2088 | peer_port = tsk_peer_port(port); |
2089 | peer_node = tsk_peer_node(port); | |
57289015 JPM |
2090 | |
2091 | if (port->probing_state == TIPC_CONN_PROBING) { | |
2092 | /* Previous probe not answered -> self abort */ | |
2093 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, | |
2094 | SHORT_H_SIZE, 0, tipc_own_addr, | |
2095 | peer_node, ref, peer_port, | |
2096 | TIPC_ERR_NO_PORT); | |
2097 | } else { | |
2098 | buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, | |
2099 | 0, peer_node, tipc_own_addr, | |
2100 | peer_port, ref, TIPC_OK); | |
2101 | port->probing_state = TIPC_CONN_PROBING; | |
2102 | k_start_timer(&port->timer, port->probing_interval); | |
2103 | } | |
2104 | bh_unlock_sock(sk); | |
6c9808ce JPM |
2105 | if (buf) |
2106 | tipc_link_xmit(buf, peer_node, ref); | |
2107 | exit: | |
2108 | tipc_sk_put(tsk); | |
57289015 JPM |
2109 | } |
2110 | ||
0fc87aae JPM |
2111 | static int tipc_sk_publish(struct tipc_port *port, uint scope, |
2112 | struct tipc_name_seq const *seq) | |
2113 | { | |
2114 | struct publication *publ; | |
2115 | u32 key; | |
2116 | ||
2117 | if (port->connected) | |
2118 | return -EINVAL; | |
2119 | key = port->ref + port->pub_count + 1; | |
2120 | if (key == port->ref) | |
2121 | return -EADDRINUSE; | |
2122 | ||
2123 | publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper, | |
2124 | scope, port->ref, key); | |
2125 | if (unlikely(!publ)) | |
2126 | return -EINVAL; | |
2127 | ||
2128 | list_add(&publ->pport_list, &port->publications); | |
2129 | port->pub_count++; | |
2130 | port->published = 1; | |
2131 | return 0; | |
2132 | } | |
2133 | ||
2134 | static int tipc_sk_withdraw(struct tipc_port *port, uint scope, | |
2135 | struct tipc_name_seq const *seq) | |
2136 | { | |
2137 | struct publication *publ; | |
2138 | struct publication *safe; | |
2139 | int rc = -EINVAL; | |
2140 | ||
2141 | list_for_each_entry_safe(publ, safe, &port->publications, pport_list) { | |
2142 | if (seq) { | |
2143 | if (publ->scope != scope) | |
2144 | continue; | |
2145 | if (publ->type != seq->type) | |
2146 | continue; | |
2147 | if (publ->lower != seq->lower) | |
2148 | continue; | |
2149 | if (publ->upper != seq->upper) | |
2150 | break; | |
2151 | tipc_nametbl_withdraw(publ->type, publ->lower, | |
2152 | publ->ref, publ->key); | |
2153 | rc = 0; | |
2154 | break; | |
2155 | } | |
2156 | tipc_nametbl_withdraw(publ->type, publ->lower, | |
2157 | publ->ref, publ->key); | |
2158 | rc = 0; | |
2159 | } | |
2160 | if (list_empty(&port->publications)) | |
2161 | port->published = 0; | |
2162 | return rc; | |
2163 | } | |
2164 | ||
5a9ee0be JPM |
2165 | static int tipc_sk_show(struct tipc_port *port, char *buf, |
2166 | int len, int full_id) | |
2167 | { | |
2168 | struct publication *publ; | |
2169 | int ret; | |
2170 | ||
2171 | if (full_id) | |
2172 | ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:", | |
2173 | tipc_zone(tipc_own_addr), | |
2174 | tipc_cluster(tipc_own_addr), | |
2175 | tipc_node(tipc_own_addr), port->ref); | |
2176 | else | |
2177 | ret = tipc_snprintf(buf, len, "%-10u:", port->ref); | |
2178 | ||
2179 | if (port->connected) { | |
2e84c60b JPM |
2180 | u32 dport = tsk_peer_port(port); |
2181 | u32 destnode = tsk_peer_node(port); | |
5a9ee0be JPM |
2182 | |
2183 | ret += tipc_snprintf(buf + ret, len - ret, | |
2184 | " connected to <%u.%u.%u:%u>", | |
2185 | tipc_zone(destnode), | |
2186 | tipc_cluster(destnode), | |
2187 | tipc_node(destnode), dport); | |
2188 | if (port->conn_type != 0) | |
2189 | ret += tipc_snprintf(buf + ret, len - ret, | |
2190 | " via {%u,%u}", port->conn_type, | |
2191 | port->conn_instance); | |
2192 | } else if (port->published) { | |
2193 | ret += tipc_snprintf(buf + ret, len - ret, " bound to"); | |
2194 | list_for_each_entry(publ, &port->publications, pport_list) { | |
2195 | if (publ->lower == publ->upper) | |
2196 | ret += tipc_snprintf(buf + ret, len - ret, | |
2197 | " {%u,%u}", publ->type, | |
2198 | publ->lower); | |
2199 | else | |
2200 | ret += tipc_snprintf(buf + ret, len - ret, | |
2201 | " {%u,%u,%u}", publ->type, | |
2202 | publ->lower, publ->upper); | |
2203 | } | |
2204 | } | |
2205 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); | |
2206 | return ret; | |
2207 | } | |
2208 | ||
2209 | struct sk_buff *tipc_sk_socks_show(void) | |
2210 | { | |
2211 | struct sk_buff *buf; | |
2212 | struct tlv_desc *rep_tlv; | |
2213 | char *pb; | |
2214 | int pb_len; | |
2215 | struct tipc_sock *tsk; | |
2216 | int str_len = 0; | |
2217 | u32 ref = 0; | |
2218 | ||
2219 | buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); | |
2220 | if (!buf) | |
2221 | return NULL; | |
2222 | rep_tlv = (struct tlv_desc *)buf->data; | |
2223 | pb = TLV_DATA(rep_tlv); | |
2224 | pb_len = ULTRA_STRING_MAX_LEN; | |
2225 | ||
6c9808ce JPM |
2226 | tsk = tipc_sk_get_next(&ref); |
2227 | for (; tsk; tsk = tipc_sk_get_next(&ref)) { | |
2228 | lock_sock(&tsk->sk); | |
5a9ee0be JPM |
2229 | str_len += tipc_sk_show(&tsk->port, pb + str_len, |
2230 | pb_len - str_len, 0); | |
6c9808ce JPM |
2231 | release_sock(&tsk->sk); |
2232 | tipc_sk_put(tsk); | |
5a9ee0be JPM |
2233 | } |
2234 | str_len += 1; /* for "\0" */ | |
2235 | skb_put(buf, TLV_SPACE(str_len)); | |
2236 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
2237 | ||
2238 | return buf; | |
2239 | } | |
2240 | ||
2241 | /* tipc_sk_reinit: set non-zero address in all existing sockets | |
2242 | * when we go from standalone to network mode. | |
2243 | */ | |
2244 | void tipc_sk_reinit(void) | |
2245 | { | |
2246 | struct tipc_msg *msg; | |
2247 | u32 ref = 0; | |
6c9808ce | 2248 | struct tipc_sock *tsk = tipc_sk_get_next(&ref); |
5a9ee0be | 2249 | |
6c9808ce JPM |
2250 | for (; tsk; tsk = tipc_sk_get_next(&ref)) { |
2251 | lock_sock(&tsk->sk); | |
5a9ee0be JPM |
2252 | msg = &tsk->port.phdr; |
2253 | msg_set_prevnode(msg, tipc_own_addr); | |
2254 | msg_set_orignode(msg, tipc_own_addr); | |
6c9808ce JPM |
2255 | release_sock(&tsk->sk); |
2256 | tipc_sk_put(tsk); | |
5a9ee0be JPM |
2257 | } |
2258 | } | |
2259 | ||
808d90f9 JPM |
2260 | /** |
2261 | * struct reference - TIPC socket reference entry | |
2262 | * @tsk: pointer to socket associated with reference entry | |
2263 | * @ref: reference value for socket (combines instance & array index info) | |
2264 | */ | |
2265 | struct reference { | |
2266 | struct tipc_sock *tsk; | |
2267 | u32 ref; | |
2268 | }; | |
2269 | ||
2270 | /** | |
2271 | * struct tipc_ref_table - table of TIPC socket reference entries | |
2272 | * @entries: pointer to array of reference entries | |
2273 | * @capacity: array index of first unusable entry | |
2274 | * @init_point: array index of first uninitialized entry | |
2275 | * @first_free: array index of first unused socket reference entry | |
2276 | * @last_free: array index of last unused socket reference entry | |
2277 | * @index_mask: bitmask for array index portion of reference values | |
2278 | * @start_mask: initial value for instance value portion of reference values | |
2279 | */ | |
2280 | struct ref_table { | |
2281 | struct reference *entries; | |
2282 | u32 capacity; | |
2283 | u32 init_point; | |
2284 | u32 first_free; | |
2285 | u32 last_free; | |
2286 | u32 index_mask; | |
2287 | u32 start_mask; | |
2288 | }; | |
2289 | ||
2290 | /* Socket reference table consists of 2**N entries. | |
2291 | * | |
2292 | * State Socket ptr Reference | |
2293 | * ----- ---------- --------- | |
2294 | * In use non-NULL XXXX|own index | |
2295 | * (XXXX changes each time entry is acquired) | |
2296 | * Free NULL YYYY|next free index | |
2297 | * (YYYY is one more than last used XXXX) | |
2298 | * Uninitialized NULL 0 | |
2299 | * | |
2300 | * Entry 0 is not used; this allows index 0 to denote the end of the free list. | |
2301 | * | |
2302 | * Note that a reference value of 0 does not necessarily indicate that an | |
2303 | * entry is uninitialized, since the last entry in the free list could also | |
2304 | * have a reference value of 0 (although this is unlikely). | |
2305 | */ | |
2306 | ||
2307 | static struct ref_table tipc_ref_table; | |
2308 | ||
2309 | static DEFINE_RWLOCK(ref_table_lock); | |
2310 | ||
2311 | /** | |
2312 | * tipc_ref_table_init - create reference table for sockets | |
2313 | */ | |
2314 | int tipc_sk_ref_table_init(u32 req_sz, u32 start) | |
2315 | { | |
2316 | struct reference *table; | |
2317 | u32 actual_sz; | |
2318 | ||
2319 | /* account for unused entry, then round up size to a power of 2 */ | |
2320 | ||
2321 | req_sz++; | |
2322 | for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) { | |
2323 | /* do nothing */ | |
2324 | }; | |
2325 | ||
2326 | /* allocate table & mark all entries as uninitialized */ | |
2327 | table = vzalloc(actual_sz * sizeof(struct reference)); | |
2328 | if (table == NULL) | |
2329 | return -ENOMEM; | |
2330 | ||
2331 | tipc_ref_table.entries = table; | |
2332 | tipc_ref_table.capacity = req_sz; | |
2333 | tipc_ref_table.init_point = 1; | |
2334 | tipc_ref_table.first_free = 0; | |
2335 | tipc_ref_table.last_free = 0; | |
2336 | tipc_ref_table.index_mask = actual_sz - 1; | |
2337 | tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask; | |
2338 | ||
2339 | return 0; | |
2340 | } | |
2341 | ||
2342 | /** | |
2343 | * tipc_ref_table_stop - destroy reference table for sockets | |
2344 | */ | |
2345 | void tipc_sk_ref_table_stop(void) | |
2346 | { | |
2347 | if (!tipc_ref_table.entries) | |
2348 | return; | |
2349 | vfree(tipc_ref_table.entries); | |
2350 | tipc_ref_table.entries = NULL; | |
2351 | } | |
2352 | ||
2353 | /* tipc_ref_acquire - create reference to a socket | |
2354 | * | |
2355 | * Register an socket pointer in the reference table. | |
2356 | * Returns a unique reference value that is used from then on to retrieve the | |
2357 | * socket pointer, or to determine if the socket has been deregistered. | |
2358 | */ | |
2359 | u32 tipc_sk_ref_acquire(struct tipc_sock *tsk) | |
2360 | { | |
2361 | u32 index; | |
2362 | u32 index_mask; | |
2363 | u32 next_plus_upper; | |
2364 | u32 ref = 0; | |
2365 | struct reference *entry; | |
2366 | ||
2367 | if (unlikely(!tsk)) { | |
2368 | pr_err("Attempt to acquire ref. to non-existent obj\n"); | |
2369 | return 0; | |
2370 | } | |
2371 | if (unlikely(!tipc_ref_table.entries)) { | |
2372 | pr_err("Ref. table not found in acquisition attempt\n"); | |
2373 | return 0; | |
2374 | } | |
2375 | ||
2376 | /* Take a free entry, if available; otherwise initialize a new one */ | |
2377 | write_lock_bh(&ref_table_lock); | |
2378 | index = tipc_ref_table.first_free; | |
2379 | entry = &tipc_ref_table.entries[index]; | |
2380 | ||
2381 | if (likely(index)) { | |
2382 | index = tipc_ref_table.first_free; | |
2383 | entry = &tipc_ref_table.entries[index]; | |
2384 | index_mask = tipc_ref_table.index_mask; | |
2385 | next_plus_upper = entry->ref; | |
2386 | tipc_ref_table.first_free = next_plus_upper & index_mask; | |
2387 | ref = (next_plus_upper & ~index_mask) + index; | |
2388 | entry->tsk = tsk; | |
2389 | } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) { | |
2390 | index = tipc_ref_table.init_point++; | |
2391 | entry = &tipc_ref_table.entries[index]; | |
2392 | ref = tipc_ref_table.start_mask + index; | |
2393 | } | |
2394 | ||
2395 | if (ref) { | |
2396 | entry->ref = ref; | |
2397 | entry->tsk = tsk; | |
2398 | } | |
2399 | write_unlock_bh(&ref_table_lock); | |
2400 | return ref; | |
2401 | } | |
2402 | ||
2403 | /* tipc_sk_ref_discard - invalidate reference to an socket | |
2404 | * | |
2405 | * Disallow future references to an socket and free up the entry for re-use. | |
2406 | */ | |
2407 | void tipc_sk_ref_discard(u32 ref) | |
2408 | { | |
2409 | struct reference *entry; | |
2410 | u32 index; | |
2411 | u32 index_mask; | |
2412 | ||
2413 | if (unlikely(!tipc_ref_table.entries)) { | |
2414 | pr_err("Ref. table not found during discard attempt\n"); | |
2415 | return; | |
2416 | } | |
2417 | ||
2418 | index_mask = tipc_ref_table.index_mask; | |
2419 | index = ref & index_mask; | |
2420 | entry = &tipc_ref_table.entries[index]; | |
2421 | ||
2422 | write_lock_bh(&ref_table_lock); | |
2423 | ||
2424 | if (unlikely(!entry->tsk)) { | |
2425 | pr_err("Attempt to discard ref. to non-existent socket\n"); | |
2426 | goto exit; | |
2427 | } | |
2428 | if (unlikely(entry->ref != ref)) { | |
2429 | pr_err("Attempt to discard non-existent reference\n"); | |
2430 | goto exit; | |
2431 | } | |
2432 | ||
2433 | /* Mark entry as unused; increment instance part of entry's | |
2434 | * reference to invalidate any subsequent references | |
2435 | */ | |
2436 | ||
2437 | entry->tsk = NULL; | |
2438 | entry->ref = (ref & ~index_mask) + (index_mask + 1); | |
2439 | ||
2440 | /* Append entry to free entry list */ | |
2441 | if (unlikely(tipc_ref_table.first_free == 0)) | |
2442 | tipc_ref_table.first_free = index; | |
2443 | else | |
2444 | tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index; | |
2445 | tipc_ref_table.last_free = index; | |
2446 | exit: | |
2447 | write_unlock_bh(&ref_table_lock); | |
2448 | } | |
2449 | ||
2450 | /* tipc_sk_get - find referenced socket and return pointer to it | |
2451 | */ | |
2452 | struct tipc_sock *tipc_sk_get(u32 ref) | |
2453 | { | |
2454 | struct reference *entry; | |
2455 | struct tipc_sock *tsk; | |
2456 | ||
2457 | if (unlikely(!tipc_ref_table.entries)) | |
2458 | return NULL; | |
2459 | read_lock_bh(&ref_table_lock); | |
2460 | entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask]; | |
2461 | tsk = entry->tsk; | |
2462 | if (likely(tsk && (entry->ref == ref))) | |
2463 | sock_hold(&tsk->sk); | |
2464 | else | |
2465 | tsk = NULL; | |
2466 | read_unlock_bh(&ref_table_lock); | |
2467 | return tsk; | |
2468 | } | |
2469 | ||
2470 | /* tipc_sk_get_next - lock & return next socket after referenced one | |
2471 | */ | |
2472 | struct tipc_sock *tipc_sk_get_next(u32 *ref) | |
2473 | { | |
2474 | struct reference *entry; | |
2475 | struct tipc_sock *tsk = NULL; | |
2476 | uint index = *ref & tipc_ref_table.index_mask; | |
2477 | ||
2478 | read_lock_bh(&ref_table_lock); | |
2479 | while (++index < tipc_ref_table.capacity) { | |
2480 | entry = &tipc_ref_table.entries[index]; | |
2481 | if (!entry->tsk) | |
2482 | continue; | |
2483 | tsk = entry->tsk; | |
2484 | sock_hold(&tsk->sk); | |
2485 | *ref = entry->ref; | |
2486 | break; | |
2487 | } | |
2488 | read_unlock_bh(&ref_table_lock); | |
2489 | return tsk; | |
2490 | } | |
2491 | ||
2492 | static void tipc_sk_put(struct tipc_sock *tsk) | |
2493 | { | |
2494 | sock_put(&tsk->sk); | |
2495 | } | |
2496 | ||
b97bf3fd | 2497 | /** |
247f0f3c | 2498 | * tipc_setsockopt - set socket option |
b97bf3fd PL |
2499 | * @sock: socket structure |
2500 | * @lvl: option level | |
2501 | * @opt: option identifier | |
2502 | * @ov: pointer to new option value | |
2503 | * @ol: length of option value | |
c4307285 YH |
2504 | * |
2505 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options | |
b97bf3fd | 2506 | * (to ease compatibility). |
c4307285 | 2507 | * |
b97bf3fd PL |
2508 | * Returns 0 on success, errno otherwise |
2509 | */ | |
247f0f3c YX |
2510 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
2511 | char __user *ov, unsigned int ol) | |
b97bf3fd | 2512 | { |
0c3141e9 | 2513 | struct sock *sk = sock->sk; |
58ed9442 JPM |
2514 | struct tipc_sock *tsk = tipc_sk(sk); |
2515 | struct tipc_port *port = &tsk->port; | |
b97bf3fd PL |
2516 | u32 value; |
2517 | int res; | |
2518 | ||
c4307285 YH |
2519 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2520 | return 0; | |
b97bf3fd PL |
2521 | if (lvl != SOL_TIPC) |
2522 | return -ENOPROTOOPT; | |
2523 | if (ol < sizeof(value)) | |
2524 | return -EINVAL; | |
2db9983a AS |
2525 | res = get_user(value, (u32 __user *)ov); |
2526 | if (res) | |
b97bf3fd PL |
2527 | return res; |
2528 | ||
0c3141e9 | 2529 | lock_sock(sk); |
c4307285 | 2530 | |
b97bf3fd PL |
2531 | switch (opt) { |
2532 | case TIPC_IMPORTANCE: | |
2e84c60b | 2533 | res = tsk_set_importance(port, value); |
b97bf3fd PL |
2534 | break; |
2535 | case TIPC_SRC_DROPPABLE: | |
2536 | if (sock->type != SOCK_STREAM) | |
2e84c60b | 2537 | tsk_set_unreliable(port, value); |
c4307285 | 2538 | else |
b97bf3fd PL |
2539 | res = -ENOPROTOOPT; |
2540 | break; | |
2541 | case TIPC_DEST_DROPPABLE: | |
2e84c60b | 2542 | tsk_set_unreturnable(port, value); |
b97bf3fd PL |
2543 | break; |
2544 | case TIPC_CONN_TIMEOUT: | |
a0f40f02 | 2545 | tipc_sk(sk)->conn_timeout = value; |
0c3141e9 | 2546 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd PL |
2547 | break; |
2548 | default: | |
2549 | res = -EINVAL; | |
2550 | } | |
2551 | ||
0c3141e9 AS |
2552 | release_sock(sk); |
2553 | ||
b97bf3fd PL |
2554 | return res; |
2555 | } | |
2556 | ||
2557 | /** | |
247f0f3c | 2558 | * tipc_getsockopt - get socket option |
b97bf3fd PL |
2559 | * @sock: socket structure |
2560 | * @lvl: option level | |
2561 | * @opt: option identifier | |
2562 | * @ov: receptacle for option value | |
2563 | * @ol: receptacle for length of option value | |
c4307285 YH |
2564 | * |
2565 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options | |
b97bf3fd | 2566 | * (to ease compatibility). |
c4307285 | 2567 | * |
b97bf3fd PL |
2568 | * Returns 0 on success, errno otherwise |
2569 | */ | |
247f0f3c YX |
2570 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
2571 | char __user *ov, int __user *ol) | |
b97bf3fd | 2572 | { |
0c3141e9 | 2573 | struct sock *sk = sock->sk; |
58ed9442 JPM |
2574 | struct tipc_sock *tsk = tipc_sk(sk); |
2575 | struct tipc_port *port = &tsk->port; | |
c4307285 | 2576 | int len; |
b97bf3fd | 2577 | u32 value; |
c4307285 | 2578 | int res; |
b97bf3fd | 2579 | |
c4307285 YH |
2580 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2581 | return put_user(0, ol); | |
b97bf3fd PL |
2582 | if (lvl != SOL_TIPC) |
2583 | return -ENOPROTOOPT; | |
2db9983a AS |
2584 | res = get_user(len, ol); |
2585 | if (res) | |
c4307285 | 2586 | return res; |
b97bf3fd | 2587 | |
0c3141e9 | 2588 | lock_sock(sk); |
b97bf3fd PL |
2589 | |
2590 | switch (opt) { | |
2591 | case TIPC_IMPORTANCE: | |
2e84c60b | 2592 | value = tsk_importance(port); |
b97bf3fd PL |
2593 | break; |
2594 | case TIPC_SRC_DROPPABLE: | |
2e84c60b | 2595 | value = tsk_unreliable(port); |
b97bf3fd PL |
2596 | break; |
2597 | case TIPC_DEST_DROPPABLE: | |
2e84c60b | 2598 | value = tsk_unreturnable(port); |
b97bf3fd PL |
2599 | break; |
2600 | case TIPC_CONN_TIMEOUT: | |
a0f40f02 | 2601 | value = tipc_sk(sk)->conn_timeout; |
0c3141e9 | 2602 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd | 2603 | break; |
0e65967e | 2604 | case TIPC_NODE_RECVQ_DEPTH: |
9da3d475 | 2605 | value = 0; /* was tipc_queue_size, now obsolete */ |
6650613d | 2606 | break; |
0e65967e | 2607 | case TIPC_SOCK_RECVQ_DEPTH: |
6650613d | 2608 | value = skb_queue_len(&sk->sk_receive_queue); |
2609 | break; | |
b97bf3fd PL |
2610 | default: |
2611 | res = -EINVAL; | |
2612 | } | |
2613 | ||
0c3141e9 AS |
2614 | release_sock(sk); |
2615 | ||
25860c3b PG |
2616 | if (res) |
2617 | return res; /* "get" failed */ | |
b97bf3fd | 2618 | |
25860c3b PG |
2619 | if (len < sizeof(value)) |
2620 | return -EINVAL; | |
2621 | ||
2622 | if (copy_to_user(ov, &value, sizeof(value))) | |
2623 | return -EFAULT; | |
2624 | ||
2625 | return put_user(sizeof(value), ol); | |
b97bf3fd PL |
2626 | } |
2627 | ||
52f50ce5 | 2628 | static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg) |
78acb1f9 EH |
2629 | { |
2630 | struct tipc_sioc_ln_req lnr; | |
2631 | void __user *argp = (void __user *)arg; | |
2632 | ||
2633 | switch (cmd) { | |
2634 | case SIOCGETLINKNAME: | |
2635 | if (copy_from_user(&lnr, argp, sizeof(lnr))) | |
2636 | return -EFAULT; | |
2637 | if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer, | |
2638 | lnr.linkname, TIPC_MAX_LINK_NAME)) { | |
2639 | if (copy_to_user(argp, &lnr, sizeof(lnr))) | |
2640 | return -EFAULT; | |
2641 | return 0; | |
2642 | } | |
2643 | return -EADDRNOTAVAIL; | |
78acb1f9 EH |
2644 | default: |
2645 | return -ENOIOCTLCMD; | |
2646 | } | |
2647 | } | |
2648 | ||
ae86b9e3 BH |
2649 | /* Protocol switches for the various types of TIPC sockets */ |
2650 | ||
bca65eae | 2651 | static const struct proto_ops msg_ops = { |
0e65967e | 2652 | .owner = THIS_MODULE, |
b97bf3fd | 2653 | .family = AF_TIPC, |
247f0f3c YX |
2654 | .release = tipc_release, |
2655 | .bind = tipc_bind, | |
2656 | .connect = tipc_connect, | |
5eee6a6d | 2657 | .socketpair = sock_no_socketpair, |
245f3d34 | 2658 | .accept = sock_no_accept, |
247f0f3c YX |
2659 | .getname = tipc_getname, |
2660 | .poll = tipc_poll, | |
78acb1f9 | 2661 | .ioctl = tipc_ioctl, |
245f3d34 | 2662 | .listen = sock_no_listen, |
247f0f3c YX |
2663 | .shutdown = tipc_shutdown, |
2664 | .setsockopt = tipc_setsockopt, | |
2665 | .getsockopt = tipc_getsockopt, | |
2666 | .sendmsg = tipc_sendmsg, | |
2667 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2668 | .mmap = sock_no_mmap, |
2669 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2670 | }; |
2671 | ||
bca65eae | 2672 | static const struct proto_ops packet_ops = { |
0e65967e | 2673 | .owner = THIS_MODULE, |
b97bf3fd | 2674 | .family = AF_TIPC, |
247f0f3c YX |
2675 | .release = tipc_release, |
2676 | .bind = tipc_bind, | |
2677 | .connect = tipc_connect, | |
5eee6a6d | 2678 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2679 | .accept = tipc_accept, |
2680 | .getname = tipc_getname, | |
2681 | .poll = tipc_poll, | |
78acb1f9 | 2682 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2683 | .listen = tipc_listen, |
2684 | .shutdown = tipc_shutdown, | |
2685 | .setsockopt = tipc_setsockopt, | |
2686 | .getsockopt = tipc_getsockopt, | |
2687 | .sendmsg = tipc_send_packet, | |
2688 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2689 | .mmap = sock_no_mmap, |
2690 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2691 | }; |
2692 | ||
bca65eae | 2693 | static const struct proto_ops stream_ops = { |
0e65967e | 2694 | .owner = THIS_MODULE, |
b97bf3fd | 2695 | .family = AF_TIPC, |
247f0f3c YX |
2696 | .release = tipc_release, |
2697 | .bind = tipc_bind, | |
2698 | .connect = tipc_connect, | |
5eee6a6d | 2699 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2700 | .accept = tipc_accept, |
2701 | .getname = tipc_getname, | |
2702 | .poll = tipc_poll, | |
78acb1f9 | 2703 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2704 | .listen = tipc_listen, |
2705 | .shutdown = tipc_shutdown, | |
2706 | .setsockopt = tipc_setsockopt, | |
2707 | .getsockopt = tipc_getsockopt, | |
2708 | .sendmsg = tipc_send_stream, | |
2709 | .recvmsg = tipc_recv_stream, | |
8238745a YH |
2710 | .mmap = sock_no_mmap, |
2711 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2712 | }; |
2713 | ||
bca65eae | 2714 | static const struct net_proto_family tipc_family_ops = { |
0e65967e | 2715 | .owner = THIS_MODULE, |
b97bf3fd | 2716 | .family = AF_TIPC, |
c5fa7b3c | 2717 | .create = tipc_sk_create |
b97bf3fd PL |
2718 | }; |
2719 | ||
2720 | static struct proto tipc_proto = { | |
2721 | .name = "TIPC", | |
2722 | .owner = THIS_MODULE, | |
cc79dd1b YX |
2723 | .obj_size = sizeof(struct tipc_sock), |
2724 | .sysctl_rmem = sysctl_tipc_rmem | |
b97bf3fd PL |
2725 | }; |
2726 | ||
c5fa7b3c YX |
2727 | static struct proto tipc_proto_kern = { |
2728 | .name = "TIPC", | |
2729 | .obj_size = sizeof(struct tipc_sock), | |
2730 | .sysctl_rmem = sysctl_tipc_rmem | |
2731 | }; | |
2732 | ||
b97bf3fd | 2733 | /** |
4323add6 | 2734 | * tipc_socket_init - initialize TIPC socket interface |
c4307285 | 2735 | * |
b97bf3fd PL |
2736 | * Returns 0 on success, errno otherwise |
2737 | */ | |
4323add6 | 2738 | int tipc_socket_init(void) |
b97bf3fd PL |
2739 | { |
2740 | int res; | |
2741 | ||
c4307285 | 2742 | res = proto_register(&tipc_proto, 1); |
b97bf3fd | 2743 | if (res) { |
2cf8aa19 | 2744 | pr_err("Failed to register TIPC protocol type\n"); |
b97bf3fd PL |
2745 | goto out; |
2746 | } | |
2747 | ||
2748 | res = sock_register(&tipc_family_ops); | |
2749 | if (res) { | |
2cf8aa19 | 2750 | pr_err("Failed to register TIPC socket type\n"); |
b97bf3fd PL |
2751 | proto_unregister(&tipc_proto); |
2752 | goto out; | |
2753 | } | |
b97bf3fd PL |
2754 | out: |
2755 | return res; | |
2756 | } | |
2757 | ||
2758 | /** | |
4323add6 | 2759 | * tipc_socket_stop - stop TIPC socket interface |
b97bf3fd | 2760 | */ |
4323add6 | 2761 | void tipc_socket_stop(void) |
b97bf3fd | 2762 | { |
b97bf3fd PL |
2763 | sock_unregister(tipc_family_ops.family); |
2764 | proto_unregister(&tipc_proto); | |
2765 | } |