node.c 54 KB

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