node.c 48 KB

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