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