]>
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 | |
6c00055a DM |
43 | static void node_lost_contact(struct tipc_node *n_ptr); |
44 | static void node_established_contact(struct tipc_node *n_ptr); | |
b97bf3fd | 45 | |
02be61a9 JPM |
46 | struct tipc_sock_conn { |
47 | u32 port; | |
48 | u32 peer_port; | |
49 | u32 peer_node; | |
50 | struct list_head list; | |
51 | }; | |
52 | ||
3e4b6ab5 RA |
53 | static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = { |
54 | [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC }, | |
55 | [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 }, | |
56 | [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG } | |
57 | }; | |
58 | ||
a635b46b AS |
59 | /* |
60 | * A trivial power-of-two bitmask technique is used for speed, since this | |
61 | * operation is done for every incoming TIPC packet. The number of hash table | |
62 | * entries has been chosen so that no hash chain exceeds 8 nodes and will | |
63 | * usually be much smaller (typically only a single node). | |
64 | */ | |
872f24db | 65 | static unsigned int tipc_hashfn(u32 addr) |
a635b46b AS |
66 | { |
67 | return addr & (NODE_HTABLE_SIZE - 1); | |
68 | } | |
69 | ||
1ec2bb08 | 70 | /* |
672d99e1 AS |
71 | * tipc_node_find - locate specified node object, if it exists |
72 | */ | |
f2f9800d | 73 | struct tipc_node *tipc_node_find(struct net *net, u32 addr) |
672d99e1 | 74 | { |
f2f9800d | 75 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
672d99e1 | 76 | struct tipc_node *node; |
672d99e1 | 77 | |
336ebf5b | 78 | if (unlikely(!in_own_cluster_exact(addr))) |
672d99e1 AS |
79 | return NULL; |
80 | ||
6c7a762e | 81 | rcu_read_lock(); |
f2f9800d YX |
82 | hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)], |
83 | 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 | ||
f2f9800d | 93 | struct tipc_node *tipc_node_create(struct net *net, u32 addr) |
b97bf3fd | 94 | { |
f2f9800d | 95 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
672d99e1 | 96 | struct tipc_node *n_ptr, *temp_node; |
b97bf3fd | 97 | |
f2f9800d | 98 | spin_lock_bh(&tn->node_list_lock); |
2ecb0924 | 99 | |
5af54792 | 100 | n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC); |
a10bd924 | 101 | if (!n_ptr) { |
f2f9800d | 102 | spin_unlock_bh(&tn->node_list_lock); |
2cf8aa19 | 103 | pr_warn("Node creation failed, no memory\n"); |
a10bd924 AS |
104 | return NULL; |
105 | } | |
106 | ||
a10bd924 | 107 | n_ptr->addr = addr; |
f2f9800d | 108 | n_ptr->net = net; |
51a8e4de | 109 | spin_lock_init(&n_ptr->lock); |
672d99e1 AS |
110 | INIT_HLIST_NODE(&n_ptr->hash); |
111 | INIT_LIST_HEAD(&n_ptr->list); | |
a8f48af5 | 112 | INIT_LIST_HEAD(&n_ptr->publ_list); |
02be61a9 | 113 | INIT_LIST_HEAD(&n_ptr->conn_sks); |
340b6e59 | 114 | skb_queue_head_init(&n_ptr->waiting_sks); |
bc6fecd4 | 115 | __skb_queue_head_init(&n_ptr->bclink.deferred_queue); |
8f92df6a | 116 | |
f2f9800d | 117 | hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]); |
672d99e1 | 118 | |
f2f9800d | 119 | list_for_each_entry_rcu(temp_node, &tn->node_list, list) { |
672d99e1 AS |
120 | if (n_ptr->addr < temp_node->addr) |
121 | break; | |
122 | } | |
6c7a762e | 123 | list_add_tail_rcu(&n_ptr->list, &temp_node->list); |
aecb9bb8 | 124 | n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN; |
fc0eea69 | 125 | n_ptr->signature = INVALID_NODE_SIG; |
672d99e1 | 126 | |
f2f9800d | 127 | tn->num_nodes++; |
a10bd924 | 128 | |
f2f9800d | 129 | spin_unlock_bh(&tn->node_list_lock); |
b97bf3fd PL |
130 | return n_ptr; |
131 | } | |
132 | ||
f2f9800d | 133 | static void tipc_node_delete(struct tipc_net *tn, struct tipc_node *n_ptr) |
b97bf3fd | 134 | { |
6c7a762e YX |
135 | list_del_rcu(&n_ptr->list); |
136 | hlist_del_rcu(&n_ptr->hash); | |
137 | kfree_rcu(n_ptr, rcu); | |
8f92df6a | 138 | |
f2f9800d | 139 | tn->num_nodes--; |
b97bf3fd PL |
140 | } |
141 | ||
f2f9800d | 142 | void tipc_node_stop(struct net *net) |
46651c59 | 143 | { |
f2f9800d | 144 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
46651c59 YX |
145 | struct tipc_node *node, *t_node; |
146 | ||
f2f9800d YX |
147 | spin_lock_bh(&tn->node_list_lock); |
148 | list_for_each_entry_safe(node, t_node, &tn->node_list, list) | |
149 | tipc_node_delete(tn, node); | |
150 | spin_unlock_bh(&tn->node_list_lock); | |
46651c59 YX |
151 | } |
152 | ||
f2f9800d | 153 | int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port) |
02be61a9 JPM |
154 | { |
155 | struct tipc_node *node; | |
156 | struct tipc_sock_conn *conn; | |
157 | ||
158 | if (in_own_node(dnode)) | |
159 | return 0; | |
160 | ||
f2f9800d | 161 | node = tipc_node_find(net, dnode); |
02be61a9 JPM |
162 | if (!node) { |
163 | pr_warn("Connecting sock to node 0x%x failed\n", dnode); | |
164 | return -EHOSTUNREACH; | |
165 | } | |
166 | conn = kmalloc(sizeof(*conn), GFP_ATOMIC); | |
167 | if (!conn) | |
168 | return -EHOSTUNREACH; | |
169 | conn->peer_node = dnode; | |
170 | conn->port = port; | |
171 | conn->peer_port = peer_port; | |
172 | ||
173 | tipc_node_lock(node); | |
174 | list_add_tail(&conn->list, &node->conn_sks); | |
175 | tipc_node_unlock(node); | |
176 | return 0; | |
177 | } | |
178 | ||
f2f9800d | 179 | void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port) |
02be61a9 JPM |
180 | { |
181 | struct tipc_node *node; | |
182 | struct tipc_sock_conn *conn, *safe; | |
183 | ||
184 | if (in_own_node(dnode)) | |
185 | return; | |
186 | ||
f2f9800d | 187 | node = tipc_node_find(net, dnode); |
02be61a9 JPM |
188 | if (!node) |
189 | return; | |
190 | ||
191 | tipc_node_lock(node); | |
192 | list_for_each_entry_safe(conn, safe, &node->conn_sks, list) { | |
193 | if (port != conn->port) | |
194 | continue; | |
195 | list_del(&conn->list); | |
196 | kfree(conn); | |
197 | } | |
198 | tipc_node_unlock(node); | |
199 | } | |
200 | ||
f2f9800d | 201 | void tipc_node_abort_sock_conns(struct net *net, struct list_head *conns) |
02be61a9 JPM |
202 | { |
203 | struct tipc_sock_conn *conn, *safe; | |
204 | struct sk_buff *buf; | |
205 | ||
206 | list_for_each_entry_safe(conn, safe, conns, list) { | |
207 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, | |
208 | SHORT_H_SIZE, 0, tipc_own_addr, | |
209 | conn->peer_node, conn->port, | |
210 | conn->peer_port, TIPC_ERR_NO_NODE); | |
211 | if (likely(buf)) | |
f2f9800d | 212 | tipc_sk_rcv(net, buf); |
02be61a9 JPM |
213 | list_del(&conn->list); |
214 | kfree(conn); | |
215 | } | |
216 | } | |
217 | ||
b97bf3fd | 218 | /** |
4323add6 | 219 | * tipc_node_link_up - handle addition of link |
c4307285 | 220 | * |
b97bf3fd PL |
221 | * Link becomes active (alone or shared) or standby, depending on its priority. |
222 | */ | |
a18c4bc3 | 223 | void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 224 | { |
a18c4bc3 | 225 | struct tipc_link **active = &n_ptr->active_links[0]; |
b97bf3fd | 226 | |
5392d646 | 227 | n_ptr->working_links++; |
7b8613e0 YX |
228 | n_ptr->action_flags |= TIPC_NOTIFY_LINK_UP; |
229 | n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id; | |
230 | ||
2cf8aa19 | 231 | pr_info("Established link <%s> on network plane %c\n", |
7a2f7d18 | 232 | l_ptr->name, l_ptr->net_plane); |
c4307285 | 233 | |
b97bf3fd | 234 | if (!active[0]) { |
b97bf3fd PL |
235 | active[0] = active[1] = l_ptr; |
236 | node_established_contact(n_ptr); | |
16e166b8 | 237 | goto exit; |
b97bf3fd | 238 | } |
c4307285 | 239 | if (l_ptr->priority < active[0]->priority) { |
2cf8aa19 | 240 | pr_info("New link <%s> becomes standby\n", l_ptr->name); |
16e166b8 | 241 | goto exit; |
b97bf3fd | 242 | } |
247f0f3c | 243 | tipc_link_dup_queue_xmit(active[0], l_ptr); |
c4307285 | 244 | if (l_ptr->priority == active[0]->priority) { |
b97bf3fd | 245 | active[0] = l_ptr; |
16e166b8 | 246 | goto exit; |
b97bf3fd | 247 | } |
2cf8aa19 | 248 | pr_info("Old link <%s> becomes standby\n", active[0]->name); |
a10bd924 | 249 | if (active[1] != active[0]) |
2cf8aa19 | 250 | pr_info("Old link <%s> becomes standby\n", active[1]->name); |
b97bf3fd | 251 | active[0] = active[1] = l_ptr; |
16e166b8 JPM |
252 | exit: |
253 | /* Leave room for changeover header when returning 'mtu' to users: */ | |
254 | n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; | |
255 | n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; | |
b97bf3fd PL |
256 | } |
257 | ||
258 | /** | |
259 | * node_select_active_links - select active link | |
260 | */ | |
6c00055a | 261 | static void node_select_active_links(struct tipc_node *n_ptr) |
b97bf3fd | 262 | { |
a18c4bc3 | 263 | struct tipc_link **active = &n_ptr->active_links[0]; |
b97bf3fd PL |
264 | u32 i; |
265 | u32 highest_prio = 0; | |
266 | ||
c4307285 | 267 | active[0] = active[1] = NULL; |
b97bf3fd PL |
268 | |
269 | for (i = 0; i < MAX_BEARERS; i++) { | |
a18c4bc3 | 270 | struct tipc_link *l_ptr = n_ptr->links[i]; |
b97bf3fd | 271 | |
4323add6 | 272 | if (!l_ptr || !tipc_link_is_up(l_ptr) || |
b97bf3fd PL |
273 | (l_ptr->priority < highest_prio)) |
274 | continue; | |
275 | ||
276 | if (l_ptr->priority > highest_prio) { | |
c4307285 | 277 | highest_prio = l_ptr->priority; |
b97bf3fd PL |
278 | active[0] = active[1] = l_ptr; |
279 | } else { | |
280 | active[1] = l_ptr; | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
4323add6 | 286 | * tipc_node_link_down - handle loss of link |
b97bf3fd | 287 | */ |
a18c4bc3 | 288 | void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 289 | { |
a18c4bc3 | 290 | struct tipc_link **active; |
b97bf3fd | 291 | |
5392d646 | 292 | n_ptr->working_links--; |
7b8613e0 YX |
293 | n_ptr->action_flags |= TIPC_NOTIFY_LINK_DOWN; |
294 | n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id; | |
5392d646 | 295 | |
4323add6 | 296 | if (!tipc_link_is_active(l_ptr)) { |
2cf8aa19 | 297 | pr_info("Lost standby link <%s> on network plane %c\n", |
7a2f7d18 | 298 | l_ptr->name, l_ptr->net_plane); |
b97bf3fd PL |
299 | return; |
300 | } | |
2cf8aa19 | 301 | pr_info("Lost link <%s> on network plane %c\n", |
7a2f7d18 | 302 | l_ptr->name, l_ptr->net_plane); |
b97bf3fd PL |
303 | |
304 | active = &n_ptr->active_links[0]; | |
305 | if (active[0] == l_ptr) | |
306 | active[0] = active[1]; | |
307 | if (active[1] == l_ptr) | |
308 | active[1] = active[0]; | |
309 | if (active[0] == l_ptr) | |
310 | node_select_active_links(n_ptr); | |
c4307285 | 311 | if (tipc_node_is_up(n_ptr)) |
170b3927 | 312 | tipc_link_failover_send_queue(l_ptr); |
c4307285 | 313 | else |
b97bf3fd | 314 | node_lost_contact(n_ptr); |
16e166b8 JPM |
315 | |
316 | /* Leave room for changeover header when returning 'mtu' to users: */ | |
317 | if (active[0]) { | |
318 | n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; | |
319 | n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; | |
320 | return; | |
321 | } | |
322 | ||
323 | /* Loopback link went down? No fragmentation needed from now on. */ | |
324 | if (n_ptr->addr == tipc_own_addr) { | |
325 | n_ptr->act_mtus[0] = MAX_MSG_SIZE; | |
326 | n_ptr->act_mtus[1] = MAX_MSG_SIZE; | |
327 | } | |
b97bf3fd PL |
328 | } |
329 | ||
8f19afb2 | 330 | int tipc_node_active_links(struct tipc_node *n_ptr) |
b97bf3fd | 331 | { |
76ae0d71 | 332 | return n_ptr->active_links[0] != NULL; |
b97bf3fd PL |
333 | } |
334 | ||
6c00055a | 335 | int tipc_node_is_up(struct tipc_node *n_ptr) |
b97bf3fd | 336 | { |
8f19afb2 | 337 | return tipc_node_active_links(n_ptr); |
b97bf3fd PL |
338 | } |
339 | ||
a18c4bc3 | 340 | void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 341 | { |
f2f9800d YX |
342 | struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id); |
343 | ||
7a2f7d18 | 344 | n_ptr->links[l_ptr->bearer_id] = l_ptr; |
f2f9800d YX |
345 | spin_lock_bh(&tn->node_list_lock); |
346 | tn->num_links++; | |
347 | spin_unlock_bh(&tn->node_list_lock); | |
37b9c08a | 348 | n_ptr->link_cnt++; |
b97bf3fd PL |
349 | } |
350 | ||
a18c4bc3 | 351 | void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 352 | { |
f2f9800d | 353 | struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id); |
7d33939f JPM |
354 | int i; |
355 | ||
356 | for (i = 0; i < MAX_BEARERS; i++) { | |
074bb43e JPM |
357 | if (l_ptr != n_ptr->links[i]) |
358 | continue; | |
359 | n_ptr->links[i] = NULL; | |
f2f9800d YX |
360 | spin_lock_bh(&tn->node_list_lock); |
361 | tn->num_links--; | |
362 | spin_unlock_bh(&tn->node_list_lock); | |
074bb43e | 363 | n_ptr->link_cnt--; |
7d33939f | 364 | } |
b97bf3fd PL |
365 | } |
366 | ||
6c00055a | 367 | static void node_established_contact(struct tipc_node *n_ptr) |
b97bf3fd | 368 | { |
aecb9bb8 | 369 | n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP; |
c64f7a6a | 370 | n_ptr->bclink.oos_state = 0; |
1da46568 YX |
371 | n_ptr->bclink.acked = tipc_bclink_get_last_sent(n_ptr->net); |
372 | tipc_bclink_add_node(n_ptr->net, n_ptr->addr); | |
b97bf3fd PL |
373 | } |
374 | ||
6c00055a | 375 | static void node_lost_contact(struct tipc_node *n_ptr) |
b97bf3fd | 376 | { |
b97bf3fd PL |
377 | char addr_string[16]; |
378 | u32 i; | |
379 | ||
2cf8aa19 EH |
380 | pr_info("Lost contact with %s\n", |
381 | tipc_addr_string_fill(addr_string, n_ptr->addr)); | |
c5bd4d85 AS |
382 | |
383 | /* Flush broadcast link info associated with lost node */ | |
389dd9bc | 384 | if (n_ptr->bclink.recv_permitted) { |
bc6fecd4 | 385 | __skb_queue_purge(&n_ptr->bclink.deferred_queue); |
c5bd4d85 | 386 | |
37e22164 JPM |
387 | if (n_ptr->bclink.reasm_buf) { |
388 | kfree_skb(n_ptr->bclink.reasm_buf); | |
389 | n_ptr->bclink.reasm_buf = NULL; | |
c5bd4d85 AS |
390 | } |
391 | ||
1da46568 | 392 | tipc_bclink_remove_node(n_ptr->net, n_ptr->addr); |
36559591 | 393 | tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ); |
b97bf3fd | 394 | |
389dd9bc | 395 | n_ptr->bclink.recv_permitted = false; |
c5bd4d85 | 396 | } |
b97bf3fd PL |
397 | |
398 | /* Abort link changeover */ | |
399 | for (i = 0; i < MAX_BEARERS; i++) { | |
a18c4bc3 | 400 | struct tipc_link *l_ptr = n_ptr->links[i]; |
c4307285 | 401 | if (!l_ptr) |
b97bf3fd PL |
402 | continue; |
403 | l_ptr->reset_checkpoint = l_ptr->next_in_no; | |
404 | l_ptr->exp_msg_count = 0; | |
4323add6 | 405 | tipc_link_reset_fragments(l_ptr); |
b97bf3fd PL |
406 | } |
407 | ||
ca9cf06a YX |
408 | n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN; |
409 | ||
9d561949 YX |
410 | /* Notify subscribers and prevent re-contact with node until |
411 | * cleanup is done. | |
412 | */ | |
ca9cf06a YX |
413 | n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN | |
414 | TIPC_NOTIFY_NODE_DOWN; | |
b97bf3fd PL |
415 | } |
416 | ||
f2f9800d YX |
417 | struct sk_buff *tipc_node_get_nodes(struct net *net, const void *req_tlv_area, |
418 | int req_tlv_space) | |
b97bf3fd | 419 | { |
f2f9800d | 420 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
b97bf3fd PL |
421 | u32 domain; |
422 | struct sk_buff *buf; | |
6c00055a | 423 | struct tipc_node *n_ptr; |
c4307285 | 424 | struct tipc_node_info node_info; |
ea13847b | 425 | u32 payload_size; |
b97bf3fd PL |
426 | |
427 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) | |
4323add6 | 428 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 429 | |
3e6c8cd5 | 430 | domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
4323add6 PL |
431 | if (!tipc_addr_domain_valid(domain)) |
432 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE | |
433 | " (network address)"); | |
b97bf3fd | 434 | |
f2f9800d YX |
435 | spin_lock_bh(&tn->node_list_lock); |
436 | if (!tn->num_nodes) { | |
437 | spin_unlock_bh(&tn->node_list_lock); | |
c4307285 | 438 | return tipc_cfg_reply_none(); |
1aad72d6 | 439 | } |
b97bf3fd | 440 | |
08c80e9a | 441 | /* For now, get space for all other nodes */ |
f2f9800d | 442 | payload_size = TLV_SPACE(sizeof(node_info)) * tn->num_nodes; |
1aad72d6 | 443 | if (payload_size > 32768u) { |
f2f9800d | 444 | spin_unlock_bh(&tn->node_list_lock); |
ea13847b AS |
445 | return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED |
446 | " (too many nodes)"); | |
1aad72d6 | 447 | } |
f2f9800d | 448 | spin_unlock_bh(&tn->node_list_lock); |
2220646a | 449 | |
ea13847b | 450 | buf = tipc_cfg_reply_alloc(payload_size); |
2220646a | 451 | if (!buf) |
b97bf3fd PL |
452 | return NULL; |
453 | ||
454 | /* Add TLVs for all nodes in scope */ | |
6c7a762e | 455 | rcu_read_lock(); |
f2f9800d | 456 | list_for_each_entry_rcu(n_ptr, &tn->node_list, list) { |
672d99e1 | 457 | if (!tipc_in_scope(domain, n_ptr->addr)) |
b97bf3fd | 458 | continue; |
c4307285 YH |
459 | node_info.addr = htonl(n_ptr->addr); |
460 | node_info.up = htonl(tipc_node_is_up(n_ptr)); | |
461 | tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO, | |
4323add6 | 462 | &node_info, sizeof(node_info)); |
b97bf3fd | 463 | } |
6c7a762e | 464 | rcu_read_unlock(); |
b97bf3fd PL |
465 | return buf; |
466 | } | |
467 | ||
f2f9800d YX |
468 | struct sk_buff *tipc_node_get_links(struct net *net, const void *req_tlv_area, |
469 | int req_tlv_space) | |
b97bf3fd | 470 | { |
f2f9800d | 471 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
b97bf3fd PL |
472 | u32 domain; |
473 | struct sk_buff *buf; | |
6c00055a | 474 | struct tipc_node *n_ptr; |
c4307285 | 475 | struct tipc_link_info link_info; |
ea13847b | 476 | u32 payload_size; |
b97bf3fd PL |
477 | |
478 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) | |
4323add6 | 479 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 480 | |
3e6c8cd5 | 481 | domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
4323add6 PL |
482 | if (!tipc_addr_domain_valid(domain)) |
483 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE | |
484 | " (network address)"); | |
b97bf3fd | 485 | |
b58343f9 | 486 | if (!tipc_own_addr) |
c4307285 YH |
487 | return tipc_cfg_reply_none(); |
488 | ||
f2f9800d | 489 | spin_lock_bh(&tn->node_list_lock); |
7a54d4a9 | 490 | /* Get space for all unicast links + broadcast link */ |
f2f9800d | 491 | payload_size = TLV_SPACE((sizeof(link_info)) * (tn->num_links + 1)); |
1aad72d6 | 492 | if (payload_size > 32768u) { |
f2f9800d | 493 | spin_unlock_bh(&tn->node_list_lock); |
ea13847b AS |
494 | return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED |
495 | " (too many links)"); | |
1aad72d6 | 496 | } |
f2f9800d | 497 | spin_unlock_bh(&tn->node_list_lock); |
dde20266 | 498 | |
ea13847b | 499 | buf = tipc_cfg_reply_alloc(payload_size); |
dde20266 | 500 | if (!buf) |
b97bf3fd PL |
501 | return NULL; |
502 | ||
503 | /* Add TLV for broadcast link */ | |
a3796f89 | 504 | link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr)); |
c4307285 | 505 | link_info.up = htonl(1); |
4b704d59 | 506 | strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME); |
4323add6 | 507 | tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info)); |
b97bf3fd PL |
508 | |
509 | /* Add TLVs for any other links in scope */ | |
6c7a762e | 510 | rcu_read_lock(); |
f2f9800d | 511 | list_for_each_entry_rcu(n_ptr, &tn->node_list, list) { |
c4307285 | 512 | u32 i; |
b97bf3fd | 513 | |
672d99e1 | 514 | if (!tipc_in_scope(domain, n_ptr->addr)) |
b97bf3fd | 515 | continue; |
1aad72d6 | 516 | tipc_node_lock(n_ptr); |
c4307285 YH |
517 | for (i = 0; i < MAX_BEARERS; i++) { |
518 | if (!n_ptr->links[i]) | |
519 | continue; | |
520 | link_info.dest = htonl(n_ptr->addr); | |
521 | link_info.up = htonl(tipc_link_is_up(n_ptr->links[i])); | |
522 | strcpy(link_info.str, n_ptr->links[i]->name); | |
523 | tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, | |
4323add6 | 524 | &link_info, sizeof(link_info)); |
c4307285 | 525 | } |
1aad72d6 | 526 | tipc_node_unlock(n_ptr); |
b97bf3fd | 527 | } |
6c7a762e | 528 | rcu_read_unlock(); |
b97bf3fd PL |
529 | return buf; |
530 | } | |
78acb1f9 EH |
531 | |
532 | /** | |
533 | * tipc_node_get_linkname - get the name of a link | |
534 | * | |
535 | * @bearer_id: id of the bearer | |
536 | * @node: peer node address | |
537 | * @linkname: link name output buffer | |
538 | * | |
539 | * Returns 0 on success | |
540 | */ | |
f2f9800d YX |
541 | int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr, |
542 | char *linkname, size_t len) | |
78acb1f9 EH |
543 | { |
544 | struct tipc_link *link; | |
f2f9800d | 545 | struct tipc_node *node = tipc_node_find(net, addr); |
78acb1f9 | 546 | |
d7bb74c3 | 547 | if ((bearer_id >= MAX_BEARERS) || !node) |
78acb1f9 EH |
548 | return -EINVAL; |
549 | tipc_node_lock(node); | |
550 | link = node->links[bearer_id]; | |
551 | if (link) { | |
552 | strncpy(linkname, link->name, len); | |
553 | tipc_node_unlock(node); | |
554 | return 0; | |
555 | } | |
556 | tipc_node_unlock(node); | |
557 | return -EINVAL; | |
558 | } | |
9db9fdd1 YX |
559 | |
560 | void tipc_node_unlock(struct tipc_node *node) | |
561 | { | |
f2f9800d | 562 | struct net *net = node->net; |
9db9fdd1 | 563 | LIST_HEAD(nsub_list); |
02be61a9 | 564 | LIST_HEAD(conn_sks); |
50100a5e | 565 | struct sk_buff_head waiting_sks; |
ca0c4273 | 566 | u32 addr = 0; |
7b8613e0 YX |
567 | int flags = node->action_flags; |
568 | u32 link_id = 0; | |
9db9fdd1 | 569 | |
7b8613e0 | 570 | if (likely(!flags)) { |
9db9fdd1 YX |
571 | spin_unlock_bh(&node->lock); |
572 | return; | |
573 | } | |
574 | ||
7b8613e0 YX |
575 | addr = node->addr; |
576 | link_id = node->link_id; | |
50100a5e | 577 | __skb_queue_head_init(&waiting_sks); |
7b8613e0 YX |
578 | |
579 | if (flags & TIPC_WAKEUP_USERS) | |
50100a5e | 580 | skb_queue_splice_init(&node->waiting_sks, &waiting_sks); |
7b8613e0 YX |
581 | |
582 | if (flags & TIPC_NOTIFY_NODE_DOWN) { | |
a8f48af5 | 583 | list_replace_init(&node->publ_list, &nsub_list); |
02be61a9 | 584 | list_replace_init(&node->conn_sks, &conn_sks); |
9db9fdd1 | 585 | } |
7b8613e0 YX |
586 | node->action_flags &= ~(TIPC_WAKEUP_USERS | TIPC_NOTIFY_NODE_DOWN | |
587 | TIPC_NOTIFY_NODE_UP | TIPC_NOTIFY_LINK_UP | | |
588 | TIPC_NOTIFY_LINK_DOWN | | |
589 | TIPC_WAKEUP_BCAST_USERS); | |
590 | ||
9db9fdd1 YX |
591 | spin_unlock_bh(&node->lock); |
592 | ||
50100a5e | 593 | while (!skb_queue_empty(&waiting_sks)) |
f2f9800d | 594 | tipc_sk_rcv(net, __skb_dequeue(&waiting_sks)); |
50100a5e | 595 | |
02be61a9 | 596 | if (!list_empty(&conn_sks)) |
f2f9800d | 597 | tipc_node_abort_sock_conns(net, &conn_sks); |
02be61a9 | 598 | |
9db9fdd1 | 599 | if (!list_empty(&nsub_list)) |
f2f9800d | 600 | tipc_publ_notify(net, &nsub_list, addr); |
50100a5e | 601 | |
908344cd | 602 | if (flags & TIPC_WAKEUP_BCAST_USERS) |
f2f9800d | 603 | tipc_bclink_wakeup_users(net); |
908344cd | 604 | |
7b8613e0 | 605 | if (flags & TIPC_NOTIFY_NODE_UP) |
f2f9800d | 606 | tipc_named_node_up(net, addr); |
7b8613e0 YX |
607 | |
608 | if (flags & TIPC_NOTIFY_LINK_UP) | |
f2f9800d | 609 | tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr, |
7b8613e0 YX |
610 | TIPC_NODE_SCOPE, link_id, addr); |
611 | ||
612 | if (flags & TIPC_NOTIFY_LINK_DOWN) | |
f2f9800d | 613 | tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr, |
7b8613e0 | 614 | link_id, addr); |
9db9fdd1 | 615 | } |
3e4b6ab5 RA |
616 | |
617 | /* Caller should hold node lock for the passed node */ | |
d8182804 | 618 | static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node) |
3e4b6ab5 RA |
619 | { |
620 | void *hdr; | |
621 | struct nlattr *attrs; | |
622 | ||
623 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family, | |
624 | NLM_F_MULTI, TIPC_NL_NODE_GET); | |
625 | if (!hdr) | |
626 | return -EMSGSIZE; | |
627 | ||
628 | attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE); | |
629 | if (!attrs) | |
630 | goto msg_full; | |
631 | ||
632 | if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr)) | |
633 | goto attr_msg_full; | |
634 | if (tipc_node_is_up(node)) | |
635 | if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP)) | |
636 | goto attr_msg_full; | |
637 | ||
638 | nla_nest_end(msg->skb, attrs); | |
639 | genlmsg_end(msg->skb, hdr); | |
640 | ||
641 | return 0; | |
642 | ||
643 | attr_msg_full: | |
644 | nla_nest_cancel(msg->skb, attrs); | |
645 | msg_full: | |
646 | genlmsg_cancel(msg->skb, hdr); | |
647 | ||
648 | return -EMSGSIZE; | |
649 | } | |
650 | ||
651 | int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
652 | { | |
653 | int err; | |
f2f9800d YX |
654 | struct net *net = sock_net(skb->sk); |
655 | struct tipc_net *tn = net_generic(net, tipc_net_id); | |
3e4b6ab5 RA |
656 | int done = cb->args[0]; |
657 | int last_addr = cb->args[1]; | |
658 | struct tipc_node *node; | |
659 | struct tipc_nl_msg msg; | |
660 | ||
661 | if (done) | |
662 | return 0; | |
663 | ||
664 | msg.skb = skb; | |
665 | msg.portid = NETLINK_CB(cb->skb).portid; | |
666 | msg.seq = cb->nlh->nlmsg_seq; | |
667 | ||
668 | rcu_read_lock(); | |
669 | ||
f2f9800d | 670 | if (last_addr && !tipc_node_find(net, last_addr)) { |
3e4b6ab5 RA |
671 | rcu_read_unlock(); |
672 | /* We never set seq or call nl_dump_check_consistent() this | |
673 | * means that setting prev_seq here will cause the consistence | |
674 | * check to fail in the netlink callback handler. Resulting in | |
675 | * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if | |
676 | * the node state changed while we released the lock. | |
677 | */ | |
678 | cb->prev_seq = 1; | |
679 | return -EPIPE; | |
680 | } | |
681 | ||
f2f9800d | 682 | list_for_each_entry_rcu(node, &tn->node_list, list) { |
3e4b6ab5 RA |
683 | if (last_addr) { |
684 | if (node->addr == last_addr) | |
685 | last_addr = 0; | |
686 | else | |
687 | continue; | |
688 | } | |
689 | ||
690 | tipc_node_lock(node); | |
691 | err = __tipc_nl_add_node(&msg, node); | |
692 | if (err) { | |
693 | last_addr = node->addr; | |
694 | tipc_node_unlock(node); | |
695 | goto out; | |
696 | } | |
697 | ||
698 | tipc_node_unlock(node); | |
699 | } | |
700 | done = 1; | |
701 | out: | |
702 | cb->args[0] = done; | |
703 | cb->args[1] = last_addr; | |
704 | rcu_read_unlock(); | |
705 | ||
706 | return skb->len; | |
707 | } |