node.c 56 KB

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