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