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