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