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