Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/node.c: TIPC node management routines | |
c4307285 | 3 | * |
16e166b8 | 4 | * Copyright (c) 2000-2006, 2012-2014, Ericsson AB |
46651c59 | 5 | * Copyright (c) 2005-2006, 2010-2014, 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 | ||
37 | #include "core.h" | |
38 | #include "config.h" | |
39 | #include "node.h" | |
b97bf3fd | 40 | #include "name_distr.h" |
50100a5e | 41 | #include "socket.h" |
b97bf3fd | 42 | |
a635b46b AS |
43 | #define NODE_HTABLE_SIZE 512 |
44 | ||
6c00055a DM |
45 | static void node_lost_contact(struct tipc_node *n_ptr); |
46 | static void node_established_contact(struct tipc_node *n_ptr); | |
b97bf3fd | 47 | |
672d99e1 AS |
48 | static struct hlist_head node_htable[NODE_HTABLE_SIZE]; |
49 | LIST_HEAD(tipc_node_list); | |
50 | static u32 tipc_num_nodes; | |
dde20266 | 51 | static u32 tipc_num_links; |
46651c59 | 52 | static DEFINE_SPINLOCK(node_list_lock); |
34e46258 | 53 | |
02be61a9 JPM |
54 | struct tipc_sock_conn { |
55 | u32 port; | |
56 | u32 peer_port; | |
57 | u32 peer_node; | |
58 | struct list_head list; | |
59 | }; | |
60 | ||
a635b46b AS |
61 | /* |
62 | * A trivial power-of-two bitmask technique is used for speed, since this | |
63 | * operation is done for every incoming TIPC packet. The number of hash table | |
64 | * entries has been chosen so that no hash chain exceeds 8 nodes and will | |
65 | * usually be much smaller (typically only a single node). | |
66 | */ | |
872f24db | 67 | static unsigned int tipc_hashfn(u32 addr) |
a635b46b AS |
68 | { |
69 | return addr & (NODE_HTABLE_SIZE - 1); | |
70 | } | |
71 | ||
1ec2bb08 | 72 | /* |
672d99e1 AS |
73 | * tipc_node_find - locate specified node object, if it exists |
74 | */ | |
672d99e1 AS |
75 | struct tipc_node *tipc_node_find(u32 addr) |
76 | { | |
77 | struct tipc_node *node; | |
672d99e1 | 78 | |
336ebf5b | 79 | if (unlikely(!in_own_cluster_exact(addr))) |
672d99e1 AS |
80 | return NULL; |
81 | ||
6c7a762e YX |
82 | rcu_read_lock(); |
83 | hlist_for_each_entry_rcu(node, &node_htable[tipc_hashfn(addr)], hash) { | |
46651c59 | 84 | if (node->addr == addr) { |
6c7a762e | 85 | rcu_read_unlock(); |
672d99e1 | 86 | return node; |
46651c59 | 87 | } |
672d99e1 | 88 | } |
6c7a762e | 89 | rcu_read_unlock(); |
672d99e1 AS |
90 | return NULL; |
91 | } | |
92 | ||
6c00055a | 93 | struct tipc_node *tipc_node_create(u32 addr) |
b97bf3fd | 94 | { |
672d99e1 | 95 | struct tipc_node *n_ptr, *temp_node; |
b97bf3fd | 96 | |
46651c59 | 97 | spin_lock_bh(&node_list_lock); |
2ecb0924 | 98 | |
5af54792 | 99 | n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC); |
a10bd924 | 100 | if (!n_ptr) { |
46651c59 | 101 | spin_unlock_bh(&node_list_lock); |
2cf8aa19 | 102 | pr_warn("Node creation failed, no memory\n"); |
a10bd924 AS |
103 | return NULL; |
104 | } | |
105 | ||
a10bd924 | 106 | n_ptr->addr = addr; |
51a8e4de | 107 | spin_lock_init(&n_ptr->lock); |
672d99e1 AS |
108 | INIT_HLIST_NODE(&n_ptr->hash); |
109 | INIT_LIST_HEAD(&n_ptr->list); | |
a10bd924 | 110 | INIT_LIST_HEAD(&n_ptr->nsub); |
02be61a9 | 111 | INIT_LIST_HEAD(&n_ptr->conn_sks); |
50100a5e | 112 | __skb_queue_head_init(&n_ptr->waiting_sks); |
8f92df6a | 113 | |
6c7a762e | 114 | hlist_add_head_rcu(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]); |
672d99e1 | 115 | |
6c7a762e | 116 | list_for_each_entry_rcu(temp_node, &tipc_node_list, list) { |
672d99e1 AS |
117 | if (n_ptr->addr < temp_node->addr) |
118 | break; | |
119 | } | |
6c7a762e | 120 | list_add_tail_rcu(&n_ptr->list, &temp_node->list); |
aecb9bb8 | 121 | n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN; |
fc0eea69 | 122 | n_ptr->signature = INVALID_NODE_SIG; |
672d99e1 AS |
123 | |
124 | tipc_num_nodes++; | |
a10bd924 | 125 | |
46651c59 | 126 | spin_unlock_bh(&node_list_lock); |
b97bf3fd PL |
127 | return n_ptr; |
128 | } | |
129 | ||
46651c59 | 130 | static void tipc_node_delete(struct tipc_node *n_ptr) |
b97bf3fd | 131 | { |
6c7a762e YX |
132 | list_del_rcu(&n_ptr->list); |
133 | hlist_del_rcu(&n_ptr->hash); | |
134 | kfree_rcu(n_ptr, rcu); | |
8f92df6a | 135 | |
672d99e1 | 136 | tipc_num_nodes--; |
b97bf3fd PL |
137 | } |
138 | ||
46651c59 YX |
139 | void tipc_node_stop(void) |
140 | { | |
141 | struct tipc_node *node, *t_node; | |
142 | ||
143 | spin_lock_bh(&node_list_lock); | |
144 | list_for_each_entry_safe(node, t_node, &tipc_node_list, list) | |
145 | tipc_node_delete(node); | |
146 | spin_unlock_bh(&node_list_lock); | |
147 | } | |
148 | ||
02be61a9 JPM |
149 | int tipc_node_add_conn(u32 dnode, u32 port, u32 peer_port) |
150 | { | |
151 | struct tipc_node *node; | |
152 | struct tipc_sock_conn *conn; | |
153 | ||
154 | if (in_own_node(dnode)) | |
155 | return 0; | |
156 | ||
157 | node = tipc_node_find(dnode); | |
158 | if (!node) { | |
159 | pr_warn("Connecting sock to node 0x%x failed\n", dnode); | |
160 | return -EHOSTUNREACH; | |
161 | } | |
162 | conn = kmalloc(sizeof(*conn), GFP_ATOMIC); | |
163 | if (!conn) | |
164 | return -EHOSTUNREACH; | |
165 | conn->peer_node = dnode; | |
166 | conn->port = port; | |
167 | conn->peer_port = peer_port; | |
168 | ||
169 | tipc_node_lock(node); | |
170 | list_add_tail(&conn->list, &node->conn_sks); | |
171 | tipc_node_unlock(node); | |
172 | return 0; | |
173 | } | |
174 | ||
175 | void tipc_node_remove_conn(u32 dnode, u32 port) | |
176 | { | |
177 | struct tipc_node *node; | |
178 | struct tipc_sock_conn *conn, *safe; | |
179 | ||
180 | if (in_own_node(dnode)) | |
181 | return; | |
182 | ||
183 | node = tipc_node_find(dnode); | |
184 | if (!node) | |
185 | return; | |
186 | ||
187 | tipc_node_lock(node); | |
188 | list_for_each_entry_safe(conn, safe, &node->conn_sks, list) { | |
189 | if (port != conn->port) | |
190 | continue; | |
191 | list_del(&conn->list); | |
192 | kfree(conn); | |
193 | } | |
194 | tipc_node_unlock(node); | |
195 | } | |
196 | ||
197 | void tipc_node_abort_sock_conns(struct list_head *conns) | |
198 | { | |
199 | struct tipc_sock_conn *conn, *safe; | |
200 | struct sk_buff *buf; | |
201 | ||
202 | list_for_each_entry_safe(conn, safe, conns, list) { | |
203 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, | |
204 | SHORT_H_SIZE, 0, tipc_own_addr, | |
205 | conn->peer_node, conn->port, | |
206 | conn->peer_port, TIPC_ERR_NO_NODE); | |
207 | if (likely(buf)) | |
208 | tipc_sk_rcv(buf); | |
209 | list_del(&conn->list); | |
210 | kfree(conn); | |
211 | } | |
212 | } | |
213 | ||
b97bf3fd | 214 | /** |
4323add6 | 215 | * tipc_node_link_up - handle addition of link |
c4307285 | 216 | * |
b97bf3fd PL |
217 | * Link becomes active (alone or shared) or standby, depending on its priority. |
218 | */ | |
a18c4bc3 | 219 | void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 220 | { |
a18c4bc3 | 221 | struct tipc_link **active = &n_ptr->active_links[0]; |
b97bf3fd | 222 | |
5392d646 | 223 | n_ptr->working_links++; |
7b8613e0 YX |
224 | n_ptr->action_flags |= TIPC_NOTIFY_LINK_UP; |
225 | n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id; | |
226 | ||
2cf8aa19 | 227 | pr_info("Established link <%s> on network plane %c\n", |
7a2f7d18 | 228 | l_ptr->name, l_ptr->net_plane); |
c4307285 | 229 | |
b97bf3fd | 230 | if (!active[0]) { |
b97bf3fd PL |
231 | active[0] = active[1] = l_ptr; |
232 | node_established_contact(n_ptr); | |
16e166b8 | 233 | goto exit; |
b97bf3fd | 234 | } |
c4307285 | 235 | if (l_ptr->priority < active[0]->priority) { |
2cf8aa19 | 236 | pr_info("New link <%s> becomes standby\n", l_ptr->name); |
16e166b8 | 237 | goto exit; |
b97bf3fd | 238 | } |
247f0f3c | 239 | tipc_link_dup_queue_xmit(active[0], l_ptr); |
c4307285 | 240 | if (l_ptr->priority == active[0]->priority) { |
b97bf3fd | 241 | active[0] = l_ptr; |
16e166b8 | 242 | goto exit; |
b97bf3fd | 243 | } |
2cf8aa19 | 244 | pr_info("Old link <%s> becomes standby\n", active[0]->name); |
a10bd924 | 245 | if (active[1] != active[0]) |
2cf8aa19 | 246 | pr_info("Old link <%s> becomes standby\n", active[1]->name); |
b97bf3fd | 247 | active[0] = active[1] = l_ptr; |
16e166b8 JPM |
248 | exit: |
249 | /* Leave room for changeover header when returning 'mtu' to users: */ | |
250 | n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; | |
251 | n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; | |
b97bf3fd PL |
252 | } |
253 | ||
254 | /** | |
255 | * node_select_active_links - select active link | |
256 | */ | |
6c00055a | 257 | static void node_select_active_links(struct tipc_node *n_ptr) |
b97bf3fd | 258 | { |
a18c4bc3 | 259 | struct tipc_link **active = &n_ptr->active_links[0]; |
b97bf3fd PL |
260 | u32 i; |
261 | u32 highest_prio = 0; | |
262 | ||
c4307285 | 263 | active[0] = active[1] = NULL; |
b97bf3fd PL |
264 | |
265 | for (i = 0; i < MAX_BEARERS; i++) { | |
a18c4bc3 | 266 | struct tipc_link *l_ptr = n_ptr->links[i]; |
b97bf3fd | 267 | |
4323add6 | 268 | if (!l_ptr || !tipc_link_is_up(l_ptr) || |
b97bf3fd PL |
269 | (l_ptr->priority < highest_prio)) |
270 | continue; | |
271 | ||
272 | if (l_ptr->priority > highest_prio) { | |
c4307285 | 273 | highest_prio = l_ptr->priority; |
b97bf3fd PL |
274 | active[0] = active[1] = l_ptr; |
275 | } else { | |
276 | active[1] = l_ptr; | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | /** | |
4323add6 | 282 | * tipc_node_link_down - handle loss of link |
b97bf3fd | 283 | */ |
a18c4bc3 | 284 | void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 285 | { |
a18c4bc3 | 286 | struct tipc_link **active; |
b97bf3fd | 287 | |
5392d646 | 288 | n_ptr->working_links--; |
7b8613e0 YX |
289 | n_ptr->action_flags |= TIPC_NOTIFY_LINK_DOWN; |
290 | n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id; | |
5392d646 | 291 | |
4323add6 | 292 | if (!tipc_link_is_active(l_ptr)) { |
2cf8aa19 | 293 | pr_info("Lost standby link <%s> on network plane %c\n", |
7a2f7d18 | 294 | l_ptr->name, l_ptr->net_plane); |
b97bf3fd PL |
295 | return; |
296 | } | |
2cf8aa19 | 297 | pr_info("Lost link <%s> on network plane %c\n", |
7a2f7d18 | 298 | l_ptr->name, l_ptr->net_plane); |
b97bf3fd PL |
299 | |
300 | active = &n_ptr->active_links[0]; | |
301 | if (active[0] == l_ptr) | |
302 | active[0] = active[1]; | |
303 | if (active[1] == l_ptr) | |
304 | active[1] = active[0]; | |
305 | if (active[0] == l_ptr) | |
306 | node_select_active_links(n_ptr); | |
c4307285 | 307 | if (tipc_node_is_up(n_ptr)) |
170b3927 | 308 | tipc_link_failover_send_queue(l_ptr); |
c4307285 | 309 | else |
b97bf3fd | 310 | node_lost_contact(n_ptr); |
16e166b8 JPM |
311 | |
312 | /* Leave room for changeover header when returning 'mtu' to users: */ | |
313 | if (active[0]) { | |
314 | n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; | |
315 | n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; | |
316 | return; | |
317 | } | |
318 | ||
319 | /* Loopback link went down? No fragmentation needed from now on. */ | |
320 | if (n_ptr->addr == tipc_own_addr) { | |
321 | n_ptr->act_mtus[0] = MAX_MSG_SIZE; | |
322 | n_ptr->act_mtus[1] = MAX_MSG_SIZE; | |
323 | } | |
b97bf3fd PL |
324 | } |
325 | ||
8f19afb2 | 326 | int tipc_node_active_links(struct tipc_node *n_ptr) |
b97bf3fd | 327 | { |
76ae0d71 | 328 | return n_ptr->active_links[0] != NULL; |
b97bf3fd PL |
329 | } |
330 | ||
6c00055a | 331 | int tipc_node_is_up(struct tipc_node *n_ptr) |
b97bf3fd | 332 | { |
8f19afb2 | 333 | return tipc_node_active_links(n_ptr); |
b97bf3fd PL |
334 | } |
335 | ||
a18c4bc3 | 336 | void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 337 | { |
7a2f7d18 | 338 | n_ptr->links[l_ptr->bearer_id] = l_ptr; |
dde20266 YX |
339 | spin_lock_bh(&node_list_lock); |
340 | tipc_num_links++; | |
341 | spin_unlock_bh(&node_list_lock); | |
37b9c08a | 342 | n_ptr->link_cnt++; |
b97bf3fd PL |
343 | } |
344 | ||
a18c4bc3 | 345 | void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 346 | { |
7d33939f JPM |
347 | int i; |
348 | ||
349 | for (i = 0; i < MAX_BEARERS; i++) { | |
074bb43e JPM |
350 | if (l_ptr != n_ptr->links[i]) |
351 | continue; | |
352 | n_ptr->links[i] = NULL; | |
dde20266 YX |
353 | spin_lock_bh(&node_list_lock); |
354 | tipc_num_links--; | |
355 | spin_unlock_bh(&node_list_lock); | |
074bb43e | 356 | n_ptr->link_cnt--; |
7d33939f | 357 | } |
b97bf3fd PL |
358 | } |
359 | ||
6c00055a | 360 | static void node_established_contact(struct tipc_node *n_ptr) |
b97bf3fd | 361 | { |
aecb9bb8 | 362 | n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP; |
c64f7a6a | 363 | n_ptr->bclink.oos_state = 0; |
818f4da5 YX |
364 | n_ptr->bclink.acked = tipc_bclink_get_last_sent(); |
365 | tipc_bclink_add_node(n_ptr->addr); | |
b97bf3fd PL |
366 | } |
367 | ||
6c00055a | 368 | static void node_lost_contact(struct tipc_node *n_ptr) |
b97bf3fd | 369 | { |
b97bf3fd PL |
370 | char addr_string[16]; |
371 | u32 i; | |
372 | ||
2cf8aa19 EH |
373 | pr_info("Lost contact with %s\n", |
374 | tipc_addr_string_fill(addr_string, n_ptr->addr)); | |
c5bd4d85 AS |
375 | |
376 | /* Flush broadcast link info associated with lost node */ | |
389dd9bc | 377 | if (n_ptr->bclink.recv_permitted) { |
d77b3831 | 378 | kfree_skb_list(n_ptr->bclink.deferred_head); |
7a54d4a9 | 379 | n_ptr->bclink.deferred_size = 0; |
c5bd4d85 | 380 | |
37e22164 JPM |
381 | if (n_ptr->bclink.reasm_buf) { |
382 | kfree_skb(n_ptr->bclink.reasm_buf); | |
383 | n_ptr->bclink.reasm_buf = NULL; | |
c5bd4d85 AS |
384 | } |
385 | ||
cd3decdf | 386 | tipc_bclink_remove_node(n_ptr->addr); |
36559591 | 387 | tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ); |
b97bf3fd | 388 | |
389dd9bc | 389 | n_ptr->bclink.recv_permitted = false; |
c5bd4d85 | 390 | } |
b97bf3fd PL |
391 | |
392 | /* Abort link changeover */ | |
393 | for (i = 0; i < MAX_BEARERS; i++) { | |
a18c4bc3 | 394 | struct tipc_link *l_ptr = n_ptr->links[i]; |
c4307285 | 395 | if (!l_ptr) |
b97bf3fd PL |
396 | continue; |
397 | l_ptr->reset_checkpoint = l_ptr->next_in_no; | |
398 | l_ptr->exp_msg_count = 0; | |
4323add6 | 399 | tipc_link_reset_fragments(l_ptr); |
b97bf3fd PL |
400 | } |
401 | ||
ca9cf06a YX |
402 | n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN; |
403 | ||
9d561949 YX |
404 | /* Notify subscribers and prevent re-contact with node until |
405 | * cleanup is done. | |
406 | */ | |
ca9cf06a YX |
407 | n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN | |
408 | TIPC_NOTIFY_NODE_DOWN; | |
b97bf3fd PL |
409 | } |
410 | ||
4323add6 | 411 | struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
412 | { |
413 | u32 domain; | |
414 | struct sk_buff *buf; | |
6c00055a | 415 | struct tipc_node *n_ptr; |
c4307285 | 416 | struct tipc_node_info node_info; |
ea13847b | 417 | u32 payload_size; |
b97bf3fd PL |
418 | |
419 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) | |
4323add6 | 420 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 421 | |
3e6c8cd5 | 422 | domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
4323add6 PL |
423 | if (!tipc_addr_domain_valid(domain)) |
424 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE | |
425 | " (network address)"); | |
b97bf3fd | 426 | |
46651c59 | 427 | spin_lock_bh(&node_list_lock); |
672d99e1 | 428 | if (!tipc_num_nodes) { |
46651c59 | 429 | spin_unlock_bh(&node_list_lock); |
c4307285 | 430 | return tipc_cfg_reply_none(); |
1aad72d6 | 431 | } |
b97bf3fd | 432 | |
08c80e9a | 433 | /* For now, get space for all other nodes */ |
672d99e1 | 434 | payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes; |
1aad72d6 | 435 | if (payload_size > 32768u) { |
46651c59 | 436 | spin_unlock_bh(&node_list_lock); |
ea13847b AS |
437 | return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED |
438 | " (too many nodes)"); | |
1aad72d6 | 439 | } |
2220646a YX |
440 | spin_unlock_bh(&node_list_lock); |
441 | ||
ea13847b | 442 | buf = tipc_cfg_reply_alloc(payload_size); |
2220646a | 443 | if (!buf) |
b97bf3fd PL |
444 | return NULL; |
445 | ||
446 | /* Add TLVs for all nodes in scope */ | |
6c7a762e YX |
447 | rcu_read_lock(); |
448 | list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) { | |
672d99e1 | 449 | if (!tipc_in_scope(domain, n_ptr->addr)) |
b97bf3fd | 450 | continue; |
c4307285 YH |
451 | node_info.addr = htonl(n_ptr->addr); |
452 | node_info.up = htonl(tipc_node_is_up(n_ptr)); | |
453 | tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO, | |
4323add6 | 454 | &node_info, sizeof(node_info)); |
b97bf3fd | 455 | } |
6c7a762e | 456 | rcu_read_unlock(); |
b97bf3fd PL |
457 | return buf; |
458 | } | |
459 | ||
4323add6 | 460 | struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
461 | { |
462 | u32 domain; | |
463 | struct sk_buff *buf; | |
6c00055a | 464 | struct tipc_node *n_ptr; |
c4307285 | 465 | struct tipc_link_info link_info; |
ea13847b | 466 | u32 payload_size; |
b97bf3fd PL |
467 | |
468 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) | |
4323add6 | 469 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 470 | |
3e6c8cd5 | 471 | domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
4323add6 PL |
472 | if (!tipc_addr_domain_valid(domain)) |
473 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE | |
474 | " (network address)"); | |
b97bf3fd | 475 | |
b58343f9 | 476 | if (!tipc_own_addr) |
c4307285 YH |
477 | return tipc_cfg_reply_none(); |
478 | ||
46651c59 | 479 | spin_lock_bh(&node_list_lock); |
7a54d4a9 | 480 | /* Get space for all unicast links + broadcast link */ |
dde20266 | 481 | payload_size = TLV_SPACE((sizeof(link_info)) * (tipc_num_links + 1)); |
1aad72d6 | 482 | if (payload_size > 32768u) { |
46651c59 | 483 | spin_unlock_bh(&node_list_lock); |
ea13847b AS |
484 | return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED |
485 | " (too many links)"); | |
1aad72d6 | 486 | } |
dde20266 YX |
487 | spin_unlock_bh(&node_list_lock); |
488 | ||
ea13847b | 489 | buf = tipc_cfg_reply_alloc(payload_size); |
dde20266 | 490 | if (!buf) |
b97bf3fd PL |
491 | return NULL; |
492 | ||
493 | /* Add TLV for broadcast link */ | |
a3796f89 | 494 | link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr)); |
c4307285 | 495 | link_info.up = htonl(1); |
4b704d59 | 496 | strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME); |
4323add6 | 497 | tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info)); |
b97bf3fd PL |
498 | |
499 | /* Add TLVs for any other links in scope */ | |
6c7a762e YX |
500 | rcu_read_lock(); |
501 | list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) { | |
c4307285 | 502 | u32 i; |
b97bf3fd | 503 | |
672d99e1 | 504 | if (!tipc_in_scope(domain, n_ptr->addr)) |
b97bf3fd | 505 | continue; |
1aad72d6 | 506 | tipc_node_lock(n_ptr); |
c4307285 YH |
507 | for (i = 0; i < MAX_BEARERS; i++) { |
508 | if (!n_ptr->links[i]) | |
509 | continue; | |
510 | link_info.dest = htonl(n_ptr->addr); | |
511 | link_info.up = htonl(tipc_link_is_up(n_ptr->links[i])); | |
512 | strcpy(link_info.str, n_ptr->links[i]->name); | |
513 | tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, | |
4323add6 | 514 | &link_info, sizeof(link_info)); |
c4307285 | 515 | } |
1aad72d6 | 516 | tipc_node_unlock(n_ptr); |
b97bf3fd | 517 | } |
6c7a762e | 518 | rcu_read_unlock(); |
b97bf3fd PL |
519 | return buf; |
520 | } | |
78acb1f9 EH |
521 | |
522 | /** | |
523 | * tipc_node_get_linkname - get the name of a link | |
524 | * | |
525 | * @bearer_id: id of the bearer | |
526 | * @node: peer node address | |
527 | * @linkname: link name output buffer | |
528 | * | |
529 | * Returns 0 on success | |
530 | */ | |
531 | int tipc_node_get_linkname(u32 bearer_id, u32 addr, char *linkname, size_t len) | |
532 | { | |
533 | struct tipc_link *link; | |
534 | struct tipc_node *node = tipc_node_find(addr); | |
535 | ||
d7bb74c3 | 536 | if ((bearer_id >= MAX_BEARERS) || !node) |
78acb1f9 EH |
537 | return -EINVAL; |
538 | tipc_node_lock(node); | |
539 | link = node->links[bearer_id]; | |
540 | if (link) { | |
541 | strncpy(linkname, link->name, len); | |
542 | tipc_node_unlock(node); | |
543 | return 0; | |
544 | } | |
545 | tipc_node_unlock(node); | |
546 | return -EINVAL; | |
547 | } | |
9db9fdd1 YX |
548 | |
549 | void tipc_node_unlock(struct tipc_node *node) | |
550 | { | |
551 | LIST_HEAD(nsub_list); | |
02be61a9 | 552 | LIST_HEAD(conn_sks); |
50100a5e | 553 | struct sk_buff_head waiting_sks; |
ca0c4273 | 554 | u32 addr = 0; |
7b8613e0 YX |
555 | int flags = node->action_flags; |
556 | u32 link_id = 0; | |
9db9fdd1 | 557 | |
7b8613e0 | 558 | if (likely(!flags)) { |
9db9fdd1 YX |
559 | spin_unlock_bh(&node->lock); |
560 | return; | |
561 | } | |
562 | ||
7b8613e0 YX |
563 | addr = node->addr; |
564 | link_id = node->link_id; | |
50100a5e | 565 | __skb_queue_head_init(&waiting_sks); |
7b8613e0 YX |
566 | |
567 | if (flags & TIPC_WAKEUP_USERS) | |
50100a5e | 568 | skb_queue_splice_init(&node->waiting_sks, &waiting_sks); |
7b8613e0 YX |
569 | |
570 | if (flags & TIPC_NOTIFY_NODE_DOWN) { | |
9db9fdd1 | 571 | list_replace_init(&node->nsub, &nsub_list); |
02be61a9 | 572 | list_replace_init(&node->conn_sks, &conn_sks); |
9db9fdd1 | 573 | } |
7b8613e0 YX |
574 | node->action_flags &= ~(TIPC_WAKEUP_USERS | TIPC_NOTIFY_NODE_DOWN | |
575 | TIPC_NOTIFY_NODE_UP | TIPC_NOTIFY_LINK_UP | | |
576 | TIPC_NOTIFY_LINK_DOWN | | |
577 | TIPC_WAKEUP_BCAST_USERS); | |
578 | ||
9db9fdd1 YX |
579 | spin_unlock_bh(&node->lock); |
580 | ||
50100a5e JPM |
581 | while (!skb_queue_empty(&waiting_sks)) |
582 | tipc_sk_rcv(__skb_dequeue(&waiting_sks)); | |
583 | ||
02be61a9 JPM |
584 | if (!list_empty(&conn_sks)) |
585 | tipc_node_abort_sock_conns(&conn_sks); | |
586 | ||
9db9fdd1 YX |
587 | if (!list_empty(&nsub_list)) |
588 | tipc_nodesub_notify(&nsub_list); | |
50100a5e | 589 | |
908344cd JM |
590 | if (flags & TIPC_WAKEUP_BCAST_USERS) |
591 | tipc_bclink_wakeup_users(); | |
592 | ||
7b8613e0 | 593 | if (flags & TIPC_NOTIFY_NODE_UP) |
dbdf6d24 | 594 | tipc_named_node_up(addr); |
7b8613e0 YX |
595 | |
596 | if (flags & TIPC_NOTIFY_LINK_UP) | |
597 | tipc_nametbl_publish(TIPC_LINK_STATE, addr, addr, | |
598 | TIPC_NODE_SCOPE, link_id, addr); | |
599 | ||
600 | if (flags & TIPC_NOTIFY_LINK_DOWN) | |
601 | tipc_nametbl_withdraw(TIPC_LINK_STATE, addr, | |
602 | link_id, addr); | |
9db9fdd1 | 603 | } |