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