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