~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/tipc/node.c

Version: ~ [ linux-5.16-rc1 ] ~ [ linux-5.15.2 ] ~ [ linux-5.14.18 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.79 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.159 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.217 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.255 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.290 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.292 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp