node.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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. #include "core.h"
  37. #include "link.h"
  38. #include "node.h"
  39. #include "name_distr.h"
  40. #include "socket.h"
  41. #include "bcast.h"
  42. #include "discover.h"
  43. #include "netlink.h"
  44. #define INVALID_NODE_SIG 0x10000
  45. /* Flags used to take different actions according to flag type
  46. * TIPC_NOTIFY_NODE_DOWN: notify node is down
  47. * TIPC_NOTIFY_NODE_UP: notify node is up
  48. * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
  49. */
  50. enum {
  51. TIPC_NOTIFY_NODE_DOWN = (1 << 3),
  52. TIPC_NOTIFY_NODE_UP = (1 << 4),
  53. TIPC_NOTIFY_LINK_UP = (1 << 6),
  54. TIPC_NOTIFY_LINK_DOWN = (1 << 7)
  55. };
  56. struct tipc_link_entry {
  57. struct tipc_link *link;
  58. spinlock_t lock; /* per link */
  59. u32 mtu;
  60. struct sk_buff_head inputq;
  61. struct tipc_media_addr maddr;
  62. };
  63. struct tipc_bclink_entry {
  64. struct tipc_link *link;
  65. struct sk_buff_head inputq1;
  66. struct sk_buff_head arrvq;
  67. struct sk_buff_head inputq2;
  68. struct sk_buff_head namedq;
  69. };
  70. /**
  71. * struct tipc_node - TIPC node structure
  72. * @addr: network address of node
  73. * @ref: reference counter to node object
  74. * @lock: rwlock governing access to structure
  75. * @net: the applicable net namespace
  76. * @hash: links to adjacent nodes in unsorted hash chain
  77. * @inputq: pointer to input queue containing messages for msg event
  78. * @namedq: pointer to name table input queue with name table messages
  79. * @active_links: bearer ids of active links, used as index into links[] array
  80. * @links: array containing references to all links to node
  81. * @action_flags: bit mask of different types of node actions
  82. * @state: connectivity state vs peer node
  83. * @sync_point: sequence number where synch/failover is finished
  84. * @list: links to adjacent nodes in sorted list of cluster's nodes
  85. * @working_links: number of working links to node (both active and standby)
  86. * @link_cnt: number of links to node
  87. * @capabilities: bitmap, indicating peer node's functional capabilities
  88. * @signature: node instance identifier
  89. * @link_id: local and remote bearer ids of changing link, if any
  90. * @publ_list: list of publications
  91. * @rcu: rcu struct for tipc_node
  92. */
  93. struct tipc_node {
  94. u32 addr;
  95. struct kref kref;
  96. rwlock_t lock;
  97. struct net *net;
  98. struct hlist_node hash;
  99. int active_links[2];
  100. struct tipc_link_entry links[MAX_BEARERS];
  101. struct tipc_bclink_entry bc_entry;
  102. int action_flags;
  103. struct list_head list;
  104. int state;
  105. u16 sync_point;
  106. int link_cnt;
  107. u16 working_links;
  108. u16 capabilities;
  109. u32 signature;
  110. u32 link_id;
  111. struct list_head publ_list;
  112. struct list_head conn_sks;
  113. unsigned long keepalive_intv;
  114. struct timer_list timer;
  115. struct rcu_head rcu;
  116. };
  117. /* Node FSM states and events:
  118. */
  119. enum {
  120. SELF_DOWN_PEER_DOWN = 0xdd,
  121. SELF_UP_PEER_UP = 0xaa,
  122. SELF_DOWN_PEER_LEAVING = 0xd1,
  123. SELF_UP_PEER_COMING = 0xac,
  124. SELF_COMING_PEER_UP = 0xca,
  125. SELF_LEAVING_PEER_DOWN = 0x1d,
  126. NODE_FAILINGOVER = 0xf0,
  127. NODE_SYNCHING = 0xcc
  128. };
  129. enum {
  130. SELF_ESTABL_CONTACT_EVT = 0xece,
  131. SELF_LOST_CONTACT_EVT = 0x1ce,
  132. PEER_ESTABL_CONTACT_EVT = 0x9ece,
  133. PEER_LOST_CONTACT_EVT = 0x91ce,
  134. NODE_FAILOVER_BEGIN_EVT = 0xfbe,
  135. NODE_FAILOVER_END_EVT = 0xfee,
  136. NODE_SYNCH_BEGIN_EVT = 0xcbe,
  137. NODE_SYNCH_END_EVT = 0xcee
  138. };
  139. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  140. struct sk_buff_head *xmitq,
  141. struct tipc_media_addr **maddr);
  142. static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
  143. bool delete);
  144. static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
  145. static void tipc_node_delete(struct tipc_node *node);
  146. static void tipc_node_timeout(unsigned long data);
  147. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  148. static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
  149. static void tipc_node_put(struct tipc_node *node);
  150. static bool tipc_node_is_up(struct tipc_node *n);
  151. struct tipc_sock_conn {
  152. u32 port;
  153. u32 peer_port;
  154. u32 peer_node;
  155. struct list_head list;
  156. };
  157. static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
  158. {
  159. int bearer_id = n->active_links[sel & 1];
  160. if (unlikely(bearer_id == INVALID_BEARER_ID))
  161. return NULL;
  162. return n->links[bearer_id].link;
  163. }
  164. int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
  165. {
  166. struct tipc_node *n;
  167. int bearer_id;
  168. unsigned int mtu = MAX_MSG_SIZE;
  169. n = tipc_node_find(net, addr);
  170. if (unlikely(!n))
  171. return mtu;
  172. bearer_id = n->active_links[sel & 1];
  173. if (likely(bearer_id != INVALID_BEARER_ID))
  174. mtu = n->links[bearer_id].mtu;
  175. tipc_node_put(n);
  176. return mtu;
  177. }
  178. u16 tipc_node_get_capabilities(struct net *net, u32 addr)
  179. {
  180. struct tipc_node *n;
  181. u16 caps;
  182. n = tipc_node_find(net, addr);
  183. if (unlikely(!n))
  184. return TIPC_NODE_CAPABILITIES;
  185. caps = n->capabilities;
  186. tipc_node_put(n);
  187. return caps;
  188. }
  189. /*
  190. * A trivial power-of-two bitmask technique is used for speed, since this
  191. * operation is done for every incoming TIPC packet. The number of hash table
  192. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  193. * usually be much smaller (typically only a single node).
  194. */
  195. static unsigned int tipc_hashfn(u32 addr)
  196. {
  197. return addr & (NODE_HTABLE_SIZE - 1);
  198. }
  199. static void tipc_node_kref_release(struct kref *kref)
  200. {
  201. struct tipc_node *n = container_of(kref, struct tipc_node, kref);
  202. kfree(n->bc_entry.link);
  203. kfree_rcu(n, rcu);
  204. }
  205. static void tipc_node_put(struct tipc_node *node)
  206. {
  207. kref_put(&node->kref, tipc_node_kref_release);
  208. }
  209. static void tipc_node_get(struct tipc_node *node)
  210. {
  211. kref_get(&node->kref);
  212. }
  213. /*
  214. * tipc_node_find - locate specified node object, if it exists
  215. */
  216. static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  217. {
  218. struct tipc_net *tn = tipc_net(net);
  219. struct tipc_node *node;
  220. unsigned int thash = tipc_hashfn(addr);
  221. if (unlikely(!in_own_cluster_exact(net, addr)))
  222. return NULL;
  223. rcu_read_lock();
  224. hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
  225. if (node->addr != addr)
  226. continue;
  227. if (!kref_get_unless_zero(&node->kref))
  228. node = NULL;
  229. break;
  230. }
  231. rcu_read_unlock();
  232. return node;
  233. }
  234. static void tipc_node_read_lock(struct tipc_node *n)
  235. {
  236. read_lock_bh(&n->lock);
  237. }
  238. static void tipc_node_read_unlock(struct tipc_node *n)
  239. {
  240. read_unlock_bh(&n->lock);
  241. }
  242. static void tipc_node_write_lock(struct tipc_node *n)
  243. {
  244. write_lock_bh(&n->lock);
  245. }
  246. static void tipc_node_write_unlock(struct tipc_node *n)
  247. {
  248. struct net *net = n->net;
  249. u32 addr = 0;
  250. u32 flags = n->action_flags;
  251. u32 link_id = 0;
  252. struct list_head *publ_list;
  253. if (likely(!flags)) {
  254. write_unlock_bh(&n->lock);
  255. return;
  256. }
  257. addr = n->addr;
  258. link_id = n->link_id;
  259. publ_list = &n->publ_list;
  260. n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  261. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
  262. write_unlock_bh(&n->lock);
  263. if (flags & TIPC_NOTIFY_NODE_DOWN)
  264. tipc_publ_notify(net, publ_list, addr);
  265. if (flags & TIPC_NOTIFY_NODE_UP)
  266. tipc_named_node_up(net, addr);
  267. if (flags & TIPC_NOTIFY_LINK_UP)
  268. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  269. TIPC_NODE_SCOPE, link_id, addr);
  270. if (flags & TIPC_NOTIFY_LINK_DOWN)
  271. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  272. link_id, addr);
  273. }
  274. struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
  275. {
  276. struct tipc_net *tn = net_generic(net, tipc_net_id);
  277. struct tipc_node *n, *temp_node;
  278. int i;
  279. spin_lock_bh(&tn->node_list_lock);
  280. n = tipc_node_find(net, addr);
  281. if (n) {
  282. /* Same node may come back with new capabilities */
  283. n->capabilities = capabilities;
  284. goto exit;
  285. }
  286. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  287. if (!n) {
  288. pr_warn("Node creation failed, no memory\n");
  289. goto exit;
  290. }
  291. n->addr = addr;
  292. n->net = net;
  293. n->capabilities = capabilities;
  294. kref_init(&n->kref);
  295. rwlock_init(&n->lock);
  296. INIT_HLIST_NODE(&n->hash);
  297. INIT_LIST_HEAD(&n->list);
  298. INIT_LIST_HEAD(&n->publ_list);
  299. INIT_LIST_HEAD(&n->conn_sks);
  300. skb_queue_head_init(&n->bc_entry.namedq);
  301. skb_queue_head_init(&n->bc_entry.inputq1);
  302. __skb_queue_head_init(&n->bc_entry.arrvq);
  303. skb_queue_head_init(&n->bc_entry.inputq2);
  304. for (i = 0; i < MAX_BEARERS; i++)
  305. spin_lock_init(&n->links[i].lock);
  306. n->state = SELF_DOWN_PEER_LEAVING;
  307. n->signature = INVALID_NODE_SIG;
  308. n->active_links[0] = INVALID_BEARER_ID;
  309. n->active_links[1] = INVALID_BEARER_ID;
  310. if (!tipc_link_bc_create(net, tipc_own_addr(net), n->addr,
  311. U16_MAX,
  312. tipc_link_window(tipc_bc_sndlink(net)),
  313. n->capabilities,
  314. &n->bc_entry.inputq1,
  315. &n->bc_entry.namedq,
  316. tipc_bc_sndlink(net),
  317. &n->bc_entry.link)) {
  318. pr_warn("Broadcast rcv link creation failed, no memory\n");
  319. kfree(n);
  320. n = NULL;
  321. goto exit;
  322. }
  323. tipc_node_get(n);
  324. setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
  325. n->keepalive_intv = U32_MAX;
  326. hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
  327. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  328. if (n->addr < temp_node->addr)
  329. break;
  330. }
  331. list_add_tail_rcu(&n->list, &temp_node->list);
  332. exit:
  333. spin_unlock_bh(&tn->node_list_lock);
  334. return n;
  335. }
  336. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  337. {
  338. unsigned long tol = tipc_link_tolerance(l);
  339. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  340. unsigned long keepalive_intv = msecs_to_jiffies(intv);
  341. /* Link with lowest tolerance determines timer interval */
  342. if (keepalive_intv < n->keepalive_intv)
  343. n->keepalive_intv = keepalive_intv;
  344. /* Ensure link's abort limit corresponds to current interval */
  345. tipc_link_set_abort_limit(l, tol / jiffies_to_msecs(n->keepalive_intv));
  346. }
  347. static void tipc_node_delete(struct tipc_node *node)
  348. {
  349. list_del_rcu(&node->list);
  350. hlist_del_rcu(&node->hash);
  351. tipc_node_put(node);
  352. del_timer_sync(&node->timer);
  353. tipc_node_put(node);
  354. }
  355. void tipc_node_stop(struct net *net)
  356. {
  357. struct tipc_net *tn = tipc_net(net);
  358. struct tipc_node *node, *t_node;
  359. spin_lock_bh(&tn->node_list_lock);
  360. list_for_each_entry_safe(node, t_node, &tn->node_list, list)
  361. tipc_node_delete(node);
  362. spin_unlock_bh(&tn->node_list_lock);
  363. }
  364. void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
  365. {
  366. struct tipc_node *n;
  367. if (in_own_node(net, addr))
  368. return;
  369. n = tipc_node_find(net, addr);
  370. if (!n) {
  371. pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
  372. return;
  373. }
  374. tipc_node_write_lock(n);
  375. list_add_tail(subscr, &n->publ_list);
  376. tipc_node_write_unlock(n);
  377. tipc_node_put(n);
  378. }
  379. void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
  380. {
  381. struct tipc_node *n;
  382. if (in_own_node(net, addr))
  383. return;
  384. n = tipc_node_find(net, addr);
  385. if (!n) {
  386. pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
  387. return;
  388. }
  389. tipc_node_write_lock(n);
  390. list_del_init(subscr);
  391. tipc_node_write_unlock(n);
  392. tipc_node_put(n);
  393. }
  394. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  395. {
  396. struct tipc_node *node;
  397. struct tipc_sock_conn *conn;
  398. int err = 0;
  399. if (in_own_node(net, dnode))
  400. return 0;
  401. node = tipc_node_find(net, dnode);
  402. if (!node) {
  403. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  404. return -EHOSTUNREACH;
  405. }
  406. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  407. if (!conn) {
  408. err = -EHOSTUNREACH;
  409. goto exit;
  410. }
  411. conn->peer_node = dnode;
  412. conn->port = port;
  413. conn->peer_port = peer_port;
  414. tipc_node_write_lock(node);
  415. list_add_tail(&conn->list, &node->conn_sks);
  416. tipc_node_write_unlock(node);
  417. exit:
  418. tipc_node_put(node);
  419. return err;
  420. }
  421. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  422. {
  423. struct tipc_node *node;
  424. struct tipc_sock_conn *conn, *safe;
  425. if (in_own_node(net, dnode))
  426. return;
  427. node = tipc_node_find(net, dnode);
  428. if (!node)
  429. return;
  430. tipc_node_write_lock(node);
  431. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  432. if (port != conn->port)
  433. continue;
  434. list_del(&conn->list);
  435. kfree(conn);
  436. }
  437. tipc_node_write_unlock(node);
  438. tipc_node_put(node);
  439. }
  440. /* tipc_node_timeout - handle expiration of node timer
  441. */
  442. static void tipc_node_timeout(unsigned long data)
  443. {
  444. struct tipc_node *n = (struct tipc_node *)data;
  445. struct tipc_link_entry *le;
  446. struct sk_buff_head xmitq;
  447. int bearer_id;
  448. int rc = 0;
  449. __skb_queue_head_init(&xmitq);
  450. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  451. tipc_node_read_lock(n);
  452. le = &n->links[bearer_id];
  453. spin_lock_bh(&le->lock);
  454. if (le->link) {
  455. /* Link tolerance may change asynchronously: */
  456. tipc_node_calculate_timer(n, le->link);
  457. rc = tipc_link_timeout(le->link, &xmitq);
  458. }
  459. spin_unlock_bh(&le->lock);
  460. tipc_node_read_unlock(n);
  461. tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
  462. if (rc & TIPC_LINK_DOWN_EVT)
  463. tipc_node_link_down(n, bearer_id, false);
  464. }
  465. mod_timer(&n->timer, jiffies + n->keepalive_intv);
  466. }
  467. /**
  468. * __tipc_node_link_up - handle addition of link
  469. * Node lock must be held by caller
  470. * Link becomes active (alone or shared) or standby, depending on its priority.
  471. */
  472. static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
  473. struct sk_buff_head *xmitq)
  474. {
  475. int *slot0 = &n->active_links[0];
  476. int *slot1 = &n->active_links[1];
  477. struct tipc_link *ol = node_active_link(n, 0);
  478. struct tipc_link *nl = n->links[bearer_id].link;
  479. if (!nl || tipc_link_is_up(nl))
  480. return;
  481. tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
  482. if (!tipc_link_is_up(nl))
  483. return;
  484. n->working_links++;
  485. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  486. n->link_id = tipc_link_id(nl);
  487. /* Leave room for tunnel header when returning 'mtu' to users: */
  488. n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
  489. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  490. tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
  491. pr_debug("Established link <%s> on network plane %c\n",
  492. tipc_link_name(nl), tipc_link_plane(nl));
  493. /* Ensure that a STATE message goes first */
  494. tipc_link_build_state_msg(nl, xmitq);
  495. /* First link? => give it both slots */
  496. if (!ol) {
  497. *slot0 = bearer_id;
  498. *slot1 = bearer_id;
  499. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  500. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  501. tipc_link_set_active(nl, true);
  502. tipc_bcast_add_peer(n->net, nl, xmitq);
  503. return;
  504. }
  505. /* Second link => redistribute slots */
  506. if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
  507. pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
  508. *slot0 = bearer_id;
  509. *slot1 = bearer_id;
  510. tipc_link_set_active(nl, true);
  511. tipc_link_set_active(ol, false);
  512. } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
  513. tipc_link_set_active(nl, true);
  514. *slot1 = bearer_id;
  515. } else {
  516. pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
  517. }
  518. /* Prepare synchronization with first link */
  519. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  520. }
  521. /**
  522. * tipc_node_link_up - handle addition of link
  523. *
  524. * Link becomes active (alone or shared) or standby, depending on its priority.
  525. */
  526. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  527. struct sk_buff_head *xmitq)
  528. {
  529. struct tipc_media_addr *maddr;
  530. tipc_node_write_lock(n);
  531. __tipc_node_link_up(n, bearer_id, xmitq);
  532. maddr = &n->links[bearer_id].maddr;
  533. tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
  534. tipc_node_write_unlock(n);
  535. }
  536. /**
  537. * __tipc_node_link_down - handle loss of link
  538. */
  539. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  540. struct sk_buff_head *xmitq,
  541. struct tipc_media_addr **maddr)
  542. {
  543. struct tipc_link_entry *le = &n->links[*bearer_id];
  544. int *slot0 = &n->active_links[0];
  545. int *slot1 = &n->active_links[1];
  546. int i, highest = 0, prio;
  547. struct tipc_link *l, *_l, *tnl;
  548. l = n->links[*bearer_id].link;
  549. if (!l || tipc_link_is_reset(l))
  550. return;
  551. n->working_links--;
  552. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  553. n->link_id = tipc_link_id(l);
  554. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  555. pr_debug("Lost link <%s> on network plane %c\n",
  556. tipc_link_name(l), tipc_link_plane(l));
  557. /* Select new active link if any available */
  558. *slot0 = INVALID_BEARER_ID;
  559. *slot1 = INVALID_BEARER_ID;
  560. for (i = 0; i < MAX_BEARERS; i++) {
  561. _l = n->links[i].link;
  562. if (!_l || !tipc_link_is_up(_l))
  563. continue;
  564. if (_l == l)
  565. continue;
  566. prio = tipc_link_prio(_l);
  567. if (prio < highest)
  568. continue;
  569. if (prio > highest) {
  570. highest = prio;
  571. *slot0 = i;
  572. *slot1 = i;
  573. continue;
  574. }
  575. *slot1 = i;
  576. }
  577. if (!tipc_node_is_up(n)) {
  578. if (tipc_link_peer_is_down(l))
  579. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  580. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  581. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  582. tipc_link_reset(l);
  583. tipc_link_build_reset_msg(l, xmitq);
  584. *maddr = &n->links[*bearer_id].maddr;
  585. node_lost_contact(n, &le->inputq);
  586. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  587. return;
  588. }
  589. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  590. /* There is still a working link => initiate failover */
  591. *bearer_id = n->active_links[0];
  592. tnl = n->links[*bearer_id].link;
  593. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  594. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  595. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  596. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  597. tipc_link_reset(l);
  598. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  599. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  600. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  601. *maddr = &n->links[*bearer_id].maddr;
  602. }
  603. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  604. {
  605. struct tipc_link_entry *le = &n->links[bearer_id];
  606. struct tipc_link *l = le->link;
  607. struct tipc_media_addr *maddr;
  608. struct sk_buff_head xmitq;
  609. if (!l)
  610. return;
  611. __skb_queue_head_init(&xmitq);
  612. tipc_node_write_lock(n);
  613. if (!tipc_link_is_establishing(l)) {
  614. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  615. if (delete) {
  616. kfree(l);
  617. le->link = NULL;
  618. n->link_cnt--;
  619. }
  620. } else {
  621. /* Defuse pending tipc_node_link_up() */
  622. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  623. }
  624. tipc_node_write_unlock(n);
  625. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  626. tipc_sk_rcv(n->net, &le->inputq);
  627. }
  628. static bool tipc_node_is_up(struct tipc_node *n)
  629. {
  630. return n->active_links[0] != INVALID_BEARER_ID;
  631. }
  632. void tipc_node_check_dest(struct net *net, u32 onode,
  633. struct tipc_bearer *b,
  634. u16 capabilities, u32 signature,
  635. struct tipc_media_addr *maddr,
  636. bool *respond, bool *dupl_addr)
  637. {
  638. struct tipc_node *n;
  639. struct tipc_link *l;
  640. struct tipc_link_entry *le;
  641. bool addr_match = false;
  642. bool sign_match = false;
  643. bool link_up = false;
  644. bool accept_addr = false;
  645. bool reset = true;
  646. char *if_name;
  647. *dupl_addr = false;
  648. *respond = false;
  649. n = tipc_node_create(net, onode, capabilities);
  650. if (!n)
  651. return;
  652. tipc_node_write_lock(n);
  653. le = &n->links[b->identity];
  654. /* Prepare to validate requesting node's signature and media address */
  655. l = le->link;
  656. link_up = l && tipc_link_is_up(l);
  657. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  658. sign_match = (signature == n->signature);
  659. /* These three flags give us eight permutations: */
  660. if (sign_match && addr_match && link_up) {
  661. /* All is fine. Do nothing. */
  662. reset = false;
  663. } else if (sign_match && addr_match && !link_up) {
  664. /* Respond. The link will come up in due time */
  665. *respond = true;
  666. } else if (sign_match && !addr_match && link_up) {
  667. /* Peer has changed i/f address without rebooting.
  668. * If so, the link will reset soon, and the next
  669. * discovery will be accepted. So we can ignore it.
  670. * It may also be an cloned or malicious peer having
  671. * chosen the same node address and signature as an
  672. * existing one.
  673. * Ignore requests until the link goes down, if ever.
  674. */
  675. *dupl_addr = true;
  676. } else if (sign_match && !addr_match && !link_up) {
  677. /* Peer link has changed i/f address without rebooting.
  678. * It may also be a cloned or malicious peer; we can't
  679. * distinguish between the two.
  680. * The signature is correct, so we must accept.
  681. */
  682. accept_addr = true;
  683. *respond = true;
  684. } else if (!sign_match && addr_match && link_up) {
  685. /* Peer node rebooted. Two possibilities:
  686. * - Delayed re-discovery; this link endpoint has already
  687. * reset and re-established contact with the peer, before
  688. * receiving a discovery message from that node.
  689. * (The peer happened to receive one from this node first).
  690. * - The peer came back so fast that our side has not
  691. * discovered it yet. Probing from this side will soon
  692. * reset the link, since there can be no working link
  693. * endpoint at the peer end, and the link will re-establish.
  694. * Accept the signature, since it comes from a known peer.
  695. */
  696. n->signature = signature;
  697. } else if (!sign_match && addr_match && !link_up) {
  698. /* The peer node has rebooted.
  699. * Accept signature, since it is a known peer.
  700. */
  701. n->signature = signature;
  702. *respond = true;
  703. } else if (!sign_match && !addr_match && link_up) {
  704. /* Peer rebooted with new address, or a new/duplicate peer.
  705. * Ignore until the link goes down, if ever.
  706. */
  707. *dupl_addr = true;
  708. } else if (!sign_match && !addr_match && !link_up) {
  709. /* Peer rebooted with new address, or it is a new peer.
  710. * Accept signature and address.
  711. */
  712. n->signature = signature;
  713. accept_addr = true;
  714. *respond = true;
  715. }
  716. if (!accept_addr)
  717. goto exit;
  718. /* Now create new link if not already existing */
  719. if (!l) {
  720. if (n->link_cnt == 2) {
  721. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  722. goto exit;
  723. }
  724. if_name = strchr(b->name, ':') + 1;
  725. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  726. b->net_plane, b->mtu, b->priority,
  727. b->window, mod(tipc_net(net)->random),
  728. tipc_own_addr(net), onode,
  729. n->capabilities,
  730. tipc_bc_sndlink(n->net), n->bc_entry.link,
  731. &le->inputq,
  732. &n->bc_entry.namedq, &l)) {
  733. *respond = false;
  734. goto exit;
  735. }
  736. tipc_link_reset(l);
  737. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  738. if (n->state == NODE_FAILINGOVER)
  739. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  740. le->link = l;
  741. n->link_cnt++;
  742. tipc_node_calculate_timer(n, l);
  743. if (n->link_cnt == 1)
  744. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  745. tipc_node_get(n);
  746. }
  747. memcpy(&le->maddr, maddr, sizeof(*maddr));
  748. exit:
  749. tipc_node_write_unlock(n);
  750. if (reset && l && !tipc_link_is_reset(l))
  751. tipc_node_link_down(n, b->identity, false);
  752. tipc_node_put(n);
  753. }
  754. void tipc_node_delete_links(struct net *net, int bearer_id)
  755. {
  756. struct tipc_net *tn = net_generic(net, tipc_net_id);
  757. struct tipc_node *n;
  758. rcu_read_lock();
  759. list_for_each_entry_rcu(n, &tn->node_list, list) {
  760. tipc_node_link_down(n, bearer_id, true);
  761. }
  762. rcu_read_unlock();
  763. }
  764. static void tipc_node_reset_links(struct tipc_node *n)
  765. {
  766. char addr_string[16];
  767. int i;
  768. pr_warn("Resetting all links to %s\n",
  769. tipc_addr_string_fill(addr_string, n->addr));
  770. for (i = 0; i < MAX_BEARERS; i++) {
  771. tipc_node_link_down(n, i, false);
  772. }
  773. }
  774. /* tipc_node_fsm_evt - node finite state machine
  775. * Determines when contact is allowed with peer node
  776. */
  777. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  778. {
  779. int state = n->state;
  780. switch (state) {
  781. case SELF_DOWN_PEER_DOWN:
  782. switch (evt) {
  783. case SELF_ESTABL_CONTACT_EVT:
  784. state = SELF_UP_PEER_COMING;
  785. break;
  786. case PEER_ESTABL_CONTACT_EVT:
  787. state = SELF_COMING_PEER_UP;
  788. break;
  789. case SELF_LOST_CONTACT_EVT:
  790. case PEER_LOST_CONTACT_EVT:
  791. break;
  792. case NODE_SYNCH_END_EVT:
  793. case NODE_SYNCH_BEGIN_EVT:
  794. case NODE_FAILOVER_BEGIN_EVT:
  795. case NODE_FAILOVER_END_EVT:
  796. default:
  797. goto illegal_evt;
  798. }
  799. break;
  800. case SELF_UP_PEER_UP:
  801. switch (evt) {
  802. case SELF_LOST_CONTACT_EVT:
  803. state = SELF_DOWN_PEER_LEAVING;
  804. break;
  805. case PEER_LOST_CONTACT_EVT:
  806. state = SELF_LEAVING_PEER_DOWN;
  807. break;
  808. case NODE_SYNCH_BEGIN_EVT:
  809. state = NODE_SYNCHING;
  810. break;
  811. case NODE_FAILOVER_BEGIN_EVT:
  812. state = NODE_FAILINGOVER;
  813. break;
  814. case SELF_ESTABL_CONTACT_EVT:
  815. case PEER_ESTABL_CONTACT_EVT:
  816. case NODE_SYNCH_END_EVT:
  817. case NODE_FAILOVER_END_EVT:
  818. break;
  819. default:
  820. goto illegal_evt;
  821. }
  822. break;
  823. case SELF_DOWN_PEER_LEAVING:
  824. switch (evt) {
  825. case PEER_LOST_CONTACT_EVT:
  826. state = SELF_DOWN_PEER_DOWN;
  827. break;
  828. case SELF_ESTABL_CONTACT_EVT:
  829. case PEER_ESTABL_CONTACT_EVT:
  830. case SELF_LOST_CONTACT_EVT:
  831. break;
  832. case NODE_SYNCH_END_EVT:
  833. case NODE_SYNCH_BEGIN_EVT:
  834. case NODE_FAILOVER_BEGIN_EVT:
  835. case NODE_FAILOVER_END_EVT:
  836. default:
  837. goto illegal_evt;
  838. }
  839. break;
  840. case SELF_UP_PEER_COMING:
  841. switch (evt) {
  842. case PEER_ESTABL_CONTACT_EVT:
  843. state = SELF_UP_PEER_UP;
  844. break;
  845. case SELF_LOST_CONTACT_EVT:
  846. state = SELF_DOWN_PEER_LEAVING;
  847. break;
  848. case SELF_ESTABL_CONTACT_EVT:
  849. case PEER_LOST_CONTACT_EVT:
  850. case NODE_SYNCH_END_EVT:
  851. case NODE_FAILOVER_BEGIN_EVT:
  852. break;
  853. case NODE_SYNCH_BEGIN_EVT:
  854. case NODE_FAILOVER_END_EVT:
  855. default:
  856. goto illegal_evt;
  857. }
  858. break;
  859. case SELF_COMING_PEER_UP:
  860. switch (evt) {
  861. case SELF_ESTABL_CONTACT_EVT:
  862. state = SELF_UP_PEER_UP;
  863. break;
  864. case PEER_LOST_CONTACT_EVT:
  865. state = SELF_LEAVING_PEER_DOWN;
  866. break;
  867. case SELF_LOST_CONTACT_EVT:
  868. case PEER_ESTABL_CONTACT_EVT:
  869. break;
  870. case NODE_SYNCH_END_EVT:
  871. case NODE_SYNCH_BEGIN_EVT:
  872. case NODE_FAILOVER_BEGIN_EVT:
  873. case NODE_FAILOVER_END_EVT:
  874. default:
  875. goto illegal_evt;
  876. }
  877. break;
  878. case SELF_LEAVING_PEER_DOWN:
  879. switch (evt) {
  880. case SELF_LOST_CONTACT_EVT:
  881. state = SELF_DOWN_PEER_DOWN;
  882. break;
  883. case SELF_ESTABL_CONTACT_EVT:
  884. case PEER_ESTABL_CONTACT_EVT:
  885. case PEER_LOST_CONTACT_EVT:
  886. break;
  887. case NODE_SYNCH_END_EVT:
  888. case NODE_SYNCH_BEGIN_EVT:
  889. case NODE_FAILOVER_BEGIN_EVT:
  890. case NODE_FAILOVER_END_EVT:
  891. default:
  892. goto illegal_evt;
  893. }
  894. break;
  895. case NODE_FAILINGOVER:
  896. switch (evt) {
  897. case SELF_LOST_CONTACT_EVT:
  898. state = SELF_DOWN_PEER_LEAVING;
  899. break;
  900. case PEER_LOST_CONTACT_EVT:
  901. state = SELF_LEAVING_PEER_DOWN;
  902. break;
  903. case NODE_FAILOVER_END_EVT:
  904. state = SELF_UP_PEER_UP;
  905. break;
  906. case NODE_FAILOVER_BEGIN_EVT:
  907. case SELF_ESTABL_CONTACT_EVT:
  908. case PEER_ESTABL_CONTACT_EVT:
  909. break;
  910. case NODE_SYNCH_BEGIN_EVT:
  911. case NODE_SYNCH_END_EVT:
  912. default:
  913. goto illegal_evt;
  914. }
  915. break;
  916. case NODE_SYNCHING:
  917. switch (evt) {
  918. case SELF_LOST_CONTACT_EVT:
  919. state = SELF_DOWN_PEER_LEAVING;
  920. break;
  921. case PEER_LOST_CONTACT_EVT:
  922. state = SELF_LEAVING_PEER_DOWN;
  923. break;
  924. case NODE_SYNCH_END_EVT:
  925. state = SELF_UP_PEER_UP;
  926. break;
  927. case NODE_FAILOVER_BEGIN_EVT:
  928. state = NODE_FAILINGOVER;
  929. break;
  930. case NODE_SYNCH_BEGIN_EVT:
  931. case SELF_ESTABL_CONTACT_EVT:
  932. case PEER_ESTABL_CONTACT_EVT:
  933. break;
  934. case NODE_FAILOVER_END_EVT:
  935. default:
  936. goto illegal_evt;
  937. }
  938. break;
  939. default:
  940. pr_err("Unknown node fsm state %x\n", state);
  941. break;
  942. }
  943. n->state = state;
  944. return;
  945. illegal_evt:
  946. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  947. }
  948. static void node_lost_contact(struct tipc_node *n,
  949. struct sk_buff_head *inputq)
  950. {
  951. char addr_string[16];
  952. struct tipc_sock_conn *conn, *safe;
  953. struct tipc_link *l;
  954. struct list_head *conns = &n->conn_sks;
  955. struct sk_buff *skb;
  956. uint i;
  957. pr_debug("Lost contact with %s\n",
  958. tipc_addr_string_fill(addr_string, n->addr));
  959. /* Clean up broadcast state */
  960. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  961. /* Abort any ongoing link failover */
  962. for (i = 0; i < MAX_BEARERS; i++) {
  963. l = n->links[i].link;
  964. if (l)
  965. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  966. }
  967. /* Notify publications from this node */
  968. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  969. /* Notify sockets connected to node */
  970. list_for_each_entry_safe(conn, safe, conns, list) {
  971. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  972. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  973. conn->peer_node, conn->port,
  974. conn->peer_port, TIPC_ERR_NO_NODE);
  975. if (likely(skb))
  976. skb_queue_tail(inputq, skb);
  977. list_del(&conn->list);
  978. kfree(conn);
  979. }
  980. }
  981. /**
  982. * tipc_node_get_linkname - get the name of a link
  983. *
  984. * @bearer_id: id of the bearer
  985. * @node: peer node address
  986. * @linkname: link name output buffer
  987. *
  988. * Returns 0 on success
  989. */
  990. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  991. char *linkname, size_t len)
  992. {
  993. struct tipc_link *link;
  994. int err = -EINVAL;
  995. struct tipc_node *node = tipc_node_find(net, addr);
  996. if (!node)
  997. return err;
  998. if (bearer_id >= MAX_BEARERS)
  999. goto exit;
  1000. tipc_node_read_lock(node);
  1001. link = node->links[bearer_id].link;
  1002. if (link) {
  1003. strncpy(linkname, tipc_link_name(link), len);
  1004. err = 0;
  1005. }
  1006. exit:
  1007. tipc_node_read_unlock(node);
  1008. tipc_node_put(node);
  1009. return err;
  1010. }
  1011. /* Caller should hold node lock for the passed node */
  1012. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  1013. {
  1014. void *hdr;
  1015. struct nlattr *attrs;
  1016. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1017. NLM_F_MULTI, TIPC_NL_NODE_GET);
  1018. if (!hdr)
  1019. return -EMSGSIZE;
  1020. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  1021. if (!attrs)
  1022. goto msg_full;
  1023. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  1024. goto attr_msg_full;
  1025. if (tipc_node_is_up(node))
  1026. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  1027. goto attr_msg_full;
  1028. nla_nest_end(msg->skb, attrs);
  1029. genlmsg_end(msg->skb, hdr);
  1030. return 0;
  1031. attr_msg_full:
  1032. nla_nest_cancel(msg->skb, attrs);
  1033. msg_full:
  1034. genlmsg_cancel(msg->skb, hdr);
  1035. return -EMSGSIZE;
  1036. }
  1037. /**
  1038. * tipc_node_xmit() is the general link level function for message sending
  1039. * @net: the applicable net namespace
  1040. * @list: chain of buffers containing message
  1041. * @dnode: address of destination node
  1042. * @selector: a number used for deterministic link selection
  1043. * Consumes the buffer chain, except when returning -ELINKCONG
  1044. * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  1045. */
  1046. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  1047. u32 dnode, int selector)
  1048. {
  1049. struct tipc_link_entry *le = NULL;
  1050. struct tipc_node *n;
  1051. struct sk_buff_head xmitq;
  1052. int bearer_id;
  1053. int rc;
  1054. if (in_own_node(net, dnode)) {
  1055. tipc_sk_rcv(net, list);
  1056. return 0;
  1057. }
  1058. n = tipc_node_find(net, dnode);
  1059. if (unlikely(!n)) {
  1060. skb_queue_purge(list);
  1061. return -EHOSTUNREACH;
  1062. }
  1063. tipc_node_read_lock(n);
  1064. bearer_id = n->active_links[selector & 1];
  1065. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1066. tipc_node_read_unlock(n);
  1067. tipc_node_put(n);
  1068. skb_queue_purge(list);
  1069. return -EHOSTUNREACH;
  1070. }
  1071. __skb_queue_head_init(&xmitq);
  1072. le = &n->links[bearer_id];
  1073. spin_lock_bh(&le->lock);
  1074. rc = tipc_link_xmit(le->link, list, &xmitq);
  1075. spin_unlock_bh(&le->lock);
  1076. tipc_node_read_unlock(n);
  1077. if (likely(rc == 0))
  1078. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1079. else if (rc == -ENOBUFS)
  1080. tipc_node_link_down(n, bearer_id, false);
  1081. tipc_node_put(n);
  1082. return rc;
  1083. }
  1084. /* tipc_node_xmit_skb(): send single buffer to destination
  1085. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  1086. * messages, which will not be rejected
  1087. * The only exception is datagram messages rerouted after secondary
  1088. * lookup, which are rare and safe to dispose of anyway.
  1089. * TODO: Return real return value, and let callers use
  1090. * tipc_wait_for_sendpkt() where applicable
  1091. */
  1092. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1093. u32 selector)
  1094. {
  1095. struct sk_buff_head head;
  1096. int rc;
  1097. skb_queue_head_init(&head);
  1098. __skb_queue_tail(&head, skb);
  1099. rc = tipc_node_xmit(net, &head, dnode, selector);
  1100. if (rc == -ELINKCONG)
  1101. kfree_skb(skb);
  1102. return 0;
  1103. }
  1104. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  1105. {
  1106. struct sk_buff *txskb;
  1107. struct tipc_node *n;
  1108. u32 dst;
  1109. rcu_read_lock();
  1110. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1111. dst = n->addr;
  1112. if (in_own_node(net, dst))
  1113. continue;
  1114. if (!tipc_node_is_up(n))
  1115. continue;
  1116. txskb = pskb_copy(skb, GFP_ATOMIC);
  1117. if (!txskb)
  1118. break;
  1119. msg_set_destnode(buf_msg(txskb), dst);
  1120. tipc_node_xmit_skb(net, txskb, dst, 0);
  1121. }
  1122. rcu_read_unlock();
  1123. kfree_skb(skb);
  1124. }
  1125. /**
  1126. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1127. * @net: the applicable net namespace
  1128. * @skb: TIPC packet
  1129. * @bearer_id: id of bearer message arrived on
  1130. *
  1131. * Invoked with no locks held.
  1132. */
  1133. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1134. {
  1135. int rc;
  1136. struct sk_buff_head xmitq;
  1137. struct tipc_bclink_entry *be;
  1138. struct tipc_link_entry *le;
  1139. struct tipc_msg *hdr = buf_msg(skb);
  1140. int usr = msg_user(hdr);
  1141. u32 dnode = msg_destnode(hdr);
  1142. struct tipc_node *n;
  1143. __skb_queue_head_init(&xmitq);
  1144. /* If NACK for other node, let rcv link for that node peek into it */
  1145. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1146. n = tipc_node_find(net, dnode);
  1147. else
  1148. n = tipc_node_find(net, msg_prevnode(hdr));
  1149. if (!n) {
  1150. kfree_skb(skb);
  1151. return;
  1152. }
  1153. be = &n->bc_entry;
  1154. le = &n->links[bearer_id];
  1155. rc = tipc_bcast_rcv(net, be->link, skb);
  1156. /* Broadcast ACKs are sent on a unicast link */
  1157. if (rc & TIPC_LINK_SND_BC_ACK) {
  1158. tipc_node_read_lock(n);
  1159. tipc_link_build_state_msg(le->link, &xmitq);
  1160. tipc_node_read_unlock(n);
  1161. }
  1162. if (!skb_queue_empty(&xmitq))
  1163. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1164. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1165. if (!skb_queue_empty(&be->inputq1)) {
  1166. spin_lock_bh(&be->inputq2.lock);
  1167. spin_lock_bh(&be->inputq1.lock);
  1168. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1169. spin_unlock_bh(&be->inputq1.lock);
  1170. spin_unlock_bh(&be->inputq2.lock);
  1171. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1172. }
  1173. if (rc & TIPC_LINK_DOWN_EVT) {
  1174. /* Reception reassembly failure => reset all links to peer */
  1175. if (!tipc_link_is_up(be->link))
  1176. tipc_node_reset_links(n);
  1177. /* Retransmission failure => reset all links to all peers */
  1178. if (!tipc_link_is_up(tipc_bc_sndlink(net)))
  1179. tipc_bearer_reset_all(net);
  1180. }
  1181. tipc_node_put(n);
  1182. }
  1183. /**
  1184. * tipc_node_check_state - check and if necessary update node state
  1185. * @skb: TIPC packet
  1186. * @bearer_id: identity of bearer delivering the packet
  1187. * Returns true if state is ok, otherwise consumes buffer and returns false
  1188. */
  1189. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1190. int bearer_id, struct sk_buff_head *xmitq)
  1191. {
  1192. struct tipc_msg *hdr = buf_msg(skb);
  1193. int usr = msg_user(hdr);
  1194. int mtyp = msg_type(hdr);
  1195. u16 oseqno = msg_seqno(hdr);
  1196. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1197. u16 exp_pkts = msg_msgcnt(hdr);
  1198. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1199. int state = n->state;
  1200. struct tipc_link *l, *tnl, *pl = NULL;
  1201. struct tipc_media_addr *maddr;
  1202. int pb_id;
  1203. l = n->links[bearer_id].link;
  1204. if (!l)
  1205. return false;
  1206. rcv_nxt = tipc_link_rcv_nxt(l);
  1207. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1208. return true;
  1209. /* Find parallel link, if any */
  1210. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1211. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1212. pl = n->links[pb_id].link;
  1213. break;
  1214. }
  1215. }
  1216. /* Check and update node accesibility if applicable */
  1217. if (state == SELF_UP_PEER_COMING) {
  1218. if (!tipc_link_is_up(l))
  1219. return true;
  1220. if (!msg_peer_link_is_up(hdr))
  1221. return true;
  1222. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1223. }
  1224. if (state == SELF_DOWN_PEER_LEAVING) {
  1225. if (msg_peer_node_is_up(hdr))
  1226. return false;
  1227. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1228. return true;
  1229. }
  1230. if (state == SELF_LEAVING_PEER_DOWN)
  1231. return false;
  1232. /* Ignore duplicate packets */
  1233. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1234. return true;
  1235. /* Initiate or update failover mode if applicable */
  1236. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1237. syncpt = oseqno + exp_pkts - 1;
  1238. if (pl && tipc_link_is_up(pl)) {
  1239. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1240. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1241. tipc_link_inputq(l));
  1242. }
  1243. /* If pkts arrive out of order, use lowest calculated syncpt */
  1244. if (less(syncpt, n->sync_point))
  1245. n->sync_point = syncpt;
  1246. }
  1247. /* Open parallel link when tunnel link reaches synch point */
  1248. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1249. if (!more(rcv_nxt, n->sync_point))
  1250. return true;
  1251. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1252. if (pl)
  1253. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1254. return true;
  1255. }
  1256. /* No synching needed if only one link */
  1257. if (!pl || !tipc_link_is_up(pl))
  1258. return true;
  1259. /* Initiate synch mode if applicable */
  1260. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1261. syncpt = iseqno + exp_pkts - 1;
  1262. if (!tipc_link_is_up(l)) {
  1263. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1264. __tipc_node_link_up(n, bearer_id, xmitq);
  1265. }
  1266. if (n->state == SELF_UP_PEER_UP) {
  1267. n->sync_point = syncpt;
  1268. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1269. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1270. }
  1271. }
  1272. /* Open tunnel link when parallel link reaches synch point */
  1273. if (n->state == NODE_SYNCHING) {
  1274. if (tipc_link_is_synching(l)) {
  1275. tnl = l;
  1276. } else {
  1277. tnl = pl;
  1278. pl = l;
  1279. }
  1280. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1281. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1282. if (more(dlv_nxt, n->sync_point)) {
  1283. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1284. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1285. return true;
  1286. }
  1287. if (l == pl)
  1288. return true;
  1289. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1290. return true;
  1291. if (usr == LINK_PROTOCOL)
  1292. return true;
  1293. return false;
  1294. }
  1295. return true;
  1296. }
  1297. /**
  1298. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1299. * @net: the applicable net namespace
  1300. * @skb: TIPC packet
  1301. * @bearer: pointer to bearer message arrived on
  1302. *
  1303. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1304. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1305. */
  1306. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1307. {
  1308. struct sk_buff_head xmitq;
  1309. struct tipc_node *n;
  1310. struct tipc_msg *hdr = buf_msg(skb);
  1311. int usr = msg_user(hdr);
  1312. int bearer_id = b->identity;
  1313. struct tipc_link_entry *le;
  1314. u16 bc_ack = msg_bcast_ack(hdr);
  1315. u32 self = tipc_own_addr(net);
  1316. int rc = 0;
  1317. __skb_queue_head_init(&xmitq);
  1318. /* Ensure message is well-formed */
  1319. if (unlikely(!tipc_msg_validate(skb)))
  1320. goto discard;
  1321. /* Handle arrival of discovery or broadcast packet */
  1322. if (unlikely(msg_non_seq(hdr))) {
  1323. if (unlikely(usr == LINK_CONFIG))
  1324. return tipc_disc_rcv(net, skb, b);
  1325. else
  1326. return tipc_node_bc_rcv(net, skb, bearer_id);
  1327. }
  1328. /* Discard unicast link messages destined for another node */
  1329. if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
  1330. goto discard;
  1331. /* Locate neighboring node that sent packet */
  1332. n = tipc_node_find(net, msg_prevnode(hdr));
  1333. if (unlikely(!n))
  1334. goto discard;
  1335. le = &n->links[bearer_id];
  1336. /* Ensure broadcast reception is in synch with peer's send state */
  1337. if (unlikely(usr == LINK_PROTOCOL))
  1338. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1339. else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
  1340. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1341. /* Receive packet directly if conditions permit */
  1342. tipc_node_read_lock(n);
  1343. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1344. spin_lock_bh(&le->lock);
  1345. if (le->link) {
  1346. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1347. skb = NULL;
  1348. }
  1349. spin_unlock_bh(&le->lock);
  1350. }
  1351. tipc_node_read_unlock(n);
  1352. /* Check/update node state before receiving */
  1353. if (unlikely(skb)) {
  1354. tipc_node_write_lock(n);
  1355. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1356. if (le->link) {
  1357. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1358. skb = NULL;
  1359. }
  1360. }
  1361. tipc_node_write_unlock(n);
  1362. }
  1363. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1364. tipc_node_link_up(n, bearer_id, &xmitq);
  1365. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1366. tipc_node_link_down(n, bearer_id, false);
  1367. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1368. tipc_named_rcv(net, &n->bc_entry.namedq);
  1369. if (!skb_queue_empty(&le->inputq))
  1370. tipc_sk_rcv(net, &le->inputq);
  1371. if (!skb_queue_empty(&xmitq))
  1372. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1373. tipc_node_put(n);
  1374. discard:
  1375. kfree_skb(skb);
  1376. }
  1377. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1378. {
  1379. int err;
  1380. struct net *net = sock_net(skb->sk);
  1381. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1382. int done = cb->args[0];
  1383. int last_addr = cb->args[1];
  1384. struct tipc_node *node;
  1385. struct tipc_nl_msg msg;
  1386. if (done)
  1387. return 0;
  1388. msg.skb = skb;
  1389. msg.portid = NETLINK_CB(cb->skb).portid;
  1390. msg.seq = cb->nlh->nlmsg_seq;
  1391. rcu_read_lock();
  1392. if (last_addr) {
  1393. node = tipc_node_find(net, last_addr);
  1394. if (!node) {
  1395. rcu_read_unlock();
  1396. /* We never set seq or call nl_dump_check_consistent()
  1397. * this means that setting prev_seq here will cause the
  1398. * consistence check to fail in the netlink callback
  1399. * handler. Resulting in the NLMSG_DONE message having
  1400. * the NLM_F_DUMP_INTR flag set if the node state
  1401. * changed while we released the lock.
  1402. */
  1403. cb->prev_seq = 1;
  1404. return -EPIPE;
  1405. }
  1406. tipc_node_put(node);
  1407. }
  1408. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1409. if (last_addr) {
  1410. if (node->addr == last_addr)
  1411. last_addr = 0;
  1412. else
  1413. continue;
  1414. }
  1415. tipc_node_read_lock(node);
  1416. err = __tipc_nl_add_node(&msg, node);
  1417. if (err) {
  1418. last_addr = node->addr;
  1419. tipc_node_read_unlock(node);
  1420. goto out;
  1421. }
  1422. tipc_node_read_unlock(node);
  1423. }
  1424. done = 1;
  1425. out:
  1426. cb->args[0] = done;
  1427. cb->args[1] = last_addr;
  1428. rcu_read_unlock();
  1429. return skb->len;
  1430. }
  1431. /* tipc_node_find_by_name - locate owner node of link by link's name
  1432. * @net: the applicable net namespace
  1433. * @name: pointer to link name string
  1434. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1435. *
  1436. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1437. */
  1438. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  1439. const char *link_name,
  1440. unsigned int *bearer_id)
  1441. {
  1442. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1443. struct tipc_link *l;
  1444. struct tipc_node *n;
  1445. struct tipc_node *found_node = NULL;
  1446. int i;
  1447. *bearer_id = 0;
  1448. rcu_read_lock();
  1449. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1450. tipc_node_read_lock(n);
  1451. for (i = 0; i < MAX_BEARERS; i++) {
  1452. l = n->links[i].link;
  1453. if (l && !strcmp(tipc_link_name(l), link_name)) {
  1454. *bearer_id = i;
  1455. found_node = n;
  1456. break;
  1457. }
  1458. }
  1459. tipc_node_read_unlock(n);
  1460. if (found_node)
  1461. break;
  1462. }
  1463. rcu_read_unlock();
  1464. return found_node;
  1465. }
  1466. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  1467. {
  1468. int err;
  1469. int res = 0;
  1470. int bearer_id;
  1471. char *name;
  1472. struct tipc_link *link;
  1473. struct tipc_node *node;
  1474. struct sk_buff_head xmitq;
  1475. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1476. struct net *net = sock_net(skb->sk);
  1477. __skb_queue_head_init(&xmitq);
  1478. if (!info->attrs[TIPC_NLA_LINK])
  1479. return -EINVAL;
  1480. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1481. info->attrs[TIPC_NLA_LINK],
  1482. tipc_nl_link_policy);
  1483. if (err)
  1484. return err;
  1485. if (!attrs[TIPC_NLA_LINK_NAME])
  1486. return -EINVAL;
  1487. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1488. if (strcmp(name, tipc_bclink_name) == 0)
  1489. return tipc_nl_bc_link_set(net, attrs);
  1490. node = tipc_node_find_by_name(net, name, &bearer_id);
  1491. if (!node)
  1492. return -EINVAL;
  1493. tipc_node_read_lock(node);
  1494. link = node->links[bearer_id].link;
  1495. if (!link) {
  1496. res = -EINVAL;
  1497. goto out;
  1498. }
  1499. if (attrs[TIPC_NLA_LINK_PROP]) {
  1500. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1501. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1502. props);
  1503. if (err) {
  1504. res = err;
  1505. goto out;
  1506. }
  1507. if (props[TIPC_NLA_PROP_TOL]) {
  1508. u32 tol;
  1509. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1510. tipc_link_set_tolerance(link, tol, &xmitq);
  1511. }
  1512. if (props[TIPC_NLA_PROP_PRIO]) {
  1513. u32 prio;
  1514. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1515. tipc_link_set_prio(link, prio, &xmitq);
  1516. }
  1517. if (props[TIPC_NLA_PROP_WIN]) {
  1518. u32 win;
  1519. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1520. tipc_link_set_queue_limits(link, win);
  1521. }
  1522. }
  1523. out:
  1524. tipc_node_read_unlock(node);
  1525. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
  1526. return res;
  1527. }
  1528. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  1529. {
  1530. struct net *net = genl_info_net(info);
  1531. struct tipc_nl_msg msg;
  1532. char *name;
  1533. int err;
  1534. msg.portid = info->snd_portid;
  1535. msg.seq = info->snd_seq;
  1536. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1537. return -EINVAL;
  1538. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1539. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1540. if (!msg.skb)
  1541. return -ENOMEM;
  1542. if (strcmp(name, tipc_bclink_name) == 0) {
  1543. err = tipc_nl_add_bc_link(net, &msg);
  1544. if (err) {
  1545. nlmsg_free(msg.skb);
  1546. return err;
  1547. }
  1548. } else {
  1549. int bearer_id;
  1550. struct tipc_node *node;
  1551. struct tipc_link *link;
  1552. node = tipc_node_find_by_name(net, name, &bearer_id);
  1553. if (!node)
  1554. return -EINVAL;
  1555. tipc_node_read_lock(node);
  1556. link = node->links[bearer_id].link;
  1557. if (!link) {
  1558. tipc_node_read_unlock(node);
  1559. nlmsg_free(msg.skb);
  1560. return -EINVAL;
  1561. }
  1562. err = __tipc_nl_add_link(net, &msg, link, 0);
  1563. tipc_node_read_unlock(node);
  1564. if (err) {
  1565. nlmsg_free(msg.skb);
  1566. return err;
  1567. }
  1568. }
  1569. return genlmsg_reply(msg.skb, info);
  1570. }
  1571. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  1572. {
  1573. int err;
  1574. char *link_name;
  1575. unsigned int bearer_id;
  1576. struct tipc_link *link;
  1577. struct tipc_node *node;
  1578. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1579. struct net *net = sock_net(skb->sk);
  1580. struct tipc_link_entry *le;
  1581. if (!info->attrs[TIPC_NLA_LINK])
  1582. return -EINVAL;
  1583. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1584. info->attrs[TIPC_NLA_LINK],
  1585. tipc_nl_link_policy);
  1586. if (err)
  1587. return err;
  1588. if (!attrs[TIPC_NLA_LINK_NAME])
  1589. return -EINVAL;
  1590. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1591. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1592. err = tipc_bclink_reset_stats(net);
  1593. if (err)
  1594. return err;
  1595. return 0;
  1596. }
  1597. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  1598. if (!node)
  1599. return -EINVAL;
  1600. le = &node->links[bearer_id];
  1601. tipc_node_read_lock(node);
  1602. spin_lock_bh(&le->lock);
  1603. link = node->links[bearer_id].link;
  1604. if (!link) {
  1605. spin_unlock_bh(&le->lock);
  1606. tipc_node_read_unlock(node);
  1607. return -EINVAL;
  1608. }
  1609. tipc_link_reset_stats(link);
  1610. spin_unlock_bh(&le->lock);
  1611. tipc_node_read_unlock(node);
  1612. return 0;
  1613. }
  1614. /* Caller should hold node lock */
  1615. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1616. struct tipc_node *node, u32 *prev_link)
  1617. {
  1618. u32 i;
  1619. int err;
  1620. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1621. *prev_link = i;
  1622. if (!node->links[i].link)
  1623. continue;
  1624. err = __tipc_nl_add_link(net, msg,
  1625. node->links[i].link, NLM_F_MULTI);
  1626. if (err)
  1627. return err;
  1628. }
  1629. *prev_link = 0;
  1630. return 0;
  1631. }
  1632. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  1633. {
  1634. struct net *net = sock_net(skb->sk);
  1635. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1636. struct tipc_node *node;
  1637. struct tipc_nl_msg msg;
  1638. u32 prev_node = cb->args[0];
  1639. u32 prev_link = cb->args[1];
  1640. int done = cb->args[2];
  1641. int err;
  1642. if (done)
  1643. return 0;
  1644. msg.skb = skb;
  1645. msg.portid = NETLINK_CB(cb->skb).portid;
  1646. msg.seq = cb->nlh->nlmsg_seq;
  1647. rcu_read_lock();
  1648. if (prev_node) {
  1649. node = tipc_node_find(net, prev_node);
  1650. if (!node) {
  1651. /* We never set seq or call nl_dump_check_consistent()
  1652. * this means that setting prev_seq here will cause the
  1653. * consistence check to fail in the netlink callback
  1654. * handler. Resulting in the last NLMSG_DONE message
  1655. * having the NLM_F_DUMP_INTR flag set.
  1656. */
  1657. cb->prev_seq = 1;
  1658. goto out;
  1659. }
  1660. tipc_node_put(node);
  1661. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1662. list) {
  1663. tipc_node_read_lock(node);
  1664. err = __tipc_nl_add_node_links(net, &msg, node,
  1665. &prev_link);
  1666. tipc_node_read_unlock(node);
  1667. if (err)
  1668. goto out;
  1669. prev_node = node->addr;
  1670. }
  1671. } else {
  1672. err = tipc_nl_add_bc_link(net, &msg);
  1673. if (err)
  1674. goto out;
  1675. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1676. tipc_node_read_lock(node);
  1677. err = __tipc_nl_add_node_links(net, &msg, node,
  1678. &prev_link);
  1679. tipc_node_read_unlock(node);
  1680. if (err)
  1681. goto out;
  1682. prev_node = node->addr;
  1683. }
  1684. }
  1685. done = 1;
  1686. out:
  1687. rcu_read_unlock();
  1688. cb->args[0] = prev_node;
  1689. cb->args[1] = prev_link;
  1690. cb->args[2] = done;
  1691. return skb->len;
  1692. }