node.c 48 KB

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