]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/tipc/node.c
tipc: Optimize tipc_node_has_active_links()
[mv-sheeva.git] / net / tipc / node.c
1 /*
2  * net/tipc/node.c: TIPC node management routines
3  *
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2005-2006, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "core.h"
38 #include "config.h"
39 #include "node.h"
40 #include "cluster.h"
41 #include "net.h"
42 #include "addr.h"
43 #include "node_subscr.h"
44 #include "link.h"
45 #include "port.h"
46 #include "bearer.h"
47 #include "name_distr.h"
48
49 void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
50 static void node_lost_contact(struct tipc_node *n_ptr);
51 static void node_established_contact(struct tipc_node *n_ptr);
52
53 struct tipc_node *tipc_nodes = NULL;    /* sorted list of nodes within cluster */
54
55 static DEFINE_SPINLOCK(node_create_lock);
56
57 u32 tipc_own_tag = 0;
58
59 /**
60  * tipc_node_create - create neighboring node
61  *
62  * Currently, this routine is called by neighbor discovery code, which holds
63  * net_lock for reading only.  We must take node_create_lock to ensure a node
64  * isn't created twice if two different bearers discover the node at the same
65  * time.  (It would be preferable to switch to holding net_lock in write mode,
66  * but this is a non-trivial change.)
67  */
68
69 struct tipc_node *tipc_node_create(u32 addr)
70 {
71         struct cluster *c_ptr;
72         struct tipc_node *n_ptr;
73         struct tipc_node **curr_node;
74
75         spin_lock_bh(&node_create_lock);
76
77         for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
78                 if (addr < n_ptr->addr)
79                         break;
80                 if (addr == n_ptr->addr) {
81                         spin_unlock_bh(&node_create_lock);
82                         return n_ptr;
83                 }
84         }
85
86         n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
87         if (!n_ptr) {
88                 spin_unlock_bh(&node_create_lock);
89                 warn("Node creation failed, no memory\n");
90                 return NULL;
91         }
92
93         c_ptr = tipc_cltr_find(addr);
94         if (!c_ptr) {
95                 c_ptr = tipc_cltr_create(addr);
96         }
97         if (!c_ptr) {
98                 spin_unlock_bh(&node_create_lock);
99                 kfree(n_ptr);
100                 return NULL;
101         }
102
103         n_ptr->addr = addr;
104                 spin_lock_init(&n_ptr->lock);
105         INIT_LIST_HEAD(&n_ptr->nsub);
106         n_ptr->owner = c_ptr;
107         tipc_cltr_attach_node(c_ptr, n_ptr);
108         n_ptr->last_router = -1;
109
110         /* Insert node into ordered list */
111         for (curr_node = &tipc_nodes; *curr_node;
112              curr_node = &(*curr_node)->next) {
113                 if (addr < (*curr_node)->addr) {
114                         n_ptr->next = *curr_node;
115                         break;
116                 }
117         }
118         (*curr_node) = n_ptr;
119         spin_unlock_bh(&node_create_lock);
120         return n_ptr;
121 }
122
123 void tipc_node_delete(struct tipc_node *n_ptr)
124 {
125         if (!n_ptr)
126                 return;
127
128 #if 0
129         /* Not needed because links are already deleted via tipc_bearer_stop() */
130
131         u32 l_num;
132
133         for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
134                 link_delete(n_ptr->links[l_num]);
135         }
136 #endif
137
138         dbg("node %x deleted\n", n_ptr->addr);
139         kfree(n_ptr);
140 }
141
142
143 /**
144  * tipc_node_link_up - handle addition of link
145  *
146  * Link becomes active (alone or shared) or standby, depending on its priority.
147  */
148
149 void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
150 {
151         struct link **active = &n_ptr->active_links[0];
152
153         n_ptr->working_links++;
154
155         info("Established link <%s> on network plane %c\n",
156              l_ptr->name, l_ptr->b_ptr->net_plane);
157
158         if (!active[0]) {
159                 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
160                 active[0] = active[1] = l_ptr;
161                 node_established_contact(n_ptr);
162                 return;
163         }
164         if (l_ptr->priority < active[0]->priority) {
165                 info("New link <%s> becomes standby\n", l_ptr->name);
166                 return;
167         }
168         tipc_link_send_duplicate(active[0], l_ptr);
169         if (l_ptr->priority == active[0]->priority) {
170                 active[0] = l_ptr;
171                 return;
172         }
173         info("Old link <%s> becomes standby\n", active[0]->name);
174         if (active[1] != active[0])
175                 info("Old link <%s> becomes standby\n", active[1]->name);
176         active[0] = active[1] = l_ptr;
177 }
178
179 /**
180  * node_select_active_links - select active link
181  */
182
183 static void node_select_active_links(struct tipc_node *n_ptr)
184 {
185         struct link **active = &n_ptr->active_links[0];
186         u32 i;
187         u32 highest_prio = 0;
188
189         active[0] = active[1] = NULL;
190
191         for (i = 0; i < MAX_BEARERS; i++) {
192                 struct link *l_ptr = n_ptr->links[i];
193
194                 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
195                     (l_ptr->priority < highest_prio))
196                         continue;
197
198                 if (l_ptr->priority > highest_prio) {
199                         highest_prio = l_ptr->priority;
200                         active[0] = active[1] = l_ptr;
201                 } else {
202                         active[1] = l_ptr;
203                 }
204         }
205 }
206
207 /**
208  * tipc_node_link_down - handle loss of link
209  */
210
211 void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
212 {
213         struct link **active;
214
215         n_ptr->working_links--;
216
217         if (!tipc_link_is_active(l_ptr)) {
218                 info("Lost standby link <%s> on network plane %c\n",
219                      l_ptr->name, l_ptr->b_ptr->net_plane);
220                 return;
221         }
222         info("Lost link <%s> on network plane %c\n",
223                 l_ptr->name, l_ptr->b_ptr->net_plane);
224
225         active = &n_ptr->active_links[0];
226         if (active[0] == l_ptr)
227                 active[0] = active[1];
228         if (active[1] == l_ptr)
229                 active[1] = active[0];
230         if (active[0] == l_ptr)
231                 node_select_active_links(n_ptr);
232         if (tipc_node_is_up(n_ptr))
233                 tipc_link_changeover(l_ptr);
234         else
235                 node_lost_contact(n_ptr);
236 }
237
238 int tipc_node_has_active_links(struct tipc_node *n_ptr)
239 {
240         return n_ptr->active_links[0] != NULL;
241 }
242
243 int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
244 {
245         return (n_ptr->working_links > 1);
246 }
247
248 static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
249 {
250         return (n_ptr && (n_ptr->last_router >= 0));
251 }
252
253 int tipc_node_is_up(struct tipc_node *n_ptr)
254 {
255         return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
256 }
257
258 struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
259 {
260         struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
261
262         if (!n_ptr)
263                 n_ptr = tipc_node_create(l_ptr->addr);
264         if (n_ptr) {
265                 u32 bearer_id = l_ptr->b_ptr->identity;
266                 char addr_string[16];
267
268                 if (n_ptr->link_cnt >= 2) {
269                         err("Attempt to create third link to %s\n",
270                             tipc_addr_string_fill(addr_string, n_ptr->addr));
271                         return NULL;
272                 }
273
274                 if (!n_ptr->links[bearer_id]) {
275                         n_ptr->links[bearer_id] = l_ptr;
276                         tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
277                         n_ptr->link_cnt++;
278                         return n_ptr;
279                 }
280                 err("Attempt to establish second link on <%s> to %s\n",
281                     l_ptr->b_ptr->publ.name,
282                     tipc_addr_string_fill(addr_string, l_ptr->addr));
283         }
284         return NULL;
285 }
286
287 void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
288 {
289         n_ptr->links[l_ptr->b_ptr->identity] = NULL;
290         tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
291         n_ptr->link_cnt--;
292 }
293
294 /*
295  * Routing table management - five cases to handle:
296  *
297  * 1: A link towards a zone/cluster external node comes up.
298  *    => Send a multicast message updating routing tables of all
299  *    system nodes within own cluster that the new destination
300  *    can be reached via this node.
301  *    (node.establishedContact()=>cluster.multicastNewRoute())
302  *
303  * 2: A link towards a slave node comes up.
304  *    => Send a multicast message updating routing tables of all
305  *    system nodes within own cluster that the new destination
306  *    can be reached via this node.
307  *    (node.establishedContact()=>cluster.multicastNewRoute())
308  *    => Send a  message to the slave node about existence
309  *    of all system nodes within cluster:
310  *    (node.establishedContact()=>cluster.sendLocalRoutes())
311  *
312  * 3: A new cluster local system node becomes available.
313  *    => Send message(s) to this particular node containing
314  *    information about all cluster external and slave
315  *     nodes which can be reached via this node.
316  *    (node.establishedContact()==>network.sendExternalRoutes())
317  *    (node.establishedContact()==>network.sendSlaveRoutes())
318  *    => Send messages to all directly connected slave nodes
319  *    containing information about the existence of the new node
320  *    (node.establishedContact()=>cluster.multicastNewRoute())
321  *
322  * 4: The link towards a zone/cluster external node or slave
323  *    node goes down.
324  *    => Send a multcast message updating routing tables of all
325  *    nodes within cluster that the new destination can not any
326  *    longer be reached via this node.
327  *    (node.lostAllLinks()=>cluster.bcastLostRoute())
328  *
329  * 5: A cluster local system node becomes unavailable.
330  *    => Remove all references to this node from the local
331  *    routing tables. Note: This is a completely node
332  *    local operation.
333  *    (node.lostAllLinks()=>network.removeAsRouter())
334  *    => Send messages to all directly connected slave nodes
335  *    containing information about loss of the node
336  *    (node.establishedContact()=>cluster.multicastLostRoute())
337  *
338  */
339
340 static void node_established_contact(struct tipc_node *n_ptr)
341 {
342         struct cluster *c_ptr;
343
344         dbg("node_established_contact:-> %x\n", n_ptr->addr);
345         if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
346                 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
347         }
348
349         /* Syncronize broadcast acks */
350         n_ptr->bclink.acked = tipc_bclink_get_last_sent();
351
352         if (is_slave(tipc_own_addr))
353                 return;
354         if (!in_own_cluster(n_ptr->addr)) {
355                 /* Usage case 1 (see above) */
356                 c_ptr = tipc_cltr_find(tipc_own_addr);
357                 if (!c_ptr)
358                         c_ptr = tipc_cltr_create(tipc_own_addr);
359                 if (c_ptr)
360                         tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
361                                                   tipc_max_nodes);
362                 return;
363         }
364
365         c_ptr = n_ptr->owner;
366         if (is_slave(n_ptr->addr)) {
367                 /* Usage case 2 (see above) */
368                 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
369                 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
370                 return;
371         }
372
373         if (n_ptr->bclink.supported) {
374                 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
375                 if (n_ptr->addr < tipc_own_addr)
376                         tipc_own_tag++;
377         }
378
379         /* Case 3 (see above) */
380         tipc_net_send_external_routes(n_ptr->addr);
381         tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
382         tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
383                                   tipc_highest_allowed_slave);
384 }
385
386 static void node_lost_contact(struct tipc_node *n_ptr)
387 {
388         struct cluster *c_ptr;
389         struct tipc_node_subscr *ns, *tns;
390         char addr_string[16];
391         u32 i;
392
393         /* Clean up broadcast reception remains */
394         n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
395         while (n_ptr->bclink.deferred_head) {
396                 struct sk_buff* buf = n_ptr->bclink.deferred_head;
397                 n_ptr->bclink.deferred_head = buf->next;
398                 buf_discard(buf);
399         }
400         if (n_ptr->bclink.defragm) {
401                 buf_discard(n_ptr->bclink.defragm);
402                 n_ptr->bclink.defragm = NULL;
403         }
404         if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
405                 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
406         }
407
408         /* Update routing tables */
409         if (is_slave(tipc_own_addr)) {
410                 tipc_net_remove_as_router(n_ptr->addr);
411         } else {
412                 if (!in_own_cluster(n_ptr->addr)) {
413                         /* Case 4 (see above) */
414                         c_ptr = tipc_cltr_find(tipc_own_addr);
415                         tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
416                                                    tipc_max_nodes);
417                 } else {
418                         /* Case 5 (see above) */
419                         c_ptr = tipc_cltr_find(n_ptr->addr);
420                         if (is_slave(n_ptr->addr)) {
421                                 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
422                                                            tipc_max_nodes);
423                         } else {
424                                 if (n_ptr->bclink.supported) {
425                                         tipc_nmap_remove(&tipc_cltr_bcast_nodes,
426                                                          n_ptr->addr);
427                                         if (n_ptr->addr < tipc_own_addr)
428                                                 tipc_own_tag--;
429                                 }
430                                 tipc_net_remove_as_router(n_ptr->addr);
431                                 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
432                                                            LOWEST_SLAVE,
433                                                            tipc_highest_allowed_slave);
434                         }
435                 }
436         }
437         if (tipc_node_has_active_routes(n_ptr))
438                 return;
439
440         info("Lost contact with %s\n",
441              tipc_addr_string_fill(addr_string, n_ptr->addr));
442
443         /* Abort link changeover */
444         for (i = 0; i < MAX_BEARERS; i++) {
445                 struct link *l_ptr = n_ptr->links[i];
446                 if (!l_ptr)
447                         continue;
448                 l_ptr->reset_checkpoint = l_ptr->next_in_no;
449                 l_ptr->exp_msg_count = 0;
450                 tipc_link_reset_fragments(l_ptr);
451         }
452
453         /* Notify subscribers */
454         list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
455                 ns->node = NULL;
456                 list_del_init(&ns->nodesub_list);
457                 tipc_k_signal((Handler)ns->handle_node_down,
458                               (unsigned long)ns->usr_handle);
459         }
460 }
461
462 /**
463  * tipc_node_select_next_hop - find the next-hop node for a message
464  *
465  * Called by when cluster local lookup has failed.
466  */
467
468 struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
469 {
470         struct tipc_node *n_ptr;
471         u32 router_addr;
472
473         if (!tipc_addr_domain_valid(addr))
474                 return NULL;
475
476         /* Look for direct link to destination processsor */
477         n_ptr = tipc_node_find(addr);
478         if (n_ptr && tipc_node_has_active_links(n_ptr))
479                 return n_ptr;
480
481         /* Cluster local system nodes *must* have direct links */
482         if (!is_slave(addr) && in_own_cluster(addr))
483                 return NULL;
484
485         /* Look for cluster local router with direct link to node */
486         router_addr = tipc_node_select_router(n_ptr, selector);
487         if (router_addr)
488                 return tipc_node_select(router_addr, selector);
489
490         /* Slave nodes can only be accessed within own cluster via a
491            known router with direct link -- if no router was found,give up */
492         if (is_slave(addr))
493                 return NULL;
494
495         /* Inter zone/cluster -- find any direct link to remote cluster */
496         addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
497         n_ptr = tipc_net_select_remote_node(addr, selector);
498         if (n_ptr && tipc_node_has_active_links(n_ptr))
499                 return n_ptr;
500
501         /* Last resort -- look for any router to anywhere in remote zone */
502         router_addr =  tipc_net_select_router(addr, selector);
503         if (router_addr)
504                 return tipc_node_select(router_addr, selector);
505
506         return NULL;
507 }
508
509 /**
510  * tipc_node_select_router - select router to reach specified node
511  *
512  * Uses a deterministic and fair algorithm for selecting router node.
513  */
514
515 u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
516 {
517         u32 ulim;
518         u32 mask;
519         u32 start;
520         u32 r;
521
522         if (!n_ptr)
523                 return 0;
524
525         if (n_ptr->last_router < 0)
526                 return 0;
527         ulim = ((n_ptr->last_router + 1) * 32) - 1;
528
529         /* Start entry must be random */
530         mask = tipc_max_nodes;
531         while (mask > ulim)
532                 mask >>= 1;
533         start = ref & mask;
534         r = start;
535
536         /* Lookup upwards with wrap-around */
537         do {
538                 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
539                         break;
540         } while (++r <= ulim);
541         if (r > ulim) {
542                 r = 1;
543                 do {
544                         if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
545                                 break;
546                 } while (++r < start);
547                 assert(r != start);
548         }
549         assert(r && (r <= ulim));
550         return tipc_addr(own_zone(), own_cluster(), r);
551 }
552
553 void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
554 {
555         u32 r_num = tipc_node(router);
556
557         n_ptr->routers[r_num / 32] =
558                 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
559         n_ptr->last_router = tipc_max_nodes / 32;
560         while ((--n_ptr->last_router >= 0) &&
561                !n_ptr->routers[n_ptr->last_router]);
562 }
563
564 void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
565 {
566         u32 r_num = tipc_node(router);
567
568         if (n_ptr->last_router < 0)
569                 return;         /* No routes */
570
571         n_ptr->routers[r_num / 32] =
572                 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
573         n_ptr->last_router = tipc_max_nodes / 32;
574         while ((--n_ptr->last_router >= 0) &&
575                !n_ptr->routers[n_ptr->last_router]);
576
577         if (!tipc_node_is_up(n_ptr))
578                 node_lost_contact(n_ptr);
579 }
580
581 #if 0
582 void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str)
583 {
584         u32 i;
585
586         tipc_printf(buf, "\n\n%s", str);
587         for (i = 0; i < MAX_BEARERS; i++) {
588                 if (!n_ptr->links[i])
589                         continue;
590                 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
591         }
592         tipc_printf(buf, "Active links: [%x,%x]\n",
593                     n_ptr->active_links[0], n_ptr->active_links[1]);
594 }
595 #endif
596
597 u32 tipc_available_nodes(const u32 domain)
598 {
599         struct tipc_node *n_ptr;
600         u32 cnt = 0;
601
602         read_lock_bh(&tipc_net_lock);
603         for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
604                 if (!tipc_in_scope(domain, n_ptr->addr))
605                         continue;
606                 if (tipc_node_is_up(n_ptr))
607                         cnt++;
608         }
609         read_unlock_bh(&tipc_net_lock);
610         return cnt;
611 }
612
613 struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
614 {
615         u32 domain;
616         struct sk_buff *buf;
617         struct tipc_node *n_ptr;
618         struct tipc_node_info node_info;
619         u32 payload_size;
620
621         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
622                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
623
624         domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
625         if (!tipc_addr_domain_valid(domain))
626                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
627                                                    " (network address)");
628
629         read_lock_bh(&tipc_net_lock);
630         if (!tipc_nodes) {
631                 read_unlock_bh(&tipc_net_lock);
632                 return tipc_cfg_reply_none();
633         }
634
635         /* For now, get space for all other nodes
636            (will need to modify this when slave nodes are supported */
637
638         payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
639         if (payload_size > 32768u) {
640                 read_unlock_bh(&tipc_net_lock);
641                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
642                                                    " (too many nodes)");
643         }
644         buf = tipc_cfg_reply_alloc(payload_size);
645         if (!buf) {
646                 read_unlock_bh(&tipc_net_lock);
647                 return NULL;
648         }
649
650         /* Add TLVs for all nodes in scope */
651
652         for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
653                 if (!tipc_in_scope(domain, n_ptr->addr))
654                         continue;
655                 node_info.addr = htonl(n_ptr->addr);
656                 node_info.up = htonl(tipc_node_is_up(n_ptr));
657                 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
658                                     &node_info, sizeof(node_info));
659         }
660
661         read_unlock_bh(&tipc_net_lock);
662         return buf;
663 }
664
665 struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
666 {
667         u32 domain;
668         struct sk_buff *buf;
669         struct tipc_node *n_ptr;
670         struct tipc_link_info link_info;
671         u32 payload_size;
672
673         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
674                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
675
676         domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
677         if (!tipc_addr_domain_valid(domain))
678                 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
679                                                    " (network address)");
680
681         if (tipc_mode != TIPC_NET_MODE)
682                 return tipc_cfg_reply_none();
683
684         read_lock_bh(&tipc_net_lock);
685
686         /* Get space for all unicast links + multicast link */
687
688         payload_size = TLV_SPACE(sizeof(link_info)) *
689                 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
690         if (payload_size > 32768u) {
691                 read_unlock_bh(&tipc_net_lock);
692                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
693                                                    " (too many links)");
694         }
695         buf = tipc_cfg_reply_alloc(payload_size);
696         if (!buf) {
697                 read_unlock_bh(&tipc_net_lock);
698                 return NULL;
699         }
700
701         /* Add TLV for broadcast link */
702
703         link_info.dest = htonl(tipc_own_addr & 0xfffff00);
704         link_info.up = htonl(1);
705         strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
706         tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
707
708         /* Add TLVs for any other links in scope */
709
710         for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
711                 u32 i;
712
713                 if (!tipc_in_scope(domain, n_ptr->addr))
714                         continue;
715                 tipc_node_lock(n_ptr);
716                 for (i = 0; i < MAX_BEARERS; i++) {
717                         if (!n_ptr->links[i])
718                                 continue;
719                         link_info.dest = htonl(n_ptr->addr);
720                         link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
721                         strcpy(link_info.str, n_ptr->links[i]->name);
722                         tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
723                                             &link_info, sizeof(link_info));
724                 }
725                 tipc_node_unlock(n_ptr);
726         }
727
728         read_unlock_bh(&tipc_net_lock);
729         return buf;
730 }