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