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