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