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