4fd6e2887818fb1bd26b633d04acaf9705147cf2
[linux-2.6-block.git] / net / tipc / node.c
1 /*
2  * net/tipc/node.c: TIPC node management routines
3  *
4  * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
5  * Copyright (c) 2005-2006, 2010-2014, 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 "link.h"
39 #include "node.h"
40 #include "name_distr.h"
41 #include "socket.h"
42 #include "bcast.h"
43 #include "monitor.h"
44 #include "discover.h"
45 #include "netlink.h"
46 #include "trace.h"
47
48 #define INVALID_NODE_SIG        0x10000
49 #define NODE_CLEANUP_AFTER      300000
50
51 /* Flags used to take different actions according to flag type
52  * TIPC_NOTIFY_NODE_DOWN: notify node is down
53  * TIPC_NOTIFY_NODE_UP: notify node is up
54  * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
55  */
56 enum {
57         TIPC_NOTIFY_NODE_DOWN           = (1 << 3),
58         TIPC_NOTIFY_NODE_UP             = (1 << 4),
59         TIPC_NOTIFY_LINK_UP             = (1 << 6),
60         TIPC_NOTIFY_LINK_DOWN           = (1 << 7)
61 };
62
63 struct tipc_link_entry {
64         struct tipc_link *link;
65         spinlock_t lock; /* per link */
66         u32 mtu;
67         struct sk_buff_head inputq;
68         struct tipc_media_addr maddr;
69 };
70
71 struct tipc_bclink_entry {
72         struct tipc_link *link;
73         struct sk_buff_head inputq1;
74         struct sk_buff_head arrvq;
75         struct sk_buff_head inputq2;
76         struct sk_buff_head namedq;
77 };
78
79 /**
80  * struct tipc_node - TIPC node structure
81  * @addr: network address of node
82  * @ref: reference counter to node object
83  * @lock: rwlock governing access to structure
84  * @net: the applicable net namespace
85  * @hash: links to adjacent nodes in unsorted hash chain
86  * @inputq: pointer to input queue containing messages for msg event
87  * @namedq: pointer to name table input queue with name table messages
88  * @active_links: bearer ids of active links, used as index into links[] array
89  * @links: array containing references to all links to node
90  * @action_flags: bit mask of different types of node actions
91  * @state: connectivity state vs peer node
92  * @sync_point: sequence number where synch/failover is finished
93  * @list: links to adjacent nodes in sorted list of cluster's nodes
94  * @working_links: number of working links to node (both active and standby)
95  * @link_cnt: number of links to node
96  * @capabilities: bitmap, indicating peer node's functional capabilities
97  * @signature: node instance identifier
98  * @link_id: local and remote bearer ids of changing link, if any
99  * @publ_list: list of publications
100  * @rcu: rcu struct for tipc_node
101  * @delete_at: indicates the time for deleting a down node
102  */
103 struct tipc_node {
104         u32 addr;
105         struct kref kref;
106         rwlock_t lock;
107         struct net *net;
108         struct hlist_node hash;
109         int active_links[2];
110         struct tipc_link_entry links[MAX_BEARERS];
111         struct tipc_bclink_entry bc_entry;
112         int action_flags;
113         struct list_head list;
114         int state;
115         bool failover_sent;
116         u16 sync_point;
117         int link_cnt;
118         u16 working_links;
119         u16 capabilities;
120         u32 signature;
121         u32 link_id;
122         u8 peer_id[16];
123         struct list_head publ_list;
124         struct list_head conn_sks;
125         unsigned long keepalive_intv;
126         struct timer_list timer;
127         struct rcu_head rcu;
128         unsigned long delete_at;
129 };
130
131 /* Node FSM states and events:
132  */
133 enum {
134         SELF_DOWN_PEER_DOWN    = 0xdd,
135         SELF_UP_PEER_UP        = 0xaa,
136         SELF_DOWN_PEER_LEAVING = 0xd1,
137         SELF_UP_PEER_COMING    = 0xac,
138         SELF_COMING_PEER_UP    = 0xca,
139         SELF_LEAVING_PEER_DOWN = 0x1d,
140         NODE_FAILINGOVER       = 0xf0,
141         NODE_SYNCHING          = 0xcc
142 };
143
144 enum {
145         SELF_ESTABL_CONTACT_EVT = 0xece,
146         SELF_LOST_CONTACT_EVT   = 0x1ce,
147         PEER_ESTABL_CONTACT_EVT = 0x9ece,
148         PEER_LOST_CONTACT_EVT   = 0x91ce,
149         NODE_FAILOVER_BEGIN_EVT = 0xfbe,
150         NODE_FAILOVER_END_EVT   = 0xfee,
151         NODE_SYNCH_BEGIN_EVT    = 0xcbe,
152         NODE_SYNCH_END_EVT      = 0xcee
153 };
154
155 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
156                                   struct sk_buff_head *xmitq,
157                                   struct tipc_media_addr **maddr);
158 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
159                                 bool delete);
160 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
161 static void tipc_node_delete(struct tipc_node *node);
162 static void tipc_node_timeout(struct timer_list *t);
163 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
164 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
165 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
166 static void tipc_node_put(struct tipc_node *node);
167 static bool node_is_up(struct tipc_node *n);
168 static void tipc_node_delete_from_list(struct tipc_node *node);
169
170 struct tipc_sock_conn {
171         u32 port;
172         u32 peer_port;
173         u32 peer_node;
174         struct list_head list;
175 };
176
177 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
178 {
179         int bearer_id = n->active_links[sel & 1];
180
181         if (unlikely(bearer_id == INVALID_BEARER_ID))
182                 return NULL;
183
184         return n->links[bearer_id].link;
185 }
186
187 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
188 {
189         struct tipc_node *n;
190         int bearer_id;
191         unsigned int mtu = MAX_MSG_SIZE;
192
193         n = tipc_node_find(net, addr);
194         if (unlikely(!n))
195                 return mtu;
196
197         bearer_id = n->active_links[sel & 1];
198         if (likely(bearer_id != INVALID_BEARER_ID))
199                 mtu = n->links[bearer_id].mtu;
200         tipc_node_put(n);
201         return mtu;
202 }
203
204 bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
205 {
206         u8 *own_id = tipc_own_id(net);
207         struct tipc_node *n;
208
209         if (!own_id)
210                 return true;
211
212         if (addr == tipc_own_addr(net)) {
213                 memcpy(id, own_id, TIPC_NODEID_LEN);
214                 return true;
215         }
216         n = tipc_node_find(net, addr);
217         if (!n)
218                 return false;
219
220         memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
221         tipc_node_put(n);
222         return true;
223 }
224
225 u16 tipc_node_get_capabilities(struct net *net, u32 addr)
226 {
227         struct tipc_node *n;
228         u16 caps;
229
230         n = tipc_node_find(net, addr);
231         if (unlikely(!n))
232                 return TIPC_NODE_CAPABILITIES;
233         caps = n->capabilities;
234         tipc_node_put(n);
235         return caps;
236 }
237
238 static void tipc_node_kref_release(struct kref *kref)
239 {
240         struct tipc_node *n = container_of(kref, struct tipc_node, kref);
241
242         kfree(n->bc_entry.link);
243         kfree_rcu(n, rcu);
244 }
245
246 static void tipc_node_put(struct tipc_node *node)
247 {
248         kref_put(&node->kref, tipc_node_kref_release);
249 }
250
251 static void tipc_node_get(struct tipc_node *node)
252 {
253         kref_get(&node->kref);
254 }
255
256 /*
257  * tipc_node_find - locate specified node object, if it exists
258  */
259 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
260 {
261         struct tipc_net *tn = tipc_net(net);
262         struct tipc_node *node;
263         unsigned int thash = tipc_hashfn(addr);
264
265         rcu_read_lock();
266         hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
267                 if (node->addr != addr)
268                         continue;
269                 if (!kref_get_unless_zero(&node->kref))
270                         node = NULL;
271                 break;
272         }
273         rcu_read_unlock();
274         return node;
275 }
276
277 /* tipc_node_find_by_id - locate specified node object by its 128-bit id
278  * Note: this function is called only when a discovery request failed
279  * to find the node by its 32-bit id, and is not time critical
280  */
281 static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
282 {
283         struct tipc_net *tn = tipc_net(net);
284         struct tipc_node *n;
285         bool found = false;
286
287         rcu_read_lock();
288         list_for_each_entry_rcu(n, &tn->node_list, list) {
289                 read_lock_bh(&n->lock);
290                 if (!memcmp(id, n->peer_id, 16) &&
291                     kref_get_unless_zero(&n->kref))
292                         found = true;
293                 read_unlock_bh(&n->lock);
294                 if (found)
295                         break;
296         }
297         rcu_read_unlock();
298         return found ? n : NULL;
299 }
300
301 static void tipc_node_read_lock(struct tipc_node *n)
302 {
303         read_lock_bh(&n->lock);
304 }
305
306 static void tipc_node_read_unlock(struct tipc_node *n)
307 {
308         read_unlock_bh(&n->lock);
309 }
310
311 static void tipc_node_write_lock(struct tipc_node *n)
312 {
313         write_lock_bh(&n->lock);
314 }
315
316 static void tipc_node_write_unlock_fast(struct tipc_node *n)
317 {
318         write_unlock_bh(&n->lock);
319 }
320
321 static void tipc_node_write_unlock(struct tipc_node *n)
322 {
323         struct net *net = n->net;
324         u32 addr = 0;
325         u32 flags = n->action_flags;
326         u32 link_id = 0;
327         u32 bearer_id;
328         struct list_head *publ_list;
329
330         if (likely(!flags)) {
331                 write_unlock_bh(&n->lock);
332                 return;
333         }
334
335         addr = n->addr;
336         link_id = n->link_id;
337         bearer_id = link_id & 0xffff;
338         publ_list = &n->publ_list;
339
340         n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
341                              TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
342
343         write_unlock_bh(&n->lock);
344
345         if (flags & TIPC_NOTIFY_NODE_DOWN)
346                 tipc_publ_notify(net, publ_list, addr);
347
348         if (flags & TIPC_NOTIFY_NODE_UP)
349                 tipc_named_node_up(net, addr);
350
351         if (flags & TIPC_NOTIFY_LINK_UP) {
352                 tipc_mon_peer_up(net, addr, bearer_id);
353                 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
354                                      TIPC_NODE_SCOPE, link_id, link_id);
355         }
356         if (flags & TIPC_NOTIFY_LINK_DOWN) {
357                 tipc_mon_peer_down(net, addr, bearer_id);
358                 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
359                                       addr, link_id);
360         }
361 }
362
363 static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
364                                           u8 *peer_id, u16 capabilities)
365 {
366         struct tipc_net *tn = net_generic(net, tipc_net_id);
367         struct tipc_node *n, *temp_node;
368         struct tipc_link *l;
369         int bearer_id;
370         int i;
371
372         spin_lock_bh(&tn->node_list_lock);
373         n = tipc_node_find(net, addr);
374         if (n) {
375                 if (n->capabilities == capabilities)
376                         goto exit;
377                 /* Same node may come back with new capabilities */
378                 write_lock_bh(&n->lock);
379                 n->capabilities = capabilities;
380                 for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
381                         l = n->links[bearer_id].link;
382                         if (l)
383                                 tipc_link_update_caps(l, capabilities);
384                 }
385                 write_unlock_bh(&n->lock);
386                 goto exit;
387         }
388         n = kzalloc(sizeof(*n), GFP_ATOMIC);
389         if (!n) {
390                 pr_warn("Node creation failed, no memory\n");
391                 goto exit;
392         }
393         n->addr = addr;
394         memcpy(&n->peer_id, peer_id, 16);
395         n->net = net;
396         n->capabilities = capabilities;
397         kref_init(&n->kref);
398         rwlock_init(&n->lock);
399         INIT_HLIST_NODE(&n->hash);
400         INIT_LIST_HEAD(&n->list);
401         INIT_LIST_HEAD(&n->publ_list);
402         INIT_LIST_HEAD(&n->conn_sks);
403         skb_queue_head_init(&n->bc_entry.namedq);
404         skb_queue_head_init(&n->bc_entry.inputq1);
405         __skb_queue_head_init(&n->bc_entry.arrvq);
406         skb_queue_head_init(&n->bc_entry.inputq2);
407         for (i = 0; i < MAX_BEARERS; i++)
408                 spin_lock_init(&n->links[i].lock);
409         n->state = SELF_DOWN_PEER_LEAVING;
410         n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
411         n->signature = INVALID_NODE_SIG;
412         n->active_links[0] = INVALID_BEARER_ID;
413         n->active_links[1] = INVALID_BEARER_ID;
414         if (!tipc_link_bc_create(net, tipc_own_addr(net),
415                                  addr, U16_MAX,
416                                  tipc_link_window(tipc_bc_sndlink(net)),
417                                  n->capabilities,
418                                  &n->bc_entry.inputq1,
419                                  &n->bc_entry.namedq,
420                                  tipc_bc_sndlink(net),
421                                  &n->bc_entry.link)) {
422                 pr_warn("Broadcast rcv link creation failed, no memory\n");
423                 kfree(n);
424                 n = NULL;
425                 goto exit;
426         }
427         tipc_node_get(n);
428         timer_setup(&n->timer, tipc_node_timeout, 0);
429         n->keepalive_intv = U32_MAX;
430         hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
431         list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
432                 if (n->addr < temp_node->addr)
433                         break;
434         }
435         list_add_tail_rcu(&n->list, &temp_node->list);
436 exit:
437         spin_unlock_bh(&tn->node_list_lock);
438         return n;
439 }
440
441 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
442 {
443         unsigned long tol = tipc_link_tolerance(l);
444         unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
445
446         /* Link with lowest tolerance determines timer interval */
447         if (intv < n->keepalive_intv)
448                 n->keepalive_intv = intv;
449
450         /* Ensure link's abort limit corresponds to current tolerance */
451         tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
452 }
453
454 static void tipc_node_delete_from_list(struct tipc_node *node)
455 {
456         list_del_rcu(&node->list);
457         hlist_del_rcu(&node->hash);
458         tipc_node_put(node);
459 }
460
461 static void tipc_node_delete(struct tipc_node *node)
462 {
463         tipc_node_delete_from_list(node);
464
465         del_timer_sync(&node->timer);
466         tipc_node_put(node);
467 }
468
469 void tipc_node_stop(struct net *net)
470 {
471         struct tipc_net *tn = tipc_net(net);
472         struct tipc_node *node, *t_node;
473
474         spin_lock_bh(&tn->node_list_lock);
475         list_for_each_entry_safe(node, t_node, &tn->node_list, list)
476                 tipc_node_delete(node);
477         spin_unlock_bh(&tn->node_list_lock);
478 }
479
480 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
481 {
482         struct tipc_node *n;
483
484         if (in_own_node(net, addr))
485                 return;
486
487         n = tipc_node_find(net, addr);
488         if (!n) {
489                 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
490                 return;
491         }
492         tipc_node_write_lock(n);
493         list_add_tail(subscr, &n->publ_list);
494         tipc_node_write_unlock_fast(n);
495         tipc_node_put(n);
496 }
497
498 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
499 {
500         struct tipc_node *n;
501
502         if (in_own_node(net, addr))
503                 return;
504
505         n = tipc_node_find(net, addr);
506         if (!n) {
507                 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
508                 return;
509         }
510         tipc_node_write_lock(n);
511         list_del_init(subscr);
512         tipc_node_write_unlock_fast(n);
513         tipc_node_put(n);
514 }
515
516 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
517 {
518         struct tipc_node *node;
519         struct tipc_sock_conn *conn;
520         int err = 0;
521
522         if (in_own_node(net, dnode))
523                 return 0;
524
525         node = tipc_node_find(net, dnode);
526         if (!node) {
527                 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
528                 return -EHOSTUNREACH;
529         }
530         conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
531         if (!conn) {
532                 err = -EHOSTUNREACH;
533                 goto exit;
534         }
535         conn->peer_node = dnode;
536         conn->port = port;
537         conn->peer_port = peer_port;
538
539         tipc_node_write_lock(node);
540         list_add_tail(&conn->list, &node->conn_sks);
541         tipc_node_write_unlock(node);
542 exit:
543         tipc_node_put(node);
544         return err;
545 }
546
547 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
548 {
549         struct tipc_node *node;
550         struct tipc_sock_conn *conn, *safe;
551
552         if (in_own_node(net, dnode))
553                 return;
554
555         node = tipc_node_find(net, dnode);
556         if (!node)
557                 return;
558
559         tipc_node_write_lock(node);
560         list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
561                 if (port != conn->port)
562                         continue;
563                 list_del(&conn->list);
564                 kfree(conn);
565         }
566         tipc_node_write_unlock(node);
567         tipc_node_put(node);
568 }
569
570 static void  tipc_node_clear_links(struct tipc_node *node)
571 {
572         int i;
573
574         for (i = 0; i < MAX_BEARERS; i++) {
575                 struct tipc_link_entry *le = &node->links[i];
576
577                 if (le->link) {
578                         kfree(le->link);
579                         le->link = NULL;
580                         node->link_cnt--;
581                 }
582         }
583 }
584
585 /* tipc_node_cleanup - delete nodes that does not
586  * have active links for NODE_CLEANUP_AFTER time
587  */
588 static bool tipc_node_cleanup(struct tipc_node *peer)
589 {
590         struct tipc_net *tn = tipc_net(peer->net);
591         bool deleted = false;
592
593         /* If lock held by tipc_node_stop() the node will be deleted anyway */
594         if (!spin_trylock_bh(&tn->node_list_lock))
595                 return false;
596
597         tipc_node_write_lock(peer);
598
599         if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
600                 tipc_node_clear_links(peer);
601                 tipc_node_delete_from_list(peer);
602                 deleted = true;
603         }
604         tipc_node_write_unlock(peer);
605         spin_unlock_bh(&tn->node_list_lock);
606         return deleted;
607 }
608
609 /* tipc_node_timeout - handle expiration of node timer
610  */
611 static void tipc_node_timeout(struct timer_list *t)
612 {
613         struct tipc_node *n = from_timer(n, t, timer);
614         struct tipc_link_entry *le;
615         struct sk_buff_head xmitq;
616         int remains = n->link_cnt;
617         int bearer_id;
618         int rc = 0;
619
620         if (!node_is_up(n) && tipc_node_cleanup(n)) {
621                 /*Removing the reference of Timer*/
622                 tipc_node_put(n);
623                 return;
624         }
625
626         __skb_queue_head_init(&xmitq);
627
628         /* Initial node interval to value larger (10 seconds), then it will be
629          * recalculated with link lowest tolerance
630          */
631         tipc_node_read_lock(n);
632         n->keepalive_intv = 10000;
633         tipc_node_read_unlock(n);
634         for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
635                 tipc_node_read_lock(n);
636                 le = &n->links[bearer_id];
637                 if (le->link) {
638                         spin_lock_bh(&le->lock);
639                         /* Link tolerance may change asynchronously: */
640                         tipc_node_calculate_timer(n, le->link);
641                         rc = tipc_link_timeout(le->link, &xmitq);
642                         spin_unlock_bh(&le->lock);
643                         remains--;
644                 }
645                 tipc_node_read_unlock(n);
646                 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
647                 if (rc & TIPC_LINK_DOWN_EVT)
648                         tipc_node_link_down(n, bearer_id, false);
649         }
650         mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
651 }
652
653 /**
654  * __tipc_node_link_up - handle addition of link
655  * Node lock must be held by caller
656  * Link becomes active (alone or shared) or standby, depending on its priority.
657  */
658 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
659                                 struct sk_buff_head *xmitq)
660 {
661         int *slot0 = &n->active_links[0];
662         int *slot1 = &n->active_links[1];
663         struct tipc_link *ol = node_active_link(n, 0);
664         struct tipc_link *nl = n->links[bearer_id].link;
665
666         if (!nl || tipc_link_is_up(nl))
667                 return;
668
669         tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
670         if (!tipc_link_is_up(nl))
671                 return;
672
673         n->working_links++;
674         n->action_flags |= TIPC_NOTIFY_LINK_UP;
675         n->link_id = tipc_link_id(nl);
676
677         /* Leave room for tunnel header when returning 'mtu' to users: */
678         n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
679
680         tipc_bearer_add_dest(n->net, bearer_id, n->addr);
681         tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
682
683         pr_debug("Established link <%s> on network plane %c\n",
684                  tipc_link_name(nl), tipc_link_plane(nl));
685
686         /* Ensure that a STATE message goes first */
687         tipc_link_build_state_msg(nl, xmitq);
688
689         /* First link? => give it both slots */
690         if (!ol) {
691                 *slot0 = bearer_id;
692                 *slot1 = bearer_id;
693                 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
694                 n->failover_sent = false;
695                 n->action_flags |= TIPC_NOTIFY_NODE_UP;
696                 tipc_link_set_active(nl, true);
697                 tipc_bcast_add_peer(n->net, nl, xmitq);
698                 return;
699         }
700
701         /* Second link => redistribute slots */
702         if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
703                 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
704                 *slot0 = bearer_id;
705                 *slot1 = bearer_id;
706                 tipc_link_set_active(nl, true);
707                 tipc_link_set_active(ol, false);
708         } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
709                 tipc_link_set_active(nl, true);
710                 *slot1 = bearer_id;
711         } else {
712                 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
713         }
714
715         /* Prepare synchronization with first link */
716         tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
717 }
718
719 /**
720  * tipc_node_link_up - handle addition of link
721  *
722  * Link becomes active (alone or shared) or standby, depending on its priority.
723  */
724 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
725                               struct sk_buff_head *xmitq)
726 {
727         struct tipc_media_addr *maddr;
728
729         tipc_node_write_lock(n);
730         __tipc_node_link_up(n, bearer_id, xmitq);
731         maddr = &n->links[bearer_id].maddr;
732         tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
733         tipc_node_write_unlock(n);
734 }
735
736 /**
737  * __tipc_node_link_down - handle loss of link
738  */
739 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
740                                   struct sk_buff_head *xmitq,
741                                   struct tipc_media_addr **maddr)
742 {
743         struct tipc_link_entry *le = &n->links[*bearer_id];
744         int *slot0 = &n->active_links[0];
745         int *slot1 = &n->active_links[1];
746         int i, highest = 0, prio;
747         struct tipc_link *l, *_l, *tnl;
748
749         l = n->links[*bearer_id].link;
750         if (!l || tipc_link_is_reset(l))
751                 return;
752
753         n->working_links--;
754         n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
755         n->link_id = tipc_link_id(l);
756
757         tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
758
759         pr_debug("Lost link <%s> on network plane %c\n",
760                  tipc_link_name(l), tipc_link_plane(l));
761
762         /* Select new active link if any available */
763         *slot0 = INVALID_BEARER_ID;
764         *slot1 = INVALID_BEARER_ID;
765         for (i = 0; i < MAX_BEARERS; i++) {
766                 _l = n->links[i].link;
767                 if (!_l || !tipc_link_is_up(_l))
768                         continue;
769                 if (_l == l)
770                         continue;
771                 prio = tipc_link_prio(_l);
772                 if (prio < highest)
773                         continue;
774                 if (prio > highest) {
775                         highest = prio;
776                         *slot0 = i;
777                         *slot1 = i;
778                         continue;
779                 }
780                 *slot1 = i;
781         }
782
783         if (!node_is_up(n)) {
784                 if (tipc_link_peer_is_down(l))
785                         tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
786                 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
787                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
788                 tipc_link_reset(l);
789                 tipc_link_build_reset_msg(l, xmitq);
790                 *maddr = &n->links[*bearer_id].maddr;
791                 node_lost_contact(n, &le->inputq);
792                 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
793                 return;
794         }
795         tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
796
797         /* There is still a working link => initiate failover */
798         *bearer_id = n->active_links[0];
799         tnl = n->links[*bearer_id].link;
800         tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
801         tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
802         n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
803         tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
804         tipc_link_reset(l);
805         tipc_link_fsm_evt(l, LINK_RESET_EVT);
806         tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
807         tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
808         *maddr = &n->links[*bearer_id].maddr;
809 }
810
811 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
812 {
813         struct tipc_link_entry *le = &n->links[bearer_id];
814         struct tipc_link *l = le->link;
815         struct tipc_media_addr *maddr;
816         struct sk_buff_head xmitq;
817         int old_bearer_id = bearer_id;
818
819         if (!l)
820                 return;
821
822         __skb_queue_head_init(&xmitq);
823
824         tipc_node_write_lock(n);
825         if (!tipc_link_is_establishing(l)) {
826                 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
827                 if (delete) {
828                         kfree(l);
829                         le->link = NULL;
830                         n->link_cnt--;
831                 }
832         } else {
833                 /* Defuse pending tipc_node_link_up() */
834                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
835         }
836         tipc_node_write_unlock(n);
837         if (delete)
838                 tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
839         tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
840         tipc_sk_rcv(n->net, &le->inputq);
841 }
842
843 static bool node_is_up(struct tipc_node *n)
844 {
845         return n->active_links[0] != INVALID_BEARER_ID;
846 }
847
848 bool tipc_node_is_up(struct net *net, u32 addr)
849 {
850         struct tipc_node *n;
851         bool retval = false;
852
853         if (in_own_node(net, addr))
854                 return true;
855
856         n = tipc_node_find(net, addr);
857         if (!n)
858                 return false;
859         retval = node_is_up(n);
860         tipc_node_put(n);
861         return retval;
862 }
863
864 static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
865 {
866         struct tipc_node *n;
867
868         addr ^= tipc_net(net)->random;
869         while ((n = tipc_node_find(net, addr))) {
870                 tipc_node_put(n);
871                 addr++;
872         }
873         return addr;
874 }
875
876 /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
877  * Returns suggested address if any, otherwise 0
878  */
879 u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
880 {
881         struct tipc_net *tn = tipc_net(net);
882         struct tipc_node *n;
883
884         /* Suggest new address if some other peer is using this one */
885         n = tipc_node_find(net, addr);
886         if (n) {
887                 if (!memcmp(n->peer_id, id, NODE_ID_LEN))
888                         addr = 0;
889                 tipc_node_put(n);
890                 if (!addr)
891                         return 0;
892                 return tipc_node_suggest_addr(net, addr);
893         }
894
895         /* Suggest previously used address if peer is known */
896         n = tipc_node_find_by_id(net, id);
897         if (n) {
898                 addr = n->addr;
899                 tipc_node_put(n);
900                 return addr;
901         }
902
903         /* Even this node may be in conflict */
904         if (tn->trial_addr == addr)
905                 return tipc_node_suggest_addr(net, addr);
906
907         return 0;
908 }
909
910 void tipc_node_check_dest(struct net *net, u32 addr,
911                           u8 *peer_id, struct tipc_bearer *b,
912                           u16 capabilities, u32 signature,
913                           struct tipc_media_addr *maddr,
914                           bool *respond, bool *dupl_addr)
915 {
916         struct tipc_node *n;
917         struct tipc_link *l;
918         struct tipc_link_entry *le;
919         bool addr_match = false;
920         bool sign_match = false;
921         bool link_up = false;
922         bool accept_addr = false;
923         bool reset = true;
924         char *if_name;
925         unsigned long intv;
926         u16 session;
927
928         *dupl_addr = false;
929         *respond = false;
930
931         n = tipc_node_create(net, addr, peer_id, capabilities);
932         if (!n)
933                 return;
934
935         tipc_node_write_lock(n);
936
937         le = &n->links[b->identity];
938
939         /* Prepare to validate requesting node's signature and media address */
940         l = le->link;
941         link_up = l && tipc_link_is_up(l);
942         addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
943         sign_match = (signature == n->signature);
944
945         /* These three flags give us eight permutations: */
946
947         if (sign_match && addr_match && link_up) {
948                 /* All is fine. Do nothing. */
949                 reset = false;
950         } else if (sign_match && addr_match && !link_up) {
951                 /* Respond. The link will come up in due time */
952                 *respond = true;
953         } else if (sign_match && !addr_match && link_up) {
954                 /* Peer has changed i/f address without rebooting.
955                  * If so, the link will reset soon, and the next
956                  * discovery will be accepted. So we can ignore it.
957                  * It may also be an cloned or malicious peer having
958                  * chosen the same node address and signature as an
959                  * existing one.
960                  * Ignore requests until the link goes down, if ever.
961                  */
962                 *dupl_addr = true;
963         } else if (sign_match && !addr_match && !link_up) {
964                 /* Peer link has changed i/f address without rebooting.
965                  * It may also be a cloned or malicious peer; we can't
966                  * distinguish between the two.
967                  * The signature is correct, so we must accept.
968                  */
969                 accept_addr = true;
970                 *respond = true;
971         } else if (!sign_match && addr_match && link_up) {
972                 /* Peer node rebooted. Two possibilities:
973                  *  - Delayed re-discovery; this link endpoint has already
974                  *    reset and re-established contact with the peer, before
975                  *    receiving a discovery message from that node.
976                  *    (The peer happened to receive one from this node first).
977                  *  - The peer came back so fast that our side has not
978                  *    discovered it yet. Probing from this side will soon
979                  *    reset the link, since there can be no working link
980                  *    endpoint at the peer end, and the link will re-establish.
981                  *  Accept the signature, since it comes from a known peer.
982                  */
983                 n->signature = signature;
984         } else if (!sign_match && addr_match && !link_up) {
985                 /*  The peer node has rebooted.
986                  *  Accept signature, since it is a known peer.
987                  */
988                 n->signature = signature;
989                 *respond = true;
990         } else if (!sign_match && !addr_match && link_up) {
991                 /* Peer rebooted with new address, or a new/duplicate peer.
992                  * Ignore until the link goes down, if ever.
993                  */
994                 *dupl_addr = true;
995         } else if (!sign_match && !addr_match && !link_up) {
996                 /* Peer rebooted with new address, or it is a new peer.
997                  * Accept signature and address.
998                  */
999                 n->signature = signature;
1000                 accept_addr = true;
1001                 *respond = true;
1002         }
1003
1004         if (!accept_addr)
1005                 goto exit;
1006
1007         /* Now create new link if not already existing */
1008         if (!l) {
1009                 if (n->link_cnt == 2)
1010                         goto exit;
1011
1012                 if_name = strchr(b->name, ':') + 1;
1013                 get_random_bytes(&session, sizeof(u16));
1014                 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
1015                                       b->net_plane, b->mtu, b->priority,
1016                                       b->window, session,
1017                                       tipc_own_addr(net), addr, peer_id,
1018                                       n->capabilities,
1019                                       tipc_bc_sndlink(n->net), n->bc_entry.link,
1020                                       &le->inputq,
1021                                       &n->bc_entry.namedq, &l)) {
1022                         *respond = false;
1023                         goto exit;
1024                 }
1025                 tipc_link_reset(l);
1026                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
1027                 if (n->state == NODE_FAILINGOVER)
1028                         tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
1029                 le->link = l;
1030                 n->link_cnt++;
1031                 tipc_node_calculate_timer(n, l);
1032                 if (n->link_cnt == 1) {
1033                         intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
1034                         if (!mod_timer(&n->timer, intv))
1035                                 tipc_node_get(n);
1036                 }
1037         }
1038         memcpy(&le->maddr, maddr, sizeof(*maddr));
1039 exit:
1040         tipc_node_write_unlock(n);
1041         if (reset && l && !tipc_link_is_reset(l))
1042                 tipc_node_link_down(n, b->identity, false);
1043         tipc_node_put(n);
1044 }
1045
1046 void tipc_node_delete_links(struct net *net, int bearer_id)
1047 {
1048         struct tipc_net *tn = net_generic(net, tipc_net_id);
1049         struct tipc_node *n;
1050
1051         rcu_read_lock();
1052         list_for_each_entry_rcu(n, &tn->node_list, list) {
1053                 tipc_node_link_down(n, bearer_id, true);
1054         }
1055         rcu_read_unlock();
1056 }
1057
1058 static void tipc_node_reset_links(struct tipc_node *n)
1059 {
1060         int i;
1061
1062         pr_warn("Resetting all links to %x\n", n->addr);
1063
1064         for (i = 0; i < MAX_BEARERS; i++) {
1065                 tipc_node_link_down(n, i, false);
1066         }
1067 }
1068
1069 /* tipc_node_fsm_evt - node finite state machine
1070  * Determines when contact is allowed with peer node
1071  */
1072 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
1073 {
1074         int state = n->state;
1075
1076         switch (state) {
1077         case SELF_DOWN_PEER_DOWN:
1078                 switch (evt) {
1079                 case SELF_ESTABL_CONTACT_EVT:
1080                         state = SELF_UP_PEER_COMING;
1081                         break;
1082                 case PEER_ESTABL_CONTACT_EVT:
1083                         state = SELF_COMING_PEER_UP;
1084                         break;
1085                 case SELF_LOST_CONTACT_EVT:
1086                 case PEER_LOST_CONTACT_EVT:
1087                         break;
1088                 case NODE_SYNCH_END_EVT:
1089                 case NODE_SYNCH_BEGIN_EVT:
1090                 case NODE_FAILOVER_BEGIN_EVT:
1091                 case NODE_FAILOVER_END_EVT:
1092                 default:
1093                         goto illegal_evt;
1094                 }
1095                 break;
1096         case SELF_UP_PEER_UP:
1097                 switch (evt) {
1098                 case SELF_LOST_CONTACT_EVT:
1099                         state = SELF_DOWN_PEER_LEAVING;
1100                         break;
1101                 case PEER_LOST_CONTACT_EVT:
1102                         state = SELF_LEAVING_PEER_DOWN;
1103                         break;
1104                 case NODE_SYNCH_BEGIN_EVT:
1105                         state = NODE_SYNCHING;
1106                         break;
1107                 case NODE_FAILOVER_BEGIN_EVT:
1108                         state = NODE_FAILINGOVER;
1109                         break;
1110                 case SELF_ESTABL_CONTACT_EVT:
1111                 case PEER_ESTABL_CONTACT_EVT:
1112                 case NODE_SYNCH_END_EVT:
1113                 case NODE_FAILOVER_END_EVT:
1114                         break;
1115                 default:
1116                         goto illegal_evt;
1117                 }
1118                 break;
1119         case SELF_DOWN_PEER_LEAVING:
1120                 switch (evt) {
1121                 case PEER_LOST_CONTACT_EVT:
1122                         state = SELF_DOWN_PEER_DOWN;
1123                         break;
1124                 case SELF_ESTABL_CONTACT_EVT:
1125                 case PEER_ESTABL_CONTACT_EVT:
1126                 case SELF_LOST_CONTACT_EVT:
1127                         break;
1128                 case NODE_SYNCH_END_EVT:
1129                 case NODE_SYNCH_BEGIN_EVT:
1130                 case NODE_FAILOVER_BEGIN_EVT:
1131                 case NODE_FAILOVER_END_EVT:
1132                 default:
1133                         goto illegal_evt;
1134                 }
1135                 break;
1136         case SELF_UP_PEER_COMING:
1137                 switch (evt) {
1138                 case PEER_ESTABL_CONTACT_EVT:
1139                         state = SELF_UP_PEER_UP;
1140                         break;
1141                 case SELF_LOST_CONTACT_EVT:
1142                         state = SELF_DOWN_PEER_DOWN;
1143                         break;
1144                 case SELF_ESTABL_CONTACT_EVT:
1145                 case PEER_LOST_CONTACT_EVT:
1146                 case NODE_SYNCH_END_EVT:
1147                 case NODE_FAILOVER_BEGIN_EVT:
1148                         break;
1149                 case NODE_SYNCH_BEGIN_EVT:
1150                 case NODE_FAILOVER_END_EVT:
1151                 default:
1152                         goto illegal_evt;
1153                 }
1154                 break;
1155         case SELF_COMING_PEER_UP:
1156                 switch (evt) {
1157                 case SELF_ESTABL_CONTACT_EVT:
1158                         state = SELF_UP_PEER_UP;
1159                         break;
1160                 case PEER_LOST_CONTACT_EVT:
1161                         state = SELF_DOWN_PEER_DOWN;
1162                         break;
1163                 case SELF_LOST_CONTACT_EVT:
1164                 case PEER_ESTABL_CONTACT_EVT:
1165                         break;
1166                 case NODE_SYNCH_END_EVT:
1167                 case NODE_SYNCH_BEGIN_EVT:
1168                 case NODE_FAILOVER_BEGIN_EVT:
1169                 case NODE_FAILOVER_END_EVT:
1170                 default:
1171                         goto illegal_evt;
1172                 }
1173                 break;
1174         case SELF_LEAVING_PEER_DOWN:
1175                 switch (evt) {
1176                 case SELF_LOST_CONTACT_EVT:
1177                         state = SELF_DOWN_PEER_DOWN;
1178                         break;
1179                 case SELF_ESTABL_CONTACT_EVT:
1180                 case PEER_ESTABL_CONTACT_EVT:
1181                 case PEER_LOST_CONTACT_EVT:
1182                         break;
1183                 case NODE_SYNCH_END_EVT:
1184                 case NODE_SYNCH_BEGIN_EVT:
1185                 case NODE_FAILOVER_BEGIN_EVT:
1186                 case NODE_FAILOVER_END_EVT:
1187                 default:
1188                         goto illegal_evt;
1189                 }
1190                 break;
1191         case NODE_FAILINGOVER:
1192                 switch (evt) {
1193                 case SELF_LOST_CONTACT_EVT:
1194                         state = SELF_DOWN_PEER_LEAVING;
1195                         break;
1196                 case PEER_LOST_CONTACT_EVT:
1197                         state = SELF_LEAVING_PEER_DOWN;
1198                         break;
1199                 case NODE_FAILOVER_END_EVT:
1200                         state = SELF_UP_PEER_UP;
1201                         break;
1202                 case NODE_FAILOVER_BEGIN_EVT:
1203                 case SELF_ESTABL_CONTACT_EVT:
1204                 case PEER_ESTABL_CONTACT_EVT:
1205                         break;
1206                 case NODE_SYNCH_BEGIN_EVT:
1207                 case NODE_SYNCH_END_EVT:
1208                 default:
1209                         goto illegal_evt;
1210                 }
1211                 break;
1212         case NODE_SYNCHING:
1213                 switch (evt) {
1214                 case SELF_LOST_CONTACT_EVT:
1215                         state = SELF_DOWN_PEER_LEAVING;
1216                         break;
1217                 case PEER_LOST_CONTACT_EVT:
1218                         state = SELF_LEAVING_PEER_DOWN;
1219                         break;
1220                 case NODE_SYNCH_END_EVT:
1221                         state = SELF_UP_PEER_UP;
1222                         break;
1223                 case NODE_FAILOVER_BEGIN_EVT:
1224                         state = NODE_FAILINGOVER;
1225                         break;
1226                 case NODE_SYNCH_BEGIN_EVT:
1227                 case SELF_ESTABL_CONTACT_EVT:
1228                 case PEER_ESTABL_CONTACT_EVT:
1229                         break;
1230                 case NODE_FAILOVER_END_EVT:
1231                 default:
1232                         goto illegal_evt;
1233                 }
1234                 break;
1235         default:
1236                 pr_err("Unknown node fsm state %x\n", state);
1237                 break;
1238         }
1239         n->state = state;
1240         return;
1241
1242 illegal_evt:
1243         pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1244 }
1245
1246 static void node_lost_contact(struct tipc_node *n,
1247                               struct sk_buff_head *inputq)
1248 {
1249         struct tipc_sock_conn *conn, *safe;
1250         struct tipc_link *l;
1251         struct list_head *conns = &n->conn_sks;
1252         struct sk_buff *skb;
1253         uint i;
1254
1255         pr_debug("Lost contact with %x\n", n->addr);
1256         n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
1257
1258         /* Clean up broadcast state */
1259         tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1260
1261         /* Abort any ongoing link failover */
1262         for (i = 0; i < MAX_BEARERS; i++) {
1263                 l = n->links[i].link;
1264                 if (l)
1265                         tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1266         }
1267
1268         /* Notify publications from this node */
1269         n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1270
1271         /* Notify sockets connected to node */
1272         list_for_each_entry_safe(conn, safe, conns, list) {
1273                 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1274                                       SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1275                                       conn->peer_node, conn->port,
1276                                       conn->peer_port, TIPC_ERR_NO_NODE);
1277                 if (likely(skb))
1278                         skb_queue_tail(inputq, skb);
1279                 list_del(&conn->list);
1280                 kfree(conn);
1281         }
1282 }
1283
1284 /**
1285  * tipc_node_get_linkname - get the name of a link
1286  *
1287  * @bearer_id: id of the bearer
1288  * @node: peer node address
1289  * @linkname: link name output buffer
1290  *
1291  * Returns 0 on success
1292  */
1293 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1294                            char *linkname, size_t len)
1295 {
1296         struct tipc_link *link;
1297         int err = -EINVAL;
1298         struct tipc_node *node = tipc_node_find(net, addr);
1299
1300         if (!node)
1301                 return err;
1302
1303         if (bearer_id >= MAX_BEARERS)
1304                 goto exit;
1305
1306         tipc_node_read_lock(node);
1307         link = node->links[bearer_id].link;
1308         if (link) {
1309                 strncpy(linkname, tipc_link_name(link), len);
1310                 err = 0;
1311         }
1312         tipc_node_read_unlock(node);
1313 exit:
1314         tipc_node_put(node);
1315         return err;
1316 }
1317
1318 /* Caller should hold node lock for the passed node */
1319 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1320 {
1321         void *hdr;
1322         struct nlattr *attrs;
1323
1324         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1325                           NLM_F_MULTI, TIPC_NL_NODE_GET);
1326         if (!hdr)
1327                 return -EMSGSIZE;
1328
1329         attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1330         if (!attrs)
1331                 goto msg_full;
1332
1333         if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1334                 goto attr_msg_full;
1335         if (node_is_up(node))
1336                 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1337                         goto attr_msg_full;
1338
1339         nla_nest_end(msg->skb, attrs);
1340         genlmsg_end(msg->skb, hdr);
1341
1342         return 0;
1343
1344 attr_msg_full:
1345         nla_nest_cancel(msg->skb, attrs);
1346 msg_full:
1347         genlmsg_cancel(msg->skb, hdr);
1348
1349         return -EMSGSIZE;
1350 }
1351
1352 /**
1353  * tipc_node_xmit() is the general link level function for message sending
1354  * @net: the applicable net namespace
1355  * @list: chain of buffers containing message
1356  * @dnode: address of destination node
1357  * @selector: a number used for deterministic link selection
1358  * Consumes the buffer chain.
1359  * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1360  */
1361 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1362                    u32 dnode, int selector)
1363 {
1364         struct tipc_link_entry *le = NULL;
1365         struct tipc_node *n;
1366         struct sk_buff_head xmitq;
1367         int bearer_id;
1368         int rc;
1369
1370         if (in_own_node(net, dnode)) {
1371                 tipc_sk_rcv(net, list);
1372                 return 0;
1373         }
1374
1375         n = tipc_node_find(net, dnode);
1376         if (unlikely(!n)) {
1377                 skb_queue_purge(list);
1378                 return -EHOSTUNREACH;
1379         }
1380
1381         tipc_node_read_lock(n);
1382         bearer_id = n->active_links[selector & 1];
1383         if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1384                 tipc_node_read_unlock(n);
1385                 tipc_node_put(n);
1386                 skb_queue_purge(list);
1387                 return -EHOSTUNREACH;
1388         }
1389
1390         __skb_queue_head_init(&xmitq);
1391         le = &n->links[bearer_id];
1392         spin_lock_bh(&le->lock);
1393         rc = tipc_link_xmit(le->link, list, &xmitq);
1394         spin_unlock_bh(&le->lock);
1395         tipc_node_read_unlock(n);
1396
1397         if (unlikely(rc == -ENOBUFS))
1398                 tipc_node_link_down(n, bearer_id, false);
1399         else
1400                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1401
1402         tipc_node_put(n);
1403
1404         return rc;
1405 }
1406
1407 /* tipc_node_xmit_skb(): send single buffer to destination
1408  * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1409  * messages, which will not be rejected
1410  * The only exception is datagram messages rerouted after secondary
1411  * lookup, which are rare and safe to dispose of anyway.
1412  */
1413 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1414                        u32 selector)
1415 {
1416         struct sk_buff_head head;
1417
1418         skb_queue_head_init(&head);
1419         __skb_queue_tail(&head, skb);
1420         tipc_node_xmit(net, &head, dnode, selector);
1421         return 0;
1422 }
1423
1424 /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
1425  * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
1426  */
1427 int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
1428 {
1429         struct sk_buff *skb;
1430         u32 selector, dnode;
1431
1432         while ((skb = __skb_dequeue(xmitq))) {
1433                 selector = msg_origport(buf_msg(skb));
1434                 dnode = msg_destnode(buf_msg(skb));
1435                 tipc_node_xmit_skb(net, skb, dnode, selector);
1436         }
1437         return 0;
1438 }
1439
1440 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1441 {
1442         struct sk_buff *txskb;
1443         struct tipc_node *n;
1444         u32 dst;
1445
1446         rcu_read_lock();
1447         list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1448                 dst = n->addr;
1449                 if (in_own_node(net, dst))
1450                         continue;
1451                 if (!node_is_up(n))
1452                         continue;
1453                 txskb = pskb_copy(skb, GFP_ATOMIC);
1454                 if (!txskb)
1455                         break;
1456                 msg_set_destnode(buf_msg(txskb), dst);
1457                 tipc_node_xmit_skb(net, txskb, dst, 0);
1458         }
1459         rcu_read_unlock();
1460
1461         kfree_skb(skb);
1462 }
1463
1464 static void tipc_node_mcast_rcv(struct tipc_node *n)
1465 {
1466         struct tipc_bclink_entry *be = &n->bc_entry;
1467
1468         /* 'arrvq' is under inputq2's lock protection */
1469         spin_lock_bh(&be->inputq2.lock);
1470         spin_lock_bh(&be->inputq1.lock);
1471         skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1472         spin_unlock_bh(&be->inputq1.lock);
1473         spin_unlock_bh(&be->inputq2.lock);
1474         tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
1475 }
1476
1477 static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
1478                                   int bearer_id, struct sk_buff_head *xmitq)
1479 {
1480         struct tipc_link *ucl;
1481         int rc;
1482
1483         rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
1484
1485         if (rc & TIPC_LINK_DOWN_EVT) {
1486                 tipc_node_reset_links(n);
1487                 return;
1488         }
1489
1490         if (!(rc & TIPC_LINK_SND_STATE))
1491                 return;
1492
1493         /* If probe message, a STATE response will be sent anyway */
1494         if (msg_probe(hdr))
1495                 return;
1496
1497         /* Produce a STATE message carrying broadcast NACK */
1498         tipc_node_read_lock(n);
1499         ucl = n->links[bearer_id].link;
1500         if (ucl)
1501                 tipc_link_build_state_msg(ucl, xmitq);
1502         tipc_node_read_unlock(n);
1503 }
1504
1505 /**
1506  * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1507  * @net: the applicable net namespace
1508  * @skb: TIPC packet
1509  * @bearer_id: id of bearer message arrived on
1510  *
1511  * Invoked with no locks held.
1512  */
1513 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1514 {
1515         int rc;
1516         struct sk_buff_head xmitq;
1517         struct tipc_bclink_entry *be;
1518         struct tipc_link_entry *le;
1519         struct tipc_msg *hdr = buf_msg(skb);
1520         int usr = msg_user(hdr);
1521         u32 dnode = msg_destnode(hdr);
1522         struct tipc_node *n;
1523
1524         __skb_queue_head_init(&xmitq);
1525
1526         /* If NACK for other node, let rcv link for that node peek into it */
1527         if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1528                 n = tipc_node_find(net, dnode);
1529         else
1530                 n = tipc_node_find(net, msg_prevnode(hdr));
1531         if (!n) {
1532                 kfree_skb(skb);
1533                 return;
1534         }
1535         be = &n->bc_entry;
1536         le = &n->links[bearer_id];
1537
1538         rc = tipc_bcast_rcv(net, be->link, skb);
1539
1540         /* Broadcast ACKs are sent on a unicast link */
1541         if (rc & TIPC_LINK_SND_STATE) {
1542                 tipc_node_read_lock(n);
1543                 tipc_link_build_state_msg(le->link, &xmitq);
1544                 tipc_node_read_unlock(n);
1545         }
1546
1547         if (!skb_queue_empty(&xmitq))
1548                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1549
1550         if (!skb_queue_empty(&be->inputq1))
1551                 tipc_node_mcast_rcv(n);
1552
1553         /* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
1554         if (!skb_queue_empty(&n->bc_entry.namedq))
1555                 tipc_named_rcv(net, &n->bc_entry.namedq);
1556
1557         /* If reassembly or retransmission failure => reset all links to peer */
1558         if (rc & TIPC_LINK_DOWN_EVT)
1559                 tipc_node_reset_links(n);
1560
1561         tipc_node_put(n);
1562 }
1563
1564 /**
1565  * tipc_node_check_state - check and if necessary update node state
1566  * @skb: TIPC packet
1567  * @bearer_id: identity of bearer delivering the packet
1568  * Returns true if state and msg are ok, otherwise false
1569  */
1570 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1571                                   int bearer_id, struct sk_buff_head *xmitq)
1572 {
1573         struct tipc_msg *hdr = buf_msg(skb);
1574         int usr = msg_user(hdr);
1575         int mtyp = msg_type(hdr);
1576         u16 oseqno = msg_seqno(hdr);
1577         u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1578         u16 exp_pkts = msg_msgcnt(hdr);
1579         u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1580         int state = n->state;
1581         struct tipc_link *l, *tnl, *pl = NULL;
1582         struct tipc_media_addr *maddr;
1583         int pb_id;
1584
1585         l = n->links[bearer_id].link;
1586         if (!l)
1587                 return false;
1588         rcv_nxt = tipc_link_rcv_nxt(l);
1589
1590
1591         if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1592                 return true;
1593
1594         /* Find parallel link, if any */
1595         for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1596                 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1597                         pl = n->links[pb_id].link;
1598                         break;
1599                 }
1600         }
1601
1602         if (!tipc_link_validate_msg(l, hdr))
1603                 return false;
1604
1605         /* Check and update node accesibility if applicable */
1606         if (state == SELF_UP_PEER_COMING) {
1607                 if (!tipc_link_is_up(l))
1608                         return true;
1609                 if (!msg_peer_link_is_up(hdr))
1610                         return true;
1611                 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1612         }
1613
1614         if (state == SELF_DOWN_PEER_LEAVING) {
1615                 if (msg_peer_node_is_up(hdr))
1616                         return false;
1617                 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1618                 return true;
1619         }
1620
1621         if (state == SELF_LEAVING_PEER_DOWN)
1622                 return false;
1623
1624         /* Ignore duplicate packets */
1625         if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1626                 return true;
1627
1628         /* Initiate or update failover mode if applicable */
1629         if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1630                 syncpt = oseqno + exp_pkts - 1;
1631                 if (pl && tipc_link_is_up(pl)) {
1632                         __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1633                         tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1634                                                         tipc_link_inputq(l));
1635                 }
1636                 /* If parallel link was already down, and this happened before
1637                  * the tunnel link came up, FAILOVER was never sent. Ensure that
1638                  * FAILOVER is sent to get peer out of NODE_FAILINGOVER state.
1639                  */
1640                 if (n->state != NODE_FAILINGOVER && !n->failover_sent) {
1641                         tipc_link_create_dummy_tnl_msg(l, xmitq);
1642                         n->failover_sent = true;
1643                 }
1644                 /* If pkts arrive out of order, use lowest calculated syncpt */
1645                 if (less(syncpt, n->sync_point))
1646                         n->sync_point = syncpt;
1647         }
1648
1649         /* Open parallel link when tunnel link reaches synch point */
1650         if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1651                 if (!more(rcv_nxt, n->sync_point))
1652                         return true;
1653                 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1654                 if (pl)
1655                         tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1656                 return true;
1657         }
1658
1659         /* No synching needed if only one link */
1660         if (!pl || !tipc_link_is_up(pl))
1661                 return true;
1662
1663         /* Initiate synch mode if applicable */
1664         if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1665                 syncpt = iseqno + exp_pkts - 1;
1666                 if (!tipc_link_is_up(l))
1667                         __tipc_node_link_up(n, bearer_id, xmitq);
1668                 if (n->state == SELF_UP_PEER_UP) {
1669                         n->sync_point = syncpt;
1670                         tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1671                         tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1672                 }
1673         }
1674
1675         /* Open tunnel link when parallel link reaches synch point */
1676         if (n->state == NODE_SYNCHING) {
1677                 if (tipc_link_is_synching(l)) {
1678                         tnl = l;
1679                 } else {
1680                         tnl = pl;
1681                         pl = l;
1682                 }
1683                 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1684                 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1685                 if (more(dlv_nxt, n->sync_point)) {
1686                         tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1687                         tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1688                         return true;
1689                 }
1690                 if (l == pl)
1691                         return true;
1692                 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1693                         return true;
1694                 if (usr == LINK_PROTOCOL)
1695                         return true;
1696                 return false;
1697         }
1698         return true;
1699 }
1700
1701 /**
1702  * tipc_rcv - process TIPC packets/messages arriving from off-node
1703  * @net: the applicable net namespace
1704  * @skb: TIPC packet
1705  * @bearer: pointer to bearer message arrived on
1706  *
1707  * Invoked with no locks held. Bearer pointer must point to a valid bearer
1708  * structure (i.e. cannot be NULL), but bearer can be inactive.
1709  */
1710 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1711 {
1712         struct sk_buff_head xmitq;
1713         struct tipc_node *n;
1714         struct tipc_msg *hdr;
1715         int bearer_id = b->identity;
1716         struct tipc_link_entry *le;
1717         u32 self = tipc_own_addr(net);
1718         int usr, rc = 0;
1719         u16 bc_ack;
1720
1721         __skb_queue_head_init(&xmitq);
1722
1723         /* Ensure message is well-formed before touching the header */
1724         if (unlikely(!tipc_msg_validate(&skb)))
1725                 goto discard;
1726         hdr = buf_msg(skb);
1727         usr = msg_user(hdr);
1728         bc_ack = msg_bcast_ack(hdr);
1729
1730         /* Handle arrival of discovery or broadcast packet */
1731         if (unlikely(msg_non_seq(hdr))) {
1732                 if (unlikely(usr == LINK_CONFIG))
1733                         return tipc_disc_rcv(net, skb, b);
1734                 else
1735                         return tipc_node_bc_rcv(net, skb, bearer_id);
1736         }
1737
1738         /* Discard unicast link messages destined for another node */
1739         if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
1740                 goto discard;
1741
1742         /* Locate neighboring node that sent packet */
1743         n = tipc_node_find(net, msg_prevnode(hdr));
1744         if (unlikely(!n))
1745                 goto discard;
1746         le = &n->links[bearer_id];
1747
1748         /* Ensure broadcast reception is in synch with peer's send state */
1749         if (unlikely(usr == LINK_PROTOCOL))
1750                 tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
1751         else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
1752                 tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
1753
1754         /* Receive packet directly if conditions permit */
1755         tipc_node_read_lock(n);
1756         if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
1757                 spin_lock_bh(&le->lock);
1758                 if (le->link) {
1759                         rc = tipc_link_rcv(le->link, skb, &xmitq);
1760                         skb = NULL;
1761                 }
1762                 spin_unlock_bh(&le->lock);
1763         }
1764         tipc_node_read_unlock(n);
1765
1766         /* Check/update node state before receiving */
1767         if (unlikely(skb)) {
1768                 if (unlikely(skb_linearize(skb)))
1769                         goto discard;
1770                 tipc_node_write_lock(n);
1771                 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1772                         if (le->link) {
1773                                 rc = tipc_link_rcv(le->link, skb, &xmitq);
1774                                 skb = NULL;
1775                         }
1776                 }
1777                 tipc_node_write_unlock(n);
1778         }
1779
1780         if (unlikely(rc & TIPC_LINK_UP_EVT))
1781                 tipc_node_link_up(n, bearer_id, &xmitq);
1782
1783         if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1784                 tipc_node_link_down(n, bearer_id, false);
1785
1786         if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1787                 tipc_named_rcv(net, &n->bc_entry.namedq);
1788
1789         if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
1790                 tipc_node_mcast_rcv(n);
1791
1792         if (!skb_queue_empty(&le->inputq))
1793                 tipc_sk_rcv(net, &le->inputq);
1794
1795         if (!skb_queue_empty(&xmitq))
1796                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1797
1798         tipc_node_put(n);
1799 discard:
1800         kfree_skb(skb);
1801 }
1802
1803 void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
1804                               int prop)
1805 {
1806         struct tipc_net *tn = tipc_net(net);
1807         int bearer_id = b->identity;
1808         struct sk_buff_head xmitq;
1809         struct tipc_link_entry *e;
1810         struct tipc_node *n;
1811
1812         __skb_queue_head_init(&xmitq);
1813
1814         rcu_read_lock();
1815
1816         list_for_each_entry_rcu(n, &tn->node_list, list) {
1817                 tipc_node_write_lock(n);
1818                 e = &n->links[bearer_id];
1819                 if (e->link) {
1820                         if (prop == TIPC_NLA_PROP_TOL)
1821                                 tipc_link_set_tolerance(e->link, b->tolerance,
1822                                                         &xmitq);
1823                         else if (prop == TIPC_NLA_PROP_MTU)
1824                                 tipc_link_set_mtu(e->link, b->mtu);
1825                 }
1826                 tipc_node_write_unlock(n);
1827                 tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
1828         }
1829
1830         rcu_read_unlock();
1831 }
1832
1833 int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
1834 {
1835         struct net *net = sock_net(skb->sk);
1836         struct tipc_net *tn = net_generic(net, tipc_net_id);
1837         struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
1838         struct tipc_node *peer;
1839         u32 addr;
1840         int err;
1841
1842         /* We identify the peer by its net */
1843         if (!info->attrs[TIPC_NLA_NET])
1844                 return -EINVAL;
1845
1846         err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
1847                                info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
1848                                info->extack);
1849         if (err)
1850                 return err;
1851
1852         if (!attrs[TIPC_NLA_NET_ADDR])
1853                 return -EINVAL;
1854
1855         addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
1856
1857         if (in_own_node(net, addr))
1858                 return -ENOTSUPP;
1859
1860         spin_lock_bh(&tn->node_list_lock);
1861         peer = tipc_node_find(net, addr);
1862         if (!peer) {
1863                 spin_unlock_bh(&tn->node_list_lock);
1864                 return -ENXIO;
1865         }
1866
1867         tipc_node_write_lock(peer);
1868         if (peer->state != SELF_DOWN_PEER_DOWN &&
1869             peer->state != SELF_DOWN_PEER_LEAVING) {
1870                 tipc_node_write_unlock(peer);
1871                 err = -EBUSY;
1872                 goto err_out;
1873         }
1874
1875         tipc_node_clear_links(peer);
1876         tipc_node_write_unlock(peer);
1877         tipc_node_delete(peer);
1878
1879         err = 0;
1880 err_out:
1881         tipc_node_put(peer);
1882         spin_unlock_bh(&tn->node_list_lock);
1883
1884         return err;
1885 }
1886
1887 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1888 {
1889         int err;
1890         struct net *net = sock_net(skb->sk);
1891         struct tipc_net *tn = net_generic(net, tipc_net_id);
1892         int done = cb->args[0];
1893         int last_addr = cb->args[1];
1894         struct tipc_node *node;
1895         struct tipc_nl_msg msg;
1896
1897         if (done)
1898                 return 0;
1899
1900         msg.skb = skb;
1901         msg.portid = NETLINK_CB(cb->skb).portid;
1902         msg.seq = cb->nlh->nlmsg_seq;
1903
1904         rcu_read_lock();
1905         if (last_addr) {
1906                 node = tipc_node_find(net, last_addr);
1907                 if (!node) {
1908                         rcu_read_unlock();
1909                         /* We never set seq or call nl_dump_check_consistent()
1910                          * this means that setting prev_seq here will cause the
1911                          * consistence check to fail in the netlink callback
1912                          * handler. Resulting in the NLMSG_DONE message having
1913                          * the NLM_F_DUMP_INTR flag set if the node state
1914                          * changed while we released the lock.
1915                          */
1916                         cb->prev_seq = 1;
1917                         return -EPIPE;
1918                 }
1919                 tipc_node_put(node);
1920         }
1921
1922         list_for_each_entry_rcu(node, &tn->node_list, list) {
1923                 if (last_addr) {
1924                         if (node->addr == last_addr)
1925                                 last_addr = 0;
1926                         else
1927                                 continue;
1928                 }
1929
1930                 tipc_node_read_lock(node);
1931                 err = __tipc_nl_add_node(&msg, node);
1932                 if (err) {
1933                         last_addr = node->addr;
1934                         tipc_node_read_unlock(node);
1935                         goto out;
1936                 }
1937
1938                 tipc_node_read_unlock(node);
1939         }
1940         done = 1;
1941 out:
1942         cb->args[0] = done;
1943         cb->args[1] = last_addr;
1944         rcu_read_unlock();
1945
1946         return skb->len;
1947 }
1948
1949 /* tipc_node_find_by_name - locate owner node of link by link's name
1950  * @net: the applicable net namespace
1951  * @name: pointer to link name string
1952  * @bearer_id: pointer to index in 'node->links' array where the link was found.
1953  *
1954  * Returns pointer to node owning the link, or 0 if no matching link is found.
1955  */
1956 static struct tipc_node *tipc_node_find_by_name(struct net *net,
1957                                                 const char *link_name,
1958                                                 unsigned int *bearer_id)
1959 {
1960         struct tipc_net *tn = net_generic(net, tipc_net_id);
1961         struct tipc_link *l;
1962         struct tipc_node *n;
1963         struct tipc_node *found_node = NULL;
1964         int i;
1965
1966         *bearer_id = 0;
1967         rcu_read_lock();
1968         list_for_each_entry_rcu(n, &tn->node_list, list) {
1969                 tipc_node_read_lock(n);
1970                 for (i = 0; i < MAX_BEARERS; i++) {
1971                         l = n->links[i].link;
1972                         if (l && !strcmp(tipc_link_name(l), link_name)) {
1973                                 *bearer_id = i;
1974                                 found_node = n;
1975                                 break;
1976                         }
1977                 }
1978                 tipc_node_read_unlock(n);
1979                 if (found_node)
1980                         break;
1981         }
1982         rcu_read_unlock();
1983
1984         return found_node;
1985 }
1986
1987 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1988 {
1989         int err;
1990         int res = 0;
1991         int bearer_id;
1992         char *name;
1993         struct tipc_link *link;
1994         struct tipc_node *node;
1995         struct sk_buff_head xmitq;
1996         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1997         struct net *net = sock_net(skb->sk);
1998
1999         __skb_queue_head_init(&xmitq);
2000
2001         if (!info->attrs[TIPC_NLA_LINK])
2002                 return -EINVAL;
2003
2004         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2005                                info->attrs[TIPC_NLA_LINK],
2006                                tipc_nl_link_policy, info->extack);
2007         if (err)
2008                 return err;
2009
2010         if (!attrs[TIPC_NLA_LINK_NAME])
2011                 return -EINVAL;
2012
2013         name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2014
2015         if (strcmp(name, tipc_bclink_name) == 0)
2016                 return tipc_nl_bc_link_set(net, attrs);
2017
2018         node = tipc_node_find_by_name(net, name, &bearer_id);
2019         if (!node)
2020                 return -EINVAL;
2021
2022         tipc_node_read_lock(node);
2023
2024         link = node->links[bearer_id].link;
2025         if (!link) {
2026                 res = -EINVAL;
2027                 goto out;
2028         }
2029
2030         if (attrs[TIPC_NLA_LINK_PROP]) {
2031                 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2032
2033                 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2034                                               props);
2035                 if (err) {
2036                         res = err;
2037                         goto out;
2038                 }
2039
2040                 if (props[TIPC_NLA_PROP_TOL]) {
2041                         u32 tol;
2042
2043                         tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2044                         tipc_link_set_tolerance(link, tol, &xmitq);
2045                 }
2046                 if (props[TIPC_NLA_PROP_PRIO]) {
2047                         u32 prio;
2048
2049                         prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2050                         tipc_link_set_prio(link, prio, &xmitq);
2051                 }
2052                 if (props[TIPC_NLA_PROP_WIN]) {
2053                         u32 win;
2054
2055                         win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2056                         tipc_link_set_queue_limits(link, win);
2057                 }
2058         }
2059
2060 out:
2061         tipc_node_read_unlock(node);
2062         tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
2063         return res;
2064 }
2065
2066 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
2067 {
2068         struct net *net = genl_info_net(info);
2069         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2070         struct tipc_nl_msg msg;
2071         char *name;
2072         int err;
2073
2074         msg.portid = info->snd_portid;
2075         msg.seq = info->snd_seq;
2076
2077         if (!info->attrs[TIPC_NLA_LINK])
2078                 return -EINVAL;
2079
2080         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2081                                info->attrs[TIPC_NLA_LINK],
2082                                tipc_nl_link_policy, info->extack);
2083         if (err)
2084                 return err;
2085
2086         if (!attrs[TIPC_NLA_LINK_NAME])
2087                 return -EINVAL;
2088
2089         name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2090
2091         msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2092         if (!msg.skb)
2093                 return -ENOMEM;
2094
2095         if (strcmp(name, tipc_bclink_name) == 0) {
2096                 err = tipc_nl_add_bc_link(net, &msg);
2097                 if (err)
2098                         goto err_free;
2099         } else {
2100                 int bearer_id;
2101                 struct tipc_node *node;
2102                 struct tipc_link *link;
2103
2104                 node = tipc_node_find_by_name(net, name, &bearer_id);
2105                 if (!node) {
2106                         err = -EINVAL;
2107                         goto err_free;
2108                 }
2109
2110                 tipc_node_read_lock(node);
2111                 link = node->links[bearer_id].link;
2112                 if (!link) {
2113                         tipc_node_read_unlock(node);
2114                         err = -EINVAL;
2115                         goto err_free;
2116                 }
2117
2118                 err = __tipc_nl_add_link(net, &msg, link, 0);
2119                 tipc_node_read_unlock(node);
2120                 if (err)
2121                         goto err_free;
2122         }
2123
2124         return genlmsg_reply(msg.skb, info);
2125
2126 err_free:
2127         nlmsg_free(msg.skb);
2128         return err;
2129 }
2130
2131 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
2132 {
2133         int err;
2134         char *link_name;
2135         unsigned int bearer_id;
2136         struct tipc_link *link;
2137         struct tipc_node *node;
2138         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2139         struct net *net = sock_net(skb->sk);
2140         struct tipc_link_entry *le;
2141
2142         if (!info->attrs[TIPC_NLA_LINK])
2143                 return -EINVAL;
2144
2145         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2146                                info->attrs[TIPC_NLA_LINK],
2147                                tipc_nl_link_policy, info->extack);
2148         if (err)
2149                 return err;
2150
2151         if (!attrs[TIPC_NLA_LINK_NAME])
2152                 return -EINVAL;
2153
2154         link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2155
2156         if (strcmp(link_name, tipc_bclink_name) == 0) {
2157                 err = tipc_bclink_reset_stats(net);
2158                 if (err)
2159                         return err;
2160                 return 0;
2161         }
2162
2163         node = tipc_node_find_by_name(net, link_name, &bearer_id);
2164         if (!node)
2165                 return -EINVAL;
2166
2167         le = &node->links[bearer_id];
2168         tipc_node_read_lock(node);
2169         spin_lock_bh(&le->lock);
2170         link = node->links[bearer_id].link;
2171         if (!link) {
2172                 spin_unlock_bh(&le->lock);
2173                 tipc_node_read_unlock(node);
2174                 return -EINVAL;
2175         }
2176         tipc_link_reset_stats(link);
2177         spin_unlock_bh(&le->lock);
2178         tipc_node_read_unlock(node);
2179         return 0;
2180 }
2181
2182 /* Caller should hold node lock  */
2183 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2184                                     struct tipc_node *node, u32 *prev_link)
2185 {
2186         u32 i;
2187         int err;
2188
2189         for (i = *prev_link; i < MAX_BEARERS; i++) {
2190                 *prev_link = i;
2191
2192                 if (!node->links[i].link)
2193                         continue;
2194
2195                 err = __tipc_nl_add_link(net, msg,
2196                                          node->links[i].link, NLM_F_MULTI);
2197                 if (err)
2198                         return err;
2199         }
2200         *prev_link = 0;
2201
2202         return 0;
2203 }
2204
2205 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
2206 {
2207         struct net *net = sock_net(skb->sk);
2208         struct tipc_net *tn = net_generic(net, tipc_net_id);
2209         struct tipc_node *node;
2210         struct tipc_nl_msg msg;
2211         u32 prev_node = cb->args[0];
2212         u32 prev_link = cb->args[1];
2213         int done = cb->args[2];
2214         int err;
2215
2216         if (done)
2217                 return 0;
2218
2219         msg.skb = skb;
2220         msg.portid = NETLINK_CB(cb->skb).portid;
2221         msg.seq = cb->nlh->nlmsg_seq;
2222
2223         rcu_read_lock();
2224         if (prev_node) {
2225                 node = tipc_node_find(net, prev_node);
2226                 if (!node) {
2227                         /* We never set seq or call nl_dump_check_consistent()
2228                          * this means that setting prev_seq here will cause the
2229                          * consistence check to fail in the netlink callback
2230                          * handler. Resulting in the last NLMSG_DONE message
2231                          * having the NLM_F_DUMP_INTR flag set.
2232                          */
2233                         cb->prev_seq = 1;
2234                         goto out;
2235                 }
2236                 tipc_node_put(node);
2237
2238                 list_for_each_entry_continue_rcu(node, &tn->node_list,
2239                                                  list) {
2240                         tipc_node_read_lock(node);
2241                         err = __tipc_nl_add_node_links(net, &msg, node,
2242                                                        &prev_link);
2243                         tipc_node_read_unlock(node);
2244                         if (err)
2245                                 goto out;
2246
2247                         prev_node = node->addr;
2248                 }
2249         } else {
2250                 err = tipc_nl_add_bc_link(net, &msg);
2251                 if (err)
2252                         goto out;
2253
2254                 list_for_each_entry_rcu(node, &tn->node_list, list) {
2255                         tipc_node_read_lock(node);
2256                         err = __tipc_nl_add_node_links(net, &msg, node,
2257                                                        &prev_link);
2258                         tipc_node_read_unlock(node);
2259                         if (err)
2260                                 goto out;
2261
2262                         prev_node = node->addr;
2263                 }
2264         }
2265         done = 1;
2266 out:
2267         rcu_read_unlock();
2268
2269         cb->args[0] = prev_node;
2270         cb->args[1] = prev_link;
2271         cb->args[2] = done;
2272
2273         return skb->len;
2274 }
2275
2276 int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
2277 {
2278         struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
2279         struct net *net = sock_net(skb->sk);
2280         int err;
2281
2282         if (!info->attrs[TIPC_NLA_MON])
2283                 return -EINVAL;
2284
2285         err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
2286                                info->attrs[TIPC_NLA_MON],
2287                                tipc_nl_monitor_policy, info->extack);
2288         if (err)
2289                 return err;
2290
2291         if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
2292                 u32 val;
2293
2294                 val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
2295                 err = tipc_nl_monitor_set_threshold(net, val);
2296                 if (err)
2297                         return err;
2298         }
2299
2300         return 0;
2301 }
2302
2303 static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
2304 {
2305         struct nlattr *attrs;
2306         void *hdr;
2307         u32 val;
2308
2309         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2310                           0, TIPC_NL_MON_GET);
2311         if (!hdr)
2312                 return -EMSGSIZE;
2313
2314         attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
2315         if (!attrs)
2316                 goto msg_full;
2317
2318         val = tipc_nl_monitor_get_threshold(net);
2319
2320         if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
2321                 goto attr_msg_full;
2322
2323         nla_nest_end(msg->skb, attrs);
2324         genlmsg_end(msg->skb, hdr);
2325
2326         return 0;
2327
2328 attr_msg_full:
2329         nla_nest_cancel(msg->skb, attrs);
2330 msg_full:
2331         genlmsg_cancel(msg->skb, hdr);
2332
2333         return -EMSGSIZE;
2334 }
2335
2336 int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
2337 {
2338         struct net *net = sock_net(skb->sk);
2339         struct tipc_nl_msg msg;
2340         int err;
2341
2342         msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2343         if (!msg.skb)
2344                 return -ENOMEM;
2345         msg.portid = info->snd_portid;
2346         msg.seq = info->snd_seq;
2347
2348         err = __tipc_nl_add_monitor_prop(net, &msg);
2349         if (err) {
2350                 nlmsg_free(msg.skb);
2351                 return err;
2352         }
2353
2354         return genlmsg_reply(msg.skb, info);
2355 }
2356
2357 int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
2358 {
2359         struct net *net = sock_net(skb->sk);
2360         u32 prev_bearer = cb->args[0];
2361         struct tipc_nl_msg msg;
2362         int bearer_id;
2363         int err;
2364
2365         if (prev_bearer == MAX_BEARERS)
2366                 return 0;
2367
2368         msg.skb = skb;
2369         msg.portid = NETLINK_CB(cb->skb).portid;
2370         msg.seq = cb->nlh->nlmsg_seq;
2371
2372         rtnl_lock();
2373         for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
2374                 err = __tipc_nl_add_monitor(net, &msg, bearer_id);
2375                 if (err)
2376                         break;
2377         }
2378         rtnl_unlock();
2379         cb->args[0] = bearer_id;
2380
2381         return skb->len;
2382 }
2383
2384 int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
2385                                    struct netlink_callback *cb)
2386 {
2387         struct net *net = sock_net(skb->sk);
2388         u32 prev_node = cb->args[1];
2389         u32 bearer_id = cb->args[2];
2390         int done = cb->args[0];
2391         struct tipc_nl_msg msg;
2392         int err;
2393
2394         if (!prev_node) {
2395                 struct nlattr **attrs;
2396                 struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
2397
2398                 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2399                 if (err)
2400                         return err;
2401
2402                 if (!attrs[TIPC_NLA_MON])
2403                         return -EINVAL;
2404
2405                 err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
2406                                        attrs[TIPC_NLA_MON],
2407                                        tipc_nl_monitor_policy, NULL);
2408                 if (err)
2409                         return err;
2410
2411                 if (!mon[TIPC_NLA_MON_REF])
2412                         return -EINVAL;
2413
2414                 bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
2415
2416                 if (bearer_id >= MAX_BEARERS)
2417                         return -EINVAL;
2418         }
2419
2420         if (done)
2421                 return 0;
2422
2423         msg.skb = skb;
2424         msg.portid = NETLINK_CB(cb->skb).portid;
2425         msg.seq = cb->nlh->nlmsg_seq;
2426
2427         rtnl_lock();
2428         err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
2429         if (!err)
2430                 done = 1;
2431
2432         rtnl_unlock();
2433         cb->args[0] = done;
2434         cb->args[1] = prev_node;
2435         cb->args[2] = bearer_id;
2436
2437         return skb->len;
2438 }
2439
2440 u32 tipc_node_get_addr(struct tipc_node *node)
2441 {
2442         return (node) ? node->addr : 0;
2443 }
2444
2445 /**
2446  * tipc_node_dump - dump TIPC node data
2447  * @n: tipc node to be dumped
2448  * @more: dump more?
2449  *        - false: dump only tipc node data
2450  *        - true: dump node link data as well
2451  * @buf: returned buffer of dump data in format
2452  */
2453 int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
2454 {
2455         int i = 0;
2456         size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
2457
2458         if (!n) {
2459                 i += scnprintf(buf, sz, "node data: (null)\n");
2460                 return i;
2461         }
2462
2463         i += scnprintf(buf, sz, "node data: %x", n->addr);
2464         i += scnprintf(buf + i, sz - i, " %x", n->state);
2465         i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
2466         i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
2467         i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
2468         i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
2469         i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
2470         i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
2471         i += scnprintf(buf + i, sz - i, " %u", n->working_links);
2472         i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
2473         i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
2474
2475         if (!more)
2476                 return i;
2477
2478         i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
2479         i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
2480         i += scnprintf(buf + i, sz - i, " media: ");
2481         i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
2482         i += scnprintf(buf + i, sz - i, "\n");
2483         i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
2484         i += scnprintf(buf + i, sz - i, " inputq: ");
2485         i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
2486
2487         i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
2488         i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
2489         i += scnprintf(buf + i, sz - i, " media: ");
2490         i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
2491         i += scnprintf(buf + i, sz - i, "\n");
2492         i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
2493         i += scnprintf(buf + i, sz - i, " inputq: ");
2494         i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
2495
2496         i += scnprintf(buf + i, sz - i, "bclink:\n ");
2497         i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
2498
2499         return i;
2500 }