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