node.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * net/tipc/node.c: TIPC node management routines
  3. *
  4. * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
  5. * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "link.h"
  38. #include "node.h"
  39. #include "name_distr.h"
  40. #include "socket.h"
  41. #include "bcast.h"
  42. #include "monitor.h"
  43. #include "discover.h"
  44. #include "netlink.h"
  45. #define INVALID_NODE_SIG 0x10000
  46. /* Flags used to take different actions according to flag type
  47. * TIPC_NOTIFY_NODE_DOWN: notify node is down
  48. * TIPC_NOTIFY_NODE_UP: notify node is up
  49. * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
  50. */
  51. enum {
  52. TIPC_NOTIFY_NODE_DOWN = (1 << 3),
  53. TIPC_NOTIFY_NODE_UP = (1 << 4),
  54. TIPC_NOTIFY_LINK_UP = (1 << 6),
  55. TIPC_NOTIFY_LINK_DOWN = (1 << 7)
  56. };
  57. struct tipc_link_entry {
  58. struct tipc_link *link;
  59. spinlock_t lock; /* per link */
  60. u32 mtu;
  61. struct sk_buff_head inputq;
  62. struct tipc_media_addr maddr;
  63. };
  64. struct tipc_bclink_entry {
  65. struct tipc_link *link;
  66. struct sk_buff_head inputq1;
  67. struct sk_buff_head arrvq;
  68. struct sk_buff_head inputq2;
  69. struct sk_buff_head namedq;
  70. };
  71. /**
  72. * struct tipc_node - TIPC node structure
  73. * @addr: network address of node
  74. * @ref: reference counter to node object
  75. * @lock: rwlock governing access to structure
  76. * @net: the applicable net namespace
  77. * @hash: links to adjacent nodes in unsorted hash chain
  78. * @inputq: pointer to input queue containing messages for msg event
  79. * @namedq: pointer to name table input queue with name table messages
  80. * @active_links: bearer ids of active links, used as index into links[] array
  81. * @links: array containing references to all links to node
  82. * @action_flags: bit mask of different types of node actions
  83. * @state: connectivity state vs peer node
  84. * @sync_point: sequence number where synch/failover is finished
  85. * @list: links to adjacent nodes in sorted list of cluster's nodes
  86. * @working_links: number of working links to node (both active and standby)
  87. * @link_cnt: number of links to node
  88. * @capabilities: bitmap, indicating peer node's functional capabilities
  89. * @signature: node instance identifier
  90. * @link_id: local and remote bearer ids of changing link, if any
  91. * @publ_list: list of publications
  92. * @rcu: rcu struct for tipc_node
  93. */
  94. struct tipc_node {
  95. u32 addr;
  96. struct kref kref;
  97. rwlock_t lock;
  98. struct net *net;
  99. struct hlist_node hash;
  100. int active_links[2];
  101. struct tipc_link_entry links[MAX_BEARERS];
  102. struct tipc_bclink_entry bc_entry;
  103. int action_flags;
  104. struct list_head list;
  105. int state;
  106. u16 sync_point;
  107. int link_cnt;
  108. u16 working_links;
  109. u16 capabilities;
  110. u32 signature;
  111. u32 link_id;
  112. struct list_head publ_list;
  113. struct list_head conn_sks;
  114. unsigned long keepalive_intv;
  115. struct timer_list timer;
  116. struct rcu_head rcu;
  117. };
  118. /* Node FSM states and events:
  119. */
  120. enum {
  121. SELF_DOWN_PEER_DOWN = 0xdd,
  122. SELF_UP_PEER_UP = 0xaa,
  123. SELF_DOWN_PEER_LEAVING = 0xd1,
  124. SELF_UP_PEER_COMING = 0xac,
  125. SELF_COMING_PEER_UP = 0xca,
  126. SELF_LEAVING_PEER_DOWN = 0x1d,
  127. NODE_FAILINGOVER = 0xf0,
  128. NODE_SYNCHING = 0xcc
  129. };
  130. enum {
  131. SELF_ESTABL_CONTACT_EVT = 0xece,
  132. SELF_LOST_CONTACT_EVT = 0x1ce,
  133. PEER_ESTABL_CONTACT_EVT = 0x9ece,
  134. PEER_LOST_CONTACT_EVT = 0x91ce,
  135. NODE_FAILOVER_BEGIN_EVT = 0xfbe,
  136. NODE_FAILOVER_END_EVT = 0xfee,
  137. NODE_SYNCH_BEGIN_EVT = 0xcbe,
  138. NODE_SYNCH_END_EVT = 0xcee
  139. };
  140. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  141. struct sk_buff_head *xmitq,
  142. struct tipc_media_addr **maddr);
  143. static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
  144. bool delete);
  145. static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
  146. static void tipc_node_delete(struct tipc_node *node);
  147. static void tipc_node_timeout(unsigned long data);
  148. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  149. static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
  150. static void tipc_node_put(struct tipc_node *node);
  151. static bool tipc_node_is_up(struct tipc_node *n);
  152. struct tipc_sock_conn {
  153. u32 port;
  154. u32 peer_port;
  155. u32 peer_node;
  156. struct list_head list;
  157. };
  158. static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
  159. {
  160. int bearer_id = n->active_links[sel & 1];
  161. if (unlikely(bearer_id == INVALID_BEARER_ID))
  162. return NULL;
  163. return n->links[bearer_id].link;
  164. }
  165. int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
  166. {
  167. struct tipc_node *n;
  168. int bearer_id;
  169. unsigned int mtu = MAX_MSG_SIZE;
  170. n = tipc_node_find(net, addr);
  171. if (unlikely(!n))
  172. return mtu;
  173. bearer_id = n->active_links[sel & 1];
  174. if (likely(bearer_id != INVALID_BEARER_ID))
  175. mtu = n->links[bearer_id].mtu;
  176. tipc_node_put(n);
  177. return mtu;
  178. }
  179. u16 tipc_node_get_capabilities(struct net *net, u32 addr)
  180. {
  181. struct tipc_node *n;
  182. u16 caps;
  183. n = tipc_node_find(net, addr);
  184. if (unlikely(!n))
  185. return TIPC_NODE_CAPABILITIES;
  186. caps = n->capabilities;
  187. tipc_node_put(n);
  188. return caps;
  189. }
  190. static void tipc_node_kref_release(struct kref *kref)
  191. {
  192. struct tipc_node *n = container_of(kref, struct tipc_node, kref);
  193. kfree(n->bc_entry.link);
  194. kfree_rcu(n, rcu);
  195. }
  196. static void tipc_node_put(struct tipc_node *node)
  197. {
  198. kref_put(&node->kref, tipc_node_kref_release);
  199. }
  200. static void tipc_node_get(struct tipc_node *node)
  201. {
  202. kref_get(&node->kref);
  203. }
  204. /*
  205. * tipc_node_find - locate specified node object, if it exists
  206. */
  207. static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  208. {
  209. struct tipc_net *tn = tipc_net(net);
  210. struct tipc_node *node;
  211. unsigned int thash = tipc_hashfn(addr);
  212. if (unlikely(!in_own_cluster_exact(net, addr)))
  213. return NULL;
  214. rcu_read_lock();
  215. hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
  216. if (node->addr != addr)
  217. continue;
  218. if (!kref_get_unless_zero(&node->kref))
  219. node = NULL;
  220. break;
  221. }
  222. rcu_read_unlock();
  223. return node;
  224. }
  225. static void tipc_node_read_lock(struct tipc_node *n)
  226. {
  227. read_lock_bh(&n->lock);
  228. }
  229. static void tipc_node_read_unlock(struct tipc_node *n)
  230. {
  231. read_unlock_bh(&n->lock);
  232. }
  233. static void tipc_node_write_lock(struct tipc_node *n)
  234. {
  235. write_lock_bh(&n->lock);
  236. }
  237. static void tipc_node_write_unlock(struct tipc_node *n)
  238. {
  239. struct net *net = n->net;
  240. u32 addr = 0;
  241. u32 flags = n->action_flags;
  242. u32 link_id = 0;
  243. u32 bearer_id;
  244. struct list_head *publ_list;
  245. if (likely(!flags)) {
  246. write_unlock_bh(&n->lock);
  247. return;
  248. }
  249. addr = n->addr;
  250. link_id = n->link_id;
  251. bearer_id = link_id & 0xffff;
  252. publ_list = &n->publ_list;
  253. n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  254. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
  255. write_unlock_bh(&n->lock);
  256. if (flags & TIPC_NOTIFY_NODE_DOWN)
  257. tipc_publ_notify(net, publ_list, addr);
  258. if (flags & TIPC_NOTIFY_NODE_UP)
  259. tipc_named_node_up(net, addr);
  260. if (flags & TIPC_NOTIFY_LINK_UP) {
  261. tipc_mon_peer_up(net, addr, bearer_id);
  262. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  263. TIPC_NODE_SCOPE, link_id, addr);
  264. }
  265. if (flags & TIPC_NOTIFY_LINK_DOWN) {
  266. tipc_mon_peer_down(net, addr, bearer_id);
  267. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  268. link_id, addr);
  269. }
  270. }
  271. struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
  272. {
  273. struct tipc_net *tn = net_generic(net, tipc_net_id);
  274. struct tipc_node *n, *temp_node;
  275. int i;
  276. spin_lock_bh(&tn->node_list_lock);
  277. n = tipc_node_find(net, addr);
  278. if (n) {
  279. /* Same node may come back with new capabilities */
  280. n->capabilities = capabilities;
  281. goto exit;
  282. }
  283. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  284. if (!n) {
  285. pr_warn("Node creation failed, no memory\n");
  286. goto exit;
  287. }
  288. n->addr = addr;
  289. n->net = net;
  290. n->capabilities = capabilities;
  291. kref_init(&n->kref);
  292. rwlock_init(&n->lock);
  293. INIT_HLIST_NODE(&n->hash);
  294. INIT_LIST_HEAD(&n->list);
  295. INIT_LIST_HEAD(&n->publ_list);
  296. INIT_LIST_HEAD(&n->conn_sks);
  297. skb_queue_head_init(&n->bc_entry.namedq);
  298. skb_queue_head_init(&n->bc_entry.inputq1);
  299. __skb_queue_head_init(&n->bc_entry.arrvq);
  300. skb_queue_head_init(&n->bc_entry.inputq2);
  301. for (i = 0; i < MAX_BEARERS; i++)
  302. spin_lock_init(&n->links[i].lock);
  303. n->state = SELF_DOWN_PEER_LEAVING;
  304. n->signature = INVALID_NODE_SIG;
  305. n->active_links[0] = INVALID_BEARER_ID;
  306. n->active_links[1] = INVALID_BEARER_ID;
  307. if (!tipc_link_bc_create(net, tipc_own_addr(net), n->addr,
  308. U16_MAX,
  309. tipc_link_window(tipc_bc_sndlink(net)),
  310. n->capabilities,
  311. &n->bc_entry.inputq1,
  312. &n->bc_entry.namedq,
  313. tipc_bc_sndlink(net),
  314. &n->bc_entry.link)) {
  315. pr_warn("Broadcast rcv link creation failed, no memory\n");
  316. kfree(n);
  317. n = NULL;
  318. goto exit;
  319. }
  320. tipc_node_get(n);
  321. setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
  322. n->keepalive_intv = U32_MAX;
  323. hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
  324. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  325. if (n->addr < temp_node->addr)
  326. break;
  327. }
  328. list_add_tail_rcu(&n->list, &temp_node->list);
  329. exit:
  330. spin_unlock_bh(&tn->node_list_lock);
  331. return n;
  332. }
  333. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  334. {
  335. unsigned long tol = tipc_link_tolerance(l);
  336. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  337. /* Link with lowest tolerance determines timer interval */
  338. if (intv < n->keepalive_intv)
  339. n->keepalive_intv = intv;
  340. /* Ensure link's abort limit corresponds to current tolerance */
  341. tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
  342. }
  343. static void tipc_node_delete(struct tipc_node *node)
  344. {
  345. list_del_rcu(&node->list);
  346. hlist_del_rcu(&node->hash);
  347. tipc_node_put(node);
  348. del_timer_sync(&node->timer);
  349. tipc_node_put(node);
  350. }
  351. void tipc_node_stop(struct net *net)
  352. {
  353. struct tipc_net *tn = tipc_net(net);
  354. struct tipc_node *node, *t_node;
  355. spin_lock_bh(&tn->node_list_lock);
  356. list_for_each_entry_safe(node, t_node, &tn->node_list, list)
  357. tipc_node_delete(node);
  358. spin_unlock_bh(&tn->node_list_lock);
  359. }
  360. void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
  361. {
  362. struct tipc_node *n;
  363. if (in_own_node(net, addr))
  364. return;
  365. n = tipc_node_find(net, addr);
  366. if (!n) {
  367. pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
  368. return;
  369. }
  370. tipc_node_write_lock(n);
  371. list_add_tail(subscr, &n->publ_list);
  372. tipc_node_write_unlock(n);
  373. tipc_node_put(n);
  374. }
  375. void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
  376. {
  377. struct tipc_node *n;
  378. if (in_own_node(net, addr))
  379. return;
  380. n = tipc_node_find(net, addr);
  381. if (!n) {
  382. pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
  383. return;
  384. }
  385. tipc_node_write_lock(n);
  386. list_del_init(subscr);
  387. tipc_node_write_unlock(n);
  388. tipc_node_put(n);
  389. }
  390. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  391. {
  392. struct tipc_node *node;
  393. struct tipc_sock_conn *conn;
  394. int err = 0;
  395. if (in_own_node(net, dnode))
  396. return 0;
  397. node = tipc_node_find(net, dnode);
  398. if (!node) {
  399. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  400. return -EHOSTUNREACH;
  401. }
  402. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  403. if (!conn) {
  404. err = -EHOSTUNREACH;
  405. goto exit;
  406. }
  407. conn->peer_node = dnode;
  408. conn->port = port;
  409. conn->peer_port = peer_port;
  410. tipc_node_write_lock(node);
  411. list_add_tail(&conn->list, &node->conn_sks);
  412. tipc_node_write_unlock(node);
  413. exit:
  414. tipc_node_put(node);
  415. return err;
  416. }
  417. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  418. {
  419. struct tipc_node *node;
  420. struct tipc_sock_conn *conn, *safe;
  421. if (in_own_node(net, dnode))
  422. return;
  423. node = tipc_node_find(net, dnode);
  424. if (!node)
  425. return;
  426. tipc_node_write_lock(node);
  427. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  428. if (port != conn->port)
  429. continue;
  430. list_del(&conn->list);
  431. kfree(conn);
  432. }
  433. tipc_node_write_unlock(node);
  434. tipc_node_put(node);
  435. }
  436. /* tipc_node_timeout - handle expiration of node timer
  437. */
  438. static void tipc_node_timeout(unsigned long data)
  439. {
  440. struct tipc_node *n = (struct tipc_node *)data;
  441. struct tipc_link_entry *le;
  442. struct sk_buff_head xmitq;
  443. int bearer_id;
  444. int rc = 0;
  445. __skb_queue_head_init(&xmitq);
  446. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  447. tipc_node_read_lock(n);
  448. le = &n->links[bearer_id];
  449. spin_lock_bh(&le->lock);
  450. if (le->link) {
  451. /* Link tolerance may change asynchronously: */
  452. tipc_node_calculate_timer(n, le->link);
  453. rc = tipc_link_timeout(le->link, &xmitq);
  454. }
  455. spin_unlock_bh(&le->lock);
  456. tipc_node_read_unlock(n);
  457. tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
  458. if (rc & TIPC_LINK_DOWN_EVT)
  459. tipc_node_link_down(n, bearer_id, false);
  460. }
  461. mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
  462. }
  463. /**
  464. * __tipc_node_link_up - handle addition of link
  465. * Node lock must be held by caller
  466. * Link becomes active (alone or shared) or standby, depending on its priority.
  467. */
  468. static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
  469. struct sk_buff_head *xmitq)
  470. {
  471. int *slot0 = &n->active_links[0];
  472. int *slot1 = &n->active_links[1];
  473. struct tipc_link *ol = node_active_link(n, 0);
  474. struct tipc_link *nl = n->links[bearer_id].link;
  475. if (!nl || tipc_link_is_up(nl))
  476. return;
  477. tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
  478. if (!tipc_link_is_up(nl))
  479. return;
  480. n->working_links++;
  481. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  482. n->link_id = tipc_link_id(nl);
  483. /* Leave room for tunnel header when returning 'mtu' to users: */
  484. n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
  485. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  486. tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
  487. pr_debug("Established link <%s> on network plane %c\n",
  488. tipc_link_name(nl), tipc_link_plane(nl));
  489. /* Ensure that a STATE message goes first */
  490. tipc_link_build_state_msg(nl, xmitq);
  491. /* First link? => give it both slots */
  492. if (!ol) {
  493. *slot0 = bearer_id;
  494. *slot1 = bearer_id;
  495. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  496. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  497. tipc_link_set_active(nl, true);
  498. tipc_bcast_add_peer(n->net, nl, xmitq);
  499. return;
  500. }
  501. /* Second link => redistribute slots */
  502. if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
  503. pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
  504. *slot0 = bearer_id;
  505. *slot1 = bearer_id;
  506. tipc_link_set_active(nl, true);
  507. tipc_link_set_active(ol, false);
  508. } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
  509. tipc_link_set_active(nl, true);
  510. *slot1 = bearer_id;
  511. } else {
  512. pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
  513. }
  514. /* Prepare synchronization with first link */
  515. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  516. }
  517. /**
  518. * tipc_node_link_up - handle addition of link
  519. *
  520. * Link becomes active (alone or shared) or standby, depending on its priority.
  521. */
  522. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  523. struct sk_buff_head *xmitq)
  524. {
  525. struct tipc_media_addr *maddr;
  526. tipc_node_write_lock(n);
  527. __tipc_node_link_up(n, bearer_id, xmitq);
  528. maddr = &n->links[bearer_id].maddr;
  529. tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
  530. tipc_node_write_unlock(n);
  531. }
  532. /**
  533. * __tipc_node_link_down - handle loss of link
  534. */
  535. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  536. struct sk_buff_head *xmitq,
  537. struct tipc_media_addr **maddr)
  538. {
  539. struct tipc_link_entry *le = &n->links[*bearer_id];
  540. int *slot0 = &n->active_links[0];
  541. int *slot1 = &n->active_links[1];
  542. int i, highest = 0, prio;
  543. struct tipc_link *l, *_l, *tnl;
  544. l = n->links[*bearer_id].link;
  545. if (!l || tipc_link_is_reset(l))
  546. return;
  547. n->working_links--;
  548. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  549. n->link_id = tipc_link_id(l);
  550. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  551. pr_debug("Lost link <%s> on network plane %c\n",
  552. tipc_link_name(l), tipc_link_plane(l));
  553. /* Select new active link if any available */
  554. *slot0 = INVALID_BEARER_ID;
  555. *slot1 = INVALID_BEARER_ID;
  556. for (i = 0; i < MAX_BEARERS; i++) {
  557. _l = n->links[i].link;
  558. if (!_l || !tipc_link_is_up(_l))
  559. continue;
  560. if (_l == l)
  561. continue;
  562. prio = tipc_link_prio(_l);
  563. if (prio < highest)
  564. continue;
  565. if (prio > highest) {
  566. highest = prio;
  567. *slot0 = i;
  568. *slot1 = i;
  569. continue;
  570. }
  571. *slot1 = i;
  572. }
  573. if (!tipc_node_is_up(n)) {
  574. if (tipc_link_peer_is_down(l))
  575. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  576. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  577. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  578. tipc_link_reset(l);
  579. tipc_link_build_reset_msg(l, xmitq);
  580. *maddr = &n->links[*bearer_id].maddr;
  581. node_lost_contact(n, &le->inputq);
  582. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  583. return;
  584. }
  585. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  586. /* There is still a working link => initiate failover */
  587. *bearer_id = n->active_links[0];
  588. tnl = n->links[*bearer_id].link;
  589. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  590. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  591. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  592. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  593. tipc_link_reset(l);
  594. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  595. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  596. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  597. *maddr = &n->links[*bearer_id].maddr;
  598. }
  599. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  600. {
  601. struct tipc_link_entry *le = &n->links[bearer_id];
  602. struct tipc_link *l = le->link;
  603. struct tipc_media_addr *maddr;
  604. struct sk_buff_head xmitq;
  605. int old_bearer_id = bearer_id;
  606. if (!l)
  607. return;
  608. __skb_queue_head_init(&xmitq);
  609. tipc_node_write_lock(n);
  610. if (!tipc_link_is_establishing(l)) {
  611. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  612. if (delete) {
  613. kfree(l);
  614. le->link = NULL;
  615. n->link_cnt--;
  616. }
  617. } else {
  618. /* Defuse pending tipc_node_link_up() */
  619. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  620. }
  621. tipc_node_write_unlock(n);
  622. if (delete)
  623. tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
  624. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  625. tipc_sk_rcv(n->net, &le->inputq);
  626. }
  627. static bool tipc_node_is_up(struct tipc_node *n)
  628. {
  629. return n->active_links[0] != INVALID_BEARER_ID;
  630. }
  631. void tipc_node_check_dest(struct net *net, u32 onode,
  632. struct tipc_bearer *b,
  633. u16 capabilities, u32 signature,
  634. struct tipc_media_addr *maddr,
  635. bool *respond, bool *dupl_addr)
  636. {
  637. struct tipc_node *n;
  638. struct tipc_link *l;
  639. struct tipc_link_entry *le;
  640. bool addr_match = false;
  641. bool sign_match = false;
  642. bool link_up = false;
  643. bool accept_addr = false;
  644. bool reset = true;
  645. char *if_name;
  646. unsigned long intv;
  647. *dupl_addr = false;
  648. *respond = false;
  649. n = tipc_node_create(net, onode, capabilities);
  650. if (!n)
  651. return;
  652. tipc_node_write_lock(n);
  653. le = &n->links[b->identity];
  654. /* Prepare to validate requesting node's signature and media address */
  655. l = le->link;
  656. link_up = l && tipc_link_is_up(l);
  657. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  658. sign_match = (signature == n->signature);
  659. /* These three flags give us eight permutations: */
  660. if (sign_match && addr_match && link_up) {
  661. /* All is fine. Do nothing. */
  662. reset = false;
  663. } else if (sign_match && addr_match && !link_up) {
  664. /* Respond. The link will come up in due time */
  665. *respond = true;
  666. } else if (sign_match && !addr_match && link_up) {
  667. /* Peer has changed i/f address without rebooting.
  668. * If so, the link will reset soon, and the next
  669. * discovery will be accepted. So we can ignore it.
  670. * It may also be an cloned or malicious peer having
  671. * chosen the same node address and signature as an
  672. * existing one.
  673. * Ignore requests until the link goes down, if ever.
  674. */
  675. *dupl_addr = true;
  676. } else if (sign_match && !addr_match && !link_up) {
  677. /* Peer link has changed i/f address without rebooting.
  678. * It may also be a cloned or malicious peer; we can't
  679. * distinguish between the two.
  680. * The signature is correct, so we must accept.
  681. */
  682. accept_addr = true;
  683. *respond = true;
  684. } else if (!sign_match && addr_match && link_up) {
  685. /* Peer node rebooted. Two possibilities:
  686. * - Delayed re-discovery; this link endpoint has already
  687. * reset and re-established contact with the peer, before
  688. * receiving a discovery message from that node.
  689. * (The peer happened to receive one from this node first).
  690. * - The peer came back so fast that our side has not
  691. * discovered it yet. Probing from this side will soon
  692. * reset the link, since there can be no working link
  693. * endpoint at the peer end, and the link will re-establish.
  694. * Accept the signature, since it comes from a known peer.
  695. */
  696. n->signature = signature;
  697. } else if (!sign_match && addr_match && !link_up) {
  698. /* The peer node has rebooted.
  699. * Accept signature, since it is a known peer.
  700. */
  701. n->signature = signature;
  702. *respond = true;
  703. } else if (!sign_match && !addr_match && link_up) {
  704. /* Peer rebooted with new address, or a new/duplicate peer.
  705. * Ignore until the link goes down, if ever.
  706. */
  707. *dupl_addr = true;
  708. } else if (!sign_match && !addr_match && !link_up) {
  709. /* Peer rebooted with new address, or it is a new peer.
  710. * Accept signature and address.
  711. */
  712. n->signature = signature;
  713. accept_addr = true;
  714. *respond = true;
  715. }
  716. if (!accept_addr)
  717. goto exit;
  718. /* Now create new link if not already existing */
  719. if (!l) {
  720. if (n->link_cnt == 2) {
  721. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  722. goto exit;
  723. }
  724. if_name = strchr(b->name, ':') + 1;
  725. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  726. b->net_plane, b->mtu, b->priority,
  727. b->window, mod(tipc_net(net)->random),
  728. tipc_own_addr(net), onode,
  729. n->capabilities,
  730. tipc_bc_sndlink(n->net), n->bc_entry.link,
  731. &le->inputq,
  732. &n->bc_entry.namedq, &l)) {
  733. *respond = false;
  734. goto exit;
  735. }
  736. tipc_link_reset(l);
  737. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  738. if (n->state == NODE_FAILINGOVER)
  739. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  740. le->link = l;
  741. n->link_cnt++;
  742. tipc_node_calculate_timer(n, l);
  743. if (n->link_cnt == 1) {
  744. intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
  745. if (!mod_timer(&n->timer, intv))
  746. tipc_node_get(n);
  747. }
  748. }
  749. memcpy(&le->maddr, maddr, sizeof(*maddr));
  750. exit:
  751. tipc_node_write_unlock(n);
  752. if (reset && l && !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_DOWN;
  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_DOWN_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: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  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;
  1055. int rc;
  1056. if (in_own_node(net, dnode)) {
  1057. tipc_sk_rcv(net, list);
  1058. return 0;
  1059. }
  1060. n = tipc_node_find(net, dnode);
  1061. if (unlikely(!n)) {
  1062. skb_queue_purge(list);
  1063. return -EHOSTUNREACH;
  1064. }
  1065. tipc_node_read_lock(n);
  1066. bearer_id = n->active_links[selector & 1];
  1067. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1068. tipc_node_read_unlock(n);
  1069. tipc_node_put(n);
  1070. skb_queue_purge(list);
  1071. return -EHOSTUNREACH;
  1072. }
  1073. __skb_queue_head_init(&xmitq);
  1074. le = &n->links[bearer_id];
  1075. spin_lock_bh(&le->lock);
  1076. rc = tipc_link_xmit(le->link, list, &xmitq);
  1077. spin_unlock_bh(&le->lock);
  1078. tipc_node_read_unlock(n);
  1079. if (likely(rc == 0))
  1080. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1081. else if (rc == -ENOBUFS)
  1082. tipc_node_link_down(n, bearer_id, false);
  1083. tipc_node_put(n);
  1084. return rc;
  1085. }
  1086. /* tipc_node_xmit_skb(): send single buffer to destination
  1087. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  1088. * messages, which will not be rejected
  1089. * The only exception is datagram messages rerouted after secondary
  1090. * lookup, which are rare and safe to dispose of anyway.
  1091. * TODO: Return real return value, and let callers use
  1092. * tipc_wait_for_sendpkt() where applicable
  1093. */
  1094. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1095. u32 selector)
  1096. {
  1097. struct sk_buff_head head;
  1098. int rc;
  1099. skb_queue_head_init(&head);
  1100. __skb_queue_tail(&head, skb);
  1101. rc = tipc_node_xmit(net, &head, dnode, selector);
  1102. if (rc == -ELINKCONG)
  1103. kfree_skb(skb);
  1104. return 0;
  1105. }
  1106. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  1107. {
  1108. struct sk_buff *txskb;
  1109. struct tipc_node *n;
  1110. u32 dst;
  1111. rcu_read_lock();
  1112. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1113. dst = n->addr;
  1114. if (in_own_node(net, dst))
  1115. continue;
  1116. if (!tipc_node_is_up(n))
  1117. continue;
  1118. txskb = pskb_copy(skb, GFP_ATOMIC);
  1119. if (!txskb)
  1120. break;
  1121. msg_set_destnode(buf_msg(txskb), dst);
  1122. tipc_node_xmit_skb(net, txskb, dst, 0);
  1123. }
  1124. rcu_read_unlock();
  1125. kfree_skb(skb);
  1126. }
  1127. /**
  1128. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1129. * @net: the applicable net namespace
  1130. * @skb: TIPC packet
  1131. * @bearer_id: id of bearer message arrived on
  1132. *
  1133. * Invoked with no locks held.
  1134. */
  1135. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1136. {
  1137. int rc;
  1138. struct sk_buff_head xmitq;
  1139. struct tipc_bclink_entry *be;
  1140. struct tipc_link_entry *le;
  1141. struct tipc_msg *hdr = buf_msg(skb);
  1142. int usr = msg_user(hdr);
  1143. u32 dnode = msg_destnode(hdr);
  1144. struct tipc_node *n;
  1145. __skb_queue_head_init(&xmitq);
  1146. /* If NACK for other node, let rcv link for that node peek into it */
  1147. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1148. n = tipc_node_find(net, dnode);
  1149. else
  1150. n = tipc_node_find(net, msg_prevnode(hdr));
  1151. if (!n) {
  1152. kfree_skb(skb);
  1153. return;
  1154. }
  1155. be = &n->bc_entry;
  1156. le = &n->links[bearer_id];
  1157. rc = tipc_bcast_rcv(net, be->link, skb);
  1158. /* Broadcast ACKs are sent on a unicast link */
  1159. if (rc & TIPC_LINK_SND_BC_ACK) {
  1160. tipc_node_read_lock(n);
  1161. tipc_link_build_state_msg(le->link, &xmitq);
  1162. tipc_node_read_unlock(n);
  1163. }
  1164. if (!skb_queue_empty(&xmitq))
  1165. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1166. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1167. if (!skb_queue_empty(&be->inputq1)) {
  1168. spin_lock_bh(&be->inputq2.lock);
  1169. spin_lock_bh(&be->inputq1.lock);
  1170. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1171. spin_unlock_bh(&be->inputq1.lock);
  1172. spin_unlock_bh(&be->inputq2.lock);
  1173. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1174. }
  1175. if (rc & TIPC_LINK_DOWN_EVT) {
  1176. /* Reception reassembly failure => reset all links to peer */
  1177. if (!tipc_link_is_up(be->link))
  1178. tipc_node_reset_links(n);
  1179. /* Retransmission failure => reset all links to all peers */
  1180. if (!tipc_link_is_up(tipc_bc_sndlink(net)))
  1181. tipc_bearer_reset_all(net);
  1182. }
  1183. tipc_node_put(n);
  1184. }
  1185. /**
  1186. * tipc_node_check_state - check and if necessary update node state
  1187. * @skb: TIPC packet
  1188. * @bearer_id: identity of bearer delivering the packet
  1189. * Returns true if state is ok, otherwise consumes buffer and returns false
  1190. */
  1191. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1192. int bearer_id, struct sk_buff_head *xmitq)
  1193. {
  1194. struct tipc_msg *hdr = buf_msg(skb);
  1195. int usr = msg_user(hdr);
  1196. int mtyp = msg_type(hdr);
  1197. u16 oseqno = msg_seqno(hdr);
  1198. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1199. u16 exp_pkts = msg_msgcnt(hdr);
  1200. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1201. int state = n->state;
  1202. struct tipc_link *l, *tnl, *pl = NULL;
  1203. struct tipc_media_addr *maddr;
  1204. int pb_id;
  1205. l = n->links[bearer_id].link;
  1206. if (!l)
  1207. return false;
  1208. rcv_nxt = tipc_link_rcv_nxt(l);
  1209. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1210. return true;
  1211. /* Find parallel link, if any */
  1212. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1213. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1214. pl = n->links[pb_id].link;
  1215. break;
  1216. }
  1217. }
  1218. /* Check and update node accesibility if applicable */
  1219. if (state == SELF_UP_PEER_COMING) {
  1220. if (!tipc_link_is_up(l))
  1221. return true;
  1222. if (!msg_peer_link_is_up(hdr))
  1223. return true;
  1224. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1225. }
  1226. if (state == SELF_DOWN_PEER_LEAVING) {
  1227. if (msg_peer_node_is_up(hdr))
  1228. return false;
  1229. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1230. return true;
  1231. }
  1232. if (state == SELF_LEAVING_PEER_DOWN)
  1233. return false;
  1234. /* Ignore duplicate packets */
  1235. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1236. return true;
  1237. /* Initiate or update failover mode if applicable */
  1238. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1239. syncpt = oseqno + exp_pkts - 1;
  1240. if (pl && tipc_link_is_up(pl)) {
  1241. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1242. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1243. tipc_link_inputq(l));
  1244. }
  1245. /* If pkts arrive out of order, use lowest calculated syncpt */
  1246. if (less(syncpt, n->sync_point))
  1247. n->sync_point = syncpt;
  1248. }
  1249. /* Open parallel link when tunnel link reaches synch point */
  1250. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1251. if (!more(rcv_nxt, n->sync_point))
  1252. return true;
  1253. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1254. if (pl)
  1255. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1256. return true;
  1257. }
  1258. /* No synching needed if only one link */
  1259. if (!pl || !tipc_link_is_up(pl))
  1260. return true;
  1261. /* Initiate synch mode if applicable */
  1262. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1263. syncpt = iseqno + exp_pkts - 1;
  1264. if (!tipc_link_is_up(l)) {
  1265. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1266. __tipc_node_link_up(n, bearer_id, xmitq);
  1267. }
  1268. if (n->state == SELF_UP_PEER_UP) {
  1269. n->sync_point = syncpt;
  1270. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1271. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1272. }
  1273. }
  1274. /* Open tunnel link when parallel link reaches synch point */
  1275. if (n->state == NODE_SYNCHING) {
  1276. if (tipc_link_is_synching(l)) {
  1277. tnl = l;
  1278. } else {
  1279. tnl = pl;
  1280. pl = l;
  1281. }
  1282. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1283. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1284. if (more(dlv_nxt, n->sync_point)) {
  1285. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1286. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1287. return true;
  1288. }
  1289. if (l == pl)
  1290. return true;
  1291. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1292. return true;
  1293. if (usr == LINK_PROTOCOL)
  1294. return true;
  1295. return false;
  1296. }
  1297. return true;
  1298. }
  1299. /**
  1300. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1301. * @net: the applicable net namespace
  1302. * @skb: TIPC packet
  1303. * @bearer: pointer to bearer message arrived on
  1304. *
  1305. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1306. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1307. */
  1308. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1309. {
  1310. struct sk_buff_head xmitq;
  1311. struct tipc_node *n;
  1312. struct tipc_msg *hdr = buf_msg(skb);
  1313. int usr = msg_user(hdr);
  1314. int bearer_id = b->identity;
  1315. struct tipc_link_entry *le;
  1316. u16 bc_ack = msg_bcast_ack(hdr);
  1317. u32 self = tipc_own_addr(net);
  1318. int rc = 0;
  1319. __skb_queue_head_init(&xmitq);
  1320. /* Ensure message is well-formed */
  1321. if (unlikely(!tipc_msg_validate(skb)))
  1322. goto discard;
  1323. /* Handle arrival of discovery or broadcast packet */
  1324. if (unlikely(msg_non_seq(hdr))) {
  1325. if (unlikely(usr == LINK_CONFIG))
  1326. return tipc_disc_rcv(net, skb, b);
  1327. else
  1328. return tipc_node_bc_rcv(net, skb, bearer_id);
  1329. }
  1330. /* Discard unicast link messages destined for another node */
  1331. if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
  1332. goto discard;
  1333. /* Locate neighboring node that sent packet */
  1334. n = tipc_node_find(net, msg_prevnode(hdr));
  1335. if (unlikely(!n))
  1336. goto discard;
  1337. le = &n->links[bearer_id];
  1338. /* Ensure broadcast reception is in synch with peer's send state */
  1339. if (unlikely(usr == LINK_PROTOCOL))
  1340. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1341. else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
  1342. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1343. /* Receive packet directly if conditions permit */
  1344. tipc_node_read_lock(n);
  1345. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1346. spin_lock_bh(&le->lock);
  1347. if (le->link) {
  1348. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1349. skb = NULL;
  1350. }
  1351. spin_unlock_bh(&le->lock);
  1352. }
  1353. tipc_node_read_unlock(n);
  1354. /* Check/update node state before receiving */
  1355. if (unlikely(skb)) {
  1356. tipc_node_write_lock(n);
  1357. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1358. if (le->link) {
  1359. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1360. skb = NULL;
  1361. }
  1362. }
  1363. tipc_node_write_unlock(n);
  1364. }
  1365. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1366. tipc_node_link_up(n, bearer_id, &xmitq);
  1367. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1368. tipc_node_link_down(n, bearer_id, false);
  1369. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1370. tipc_named_rcv(net, &n->bc_entry.namedq);
  1371. if (!skb_queue_empty(&le->inputq))
  1372. tipc_sk_rcv(net, &le->inputq);
  1373. if (!skb_queue_empty(&xmitq))
  1374. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1375. tipc_node_put(n);
  1376. discard:
  1377. kfree_skb(skb);
  1378. }
  1379. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1380. {
  1381. int err;
  1382. struct net *net = sock_net(skb->sk);
  1383. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1384. int done = cb->args[0];
  1385. int last_addr = cb->args[1];
  1386. struct tipc_node *node;
  1387. struct tipc_nl_msg msg;
  1388. if (done)
  1389. return 0;
  1390. msg.skb = skb;
  1391. msg.portid = NETLINK_CB(cb->skb).portid;
  1392. msg.seq = cb->nlh->nlmsg_seq;
  1393. rcu_read_lock();
  1394. if (last_addr) {
  1395. node = tipc_node_find(net, last_addr);
  1396. if (!node) {
  1397. rcu_read_unlock();
  1398. /* We never set seq or call nl_dump_check_consistent()
  1399. * this means that setting prev_seq here will cause the
  1400. * consistence check to fail in the netlink callback
  1401. * handler. Resulting in the NLMSG_DONE message having
  1402. * the NLM_F_DUMP_INTR flag set if the node state
  1403. * changed while we released the lock.
  1404. */
  1405. cb->prev_seq = 1;
  1406. return -EPIPE;
  1407. }
  1408. tipc_node_put(node);
  1409. }
  1410. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1411. if (last_addr) {
  1412. if (node->addr == last_addr)
  1413. last_addr = 0;
  1414. else
  1415. continue;
  1416. }
  1417. tipc_node_read_lock(node);
  1418. err = __tipc_nl_add_node(&msg, node);
  1419. if (err) {
  1420. last_addr = node->addr;
  1421. tipc_node_read_unlock(node);
  1422. goto out;
  1423. }
  1424. tipc_node_read_unlock(node);
  1425. }
  1426. done = 1;
  1427. out:
  1428. cb->args[0] = done;
  1429. cb->args[1] = last_addr;
  1430. rcu_read_unlock();
  1431. return skb->len;
  1432. }
  1433. /* tipc_node_find_by_name - locate owner node of link by link's name
  1434. * @net: the applicable net namespace
  1435. * @name: pointer to link name string
  1436. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1437. *
  1438. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1439. */
  1440. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  1441. const char *link_name,
  1442. unsigned int *bearer_id)
  1443. {
  1444. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1445. struct tipc_link *l;
  1446. struct tipc_node *n;
  1447. struct tipc_node *found_node = NULL;
  1448. int i;
  1449. *bearer_id = 0;
  1450. rcu_read_lock();
  1451. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1452. tipc_node_read_lock(n);
  1453. for (i = 0; i < MAX_BEARERS; i++) {
  1454. l = n->links[i].link;
  1455. if (l && !strcmp(tipc_link_name(l), link_name)) {
  1456. *bearer_id = i;
  1457. found_node = n;
  1458. break;
  1459. }
  1460. }
  1461. tipc_node_read_unlock(n);
  1462. if (found_node)
  1463. break;
  1464. }
  1465. rcu_read_unlock();
  1466. return found_node;
  1467. }
  1468. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  1469. {
  1470. int err;
  1471. int res = 0;
  1472. int bearer_id;
  1473. char *name;
  1474. struct tipc_link *link;
  1475. struct tipc_node *node;
  1476. struct sk_buff_head xmitq;
  1477. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1478. struct net *net = sock_net(skb->sk);
  1479. __skb_queue_head_init(&xmitq);
  1480. if (!info->attrs[TIPC_NLA_LINK])
  1481. return -EINVAL;
  1482. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1483. info->attrs[TIPC_NLA_LINK],
  1484. tipc_nl_link_policy);
  1485. if (err)
  1486. return err;
  1487. if (!attrs[TIPC_NLA_LINK_NAME])
  1488. return -EINVAL;
  1489. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1490. if (strcmp(name, tipc_bclink_name) == 0)
  1491. return tipc_nl_bc_link_set(net, attrs);
  1492. node = tipc_node_find_by_name(net, name, &bearer_id);
  1493. if (!node)
  1494. return -EINVAL;
  1495. tipc_node_read_lock(node);
  1496. link = node->links[bearer_id].link;
  1497. if (!link) {
  1498. res = -EINVAL;
  1499. goto out;
  1500. }
  1501. if (attrs[TIPC_NLA_LINK_PROP]) {
  1502. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1503. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1504. props);
  1505. if (err) {
  1506. res = err;
  1507. goto out;
  1508. }
  1509. if (props[TIPC_NLA_PROP_TOL]) {
  1510. u32 tol;
  1511. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1512. tipc_link_set_tolerance(link, tol, &xmitq);
  1513. }
  1514. if (props[TIPC_NLA_PROP_PRIO]) {
  1515. u32 prio;
  1516. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1517. tipc_link_set_prio(link, prio, &xmitq);
  1518. }
  1519. if (props[TIPC_NLA_PROP_WIN]) {
  1520. u32 win;
  1521. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1522. tipc_link_set_queue_limits(link, win);
  1523. }
  1524. }
  1525. out:
  1526. tipc_node_read_unlock(node);
  1527. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
  1528. return res;
  1529. }
  1530. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  1531. {
  1532. struct net *net = genl_info_net(info);
  1533. struct tipc_nl_msg msg;
  1534. char *name;
  1535. int err;
  1536. msg.portid = info->snd_portid;
  1537. msg.seq = info->snd_seq;
  1538. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1539. return -EINVAL;
  1540. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1541. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1542. if (!msg.skb)
  1543. return -ENOMEM;
  1544. if (strcmp(name, tipc_bclink_name) == 0) {
  1545. err = tipc_nl_add_bc_link(net, &msg);
  1546. if (err) {
  1547. nlmsg_free(msg.skb);
  1548. return err;
  1549. }
  1550. } else {
  1551. int bearer_id;
  1552. struct tipc_node *node;
  1553. struct tipc_link *link;
  1554. node = tipc_node_find_by_name(net, name, &bearer_id);
  1555. if (!node)
  1556. return -EINVAL;
  1557. tipc_node_read_lock(node);
  1558. link = node->links[bearer_id].link;
  1559. if (!link) {
  1560. tipc_node_read_unlock(node);
  1561. nlmsg_free(msg.skb);
  1562. return -EINVAL;
  1563. }
  1564. err = __tipc_nl_add_link(net, &msg, link, 0);
  1565. tipc_node_read_unlock(node);
  1566. if (err) {
  1567. nlmsg_free(msg.skb);
  1568. return err;
  1569. }
  1570. }
  1571. return genlmsg_reply(msg.skb, info);
  1572. }
  1573. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  1574. {
  1575. int err;
  1576. char *link_name;
  1577. unsigned int bearer_id;
  1578. struct tipc_link *link;
  1579. struct tipc_node *node;
  1580. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1581. struct net *net = sock_net(skb->sk);
  1582. struct tipc_link_entry *le;
  1583. if (!info->attrs[TIPC_NLA_LINK])
  1584. return -EINVAL;
  1585. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1586. info->attrs[TIPC_NLA_LINK],
  1587. tipc_nl_link_policy);
  1588. if (err)
  1589. return err;
  1590. if (!attrs[TIPC_NLA_LINK_NAME])
  1591. return -EINVAL;
  1592. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1593. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1594. err = tipc_bclink_reset_stats(net);
  1595. if (err)
  1596. return err;
  1597. return 0;
  1598. }
  1599. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  1600. if (!node)
  1601. return -EINVAL;
  1602. le = &node->links[bearer_id];
  1603. tipc_node_read_lock(node);
  1604. spin_lock_bh(&le->lock);
  1605. link = node->links[bearer_id].link;
  1606. if (!link) {
  1607. spin_unlock_bh(&le->lock);
  1608. tipc_node_read_unlock(node);
  1609. return -EINVAL;
  1610. }
  1611. tipc_link_reset_stats(link);
  1612. spin_unlock_bh(&le->lock);
  1613. tipc_node_read_unlock(node);
  1614. return 0;
  1615. }
  1616. /* Caller should hold node lock */
  1617. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1618. struct tipc_node *node, u32 *prev_link)
  1619. {
  1620. u32 i;
  1621. int err;
  1622. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1623. *prev_link = i;
  1624. if (!node->links[i].link)
  1625. continue;
  1626. err = __tipc_nl_add_link(net, msg,
  1627. node->links[i].link, NLM_F_MULTI);
  1628. if (err)
  1629. return err;
  1630. }
  1631. *prev_link = 0;
  1632. return 0;
  1633. }
  1634. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  1635. {
  1636. struct net *net = sock_net(skb->sk);
  1637. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1638. struct tipc_node *node;
  1639. struct tipc_nl_msg msg;
  1640. u32 prev_node = cb->args[0];
  1641. u32 prev_link = cb->args[1];
  1642. int done = cb->args[2];
  1643. int err;
  1644. if (done)
  1645. return 0;
  1646. msg.skb = skb;
  1647. msg.portid = NETLINK_CB(cb->skb).portid;
  1648. msg.seq = cb->nlh->nlmsg_seq;
  1649. rcu_read_lock();
  1650. if (prev_node) {
  1651. node = tipc_node_find(net, prev_node);
  1652. if (!node) {
  1653. /* We never set seq or call nl_dump_check_consistent()
  1654. * this means that setting prev_seq here will cause the
  1655. * consistence check to fail in the netlink callback
  1656. * handler. Resulting in the last NLMSG_DONE message
  1657. * having the NLM_F_DUMP_INTR flag set.
  1658. */
  1659. cb->prev_seq = 1;
  1660. goto out;
  1661. }
  1662. tipc_node_put(node);
  1663. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1664. list) {
  1665. tipc_node_read_lock(node);
  1666. err = __tipc_nl_add_node_links(net, &msg, node,
  1667. &prev_link);
  1668. tipc_node_read_unlock(node);
  1669. if (err)
  1670. goto out;
  1671. prev_node = node->addr;
  1672. }
  1673. } else {
  1674. err = tipc_nl_add_bc_link(net, &msg);
  1675. if (err)
  1676. goto out;
  1677. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1678. tipc_node_read_lock(node);
  1679. err = __tipc_nl_add_node_links(net, &msg, node,
  1680. &prev_link);
  1681. tipc_node_read_unlock(node);
  1682. if (err)
  1683. goto out;
  1684. prev_node = node->addr;
  1685. }
  1686. }
  1687. done = 1;
  1688. out:
  1689. rcu_read_unlock();
  1690. cb->args[0] = prev_node;
  1691. cb->args[1] = prev_link;
  1692. cb->args[2] = done;
  1693. return skb->len;
  1694. }
  1695. int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
  1696. {
  1697. struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
  1698. struct net *net = sock_net(skb->sk);
  1699. int err;
  1700. if (!info->attrs[TIPC_NLA_MON])
  1701. return -EINVAL;
  1702. err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
  1703. info->attrs[TIPC_NLA_MON],
  1704. tipc_nl_monitor_policy);
  1705. if (err)
  1706. return err;
  1707. if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
  1708. u32 val;
  1709. val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
  1710. err = tipc_nl_monitor_set_threshold(net, val);
  1711. if (err)
  1712. return err;
  1713. }
  1714. return 0;
  1715. }
  1716. static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
  1717. {
  1718. struct nlattr *attrs;
  1719. void *hdr;
  1720. u32 val;
  1721. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1722. 0, TIPC_NL_MON_GET);
  1723. if (!hdr)
  1724. return -EMSGSIZE;
  1725. attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
  1726. if (!attrs)
  1727. goto msg_full;
  1728. val = tipc_nl_monitor_get_threshold(net);
  1729. if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
  1730. goto attr_msg_full;
  1731. nla_nest_end(msg->skb, attrs);
  1732. genlmsg_end(msg->skb, hdr);
  1733. return 0;
  1734. attr_msg_full:
  1735. nla_nest_cancel(msg->skb, attrs);
  1736. msg_full:
  1737. genlmsg_cancel(msg->skb, hdr);
  1738. return -EMSGSIZE;
  1739. }
  1740. int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
  1741. {
  1742. struct net *net = sock_net(skb->sk);
  1743. struct tipc_nl_msg msg;
  1744. int err;
  1745. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1746. msg.portid = info->snd_portid;
  1747. msg.seq = info->snd_seq;
  1748. err = __tipc_nl_add_monitor_prop(net, &msg);
  1749. if (err) {
  1750. nlmsg_free(msg.skb);
  1751. return err;
  1752. }
  1753. return genlmsg_reply(msg.skb, info);
  1754. }
  1755. int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
  1756. {
  1757. struct net *net = sock_net(skb->sk);
  1758. u32 prev_bearer = cb->args[0];
  1759. struct tipc_nl_msg msg;
  1760. int err;
  1761. int i;
  1762. if (prev_bearer == MAX_BEARERS)
  1763. return 0;
  1764. msg.skb = skb;
  1765. msg.portid = NETLINK_CB(cb->skb).portid;
  1766. msg.seq = cb->nlh->nlmsg_seq;
  1767. rtnl_lock();
  1768. for (i = prev_bearer; i < MAX_BEARERS; i++) {
  1769. prev_bearer = i;
  1770. err = __tipc_nl_add_monitor(net, &msg, prev_bearer);
  1771. if (err)
  1772. goto out;
  1773. }
  1774. out:
  1775. rtnl_unlock();
  1776. cb->args[0] = prev_bearer;
  1777. return skb->len;
  1778. }
  1779. int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
  1780. struct netlink_callback *cb)
  1781. {
  1782. struct net *net = sock_net(skb->sk);
  1783. u32 prev_node = cb->args[1];
  1784. u32 bearer_id = cb->args[2];
  1785. int done = cb->args[0];
  1786. struct tipc_nl_msg msg;
  1787. int err;
  1788. if (!prev_node) {
  1789. struct nlattr **attrs;
  1790. struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
  1791. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  1792. if (err)
  1793. return err;
  1794. if (!attrs[TIPC_NLA_MON])
  1795. return -EINVAL;
  1796. err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
  1797. attrs[TIPC_NLA_MON],
  1798. tipc_nl_monitor_policy);
  1799. if (err)
  1800. return err;
  1801. if (!mon[TIPC_NLA_MON_REF])
  1802. return -EINVAL;
  1803. bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
  1804. if (bearer_id >= MAX_BEARERS)
  1805. return -EINVAL;
  1806. }
  1807. if (done)
  1808. return 0;
  1809. msg.skb = skb;
  1810. msg.portid = NETLINK_CB(cb->skb).portid;
  1811. msg.seq = cb->nlh->nlmsg_seq;
  1812. rtnl_lock();
  1813. err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
  1814. if (!err)
  1815. done = 1;
  1816. rtnl_unlock();
  1817. cb->args[0] = done;
  1818. cb->args[1] = prev_node;
  1819. cb->args[2] = bearer_id;
  1820. return skb->len;
  1821. }