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