node.c 57 KB

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