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