]>
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 | { |
0c3141e9 | 247 | struct sk_buff *buf; |
8db1bae3 | 248 | u32 dnode; |
0c3141e9 | 249 | |
8db1bae3 JPM |
250 | while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { |
251 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) | |
9fbfb8b1 | 252 | tipc_link_xmit(buf, 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; |
b97bf3fd | 465 | struct sk_buff *buf; |
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) { |
0c3141e9 AS |
484 | buf = __skb_dequeue(&sk->sk_receive_queue); |
485 | if (buf == NULL) | |
b97bf3fd | 486 | break; |
40682432 | 487 | if (TIPC_SKB_CB(buf)->handle != NULL) |
5f6d9123 | 488 | kfree_skb(buf); |
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 | } |
8db1bae3 | 496 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) |
9fbfb8b1 | 497 | tipc_link_xmit(buf, 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) { | |
5b8fa7ce JPM |
505 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
506 | SHORT_H_SIZE, 0, dnode, tipc_own_addr, | |
301bae56 JPM |
507 | tsk_peer_port(tsk), |
508 | tsk->ref, TIPC_ERR_NO_PORT); | |
5b8fa7ce | 509 | if (buf) |
301bae56 JPM |
510 | tipc_link_xmit(buf, dnode, tsk->ref); |
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; |
0abd8ff2 JPM |
715 | struct sk_buff *buf; |
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(); | |
45dcc687 | 730 | rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &buf); |
0abd8ff2 JPM |
731 | if (unlikely(rc < 0)) |
732 | return rc; | |
733 | ||
734 | do { | |
735 | rc = tipc_bclink_xmit(buf); | |
736 | if (likely(rc >= 0)) { | |
737 | rc = dsz; | |
738 | break; | |
739 | } | |
740 | if (rc == -EMSGSIZE) | |
741 | goto new_mtu; | |
742 | if (rc != -ELINKCONG) | |
743 | break; | |
50100a5e | 744 | tipc_sk(sk)->link_cong = 1; |
0abd8ff2 JPM |
745 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
746 | if (rc) | |
747 | kfree_skb_list(buf); | |
748 | } while (!rc); | |
749 | return rc; | |
750 | } | |
751 | ||
078bec82 JPM |
752 | /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets |
753 | */ | |
754 | void tipc_sk_mcast_rcv(struct sk_buff *buf) | |
755 | { | |
756 | struct tipc_msg *msg = buf_msg(buf); | |
757 | struct tipc_port_list dports = {0, NULL, }; | |
758 | struct tipc_port_list *item; | |
759 | struct sk_buff *b; | |
760 | uint i, last, dst = 0; | |
761 | u32 scope = TIPC_CLUSTER_SCOPE; | |
762 | ||
763 | if (in_own_node(msg_orignode(msg))) | |
764 | scope = TIPC_NODE_SCOPE; | |
765 | ||
766 | /* Create destination port list: */ | |
767 | tipc_nametbl_mc_translate(msg_nametype(msg), | |
768 | msg_namelower(msg), | |
769 | msg_nameupper(msg), | |
770 | scope, | |
771 | &dports); | |
772 | last = dports.count; | |
773 | if (!last) { | |
774 | kfree_skb(buf); | |
775 | return; | |
776 | } | |
777 | ||
778 | for (item = &dports; item; item = item->next) { | |
779 | for (i = 0; i < PLSIZE && ++dst <= last; i++) { | |
780 | b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf; | |
781 | if (!b) { | |
782 | pr_warn("Failed do clone mcast rcv buffer\n"); | |
783 | continue; | |
784 | } | |
785 | msg_set_destport(msg, item->ports[i]); | |
786 | tipc_sk_rcv(b); | |
787 | } | |
788 | } | |
789 | tipc_port_list_free(&dports); | |
790 | } | |
791 | ||
ac0074ee JPM |
792 | /** |
793 | * tipc_sk_proto_rcv - receive a connection mng protocol message | |
794 | * @tsk: receiving socket | |
795 | * @dnode: node to send response message to, if any | |
796 | * @buf: buffer containing protocol message | |
797 | * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if | |
798 | * (CONN_PROBE_REPLY) message should be forwarded. | |
799 | */ | |
52f50ce5 WY |
800 | static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, |
801 | struct sk_buff *buf) | |
ac0074ee JPM |
802 | { |
803 | struct tipc_msg *msg = buf_msg(buf); | |
60120526 | 804 | int conn_cong; |
ac0074ee JPM |
805 | |
806 | /* Ignore if connection cannot be validated: */ | |
2e84c60b | 807 | if (!tsk_peer_msg(tsk, msg)) |
ac0074ee JPM |
808 | goto exit; |
809 | ||
301bae56 | 810 | tsk->probing_state = TIPC_CONN_OK; |
ac0074ee JPM |
811 | |
812 | if (msg_type(msg) == CONN_ACK) { | |
301bae56 | 813 | conn_cong = tsk_conn_cong(tsk); |
60120526 JPM |
814 | tsk->sent_unacked -= msg_msgcnt(msg); |
815 | if (conn_cong) | |
50100a5e | 816 | tsk->sk.sk_write_space(&tsk->sk); |
ac0074ee JPM |
817 | } else if (msg_type(msg) == CONN_PROBE) { |
818 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) | |
819 | return TIPC_OK; | |
820 | msg_set_type(msg, CONN_PROBE_REPLY); | |
821 | return TIPC_FWD_MSG; | |
822 | } | |
823 | /* Do nothing if msg_type() == CONN_PROBE_REPLY */ | |
824 | exit: | |
825 | kfree_skb(buf); | |
826 | return TIPC_OK; | |
827 | } | |
828 | ||
c4307285 | 829 | /** |
b97bf3fd PL |
830 | * dest_name_check - verify user is permitted to send to specified port name |
831 | * @dest: destination address | |
832 | * @m: descriptor for message to be sent | |
c4307285 | 833 | * |
b97bf3fd PL |
834 | * Prevents restricted configuration commands from being issued by |
835 | * unauthorized users. | |
c4307285 | 836 | * |
b97bf3fd PL |
837 | * Returns 0 if permission is granted, otherwise errno |
838 | */ | |
05790c64 | 839 | static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) |
b97bf3fd PL |
840 | { |
841 | struct tipc_cfg_msg_hdr hdr; | |
842 | ||
e2dafe87 JPM |
843 | if (unlikely(dest->addrtype == TIPC_ADDR_ID)) |
844 | return 0; | |
c4307285 YH |
845 | if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) |
846 | return 0; | |
847 | if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) | |
848 | return 0; | |
c4307285 YH |
849 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
850 | return -EACCES; | |
b97bf3fd | 851 | |
3f8dd944 AS |
852 | if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr))) |
853 | return -EMSGSIZE; | |
c4307285 | 854 | if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) |
b97bf3fd | 855 | return -EFAULT; |
70cb2347 | 856 | if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) |
b97bf3fd | 857 | return -EACCES; |
c4307285 | 858 | |
b97bf3fd PL |
859 | return 0; |
860 | } | |
861 | ||
3f40504f YX |
862 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) |
863 | { | |
864 | struct sock *sk = sock->sk; | |
58ed9442 | 865 | struct tipc_sock *tsk = tipc_sk(sk); |
3f40504f YX |
866 | DEFINE_WAIT(wait); |
867 | int done; | |
868 | ||
869 | do { | |
870 | int err = sock_error(sk); | |
871 | if (err) | |
872 | return err; | |
873 | if (sock->state == SS_DISCONNECTING) | |
874 | return -EPIPE; | |
875 | if (!*timeo_p) | |
876 | return -EAGAIN; | |
877 | if (signal_pending(current)) | |
878 | return sock_intr_errno(*timeo_p); | |
879 | ||
880 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
60120526 | 881 | done = sk_wait_event(sk, timeo_p, !tsk->link_cong); |
3f40504f YX |
882 | finish_wait(sk_sleep(sk), &wait); |
883 | } while (!done); | |
884 | return 0; | |
885 | } | |
886 | ||
b97bf3fd | 887 | /** |
247f0f3c | 888 | * tipc_sendmsg - send message in connectionless manner |
0c3141e9 | 889 | * @iocb: if NULL, indicates that socket lock is already held |
b97bf3fd PL |
890 | * @sock: socket structure |
891 | * @m: message to send | |
e2dafe87 | 892 | * @dsz: amount of user data to be sent |
c4307285 | 893 | * |
b97bf3fd | 894 | * Message must have an destination specified explicitly. |
c4307285 | 895 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
b97bf3fd PL |
896 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
897 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) | |
c4307285 | 898 | * |
b97bf3fd PL |
899 | * Returns the number of bytes sent on success, or errno otherwise |
900 | */ | |
247f0f3c | 901 | static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, |
e2dafe87 | 902 | struct msghdr *m, size_t dsz) |
b97bf3fd | 903 | { |
e2dafe87 | 904 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
0c3141e9 | 905 | struct sock *sk = sock->sk; |
58ed9442 | 906 | struct tipc_sock *tsk = tipc_sk(sk); |
301bae56 | 907 | struct tipc_msg *mhdr = &tsk->phdr; |
e2dafe87 JPM |
908 | u32 dnode, dport; |
909 | struct sk_buff *buf; | |
910 | struct tipc_name_seq *seq = &dest->addr.nameseq; | |
911 | u32 mtu; | |
3f40504f | 912 | long timeo; |
e2dafe87 | 913 | int rc = -EINVAL; |
b97bf3fd PL |
914 | |
915 | if (unlikely(!dest)) | |
916 | return -EDESTADDRREQ; | |
e2dafe87 | 917 | |
51f9cc1f AS |
918 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
919 | (dest->family != AF_TIPC))) | |
b97bf3fd | 920 | return -EINVAL; |
e2dafe87 JPM |
921 | |
922 | if (dsz > TIPC_MAX_USER_MSG_SIZE) | |
c29c3f70 | 923 | return -EMSGSIZE; |
b97bf3fd | 924 | |
0c3141e9 AS |
925 | if (iocb) |
926 | lock_sock(sk); | |
927 | ||
e2dafe87 | 928 | if (unlikely(sock->state != SS_READY)) { |
0c3141e9 | 929 | if (sock->state == SS_LISTENING) { |
e2dafe87 | 930 | rc = -EPIPE; |
0c3141e9 AS |
931 | goto exit; |
932 | } | |
933 | if (sock->state != SS_UNCONNECTED) { | |
e2dafe87 | 934 | rc = -EISCONN; |
0c3141e9 AS |
935 | goto exit; |
936 | } | |
301bae56 | 937 | if (tsk->published) { |
e2dafe87 | 938 | rc = -EOPNOTSUPP; |
0c3141e9 AS |
939 | goto exit; |
940 | } | |
3388007b | 941 | if (dest->addrtype == TIPC_ADDR_NAME) { |
301bae56 JPM |
942 | tsk->conn_type = dest->addr.name.name.type; |
943 | tsk->conn_instance = dest->addr.name.name.instance; | |
3388007b | 944 | } |
b97bf3fd | 945 | } |
e2dafe87 JPM |
946 | rc = dest_name_check(dest, m); |
947 | if (rc) | |
948 | goto exit; | |
b97bf3fd | 949 | |
3f40504f | 950 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
e2dafe87 JPM |
951 | |
952 | if (dest->addrtype == TIPC_ADDR_MCAST) { | |
562640f3 | 953 | rc = tipc_sendmcast(sock, seq, m, dsz, timeo); |
e2dafe87 JPM |
954 | goto exit; |
955 | } else if (dest->addrtype == TIPC_ADDR_NAME) { | |
956 | u32 type = dest->addr.name.name.type; | |
957 | u32 inst = dest->addr.name.name.instance; | |
958 | u32 domain = dest->addr.name.domain; | |
959 | ||
960 | dnode = domain; | |
961 | msg_set_type(mhdr, TIPC_NAMED_MSG); | |
962 | msg_set_hdr_sz(mhdr, NAMED_H_SIZE); | |
963 | msg_set_nametype(mhdr, type); | |
964 | msg_set_nameinst(mhdr, inst); | |
965 | msg_set_lookup_scope(mhdr, tipc_addr_scope(domain)); | |
966 | dport = tipc_nametbl_translate(type, inst, &dnode); | |
967 | msg_set_destnode(mhdr, dnode); | |
968 | msg_set_destport(mhdr, dport); | |
969 | if (unlikely(!dport && !dnode)) { | |
970 | rc = -EHOSTUNREACH; | |
971 | goto exit; | |
c4307285 | 972 | } |
e2dafe87 JPM |
973 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
974 | dnode = dest->addr.id.node; | |
975 | msg_set_type(mhdr, TIPC_DIRECT_MSG); | |
976 | msg_set_lookup_scope(mhdr, 0); | |
977 | msg_set_destnode(mhdr, dnode); | |
978 | msg_set_destport(mhdr, dest->addr.id.ref); | |
979 | msg_set_hdr_sz(mhdr, BASIC_H_SIZE); | |
980 | } | |
981 | ||
982 | new_mtu: | |
301bae56 | 983 | mtu = tipc_node_get_mtu(dnode, tsk->ref); |
45dcc687 | 984 | rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &buf); |
e2dafe87 JPM |
985 | if (rc < 0) |
986 | goto exit; | |
987 | ||
988 | do { | |
50100a5e | 989 | TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong; |
301bae56 | 990 | rc = tipc_link_xmit(buf, dnode, tsk->ref); |
e2dafe87 JPM |
991 | if (likely(rc >= 0)) { |
992 | if (sock->state != SS_READY) | |
0c3141e9 | 993 | sock->state = SS_CONNECTING; |
e2dafe87 | 994 | rc = dsz; |
0c3141e9 | 995 | break; |
c4307285 | 996 | } |
e2dafe87 JPM |
997 | if (rc == -EMSGSIZE) |
998 | goto new_mtu; | |
e2dafe87 | 999 | if (rc != -ELINKCONG) |
0c3141e9 | 1000 | break; |
50100a5e | 1001 | tsk->link_cong = 1; |
e2dafe87 | 1002 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
70452dcb EH |
1003 | if (rc) |
1004 | kfree_skb_list(buf); | |
e2dafe87 | 1005 | } while (!rc); |
0c3141e9 AS |
1006 | exit: |
1007 | if (iocb) | |
1008 | release_sock(sk); | |
e2dafe87 JPM |
1009 | |
1010 | return rc; | |
b97bf3fd PL |
1011 | } |
1012 | ||
391a6dd1 YX |
1013 | static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) |
1014 | { | |
1015 | struct sock *sk = sock->sk; | |
58ed9442 | 1016 | struct tipc_sock *tsk = tipc_sk(sk); |
391a6dd1 YX |
1017 | DEFINE_WAIT(wait); |
1018 | int done; | |
1019 | ||
1020 | do { | |
1021 | int err = sock_error(sk); | |
1022 | if (err) | |
1023 | return err; | |
1024 | if (sock->state == SS_DISCONNECTING) | |
1025 | return -EPIPE; | |
1026 | else if (sock->state != SS_CONNECTED) | |
1027 | return -ENOTCONN; | |
1028 | if (!*timeo_p) | |
1029 | return -EAGAIN; | |
1030 | if (signal_pending(current)) | |
1031 | return sock_intr_errno(*timeo_p); | |
1032 | ||
1033 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1034 | done = sk_wait_event(sk, timeo_p, | |
60120526 | 1035 | (!tsk->link_cong && |
301bae56 JPM |
1036 | !tsk_conn_cong(tsk)) || |
1037 | !tsk->connected); | |
391a6dd1 YX |
1038 | finish_wait(sk_sleep(sk), &wait); |
1039 | } while (!done); | |
1040 | return 0; | |
1041 | } | |
1042 | ||
c4307285 | 1043 | /** |
4ccfe5e0 JPM |
1044 | * tipc_send_stream - send stream-oriented data |
1045 | * @iocb: (unused) | |
b97bf3fd | 1046 | * @sock: socket structure |
4ccfe5e0 JPM |
1047 | * @m: data to send |
1048 | * @dsz: total length of data to be transmitted | |
c4307285 | 1049 | * |
4ccfe5e0 | 1050 | * Used for SOCK_STREAM data. |
c4307285 | 1051 | * |
4ccfe5e0 JPM |
1052 | * Returns the number of bytes sent on success (or partial success), |
1053 | * or errno if no data sent | |
b97bf3fd | 1054 | */ |
4ccfe5e0 JPM |
1055 | static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, |
1056 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 1057 | { |
0c3141e9 | 1058 | struct sock *sk = sock->sk; |
58ed9442 | 1059 | struct tipc_sock *tsk = tipc_sk(sk); |
301bae56 | 1060 | struct tipc_msg *mhdr = &tsk->phdr; |
4ccfe5e0 | 1061 | struct sk_buff *buf; |
342dfc30 | 1062 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
301bae56 | 1063 | u32 ref = tsk->ref; |
4ccfe5e0 | 1064 | int rc = -EINVAL; |
391a6dd1 | 1065 | long timeo; |
4ccfe5e0 JPM |
1066 | u32 dnode; |
1067 | uint mtu, send, sent = 0; | |
b97bf3fd PL |
1068 | |
1069 | /* Handle implied connection establishment */ | |
4ccfe5e0 JPM |
1070 | if (unlikely(dest)) { |
1071 | rc = tipc_sendmsg(iocb, sock, m, dsz); | |
1072 | if (dsz && (dsz == rc)) | |
60120526 | 1073 | tsk->sent_unacked = 1; |
4ccfe5e0 JPM |
1074 | return rc; |
1075 | } | |
1076 | if (dsz > (uint)INT_MAX) | |
c29c3f70 AS |
1077 | return -EMSGSIZE; |
1078 | ||
0c3141e9 AS |
1079 | if (iocb) |
1080 | lock_sock(sk); | |
b97bf3fd | 1081 | |
391a6dd1 YX |
1082 | if (unlikely(sock->state != SS_CONNECTED)) { |
1083 | if (sock->state == SS_DISCONNECTING) | |
4ccfe5e0 | 1084 | rc = -EPIPE; |
391a6dd1 | 1085 | else |
4ccfe5e0 | 1086 | rc = -ENOTCONN; |
391a6dd1 YX |
1087 | goto exit; |
1088 | } | |
1d835874 | 1089 | |
391a6dd1 | 1090 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
301bae56 | 1091 | dnode = tsk_peer_node(tsk); |
4ccfe5e0 JPM |
1092 | |
1093 | next: | |
301bae56 | 1094 | mtu = tsk->max_pkt; |
4ccfe5e0 | 1095 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); |
45dcc687 | 1096 | rc = tipc_msg_build(mhdr, m, sent, send, mtu, &buf); |
4ccfe5e0 JPM |
1097 | if (unlikely(rc < 0)) |
1098 | goto exit; | |
c4307285 | 1099 | do { |
301bae56 | 1100 | if (likely(!tsk_conn_cong(tsk))) { |
9fbfb8b1 | 1101 | rc = tipc_link_xmit(buf, dnode, ref); |
4ccfe5e0 | 1102 | if (likely(!rc)) { |
60120526 | 1103 | tsk->sent_unacked++; |
4ccfe5e0 JPM |
1104 | sent += send; |
1105 | if (sent == dsz) | |
1106 | break; | |
1107 | goto next; | |
1108 | } | |
1109 | if (rc == -EMSGSIZE) { | |
301bae56 | 1110 | tsk->max_pkt = tipc_node_get_mtu(dnode, ref); |
4ccfe5e0 JPM |
1111 | goto next; |
1112 | } | |
1113 | if (rc != -ELINKCONG) | |
1114 | break; | |
50100a5e | 1115 | tsk->link_cong = 1; |
4ccfe5e0 JPM |
1116 | } |
1117 | rc = tipc_wait_for_sndpkt(sock, &timeo); | |
70452dcb EH |
1118 | if (rc) |
1119 | kfree_skb_list(buf); | |
4ccfe5e0 | 1120 | } while (!rc); |
391a6dd1 | 1121 | exit: |
0c3141e9 AS |
1122 | if (iocb) |
1123 | release_sock(sk); | |
4ccfe5e0 | 1124 | return sent ? sent : rc; |
b97bf3fd PL |
1125 | } |
1126 | ||
c4307285 | 1127 | /** |
4ccfe5e0 JPM |
1128 | * tipc_send_packet - send a connection-oriented message |
1129 | * @iocb: if NULL, indicates that socket lock is already held | |
b97bf3fd | 1130 | * @sock: socket structure |
4ccfe5e0 JPM |
1131 | * @m: message to send |
1132 | * @dsz: length of data to be transmitted | |
c4307285 | 1133 | * |
4ccfe5e0 | 1134 | * Used for SOCK_SEQPACKET messages. |
c4307285 | 1135 | * |
4ccfe5e0 | 1136 | * Returns the number of bytes sent on success, or errno otherwise |
b97bf3fd | 1137 | */ |
4ccfe5e0 JPM |
1138 | static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, |
1139 | struct msghdr *m, size_t dsz) | |
b97bf3fd | 1140 | { |
4ccfe5e0 JPM |
1141 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
1142 | return -EMSGSIZE; | |
b97bf3fd | 1143 | |
4ccfe5e0 | 1144 | return tipc_send_stream(iocb, sock, m, dsz); |
b97bf3fd PL |
1145 | } |
1146 | ||
dadebc00 | 1147 | /* tipc_sk_finish_conn - complete the setup of a connection |
b97bf3fd | 1148 | */ |
301bae56 | 1149 | static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, |
dadebc00 | 1150 | u32 peer_node) |
b97bf3fd | 1151 | { |
301bae56 | 1152 | struct tipc_msg *msg = &tsk->phdr; |
b97bf3fd | 1153 | |
dadebc00 JPM |
1154 | msg_set_destnode(msg, peer_node); |
1155 | msg_set_destport(msg, peer_port); | |
1156 | msg_set_type(msg, TIPC_CONN_MSG); | |
1157 | msg_set_lookup_scope(msg, 0); | |
1158 | msg_set_hdr_sz(msg, SHORT_H_SIZE); | |
584d24b3 | 1159 | |
301bae56 JPM |
1160 | tsk->probing_interval = CONN_PROBING_INTERVAL; |
1161 | tsk->probing_state = TIPC_CONN_OK; | |
1162 | tsk->connected = 1; | |
1163 | k_start_timer(&tsk->timer, tsk->probing_interval); | |
1164 | tipc_node_add_conn(peer_node, tsk->ref, peer_port); | |
1165 | tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref); | |
b97bf3fd PL |
1166 | } |
1167 | ||
1168 | /** | |
1169 | * set_orig_addr - capture sender's address for received message | |
1170 | * @m: descriptor for message info | |
1171 | * @msg: received message header | |
c4307285 | 1172 | * |
b97bf3fd PL |
1173 | * Note: Address is not captured if not requested by receiver. |
1174 | */ | |
05790c64 | 1175 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
b97bf3fd | 1176 | { |
342dfc30 | 1177 | DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); |
b97bf3fd | 1178 | |
c4307285 | 1179 | if (addr) { |
b97bf3fd PL |
1180 | addr->family = AF_TIPC; |
1181 | addr->addrtype = TIPC_ADDR_ID; | |
60085c3d | 1182 | memset(&addr->addr, 0, sizeof(addr->addr)); |
b97bf3fd PL |
1183 | addr->addr.id.ref = msg_origport(msg); |
1184 | addr->addr.id.node = msg_orignode(msg); | |
0e65967e AS |
1185 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
1186 | addr->scope = 0; /* could leave uninitialized */ | |
b97bf3fd PL |
1187 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
1188 | } | |
1189 | } | |
1190 | ||
1191 | /** | |
301bae56 | 1192 | * tipc_sk_anc_data_recv - optionally capture ancillary data for received message |
b97bf3fd PL |
1193 | * @m: descriptor for message info |
1194 | * @msg: received message header | |
301bae56 | 1195 | * @tsk: TIPC port associated with message |
c4307285 | 1196 | * |
b97bf3fd | 1197 | * Note: Ancillary data is not captured if not requested by receiver. |
c4307285 | 1198 | * |
b97bf3fd PL |
1199 | * Returns 0 if successful, otherwise errno |
1200 | */ | |
301bae56 JPM |
1201 | static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
1202 | struct tipc_sock *tsk) | |
b97bf3fd PL |
1203 | { |
1204 | u32 anc_data[3]; | |
1205 | u32 err; | |
1206 | u32 dest_type; | |
3546c750 | 1207 | int has_name; |
b97bf3fd PL |
1208 | int res; |
1209 | ||
1210 | if (likely(m->msg_controllen == 0)) | |
1211 | return 0; | |
1212 | ||
1213 | /* Optionally capture errored message object(s) */ | |
b97bf3fd PL |
1214 | err = msg ? msg_errcode(msg) : 0; |
1215 | if (unlikely(err)) { | |
1216 | anc_data[0] = err; | |
1217 | anc_data[1] = msg_data_sz(msg); | |
2db9983a AS |
1218 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
1219 | if (res) | |
b97bf3fd | 1220 | return res; |
2db9983a AS |
1221 | if (anc_data[1]) { |
1222 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], | |
1223 | msg_data(msg)); | |
1224 | if (res) | |
1225 | return res; | |
1226 | } | |
b97bf3fd PL |
1227 | } |
1228 | ||
1229 | /* Optionally capture message destination object */ | |
b97bf3fd PL |
1230 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
1231 | switch (dest_type) { | |
1232 | case TIPC_NAMED_MSG: | |
3546c750 | 1233 | has_name = 1; |
b97bf3fd PL |
1234 | anc_data[0] = msg_nametype(msg); |
1235 | anc_data[1] = msg_namelower(msg); | |
1236 | anc_data[2] = msg_namelower(msg); | |
1237 | break; | |
1238 | case TIPC_MCAST_MSG: | |
3546c750 | 1239 | has_name = 1; |
b97bf3fd PL |
1240 | anc_data[0] = msg_nametype(msg); |
1241 | anc_data[1] = msg_namelower(msg); | |
1242 | anc_data[2] = msg_nameupper(msg); | |
1243 | break; | |
1244 | case TIPC_CONN_MSG: | |
301bae56 JPM |
1245 | has_name = (tsk->conn_type != 0); |
1246 | anc_data[0] = tsk->conn_type; | |
1247 | anc_data[1] = tsk->conn_instance; | |
1248 | anc_data[2] = tsk->conn_instance; | |
b97bf3fd PL |
1249 | break; |
1250 | default: | |
3546c750 | 1251 | has_name = 0; |
b97bf3fd | 1252 | } |
2db9983a AS |
1253 | if (has_name) { |
1254 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); | |
1255 | if (res) | |
1256 | return res; | |
1257 | } | |
b97bf3fd PL |
1258 | |
1259 | return 0; | |
1260 | } | |
1261 | ||
301bae56 | 1262 | static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack) |
739f5e4e JPM |
1263 | { |
1264 | struct sk_buff *buf = NULL; | |
1265 | struct tipc_msg *msg; | |
301bae56 JPM |
1266 | u32 peer_port = tsk_peer_port(tsk); |
1267 | u32 dnode = tsk_peer_node(tsk); | |
739f5e4e | 1268 | |
301bae56 | 1269 | if (!tsk->connected) |
739f5e4e JPM |
1270 | return; |
1271 | buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode, | |
301bae56 | 1272 | tipc_own_addr, peer_port, tsk->ref, TIPC_OK); |
739f5e4e JPM |
1273 | if (!buf) |
1274 | return; | |
1275 | msg = buf_msg(buf); | |
1276 | msg_set_msgcnt(msg, ack); | |
1277 | tipc_link_xmit(buf, dnode, msg_link_selector(msg)); | |
1278 | } | |
1279 | ||
85d3fc94 | 1280 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
9bbb4ecc YX |
1281 | { |
1282 | struct sock *sk = sock->sk; | |
1283 | DEFINE_WAIT(wait); | |
85d3fc94 | 1284 | long timeo = *timeop; |
9bbb4ecc YX |
1285 | int err; |
1286 | ||
1287 | for (;;) { | |
1288 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
fe8e4649 | 1289 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
9bbb4ecc YX |
1290 | if (sock->state == SS_DISCONNECTING) { |
1291 | err = -ENOTCONN; | |
1292 | break; | |
1293 | } | |
1294 | release_sock(sk); | |
1295 | timeo = schedule_timeout(timeo); | |
1296 | lock_sock(sk); | |
1297 | } | |
1298 | err = 0; | |
1299 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1300 | break; | |
1301 | err = sock_intr_errno(timeo); | |
1302 | if (signal_pending(current)) | |
1303 | break; | |
1304 | err = -EAGAIN; | |
1305 | if (!timeo) | |
1306 | break; | |
1307 | } | |
1308 | finish_wait(sk_sleep(sk), &wait); | |
85d3fc94 | 1309 | *timeop = timeo; |
9bbb4ecc YX |
1310 | return err; |
1311 | } | |
1312 | ||
c4307285 | 1313 | /** |
247f0f3c | 1314 | * tipc_recvmsg - receive packet-oriented message |
b97bf3fd PL |
1315 | * @iocb: (unused) |
1316 | * @m: descriptor for message info | |
1317 | * @buf_len: total size of user buffer area | |
1318 | * @flags: receive flags | |
c4307285 | 1319 | * |
b97bf3fd PL |
1320 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
1321 | * If the complete message doesn't fit in user area, truncate it. | |
1322 | * | |
1323 | * Returns size of returned message data, errno otherwise | |
1324 | */ | |
247f0f3c YX |
1325 | static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, |
1326 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1327 | { |
0c3141e9 | 1328 | struct sock *sk = sock->sk; |
58ed9442 | 1329 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
1330 | struct sk_buff *buf; |
1331 | struct tipc_msg *msg; | |
9bbb4ecc | 1332 | long timeo; |
b97bf3fd PL |
1333 | unsigned int sz; |
1334 | u32 err; | |
1335 | int res; | |
1336 | ||
0c3141e9 | 1337 | /* Catch invalid receive requests */ |
b97bf3fd PL |
1338 | if (unlikely(!buf_len)) |
1339 | return -EINVAL; | |
1340 | ||
0c3141e9 | 1341 | lock_sock(sk); |
b97bf3fd | 1342 | |
0c3141e9 AS |
1343 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
1344 | res = -ENOTCONN; | |
b97bf3fd PL |
1345 | goto exit; |
1346 | } | |
1347 | ||
9bbb4ecc | 1348 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
0c3141e9 | 1349 | restart: |
b97bf3fd | 1350 | |
0c3141e9 | 1351 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1352 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1353 | if (res) |
1354 | goto exit; | |
b97bf3fd | 1355 | |
0c3141e9 | 1356 | /* Look at first message in receive queue */ |
0c3141e9 | 1357 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1358 | msg = buf_msg(buf); |
1359 | sz = msg_data_sz(msg); | |
1360 | err = msg_errcode(msg); | |
1361 | ||
b97bf3fd | 1362 | /* Discard an empty non-errored message & try again */ |
b97bf3fd | 1363 | if ((!sz) && (!err)) { |
2e84c60b | 1364 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1365 | goto restart; |
1366 | } | |
1367 | ||
1368 | /* Capture sender's address (optional) */ | |
b97bf3fd PL |
1369 | set_orig_addr(m, msg); |
1370 | ||
1371 | /* Capture ancillary data (optional) */ | |
301bae56 | 1372 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
0c3141e9 | 1373 | if (res) |
b97bf3fd PL |
1374 | goto exit; |
1375 | ||
1376 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd PL |
1377 | if (!err) { |
1378 | if (unlikely(buf_len < sz)) { | |
1379 | sz = buf_len; | |
1380 | m->msg_flags |= MSG_TRUNC; | |
1381 | } | |
51f3d02b | 1382 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz); |
0232fd0a | 1383 | if (res) |
b97bf3fd | 1384 | goto exit; |
b97bf3fd PL |
1385 | res = sz; |
1386 | } else { | |
1387 | if ((sock->state == SS_READY) || | |
1388 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) | |
1389 | res = 0; | |
1390 | else | |
1391 | res = -ECONNRESET; | |
1392 | } | |
1393 | ||
1394 | /* Consume received message (optional) */ | |
b97bf3fd | 1395 | if (likely(!(flags & MSG_PEEK))) { |
99009806 | 1396 | if ((sock->state != SS_READY) && |
60120526 | 1397 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
301bae56 | 1398 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
60120526 JPM |
1399 | tsk->rcv_unacked = 0; |
1400 | } | |
2e84c60b | 1401 | tsk_advance_rx_queue(sk); |
c4307285 | 1402 | } |
b97bf3fd | 1403 | exit: |
0c3141e9 | 1404 | release_sock(sk); |
b97bf3fd PL |
1405 | return res; |
1406 | } | |
1407 | ||
c4307285 | 1408 | /** |
247f0f3c | 1409 | * tipc_recv_stream - receive stream-oriented data |
b97bf3fd PL |
1410 | * @iocb: (unused) |
1411 | * @m: descriptor for message info | |
1412 | * @buf_len: total size of user buffer area | |
1413 | * @flags: receive flags | |
c4307285 YH |
1414 | * |
1415 | * Used for SOCK_STREAM messages only. If not enough data is available | |
b97bf3fd PL |
1416 | * will optionally wait for more; never truncates data. |
1417 | * | |
1418 | * Returns size of returned message data, errno otherwise | |
1419 | */ | |
247f0f3c YX |
1420 | static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, |
1421 | struct msghdr *m, size_t buf_len, int flags) | |
b97bf3fd | 1422 | { |
0c3141e9 | 1423 | struct sock *sk = sock->sk; |
58ed9442 | 1424 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
1425 | struct sk_buff *buf; |
1426 | struct tipc_msg *msg; | |
9bbb4ecc | 1427 | long timeo; |
b97bf3fd | 1428 | unsigned int sz; |
3720d40b | 1429 | int sz_to_copy, target, needed; |
b97bf3fd | 1430 | int sz_copied = 0; |
b97bf3fd | 1431 | u32 err; |
0c3141e9 | 1432 | int res = 0; |
b97bf3fd | 1433 | |
0c3141e9 | 1434 | /* Catch invalid receive attempts */ |
b97bf3fd PL |
1435 | if (unlikely(!buf_len)) |
1436 | return -EINVAL; | |
1437 | ||
0c3141e9 | 1438 | lock_sock(sk); |
b97bf3fd | 1439 | |
9bbb4ecc | 1440 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
0c3141e9 | 1441 | res = -ENOTCONN; |
b97bf3fd PL |
1442 | goto exit; |
1443 | } | |
1444 | ||
3720d40b | 1445 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
9bbb4ecc | 1446 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
b97bf3fd | 1447 | |
617d3c7a | 1448 | restart: |
0c3141e9 | 1449 | /* Look for a message in receive queue; wait if necessary */ |
85d3fc94 | 1450 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
9bbb4ecc YX |
1451 | if (res) |
1452 | goto exit; | |
b97bf3fd | 1453 | |
0c3141e9 | 1454 | /* Look at first message in receive queue */ |
0c3141e9 | 1455 | buf = skb_peek(&sk->sk_receive_queue); |
b97bf3fd PL |
1456 | msg = buf_msg(buf); |
1457 | sz = msg_data_sz(msg); | |
1458 | err = msg_errcode(msg); | |
1459 | ||
1460 | /* Discard an empty non-errored message & try again */ | |
b97bf3fd | 1461 | if ((!sz) && (!err)) { |
2e84c60b | 1462 | tsk_advance_rx_queue(sk); |
b97bf3fd PL |
1463 | goto restart; |
1464 | } | |
1465 | ||
1466 | /* Optionally capture sender's address & ancillary data of first msg */ | |
b97bf3fd PL |
1467 | if (sz_copied == 0) { |
1468 | set_orig_addr(m, msg); | |
301bae56 | 1469 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
0c3141e9 | 1470 | if (res) |
b97bf3fd PL |
1471 | goto exit; |
1472 | } | |
1473 | ||
1474 | /* Capture message data (if valid) & compute return value (always) */ | |
b97bf3fd | 1475 | if (!err) { |
0232fd0a | 1476 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
b97bf3fd | 1477 | |
0232fd0a | 1478 | sz -= offset; |
b97bf3fd PL |
1479 | needed = (buf_len - sz_copied); |
1480 | sz_to_copy = (sz <= needed) ? sz : needed; | |
0232fd0a | 1481 | |
51f3d02b DM |
1482 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset, |
1483 | m, sz_to_copy); | |
0232fd0a | 1484 | if (res) |
b97bf3fd | 1485 | goto exit; |
0232fd0a | 1486 | |
b97bf3fd PL |
1487 | sz_copied += sz_to_copy; |
1488 | ||
1489 | if (sz_to_copy < sz) { | |
1490 | if (!(flags & MSG_PEEK)) | |
0232fd0a AS |
1491 | TIPC_SKB_CB(buf)->handle = |
1492 | (void *)(unsigned long)(offset + sz_to_copy); | |
b97bf3fd PL |
1493 | goto exit; |
1494 | } | |
b97bf3fd PL |
1495 | } else { |
1496 | if (sz_copied != 0) | |
1497 | goto exit; /* can't add error msg to valid data */ | |
1498 | ||
1499 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) | |
1500 | res = 0; | |
1501 | else | |
1502 | res = -ECONNRESET; | |
1503 | } | |
1504 | ||
1505 | /* Consume received message (optional) */ | |
b97bf3fd | 1506 | if (likely(!(flags & MSG_PEEK))) { |
60120526 | 1507 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
301bae56 | 1508 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
60120526 JPM |
1509 | tsk->rcv_unacked = 0; |
1510 | } | |
2e84c60b | 1511 | tsk_advance_rx_queue(sk); |
c4307285 | 1512 | } |
b97bf3fd PL |
1513 | |
1514 | /* Loop around if more data is required */ | |
f64f9e71 JP |
1515 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
1516 | (!skb_queue_empty(&sk->sk_receive_queue) || | |
3720d40b | 1517 | (sz_copied < target)) && /* and more is ready or required */ |
f64f9e71 JP |
1518 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
1519 | (!err)) /* and haven't reached a FIN */ | |
b97bf3fd PL |
1520 | goto restart; |
1521 | ||
1522 | exit: | |
0c3141e9 | 1523 | release_sock(sk); |
a3b0a5a9 | 1524 | return sz_copied ? sz_copied : res; |
b97bf3fd PL |
1525 | } |
1526 | ||
f288bef4 YX |
1527 | /** |
1528 | * tipc_write_space - wake up thread if port congestion is released | |
1529 | * @sk: socket | |
1530 | */ | |
1531 | static void tipc_write_space(struct sock *sk) | |
1532 | { | |
1533 | struct socket_wq *wq; | |
1534 | ||
1535 | rcu_read_lock(); | |
1536 | wq = rcu_dereference(sk->sk_wq); | |
1537 | if (wq_has_sleeper(wq)) | |
1538 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | | |
1539 | POLLWRNORM | POLLWRBAND); | |
1540 | rcu_read_unlock(); | |
1541 | } | |
1542 | ||
1543 | /** | |
1544 | * tipc_data_ready - wake up threads to indicate messages have been received | |
1545 | * @sk: socket | |
1546 | * @len: the length of messages | |
1547 | */ | |
676d2369 | 1548 | static void tipc_data_ready(struct sock *sk) |
f288bef4 YX |
1549 | { |
1550 | struct socket_wq *wq; | |
1551 | ||
1552 | rcu_read_lock(); | |
1553 | wq = rcu_dereference(sk->sk_wq); | |
1554 | if (wq_has_sleeper(wq)) | |
1555 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | | |
1556 | POLLRDNORM | POLLRDBAND); | |
1557 | rcu_read_unlock(); | |
1558 | } | |
1559 | ||
7e6c131e YX |
1560 | /** |
1561 | * filter_connect - Handle all incoming messages for a connection-based socket | |
58ed9442 | 1562 | * @tsk: TIPC socket |
7e6c131e YX |
1563 | * @msg: message |
1564 | * | |
b2ad5e5f | 1565 | * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise |
7e6c131e | 1566 | */ |
e4de5fab | 1567 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
7e6c131e | 1568 | { |
58ed9442 | 1569 | struct sock *sk = &tsk->sk; |
8826cde6 | 1570 | struct socket *sock = sk->sk_socket; |
7e6c131e | 1571 | struct tipc_msg *msg = buf_msg(*buf); |
e4de5fab | 1572 | int retval = -TIPC_ERR_NO_PORT; |
7e6c131e YX |
1573 | |
1574 | if (msg_mcast(msg)) | |
1575 | return retval; | |
1576 | ||
1577 | switch ((int)sock->state) { | |
1578 | case SS_CONNECTED: | |
1579 | /* Accept only connection-based messages sent by peer */ | |
2e84c60b | 1580 | if (tsk_peer_msg(tsk, msg)) { |
7e6c131e YX |
1581 | if (unlikely(msg_errcode(msg))) { |
1582 | sock->state = SS_DISCONNECTING; | |
301bae56 | 1583 | tsk->connected = 0; |
dadebc00 | 1584 | /* let timer expire on it's own */ |
301bae56 JPM |
1585 | tipc_node_remove_conn(tsk_peer_node(tsk), |
1586 | tsk->ref); | |
7e6c131e YX |
1587 | } |
1588 | retval = TIPC_OK; | |
1589 | } | |
1590 | break; | |
1591 | case SS_CONNECTING: | |
1592 | /* Accept only ACK or NACK message */ | |
dadebc00 JPM |
1593 | |
1594 | if (unlikely(!msg_connected(msg))) | |
1595 | break; | |
1596 | ||
584d24b3 YX |
1597 | if (unlikely(msg_errcode(msg))) { |
1598 | sock->state = SS_DISCONNECTING; | |
2c8d8518 | 1599 | sk->sk_err = ECONNREFUSED; |
584d24b3 YX |
1600 | retval = TIPC_OK; |
1601 | break; | |
1602 | } | |
1603 | ||
dadebc00 | 1604 | if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) { |
584d24b3 | 1605 | sock->state = SS_DISCONNECTING; |
dadebc00 | 1606 | sk->sk_err = EINVAL; |
7e6c131e | 1607 | retval = TIPC_OK; |
584d24b3 YX |
1608 | break; |
1609 | } | |
1610 | ||
301bae56 JPM |
1611 | tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg)); |
1612 | msg_set_importance(&tsk->phdr, msg_importance(msg)); | |
dadebc00 JPM |
1613 | sock->state = SS_CONNECTED; |
1614 | ||
584d24b3 YX |
1615 | /* If an incoming message is an 'ACK-', it should be |
1616 | * discarded here because it doesn't contain useful | |
1617 | * data. In addition, we should try to wake up | |
1618 | * connect() routine if sleeping. | |
1619 | */ | |
1620 | if (msg_data_sz(msg) == 0) { | |
1621 | kfree_skb(*buf); | |
1622 | *buf = NULL; | |
1623 | if (waitqueue_active(sk_sleep(sk))) | |
1624 | wake_up_interruptible(sk_sleep(sk)); | |
1625 | } | |
1626 | retval = TIPC_OK; | |
7e6c131e YX |
1627 | break; |
1628 | case SS_LISTENING: | |
1629 | case SS_UNCONNECTED: | |
1630 | /* Accept only SYN message */ | |
1631 | if (!msg_connected(msg) && !(msg_errcode(msg))) | |
1632 | retval = TIPC_OK; | |
1633 | break; | |
1634 | case SS_DISCONNECTING: | |
1635 | break; | |
1636 | default: | |
1637 | pr_err("Unknown socket state %u\n", sock->state); | |
1638 | } | |
1639 | return retval; | |
1640 | } | |
1641 | ||
aba79f33 YX |
1642 | /** |
1643 | * rcvbuf_limit - get proper overload limit of socket receive queue | |
1644 | * @sk: socket | |
1645 | * @buf: message | |
1646 | * | |
1647 | * For all connection oriented messages, irrespective of importance, | |
1648 | * the default overload value (i.e. 67MB) is set as limit. | |
1649 | * | |
1650 | * For all connectionless messages, by default new queue limits are | |
1651 | * as belows: | |
1652 | * | |
cc79dd1b YX |
1653 | * TIPC_LOW_IMPORTANCE (4 MB) |
1654 | * TIPC_MEDIUM_IMPORTANCE (8 MB) | |
1655 | * TIPC_HIGH_IMPORTANCE (16 MB) | |
1656 | * TIPC_CRITICAL_IMPORTANCE (32 MB) | |
aba79f33 YX |
1657 | * |
1658 | * Returns overload limit according to corresponding message importance | |
1659 | */ | |
1660 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf) | |
1661 | { | |
1662 | struct tipc_msg *msg = buf_msg(buf); | |
aba79f33 YX |
1663 | |
1664 | if (msg_connected(msg)) | |
0cee6bbe | 1665 | return sysctl_tipc_rmem[2]; |
1666 | ||
1667 | return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE << | |
1668 | msg_importance(msg); | |
aba79f33 YX |
1669 | } |
1670 | ||
c4307285 | 1671 | /** |
0c3141e9 AS |
1672 | * filter_rcv - validate incoming message |
1673 | * @sk: socket | |
b97bf3fd | 1674 | * @buf: message |
c4307285 | 1675 | * |
0c3141e9 AS |
1676 | * Enqueues message on receive queue if acceptable; optionally handles |
1677 | * disconnect indication for a connected socket. | |
1678 | * | |
1679 | * Called with socket lock already taken; port lock may also be taken. | |
c4307285 | 1680 | * |
e4de5fab | 1681 | * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message |
ac0074ee | 1682 | * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded |
b97bf3fd | 1683 | */ |
e4de5fab | 1684 | static int filter_rcv(struct sock *sk, struct sk_buff *buf) |
b97bf3fd | 1685 | { |
0c3141e9 | 1686 | struct socket *sock = sk->sk_socket; |
58ed9442 | 1687 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd | 1688 | struct tipc_msg *msg = buf_msg(buf); |
aba79f33 | 1689 | unsigned int limit = rcvbuf_limit(sk, buf); |
ac0074ee | 1690 | u32 onode; |
e4de5fab | 1691 | int rc = TIPC_OK; |
b97bf3fd | 1692 | |
ac0074ee JPM |
1693 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
1694 | return tipc_sk_proto_rcv(tsk, &onode, buf); | |
ec8a2e56 | 1695 | |
50100a5e JPM |
1696 | if (unlikely(msg_user(msg) == SOCK_WAKEUP)) { |
1697 | kfree_skb(buf); | |
1698 | tsk->link_cong = 0; | |
1699 | sk->sk_write_space(sk); | |
1700 | return TIPC_OK; | |
1701 | } | |
1702 | ||
b97bf3fd | 1703 | /* Reject message if it is wrong sort of message for socket */ |
aad58547 | 1704 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
e4de5fab | 1705 | return -TIPC_ERR_NO_PORT; |
0c3141e9 | 1706 | |
b97bf3fd | 1707 | if (sock->state == SS_READY) { |
b29f1428 | 1708 | if (msg_connected(msg)) |
e4de5fab | 1709 | return -TIPC_ERR_NO_PORT; |
b97bf3fd | 1710 | } else { |
e4de5fab JPM |
1711 | rc = filter_connect(tsk, &buf); |
1712 | if (rc != TIPC_OK || buf == NULL) | |
1713 | return rc; | |
b97bf3fd PL |
1714 | } |
1715 | ||
1716 | /* Reject message if there isn't room to queue it */ | |
aba79f33 | 1717 | if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) |
e4de5fab | 1718 | return -TIPC_ERR_OVERLOAD; |
b97bf3fd | 1719 | |
aba79f33 | 1720 | /* Enqueue message */ |
40682432 | 1721 | TIPC_SKB_CB(buf)->handle = NULL; |
0c3141e9 | 1722 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
aba79f33 | 1723 | skb_set_owner_r(buf, sk); |
0c3141e9 | 1724 | |
676d2369 | 1725 | sk->sk_data_ready(sk); |
0c3141e9 AS |
1726 | return TIPC_OK; |
1727 | } | |
b97bf3fd | 1728 | |
0c3141e9 | 1729 | /** |
4f4482dc | 1730 | * tipc_backlog_rcv - handle incoming message from backlog queue |
0c3141e9 AS |
1731 | * @sk: socket |
1732 | * @buf: message | |
1733 | * | |
1734 | * Caller must hold socket lock, but not port lock. | |
1735 | * | |
1736 | * Returns 0 | |
1737 | */ | |
4f4482dc | 1738 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) |
0c3141e9 | 1739 | { |
e4de5fab | 1740 | int rc; |
8db1bae3 | 1741 | u32 onode; |
4f4482dc | 1742 | struct tipc_sock *tsk = tipc_sk(sk); |
02c00c2a | 1743 | uint truesize = buf->truesize; |
0c3141e9 | 1744 | |
e4de5fab | 1745 | rc = filter_rcv(sk, buf); |
4f4482dc | 1746 | |
ac0074ee JPM |
1747 | if (likely(!rc)) { |
1748 | if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) | |
1749 | atomic_add(truesize, &tsk->dupl_rcvcnt); | |
1750 | return 0; | |
1751 | } | |
1752 | ||
1753 | if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc)) | |
1754 | return 0; | |
1755 | ||
9fbfb8b1 | 1756 | tipc_link_xmit(buf, onode, 0); |
4f4482dc | 1757 | |
0c3141e9 AS |
1758 | return 0; |
1759 | } | |
1760 | ||
1761 | /** | |
24be34b5 | 1762 | * tipc_sk_rcv - handle incoming message |
9816f061 JPM |
1763 | * @buf: buffer containing arriving message |
1764 | * Consumes buffer | |
1765 | * Returns 0 if success, or errno: -EHOSTUNREACH | |
0c3141e9 | 1766 | */ |
9816f061 | 1767 | int tipc_sk_rcv(struct sk_buff *buf) |
0c3141e9 | 1768 | { |
9816f061 | 1769 | struct tipc_sock *tsk; |
9816f061 JPM |
1770 | struct sock *sk; |
1771 | u32 dport = msg_destport(buf_msg(buf)); | |
e4de5fab | 1772 | int rc = TIPC_OK; |
4f4482dc | 1773 | uint limit; |
8db1bae3 | 1774 | u32 dnode; |
9816f061 | 1775 | |
5a379074 | 1776 | /* Validate destination and message */ |
6c9808ce | 1777 | tsk = tipc_sk_get(dport); |
9b50fd08 | 1778 | if (unlikely(!tsk)) { |
5a379074 | 1779 | rc = tipc_msg_eval(buf, &dnode); |
9816f061 JPM |
1780 | goto exit; |
1781 | } | |
9816f061 JPM |
1782 | sk = &tsk->sk; |
1783 | ||
1784 | /* Queue message */ | |
1a194c2d | 1785 | spin_lock_bh(&sk->sk_lock.slock); |
9816f061 | 1786 | |
0c3141e9 | 1787 | if (!sock_owned_by_user(sk)) { |
e4de5fab | 1788 | rc = filter_rcv(sk, buf); |
0c3141e9 | 1789 | } else { |
4f4482dc JPM |
1790 | if (sk->sk_backlog.len == 0) |
1791 | atomic_set(&tsk->dupl_rcvcnt, 0); | |
1792 | limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt); | |
1793 | if (sk_add_backlog(sk, buf, limit)) | |
e4de5fab | 1794 | rc = -TIPC_ERR_OVERLOAD; |
0c3141e9 | 1795 | } |
1a194c2d | 1796 | spin_unlock_bh(&sk->sk_lock.slock); |
6c9808ce | 1797 | tipc_sk_put(tsk); |
e4de5fab | 1798 | if (likely(!rc)) |
9816f061 JPM |
1799 | return 0; |
1800 | exit: | |
5a379074 | 1801 | if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc)) |
8db1bae3 | 1802 | return -EHOSTUNREACH; |
5a379074 | 1803 | |
9fbfb8b1 | 1804 | tipc_link_xmit(buf, dnode, 0); |
5a379074 | 1805 | return (rc < 0) ? -EHOSTUNREACH : 0; |
b97bf3fd PL |
1806 | } |
1807 | ||
78eb3a53 YX |
1808 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
1809 | { | |
1810 | struct sock *sk = sock->sk; | |
1811 | DEFINE_WAIT(wait); | |
1812 | int done; | |
1813 | ||
1814 | do { | |
1815 | int err = sock_error(sk); | |
1816 | if (err) | |
1817 | return err; | |
1818 | if (!*timeo_p) | |
1819 | return -ETIMEDOUT; | |
1820 | if (signal_pending(current)) | |
1821 | return sock_intr_errno(*timeo_p); | |
1822 | ||
1823 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | |
1824 | done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); | |
1825 | finish_wait(sk_sleep(sk), &wait); | |
1826 | } while (!done); | |
1827 | return 0; | |
1828 | } | |
1829 | ||
b97bf3fd | 1830 | /** |
247f0f3c | 1831 | * tipc_connect - establish a connection to another TIPC port |
b97bf3fd PL |
1832 | * @sock: socket structure |
1833 | * @dest: socket address for destination port | |
1834 | * @destlen: size of socket address data structure | |
0c3141e9 | 1835 | * @flags: file-related flags associated with socket |
b97bf3fd PL |
1836 | * |
1837 | * Returns 0 on success, errno otherwise | |
1838 | */ | |
247f0f3c YX |
1839 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
1840 | int destlen, int flags) | |
b97bf3fd | 1841 | { |
0c3141e9 | 1842 | struct sock *sk = sock->sk; |
b89741a0 AS |
1843 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
1844 | struct msghdr m = {NULL,}; | |
78eb3a53 YX |
1845 | long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; |
1846 | socket_state previous; | |
b89741a0 AS |
1847 | int res; |
1848 | ||
0c3141e9 AS |
1849 | lock_sock(sk); |
1850 | ||
b89741a0 | 1851 | /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ |
0c3141e9 AS |
1852 | if (sock->state == SS_READY) { |
1853 | res = -EOPNOTSUPP; | |
1854 | goto exit; | |
1855 | } | |
b89741a0 | 1856 | |
b89741a0 AS |
1857 | /* |
1858 | * Reject connection attempt using multicast address | |
1859 | * | |
1860 | * Note: send_msg() validates the rest of the address fields, | |
1861 | * so there's no need to do it here | |
1862 | */ | |
0c3141e9 AS |
1863 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
1864 | res = -EINVAL; | |
1865 | goto exit; | |
1866 | } | |
1867 | ||
78eb3a53 | 1868 | previous = sock->state; |
584d24b3 YX |
1869 | switch (sock->state) { |
1870 | case SS_UNCONNECTED: | |
1871 | /* Send a 'SYN-' to destination */ | |
1872 | m.msg_name = dest; | |
1873 | m.msg_namelen = destlen; | |
1874 | ||
1875 | /* If connect is in non-blocking case, set MSG_DONTWAIT to | |
1876 | * indicate send_msg() is never blocked. | |
1877 | */ | |
1878 | if (!timeout) | |
1879 | m.msg_flags = MSG_DONTWAIT; | |
1880 | ||
247f0f3c | 1881 | res = tipc_sendmsg(NULL, sock, &m, 0); |
584d24b3 YX |
1882 | if ((res < 0) && (res != -EWOULDBLOCK)) |
1883 | goto exit; | |
1884 | ||
1885 | /* Just entered SS_CONNECTING state; the only | |
1886 | * difference is that return value in non-blocking | |
1887 | * case is EINPROGRESS, rather than EALREADY. | |
1888 | */ | |
1889 | res = -EINPROGRESS; | |
584d24b3 | 1890 | case SS_CONNECTING: |
78eb3a53 YX |
1891 | if (previous == SS_CONNECTING) |
1892 | res = -EALREADY; | |
1893 | if (!timeout) | |
1894 | goto exit; | |
1895 | timeout = msecs_to_jiffies(timeout); | |
1896 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ | |
1897 | res = tipc_wait_for_connect(sock, &timeout); | |
584d24b3 YX |
1898 | break; |
1899 | case SS_CONNECTED: | |
1900 | res = -EISCONN; | |
1901 | break; | |
1902 | default: | |
1903 | res = -EINVAL; | |
78eb3a53 | 1904 | break; |
b89741a0 | 1905 | } |
0c3141e9 AS |
1906 | exit: |
1907 | release_sock(sk); | |
b89741a0 | 1908 | return res; |
b97bf3fd PL |
1909 | } |
1910 | ||
c4307285 | 1911 | /** |
247f0f3c | 1912 | * tipc_listen - allow socket to listen for incoming connections |
b97bf3fd PL |
1913 | * @sock: socket structure |
1914 | * @len: (unused) | |
c4307285 | 1915 | * |
b97bf3fd PL |
1916 | * Returns 0 on success, errno otherwise |
1917 | */ | |
247f0f3c | 1918 | static int tipc_listen(struct socket *sock, int len) |
b97bf3fd | 1919 | { |
0c3141e9 AS |
1920 | struct sock *sk = sock->sk; |
1921 | int res; | |
1922 | ||
1923 | lock_sock(sk); | |
b97bf3fd | 1924 | |
245f3d34 | 1925 | if (sock->state != SS_UNCONNECTED) |
0c3141e9 AS |
1926 | res = -EINVAL; |
1927 | else { | |
1928 | sock->state = SS_LISTENING; | |
1929 | res = 0; | |
1930 | } | |
1931 | ||
1932 | release_sock(sk); | |
1933 | return res; | |
b97bf3fd PL |
1934 | } |
1935 | ||
6398e23c YX |
1936 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
1937 | { | |
1938 | struct sock *sk = sock->sk; | |
1939 | DEFINE_WAIT(wait); | |
1940 | int err; | |
1941 | ||
1942 | /* True wake-one mechanism for incoming connections: only | |
1943 | * one process gets woken up, not the 'whole herd'. | |
1944 | * Since we do not 'race & poll' for established sockets | |
1945 | * anymore, the common case will execute the loop only once. | |
1946 | */ | |
1947 | for (;;) { | |
1948 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, | |
1949 | TASK_INTERRUPTIBLE); | |
fe8e4649 | 1950 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
6398e23c YX |
1951 | release_sock(sk); |
1952 | timeo = schedule_timeout(timeo); | |
1953 | lock_sock(sk); | |
1954 | } | |
1955 | err = 0; | |
1956 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
1957 | break; | |
1958 | err = -EINVAL; | |
1959 | if (sock->state != SS_LISTENING) | |
1960 | break; | |
1961 | err = sock_intr_errno(timeo); | |
1962 | if (signal_pending(current)) | |
1963 | break; | |
1964 | err = -EAGAIN; | |
1965 | if (!timeo) | |
1966 | break; | |
1967 | } | |
1968 | finish_wait(sk_sleep(sk), &wait); | |
1969 | return err; | |
1970 | } | |
1971 | ||
c4307285 | 1972 | /** |
247f0f3c | 1973 | * tipc_accept - wait for connection request |
b97bf3fd PL |
1974 | * @sock: listening socket |
1975 | * @newsock: new socket that is to be connected | |
1976 | * @flags: file-related flags associated with socket | |
c4307285 | 1977 | * |
b97bf3fd PL |
1978 | * Returns 0 on success, errno otherwise |
1979 | */ | |
247f0f3c | 1980 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) |
b97bf3fd | 1981 | { |
0fef8f20 | 1982 | struct sock *new_sk, *sk = sock->sk; |
b97bf3fd | 1983 | struct sk_buff *buf; |
301bae56 | 1984 | struct tipc_sock *new_tsock; |
0fef8f20 | 1985 | struct tipc_msg *msg; |
6398e23c | 1986 | long timeo; |
0c3141e9 | 1987 | int res; |
b97bf3fd | 1988 | |
0c3141e9 | 1989 | lock_sock(sk); |
b97bf3fd | 1990 | |
0c3141e9 AS |
1991 | if (sock->state != SS_LISTENING) { |
1992 | res = -EINVAL; | |
b97bf3fd PL |
1993 | goto exit; |
1994 | } | |
6398e23c YX |
1995 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
1996 | res = tipc_wait_for_accept(sock, timeo); | |
1997 | if (res) | |
1998 | goto exit; | |
0c3141e9 AS |
1999 | |
2000 | buf = skb_peek(&sk->sk_receive_queue); | |
2001 | ||
c5fa7b3c | 2002 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); |
0fef8f20 PG |
2003 | if (res) |
2004 | goto exit; | |
b97bf3fd | 2005 | |
0fef8f20 | 2006 | new_sk = new_sock->sk; |
301bae56 | 2007 | new_tsock = tipc_sk(new_sk); |
0fef8f20 | 2008 | msg = buf_msg(buf); |
b97bf3fd | 2009 | |
0fef8f20 PG |
2010 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
2011 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); | |
2012 | ||
2013 | /* | |
2014 | * Reject any stray messages received by new socket | |
2015 | * before the socket lock was taken (very, very unlikely) | |
2016 | */ | |
2e84c60b | 2017 | tsk_rej_rx_queue(new_sk); |
0fef8f20 PG |
2018 | |
2019 | /* Connect new socket to it's peer */ | |
301bae56 | 2020 | tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); |
0fef8f20 PG |
2021 | new_sock->state = SS_CONNECTED; |
2022 | ||
301bae56 | 2023 | tsk_set_importance(new_tsock, msg_importance(msg)); |
0fef8f20 | 2024 | if (msg_named(msg)) { |
301bae56 JPM |
2025 | new_tsock->conn_type = msg_nametype(msg); |
2026 | new_tsock->conn_instance = msg_nameinst(msg); | |
b97bf3fd | 2027 | } |
0fef8f20 PG |
2028 | |
2029 | /* | |
2030 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. | |
2031 | * Respond to 'SYN+' by queuing it on new socket. | |
2032 | */ | |
2033 | if (!msg_data_sz(msg)) { | |
2034 | struct msghdr m = {NULL,}; | |
2035 | ||
2e84c60b | 2036 | tsk_advance_rx_queue(sk); |
247f0f3c | 2037 | tipc_send_packet(NULL, new_sock, &m, 0); |
0fef8f20 PG |
2038 | } else { |
2039 | __skb_dequeue(&sk->sk_receive_queue); | |
2040 | __skb_queue_head(&new_sk->sk_receive_queue, buf); | |
aba79f33 | 2041 | skb_set_owner_r(buf, new_sk); |
0fef8f20 PG |
2042 | } |
2043 | release_sock(new_sk); | |
b97bf3fd | 2044 | exit: |
0c3141e9 | 2045 | release_sock(sk); |
b97bf3fd PL |
2046 | return res; |
2047 | } | |
2048 | ||
2049 | /** | |
247f0f3c | 2050 | * tipc_shutdown - shutdown socket connection |
b97bf3fd | 2051 | * @sock: socket structure |
e247a8f5 | 2052 | * @how: direction to close (must be SHUT_RDWR) |
b97bf3fd PL |
2053 | * |
2054 | * Terminates connection (if necessary), then purges socket's receive queue. | |
c4307285 | 2055 | * |
b97bf3fd PL |
2056 | * Returns 0 on success, errno otherwise |
2057 | */ | |
247f0f3c | 2058 | static int tipc_shutdown(struct socket *sock, int how) |
b97bf3fd | 2059 | { |
0c3141e9 | 2060 | struct sock *sk = sock->sk; |
58ed9442 | 2061 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd | 2062 | struct sk_buff *buf; |
80e44c22 | 2063 | u32 dnode; |
b97bf3fd PL |
2064 | int res; |
2065 | ||
e247a8f5 AS |
2066 | if (how != SHUT_RDWR) |
2067 | return -EINVAL; | |
b97bf3fd | 2068 | |
0c3141e9 | 2069 | lock_sock(sk); |
b97bf3fd PL |
2070 | |
2071 | switch (sock->state) { | |
0c3141e9 | 2072 | case SS_CONNECTING: |
b97bf3fd PL |
2073 | case SS_CONNECTED: |
2074 | ||
b97bf3fd | 2075 | restart: |
617d3c7a | 2076 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
0c3141e9 AS |
2077 | buf = __skb_dequeue(&sk->sk_receive_queue); |
2078 | if (buf) { | |
40682432 | 2079 | if (TIPC_SKB_CB(buf)->handle != NULL) { |
5f6d9123 | 2080 | kfree_skb(buf); |
b97bf3fd PL |
2081 | goto restart; |
2082 | } | |
80e44c22 | 2083 | if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN)) |
301bae56 JPM |
2084 | tipc_link_xmit(buf, dnode, tsk->ref); |
2085 | tipc_node_remove_conn(dnode, tsk->ref); | |
0c3141e9 | 2086 | } else { |
301bae56 | 2087 | dnode = tsk_peer_node(tsk); |
80e44c22 JPM |
2088 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
2089 | TIPC_CONN_MSG, SHORT_H_SIZE, | |
2090 | 0, dnode, tipc_own_addr, | |
301bae56 JPM |
2091 | tsk_peer_port(tsk), |
2092 | tsk->ref, TIPC_CONN_SHUTDOWN); | |
2093 | tipc_link_xmit(buf, dnode, tsk->ref); | |
b97bf3fd | 2094 | } |
301bae56 | 2095 | tsk->connected = 0; |
0c3141e9 | 2096 | sock->state = SS_DISCONNECTING; |
301bae56 | 2097 | tipc_node_remove_conn(dnode, tsk->ref); |
b97bf3fd PL |
2098 | /* fall through */ |
2099 | ||
2100 | case SS_DISCONNECTING: | |
2101 | ||
75031151 | 2102 | /* Discard any unreceived messages */ |
57467e56 | 2103 | __skb_queue_purge(&sk->sk_receive_queue); |
75031151 YX |
2104 | |
2105 | /* Wake up anyone sleeping in poll */ | |
2106 | sk->sk_state_change(sk); | |
b97bf3fd PL |
2107 | res = 0; |
2108 | break; | |
2109 | ||
2110 | default: | |
2111 | res = -ENOTCONN; | |
2112 | } | |
2113 | ||
0c3141e9 | 2114 | release_sock(sk); |
b97bf3fd PL |
2115 | return res; |
2116 | } | |
2117 | ||
57289015 JPM |
2118 | static void tipc_sk_timeout(unsigned long ref) |
2119 | { | |
6c9808ce | 2120 | struct tipc_sock *tsk; |
57289015 JPM |
2121 | struct sock *sk; |
2122 | struct sk_buff *buf = NULL; | |
57289015 JPM |
2123 | u32 peer_port, peer_node; |
2124 | ||
6c9808ce | 2125 | tsk = tipc_sk_get(ref); |
9b50fd08 | 2126 | if (!tsk) |
cc086fcf | 2127 | return; |
6c9808ce | 2128 | |
cc086fcf | 2129 | sk = &tsk->sk; |
6c9808ce | 2130 | bh_lock_sock(sk); |
301bae56 | 2131 | if (!tsk->connected) { |
6c9808ce JPM |
2132 | bh_unlock_sock(sk); |
2133 | goto exit; | |
57289015 | 2134 | } |
301bae56 JPM |
2135 | peer_port = tsk_peer_port(tsk); |
2136 | peer_node = tsk_peer_node(tsk); | |
57289015 | 2137 | |
301bae56 | 2138 | if (tsk->probing_state == TIPC_CONN_PROBING) { |
57289015 JPM |
2139 | /* Previous probe not answered -> self abort */ |
2140 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, | |
2141 | SHORT_H_SIZE, 0, tipc_own_addr, | |
2142 | peer_node, ref, peer_port, | |
2143 | TIPC_ERR_NO_PORT); | |
2144 | } else { | |
2145 | buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, | |
2146 | 0, peer_node, tipc_own_addr, | |
2147 | peer_port, ref, TIPC_OK); | |
301bae56 JPM |
2148 | tsk->probing_state = TIPC_CONN_PROBING; |
2149 | k_start_timer(&tsk->timer, tsk->probing_interval); | |
57289015 JPM |
2150 | } |
2151 | bh_unlock_sock(sk); | |
6c9808ce JPM |
2152 | if (buf) |
2153 | tipc_link_xmit(buf, peer_node, ref); | |
2154 | exit: | |
2155 | tipc_sk_put(tsk); | |
57289015 JPM |
2156 | } |
2157 | ||
301bae56 | 2158 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, |
0fc87aae JPM |
2159 | struct tipc_name_seq const *seq) |
2160 | { | |
2161 | struct publication *publ; | |
2162 | u32 key; | |
2163 | ||
301bae56 | 2164 | if (tsk->connected) |
0fc87aae | 2165 | return -EINVAL; |
301bae56 JPM |
2166 | key = tsk->ref + tsk->pub_count + 1; |
2167 | if (key == tsk->ref) | |
0fc87aae JPM |
2168 | return -EADDRINUSE; |
2169 | ||
2170 | publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper, | |
301bae56 | 2171 | scope, tsk->ref, key); |
0fc87aae JPM |
2172 | if (unlikely(!publ)) |
2173 | return -EINVAL; | |
2174 | ||
301bae56 JPM |
2175 | list_add(&publ->pport_list, &tsk->publications); |
2176 | tsk->pub_count++; | |
2177 | tsk->published = 1; | |
0fc87aae JPM |
2178 | return 0; |
2179 | } | |
2180 | ||
301bae56 | 2181 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, |
0fc87aae JPM |
2182 | struct tipc_name_seq const *seq) |
2183 | { | |
2184 | struct publication *publ; | |
2185 | struct publication *safe; | |
2186 | int rc = -EINVAL; | |
2187 | ||
301bae56 | 2188 | list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) { |
0fc87aae JPM |
2189 | if (seq) { |
2190 | if (publ->scope != scope) | |
2191 | continue; | |
2192 | if (publ->type != seq->type) | |
2193 | continue; | |
2194 | if (publ->lower != seq->lower) | |
2195 | continue; | |
2196 | if (publ->upper != seq->upper) | |
2197 | break; | |
2198 | tipc_nametbl_withdraw(publ->type, publ->lower, | |
2199 | publ->ref, publ->key); | |
2200 | rc = 0; | |
2201 | break; | |
2202 | } | |
2203 | tipc_nametbl_withdraw(publ->type, publ->lower, | |
2204 | publ->ref, publ->key); | |
2205 | rc = 0; | |
2206 | } | |
301bae56 JPM |
2207 | if (list_empty(&tsk->publications)) |
2208 | tsk->published = 0; | |
0fc87aae JPM |
2209 | return rc; |
2210 | } | |
2211 | ||
301bae56 | 2212 | static int tipc_sk_show(struct tipc_sock *tsk, char *buf, |
5a9ee0be JPM |
2213 | int len, int full_id) |
2214 | { | |
2215 | struct publication *publ; | |
2216 | int ret; | |
2217 | ||
2218 | if (full_id) | |
2219 | ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:", | |
2220 | tipc_zone(tipc_own_addr), | |
2221 | tipc_cluster(tipc_own_addr), | |
301bae56 | 2222 | tipc_node(tipc_own_addr), tsk->ref); |
5a9ee0be | 2223 | else |
301bae56 | 2224 | ret = tipc_snprintf(buf, len, "%-10u:", tsk->ref); |
5a9ee0be | 2225 | |
301bae56 JPM |
2226 | if (tsk->connected) { |
2227 | u32 dport = tsk_peer_port(tsk); | |
2228 | u32 destnode = tsk_peer_node(tsk); | |
5a9ee0be JPM |
2229 | |
2230 | ret += tipc_snprintf(buf + ret, len - ret, | |
2231 | " connected to <%u.%u.%u:%u>", | |
2232 | tipc_zone(destnode), | |
2233 | tipc_cluster(destnode), | |
2234 | tipc_node(destnode), dport); | |
301bae56 | 2235 | if (tsk->conn_type != 0) |
5a9ee0be | 2236 | ret += tipc_snprintf(buf + ret, len - ret, |
301bae56 JPM |
2237 | " via {%u,%u}", tsk->conn_type, |
2238 | tsk->conn_instance); | |
2239 | } else if (tsk->published) { | |
5a9ee0be | 2240 | ret += tipc_snprintf(buf + ret, len - ret, " bound to"); |
301bae56 | 2241 | list_for_each_entry(publ, &tsk->publications, pport_list) { |
5a9ee0be JPM |
2242 | if (publ->lower == publ->upper) |
2243 | ret += tipc_snprintf(buf + ret, len - ret, | |
2244 | " {%u,%u}", publ->type, | |
2245 | publ->lower); | |
2246 | else | |
2247 | ret += tipc_snprintf(buf + ret, len - ret, | |
2248 | " {%u,%u,%u}", publ->type, | |
2249 | publ->lower, publ->upper); | |
2250 | } | |
2251 | } | |
2252 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); | |
2253 | return ret; | |
2254 | } | |
2255 | ||
2256 | struct sk_buff *tipc_sk_socks_show(void) | |
2257 | { | |
2258 | struct sk_buff *buf; | |
2259 | struct tlv_desc *rep_tlv; | |
2260 | char *pb; | |
2261 | int pb_len; | |
2262 | struct tipc_sock *tsk; | |
2263 | int str_len = 0; | |
2264 | u32 ref = 0; | |
2265 | ||
2266 | buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); | |
2267 | if (!buf) | |
2268 | return NULL; | |
2269 | rep_tlv = (struct tlv_desc *)buf->data; | |
2270 | pb = TLV_DATA(rep_tlv); | |
2271 | pb_len = ULTRA_STRING_MAX_LEN; | |
2272 | ||
6c9808ce JPM |
2273 | tsk = tipc_sk_get_next(&ref); |
2274 | for (; tsk; tsk = tipc_sk_get_next(&ref)) { | |
2275 | lock_sock(&tsk->sk); | |
301bae56 | 2276 | str_len += tipc_sk_show(tsk, pb + str_len, |
5a9ee0be | 2277 | pb_len - str_len, 0); |
6c9808ce JPM |
2278 | release_sock(&tsk->sk); |
2279 | tipc_sk_put(tsk); | |
5a9ee0be JPM |
2280 | } |
2281 | str_len += 1; /* for "\0" */ | |
2282 | skb_put(buf, TLV_SPACE(str_len)); | |
2283 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
2284 | ||
2285 | return buf; | |
2286 | } | |
2287 | ||
2288 | /* tipc_sk_reinit: set non-zero address in all existing sockets | |
2289 | * when we go from standalone to network mode. | |
2290 | */ | |
2291 | void tipc_sk_reinit(void) | |
2292 | { | |
2293 | struct tipc_msg *msg; | |
2294 | u32 ref = 0; | |
6c9808ce | 2295 | struct tipc_sock *tsk = tipc_sk_get_next(&ref); |
5a9ee0be | 2296 | |
6c9808ce JPM |
2297 | for (; tsk; tsk = tipc_sk_get_next(&ref)) { |
2298 | lock_sock(&tsk->sk); | |
301bae56 | 2299 | msg = &tsk->phdr; |
5a9ee0be JPM |
2300 | msg_set_prevnode(msg, tipc_own_addr); |
2301 | msg_set_orignode(msg, tipc_own_addr); | |
6c9808ce JPM |
2302 | release_sock(&tsk->sk); |
2303 | tipc_sk_put(tsk); | |
5a9ee0be JPM |
2304 | } |
2305 | } | |
2306 | ||
808d90f9 JPM |
2307 | /** |
2308 | * struct reference - TIPC socket reference entry | |
2309 | * @tsk: pointer to socket associated with reference entry | |
2310 | * @ref: reference value for socket (combines instance & array index info) | |
2311 | */ | |
2312 | struct reference { | |
2313 | struct tipc_sock *tsk; | |
2314 | u32 ref; | |
2315 | }; | |
2316 | ||
2317 | /** | |
2318 | * struct tipc_ref_table - table of TIPC socket reference entries | |
2319 | * @entries: pointer to array of reference entries | |
2320 | * @capacity: array index of first unusable entry | |
2321 | * @init_point: array index of first uninitialized entry | |
2322 | * @first_free: array index of first unused socket reference entry | |
2323 | * @last_free: array index of last unused socket reference entry | |
2324 | * @index_mask: bitmask for array index portion of reference values | |
2325 | * @start_mask: initial value for instance value portion of reference values | |
2326 | */ | |
2327 | struct ref_table { | |
2328 | struct reference *entries; | |
2329 | u32 capacity; | |
2330 | u32 init_point; | |
2331 | u32 first_free; | |
2332 | u32 last_free; | |
2333 | u32 index_mask; | |
2334 | u32 start_mask; | |
2335 | }; | |
2336 | ||
2337 | /* Socket reference table consists of 2**N entries. | |
2338 | * | |
2339 | * State Socket ptr Reference | |
2340 | * ----- ---------- --------- | |
2341 | * In use non-NULL XXXX|own index | |
2342 | * (XXXX changes each time entry is acquired) | |
2343 | * Free NULL YYYY|next free index | |
2344 | * (YYYY is one more than last used XXXX) | |
2345 | * Uninitialized NULL 0 | |
2346 | * | |
2347 | * Entry 0 is not used; this allows index 0 to denote the end of the free list. | |
2348 | * | |
2349 | * Note that a reference value of 0 does not necessarily indicate that an | |
2350 | * entry is uninitialized, since the last entry in the free list could also | |
2351 | * have a reference value of 0 (although this is unlikely). | |
2352 | */ | |
2353 | ||
2354 | static struct ref_table tipc_ref_table; | |
2355 | ||
2356 | static DEFINE_RWLOCK(ref_table_lock); | |
2357 | ||
2358 | /** | |
2359 | * tipc_ref_table_init - create reference table for sockets | |
2360 | */ | |
2361 | int tipc_sk_ref_table_init(u32 req_sz, u32 start) | |
2362 | { | |
2363 | struct reference *table; | |
2364 | u32 actual_sz; | |
2365 | ||
2366 | /* account for unused entry, then round up size to a power of 2 */ | |
2367 | ||
2368 | req_sz++; | |
2369 | for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) { | |
2370 | /* do nothing */ | |
2371 | }; | |
2372 | ||
2373 | /* allocate table & mark all entries as uninitialized */ | |
2374 | table = vzalloc(actual_sz * sizeof(struct reference)); | |
2375 | if (table == NULL) | |
2376 | return -ENOMEM; | |
2377 | ||
2378 | tipc_ref_table.entries = table; | |
2379 | tipc_ref_table.capacity = req_sz; | |
2380 | tipc_ref_table.init_point = 1; | |
2381 | tipc_ref_table.first_free = 0; | |
2382 | tipc_ref_table.last_free = 0; | |
2383 | tipc_ref_table.index_mask = actual_sz - 1; | |
2384 | tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask; | |
2385 | ||
2386 | return 0; | |
2387 | } | |
2388 | ||
2389 | /** | |
2390 | * tipc_ref_table_stop - destroy reference table for sockets | |
2391 | */ | |
2392 | void tipc_sk_ref_table_stop(void) | |
2393 | { | |
2394 | if (!tipc_ref_table.entries) | |
2395 | return; | |
2396 | vfree(tipc_ref_table.entries); | |
2397 | tipc_ref_table.entries = NULL; | |
2398 | } | |
2399 | ||
2400 | /* tipc_ref_acquire - create reference to a socket | |
2401 | * | |
2402 | * Register an socket pointer in the reference table. | |
2403 | * Returns a unique reference value that is used from then on to retrieve the | |
2404 | * socket pointer, or to determine if the socket has been deregistered. | |
2405 | */ | |
2406 | u32 tipc_sk_ref_acquire(struct tipc_sock *tsk) | |
2407 | { | |
2408 | u32 index; | |
2409 | u32 index_mask; | |
2410 | u32 next_plus_upper; | |
2411 | u32 ref = 0; | |
2412 | struct reference *entry; | |
2413 | ||
2414 | if (unlikely(!tsk)) { | |
2415 | pr_err("Attempt to acquire ref. to non-existent obj\n"); | |
2416 | return 0; | |
2417 | } | |
2418 | if (unlikely(!tipc_ref_table.entries)) { | |
2419 | pr_err("Ref. table not found in acquisition attempt\n"); | |
2420 | return 0; | |
2421 | } | |
2422 | ||
2423 | /* Take a free entry, if available; otherwise initialize a new one */ | |
2424 | write_lock_bh(&ref_table_lock); | |
2425 | index = tipc_ref_table.first_free; | |
2426 | entry = &tipc_ref_table.entries[index]; | |
2427 | ||
2428 | if (likely(index)) { | |
2429 | index = tipc_ref_table.first_free; | |
2430 | entry = &tipc_ref_table.entries[index]; | |
2431 | index_mask = tipc_ref_table.index_mask; | |
2432 | next_plus_upper = entry->ref; | |
2433 | tipc_ref_table.first_free = next_plus_upper & index_mask; | |
2434 | ref = (next_plus_upper & ~index_mask) + index; | |
2435 | entry->tsk = tsk; | |
2436 | } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) { | |
2437 | index = tipc_ref_table.init_point++; | |
2438 | entry = &tipc_ref_table.entries[index]; | |
2439 | ref = tipc_ref_table.start_mask + index; | |
2440 | } | |
2441 | ||
2442 | if (ref) { | |
2443 | entry->ref = ref; | |
2444 | entry->tsk = tsk; | |
2445 | } | |
2446 | write_unlock_bh(&ref_table_lock); | |
2447 | return ref; | |
2448 | } | |
2449 | ||
2450 | /* tipc_sk_ref_discard - invalidate reference to an socket | |
2451 | * | |
2452 | * Disallow future references to an socket and free up the entry for re-use. | |
2453 | */ | |
2454 | void tipc_sk_ref_discard(u32 ref) | |
2455 | { | |
2456 | struct reference *entry; | |
2457 | u32 index; | |
2458 | u32 index_mask; | |
2459 | ||
2460 | if (unlikely(!tipc_ref_table.entries)) { | |
2461 | pr_err("Ref. table not found during discard attempt\n"); | |
2462 | return; | |
2463 | } | |
2464 | ||
2465 | index_mask = tipc_ref_table.index_mask; | |
2466 | index = ref & index_mask; | |
2467 | entry = &tipc_ref_table.entries[index]; | |
2468 | ||
2469 | write_lock_bh(&ref_table_lock); | |
2470 | ||
2471 | if (unlikely(!entry->tsk)) { | |
2472 | pr_err("Attempt to discard ref. to non-existent socket\n"); | |
2473 | goto exit; | |
2474 | } | |
2475 | if (unlikely(entry->ref != ref)) { | |
2476 | pr_err("Attempt to discard non-existent reference\n"); | |
2477 | goto exit; | |
2478 | } | |
2479 | ||
2480 | /* Mark entry as unused; increment instance part of entry's | |
2481 | * reference to invalidate any subsequent references | |
2482 | */ | |
2483 | ||
2484 | entry->tsk = NULL; | |
2485 | entry->ref = (ref & ~index_mask) + (index_mask + 1); | |
2486 | ||
2487 | /* Append entry to free entry list */ | |
2488 | if (unlikely(tipc_ref_table.first_free == 0)) | |
2489 | tipc_ref_table.first_free = index; | |
2490 | else | |
2491 | tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index; | |
2492 | tipc_ref_table.last_free = index; | |
2493 | exit: | |
2494 | write_unlock_bh(&ref_table_lock); | |
2495 | } | |
2496 | ||
2497 | /* tipc_sk_get - find referenced socket and return pointer to it | |
2498 | */ | |
2499 | struct tipc_sock *tipc_sk_get(u32 ref) | |
2500 | { | |
2501 | struct reference *entry; | |
2502 | struct tipc_sock *tsk; | |
2503 | ||
2504 | if (unlikely(!tipc_ref_table.entries)) | |
2505 | return NULL; | |
2506 | read_lock_bh(&ref_table_lock); | |
2507 | entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask]; | |
2508 | tsk = entry->tsk; | |
2509 | if (likely(tsk && (entry->ref == ref))) | |
2510 | sock_hold(&tsk->sk); | |
2511 | else | |
2512 | tsk = NULL; | |
2513 | read_unlock_bh(&ref_table_lock); | |
2514 | return tsk; | |
2515 | } | |
2516 | ||
2517 | /* tipc_sk_get_next - lock & return next socket after referenced one | |
2518 | */ | |
2519 | struct tipc_sock *tipc_sk_get_next(u32 *ref) | |
2520 | { | |
2521 | struct reference *entry; | |
2522 | struct tipc_sock *tsk = NULL; | |
2523 | uint index = *ref & tipc_ref_table.index_mask; | |
2524 | ||
2525 | read_lock_bh(&ref_table_lock); | |
2526 | while (++index < tipc_ref_table.capacity) { | |
2527 | entry = &tipc_ref_table.entries[index]; | |
2528 | if (!entry->tsk) | |
2529 | continue; | |
2530 | tsk = entry->tsk; | |
2531 | sock_hold(&tsk->sk); | |
2532 | *ref = entry->ref; | |
2533 | break; | |
2534 | } | |
2535 | read_unlock_bh(&ref_table_lock); | |
2536 | return tsk; | |
2537 | } | |
2538 | ||
2539 | static void tipc_sk_put(struct tipc_sock *tsk) | |
2540 | { | |
2541 | sock_put(&tsk->sk); | |
2542 | } | |
2543 | ||
b97bf3fd | 2544 | /** |
247f0f3c | 2545 | * tipc_setsockopt - set socket option |
b97bf3fd PL |
2546 | * @sock: socket structure |
2547 | * @lvl: option level | |
2548 | * @opt: option identifier | |
2549 | * @ov: pointer to new option value | |
2550 | * @ol: length of option value | |
c4307285 YH |
2551 | * |
2552 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options | |
b97bf3fd | 2553 | * (to ease compatibility). |
c4307285 | 2554 | * |
b97bf3fd PL |
2555 | * Returns 0 on success, errno otherwise |
2556 | */ | |
247f0f3c YX |
2557 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
2558 | char __user *ov, unsigned int ol) | |
b97bf3fd | 2559 | { |
0c3141e9 | 2560 | struct sock *sk = sock->sk; |
58ed9442 | 2561 | struct tipc_sock *tsk = tipc_sk(sk); |
b97bf3fd PL |
2562 | u32 value; |
2563 | int res; | |
2564 | ||
c4307285 YH |
2565 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2566 | return 0; | |
b97bf3fd PL |
2567 | if (lvl != SOL_TIPC) |
2568 | return -ENOPROTOOPT; | |
2569 | if (ol < sizeof(value)) | |
2570 | return -EINVAL; | |
2db9983a AS |
2571 | res = get_user(value, (u32 __user *)ov); |
2572 | if (res) | |
b97bf3fd PL |
2573 | return res; |
2574 | ||
0c3141e9 | 2575 | lock_sock(sk); |
c4307285 | 2576 | |
b97bf3fd PL |
2577 | switch (opt) { |
2578 | case TIPC_IMPORTANCE: | |
301bae56 | 2579 | res = tsk_set_importance(tsk, value); |
b97bf3fd PL |
2580 | break; |
2581 | case TIPC_SRC_DROPPABLE: | |
2582 | if (sock->type != SOCK_STREAM) | |
301bae56 | 2583 | tsk_set_unreliable(tsk, value); |
c4307285 | 2584 | else |
b97bf3fd PL |
2585 | res = -ENOPROTOOPT; |
2586 | break; | |
2587 | case TIPC_DEST_DROPPABLE: | |
301bae56 | 2588 | tsk_set_unreturnable(tsk, value); |
b97bf3fd PL |
2589 | break; |
2590 | case TIPC_CONN_TIMEOUT: | |
a0f40f02 | 2591 | tipc_sk(sk)->conn_timeout = value; |
0c3141e9 | 2592 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd PL |
2593 | break; |
2594 | default: | |
2595 | res = -EINVAL; | |
2596 | } | |
2597 | ||
0c3141e9 AS |
2598 | release_sock(sk); |
2599 | ||
b97bf3fd PL |
2600 | return res; |
2601 | } | |
2602 | ||
2603 | /** | |
247f0f3c | 2604 | * tipc_getsockopt - get socket option |
b97bf3fd PL |
2605 | * @sock: socket structure |
2606 | * @lvl: option level | |
2607 | * @opt: option identifier | |
2608 | * @ov: receptacle for option value | |
2609 | * @ol: receptacle for length of option value | |
c4307285 YH |
2610 | * |
2611 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options | |
b97bf3fd | 2612 | * (to ease compatibility). |
c4307285 | 2613 | * |
b97bf3fd PL |
2614 | * Returns 0 on success, errno otherwise |
2615 | */ | |
247f0f3c YX |
2616 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
2617 | char __user *ov, int __user *ol) | |
b97bf3fd | 2618 | { |
0c3141e9 | 2619 | struct sock *sk = sock->sk; |
58ed9442 | 2620 | struct tipc_sock *tsk = tipc_sk(sk); |
c4307285 | 2621 | int len; |
b97bf3fd | 2622 | u32 value; |
c4307285 | 2623 | int res; |
b97bf3fd | 2624 | |
c4307285 YH |
2625 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
2626 | return put_user(0, ol); | |
b97bf3fd PL |
2627 | if (lvl != SOL_TIPC) |
2628 | return -ENOPROTOOPT; | |
2db9983a AS |
2629 | res = get_user(len, ol); |
2630 | if (res) | |
c4307285 | 2631 | return res; |
b97bf3fd | 2632 | |
0c3141e9 | 2633 | lock_sock(sk); |
b97bf3fd PL |
2634 | |
2635 | switch (opt) { | |
2636 | case TIPC_IMPORTANCE: | |
301bae56 | 2637 | value = tsk_importance(tsk); |
b97bf3fd PL |
2638 | break; |
2639 | case TIPC_SRC_DROPPABLE: | |
301bae56 | 2640 | value = tsk_unreliable(tsk); |
b97bf3fd PL |
2641 | break; |
2642 | case TIPC_DEST_DROPPABLE: | |
301bae56 | 2643 | value = tsk_unreturnable(tsk); |
b97bf3fd PL |
2644 | break; |
2645 | case TIPC_CONN_TIMEOUT: | |
301bae56 | 2646 | value = tsk->conn_timeout; |
0c3141e9 | 2647 | /* no need to set "res", since already 0 at this point */ |
b97bf3fd | 2648 | break; |
0e65967e | 2649 | case TIPC_NODE_RECVQ_DEPTH: |
9da3d475 | 2650 | value = 0; /* was tipc_queue_size, now obsolete */ |
6650613d | 2651 | break; |
0e65967e | 2652 | case TIPC_SOCK_RECVQ_DEPTH: |
6650613d | 2653 | value = skb_queue_len(&sk->sk_receive_queue); |
2654 | break; | |
b97bf3fd PL |
2655 | default: |
2656 | res = -EINVAL; | |
2657 | } | |
2658 | ||
0c3141e9 AS |
2659 | release_sock(sk); |
2660 | ||
25860c3b PG |
2661 | if (res) |
2662 | return res; /* "get" failed */ | |
b97bf3fd | 2663 | |
25860c3b PG |
2664 | if (len < sizeof(value)) |
2665 | return -EINVAL; | |
2666 | ||
2667 | if (copy_to_user(ov, &value, sizeof(value))) | |
2668 | return -EFAULT; | |
2669 | ||
2670 | return put_user(sizeof(value), ol); | |
b97bf3fd PL |
2671 | } |
2672 | ||
52f50ce5 | 2673 | static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg) |
78acb1f9 EH |
2674 | { |
2675 | struct tipc_sioc_ln_req lnr; | |
2676 | void __user *argp = (void __user *)arg; | |
2677 | ||
2678 | switch (cmd) { | |
2679 | case SIOCGETLINKNAME: | |
2680 | if (copy_from_user(&lnr, argp, sizeof(lnr))) | |
2681 | return -EFAULT; | |
7b8613e0 | 2682 | if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer, |
78acb1f9 EH |
2683 | lnr.linkname, TIPC_MAX_LINK_NAME)) { |
2684 | if (copy_to_user(argp, &lnr, sizeof(lnr))) | |
2685 | return -EFAULT; | |
2686 | return 0; | |
2687 | } | |
2688 | return -EADDRNOTAVAIL; | |
78acb1f9 EH |
2689 | default: |
2690 | return -ENOIOCTLCMD; | |
2691 | } | |
2692 | } | |
2693 | ||
ae86b9e3 BH |
2694 | /* Protocol switches for the various types of TIPC sockets */ |
2695 | ||
bca65eae | 2696 | static const struct proto_ops msg_ops = { |
0e65967e | 2697 | .owner = THIS_MODULE, |
b97bf3fd | 2698 | .family = AF_TIPC, |
247f0f3c YX |
2699 | .release = tipc_release, |
2700 | .bind = tipc_bind, | |
2701 | .connect = tipc_connect, | |
5eee6a6d | 2702 | .socketpair = sock_no_socketpair, |
245f3d34 | 2703 | .accept = sock_no_accept, |
247f0f3c YX |
2704 | .getname = tipc_getname, |
2705 | .poll = tipc_poll, | |
78acb1f9 | 2706 | .ioctl = tipc_ioctl, |
245f3d34 | 2707 | .listen = sock_no_listen, |
247f0f3c YX |
2708 | .shutdown = tipc_shutdown, |
2709 | .setsockopt = tipc_setsockopt, | |
2710 | .getsockopt = tipc_getsockopt, | |
2711 | .sendmsg = tipc_sendmsg, | |
2712 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2713 | .mmap = sock_no_mmap, |
2714 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2715 | }; |
2716 | ||
bca65eae | 2717 | static const struct proto_ops packet_ops = { |
0e65967e | 2718 | .owner = THIS_MODULE, |
b97bf3fd | 2719 | .family = AF_TIPC, |
247f0f3c YX |
2720 | .release = tipc_release, |
2721 | .bind = tipc_bind, | |
2722 | .connect = tipc_connect, | |
5eee6a6d | 2723 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2724 | .accept = tipc_accept, |
2725 | .getname = tipc_getname, | |
2726 | .poll = tipc_poll, | |
78acb1f9 | 2727 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2728 | .listen = tipc_listen, |
2729 | .shutdown = tipc_shutdown, | |
2730 | .setsockopt = tipc_setsockopt, | |
2731 | .getsockopt = tipc_getsockopt, | |
2732 | .sendmsg = tipc_send_packet, | |
2733 | .recvmsg = tipc_recvmsg, | |
8238745a YH |
2734 | .mmap = sock_no_mmap, |
2735 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2736 | }; |
2737 | ||
bca65eae | 2738 | static const struct proto_ops stream_ops = { |
0e65967e | 2739 | .owner = THIS_MODULE, |
b97bf3fd | 2740 | .family = AF_TIPC, |
247f0f3c YX |
2741 | .release = tipc_release, |
2742 | .bind = tipc_bind, | |
2743 | .connect = tipc_connect, | |
5eee6a6d | 2744 | .socketpair = sock_no_socketpair, |
247f0f3c YX |
2745 | .accept = tipc_accept, |
2746 | .getname = tipc_getname, | |
2747 | .poll = tipc_poll, | |
78acb1f9 | 2748 | .ioctl = tipc_ioctl, |
247f0f3c YX |
2749 | .listen = tipc_listen, |
2750 | .shutdown = tipc_shutdown, | |
2751 | .setsockopt = tipc_setsockopt, | |
2752 | .getsockopt = tipc_getsockopt, | |
2753 | .sendmsg = tipc_send_stream, | |
2754 | .recvmsg = tipc_recv_stream, | |
8238745a YH |
2755 | .mmap = sock_no_mmap, |
2756 | .sendpage = sock_no_sendpage | |
b97bf3fd PL |
2757 | }; |
2758 | ||
bca65eae | 2759 | static const struct net_proto_family tipc_family_ops = { |
0e65967e | 2760 | .owner = THIS_MODULE, |
b97bf3fd | 2761 | .family = AF_TIPC, |
c5fa7b3c | 2762 | .create = tipc_sk_create |
b97bf3fd PL |
2763 | }; |
2764 | ||
2765 | static struct proto tipc_proto = { | |
2766 | .name = "TIPC", | |
2767 | .owner = THIS_MODULE, | |
cc79dd1b YX |
2768 | .obj_size = sizeof(struct tipc_sock), |
2769 | .sysctl_rmem = sysctl_tipc_rmem | |
b97bf3fd PL |
2770 | }; |
2771 | ||
c5fa7b3c YX |
2772 | static struct proto tipc_proto_kern = { |
2773 | .name = "TIPC", | |
2774 | .obj_size = sizeof(struct tipc_sock), | |
2775 | .sysctl_rmem = sysctl_tipc_rmem | |
2776 | }; | |
2777 | ||
b97bf3fd | 2778 | /** |
4323add6 | 2779 | * tipc_socket_init - initialize TIPC socket interface |
c4307285 | 2780 | * |
b97bf3fd PL |
2781 | * Returns 0 on success, errno otherwise |
2782 | */ | |
4323add6 | 2783 | int tipc_socket_init(void) |
b97bf3fd PL |
2784 | { |
2785 | int res; | |
2786 | ||
c4307285 | 2787 | res = proto_register(&tipc_proto, 1); |
b97bf3fd | 2788 | if (res) { |
2cf8aa19 | 2789 | pr_err("Failed to register TIPC protocol type\n"); |
b97bf3fd PL |
2790 | goto out; |
2791 | } | |
2792 | ||
2793 | res = sock_register(&tipc_family_ops); | |
2794 | if (res) { | |
2cf8aa19 | 2795 | pr_err("Failed to register TIPC socket type\n"); |
b97bf3fd PL |
2796 | proto_unregister(&tipc_proto); |
2797 | goto out; | |
2798 | } | |
b97bf3fd PL |
2799 | out: |
2800 | return res; | |
2801 | } | |
2802 | ||
2803 | /** | |
4323add6 | 2804 | * tipc_socket_stop - stop TIPC socket interface |
b97bf3fd | 2805 | */ |
4323add6 | 2806 | void tipc_socket_stop(void) |
b97bf3fd | 2807 | { |
b97bf3fd PL |
2808 | sock_unregister(tipc_family_ops.family); |
2809 | proto_unregister(&tipc_proto); | |
2810 | } | |
34b78a12 RA |
2811 | |
2812 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 | 2813 | static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk) |
34b78a12 RA |
2814 | { |
2815 | u32 peer_node; | |
2816 | u32 peer_port; | |
2817 | struct nlattr *nest; | |
2818 | ||
2819 | peer_node = tsk_peer_node(tsk); | |
2820 | peer_port = tsk_peer_port(tsk); | |
2821 | ||
2822 | nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON); | |
2823 | ||
2824 | if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node)) | |
2825 | goto msg_full; | |
2826 | if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port)) | |
2827 | goto msg_full; | |
2828 | ||
2829 | if (tsk->conn_type != 0) { | |
2830 | if (nla_put_flag(skb, TIPC_NLA_CON_FLAG)) | |
2831 | goto msg_full; | |
2832 | if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type)) | |
2833 | goto msg_full; | |
2834 | if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance)) | |
2835 | goto msg_full; | |
2836 | } | |
2837 | nla_nest_end(skb, nest); | |
2838 | ||
2839 | return 0; | |
2840 | ||
2841 | msg_full: | |
2842 | nla_nest_cancel(skb, nest); | |
2843 | ||
2844 | return -EMSGSIZE; | |
2845 | } | |
2846 | ||
2847 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2848 | static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb, |
2849 | struct tipc_sock *tsk) | |
34b78a12 RA |
2850 | { |
2851 | int err; | |
2852 | void *hdr; | |
2853 | struct nlattr *attrs; | |
2854 | ||
2855 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, | |
2856 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); | |
2857 | if (!hdr) | |
2858 | goto msg_cancel; | |
2859 | ||
2860 | attrs = nla_nest_start(skb, TIPC_NLA_SOCK); | |
2861 | if (!attrs) | |
2862 | goto genlmsg_cancel; | |
2863 | if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->ref)) | |
2864 | goto attr_msg_cancel; | |
2865 | if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr)) | |
2866 | goto attr_msg_cancel; | |
2867 | ||
2868 | if (tsk->connected) { | |
2869 | err = __tipc_nl_add_sk_con(skb, tsk); | |
2870 | if (err) | |
2871 | goto attr_msg_cancel; | |
2872 | } else if (!list_empty(&tsk->publications)) { | |
2873 | if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL)) | |
2874 | goto attr_msg_cancel; | |
2875 | } | |
2876 | nla_nest_end(skb, attrs); | |
2877 | genlmsg_end(skb, hdr); | |
2878 | ||
2879 | return 0; | |
2880 | ||
2881 | attr_msg_cancel: | |
2882 | nla_nest_cancel(skb, attrs); | |
2883 | genlmsg_cancel: | |
2884 | genlmsg_cancel(skb, hdr); | |
2885 | msg_cancel: | |
2886 | return -EMSGSIZE; | |
2887 | } | |
2888 | ||
2889 | int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2890 | { | |
2891 | int err; | |
2892 | struct tipc_sock *tsk; | |
2893 | u32 prev_ref = cb->args[0]; | |
2894 | u32 ref = prev_ref; | |
2895 | ||
2896 | tsk = tipc_sk_get_next(&ref); | |
2897 | for (; tsk; tsk = tipc_sk_get_next(&ref)) { | |
2898 | lock_sock(&tsk->sk); | |
2899 | err = __tipc_nl_add_sk(skb, cb, tsk); | |
2900 | release_sock(&tsk->sk); | |
2901 | tipc_sk_put(tsk); | |
2902 | if (err) | |
2903 | break; | |
2904 | ||
2905 | prev_ref = ref; | |
2906 | } | |
2907 | ||
2908 | cb->args[0] = prev_ref; | |
2909 | ||
2910 | return skb->len; | |
2911 | } | |
1a1a143d RA |
2912 | |
2913 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2914 | static int __tipc_nl_add_sk_publ(struct sk_buff *skb, |
2915 | struct netlink_callback *cb, | |
2916 | struct publication *publ) | |
1a1a143d RA |
2917 | { |
2918 | void *hdr; | |
2919 | struct nlattr *attrs; | |
2920 | ||
2921 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, | |
2922 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET); | |
2923 | if (!hdr) | |
2924 | goto msg_cancel; | |
2925 | ||
2926 | attrs = nla_nest_start(skb, TIPC_NLA_PUBL); | |
2927 | if (!attrs) | |
2928 | goto genlmsg_cancel; | |
2929 | ||
2930 | if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) | |
2931 | goto attr_msg_cancel; | |
2932 | if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type)) | |
2933 | goto attr_msg_cancel; | |
2934 | if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower)) | |
2935 | goto attr_msg_cancel; | |
2936 | if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper)) | |
2937 | goto attr_msg_cancel; | |
2938 | ||
2939 | nla_nest_end(skb, attrs); | |
2940 | genlmsg_end(skb, hdr); | |
2941 | ||
2942 | return 0; | |
2943 | ||
2944 | attr_msg_cancel: | |
2945 | nla_nest_cancel(skb, attrs); | |
2946 | genlmsg_cancel: | |
2947 | genlmsg_cancel(skb, hdr); | |
2948 | msg_cancel: | |
2949 | return -EMSGSIZE; | |
2950 | } | |
2951 | ||
2952 | /* Caller should hold socket lock for the passed tipc socket. */ | |
d8182804 RA |
2953 | static int __tipc_nl_list_sk_publ(struct sk_buff *skb, |
2954 | struct netlink_callback *cb, | |
2955 | struct tipc_sock *tsk, u32 *last_publ) | |
1a1a143d RA |
2956 | { |
2957 | int err; | |
2958 | struct publication *p; | |
2959 | ||
2960 | if (*last_publ) { | |
2961 | list_for_each_entry(p, &tsk->publications, pport_list) { | |
2962 | if (p->key == *last_publ) | |
2963 | break; | |
2964 | } | |
2965 | if (p->key != *last_publ) { | |
2966 | /* We never set seq or call nl_dump_check_consistent() | |
2967 | * this means that setting prev_seq here will cause the | |
2968 | * consistence check to fail in the netlink callback | |
2969 | * handler. Resulting in the last NLMSG_DONE message | |
2970 | * having the NLM_F_DUMP_INTR flag set. | |
2971 | */ | |
2972 | cb->prev_seq = 1; | |
2973 | *last_publ = 0; | |
2974 | return -EPIPE; | |
2975 | } | |
2976 | } else { | |
2977 | p = list_first_entry(&tsk->publications, struct publication, | |
2978 | pport_list); | |
2979 | } | |
2980 | ||
2981 | list_for_each_entry_from(p, &tsk->publications, pport_list) { | |
2982 | err = __tipc_nl_add_sk_publ(skb, cb, p); | |
2983 | if (err) { | |
2984 | *last_publ = p->key; | |
2985 | return err; | |
2986 | } | |
2987 | } | |
2988 | *last_publ = 0; | |
2989 | ||
2990 | return 0; | |
2991 | } | |
2992 | ||
2993 | int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2994 | { | |
2995 | int err; | |
2996 | u32 tsk_ref = cb->args[0]; | |
2997 | u32 last_publ = cb->args[1]; | |
2998 | u32 done = cb->args[2]; | |
2999 | struct tipc_sock *tsk; | |
3000 | ||
3001 | if (!tsk_ref) { | |
3002 | struct nlattr **attrs; | |
3003 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; | |
3004 | ||
3005 | err = tipc_nlmsg_parse(cb->nlh, &attrs); | |
3006 | if (err) | |
3007 | return err; | |
3008 | ||
3009 | err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, | |
3010 | attrs[TIPC_NLA_SOCK], | |
3011 | tipc_nl_sock_policy); | |
3012 | if (err) | |
3013 | return err; | |
3014 | ||
3015 | if (!sock[TIPC_NLA_SOCK_REF]) | |
3016 | return -EINVAL; | |
3017 | ||
3018 | tsk_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); | |
3019 | } | |
3020 | ||
3021 | if (done) | |
3022 | return 0; | |
3023 | ||
3024 | tsk = tipc_sk_get(tsk_ref); | |
3025 | if (!tsk) | |
3026 | return -EINVAL; | |
3027 | ||
3028 | lock_sock(&tsk->sk); | |
3029 | err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ); | |
3030 | if (!err) | |
3031 | done = 1; | |
3032 | release_sock(&tsk->sk); | |
3033 | tipc_sk_put(tsk); | |
3034 | ||
3035 | cb->args[0] = tsk_ref; | |
3036 | cb->args[1] = last_publ; | |
3037 | cb->args[2] = done; | |
3038 | ||
3039 | return skb->len; | |
3040 | } |