node.c 58 KB

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