Commit | Line | Data |
---|---|---|
b97bf3fd | 1 | /* |
02c00c2a | 2 | * net/tipc/socket.c: TIPC socket API |
c4307285 | 3 | * |
8826cde6 | 4 | * Copyright (c) 2001-2007, 2012-2014, Ericsson AB |
c5fa7b3c | 5 | * Copyright (c) 2004-2008, 2010-2013, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the names of the copyright holders nor the names of its | |
17 | * contributors may be used to endorse or promote products derived from | |
18 | * this software without specific prior written permission. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * Alternatively, this software may be distributed under the terms of the |
21 | * GNU General Public License ("GPL") version 2 as published by the Free | |
22 | * Software Foundation. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
07f6c4bc YX |
37 | #include <linux/rhashtable.h> |
38 | #include <linux/jhash.h> | |
b97bf3fd | 39 | #include "core.h" |
e2dafe87 | 40 | #include "name_table.h" |
78acb1f9 | 41 | #include "node.h" |
e2dafe87 | 42 | #include "link.h" |
5a9ee0be | 43 | #include "config.h" |
2e84c60b | 44 | #include "socket.h" |
2cf8aa19 | 45 | |
07f6c4bc YX |
46 | #define SS_LISTENING -1 /* socket is listening */ |
47 | #define SS_READY -2 /* socket is connectionless */ | |
b97bf3fd | 48 | |
07f6c4bc | 49 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
2f55c437 | 50 | #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */ |
07f6c4bc YX |
51 | #define TIPC_FWD_MSG 1 |
52 | #define TIPC_CONN_OK 0 | |
53 | #define TIPC_CONN_PROBING 1 | |
54 | #define TIPC_MAX_PORT 0xffffffff | |
55 | #define TIPC_MIN_PORT 1 | |
301bae56 JPM |
56 | |
57 | /** | |
58 | * struct tipc_sock - TIPC socket structure | |
59 | * @sk: socket - interacts with 'port' and with user via the socket API | |
60 | * @connected: non-zero if port is currently connected to a peer port | |
61 | * @conn_type: TIPC type used when connection was established | |
62 | * @conn_instance: TIPC instance used when connection was established | |
63 | * @published: non-zero if port has one or more associated names | |
64 | * @max_pkt: maximum packet size "hint" used when building messages sent by port | |
07f6c4bc | 65 | * @portid: unique port identity in TIPC socket hash table |
301bae56 JPM |
66 | * @phdr: preformatted message header used when sending messages |
67 | * @port_list: adjacent ports in TIPC's global list of ports | |
68 | * @publications: list of publications for port | |
69 | * @pub_count: total # of publications port has made during its lifetime | |
70 | * @probing_state: | |
2f55c437 | 71 | * @probing_intv: |
301bae56 JPM |
72 | * @timer: |
73 | * @port: port - interacts with 'sk' and with the rest of the TIPC stack | |
74 | * @peer_name: the peer of the connection, if any | |
75 | * @conn_timeout: the time we can wait for an unresponded setup request | |
76 | * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue | |
77 | * @link_cong: non-zero if owner must sleep because of link congestion | |
78 | * @sent_unacked: # messages sent by socket, and not yet acked by peer | |
79 | * @rcv_unacked: # messages read by user, but not yet acked back to peer | |
07f6c4bc YX |
80 | * @node: hash table node |
81 | * @rcu: rcu struct for tipc_sock | |
301bae56 JPM |
82 | */ |
83 | struct tipc_sock { | |
84 | struct sock sk; | |
85 | int connected; | |
86 | u32 conn_type; | |
87 | u32 conn_instance; | |
88 | int published; | |
89 | u32 max_pkt; | |
07f6c4bc | 90 | u32 portid; |
301bae56 JPM |
91 | struct tipc_msg phdr; |
92 | struct list_head sock_list; | |
93 | struct list_head publications; | |
94 | u32 pub_count; | |
95 | u32 probing_state; | |
2f55c437 | 96 | unsigned long probing_intv; |
301bae56 JPM |
97 | struct timer_list timer; |
98 | uint conn_timeout; | |
99 | atomic_t dupl_rcvcnt; | |
100 | bool link_cong; | |
101 | uint sent_unacked; | |
102 | uint rcv_unacked; | |
07f6c4bc YX |
103 | struct rhash_head node; |
104 | struct rcu_head rcu; | |
301bae56 | 105 | }; |
b97bf3fd | 106 | |
4f4482dc | 107 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
676d2369 | 108 | static void tipc_data_ready(struct sock *sk); |
f288bef4 | 109 | static void tipc_write_space(struct sock *sk); |
247f0f3c YX |
110 | static int tipc_release(struct socket *sock); |
111 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); | |
0abd8ff2 | 112 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); |
f2f2a96a | 113 | static void tipc_sk_timeout(unsigned long data); |
301bae56 | 114 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, |
0fc87aae | 115 | struct tipc_name_seq const *seq); |
301bae56 | 116 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, |
0fc87aae | 117 | struct tipc_name_seq const *seq); |
e05b31f4 | 118 | static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); |
07f6c4bc YX |
119 | static int tipc_sk_insert(struct tipc_sock *tsk); |
120 | static void tipc_sk_remove(struct tipc_sock *tsk); | |
b97bf3fd | 121 | |
bca65eae FW |
122 | static const struct proto_ops packet_ops; |
123 | static const struct proto_ops stream_ops; | |
124 | static const struct proto_ops msg_ops; | |
b97bf3fd PL |
125 | |
126 | static struct proto tipc_proto; | |
c5fa7b3c | 127 | static struct proto tipc_proto_kern; |
b97bf3fd | 128 | |
1a1a143d RA |
129 | static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = { |
130 | [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC }, | |
131 | [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 }, | |
132 | [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 }, | |
133 | [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED }, | |
134 | [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG } | |
135 | }; | |
136 | ||
c4307285 | 137 | /* |
0c3141e9 AS |
138 | * Revised TIPC socket locking policy: |
139 | * | |
140 | * Most socket operations take the standard socket lock when they start | |
141 | * and hold it until they finish (or until they need to sleep). Acquiring | |
142 | * this lock grants the owner exclusive access to the fields of the socket | |
143 | * data structures, with the exception of the backlog queue. A few socket | |
144 | * operations can be done without taking the socket lock because they only | |
145 | * read socket information that never changes during the life of the socket. | |
146 | * | |
147 | * Socket operations may acquire the lock for the associated TIPC port if they | |
148 | * need to perform an operation on the port. If any routine needs to acquire | |
149 | * both the socket lock and the port lock it must take the socket lock first | |
150 | * to avoid the risk of deadlock. | |
151 | * | |
152 | * The dispatcher handling incoming messages cannot grab the socket lock in | |
153 | * the standard fashion, since invoked it runs at the BH level and cannot block. | |
154 | * Instead, it checks to see if the socket lock is currently owned by someone, | |
155 | * and either handles the message itself or adds it to the socket's backlog | |
156 | * queue; in the latter case the queued message is processed once the process | |
157 | * owning the socket lock releases it. | |
158 | * | |
159 | * NOTE: Releasing the socket lock while an operation is sleeping overcomes | |
160 | * the problem of a blocked socket operation preventing any other operations | |
161 | * from occurring. However, applications must be careful if they have | |
162 | * multiple threads trying to send (or receive) on the same socket, as these | |
163 | * operations might interfere with each other. For example, doing a connect | |
164 | * and a receive at the same time might allow the receive to consume the | |
165 | * ACK message meant for the connect. While additional work could be done | |
166 | * to try and overcome this, it doesn't seem to be worthwhile at the present. | |
167 | * | |
168 | * NOTE: Releasing the socket lock while an operation is sleeping also ensures | |
169 | * that another operation that must be performed in a non-blocking manner is | |
170 | * not delayed for very long because the lock has already been taken. | |
171 | * | |
172 | * NOTE: This code assumes that certain fields of a port/socket pair are | |
173 | * constant over its lifetime; such fields can be examined without taking | |
174 | * the socket lock and/or port lock, and do not need to be re-read even | |
175 | * after resuming processing after waiting. These fields include: | |
176 | * - socket type | |
177 | * - pointer to socket sk structure (aka tipc_sock structure) | |
178 | * - pointer to port structure | |
179 | * - port reference | |
180 | */ | |
181 | ||
301bae56 | 182 | static u32 tsk_peer_node(struct tipc_sock *tsk) |
2e84c60b | 183 | { |
301bae56 | 184 | return msg_destnode(&tsk->phdr); |
2e84c60b JPM |
185 | } |
186 | ||
301bae56 | 187 | static u32 tsk_peer_port(struct tipc_sock *tsk) |
2e84c60b | 188 | { |
301bae56 | 189 | return msg_destport(&tsk->phdr); |
2e84c60b JPM |
190 | } |
191 | ||
301bae56 | 192 | static bool tsk_unreliable(struct tipc_sock *tsk) |
2e84c60b | 193 | { |
301bae56 | 194 | return msg_src_droppable(&tsk->phdr) != 0; |
2e84c60b JPM |
195 | } |
196 | ||
301bae56 | 197 | static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable) |
2e84c60b | 198 | { |
301bae56 | 199 | msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0); |
2e84c60b JPM |
200 | } |
201 | ||
301bae56 | 202 | static bool tsk_unreturnable(struct tipc_sock *tsk) |
2e84c60b | 203 | { |
301bae56 | 204 | return msg_dest_droppable(&tsk->phdr) != 0; |
2e84c60b JPM |
205 | } |
206 | ||
301bae56 | 207 | static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable) |
2e84c60b | 208 | { |
301bae56 | 209 | msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0); |
2e84c60b JPM |
210 | } |
211 | ||
301bae56 | 212 | static int tsk_importance(struct tipc_sock *tsk) |
2e84c60b | 213 | { |
301bae56 | 214 | return msg_importance(&tsk->phdr); |
2e84c60b JPM |
215 | } |
216 | ||
301bae56 | 217 | static int tsk_set_importance(struct tipc_sock *tsk, int imp) |
2e84c60b JPM |
218 | { |
219 | if (imp > TIPC_CRITICAL_IMPORTANCE) | |
220 | return -EINVAL; | |
301bae56 | 221 | msg_set_importance(&tsk->phdr, (u32)imp); |
2e84c60b JPM |
222 | return 0; |
223 | } | |
8826cde6 | 224 | |
301bae56 JPM |
225 | static struct tipc_sock *tipc_sk(const struct sock *sk) |
226 | { | |
227 | return container_of(sk, struct tipc_sock, sk); | |
228 | } | |
229 | ||
230 | static int tsk_conn_cong(struct tipc_sock *tsk) | |
231 | { | |
232 | return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN; | |
233 | } | |
234 | ||
0c3141e9 | 235 | /** |
2e84c60b | 236 | * tsk_advance_rx_queue - discard first buffer in socket receive queue |
0c3141e9 AS |
237 | * |
238 | * Caller must hold socket lock | |
b97bf3fd | 239 | */ |
2e84c60b | 240 | static void tsk_advance_rx_queue(struct sock *sk) |
b97bf3fd | 241 | { |
5f6d9123 | 242 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
b97bf3fd PL |
243 | } |
244 | ||
b97bf3fd | 245 | /** |
2e84c60b | 246 | * tsk_rej_rx_queue - reject all buffers in socket receive queue |
0c3141e9 AS |
247 | * |
248 | * Caller must hold socket lock | |
b97bf3fd | 249 | */ |
2e84c60b | 250 | static void tsk_rej_rx_queue(struct sock *sk) |
b97bf3fd | 251 | { |
a6ca1094 | 252 | struct sk_buff *skb; |
8db1bae3 | 253 | u32 dnode; |
0c3141e9 | 254 | |
a6ca1094 YX |
255 | while ((skb = __skb_dequeue(&sk->sk_receive_queue))) { |
256 | if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT)) | |
f2f9800d | 257 | tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0); |
8db1bae3 | 258 | } |
b97bf3fd PL |
259 | } |
260 | ||
2e84c60b | 261 | /* tsk_peer_msg - verify if message was sent by connected port's peer |
0fc87aae JPM |
262 | * |
263 | * Handles cases where the node's network address has changed from | |
264 | * the default of <0.0.0> to its configured setting. | |
265 | */ | |
2e84c60b | 266 | static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) |
0fc87aae | 267 | { |
301bae56 | 268 | u32 peer_port = tsk_peer_port(tsk); |
0fc87aae JPM |
269 | u32 orig_node; |
270 | u32 peer_node; | |
271 | ||
301bae56 | 272 | if (unlikely(!tsk->connected)) |
0fc87aae JPM |
273 | return false; |
274 | ||
275 | if (unlikely(msg_origport(msg) != peer_port)) | |
276 | return false; | |
277 | ||
278 | orig_node = msg_orignode(msg); | |
301bae56 | 279 | peer_node = tsk_peer_node(tsk); |
0fc87aae JPM |
280 | |
281 | if (likely(orig_node == peer_node)) | |
282 | return true; | |
283 | ||
284 | if (!orig_node && (peer_node == tipc_own_addr)) | |
285 | return true; | |
286 | ||
287 | if (!peer_node && (orig_node == tipc_own_addr)) | |
288 | return true; | |
289 | ||
290 | return false; | |
291 | } | |
292 | ||
b97bf3fd | 293 | /** |
c5fa7b3c | 294 | * tipc_sk_create - create a TIPC socket |
0c3141e9 | 295 | * @net: network namespace (must be default network) |
b97bf3fd PL |
296 | * @sock: pre-allocated socket structure |
297 | * @protocol: protocol indicator (must be 0) | |
3f378b68 | 298 | * @kern: caused by kernel or by userspace? |
c4307285 | 299 | * |
0c3141e9 AS |
300 | * This routine creates additional data structures used by the TIPC socket, |
301 | * initializes them, and links them together. | |
b97bf3fd PL |
302 | * |
303 | * Returns 0 on success, errno otherwise | |
304 | */ | |
58ed9442 JPM |
305 | static int tipc_sk_create(struct net *net, struct socket *sock, |
306 | int protocol, int kern) | |
b97bf3fd | 307 | { |
0c3141e9 AS |
308 | const struct proto_ops *ops; |
309 | socket_state state; | |
b97bf3fd | 310 | struct sock *sk; |
58ed9442 | 311 | struct tipc_sock *tsk; |
5b8fa7ce | 312 | struct tipc_msg *msg; |
0c3141e9 AS |
313 | |
314 | /* Validate arguments */ | |
b97bf3fd PL |
315 | if (unlikely(protocol != 0)) |
316 | return -EPROTONOSUPPORT; | |
317 | ||
b97bf3fd PL |
318 | switch (sock->type) { |
319 | case SOCK_STREAM: | |
0c3141e9 AS |
320 | ops = &stream_ops; |
321 | state = SS_UNCONNECTED; | |
b97bf3fd PL |
322 | break; |
323 | case SOCK_SEQPACKET: | |
0c3141e9 AS |
324 | ops = &packet_ops; |
325 | state = SS_UNCONNECTED; | |
b97bf3fd PL |
326 | break; |
327 | case SOCK_DGRAM: | |
b97bf3fd | 328 | case SOCK_RDM: |
0c3141e9 AS |
329 | ops = &msg_ops; |
330 | state = SS_READY; | |
b97bf3fd | 331 | break; |
49978651 | 332 | default: |
49978651 | 333 | return -EPROTOTYPE; |
b97bf3fd PL |
334 | } |
335 | ||
0c3141e9 | 336 | /* Allocate socket's protocol area */ |
c5fa7b3c YX |
337 | if (!kern) |
338 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); | |
339 | else | |
340 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern); | |
341 | ||
0c3141e9 | 342 | if (sk == NULL) |
b97bf3fd | 343 | return -ENOMEM; |
b97bf3fd | 344 | |
58ed9442 | 345 | tsk = tipc_sk(sk); |
301bae56 | 346 | tsk->max_pkt = MAX_PKT_DEFAULT; |
301bae56 JPM |
347 | INIT_LIST_HEAD(&tsk->publications); |
348 | msg = &tsk->phdr; | |
5b8fa7ce JPM |
349 | tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, |
350 | NAMED_H_SIZE, 0); | |
b97bf3fd | 351 | |
0c3141e9 | 352 | /* Finish initializing socket data structures */ |
0c3141e9 AS |
353 | sock->ops = ops; |
354 | sock->state = state; | |
0c3141e9 | 355 | sock_init_data(sock, sk); |
07f6c4bc YX |
356 | if (tipc_sk_insert(tsk)) { |
357 | pr_warn("Socket create failed; port numbrer exhausted\n"); | |
358 | return -EINVAL; | |
359 | } | |
360 | msg_set_origport(msg, tsk->portid); | |
f2f2a96a | 361 | setup_timer(&tsk->timer, tipc_sk_timeout, (unsigned long)tsk); |
4f4482dc | 362 | sk->sk_backlog_rcv = tipc_backlog_rcv; |
cc79dd1b | 363 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
f288bef4 YX |
364 | sk->sk_data_ready = tipc_data_ready; |
365 | sk->sk_write_space = tipc_write_space; | |
4f4482dc | 366 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
60120526 | 367 | tsk->sent_unacked = 0; |
4f4482dc | 368 | atomic_set(&tsk->dupl_rcvcnt, 0); |
7ef43eba | 369 | |
0c3141e9 | 370 | if (sock->state == SS_READY) { |
301bae56 | 371 | tsk_set_unreturnable(tsk, true); |
0c3141e9 | 372 | if (sock->type == SOCK_DGRAM) |
301bae56 | 373 | tsk_set_unreliable(tsk, true); |
0c3141e9 | 374 | } |
b97bf3fd PL |
375 | return 0; |
376 | } | |
377 | ||
c5fa7b3c YX |
378 | /** |
379 | * tipc_sock_create_local - create TIPC socket from inside TIPC module | |
380 | * @type: socket type - SOCK_RDM or SOCK_SEQPACKET | |
381 | * | |
382 | * We cannot use sock_creat_kern here because it bumps module user count. | |
383 | * Since socket owner and creator is the same module we must make sure | |
384 | * that module count remains zero for module local sockets, otherwise | |
385 | * we cannot do rmmod. | |
386 | * | |
387 | * Returns 0 on success, errno otherwise | |
388 | */ | |
389 | int tipc_sock_create_local(int type, struct socket **res) | |
390 | { | |
391 | int rc; | |
c5fa7b3c YX |
392 | |
393 | rc = sock_create_lite(AF_TIPC, type, 0, res); | |
394 | if (rc < 0) { | |
395 | pr_err("Failed to create kernel socket\n"); | |
396 | return rc; | |
397 | } | |
398 | tipc_sk_create(&init_net, *res, 0, 1); | |
399 | ||
c5fa7b3c YX |
400 | return 0; |
401 | } | |
402 | ||
403 | /** | |
404 | * tipc_sock_release_local - release socket created by tipc_sock_create_local | |
405 | * @sock: the socket to be released. | |
406 | * | |
407 | * Module reference count is not incremented when such sockets are created, | |
408 | * so we must keep it from being decremented when they are released. | |
409 | */ | |
410 | void tipc_sock_release_local(struct socket *sock) | |
411 | { | |
247f0f3c | 412 | tipc_release(sock); |
c5fa7b3c YX |
413 | sock->ops = NULL; |
414 | sock_release(sock); | |
415 | } | |
416 | ||
417 | /** | |
418 | * tipc_sock_accept_local - accept a connection on a socket created | |
419 | * with tipc_sock_create_local. Use this function to avoid that | |
420 | * module reference count is inadvertently incremented. | |
421 | * | |
422 | * @sock: the accepting socket | |
423 | * @newsock: reference to the new socket to be created | |
424 | * @flags: socket flags | |
425 | */ | |
426 | ||
427 | int tipc_sock_accept_local(struct socket *sock, struct socket **newsock, | |
ae8509c4 | 428 | int flags) |
c5fa7b3c YX |
429 | { |
430 | struct sock *sk = sock->sk; | |
431 | int ret; | |
432 | ||
433 | ret = sock_create_lite(sk->sk_family, sk->sk_type, | |
434 | sk->sk_protocol, newsock); | |
435 | if (ret < 0) | |
436 | return ret; | |
437 | ||
247f0f3c | 438 | ret = tipc_accept(sock, *newsock, flags); |
c5fa7b3c YX |
439 | if (ret < 0) { |
440 | sock_release(*newsock); | |
441 | return ret; | |
442 | } | |
443 | (*newsock)->ops = sock->ops; | |
444 | return ret; | |
445 | } | |
446 | ||
07f6c4bc YX |
447 | static void tipc_sk_callback(struct rcu_head *head) |
448 | { | |
449 | struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu); | |
450 | ||
451 | sock_put(&tsk->sk); | |
452 | } | |
453 | ||
b97bf3fd | 454 | /** |
247f0f3c | 455 | * tipc_release - destroy a TIPC socket |
b97bf3fd PL |
456 | * @sock: socket to destroy |
457 | * | |
458 | * This routine cleans up any messages that are still queued on the socket. | |
459 | * For DGRAM and RDM socket types, all queued messages are rejected. | |
460 | * For SEQPACKET and STREAM socket types, the first message is rejected | |
461 | * and any others are discarded. (If the first message on a STREAM socket | |
462 | * is partially-read, it is discarded and the next one is rejected instead.) | |
c4307285 | 463 | * |
b97bf3fd PL |
464 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
465 | * are returned or discarded according to the "destination droppable" setting | |
466 | * specified for the message by the sender. | |
467 | * | |
468 | * Returns 0 on success, errno otherwise | |
469 | */ | |
247f0f3c | 470 | static int tipc_release(struct socket *sock) |
b97bf3fd | 471 | { |
b97bf3fd | 472 | struct sock *sk = sock->sk; |
f2f9800d | 473 | struct net *net = sock_net(sk); |
58ed9442 | 474 | struct tipc_sock *tsk; |
a6ca1094 | 475 | struct sk_buff *skb; |
f2f2a96a | 476 | u32 dnode, probing_state; |
b97bf3fd | 477 | |
0c3141e9 AS |
478 | /* |
479 | * Exit if socket isn't fully initialized (occurs when a failed accept() | |
480 | * releases a pre-allocated child socket that was never used) | |
481 | */ | |
0c3141e9 | 482 | if (sk == NULL) |
b97bf3fd | 483 | return 0; |
c4307285 | 484 | |
58ed9442 | 485 | tsk = tipc_sk(sk); |
0c3141e9 AS |
486 | lock_sock(sk); |
487 | ||
488 | /* | |
489 | * Reject all unreceived messages, except on an active connection | |
490 | * (which disconnects locally & sends a 'FIN+' to peer) | |
491 | */ | |
301bae56 | 492 | dnode = tsk_peer_node(tsk); |
b97bf3fd | 493 | while (sock->state != SS_DISCONNECTING) { |
a6ca1094 YX |
494 | skb = __skb_dequeue(&sk->sk_receive_queue); |
495 | if (skb == NULL) | |
b97bf3fd | 496 | break; |
a6ca1094 YX |
497 | if (TIPC_SKB_CB(skb)->handle != NULL) |
498 | kfree_skb(skb); | |
0c3141e9 AS |
499 | else { |
500 | if ((sock->state == SS_CONNECTING) || | |
501 | (sock->state == SS_CONNECTED)) { | |
502 | sock->state = SS_DISCONNECTING; | |
301bae56 | 503 | tsk->connected = 0; |
f2f9800d | 504 | tipc_node_remove_conn(net, dnode, tsk->portid); |
0c3141e9 | 505 | } |
a6ca1094 | 506 | if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT)) |
f2f9800d | 507 | tipc_link_xmit_skb(net, skb, dnode, 0); |
0c3141e9 | 508 | } |
b97bf3fd PL |
509 | } |
510 | ||
301bae56 | 511 | tipc_sk_withdraw(tsk, 0, NULL); |
f2f2a96a YX |
512 | probing_state = tsk->probing_state; |
513 | if (del_timer_sync(&tsk->timer) && probing_state != TIPC_CONN_PROBING) | |
514 | sock_put(sk); | |
07f6c4bc | 515 | tipc_sk_remove(tsk); |
301bae56 | 516 | if (tsk->connected) { |
a6ca1094 | 517 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
5b8fa7ce | 518 | SHORT_H_SIZE, 0, dnode, tipc_own_addr, |
301bae56 | 519 | tsk_peer_port(tsk), |
07f6c4bc | 520 | tsk->portid, TIPC_ERR_NO_PORT); |
a6ca1094 | 521 | if (skb) |
f2f9800d YX |
522 | tipc_link_xmit_skb(net, skb, dnode, tsk->portid); |
523 | tipc_node_remove_conn(net, dnode, tsk->portid); | |
5b8fa7ce | 524 | } |
b97bf3fd | 525 | |
0c3141e9 | 526 | /* Discard any remaining (connection-based) messages in receive queue */ |
57467e56 | 527 | __skb_queue_purge(&sk->sk_receive_queue); |
b97bf3fd | 528 | |
0c3141e9 | 529 | /* Reject any messages that accumulated in backlog queue */ |
0c3141e9 AS |
530 | sock->state = SS_DISCONNECTING; |
531 | release_sock(sk); | |
07f6c4bc YX |
532 | |
533 | call_rcu(&tsk->rcu, tipc_sk_callback); | |
0c3141e9 | 534 | sock->sk = NULL; |
b97bf3fd | 535 | |
065d7e39 | 536 | return 0; |
b97bf3fd PL |
537 | } |
538 | ||
539 | /** | |
247f0f3c | 540 | * tipc_bind - associate or disassocate TIPC name(s) with a socket |
b97bf3fd PL |
541 | * @sock: socket structure |
542 | * @uaddr: socket address describing name(s) and desired operation | |
543 | * @uaddr_len: size of socket address data structure | |
c4307285 | 544 | * |
b97bf3fd PL |
545 | * Name and name sequence binding is indicated using a positive scope value; |
546 | * a negative scope value unbinds the specified name. Specifying no name | |
547 | * (i.e. a socket address length of 0) unbinds all names from the socket. | |
c4307285 | 548 | * |
b97bf3fd | 549 | * Returns 0 on success, errno otherwise |
0c3141e9 AS |
550 | * |
551 | * NOTE: This routine doesn't need to take the socket lock since it doesn't | |
552 | * access any non-constant socket information. | |
b97bf3fd | 553 | */ |
247f0f3c YX |
554 | static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, |
555 | int uaddr_len) | |
b97bf3fd | 556 | { |
84602761 | 557 | struct sock *sk = sock->sk; |
b97bf3fd | 558 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
58ed9442 | 559 | struct tipc_sock *tsk = tipc_sk(sk); |
84602761 | 560 | int res = -EINVAL; |
b97bf3fd | 561 | |
84602761 YX |
562 | lock_sock(sk); |
563 | if (unlikely(!uaddr_len)) { | |
301bae56 | 564 | res = tipc_sk_withdraw(tsk, 0, NULL); |
84602761 YX |
565 | goto exit; |
566 | } | |
c4307285 | 567 | |
84602761 YX |
568 | if (uaddr_len < sizeof(struct sockaddr_tipc)) { |
569 | res = -EINVAL; | |
570 | goto exit; | |
571 | } | |
572 | if (addr->family != AF_TIPC) { | |
573 | res = -EAFNOSUPPORT; | |
574 | goto exit; | |
575 | } | |
b97bf3fd | 576 | |
b97bf3fd PL |
577 | if (addr->addrtype == TIPC_ADDR_NAME) |
578 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; | |
84602761 YX |
579 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { |
580 | res = -EAFNOSUPPORT; | |
581 | goto exit; | |
582 | } | |
c4307285 | 583 | |
13a2e898 | 584 | if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && |
7d0ab17b | 585 | (addr->addr.nameseq.type != TIPC_TOP_SRV) && |
84602761 YX |
586 | (addr->addr.nameseq.type != TIPC_CFG_SRV)) { |
587 | res = -EACCES; | |
588 | goto exit; | |
589 | } | |
c422f1bd | 590 | |
84602761 | 591 | res = (addr->scope > 0) ? |
301bae56 JPM |
592 | tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) : |
593 | tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq); | |
84602761 YX |
594 | exit: |
595 | release_sock(sk); | |
596 | return res; | |
b97bf3fd PL |
597 | } |
598 | ||
c4307285 | 599 | /** |
247f0f3c | 600 | * tipc_getname - get port ID of socket or peer socket |
b97bf3fd PL |
601 | * @sock: socket structure |
602 | * @uaddr: area for returned socket address | |
603 | * @uaddr_len: area for returned length of socket address | |
2da59918 | 604 | * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID |
c4307285 | 605 | * |
b97bf3fd | 606 | * Returns 0 on success, errno otherwise |
0c3141e9 | 607 | * |
2da59918 AS |
608 | * NOTE: This routine doesn't need to take the socket lock since it only |
609 | * accesses socket information that is unchanging (or which changes in | |
0e65967e | 610 | * a completely predictable manner). |
b97bf3fd | 611 | */ |
247f0f3c YX |
612 | static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, |
613 | int *uaddr_len, int peer) | |
b97bf3fd | 614 | { |
b97bf3fd | 615 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
58ed9442 | 616 | struct tipc_sock *tsk = tipc_sk(sock->sk); |
b97bf3fd | 617 | |
88f8a5e3 | 618 | memset(addr, 0, sizeof(*addr)); |
0c3141e9 | 619 | if (peer) { |
2da59918 AS |
620 | if ((sock->state != SS_CONNECTED) && |
621 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) | |
622 | return -ENOTCONN; | |
301bae56 JPM |
623 | addr->addr.id.ref = tsk_peer_port(tsk); |
624 | addr->addr.id.node = tsk_peer_node(tsk); | |
0c3141e9 | 625 | } else { |
07f6c4bc | 626 | addr->addr.id.ref = tsk->portid; |
b924dcf0 | 627 | addr->addr.id.node = tipc_own_addr; |
0c3141e9 | 628 | } |
b97bf3fd PL |
629 | |
630 | *uaddr_len = sizeof(*addr); | |
631 | addr->addrtype = TIPC_ADDR_ID; | |
632 | addr->family = AF_TIPC; | |
633 | addr->scope = 0; | |
b97bf3fd PL |
634 | addr->addr.name.domain = 0; |
635 | ||
0c3141e9 | 636 | return 0; |
b97bf3fd PL |
637 | } |
638 | ||
639 | /** | |
247f0f3c | 640 | * tipc_poll - read and possibly block on pollmask |
b97bf3fd PL |
641 | * @file: file structure associated with the socket |
642 | * @sock: socket for which to calculate the poll bits | |
643 | * @wait: ??? | |
644 | * | |
9b674e82 AS |
645 | * Returns pollmask value |
646 | * | |
647 | * COMMENTARY: | |
648 | * It appears that the usual socket locking mechanisms are not useful here | |
649 | * since the pollmask info is potentially out-of-date the moment this routine | |
650 | * exits. TCP and other protocols seem to rely on higher level poll routines | |
651 | * to handle any preventable race conditions, so TIPC will do the same ... | |
652 | * | |
653 | * TIPC sets the returned events as follows: | |
f662c070 AS |
654 | * |
655 | * socket state flags set | |
656 | * ------------ --------- | |
657 | * unconnected no read flags | |
c4fc298a | 658 | * POLLOUT if port is not congested |
f662c070 AS |
659 | * |
660 | * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue | |
661 | * no write flags | |
662 | * | |
663 | * connected POLLIN/POLLRDNORM if data in rx queue | |
664 | * POLLOUT if port is not congested | |
665 | * | |
666 | * disconnecting POLLIN/POLLRDNORM/POLLHUP | |
667 | * no write flags | |
668 | * | |
669 | * listening POLLIN if SYN in rx queue | |
670 | * no write flags | |
671 | * | |
672 | * ready POLLIN/POLLRDNORM if data in rx queue | |
673 | * [connectionless] POLLOUT (since port cannot be congested) | |
674 | * | |
675 | * IMPORTANT: The fact that a read or write operation is indicated does NOT | |
676 | * imply that the operation will succeed, merely that it should be performed | |
677 | * and will not block. | |
b97bf3fd | 678 | */ |
247f0f3c YX |
679 | static unsigned int tipc_poll(struct file *file, struct socket *sock, |
680 | poll_table *wait) | |
b97bf3fd | 681 | { |
9b674e82 | 682 | struct sock *sk = sock->sk; |
58ed9442 | 683 | struct tipc_sock *tsk = tipc_sk(sk); |
f662c070 | 684 | u32 mask = 0; |
9b674e82 | 685 | |
f288bef4 | 686 | sock_poll_wait(file, sk_sleep(sk), wait); |
9b674e82 | 687 | |
f662c070 | 688 | switch ((int)sock->state) { |
c4fc298a | 689 | case SS_UNCONNECTED: |
60120526 | 690 | if (!tsk->link_cong) |
c4fc298a EH |
691 | mask |= POLLOUT; |
692 | break; | |
f662c070 AS |
693 | case SS_READY: |
694 | case SS_CONNECTED: | |
301bae56 | 695 | if (!tsk->link_cong && !tsk_conn_cong(tsk)) |
f662c070 AS |
696 | mask |= POLLOUT; |
697 | /* fall thru' */ | |
698 | case SS_CONNECTING: | |
699 | case SS_LISTENING: | |
700 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
701 | mask |= (POLLIN | POLLRDNORM); | |
702 | break; | |
703 | case SS_DISCONNECTING: | |
704 | mask = (POLLIN | POLLRDNORM | POLLHUP); | |
705 | break; | |
706 | } | |
9b674e82 AS |
707 | |
708 | return mask; | |
b97bf3fd PL |
709 | } |
710 | ||
0abd8ff2 JPM |
711 | /** |
712 | * tipc_sendmcast - send multicast message | |
713 | * @sock: socket structure | |
714 | * @seq: destination address | |
562640f3 | 715 | * @msg: message to send |
0abd8ff2 JPM |
716 | * @dsz: total length of message data |
717 | * @timeo: timeout to wait for wakeup | |
718 | * | |
719 | * Called from function tipc_sendmsg(), which has done all sanity checks | |
720 | * Returns the number of bytes sent on success, or errno | |
721 | */ | |
722 | static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, | |
562640f3 | 723 | struct msghdr *msg, size_t dsz, long timeo) |
0abd8ff2 JPM |
724 | { |
725 | struct sock *sk = sock->sk; | |
f2f9800d | 726 | struct net *net = sock_net(sk); |
301bae56 | 727 | struct tipc_msg *mhdr = &tipc_sk(sk)->phdr; |
a6ca1094 | 728 | struct sk_buff_head head; |
0abd8ff2 JPM |
729 | uint mtu; |
730 | int rc; | |
731 | ||
732 | msg_set_type(mhdr, TIPC_MCAST_MSG); | |
733 | msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE); | |
734 | msg_set_destport(mhdr, 0); | |
735 | msg_set_destnode(mhdr, 0); | |
736 | msg_set_nametype(mhdr, seq->type); | |
737 | msg_set_namelower(mhdr, seq->lower); | |
738 | msg_set_nameupper(mhdr, seq->upper); | |
739 | msg_set_hdr_sz(mhdr, MCAST_H_SIZE); | |
740 | ||
741 | new_mtu: | |
742 | mtu = tipc_bclink_get_mtu(); | |
a6ca1094 YX |
743 | __skb_queue_head_init(&head); |
744 | rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head); | |
0abd8ff2 JPM |
745 | if (unlikely(rc < 0)) |
746 | return rc; | |
747 | ||
748 | do { | |
f2f9800d | 749 | rc = tipc_bclink_xmit(net, &head); |
0abd8ff2 JPM |
750 | if (likely(rc >= 0)) { |
751 | rc = dsz; | |
752 | break; | |
753 | } | |
754 | if (rc == -EMSGSIZE) | |
755 | goto new_mtu; | |
756 | if (rc != -ELINKCONG) | |
757 | break; | |
50100a5e | 758 | tipc_sk(sk)->link_cong = 1; |
0abd8ff2 JPM |
759 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
760 | if (rc) | |
a6ca1094 | 761 | __skb_queue_purge(&head); |
0abd8ff2 JPM |
762 | } while (!rc); |
763 | return rc; | |
764 | } | |
765 | ||
078bec82 JPM |
766 | /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets |
767 | */ | |
f2f9800d | 768 | void tipc_sk_mcast_rcv(struct net *net, struct sk_buff *buf) |
078bec82 JPM |
769 | { |
770 | struct tipc_msg *msg = buf_msg(buf); | |
771 | struct tipc_port_list dports = {0, NULL, }; | |
772 | struct tipc_port_list *item; | |
773 | struct sk_buff *b; | |
774 | uint i, last, dst = 0; | |
775 | u32 scope = TIPC_CLUSTER_SCOPE; | |
776 | ||
777 | if (in_own_node(msg_orignode(msg))) | |
778 | scope = TIPC_NODE_SCOPE; | |
779 | ||
780 | /* Create destination port list: */ | |
781 | tipc_nametbl_mc_translate(msg_nametype(msg), | |
782 | msg_namelower(msg), | |
783 | msg_nameupper(msg), | |
784 | scope, | |
785 | &dports); | |
786 | last = dports.count; | |
787 | if (!last) { | |
788 | kfree_skb(buf); | |
789 | return; | |
790 | } | |
791 | ||
792 | for (item = &dports; item; item = item->next) { | |
793 | for (i = 0; i < PLSIZE && ++dst <= last; i++) { | |
794 | b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf; | |
795 | if (!b) { | |
796 | pr_warn("Failed do clone mcast rcv buffer\n"); | |
797 | continue; | |
798 | } | |
799 | msg_set_destport(msg, item->ports[i]); | |
f2f9800d | 800 | tipc_sk_rcv(net, b); |
078bec82 JPM |
801 | } |
802 | } | |
803 | tipc_port_list_free(&dports); | |
804 | } | |
805 | ||
ac0074ee JPM |
806 | /** |
807 | * tipc_sk_proto_rcv - receive a connection mng protocol message | |
808 | * @tsk: receiving socket | |
809 | * @dnode: node to send response message to, if any | |
810 | * @buf: buffer containing protocol message | |
811 | * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if | |
812 | * (CONN_PROBE_REPLY) message should be forwarded. | |
813 | */ | |
52f50ce5 WY |
814 | static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, |
815 | struct sk_buff *buf) | |
ac0074ee JPM |
816 | { |
817 | struct tipc_msg *msg = buf_msg(buf); | |
60120526 | 818 | int conn_cong; |
ac0074ee JPM |
819 | |
820 | /* Ignore if connection cannot be validated: */ | |
2e84c60b | 821 | if (!tsk_peer_msg(tsk, msg)) |
ac0074ee JPM |
822 | goto exit; |
823 | ||
301bae56 | 824 | tsk->probing_state = TIPC_CONN_OK; |
ac0074ee JPM |
825 | |
826 | if (msg_type(msg) == CONN_ACK) { | |
301bae56 | 827 | conn_cong = tsk_conn_cong(tsk); |
60120526 JPM |
828 | tsk->sent_unacked -= msg_msgcnt(msg); |
829 | if (conn_cong) | |
50100a5e | 830 | tsk->sk.sk_write_space(&tsk->sk); |
ac0074ee JPM |
831 | } else if (msg_type(msg) == CONN_PROBE) { |
832 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) | |
833 | return TIPC_OK; | |
834 | msg_set_type(msg, CONN_PROBE_REPLY); | |
835 | return TIPC_FWD_MSG; | |
836 | } | |
837 | /* Do nothing if msg_type() == CONN_PROBE_REPLY */ | |
838 | exit: | |
839 | kfree_skb(buf); | |
840 | return TIPC_OK; | |
841 | } | |
842 | ||
3f40504f YX |
843 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) |
844 | { | |
845 | struct sock *sk = sock->sk; | |
58ed9442 | 846 | struct tipc_sock *tsk = tipc_sk(sk); |
3f40504f YX |
847 | DEFINE_WAIT(wait); |
848 | int done; | |
849 | ||
850 | do { | |
851 | int err = sock_error(sk); | |
852 | if (err) | |
853 | return err; | |
854 | if (sock->state == SS_DISCONNECTING) | |
855 | return -EPIPE; | |
856 | if (!*timeo_p) | |
857 | return -EAGAIN; | |
858 | if (signal_pending(current)) | |
859 | return sock_intr_errno(*timeo_p); | |
860 | ||
861 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
60120526 | 862 | done = sk_wait_event(sk, timeo_p, !tsk->link_cong); |
3f40504f YX |
863 | finish_wait(sk_sleep(sk), &wait); |
864 | } while (!done); | |
865 | return 0; | |
866 | } | |
867 | ||
b97bf3fd | 868 | /** |
247f0f3c | 869 | * tipc_sendmsg - send message in connectionless manner |
0c3141e9 | 870 | * @iocb: if NULL, indicates that socket lock is already held |
b97bf3fd PL |
871 | * @sock: socket structure |
872 | * @m: message to send | |
e2dafe87 | 873 | * @dsz: amount of user data to be sent |
c4307285 | 874 | * |
b97bf3fd | 875 | * Message must have an destination specified explicitly. |
c4307285 | 876 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
b97bf3fd PL |
877 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
878 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) | |
c4307285 | 879 | * |
b97bf3fd PL |
880 | * Returns the number of bytes sent on success, or errno otherwise |
881 | */ | |
247f0f3c | 882 | static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, |
e2dafe87 | 883 | struct msghdr *m, size_t dsz) |
b97bf3fd | 884 | { |
e2dafe87 | 885 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
0c3141e9 | 886 | struct sock *sk = sock->sk; |
58ed9442 | 887 | struct tipc_sock *tsk = tipc_sk(sk); |
f2f9800d | 888 | struct net *net = sock_net(sk); |
301bae56 | 889 | struct tipc_msg *mhdr = &tsk->phdr; |
e2dafe87 | 890 | u32 dnode, dport; |
a6ca1094 YX |
891 | struct sk_buff_head head; |
892 | struct sk_buff *skb; | |
e2dafe87 JPM |
893 | struct tipc_name_seq *seq = &dest->addr.nameseq; |
894 | u32 mtu; | |
3f40504f | 895 | long timeo; |
88b17b6a | 896 | int rc; |
b97bf3fd PL |
897 | |
898 | if (unlikely(!dest)) | |
899 | return -EDESTADDRREQ; | |
e2dafe87 | 900 | |
51f9cc1f AS |
901 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
902 | (dest->family != AF_TIPC))) | |
b97bf3fd | 903 | return -EINVAL; |
e2dafe87 JPM |
904 | |
905 | if (dsz > TIPC_MAX_USER_MSG_SIZE) | |
c29c3f70 | 906 | return -EMSGSIZE; |
b97bf3fd | 907 | |
0c3141e9 AS |
908 | if (iocb) |
909 | lock_sock(sk); | |
910 | ||
e2dafe87 | 911 | if (unlikely(sock->state != SS_READY)) { |
0c3141e9 | 912 | if (sock->state == SS_LISTENING) { |
e2dafe87 | 913 | rc = -EPIPE; |
0c3141e9 AS |
914 | goto exit; |
915 | } | |
916 | if (sock->state != SS_UNCONNECTED) { | |
e2dafe87 | 917 | rc = -EISCONN; |
0c3141e9 AS |
918 | goto exit; |
919 | } | |
301bae56 | 920 | if (tsk->published) { |
e2dafe87 | 921 | rc = -EOPNOTSUPP; |
0c3141e9 AS |
922 | goto exit; |
923 | } | |
3388007b | 924 | if (dest->addrtype == TIPC_ADDR_NAME) { |
301bae56 JPM |
925 | tsk->conn_type = dest->addr.name.name.type; |
926 | tsk->conn_instance = dest->addr.name.name.instance; | |
3388007b | 927 | } |
b97bf3fd PL |
928 | } |
929 | ||
3f40504f | 930 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
e2dafe87 JPM |
931 | |
932 | if (dest->addrtype == TIPC_ADDR_MCAST) { | |
562640f3 | 933 | rc = tipc_sendmcast(sock, seq, m, dsz, timeo); |
e2dafe87 JPM |
934 | goto exit; |
935 | } else if (dest->addrtype == TIPC_ADDR_NAME) { | |
936 | u32 type = dest->addr.name.name.type; | |
937 | u32 inst = dest->addr.name.name.instance; | |
938 | u32 domain = dest->addr.name.domain; | |
939 | ||
940 | dnode = domain; | |
941 | msg_set_type(mhdr, TIPC_NAMED_MSG); | |
942 | msg_set_hdr_sz(mhdr, NAMED_H_SIZE); | |
943 | msg_set_nametype(mhdr, type); | |
944 | msg_set_nameinst(mhdr, inst); | |
945 | msg_set_lookup_scope(mhdr, tipc_addr_scope(domain)); | |
946 | dport = tipc_nametbl_translate(type, inst, &dnode); | |
947 | msg_set_destnode(mhdr, dnode); | |
948 | msg_set_destport(mhdr, dport); | |
949 | if (unlikely(!dport && !dnode)) { | |
950 | rc = -EHOSTUNREACH; | |
951 | goto exit; | |
c4307285 | 952 | } |
e2dafe87 JPM |
953 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
954 | dnode = dest->addr.id.node; | |
955 | msg_set_type(mhdr, TIPC_DIRECT_MSG); | |
956 | msg_set_lookup_scope(mhdr, 0); | |
957 | msg_set_destnode(mhdr, dnode); | |
958 | msg_set_destport(mhdr, dest->addr.id.ref); | |
959 | msg_set_hdr_sz(mhdr, BASIC_H_SIZE); | |
960 | } | |
961 | ||
962 | new_mtu: | |
f2f9800d | 963 | mtu = tipc_node_get_mtu(net, dnode, tsk->portid); |
a6ca1094 YX |
964 | __skb_queue_head_init(&head); |
965 | rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head); | |
e2dafe87 JPM |
966 | if (rc < 0) |
967 | goto exit; | |
968 | ||
969 | do { | |
a6ca1094 YX |
970 | skb = skb_peek(&head); |
971 | TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong; | |
f2f9800d | 972 | rc = tipc_link_xmit(net, &head, dnode, tsk->portid); |
e2dafe87 JPM |
973 | if (likely(rc >= 0)) { |
974 | if (sock->state != SS_READY) | |
0c3141e9 | 975 | sock->state = SS_CONNECTING; |
e2dafe87 | 976 | rc = dsz; |
0c3141e9 | 977 | break; |
c4307285 | 978 | } |
e2dafe87 JPM |
979 | if (rc == -EMSGSIZE) |
980 | goto new_mtu; | |
e2dafe87 | 981 | if (rc != -ELINKCONG) |
0c3141e9 | 982 | break; |
50100a5e | 983 | tsk->link_cong = 1; |
e2dafe87 | 984 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
70452dcb | 985 | if (rc) |
a6ca1094 | 986 | __skb_queue_purge(&head); |
e2dafe87 | 987 | } while (!rc); |
0c3141e9 AS |
988 | exit: |
989 | if (iocb) | |
990 | release_sock(sk); | |
e2dafe87 JPM |
991 | |
992 | return rc; | |
b97bf3fd PL |
993 | } |
994 | ||
391a6dd1 YX |
995 | static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) |
996 | { | |
997 | struct sock *sk = sock->sk; | |
58ed9442 | 998 | struct tipc_sock *tsk = tipc_sk(sk); |
391a6dd1 YX |
999 | DEFINE_WAIT(wait); |
1000 | int done; | |
1001 | ||
1002 | do { | |
1003 | int err = sock_error(sk); | |
1004 | if (err) | |
1005 | return err; | |
1006 | if (sock->state == SS_DISCONNECTING) | |
1007 | return -EPIPE; | |
1008 | else if (sock->state != SS_CONNECTED) | |
1009 | return -ENOTCONN; | |
1010 | if (!*timeo_p) | |
1011 | return -EAGAIN; | |
1012 | if (signal_pending(current)) | |
1013 | return sock_intr_errno(*timeo_p); | |
1014 | ||
1015 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1016 | done = sk_wait_event(sk, timeo_p, | |
60120526 | 1017 | (!tsk->link_cong && |
301bae56 JPM |
1018 | !tsk_conn_cong(tsk)) || |
1019 | !tsk->connected); | |
391a6dd1 YX |
1020 | finish_wait(sk_sleep(sk), &wait); |
1021 | } while (!done); | |
1022 | return 0; | |
1023 | } | |
1024 | ||
c4307285 | 1025 | /** |
4ccfe5e0 JPM |
1026 | * tipc_send_stream - send stream-oriented data |
1027 | * @iocb: (unused) | |
b97bf3fd | 1028 | * @sock: socket structure |
4ccfe5e0 JPM |
1029 | * @m: data to send |
1030 | * @dsz: total length of data to be transmitted | |
c4307285 | 1031 | * |
4ccfe5e0 | 1032 | * Used for SOCK_STREAM data. |
c4307285 | 1033 | * |
4ccfe5e0 JPM |
1034 | * Returns the number of bytes sent on success (or partial success), |
1035 | * or errno if no data sent | |
b97bf3fd | 1036 | */ |
4ccfe5e0 JPM |
1037 | static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, |
1038 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 1039 | { |
0c3141e9 | 1040 | struct sock *sk = sock->sk; |
f2f9800d | 1041 | struct net *net = sock_net(sk); |
58ed9442 | 1042 | struct tipc_sock *tsk = tipc_sk(sk); |
301bae56 | 1043 | struct tipc_msg *mhdr = &tsk->phdr; |
a6ca1094 | 1044 | struct sk_buff_head head; |
342dfc30 | 1045 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
07f6c4bc | 1046 | u32 portid = tsk->portid; |
4ccfe5e0 | 1047 | int rc = -EINVAL; |
391a6dd1 | 1048 | long timeo; |
4ccfe5e0 JPM |
1049 | u32 dnode; |
1050 | uint mtu, send, sent = 0; | |
b97bf3fd PL |
1051 | |
1052 | /* Handle implied connection establishment */ | |
4ccfe5e0 JPM |
1053 | if (unlikely(dest)) { |
1054 | rc = tipc_sendmsg(iocb, sock, m, dsz); | |
1055 | if (dsz && (dsz == rc)) | |
60120526 | 1056 | tsk->sent_unacked = 1; |
4ccfe5e0 JPM |
1057 | return rc; |
1058 | } | |
1059 | if (dsz > (uint)INT_MAX) | |
c29c3f70 AS |
1060 | return -EMSGSIZE; |
1061 | ||
0c3141e9 AS |
1062 | if (iocb) |
1063 | lock_sock(sk); | |
b97bf3fd | 1064 | |
391a6dd1 YX |
1065 | if (unlikely(sock->state != SS_CONNECTED)) { |
1066 | if (sock->state == SS_DISCONNECTING) | |
4ccfe5e0 | 1067 | rc = -EPIPE; |
391a6dd1 | 1068 | else |
4ccfe5e0 | 1069 | rc = -ENOTCONN; |
391a6dd1 YX |
1070 | goto exit; |
1071 | } | |
1d835874 | 1072 | |
391a6dd1 | 1073 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
301bae56 | 1074 | dnode = tsk_peer_node(tsk); |
4ccfe5e0 JPM |
1075 | |
1076 | next: | |
301bae56 | 1077 | mtu = tsk->max_pkt; |
4ccfe5e0 | 1078 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); |
a6ca1094 YX |
1079 | __skb_queue_head_init(&head); |
1080 | rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head); | |
4ccfe5e0 JPM |
1081 | if (unlikely(rc < 0)) |
1082 | goto exit; | |
c4307285 | 1083 | do { |
301bae56 | 1084 | if (likely(!tsk_conn_cong(tsk))) { |
f2f9800d | 1085 | rc = tipc_link_xmit(net, &head, dnode, portid); |
4ccfe5e0 | 1086 | if (likely(!rc)) { |
60120526 | 1087 | tsk->sent_unacked++; |
4ccfe5e0 JPM |
1088 | sent += send; |
1089 | if (sent == dsz) | |
1090 | break; | |
1091 | goto next; | |
1092 | } | |
1093 | if (rc == -EMSGSIZE) { | |
f2f9800d YX |
1094 | tsk->max_pkt = tipc_node_get_mtu(net, dnode, |
1095 | portid); | |
4ccfe5e0 JPM |
1096 | goto next; |
1097 | } | |
1098 | if (rc != -ELINKCONG) | |
1099 | break; | |
50100a5e | 1100 | tsk->link_cong = 1; |
4ccfe5e0 JPM |
1101 | } |
1102 | rc = tipc_wait_for_sndpkt(sock, &timeo); | |
70452dcb | 1103 | if (rc) |
a6ca1094 | 1104 | __skb_queue_purge(&head); |
4ccfe5e0 | 1105 | } while (!rc); |
391a6dd1 | 1106 | exit: |
0c3141e9 AS |
1107 | if (iocb) |
1108 | release_sock(sk); | |
4ccfe5e0 | 1109 | return sent ? sent : rc; |
b97bf3fd PL |
1110 | } |
1111 | ||
c4307285 | 1112 | /** |
4ccfe5e0 JPM |
1113 | * tipc_send_packet - send a connection-oriented message |
1114 | * @iocb: if NULL, indicates that socket lock is already held | |
b97bf3fd | 1115 | * @sock: socket structure |
4ccfe5e0 JPM |
1116 | * @m: message to send |
1117 | * @dsz: length of data to be transmitted | |
c4307285 | 1118 | * |
4ccfe5e0 | 1119 | * Used for SOCK_SEQPACKET messages. |
c4307285 | 1120 | * |
4ccfe5e0 | 1121 | * Returns the number of bytes sent on success, or errno otherwise |
b97bf3fd | 1122 | */ |
4ccfe5e0 JPM |
1123 | static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, |
1124 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 1125 | { |
4ccfe5e0 JPM |
1126 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
1127 | return -EMSGSIZE; | |
b97bf3fd | 1128 | |
4ccfe5e0 | 1129 | return tipc_send_stream(iocb, sock, m, dsz); |
b97bf3fd PL |
1130 | } |
1131 | ||
dadebc00 | 1132 | /* tipc_sk_finish_conn - complete the setup of a connection |
b97bf3fd | 1133 | */ |
301bae56 | 1134 | static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, |
dadebc00 | 1135 | u32 peer_node) |
b97bf3fd | 1136 | { |
f2f9800d | 1137 | struct net *net = sock_net(&tsk->sk); |
301bae56 | 1138 | struct tipc_msg *msg = &tsk->phdr; |
b97bf3fd | 1139 | |
dadebc00 JPM |
1140 | msg_set_destnode(msg, peer_node); |
1141 | msg_set_destport(msg, peer_port); | |
1142 | msg_set_type(msg, TIPC_CONN_MSG); | |
1143 | msg_set_lookup_scope(msg, 0); | |
1144 | msg_set_hdr_sz(msg, SHORT_H_SIZE); | |
584d24b3 | 1145 | |
2f55c437 | 1146 | tsk->probing_intv = CONN_PROBING_INTERVAL; |
301bae56 JPM |
1147 | tsk->probing_state = TIPC_CONN_OK; |
1148 | tsk->connected = 1; | |
f2f2a96a YX |
1149 | if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv)) |
1150 | sock_hold(&tsk->sk); | |
f2f9800d YX |
1151 | tipc_node_add_conn(net, peer_node, tsk->portid, peer_port); |
1152 | tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid); | |
b97bf3fd PL |
1153 | } |
1154 | ||
1155 | /** | |
1156 | * set_orig_addr - capture sender's address for received message | |
1157 | * @m: descriptor for message info | |
1158 | * @msg: received message header | |
c4307285 | 1159 | * |
b97bf3fd PL |
1160 | * Note: Address is not captured if not requested by receiver. |
1161 | */ | |
05790c64 | 1162 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
b97bf3fd | 1163 | { |
342dfc30 | 1164 | DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); |
b97bf3fd | 1165 | |
c4307285 | 1166 | if (addr) { |
b97bf3fd PL |
1167 | addr->family = AF_TIPC; |
1168 | addr->addrtype = TIPC_ADDR_ID; | |
60085c3d | 1169 | memset(&addr->addr, 0, sizeof(addr->addr)); |
b97bf3fd PL |
1170 | addr->addr.id.ref = msg_origport(msg); |
1171 | addr->addr.id.node = msg_orignode(msg); | |
0e65967e AS |
1172 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
1173 | addr->scope = 0; /* could leave uninitialized */ | |
b97bf3fd PL |
1174 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
1175 | } | |
1176 | } | |
1177 | ||
1178 | /** | |
301bae56 | 1179 | * tipc_sk_anc_data_recv - optionally capture ancillary data for received message |
b97bf3fd PL |
1180 | * @m: descriptor for message info |
1181 | * @msg: received message header | |
301bae56 | 1182 | * @tsk: TIPC port associated with message |
c4307285 | 1183 | * |
b97bf3fd | 1184 | * Note: Ancillary data is not captured if not requested by receiver. |
c4307285 | 1185 | * |
b97bf3fd PL |
1186 | * Returns 0 if successful, otherwise errno |
1187 | */ | |
301bae56 JPM |
1188 | static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
1189 | struct tipc_sock *tsk) | |
b97bf3fd PL |
1190 | { |
1191 | u32 anc_data[3]; | |
1192 | u32 err; | |
1193 | u32 dest_type; | |
3546c750 | 1194 | int has_name; |
b97bf3fd PL |
1195 | int res; |
1196 | ||
1197 | if (likely(m->msg_controllen == 0)) | |
1198 | return 0; | |
1199 | ||
1200 | /* Optionally capture errored message object(s) */ | |
b97bf3fd PL |
1201 | err = msg ? msg_errcode(msg) : 0; |
1202 | if (unlikely(err)) { | |
1203 | anc_data[0] = err; | |
1204 | anc_data[1] = msg_data_sz(msg); | |
2db9983a AS |
1205 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
1206 | if (res) | |
b97bf3fd | 1207 | return res; |
2db9983a AS |
1208 | if (anc_data[1]) { |
1209 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], | |
1210 | msg_data(msg)); | |
1211 | if (res) | |
1212 | return res; | |
1213 | } | |
b97bf3fd PL |
1214 | } |
1215 | ||
1216 | /* Optionally capture message destination object */ | |
b97bf3fd PL |
1217 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
1218 | switch (dest_type) { | |
1219 | case TIPC_NAMED_MSG: | |
3546c750 | 1220 | has_name = 1; |
b97bf3fd PL |
1221 | anc_data[0] = msg_nametype(msg); |
1222 | anc_data[1] = msg_namelower(msg); | |
1223 | anc_data[2] = msg_namelower(msg); | |
1224 | break; | |
1225 | case TIPC_MCAST_MSG: | |
3546c750 | 1226 | has_name = 1; |
b97bf3fd PL |
1227 | anc_data[0] = msg_nametype(msg); |
1228 | anc_data[1] = msg_namelower(msg); | |
1229 | anc_data[2] = msg_nameupper(msg); | |
1230 | break; | |
1231 | case TIPC_CONN_MSG: | |
301bae56 JPM |
1232 | has_name = (tsk->conn_type != 0); |
1233 | anc_data[0] = tsk->conn_type; | |
1234 | anc_data[1] = tsk->conn_instance; | |
1235 | anc_data[2] = tsk->conn_instance; | |
b97bf3fd PL |
1236 | break; |
1237 | default: | |
3546c750 | 1238 | has_name = 0; |
b97bf3fd | 1239 | } |
2db9983a AS |
1240 | if (has_name) { |
1241 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); | |
1242 | if (res) | |
1243 | return res; | |
1244 | } | |
b97bf3fd PL |
1245 | |
1246 | return 0; | |
1247 | } | |
1248 | ||
301bae56 | 1249 | static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack) |
739f5e4e | 1250 | { |
f2f9800d | 1251 | struct net *net = sock_net(&tsk->sk); |
a6ca1094 | 1252 | struct sk_buff *skb = NULL; |
739f5e4e | 1253 | struct tipc_msg *msg; |
301bae56 JPM |
1254 | u32 peer_port = tsk_peer_port(tsk); |
1255 | u32 dnode = tsk_peer_node(tsk); | |
739f5e4e | 1256 | |
301bae56 | 1257 | if (!tsk->connected) |
739f5e4e | 1258 | return; |
a6ca1094 | 1259 | skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode, |
07f6c4bc | 1260 | tipc_own_addr, peer_port, tsk->portid, TIPC_OK); |
a6ca1094 | 1261 | if (!skb) |
739f5e4e | 1262 | return; |
a6ca1094 | 1263 | msg = buf_msg(skb); |
739f5e4e | 1264 | msg_set_msgcnt(msg, ack); |
f2f9800d | 1265 | tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg)); |
739f5e4e JPM |
1266 | } |
1267 | ||
85d3fc94 | 1268 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
9bbb4ecc YX |
1269 | { |
1270 | struct sock *sk = sock->sk; | |
1271 | DEFINE_WAIT(wait); | |
85d3fc94 | 1272 | long timeo = *timeop; |
9bbb4ecc YX |
1273 | int err; |
1274 | ||
1275 | for (;;) { | |
1276 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
fe8e4649 | 1277 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
9bbb4ecc YX |
1278 | if (sock->state == SS_DISCONNECTING) { |
1279 | err = -ENOTCONN; | |
1280 | break; | |
1281 | } | |
1282 | release_sock(sk); | |
1283 | timeo = schedule_timeout(timeo); | |
1284 | lock_sock(sk); | |
1285 | } | |
1286 | err = 0; | |
1287 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1288 | break; | |
1289 | err = sock_intr_errno(timeo); | |
1290 | if (signal_pending(current)) | |
1291 | break; | |
1292 | err = -EAGAIN; | |
1293 | if (!timeo) | |
1294 | break; | |
1295 | } | |
1296 | finish_wait(sk_sleep(sk), &wait); | |
85d3fc94 | 1297 | *timeop = timeo; |
9bbb4ecc YX |
1298 | return err; |
1299 | } | |
1300 | ||
c4307285 | 1301 | /** |
247f0f3c | 1302 | * tipc_recvmsg - receive packet-oriented message |
b97bf3fd PL |
1303 | * @iocb: (unused) |
1304 | * @m: descriptor for message info | |
1305 | * @buf_len: total size of user buffer area | |
1306 | * @flags: receive flags | |
c4307285 | 1307 | * |
b97bf3fd PL |
1308 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
1309 | * If the complete message doesn't fit in user area, truncate it. | |
1310 | * | |
1311 | * Returns size of returned message data, errno otherwise | |
1312 | */ | |
247f0f3c YX |
1313 | static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, |
1314 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1315 | { |
0c3141e9 | 1316 | struct sock *sk = sock->sk; |
58ed9442 | 1317 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
1318 | struct sk_buff *buf; |
1319 | struct tipc_msg *msg; | |
9bbb4ecc | 1320 | long timeo; |
b97bf3fd PL |
1321 | unsigned int sz; |
1322 | u32 err; | |
1323 | int res; | |
1324 | ||
0c3141e9 | 1325 | /* Catch invalid receive requests */ |
b97bf3fd PL |
1326 | if (unlikely(!buf_len)) |
1327 | return -EINVAL; | |
1328 | ||
0c3141e9 | 1329 | lock_sock(sk); |
b97bf3fd | 1330 | |
0c3141e9 AS |
1331 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
1332 | res = -ENOTCONN; | |
b97bf3fd PL |
1333 | goto exit; |
1334 | } | |
1335 | ||
9bbb4ecc | 1336 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
0c3141e9 | 1337 | restart: |
b97bf3fd | 1338 | |
0c3141e9 | 1339 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1340 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1341 | if (res) |
1342 | goto exit; | |
b97bf3fd | 1343 | |
0c3141e9 | 1344 | /* Look at first message in receive queue */ |
0c3141e9 | 1345 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1346 | msg = buf_msg(buf); |
1347 | sz = msg_data_sz(msg); | |
1348 | err = msg_errcode(msg); | |
1349 | ||
b97bf3fd | 1350 | /* Discard an empty non-errored message & try again */ |
b97bf3fd | 1351 | if ((!sz) && (!err)) { |
2e84c60b | 1352 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1353 | goto restart; |
1354 | } | |
1355 | ||
1356 | /* Capture sender's address (optional) */ | |
b97bf3fd PL |
1357 | set_orig_addr(m, msg); |
1358 | ||
1359 | /* Capture ancillary data (optional) */ | |
301bae56 | 1360 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
0c3141e9 | 1361 | if (res) |
b97bf3fd PL |
1362 | goto exit; |
1363 | ||
1364 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd PL |
1365 | if (!err) { |
1366 | if (unlikely(buf_len < sz)) { | |
1367 | sz = buf_len; | |
1368 | m->msg_flags |= MSG_TRUNC; | |
1369 | } | |
51f3d02b | 1370 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz); |
0232fd0a | 1371 | if (res) |
b97bf3fd | 1372 | goto exit; |
b97bf3fd PL |
1373 | res = sz; |
1374 | } else { | |
1375 | if ((sock->state == SS_READY) || | |
1376 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) | |
1377 | res = 0; | |
1378 | else | |
1379 | res = -ECONNRESET; | |
1380 | } | |
1381 | ||
1382 | /* Consume received message (optional) */ | |
b97bf3fd | 1383 | if (likely(!(flags & MSG_PEEK))) { |
99009806 | 1384 | if ((sock->state != SS_READY) && |
60120526 | 1385 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
301bae56 | 1386 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
60120526 JPM |
1387 | tsk->rcv_unacked = 0; |
1388 | } | |
2e84c60b | 1389 | tsk_advance_rx_queue(sk); |
c4307285 | 1390 | } |
b97bf3fd | 1391 | exit: |
0c3141e9 | 1392 | release_sock(sk); |
b97bf3fd PL |
1393 | return res; |
1394 | } | |
1395 | ||
c4307285 | 1396 | /** |
247f0f3c | 1397 | * tipc_recv_stream - receive stream-oriented data |
b97bf3fd PL |
1398 | * @iocb: (unused) |
1399 | * @m: descriptor for message info | |
1400 | * @buf_len: total size of user buffer area | |
1401 | * @flags: receive flags | |
c4307285 YH |
1402 | * |
1403 | * Used for SOCK_STREAM messages only. If not enough data is available | |
b97bf3fd PL |
1404 | * will optionally wait for more; never truncates data. |
1405 | * | |
1406 | * Returns size of returned message data, errno otherwise | |
1407 | */ | |
247f0f3c YX |
1408 | static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, |
1409 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1410 | { |
0c3141e9 | 1411 | struct sock *sk = sock->sk; |
58ed9442 | 1412 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
1413 | struct sk_buff *buf; |
1414 | struct tipc_msg *msg; | |
9bbb4ecc | 1415 | long timeo; |
b97bf3fd | 1416 | unsigned int sz; |
3720d40b | 1417 | int sz_to_copy, target, needed; |
b97bf3fd | 1418 | int sz_copied = 0; |
b97bf3fd | 1419 | u32 err; |
0c3141e9 | 1420 | int res = 0; |
b97bf3fd | 1421 | |
0c3141e9 | 1422 | /* Catch invalid receive attempts */ |
b97bf3fd PL |
1423 | if (unlikely(!buf_len)) |
1424 | return -EINVAL; | |
1425 | ||
0c3141e9 | 1426 | lock_sock(sk); |
b97bf3fd | 1427 | |
9bbb4ecc | 1428 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
0c3141e9 | 1429 | res = -ENOTCONN; |
b97bf3fd PL |
1430 | goto exit; |
1431 | } | |
1432 | ||
3720d40b | 1433 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
9bbb4ecc | 1434 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
b97bf3fd | 1435 | |
617d3c7a | 1436 | restart: |
0c3141e9 | 1437 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1438 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1439 | if (res) |
1440 | goto exit; | |
b97bf3fd | 1441 | |
0c3141e9 | 1442 | /* Look at first message in receive queue */ |
0c3141e9 | 1443 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1444 | msg = buf_msg(buf); |
1445 | sz = msg_data_sz(msg); | |
1446 | err = msg_errcode(msg); | |
1447 | ||
1448 | /* Discard an empty non-errored message & try again */ | |
b97bf3fd | 1449 | if ((!sz) && (!err)) { |
2e84c60b | 1450 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1451 | goto restart; |
1452 | } | |
1453 | ||
1454 | /* Optionally capture sender's address & ancillary data of first msg */ | |
b97bf3fd PL |
1455 | if (sz_copied == 0) { |
1456 | set_orig_addr(m, msg); | |
301bae56 | 1457 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
0c3141e9 | 1458 | if (res) |
b97bf3fd PL |
1459 | goto exit; |
1460 | } | |
1461 | ||
1462 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd | 1463 | if (!err) { |
0232fd0a | 1464 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
b97bf3fd | 1465 | |
0232fd0a | 1466 | sz -= offset; |
b97bf3fd PL |
1467 | needed = (buf_len - sz_copied); |
1468 | sz_to_copy = (sz <= needed) ? sz : needed; | |
0232fd0a | 1469 | |
51f3d02b DM |
1470 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset, |
1471 | m, sz_to_copy); | |
0232fd0a | 1472 | if (res) |
b97bf3fd | 1473 | goto exit; |
0232fd0a | 1474 | |
b97bf3fd PL |
1475 | sz_copied += sz_to_copy; |
1476 | ||
1477 | if (sz_to_copy < sz) { | |
1478 | if (!(flags & MSG_PEEK)) | |
0232fd0a AS |
1479 | TIPC_SKB_CB(buf)->handle = |
1480 | (void *)(unsigned long)(offset + sz_to_copy); | |
b97bf3fd PL |
1481 | goto exit; |
1482 | } | |
b97bf3fd PL |
1483 | } else { |
1484 | if (sz_copied != 0) | |
1485 | goto exit; /* can't add error msg to valid data */ | |
1486 | ||
1487 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) | |
1488 | res = 0; | |
1489 | else | |
1490 | res = -ECONNRESET; | |
1491 | } | |
1492 | ||
1493 | /* Consume received message (optional) */ | |
b97bf3fd | 1494 | if (likely(!(flags & MSG_PEEK))) { |
60120526 | 1495 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
301bae56 | 1496 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
60120526 JPM |
1497 | tsk->rcv_unacked = 0; |
1498 | } | |
2e84c60b | 1499 | tsk_advance_rx_queue(sk); |
c4307285 | 1500 | } |
b97bf3fd PL |
1501 | |
1502 | /* Loop around if more data is required */ | |
f64f9e71 JP |
1503 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
1504 | (!skb_queue_empty(&sk->sk_receive_queue) || | |
3720d40b | 1505 | (sz_copied < target)) && /* and more is ready or required */ |
f64f9e71 JP |
1506 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
1507 | (!err)) /* and haven't reached a FIN */ | |
b97bf3fd PL |
1508 | goto restart; |
1509 | ||
1510 | exit: | |
0c3141e9 | 1511 | release_sock(sk); |
a3b0a5a9 | 1512 | return sz_copied ? sz_copied : res; |
b97bf3fd PL |
1513 | } |
1514 | ||
f288bef4 YX |
1515 | /** |
1516 | * tipc_write_space - wake up thread if port congestion is released | |
1517 | * @sk: socket | |
1518 | */ | |
1519 | static void tipc_write_space(struct sock *sk) | |
1520 | { | |
1521 | struct socket_wq *wq; | |
1522 | ||
1523 | rcu_read_lock(); | |
1524 | wq = rcu_dereference(sk->sk_wq); | |
1525 | if (wq_has_sleeper(wq)) | |
1526 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | | |
1527 | POLLWRNORM | POLLWRBAND); | |
1528 | rcu_read_unlock(); | |
1529 | } | |
1530 | ||
1531 | /** | |
1532 | * tipc_data_ready - wake up threads to indicate messages have been received | |
1533 | * @sk: socket | |
1534 | * @len: the length of messages | |
1535 | */ | |
676d2369 | 1536 | static void tipc_data_ready(struct sock *sk) |
f288bef4 YX |
1537 | { |
1538 | struct socket_wq *wq; | |
1539 | ||
1540 | rcu_read_lock(); | |
1541 | wq = rcu_dereference(sk->sk_wq); | |
1542 | if (wq_has_sleeper(wq)) | |
1543 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | | |
1544 | POLLRDNORM | POLLRDBAND); | |
1545 | rcu_read_unlock(); | |
1546 | } | |
1547 | ||
7e6c131e YX |
1548 | /** |
1549 | * filter_connect - Handle all incoming messages for a connection-based socket | |
58ed9442 | 1550 | * @tsk: TIPC socket |
7e6c131e YX |
1551 | * @msg: message |
1552 | * | |
b2ad5e5f | 1553 | * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise |
7e6c131e | 1554 | */ |
e4de5fab | 1555 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
7e6c131e | 1556 | { |
58ed9442 | 1557 | struct sock *sk = &tsk->sk; |
f2f9800d | 1558 | struct net *net = sock_net(sk); |
8826cde6 | 1559 | struct socket *sock = sk->sk_socket; |
7e6c131e | 1560 | struct tipc_msg *msg = buf_msg(*buf); |
e4de5fab | 1561 | int retval = -TIPC_ERR_NO_PORT; |
7e6c131e YX |
1562 | |
1563 | if (msg_mcast(msg)) | |
1564 | return retval; | |
1565 | ||
1566 | switch ((int)sock->state) { | |
1567 | case SS_CONNECTED: | |
1568 | /* Accept only connection-based messages sent by peer */ | |
2e84c60b | 1569 | if (tsk_peer_msg(tsk, msg)) { |
7e6c131e YX |
1570 | if (unlikely(msg_errcode(msg))) { |
1571 | sock->state = SS_DISCONNECTING; | |
301bae56 | 1572 | tsk->connected = 0; |
dadebc00 | 1573 | /* let timer expire on it's own */ |
f2f9800d | 1574 | tipc_node_remove_conn(net, tsk_peer_node(tsk), |
07f6c4bc | 1575 | tsk->portid); |
7e6c131e YX |
1576 | } |
1577 | retval = TIPC_OK; | |
1578 | } | |
1579 | break; | |
1580 | case SS_CONNECTING: | |
1581 | /* Accept only ACK or NACK message */ | |
dadebc00 JPM |
1582 | |
1583 | if (unlikely(!msg_connected(msg))) | |
1584 | break; | |
1585 | ||
584d24b3 YX |
1586 | if (unlikely(msg_errcode(msg))) { |
1587 | sock->state = SS_DISCONNECTING; | |
2c8d8518 | 1588 | sk->sk_err = ECONNREFUSED; |
584d24b3 YX |
1589 | retval = TIPC_OK; |
1590 | break; | |
1591 | } | |
1592 | ||
dadebc00 | 1593 | if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) { |
584d24b3 | 1594 | sock->state = SS_DISCONNECTING; |
dadebc00 | 1595 | sk->sk_err = EINVAL; |
7e6c131e | 1596 | retval = TIPC_OK; |
584d24b3 YX |
1597 | break; |
1598 | } | |
1599 | ||
301bae56 JPM |
1600 | tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg)); |
1601 | msg_set_importance(&tsk->phdr, msg_importance(msg)); | |
dadebc00 JPM |
1602 | sock->state = SS_CONNECTED; |
1603 | ||
584d24b3 YX |
1604 | /* If an incoming message is an 'ACK-', it should be |
1605 | * discarded here because it doesn't contain useful | |
1606 | * data. In addition, we should try to wake up | |
1607 | * connect() routine if sleeping. | |
1608 | */ | |
1609 | if (msg_data_sz(msg) == 0) { | |
1610 | kfree_skb(*buf); | |
1611 | *buf = NULL; | |
1612 | if (waitqueue_active(sk_sleep(sk))) | |
1613 | wake_up_interruptible(sk_sleep(sk)); | |
1614 | } | |
1615 | retval = TIPC_OK; | |
7e6c131e YX |
1616 | break; |
1617 | case SS_LISTENING: | |
1618 | case SS_UNCONNECTED: | |
1619 | /* Accept only SYN message */ | |
1620 | if (!msg_connected(msg) && !(msg_errcode(msg))) | |
1621 | retval = TIPC_OK; | |
1622 | break; | |
1623 | case SS_DISCONNECTING: | |
1624 | break; | |
1625 | default: | |
1626 | pr_err("Unknown socket state %u\n", sock->state); | |
1627 | } | |
1628 | return retval; | |
1629 | } | |
1630 | ||
aba79f33 YX |
1631 | /** |
1632 | * rcvbuf_limit - get proper overload limit of socket receive queue | |
1633 | * @sk: socket | |
1634 | * @buf: message | |
1635 | * | |
1636 | * For all connection oriented messages, irrespective of importance, | |
1637 | * the default overload value (i.e. 67MB) is set as limit. | |
1638 | * | |
1639 | * For all connectionless messages, by default new queue limits are | |
1640 | * as belows: | |
1641 | * | |
cc79dd1b YX |
1642 | * TIPC_LOW_IMPORTANCE (4 MB) |
1643 | * TIPC_MEDIUM_IMPORTANCE (8 MB) | |
1644 | * TIPC_HIGH_IMPORTANCE (16 MB) | |
1645 | * TIPC_CRITICAL_IMPORTANCE (32 MB) | |
aba79f33 YX |
1646 | * |
1647 | * Returns overload limit according to corresponding message importance | |
1648 | */ | |
1649 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf) | |
1650 | { | |
1651 | struct tipc_msg *msg = buf_msg(buf); | |
aba79f33 YX |
1652 | |
1653 | if (msg_connected(msg)) | |
0cee6bbe | 1654 | return sysctl_tipc_rmem[2]; |
1655 | ||
1656 | return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE << | |
1657 | msg_importance(msg); | |
aba79f33 YX |
1658 | } |
1659 | ||
c4307285 | 1660 | /** |
0c3141e9 AS |
1661 | * filter_rcv - validate incoming message |
1662 | * @sk: socket | |
b97bf3fd | 1663 | * @buf: message |
c4307285 | 1664 | * |
0c3141e9 AS |
1665 | * Enqueues message on receive queue if acceptable; optionally handles |
1666 | * disconnect indication for a connected socket. | |
1667 | * | |
1668 | * Called with socket lock already taken; port lock may also be taken. | |
c4307285 | 1669 | * |
e4de5fab | 1670 | * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message |
ac0074ee | 1671 | * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded |
b97bf3fd | 1672 | */ |
e4de5fab | 1673 | static int filter_rcv(struct sock *sk, struct sk_buff *buf) |
b97bf3fd | 1674 | { |
0c3141e9 | 1675 | struct socket *sock = sk->sk_socket; |
58ed9442 | 1676 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd | 1677 | struct tipc_msg *msg = buf_msg(buf); |
aba79f33 | 1678 | unsigned int limit = rcvbuf_limit(sk, buf); |
ac0074ee | 1679 | u32 onode; |
e4de5fab | 1680 | int rc = TIPC_OK; |
b97bf3fd | 1681 | |
ac0074ee JPM |
1682 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
1683 | return tipc_sk_proto_rcv(tsk, &onode, buf); | |
ec8a2e56 | 1684 | |
50100a5e JPM |
1685 | if (unlikely(msg_user(msg) == SOCK_WAKEUP)) { |
1686 | kfree_skb(buf); | |
1687 | tsk->link_cong = 0; | |
1688 | sk->sk_write_space(sk); | |
1689 | return TIPC_OK; | |
1690 | } | |
1691 | ||
b97bf3fd | 1692 | /* Reject message if it is wrong sort of message for socket */ |
aad58547 | 1693 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
e4de5fab | 1694 | return -TIPC_ERR_NO_PORT; |
0c3141e9 | 1695 | |
b97bf3fd | 1696 | if (sock->state == SS_READY) { |
b29f1428 | 1697 | if (msg_connected(msg)) |
e4de5fab | 1698 | return -TIPC_ERR_NO_PORT; |
b97bf3fd | 1699 | } else { |
e4de5fab JPM |
1700 | rc = filter_connect(tsk, &buf); |
1701 | if (rc != TIPC_OK || buf == NULL) | |
1702 | return rc; | |
b97bf3fd PL |
1703 | } |
1704 | ||
1705 | /* Reject message if there isn't room to queue it */ | |
aba79f33 | 1706 | if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) |
e4de5fab | 1707 | return -TIPC_ERR_OVERLOAD; |
b97bf3fd | 1708 | |
aba79f33 | 1709 | /* Enqueue message */ |
40682432 | 1710 | TIPC_SKB_CB(buf)->handle = NULL; |
0c3141e9 | 1711 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
aba79f33 | 1712 | skb_set_owner_r(buf, sk); |
0c3141e9 | 1713 | |
676d2369 | 1714 | sk->sk_data_ready(sk); |
0c3141e9 AS |
1715 | return TIPC_OK; |
1716 | } | |
b97bf3fd | 1717 | |
0c3141e9 | 1718 | /** |
4f4482dc | 1719 | * tipc_backlog_rcv - handle incoming message from backlog queue |
0c3141e9 | 1720 | * @sk: socket |
a6ca1094 | 1721 | * @skb: message |
0c3141e9 AS |
1722 | * |
1723 | * Caller must hold socket lock, but not port lock. | |
1724 | * | |
1725 | * Returns 0 | |
1726 | */ | |
a6ca1094 | 1727 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb) |
0c3141e9 | 1728 | { |
e4de5fab | 1729 | int rc; |
8db1bae3 | 1730 | u32 onode; |
4f4482dc | 1731 | struct tipc_sock *tsk = tipc_sk(sk); |
a6ca1094 | 1732 | uint truesize = skb->truesize; |
0c3141e9 | 1733 | |
a6ca1094 | 1734 | rc = filter_rcv(sk, skb); |
4f4482dc | 1735 | |
ac0074ee JPM |
1736 | if (likely(!rc)) { |
1737 | if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) | |
1738 | atomic_add(truesize, &tsk->dupl_rcvcnt); | |
1739 | return 0; | |
1740 | } | |
1741 | ||
a6ca1094 | 1742 | if ((rc < 0) && !tipc_msg_reverse(skb, &onode, -rc)) |
ac0074ee JPM |
1743 | return 0; |
1744 | ||
f2f9800d | 1745 | tipc_link_xmit_skb(sock_net(sk), skb, onode, 0); |
4f4482dc | 1746 | |
0c3141e9 AS |
1747 | return 0; |
1748 | } | |
1749 | ||
1750 | /** | |
24be34b5 | 1751 | * tipc_sk_rcv - handle incoming message |
a6ca1094 | 1752 | * @skb: buffer containing arriving message |
9816f061 JPM |
1753 | * Consumes buffer |
1754 | * Returns 0 if success, or errno: -EHOSTUNREACH | |
0c3141e9 | 1755 | */ |
f2f9800d | 1756 | int tipc_sk_rcv(struct net *net, struct sk_buff *skb) |
0c3141e9 | 1757 | { |
9816f061 | 1758 | struct tipc_sock *tsk; |
9816f061 | 1759 | struct sock *sk; |
a6ca1094 | 1760 | u32 dport = msg_destport(buf_msg(skb)); |
e4de5fab | 1761 | int rc = TIPC_OK; |
4f4482dc | 1762 | uint limit; |
8db1bae3 | 1763 | u32 dnode; |
9816f061 | 1764 | |
5a379074 | 1765 | /* Validate destination and message */ |
e05b31f4 | 1766 | tsk = tipc_sk_lookup(net, dport); |
9b50fd08 | 1767 | if (unlikely(!tsk)) { |
a6ca1094 | 1768 | rc = tipc_msg_eval(skb, &dnode); |
9816f061 JPM |
1769 | goto exit; |
1770 | } | |
9816f061 JPM |
1771 | sk = &tsk->sk; |
1772 | ||
1773 | /* Queue message */ | |
1a194c2d | 1774 | spin_lock_bh(&sk->sk_lock.slock); |
9816f061 | 1775 | |
0c3141e9 | 1776 | if (!sock_owned_by_user(sk)) { |
a6ca1094 | 1777 | rc = filter_rcv(sk, skb); |
0c3141e9 | 1778 | } else { |
4f4482dc JPM |
1779 | if (sk->sk_backlog.len == 0) |
1780 | atomic_set(&tsk->dupl_rcvcnt, 0); | |
a6ca1094 YX |
1781 | limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt); |
1782 | if (sk_add_backlog(sk, skb, limit)) | |
e4de5fab | 1783 | rc = -TIPC_ERR_OVERLOAD; |
0c3141e9 | 1784 | } |
1a194c2d | 1785 | spin_unlock_bh(&sk->sk_lock.slock); |
07f6c4bc | 1786 | sock_put(sk); |
e4de5fab | 1787 | if (likely(!rc)) |
9816f061 JPM |
1788 | return 0; |
1789 | exit: | |
a6ca1094 | 1790 | if ((rc < 0) && !tipc_msg_reverse(skb, &dnode, -rc)) |
8db1bae3 | 1791 | return -EHOSTUNREACH; |
5a379074 | 1792 | |
f2f9800d | 1793 | tipc_link_xmit_skb(net, skb, dnode, 0); |
5a379074 | 1794 | return (rc < 0) ? -EHOSTUNREACH : 0; |
b97bf3fd PL |
1795 | } |
1796 | ||
78eb3a53 YX |
1797 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
1798 | { | |
1799 | struct sock *sk = sock->sk; | |
1800 | DEFINE_WAIT(wait); | |
1801 | int done; | |
1802 | ||
1803 | do { | |
1804 | int err = sock_error(sk); | |
1805 | if (err) | |
1806 | return err; | |
1807 | if (!*timeo_p) | |
1808 | return -ETIMEDOUT; | |
1809 | if (signal_pending(current)) | |
1810 | return sock_intr_errno(*timeo_p); | |
1811 | ||
1812 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1813 | done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); | |
1814 | finish_wait(sk_sleep(sk), &wait); | |
1815 | } while (!done); | |
1816 | return 0; | |
1817 | } | |
1818 | ||
b97bf3fd | 1819 | /** |
247f0f3c | 1820 | * tipc_connect - establish a connection to another TIPC port |
b97bf3fd PL |
1821 | * @sock: socket structure |
1822 | * @dest: socket address for destination port | |
1823 | * @destlen: size of socket address data structure | |
0c3141e9 | 1824 | * @flags: file-related flags associated with socket |
b97bf3fd PL |
1825 | * |
1826 | * Returns 0 on success, errno otherwise | |
1827 | */ | |
247f0f3c YX |
1828 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
1829 | int destlen, int flags) | |
b97bf3fd | 1830 | { |
0c3141e9 | 1831 | struct sock *sk = sock->sk; |
b89741a0 AS |
1832 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
1833 | struct msghdr m = {NULL,}; | |
78eb3a53 YX |
1834 | long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; |
1835 | socket_state previous; | |
b89741a0 AS |
1836 | int res; |
1837 | ||
0c3141e9 AS |
1838 | lock_sock(sk); |
1839 | ||
b89741a0 | 1840 | /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ |
0c3141e9 AS |
1841 | if (sock->state == SS_READY) { |
1842 | res = -EOPNOTSUPP; | |
1843 | goto exit; | |
1844 | } | |
b89741a0 | 1845 | |
b89741a0 AS |
1846 | /* |
1847 | * Reject connection attempt using multicast address | |
1848 | * | |
1849 | * Note: send_msg() validates the rest of the address fields, | |
1850 | * so there's no need to do it here | |
1851 | */ | |
0c3141e9 AS |
1852 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
1853 | res = -EINVAL; | |
1854 | goto exit; | |
1855 | } | |
1856 | ||
78eb3a53 | 1857 | previous = sock->state; |
584d24b3 YX |
1858 | switch (sock->state) { |
1859 | case SS_UNCONNECTED: | |
1860 | /* Send a 'SYN-' to destination */ | |
1861 | m.msg_name = dest; | |
1862 | m.msg_namelen = destlen; | |
1863 | ||
1864 | /* If connect is in non-blocking case, set MSG_DONTWAIT to | |
1865 | * indicate send_msg() is never blocked. | |
1866 | */ | |
1867 | if (!timeout) | |
1868 | m.msg_flags = MSG_DONTWAIT; | |
1869 | ||
247f0f3c | 1870 | res = tipc_sendmsg(NULL, sock, &m, 0); |
584d24b3 YX |
1871 | if ((res < 0) && (res != -EWOULDBLOCK)) |
1872 | goto exit; | |
1873 | ||
1874 | /* Just entered SS_CONNECTING state; the only | |
1875 | * difference is that return value in non-blocking | |
1876 | * case is EINPROGRESS, rather than EALREADY. | |
1877 | */ | |
1878 | res = -EINPROGRESS; | |
584d24b3 | 1879 | case SS_CONNECTING: |
78eb3a53 YX |
1880 | if (previous == SS_CONNECTING) |
1881 | res = -EALREADY; | |
1882 | if (!timeout) | |
1883 | goto exit; | |
1884 | timeout = msecs_to_jiffies(timeout); | |
1885 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ | |
1886 | res = tipc_wait_for_connect(sock, &timeout); | |
584d24b3 YX |
1887 | break; |
1888 | case SS_CONNECTED: | |
1889 | res = -EISCONN; | |
1890 | break; | |
1891 | default: | |
1892 | res = -EINVAL; | |
78eb3a53 | 1893 | break; |
b89741a0 | 1894 | } |
0c3141e9 AS |
1895 | exit: |
1896 | release_sock(sk); | |
b89741a0 | 1897 | return res; |
b97bf3fd PL |
1898 | } |
1899 | ||
c4307285 | 1900 | /** |
247f0f3c | 1901 | * tipc_listen - allow socket to listen for incoming connections |
b97bf3fd PL |
1902 | * @sock: socket structure |
1903 | * @len: (unused) | |
c4307285 | 1904 | * |
b97bf3fd PL |
1905 | * Returns 0 on success, errno otherwise |
1906 | */ | |
247f0f3c | 1907 | static int tipc_listen(struct socket *sock, int len) |
b97bf3fd | 1908 | { |
0c3141e9 AS |
1909 | struct sock *sk = sock->sk; |
1910 | int res; | |
1911 | ||
1912 | lock_sock(sk); | |
b97bf3fd | 1913 | |
245f3d34 | 1914 | if (sock->state != SS_UNCONNECTED) |
0c3141e9 AS |
1915 | res = -EINVAL; |
1916 | else { | |
1917 | sock->state = SS_LISTENING; | |
1918 | res = 0; | |
1919 | } | |
1920 | ||
1921 | release_sock(sk); | |
1922 | return res; | |
b97bf3fd PL |
1923 | } |
1924 | ||
6398e23c YX |
1925 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
1926 | { | |
1927 | struct sock *sk = sock->sk; | |
1928 | DEFINE_WAIT(wait); | |
1929 | int err; | |
1930 | ||
1931 | /* True wake-one mechanism for incoming connections: only | |
1932 | * one process gets woken up, not the 'whole herd'. | |
1933 | * Since we do not 'race & poll' for established sockets | |
1934 | * anymore, the common case will execute the loop only once. | |
1935 | */ | |
1936 | for (;;) { | |
1937 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, | |
1938 | TASK_INTERRUPTIBLE); | |
fe8e4649 | 1939 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
6398e23c YX |
1940 | release_sock(sk); |
1941 | timeo = schedule_timeout(timeo); | |
1942 | lock_sock(sk); | |
1943 | } | |
1944 | err = 0; | |
1945 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1946 | break; | |
1947 | err = -EINVAL; | |
1948 | if (sock->state != SS_LISTENING) | |
1949 | break; | |
1950 | err = sock_intr_errno(timeo); | |
1951 | if (signal_pending(current)) | |
1952 | break; | |
1953 | err = -EAGAIN; | |
1954 | if (!timeo) | |
1955 | break; | |
1956 | } | |
1957 | finish_wait(sk_sleep(sk), &wait); | |
1958 | return err; | |
1959 | } | |
1960 | ||
c4307285 | 1961 | /** |
247f0f3c | 1962 | * tipc_accept - wait for connection request |
b97bf3fd PL |
1963 | * @sock: listening socket |
1964 | * @newsock: new socket that is to be connected | |
1965 | * @flags: file-related flags associated with socket | |
c4307285 | 1966 | * |
b97bf3fd PL |
1967 | * Returns 0 on success, errno otherwise |
1968 | */ | |
247f0f3c | 1969 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) |
b97bf3fd | 1970 | { |
0fef8f20 | 1971 | struct sock *new_sk, *sk = sock->sk; |
b97bf3fd | 1972 | struct sk_buff *buf; |
301bae56 | 1973 | struct tipc_sock *new_tsock; |
0fef8f20 | 1974 | struct tipc_msg *msg; |
6398e23c | 1975 | long timeo; |
0c3141e9 | 1976 | int res; |
b97bf3fd | 1977 | |
0c3141e9 | 1978 | lock_sock(sk); |
b97bf3fd | 1979 | |
0c3141e9 AS |
1980 | if (sock->state != SS_LISTENING) { |
1981 | res = -EINVAL; | |
b97bf3fd PL |
1982 | goto exit; |
1983 | } | |
6398e23c YX |
1984 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
1985 | res = tipc_wait_for_accept(sock, timeo); | |
1986 | if (res) | |
1987 | goto exit; | |
0c3141e9 AS |
1988 | |
1989 | buf = skb_peek(&sk->sk_receive_queue); | |
1990 | ||
c5fa7b3c | 1991 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); |
0fef8f20 PG |
1992 | if (res) |
1993 | goto exit; | |
b97bf3fd | 1994 | |
0fef8f20 | 1995 | new_sk = new_sock->sk; |
301bae56 | 1996 | new_tsock = tipc_sk(new_sk); |
0fef8f20 | 1997 | msg = buf_msg(buf); |
b97bf3fd | 1998 | |
0fef8f20 PG |
1999 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
2000 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); | |
2001 | ||
2002 | /* | |
2003 | * Reject any stray messages received by new socket | |
2004 | * before the socket lock was taken (very, very unlikely) | |
2005 | */ | |
2e84c60b | 2006 | tsk_rej_rx_queue(new_sk); |
0fef8f20 PG |
2007 | |
2008 | /* Connect new socket to it's peer */ | |
301bae56 | 2009 | tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); |
0fef8f20 PG |
2010 | new_sock->state = SS_CONNECTED; |
2011 | ||
301bae56 | 2012 | tsk_set_importance(new_tsock, msg_importance(msg)); |
0fef8f20 | 2013 | if (msg_named(msg)) { |
301bae56 JPM |
2014 | new_tsock->conn_type = msg_nametype(msg); |
2015 | new_tsock->conn_instance = msg_nameinst(msg); | |
b97bf3fd | 2016 | } |
0fef8f20 PG |
2017 | |
2018 | /* | |
2019 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. | |
2020 | * Respond to 'SYN+' by queuing it on new socket. | |
2021 | */ | |
2022 | if (!msg_data_sz(msg)) { | |
2023 | struct msghdr m = {NULL,}; | |
2024 | ||
2e84c60b | 2025 | tsk_advance_rx_queue(sk); |
247f0f3c | 2026 | tipc_send_packet(NULL, new_sock, &m, 0); |
0fef8f20 PG |
2027 | } else { |
2028 | __skb_dequeue(&sk->sk_receive_queue); | |
2029 | __skb_queue_head(&new_sk->sk_receive_queue, buf); | |
aba79f33 | 2030 | skb_set_owner_r(buf, new_sk); |
0fef8f20 PG |
2031 | } |
2032 | release_sock(new_sk); | |
b97bf3fd | 2033 | exit: |
0c3141e9 | 2034 | release_sock(sk); |
b97bf3fd PL |
2035 | return res; |
2036 | } | |
2037 | ||
2038 | /** | |
247f0f3c | 2039 | * tipc_shutdown - shutdown socket connection |
b97bf3fd | 2040 | * @sock: socket structure |
e247a8f5 | 2041 | * @how: direction to close (must be SHUT_RDWR) |
b97bf3fd PL |
2042 | * |
2043 | * Terminates connection (if necessary), then purges socket's receive queue. | |
c4307285 | 2044 | * |
b97bf3fd PL |
2045 | * Returns 0 on success, errno otherwise |
2046 | */ | |
247f0f3c | 2047 | static int tipc_shutdown(struct socket *sock, int how) |
b97bf3fd | 2048 | { |
0c3141e9 | 2049 | struct sock *sk = sock->sk; |
f2f9800d | 2050 | struct net *net = sock_net(sk); |
58ed9442 | 2051 | struct tipc_sock *tsk = tipc_sk(sk); |
a6ca1094 | 2052 | struct sk_buff *skb; |
80e44c22 | 2053 | u32 dnode; |
b97bf3fd PL |
2054 | int res; |
2055 | ||
e247a8f5 AS |
2056 | if (how != SHUT_RDWR) |
2057 | return -EINVAL; | |
b97bf3fd | 2058 | |
0c3141e9 | 2059 | lock_sock(sk); |
b97bf3fd PL |
2060 | |
2061 | switch (sock->state) { | |
0c3141e9 | 2062 | case SS_CONNECTING: |
b97bf3fd PL |
2063 | case SS_CONNECTED: |
2064 | ||
b97bf3fd | 2065 | restart: |
617d3c7a | 2066 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
a6ca1094 YX |
2067 | skb = __skb_dequeue(&sk->sk_receive_queue); |
2068 | if (skb) { | |
2069 | if (TIPC_SKB_CB(skb)->handle != NULL) { | |
2070 | kfree_skb(skb); | |
b97bf3fd PL |
2071 | goto restart; |
2072 | } | |
a6ca1094 | 2073 | if (tipc_msg_reverse(skb, &dnode, TIPC_CONN_SHUTDOWN)) |
f2f9800d YX |
2074 | tipc_link_xmit_skb(net, skb, dnode, |
2075 | tsk->portid); | |
2076 | tipc_node_remove_conn(net, dnode, tsk->portid); | |
0c3141e9 | 2077 | } else { |
301bae56 | 2078 | dnode = tsk_peer_node(tsk); |
a6ca1094 | 2079 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
80e44c22 JPM |
2080 | TIPC_CONN_MSG, SHORT_H_SIZE, |
2081 | 0, dnode, tipc_own_addr, | |
301bae56 | 2082 | tsk_peer_port(tsk), |
07f6c4bc | 2083 | tsk->portid, TIPC_CONN_SHUTDOWN); |
f2f9800d | 2084 | tipc_link_xmit_skb(net, skb, dnode, tsk->portid); |
b97bf3fd | 2085 | } |
301bae56 | 2086 | tsk->connected = 0; |
0c3141e9 | 2087 | sock->state = SS_DISCONNECTING; |
f2f9800d | 2088 | tipc_node_remove_conn(net, dnode, tsk->portid); |
b97bf3fd PL |
2089 | /* fall through */ |
2090 | ||
2091 | case SS_DISCONNECTING: | |
2092 | ||
75031151 | 2093 | /* Discard any unreceived messages */ |
57467e56 | 2094 | __skb_queue_purge(&sk->sk_receive_queue); |
75031151 YX |
2095 | |
2096 | /* Wake up anyone sleeping in poll */ | |
2097 | sk->sk_state_change(sk); | |
b97bf3fd PL |
2098 | res = 0; |
2099 | break; | |
2100 | ||
2101 | default: | |
2102 | res = -ENOTCONN; | |
2103 | } | |
2104 | ||
0c3141e9 | 2105 | release_sock(sk); |
b97bf3fd PL |
2106 | return res; |
2107 | } | |
2108 | ||
f2f2a96a | 2109 | static void tipc_sk_timeout(unsigned long data) |
57289015 | 2110 | { |
f2f2a96a YX |
2111 | struct tipc_sock *tsk = (struct tipc_sock *)data; |
2112 | struct sock *sk = &tsk->sk; | |
a6ca1094 | 2113 | struct sk_buff *skb = NULL; |
57289015 JPM |
2114 | u32 peer_port, peer_node; |
2115 | ||
6c9808ce | 2116 | bh_lock_sock(sk); |
301bae56 | 2117 | if (!tsk->connected) { |
6c9808ce JPM |
2118 | bh_unlock_sock(sk); |
2119 | goto exit; | |
57289015 | 2120 | } |
301bae56 JPM |
2121 | peer_port = tsk_peer_port(tsk); |
2122 | peer_node = tsk_peer_node(tsk); | |
57289015 | 2123 | |
301bae56 | 2124 | if (tsk->probing_state == TIPC_CONN_PROBING) { |
57289015 | 2125 | /* Previous probe not answered -> self abort */ |
a6ca1094 | 2126 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
57289015 | 2127 | SHORT_H_SIZE, 0, tipc_own_addr, |
f2f2a96a | 2128 | peer_node, tsk->portid, peer_port, |
57289015 JPM |
2129 | TIPC_ERR_NO_PORT); |
2130 | } else { | |
a6ca1094 | 2131 | skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, |
57289015 | 2132 | 0, peer_node, tipc_own_addr, |
f2f2a96a | 2133 | peer_port, tsk->portid, TIPC_OK); |
301bae56 | 2134 | tsk->probing_state = TIPC_CONN_PROBING; |
f2f2a96a YX |
2135 | if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv)) |
2136 | sock_hold(sk); | |
57289015 JPM |
2137 | } |
2138 | bh_unlock_sock(sk); | |
a6ca1094 | 2139 | if (skb) |
f2f9800d | 2140 | tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid); |
6c9808ce | 2141 | exit: |
07f6c4bc | 2142 | sock_put(sk); |
57289015 JPM |
2143 | } |
2144 | ||
301bae56 | 2145 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, |
0fc87aae JPM |
2146 | struct tipc_name_seq const *seq) |
2147 | { | |
f2f9800d | 2148 | struct net *net = sock_net(&tsk->sk); |
0fc87aae JPM |
2149 | struct publication *publ; |
2150 | u32 key; | |
2151 | ||
301bae56 | 2152 | if (tsk->connected) |
0fc87aae | 2153 | return -EINVAL; |
07f6c4bc YX |
2154 | key = tsk->portid + tsk->pub_count + 1; |
2155 | if (key == tsk->portid) | |
0fc87aae JPM |
2156 | return -EADDRINUSE; |
2157 | ||
f2f9800d | 2158 | publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper, |
07f6c4bc | 2159 | scope, tsk->portid, key); |
0fc87aae JPM |
2160 | if (unlikely(!publ)) |
2161 | return -EINVAL; | |
2162 | ||
301bae56 JPM |
2163 | list_add(&publ->pport_list, &tsk->publications); |
2164 | tsk->pub_count++; | |
2165 | tsk->published = 1; | |
0fc87aae JPM |
2166 | return 0; |
2167 | } | |
2168 | ||
301bae56 | 2169 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, |
0fc87aae JPM |
2170 | struct tipc_name_seq const *seq) |
2171 | { | |
f2f9800d | 2172 | struct net *net = sock_net(&tsk->sk); |
0fc87aae JPM |
2173 | struct publication *publ; |
2174 | struct publication *safe; | |
2175 | int rc = -EINVAL; | |
2176 | ||
301bae56 | 2177 | list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) { |
0fc87aae JPM |
2178 | if (seq) { |
2179 | if (publ->scope != scope) | |
2180 | continue; | |
2181 | if (publ->type != seq->type) | |
2182 | continue; | |
2183 | if (publ->lower != seq->lower) | |
2184 | continue; | |
2185 | if (publ->upper != seq->upper) | |
2186 | break; | |
f2f9800d | 2187 | tipc_nametbl_withdraw(net, publ->type, publ->lower, |
0fc87aae JPM |
2188 | publ->ref, publ->key); |
2189 | rc = 0; | |
2190 | break; | |
2191 | } | |
f2f9800d | 2192 | tipc_nametbl_withdraw(net, publ->type, publ->lower, |
0fc87aae JPM |
2193 | publ->ref, publ->key); |
2194 | rc = 0; | |
2195 | } | |
301bae56 JPM |
2196 | if (list_empty(&tsk->publications)) |
2197 | tsk->published = 0; | |
0fc87aae JPM |
2198 | return rc; |
2199 | } | |
2200 | ||
301bae56 | 2201 | static int tipc_sk_show(struct tipc_sock *tsk, char *buf, |
5a9ee0be JPM |
2202 | int len, int full_id) |
2203 | { | |
2204 | struct publication *publ; | |
2205 | int ret; | |
2206 | ||
2207 | if (full_id) | |
2208 | ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:", | |
2209 | tipc_zone(tipc_own_addr), | |
2210 | tipc_cluster(tipc_own_addr), | |
07f6c4bc | 2211 | tipc_node(tipc_own_addr), tsk->portid); |
5a9ee0be | 2212 | else |
07f6c4bc | 2213 | ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid); |
5a9ee0be | 2214 | |
301bae56 JPM |
2215 | if (tsk->connected) { |
2216 | u32 dport = tsk_peer_port(tsk); | |
2217 | u32 destnode = tsk_peer_node(tsk); | |
5a9ee0be JPM |
2218 | |
2219 | ret += tipc_snprintf(buf + ret, len - ret, | |
2220 | " connected to <%u.%u.%u:%u>", | |
2221 | tipc_zone(destnode), | |
2222 | tipc_cluster(destnode), | |
2223 | tipc_node(destnode), dport); | |
301bae56 | 2224 | if (tsk->conn_type != 0) |
5a9ee0be | 2225 | ret += tipc_snprintf(buf + ret, len - ret, |
301bae56 JPM |
2226 | " via {%u,%u}", tsk->conn_type, |
2227 | tsk->conn_instance); | |
2228 | } else if (tsk->published) { | |
5a9ee0be | 2229 | ret += tipc_snprintf(buf + ret, len - ret, " bound to"); |
301bae56 | 2230 | list_for_each_entry(publ, &tsk->publications, pport_list) { |
5a9ee0be JPM |
2231 | if (publ->lower == publ->upper) |
2232 | ret += tipc_snprintf(buf + ret, len - ret, | |
2233 | " {%u,%u}", publ->type, | |
2234 | publ->lower); | |
2235 | else | |
2236 | ret += tipc_snprintf(buf + ret, len - ret, | |
2237 | " {%u,%u,%u}", publ->type, | |
2238 | publ->lower, publ->upper); | |
2239 | } | |
2240 | } | |
2241 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); | |
2242 | return ret; | |
2243 | } | |
2244 | ||
e05b31f4 | 2245 | struct sk_buff *tipc_sk_socks_show(struct net *net) |
5a9ee0be | 2246 | { |
e05b31f4 | 2247 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
07f6c4bc YX |
2248 | const struct bucket_table *tbl; |
2249 | struct rhash_head *pos; | |
5a9ee0be JPM |
2250 | struct sk_buff *buf; |
2251 | struct tlv_desc *rep_tlv; | |
2252 | char *pb; | |
2253 | int pb_len; | |
2254 | struct tipc_sock *tsk; | |
2255 | int str_len = 0; | |
07f6c4bc | 2256 | int i; |
5a9ee0be JPM |
2257 | |
2258 | buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); | |
2259 | if (!buf) | |
2260 | return NULL; | |
2261 | rep_tlv = (struct tlv_desc *)buf->data; | |
2262 | pb = TLV_DATA(rep_tlv); | |
2263 | pb_len = ULTRA_STRING_MAX_LEN; | |
2264 | ||
07f6c4bc | 2265 | rcu_read_lock(); |
e05b31f4 | 2266 | tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); |
07f6c4bc YX |
2267 | for (i = 0; i < tbl->size; i++) { |
2268 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { | |
2269 | spin_lock_bh(&tsk->sk.sk_lock.slock); | |
2270 | str_len += tipc_sk_show(tsk, pb + str_len, | |
2271 | pb_len - str_len, 0); | |
2272 | spin_unlock_bh(&tsk->sk.sk_lock.slock); | |
2273 | } | |
5a9ee0be | 2274 | } |
07f6c4bc YX |
2275 | rcu_read_unlock(); |
2276 | ||
5a9ee0be JPM |
2277 | str_len += 1; /* for "\0" */ |
2278 | skb_put(buf, TLV_SPACE(str_len)); | |
2279 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
2280 | ||
2281 | return buf; | |
2282 | } | |
2283 | ||
2284 | /* tipc_sk_reinit: set non-zero address in all existing sockets | |
2285 | * when we go from standalone to network mode. | |
2286 | */ | |
e05b31f4 | 2287 | void tipc_sk_reinit(struct net *net) |
5a9ee0be | 2288 | { |
e05b31f4 | 2289 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
07f6c4bc YX |
2290 | const struct bucket_table *tbl; |
2291 | struct rhash_head *pos; | |
2292 | struct tipc_sock *tsk; | |
5a9ee0be | 2293 | struct tipc_msg *msg; |
07f6c4bc | 2294 | int i; |
5a9ee0be | 2295 | |
07f6c4bc | 2296 | rcu_read_lock(); |
e05b31f4 | 2297 | tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); |
07f6c4bc YX |
2298 | for (i = 0; i < tbl->size; i++) { |
2299 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { | |
2300 | spin_lock_bh(&tsk->sk.sk_lock.slock); | |
2301 | msg = &tsk->phdr; | |
2302 | msg_set_prevnode(msg, tipc_own_addr); | |
2303 | msg_set_orignode(msg, tipc_own_addr); | |
2304 | spin_unlock_bh(&tsk->sk.sk_lock.slock); | |
2305 | } | |
5a9ee0be | 2306 | } |
07f6c4bc | 2307 | rcu_read_unlock(); |
5a9ee0be JPM |
2308 | } |
2309 | ||
e05b31f4 | 2310 | static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid) |
808d90f9 | 2311 | { |
e05b31f4 | 2312 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
07f6c4bc | 2313 | struct tipc_sock *tsk; |
808d90f9 | 2314 | |
07f6c4bc | 2315 | rcu_read_lock(); |
e05b31f4 | 2316 | tsk = rhashtable_lookup(&tn->sk_rht, &portid); |
07f6c4bc YX |
2317 | if (tsk) |
2318 | sock_hold(&tsk->sk); | |
2319 | rcu_read_unlock(); | |
808d90f9 | 2320 | |
07f6c4bc | 2321 | return tsk; |
808d90f9 JPM |
2322 | } |
2323 | ||
07f6c4bc | 2324 | static int tipc_sk_insert(struct tipc_sock *tsk) |
808d90f9 | 2325 | { |
e05b31f4 YX |
2326 | struct sock *sk = &tsk->sk; |
2327 | struct net *net = sock_net(sk); | |
2328 | struct tipc_net *tn = net_generic(net, tipc_net_id); | |
07f6c4bc YX |
2329 | u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1; |
2330 | u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT; | |
808d90f9 | 2331 | |
07f6c4bc YX |
2332 | while (remaining--) { |
2333 | portid++; | |
2334 | if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT)) | |
2335 | portid = TIPC_MIN_PORT; | |
2336 | tsk->portid = portid; | |
2337 | sock_hold(&tsk->sk); | |
e05b31f4 | 2338 | if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node)) |
07f6c4bc YX |
2339 | return 0; |
2340 | sock_put(&tsk->sk); | |
808d90f9 JPM |
2341 | } |
2342 | ||
07f6c4bc | 2343 | return -1; |
808d90f9 JPM |
2344 | } |
2345 | ||
07f6c4bc | 2346 | static void tipc_sk_remove(struct tipc_sock *tsk) |
808d90f9 | 2347 | { |
07f6c4bc | 2348 | struct sock *sk = &tsk->sk; |
e05b31f4 | 2349 | struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id); |
808d90f9 | 2350 | |
e05b31f4 | 2351 | if (rhashtable_remove(&tn->sk_rht, &tsk->node)) { |
07f6c4bc YX |
2352 | WARN_ON(atomic_read(&sk->sk_refcnt) == 1); |
2353 | __sock_put(sk); | |
808d90f9 | 2354 | } |
808d90f9 JPM |
2355 | } |
2356 | ||
e05b31f4 | 2357 | int tipc_sk_rht_init(struct net *net) |
808d90f9 | 2358 | { |
e05b31f4 | 2359 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
07f6c4bc YX |
2360 | struct rhashtable_params rht_params = { |
2361 | .nelem_hint = 192, | |
2362 | .head_offset = offsetof(struct tipc_sock, node), | |
2363 | .key_offset = offsetof(struct tipc_sock, portid), | |
2364 | .key_len = sizeof(u32), /* portid */ | |
2365 | .hashfn = jhash, | |
2366 | .max_shift = 20, /* 1M */ | |
2367 | .min_shift = 8, /* 256 */ | |
2368 | .grow_decision = rht_grow_above_75, | |
2369 | .shrink_decision = rht_shrink_below_30, | |
2370 | }; | |
808d90f9 | 2371 | |
e05b31f4 | 2372 | return rhashtable_init(&tn->sk_rht, &rht_params); |
808d90f9 JPM |
2373 | } |
2374 | ||
e05b31f4 | 2375 | void tipc_sk_rht_destroy(struct net *net) |
808d90f9 | 2376 | { |
e05b31f4 YX |
2377 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
2378 | ||
07f6c4bc YX |
2379 | /* Wait for socket readers to complete */ |
2380 | synchronize_net(); | |
808d90f9 | 2381 | |
e05b31f4 | 2382 | rhashtable_destroy(&tn->sk_rht); |
808d90f9 JPM |
2383 | } |
2384 | ||
b97bf3fd | 2385 | /** |
247f0f3c | 2386 | * tipc_setsockopt - set socket option |
b97bf3fd PL |
2387 | * @sock: socket structure |
2388 | * @lvl: option level | |
2389 | * @opt: option identifier | |
2390 | * @ov: pointer to new option value | |
2391 | * @ol: length of option value | |
c4307285 YH |
2392 | * |
2393 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options | |
b97bf3fd | 2394 | * (to ease compatibility). |
c4307285 | 2395 | * |
b97bf3fd PL |
2396 | * Returns 0 on success, errno otherwise |
2397 | */ | |
247f0f3c YX |
2398 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
2399 | char __user *ov, unsigned int ol) | |
b97bf3fd | 2400 | { |
0c3141e9 | 2401 | struct sock *sk = sock->sk; |
58ed9442 | 2402 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
2403 | u32 value; |
2404 | int res; | |
2405 | ||
c4307285 YH |
2406 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2407 | return 0; | |
b97bf3fd PL |
2408 | if (lvl != SOL_TIPC) |
2409 | return -ENOPROTOOPT; | |
2410 | if (ol < sizeof(value)) | |
2411 | return -EINVAL; | |
2db9983a AS |
2412 | res = get_user(value, (u32 __user *)ov); |
2413 | if (res) | |
b97bf3fd PL |
2414 | return res; |
2415 | ||
0c3141e9 | 2416 | lock_sock(sk); |
c4307285 | 2417 | |
b97bf3fd PL |
2418 | switch (opt) { |
2419 | case TIPC_IMPORTANCE: | |
301bae56 | 2420 | res = tsk_set_importance(tsk, value); |
b97bf3fd PL |
2421 | break; |
2422 | case TIPC_SRC_DROPPABLE: | |
2423 | if (sock->type != SOCK_STREAM) | |
301bae56 | 2424 | tsk_set_unreliable(tsk, value); |
c4307285 | 2425 | else |
b97bf3fd PL |
2426 | res = -ENOPROTOOPT; |
2427 | break; | |
2428 | case TIPC_DEST_DROPPABLE: | |
301bae56 | 2429 | tsk_set_unreturnable(tsk, value); |
b97bf3fd PL |
2430 | break; |
2431 | case TIPC_CONN_TIMEOUT: | |
a0f40f02 | 2432 | tipc_sk(sk)->conn_timeout = value; |
0c3141e9 | 2433 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd PL |
2434 | break; |
2435 | default: | |
2436 | res = -EINVAL; | |
2437 | } | |
2438 | ||
0c3141e9 AS |
2439 | release_sock(sk); |
2440 | ||
b97bf3fd PL |
2441 | return res; |
2442 | } | |
2443 | ||
2444 | /** | |
247f0f3c | 2445 | * tipc_getsockopt - get socket option |
b97bf3fd PL |
2446 | * @sock: socket structure |
2447 | * @lvl: option level | |
2448 | * @opt: option identifier | |
2449 | * @ov: receptacle for option value | |
2450 | * @ol: receptacle for length of option value | |
c4307285 YH |
2451 | * |
2452 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options | |
b97bf3fd | 2453 | * (to ease compatibility). |
c4307285 | 2454 | * |
b97bf3fd PL |
2455 | * Returns 0 on success, errno otherwise |
2456 | */ | |
247f0f3c YX |
2457 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
2458 | char __user *ov, int __user *ol) | |
b97bf3fd | 2459 | { |
0c3141e9 | 2460 | struct sock *sk = sock->sk; |
58ed9442 | 2461 | struct tipc_sock *tsk = tipc_sk(sk); |
c4307285 | 2462 | int len; |
b97bf3fd | 2463 | u32 value; |
c4307285 | 2464 | int res; |
b97bf3fd | 2465 | |
c4307285 YH |
2466 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2467 | return put_user(0, ol); | |
b97bf3fd PL |
2468 | if (lvl != SOL_TIPC) |
2469 | return -ENOPROTOOPT; | |
2db9983a AS |
2470 | res = get_user(len, ol); |
2471 | if (res) | |
c4307285 | 2472 | return res; |
b97bf3fd | 2473 | |
0c3141e9 | 2474 | lock_sock(sk); |
b97bf3fd PL |
2475 | |
2476 | switch (opt) { | |
2477 | case TIPC_IMPORTANCE: | |
301bae56 | 2478 | value = tsk_importance(tsk); |
b97bf3fd PL |
2479 | break; |
2480 | case TIPC_SRC_DROPPABLE: | |
301bae56 | 2481 | value = tsk_unreliable(tsk); |
b97bf3fd PL |
2482 | break; |
2483 | case TIPC_DEST_DROPPABLE: | |
301bae56 | 2484 | value = tsk_unreturnable(tsk); |
b97bf3fd PL |
2485 | break; |
2486 | case TIPC_CONN_TIMEOUT: | |
301bae56 | 2487 | value = tsk->conn_timeout; |
0c3141e9 | 2488 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd | 2489 | break; |
0e65967e | 2490 | case TIPC_NODE_RECVQ_DEPTH: |
9da3d475 | 2491 | value = 0; /* was tipc_queue_size, now obsolete */ |
6650613d | 2492 | break; |
0e65967e | 2493 | case TIPC_SOCK_RECVQ_DEPTH: |
6650613d | 2494 | value = skb_queue_len(&sk->sk_receive_queue); |
2495 | break; | |
b97bf3fd PL |
2496 | default: |
2497 | res = -EINVAL; | |
2498 | } | |
2499 | ||
0c3141e9 AS |
2500 | release_sock(sk); |
2501 | ||
25860c3b PG |
2502 | if (res) |
2503 | return res; /* "get" failed */ | |
b97bf3fd | 2504 | |
25860c3b PG |
2505 | if (len < sizeof(value)) |
2506 | return -EINVAL; | |
2507 | ||
2508 | if (copy_to_user(ov, &value, sizeof(value))) | |
2509 | return -EFAULT; | |
2510 | ||
2511 | return put_user(sizeof(value), ol); | |
b97bf3fd PL |
2512 | } |
2513 | ||
f2f9800d | 2514 | static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
78acb1f9 | 2515 | { |
f2f9800d | 2516 | struct sock *sk = sock->sk; |
78acb1f9 EH |
2517 | struct tipc_sioc_ln_req lnr; |
2518 | void __user *argp = (void __user *)arg; | |
2519 | ||
2520 | switch (cmd) { | |
2521 | case SIOCGETLINKNAME: | |
2522 | if (copy_from_user(&lnr, argp, sizeof(lnr))) | |
2523 | return -EFAULT; | |
f2f9800d YX |
2524 | if (!tipc_node_get_linkname(sock_net(sk), |
2525 | lnr.bearer_id & 0xffff, lnr.peer, | |
78acb1f9 EH |
2526 | lnr.linkname, TIPC_MAX_LINK_NAME)) { |
2527 | if (copy_to_user(argp, &lnr, sizeof(lnr))) | |
2528 | return -EFAULT; | |
2529 | return 0; | |
2530 | } | |
2531 | return -EADDRNOTAVAIL; | |
78acb1f9 EH |
2532 | default: |
2533 | return -ENOIOCTLCMD; | |
2534 | } | |
2535 | } | |
2536 | ||
ae86b9e3 BH |
2537 | /* Protocol switches for the various types of TIPC sockets */ |
2538 | ||
bca65eae | 2539 | static const struct proto_ops msg_ops = { |
0e65967e | 2540 | .owner = THIS_MODULE, |
b97bf3fd | 2541 | .family = AF_TIPC, |
247f0f3c YX |
2542 | .release = tipc_release, |
2543 | .bind = tipc_bind, | |
2544 | .connect = tipc_connect, | |
5eee6a6d | 2545 | .socketpair = sock_no_socketpair, |
245f3d34 | 2546 | .accept = sock_no_accept, |
247f0f3c YX |
2547 | .getname = tipc_getname, |
2548 | .poll = tipc_poll, | |
78acb1f9 | 2549 | .ioctl = tipc_ioctl, |
245f3d34 | 2550 | .listen = sock_no_listen, |
247f0f3c YX |
2551 | .shutdown = tipc_shutdown, |
2552 | .setsockopt = tipc_setsockopt, | |
2553 | .getsockopt = tipc_getsockopt, | |
2554 | .sendmsg = tipc_sendmsg, | |
2555 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2556 | .mmap = sock_no_mmap, |
2557 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2558 | }; |
2559 | ||
bca65eae | 2560 | static const struct proto_ops packet_ops = { |
0e65967e | 2561 | .owner = THIS_MODULE, |
b97bf3fd | 2562 | .family = AF_TIPC, |
247f0f3c YX |
2563 | .release = tipc_release, |
2564 | .bind = tipc_bind, | |
2565 | .connect = tipc_connect, | |
5eee6a6d | 2566 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2567 | .accept = tipc_accept, |
2568 | .getname = tipc_getname, | |
2569 | .poll = tipc_poll, | |
78acb1f9 | 2570 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2571 | .listen = tipc_listen, |
2572 | .shutdown = tipc_shutdown, | |
2573 | .setsockopt = tipc_setsockopt, | |
2574 | .getsockopt = tipc_getsockopt, | |
2575 | .sendmsg = tipc_send_packet, | |
2576 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2577 | .mmap = sock_no_mmap, |
2578 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2579 | }; |
2580 | ||
bca65eae | 2581 | static const struct proto_ops stream_ops = { |
0e65967e | 2582 | .owner = THIS_MODULE, |
b97bf3fd | 2583 | .family = AF_TIPC, |
247f0f3c YX |
2584 | .release = tipc_release, |
2585 | .bind = tipc_bind, | |
2586 | .connect = tipc_connect, | |
5eee6a6d | 2587 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2588 | .accept = tipc_accept, |
2589 | .getname = tipc_getname, | |
2590 | .poll = tipc_poll, | |
78acb1f9 | 2591 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2592 | .listen = tipc_listen, |
2593 | .shutdown = tipc_shutdown, | |
2594 | .setsockopt = tipc_setsockopt, | |
2595 | .getsockopt = tipc_getsockopt, | |
2596 | .sendmsg = tipc_send_stream, | |
2597 | .recvmsg = tipc_recv_stream, | |
8238745a YH |
2598 | .mmap = sock_no_mmap, |
2599 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2600 | }; |
2601 | ||
bca65eae | 2602 | static const struct net_proto_family tipc_family_ops = { |
0e65967e | 2603 | .owner = THIS_MODULE, |
b97bf3fd | 2604 | .family = AF_TIPC, |
c5fa7b3c | 2605 | .create = tipc_sk_create |
b97bf3fd PL |
2606 | }; |
2607 | ||
2608 | static struct proto tipc_proto = { | |
2609 | .name = "TIPC", | |
2610 | .owner = THIS_MODULE, | |
cc79dd1b YX |
2611 | .obj_size = sizeof(struct tipc_sock), |
2612 | .sysctl_rmem = sysctl_tipc_rmem | |
b97bf3fd PL |
2613 | }; |
2614 | ||
c5fa7b3c YX |
2615 | static struct proto tipc_proto_kern = { |
2616 | .name = "TIPC", | |
2617 | .obj_size = sizeof(struct tipc_sock), | |
2618 | .sysctl_rmem = sysctl_tipc_rmem | |
2619 | }; | |
2620 | ||
b97bf3fd | 2621 | /** |
4323add6 | 2622 | * tipc_socket_init - initialize TIPC socket interface |
c4307285 | 2623 | * |
b97bf3fd PL |
2624 | * Returns 0 on success, errno otherwise |
2625 | */ | |
4323add6 | 2626 | int tipc_socket_init(void) |
b97bf3fd PL |
2627 | { |
2628 | int res; | |
2629 | ||
c4307285 | 2630 | res = proto_register(&tipc_proto, 1); |
b97bf3fd | 2631 | if (res) { |
2cf8aa19 | 2632 | pr_err("Failed to register TIPC protocol type\n"); |
b97bf3fd PL |
2633 | goto out; |
2634 | } | |
2635 | ||
2636 | res = sock_register(&tipc_family_ops); | |
2637 | if (res) { | |
2cf8aa19 | 2638 | pr_err("Failed to register TIPC socket type\n"); |
b97bf3fd PL |
2639 | proto_unregister(&tipc_proto); |
2640 | goto out; | |
2641 | } | |
b97bf3fd PL |
2642 | out: |
2643 | return res; | |
2644 | } | |
2645 | ||
2646 | /** | |
4323add6 | 2647 | * tipc_socket_stop - stop TIPC socket interface |
b97bf3fd | 2648 | */ |
4323add6 | 2649 | void tipc_socket_stop(void) |
b97bf3fd | 2650 | { |
b97bf3fd PL |
2651 | sock_unregister(tipc_family_ops.family); |
2652 | proto_unregister(&tipc_proto); | |
2653 | } | |
34b78a12 RA |
2654 | |
2655 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 | 2656 | static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk) |
34b78a12 RA |
2657 | { |
2658 | u32 peer_node; | |
2659 | u32 peer_port; | |
2660 | struct nlattr *nest; | |
2661 | ||
2662 | peer_node = tsk_peer_node(tsk); | |
2663 | peer_port = tsk_peer_port(tsk); | |
2664 | ||
2665 | nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON); | |
2666 | ||
2667 | if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node)) | |
2668 | goto msg_full; | |
2669 | if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port)) | |
2670 | goto msg_full; | |
2671 | ||
2672 | if (tsk->conn_type != 0) { | |
2673 | if (nla_put_flag(skb, TIPC_NLA_CON_FLAG)) | |
2674 | goto msg_full; | |
2675 | if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type)) | |
2676 | goto msg_full; | |
2677 | if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance)) | |
2678 | goto msg_full; | |
2679 | } | |
2680 | nla_nest_end(skb, nest); | |
2681 | ||
2682 | return 0; | |
2683 | ||
2684 | msg_full: | |
2685 | nla_nest_cancel(skb, nest); | |
2686 | ||
2687 | return -EMSGSIZE; | |
2688 | } | |
2689 | ||
2690 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2691 | static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb, |
2692 | struct tipc_sock *tsk) | |
34b78a12 RA |
2693 | { |
2694 | int err; | |
2695 | void *hdr; | |
2696 | struct nlattr *attrs; | |
2697 | ||
2698 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, | |
2699 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); | |
2700 | if (!hdr) | |
2701 | goto msg_cancel; | |
2702 | ||
2703 | attrs = nla_nest_start(skb, TIPC_NLA_SOCK); | |
2704 | if (!attrs) | |
2705 | goto genlmsg_cancel; | |
07f6c4bc | 2706 | if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid)) |
34b78a12 RA |
2707 | goto attr_msg_cancel; |
2708 | if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr)) | |
2709 | goto attr_msg_cancel; | |
2710 | ||
2711 | if (tsk->connected) { | |
2712 | err = __tipc_nl_add_sk_con(skb, tsk); | |
2713 | if (err) | |
2714 | goto attr_msg_cancel; | |
2715 | } else if (!list_empty(&tsk->publications)) { | |
2716 | if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL)) | |
2717 | goto attr_msg_cancel; | |
2718 | } | |
2719 | nla_nest_end(skb, attrs); | |
2720 | genlmsg_end(skb, hdr); | |
2721 | ||
2722 | return 0; | |
2723 | ||
2724 | attr_msg_cancel: | |
2725 | nla_nest_cancel(skb, attrs); | |
2726 | genlmsg_cancel: | |
2727 | genlmsg_cancel(skb, hdr); | |
2728 | msg_cancel: | |
2729 | return -EMSGSIZE; | |
2730 | } | |
2731 | ||
2732 | int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2733 | { | |
2734 | int err; | |
2735 | struct tipc_sock *tsk; | |
07f6c4bc YX |
2736 | const struct bucket_table *tbl; |
2737 | struct rhash_head *pos; | |
2738 | u32 prev_portid = cb->args[0]; | |
2739 | u32 portid = prev_portid; | |
e05b31f4 YX |
2740 | struct net *net = sock_net(skb->sk); |
2741 | struct tipc_net *tn = net_generic(net, tipc_net_id); | |
07f6c4bc | 2742 | int i; |
34b78a12 | 2743 | |
07f6c4bc | 2744 | rcu_read_lock(); |
e05b31f4 | 2745 | tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); |
07f6c4bc YX |
2746 | for (i = 0; i < tbl->size; i++) { |
2747 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { | |
2748 | spin_lock_bh(&tsk->sk.sk_lock.slock); | |
2749 | portid = tsk->portid; | |
2750 | err = __tipc_nl_add_sk(skb, cb, tsk); | |
2751 | spin_unlock_bh(&tsk->sk.sk_lock.slock); | |
2752 | if (err) | |
2753 | break; | |
2754 | ||
2755 | prev_portid = portid; | |
2756 | } | |
34b78a12 | 2757 | } |
07f6c4bc | 2758 | rcu_read_unlock(); |
34b78a12 | 2759 | |
07f6c4bc | 2760 | cb->args[0] = prev_portid; |
34b78a12 RA |
2761 | |
2762 | return skb->len; | |
2763 | } | |
1a1a143d RA |
2764 | |
2765 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2766 | static int __tipc_nl_add_sk_publ(struct sk_buff *skb, |
2767 | struct netlink_callback *cb, | |
2768 | struct publication *publ) | |
1a1a143d RA |
2769 | { |
2770 | void *hdr; | |
2771 | struct nlattr *attrs; | |
2772 | ||
2773 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, | |
2774 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET); | |
2775 | if (!hdr) | |
2776 | goto msg_cancel; | |
2777 | ||
2778 | attrs = nla_nest_start(skb, TIPC_NLA_PUBL); | |
2779 | if (!attrs) | |
2780 | goto genlmsg_cancel; | |
2781 | ||
2782 | if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) | |
2783 | goto attr_msg_cancel; | |
2784 | if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type)) | |
2785 | goto attr_msg_cancel; | |
2786 | if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower)) | |
2787 | goto attr_msg_cancel; | |
2788 | if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper)) | |
2789 | goto attr_msg_cancel; | |
2790 | ||
2791 | nla_nest_end(skb, attrs); | |
2792 | genlmsg_end(skb, hdr); | |
2793 | ||
2794 | return 0; | |
2795 | ||
2796 | attr_msg_cancel: | |
2797 | nla_nest_cancel(skb, attrs); | |
2798 | genlmsg_cancel: | |
2799 | genlmsg_cancel(skb, hdr); | |
2800 | msg_cancel: | |
2801 | return -EMSGSIZE; | |
2802 | } | |
2803 | ||
2804 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2805 | static int __tipc_nl_list_sk_publ(struct sk_buff *skb, |
2806 | struct netlink_callback *cb, | |
2807 | struct tipc_sock *tsk, u32 *last_publ) | |
1a1a143d RA |
2808 | { |
2809 | int err; | |
2810 | struct publication *p; | |
2811 | ||
2812 | if (*last_publ) { | |
2813 | list_for_each_entry(p, &tsk->publications, pport_list) { | |
2814 | if (p->key == *last_publ) | |
2815 | break; | |
2816 | } | |
2817 | if (p->key != *last_publ) { | |
2818 | /* We never set seq or call nl_dump_check_consistent() | |
2819 | * this means that setting prev_seq here will cause the | |
2820 | * consistence check to fail in the netlink callback | |
2821 | * handler. Resulting in the last NLMSG_DONE message | |
2822 | * having the NLM_F_DUMP_INTR flag set. | |
2823 | */ | |
2824 | cb->prev_seq = 1; | |
2825 | *last_publ = 0; | |
2826 | return -EPIPE; | |
2827 | } | |
2828 | } else { | |
2829 | p = list_first_entry(&tsk->publications, struct publication, | |
2830 | pport_list); | |
2831 | } | |
2832 | ||
2833 | list_for_each_entry_from(p, &tsk->publications, pport_list) { | |
2834 | err = __tipc_nl_add_sk_publ(skb, cb, p); | |
2835 | if (err) { | |
2836 | *last_publ = p->key; | |
2837 | return err; | |
2838 | } | |
2839 | } | |
2840 | *last_publ = 0; | |
2841 | ||
2842 | return 0; | |
2843 | } | |
2844 | ||
2845 | int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2846 | { | |
2847 | int err; | |
07f6c4bc | 2848 | u32 tsk_portid = cb->args[0]; |
1a1a143d RA |
2849 | u32 last_publ = cb->args[1]; |
2850 | u32 done = cb->args[2]; | |
e05b31f4 | 2851 | struct net *net = sock_net(skb->sk); |
1a1a143d RA |
2852 | struct tipc_sock *tsk; |
2853 | ||
07f6c4bc | 2854 | if (!tsk_portid) { |
1a1a143d RA |
2855 | struct nlattr **attrs; |
2856 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; | |
2857 | ||
2858 | err = tipc_nlmsg_parse(cb->nlh, &attrs); | |
2859 | if (err) | |
2860 | return err; | |
2861 | ||
2862 | err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, | |
2863 | attrs[TIPC_NLA_SOCK], | |
2864 | tipc_nl_sock_policy); | |
2865 | if (err) | |
2866 | return err; | |
2867 | ||
2868 | if (!sock[TIPC_NLA_SOCK_REF]) | |
2869 | return -EINVAL; | |
2870 | ||
07f6c4bc | 2871 | tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); |
1a1a143d RA |
2872 | } |
2873 | ||
2874 | if (done) | |
2875 | return 0; | |
2876 | ||
e05b31f4 | 2877 | tsk = tipc_sk_lookup(net, tsk_portid); |
1a1a143d RA |
2878 | if (!tsk) |
2879 | return -EINVAL; | |
2880 | ||
2881 | lock_sock(&tsk->sk); | |
2882 | err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ); | |
2883 | if (!err) | |
2884 | done = 1; | |
2885 | release_sock(&tsk->sk); | |
07f6c4bc | 2886 | sock_put(&tsk->sk); |
1a1a143d | 2887 | |
07f6c4bc | 2888 | cb->args[0] = tsk_portid; |
1a1a143d RA |
2889 | cb->args[1] = last_publ; |
2890 | cb->args[2] = done; | |
2891 | ||
2892 | return skb->len; | |
2893 | } |