link.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2015, Ericsson AB
  5. * Copyright (c) 2004-2007, 2010-2013, 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 "subscr.h"
  38. #include "link.h"
  39. #include "bcast.h"
  40. #include "socket.h"
  41. #include "name_distr.h"
  42. #include "discover.h"
  43. #include "netlink.h"
  44. #include <linux/pkt_sched.h>
  45. /*
  46. * Error message prefixes
  47. */
  48. static const char *link_co_err = "Link tunneling error, ";
  49. static const char *link_rst_msg = "Resetting link ";
  50. static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
  51. [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
  52. [TIPC_NLA_LINK_NAME] = {
  53. .type = NLA_STRING,
  54. .len = TIPC_MAX_LINK_NAME
  55. },
  56. [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
  57. [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
  58. [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
  59. [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
  60. [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
  61. [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
  62. [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
  63. [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
  64. };
  65. /* Properties valid for media, bearar and link */
  66. static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
  67. [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
  68. [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
  69. [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
  70. [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
  71. };
  72. /*
  73. * Interval between NACKs when packets arrive out of order
  74. */
  75. #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
  76. /*
  77. * Out-of-range value for link session numbers
  78. */
  79. #define WILDCARD_SESSION 0x10000
  80. /* Link FSM states:
  81. */
  82. enum {
  83. LINK_ESTABLISHED = 0xe,
  84. LINK_ESTABLISHING = 0xe << 4,
  85. LINK_RESET = 0x1 << 8,
  86. LINK_RESETTING = 0x2 << 12,
  87. LINK_PEER_RESET = 0xd << 16,
  88. LINK_FAILINGOVER = 0xf << 20,
  89. LINK_SYNCHING = 0xc << 24
  90. };
  91. /* Link FSM state checking routines
  92. */
  93. static int link_is_up(struct tipc_link *l)
  94. {
  95. return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
  96. }
  97. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  98. struct sk_buff_head *xmitq);
  99. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  100. u16 rcvgap, int tolerance, int priority,
  101. struct sk_buff_head *xmitq);
  102. static void link_reset_statistics(struct tipc_link *l_ptr);
  103. static void link_print(struct tipc_link *l_ptr, const char *str);
  104. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
  105. /*
  106. * Simple non-static link routines (i.e. referenced outside this file)
  107. */
  108. bool tipc_link_is_up(struct tipc_link *l)
  109. {
  110. return link_is_up(l);
  111. }
  112. bool tipc_link_is_reset(struct tipc_link *l)
  113. {
  114. return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
  115. }
  116. bool tipc_link_is_synching(struct tipc_link *l)
  117. {
  118. return l->state == LINK_SYNCHING;
  119. }
  120. bool tipc_link_is_failingover(struct tipc_link *l)
  121. {
  122. return l->state == LINK_FAILINGOVER;
  123. }
  124. bool tipc_link_is_blocked(struct tipc_link *l)
  125. {
  126. return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
  127. }
  128. int tipc_link_is_active(struct tipc_link *l)
  129. {
  130. struct tipc_node *n = l->owner;
  131. return (node_active_link(n, 0) == l) || (node_active_link(n, 1) == l);
  132. }
  133. static u32 link_own_addr(struct tipc_link *l)
  134. {
  135. return msg_prevnode(l->pmsg);
  136. }
  137. /**
  138. * tipc_link_create - create a new link
  139. * @n: pointer to associated node
  140. * @b: pointer to associated bearer
  141. * @ownnode: identity of own node
  142. * @peer: identity of peer node
  143. * @maddr: media address to be used
  144. * @inputq: queue to put messages ready for delivery
  145. * @namedq: queue to put binding table update messages ready for delivery
  146. * @link: return value, pointer to put the created link
  147. *
  148. * Returns true if link was created, otherwise false
  149. */
  150. bool tipc_link_create(struct tipc_node *n, struct tipc_bearer *b, u32 session,
  151. u32 ownnode, u32 peer, struct tipc_media_addr *maddr,
  152. struct sk_buff_head *inputq, struct sk_buff_head *namedq,
  153. struct tipc_link **link)
  154. {
  155. struct tipc_link *l;
  156. struct tipc_msg *hdr;
  157. char *if_name;
  158. l = kzalloc(sizeof(*l), GFP_ATOMIC);
  159. if (!l)
  160. return false;
  161. *link = l;
  162. /* Note: peer i/f name is completed by reset/activate message */
  163. if_name = strchr(b->name, ':') + 1;
  164. sprintf(l->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  165. tipc_zone(ownnode), tipc_cluster(ownnode), tipc_node(ownnode),
  166. if_name, tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  167. l->addr = peer;
  168. l->media_addr = maddr;
  169. l->owner = n;
  170. l->peer_session = WILDCARD_SESSION;
  171. l->bearer_id = b->identity;
  172. l->tolerance = b->tolerance;
  173. l->net_plane = b->net_plane;
  174. l->advertised_mtu = b->mtu;
  175. l->mtu = b->mtu;
  176. l->priority = b->priority;
  177. tipc_link_set_queue_limits(l, b->window);
  178. l->inputq = inputq;
  179. l->namedq = namedq;
  180. l->state = LINK_RESETTING;
  181. l->pmsg = (struct tipc_msg *)&l->proto_msg;
  182. hdr = l->pmsg;
  183. tipc_msg_init(ownnode, hdr, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, peer);
  184. msg_set_size(hdr, sizeof(l->proto_msg));
  185. msg_set_session(hdr, session);
  186. msg_set_bearer_id(hdr, l->bearer_id);
  187. strcpy((char *)msg_data(hdr), if_name);
  188. __skb_queue_head_init(&l->transmq);
  189. __skb_queue_head_init(&l->backlogq);
  190. __skb_queue_head_init(&l->deferdq);
  191. skb_queue_head_init(&l->wakeupq);
  192. skb_queue_head_init(l->inputq);
  193. return true;
  194. }
  195. /* tipc_link_build_bcast_sync_msg() - synchronize broadcast link endpoints.
  196. *
  197. * Give a newly added peer node the sequence number where it should
  198. * start receiving and acking broadcast packets.
  199. */
  200. void tipc_link_build_bcast_sync_msg(struct tipc_link *l,
  201. struct sk_buff_head *xmitq)
  202. {
  203. struct sk_buff *skb;
  204. struct sk_buff_head list;
  205. u16 last_sent;
  206. skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
  207. 0, l->addr, link_own_addr(l), 0, 0, 0);
  208. if (!skb)
  209. return;
  210. last_sent = tipc_bclink_get_last_sent(l->owner->net);
  211. msg_set_last_bcast(buf_msg(skb), last_sent);
  212. __skb_queue_head_init(&list);
  213. __skb_queue_tail(&list, skb);
  214. tipc_link_xmit(l, &list, xmitq);
  215. }
  216. /**
  217. * tipc_link_fsm_evt - link finite state machine
  218. * @l: pointer to link
  219. * @evt: state machine event to be processed
  220. */
  221. int tipc_link_fsm_evt(struct tipc_link *l, int evt)
  222. {
  223. int rc = 0;
  224. switch (l->state) {
  225. case LINK_RESETTING:
  226. switch (evt) {
  227. case LINK_PEER_RESET_EVT:
  228. l->state = LINK_PEER_RESET;
  229. break;
  230. case LINK_RESET_EVT:
  231. l->state = LINK_RESET;
  232. break;
  233. case LINK_FAILURE_EVT:
  234. case LINK_FAILOVER_BEGIN_EVT:
  235. case LINK_ESTABLISH_EVT:
  236. case LINK_FAILOVER_END_EVT:
  237. case LINK_SYNCH_BEGIN_EVT:
  238. case LINK_SYNCH_END_EVT:
  239. default:
  240. goto illegal_evt;
  241. }
  242. break;
  243. case LINK_RESET:
  244. switch (evt) {
  245. case LINK_PEER_RESET_EVT:
  246. l->state = LINK_ESTABLISHING;
  247. break;
  248. case LINK_FAILOVER_BEGIN_EVT:
  249. l->state = LINK_FAILINGOVER;
  250. case LINK_FAILURE_EVT:
  251. case LINK_RESET_EVT:
  252. case LINK_ESTABLISH_EVT:
  253. case LINK_FAILOVER_END_EVT:
  254. break;
  255. case LINK_SYNCH_BEGIN_EVT:
  256. case LINK_SYNCH_END_EVT:
  257. default:
  258. goto illegal_evt;
  259. }
  260. break;
  261. case LINK_PEER_RESET:
  262. switch (evt) {
  263. case LINK_RESET_EVT:
  264. l->state = LINK_ESTABLISHING;
  265. break;
  266. case LINK_PEER_RESET_EVT:
  267. case LINK_ESTABLISH_EVT:
  268. case LINK_FAILURE_EVT:
  269. break;
  270. case LINK_SYNCH_BEGIN_EVT:
  271. case LINK_SYNCH_END_EVT:
  272. case LINK_FAILOVER_BEGIN_EVT:
  273. case LINK_FAILOVER_END_EVT:
  274. default:
  275. goto illegal_evt;
  276. }
  277. break;
  278. case LINK_FAILINGOVER:
  279. switch (evt) {
  280. case LINK_FAILOVER_END_EVT:
  281. l->state = LINK_RESET;
  282. break;
  283. case LINK_PEER_RESET_EVT:
  284. case LINK_RESET_EVT:
  285. case LINK_ESTABLISH_EVT:
  286. case LINK_FAILURE_EVT:
  287. break;
  288. case LINK_FAILOVER_BEGIN_EVT:
  289. case LINK_SYNCH_BEGIN_EVT:
  290. case LINK_SYNCH_END_EVT:
  291. default:
  292. goto illegal_evt;
  293. }
  294. break;
  295. case LINK_ESTABLISHING:
  296. switch (evt) {
  297. case LINK_ESTABLISH_EVT:
  298. l->state = LINK_ESTABLISHED;
  299. rc |= TIPC_LINK_UP_EVT;
  300. break;
  301. case LINK_FAILOVER_BEGIN_EVT:
  302. l->state = LINK_FAILINGOVER;
  303. break;
  304. case LINK_PEER_RESET_EVT:
  305. case LINK_RESET_EVT:
  306. case LINK_FAILURE_EVT:
  307. case LINK_SYNCH_BEGIN_EVT:
  308. case LINK_FAILOVER_END_EVT:
  309. break;
  310. case LINK_SYNCH_END_EVT:
  311. default:
  312. goto illegal_evt;
  313. }
  314. break;
  315. case LINK_ESTABLISHED:
  316. switch (evt) {
  317. case LINK_PEER_RESET_EVT:
  318. l->state = LINK_PEER_RESET;
  319. rc |= TIPC_LINK_DOWN_EVT;
  320. break;
  321. case LINK_FAILURE_EVT:
  322. l->state = LINK_RESETTING;
  323. rc |= TIPC_LINK_DOWN_EVT;
  324. break;
  325. case LINK_RESET_EVT:
  326. l->state = LINK_RESET;
  327. break;
  328. case LINK_ESTABLISH_EVT:
  329. break;
  330. case LINK_SYNCH_BEGIN_EVT:
  331. l->state = LINK_SYNCHING;
  332. break;
  333. case LINK_SYNCH_END_EVT:
  334. case LINK_FAILOVER_BEGIN_EVT:
  335. case LINK_FAILOVER_END_EVT:
  336. default:
  337. goto illegal_evt;
  338. }
  339. break;
  340. case LINK_SYNCHING:
  341. switch (evt) {
  342. case LINK_PEER_RESET_EVT:
  343. l->state = LINK_PEER_RESET;
  344. rc |= TIPC_LINK_DOWN_EVT;
  345. break;
  346. case LINK_FAILURE_EVT:
  347. l->state = LINK_RESETTING;
  348. rc |= TIPC_LINK_DOWN_EVT;
  349. break;
  350. case LINK_RESET_EVT:
  351. l->state = LINK_RESET;
  352. break;
  353. case LINK_ESTABLISH_EVT:
  354. case LINK_SYNCH_BEGIN_EVT:
  355. break;
  356. case LINK_SYNCH_END_EVT:
  357. l->state = LINK_ESTABLISHED;
  358. break;
  359. case LINK_FAILOVER_BEGIN_EVT:
  360. case LINK_FAILOVER_END_EVT:
  361. default:
  362. goto illegal_evt;
  363. }
  364. break;
  365. default:
  366. pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
  367. }
  368. return rc;
  369. illegal_evt:
  370. pr_err("Illegal FSM event %x in state %x on link %s\n",
  371. evt, l->state, l->name);
  372. return rc;
  373. }
  374. /* link_profile_stats - update statistical profiling of traffic
  375. */
  376. static void link_profile_stats(struct tipc_link *l)
  377. {
  378. struct sk_buff *skb;
  379. struct tipc_msg *msg;
  380. int length;
  381. /* Update counters used in statistical profiling of send traffic */
  382. l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
  383. l->stats.queue_sz_counts++;
  384. skb = skb_peek(&l->transmq);
  385. if (!skb)
  386. return;
  387. msg = buf_msg(skb);
  388. length = msg_size(msg);
  389. if (msg_user(msg) == MSG_FRAGMENTER) {
  390. if (msg_type(msg) != FIRST_FRAGMENT)
  391. return;
  392. length = msg_size(msg_get_wrapped(msg));
  393. }
  394. l->stats.msg_lengths_total += length;
  395. l->stats.msg_length_counts++;
  396. if (length <= 64)
  397. l->stats.msg_length_profile[0]++;
  398. else if (length <= 256)
  399. l->stats.msg_length_profile[1]++;
  400. else if (length <= 1024)
  401. l->stats.msg_length_profile[2]++;
  402. else if (length <= 4096)
  403. l->stats.msg_length_profile[3]++;
  404. else if (length <= 16384)
  405. l->stats.msg_length_profile[4]++;
  406. else if (length <= 32768)
  407. l->stats.msg_length_profile[5]++;
  408. else
  409. l->stats.msg_length_profile[6]++;
  410. }
  411. /* tipc_link_timeout - perform periodic task as instructed from node timeout
  412. */
  413. int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
  414. {
  415. int rc = 0;
  416. int mtyp = STATE_MSG;
  417. bool xmit = false;
  418. bool prb = false;
  419. link_profile_stats(l);
  420. switch (l->state) {
  421. case LINK_ESTABLISHED:
  422. case LINK_SYNCHING:
  423. if (!l->silent_intv_cnt) {
  424. if (tipc_bclink_acks_missing(l->owner))
  425. xmit = true;
  426. } else if (l->silent_intv_cnt <= l->abort_limit) {
  427. xmit = true;
  428. prb = true;
  429. } else {
  430. rc |= tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  431. }
  432. l->silent_intv_cnt++;
  433. break;
  434. case LINK_RESET:
  435. xmit = true;
  436. mtyp = RESET_MSG;
  437. break;
  438. case LINK_ESTABLISHING:
  439. xmit = true;
  440. mtyp = ACTIVATE_MSG;
  441. break;
  442. case LINK_PEER_RESET:
  443. case LINK_RESETTING:
  444. case LINK_FAILINGOVER:
  445. break;
  446. default:
  447. break;
  448. }
  449. if (xmit)
  450. tipc_link_build_proto_msg(l, mtyp, prb, 0, 0, 0, xmitq);
  451. return rc;
  452. }
  453. /**
  454. * link_schedule_user - schedule a message sender for wakeup after congestion
  455. * @link: congested link
  456. * @list: message that was attempted sent
  457. * Create pseudo msg to send back to user when congestion abates
  458. * Does not consume buffer list
  459. */
  460. static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
  461. {
  462. struct tipc_msg *msg = buf_msg(skb_peek(list));
  463. int imp = msg_importance(msg);
  464. u32 oport = msg_origport(msg);
  465. u32 addr = link_own_addr(link);
  466. struct sk_buff *skb;
  467. /* This really cannot happen... */
  468. if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
  469. pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
  470. return -ENOBUFS;
  471. }
  472. /* Non-blocking sender: */
  473. if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
  474. return -ELINKCONG;
  475. /* Create and schedule wakeup pseudo message */
  476. skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
  477. addr, addr, oport, 0, 0);
  478. if (!skb)
  479. return -ENOBUFS;
  480. TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
  481. TIPC_SKB_CB(skb)->chain_imp = imp;
  482. skb_queue_tail(&link->wakeupq, skb);
  483. link->stats.link_congs++;
  484. return -ELINKCONG;
  485. }
  486. /**
  487. * link_prepare_wakeup - prepare users for wakeup after congestion
  488. * @link: congested link
  489. * Move a number of waiting users, as permitted by available space in
  490. * the send queue, from link wait queue to node wait queue for wakeup
  491. */
  492. void link_prepare_wakeup(struct tipc_link *l)
  493. {
  494. int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
  495. int imp, lim;
  496. struct sk_buff *skb, *tmp;
  497. skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
  498. imp = TIPC_SKB_CB(skb)->chain_imp;
  499. lim = l->window + l->backlog[imp].limit;
  500. pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
  501. if ((pnd[imp] + l->backlog[imp].len) >= lim)
  502. break;
  503. skb_unlink(skb, &l->wakeupq);
  504. skb_queue_tail(l->inputq, skb);
  505. }
  506. }
  507. /**
  508. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  509. * @l_ptr: pointer to link
  510. */
  511. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  512. {
  513. kfree_skb(l_ptr->reasm_buf);
  514. l_ptr->reasm_buf = NULL;
  515. }
  516. void tipc_link_purge_backlog(struct tipc_link *l)
  517. {
  518. __skb_queue_purge(&l->backlogq);
  519. l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
  520. l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
  521. l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
  522. l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
  523. l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
  524. }
  525. /**
  526. * tipc_link_purge_queues - purge all pkt queues associated with link
  527. * @l_ptr: pointer to link
  528. */
  529. void tipc_link_purge_queues(struct tipc_link *l_ptr)
  530. {
  531. __skb_queue_purge(&l_ptr->deferdq);
  532. __skb_queue_purge(&l_ptr->transmq);
  533. tipc_link_purge_backlog(l_ptr);
  534. tipc_link_reset_fragments(l_ptr);
  535. }
  536. void tipc_link_reset(struct tipc_link *l)
  537. {
  538. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  539. /* Link is down, accept any session */
  540. l->peer_session = WILDCARD_SESSION;
  541. /* If peer is up, it only accepts an incremented session number */
  542. msg_set_session(l->pmsg, msg_session(l->pmsg) + 1);
  543. /* Prepare for renewed mtu size negotiation */
  544. l->mtu = l->advertised_mtu;
  545. /* Clean up all queues: */
  546. __skb_queue_purge(&l->transmq);
  547. __skb_queue_purge(&l->deferdq);
  548. skb_queue_splice_init(&l->wakeupq, l->inputq);
  549. tipc_link_purge_backlog(l);
  550. kfree_skb(l->reasm_buf);
  551. kfree_skb(l->failover_reasm_skb);
  552. l->reasm_buf = NULL;
  553. l->failover_reasm_skb = NULL;
  554. l->rcv_unacked = 0;
  555. l->snd_nxt = 1;
  556. l->rcv_nxt = 1;
  557. l->silent_intv_cnt = 0;
  558. l->stats.recv_info = 0;
  559. l->stale_count = 0;
  560. link_reset_statistics(l);
  561. }
  562. /**
  563. * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
  564. * @link: link to use
  565. * @list: chain of buffers containing message
  566. *
  567. * Consumes the buffer chain, except when returning an error code,
  568. * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  569. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  570. */
  571. int __tipc_link_xmit(struct net *net, struct tipc_link *link,
  572. struct sk_buff_head *list)
  573. {
  574. struct tipc_msg *msg = buf_msg(skb_peek(list));
  575. unsigned int maxwin = link->window;
  576. unsigned int i, imp = msg_importance(msg);
  577. uint mtu = link->mtu;
  578. u16 ack = mod(link->rcv_nxt - 1);
  579. u16 seqno = link->snd_nxt;
  580. u16 bc_last_in = link->owner->bclink.last_in;
  581. struct tipc_media_addr *addr = link->media_addr;
  582. struct sk_buff_head *transmq = &link->transmq;
  583. struct sk_buff_head *backlogq = &link->backlogq;
  584. struct sk_buff *skb, *bskb;
  585. /* Match msg importance against this and all higher backlog limits: */
  586. for (i = imp; i <= TIPC_SYSTEM_IMPORTANCE; i++) {
  587. if (unlikely(link->backlog[i].len >= link->backlog[i].limit))
  588. return link_schedule_user(link, list);
  589. }
  590. if (unlikely(msg_size(msg) > mtu))
  591. return -EMSGSIZE;
  592. /* Prepare each packet for sending, and add to relevant queue: */
  593. while (skb_queue_len(list)) {
  594. skb = skb_peek(list);
  595. msg = buf_msg(skb);
  596. msg_set_seqno(msg, seqno);
  597. msg_set_ack(msg, ack);
  598. msg_set_bcast_ack(msg, bc_last_in);
  599. if (likely(skb_queue_len(transmq) < maxwin)) {
  600. __skb_dequeue(list);
  601. __skb_queue_tail(transmq, skb);
  602. tipc_bearer_send(net, link->bearer_id, skb, addr);
  603. link->rcv_unacked = 0;
  604. seqno++;
  605. continue;
  606. }
  607. if (tipc_msg_bundle(skb_peek_tail(backlogq), msg, mtu)) {
  608. kfree_skb(__skb_dequeue(list));
  609. link->stats.sent_bundled++;
  610. continue;
  611. }
  612. if (tipc_msg_make_bundle(&bskb, msg, mtu, link->addr)) {
  613. kfree_skb(__skb_dequeue(list));
  614. __skb_queue_tail(backlogq, bskb);
  615. link->backlog[msg_importance(buf_msg(bskb))].len++;
  616. link->stats.sent_bundled++;
  617. link->stats.sent_bundles++;
  618. continue;
  619. }
  620. link->backlog[imp].len += skb_queue_len(list);
  621. skb_queue_splice_tail_init(list, backlogq);
  622. }
  623. link->snd_nxt = seqno;
  624. return 0;
  625. }
  626. /**
  627. * tipc_link_xmit(): enqueue buffer list according to queue situation
  628. * @link: link to use
  629. * @list: chain of buffers containing message
  630. * @xmitq: returned list of packets to be sent by caller
  631. *
  632. * Consumes the buffer chain, except when returning -ELINKCONG,
  633. * since the caller then may want to make more send attempts.
  634. * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  635. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  636. */
  637. int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
  638. struct sk_buff_head *xmitq)
  639. {
  640. struct tipc_msg *hdr = buf_msg(skb_peek(list));
  641. unsigned int maxwin = l->window;
  642. unsigned int i, imp = msg_importance(hdr);
  643. unsigned int mtu = l->mtu;
  644. u16 ack = l->rcv_nxt - 1;
  645. u16 seqno = l->snd_nxt;
  646. u16 bc_last_in = l->owner->bclink.last_in;
  647. struct sk_buff_head *transmq = &l->transmq;
  648. struct sk_buff_head *backlogq = &l->backlogq;
  649. struct sk_buff *skb, *_skb, *bskb;
  650. /* Match msg importance against this and all higher backlog limits: */
  651. for (i = imp; i <= TIPC_SYSTEM_IMPORTANCE; i++) {
  652. if (unlikely(l->backlog[i].len >= l->backlog[i].limit))
  653. return link_schedule_user(l, list);
  654. }
  655. if (unlikely(msg_size(hdr) > mtu))
  656. return -EMSGSIZE;
  657. /* Prepare each packet for sending, and add to relevant queue: */
  658. while (skb_queue_len(list)) {
  659. skb = skb_peek(list);
  660. hdr = buf_msg(skb);
  661. msg_set_seqno(hdr, seqno);
  662. msg_set_ack(hdr, ack);
  663. msg_set_bcast_ack(hdr, bc_last_in);
  664. if (likely(skb_queue_len(transmq) < maxwin)) {
  665. _skb = skb_clone(skb, GFP_ATOMIC);
  666. if (!_skb)
  667. return -ENOBUFS;
  668. __skb_dequeue(list);
  669. __skb_queue_tail(transmq, skb);
  670. __skb_queue_tail(xmitq, _skb);
  671. l->rcv_unacked = 0;
  672. seqno++;
  673. continue;
  674. }
  675. if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
  676. kfree_skb(__skb_dequeue(list));
  677. l->stats.sent_bundled++;
  678. continue;
  679. }
  680. if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
  681. kfree_skb(__skb_dequeue(list));
  682. __skb_queue_tail(backlogq, bskb);
  683. l->backlog[msg_importance(buf_msg(bskb))].len++;
  684. l->stats.sent_bundled++;
  685. l->stats.sent_bundles++;
  686. continue;
  687. }
  688. l->backlog[imp].len += skb_queue_len(list);
  689. skb_queue_splice_tail_init(list, backlogq);
  690. }
  691. l->snd_nxt = seqno;
  692. return 0;
  693. }
  694. /*
  695. * tipc_link_sync_rcv - synchronize broadcast link endpoints.
  696. * Receive the sequence number where we should start receiving and
  697. * acking broadcast packets from a newly added peer node, and open
  698. * up for reception of such packets.
  699. *
  700. * Called with node locked
  701. */
  702. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
  703. {
  704. struct tipc_msg *msg = buf_msg(buf);
  705. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  706. n->bclink.recv_permitted = true;
  707. kfree_skb(buf);
  708. }
  709. /*
  710. * tipc_link_push_packets - push unsent packets to bearer
  711. *
  712. * Push out the unsent messages of a link where congestion
  713. * has abated. Node is locked.
  714. *
  715. * Called with node locked
  716. */
  717. void tipc_link_push_packets(struct tipc_link *link)
  718. {
  719. struct sk_buff *skb;
  720. struct tipc_msg *msg;
  721. u16 seqno = link->snd_nxt;
  722. u16 ack = mod(link->rcv_nxt - 1);
  723. while (skb_queue_len(&link->transmq) < link->window) {
  724. skb = __skb_dequeue(&link->backlogq);
  725. if (!skb)
  726. break;
  727. msg = buf_msg(skb);
  728. link->backlog[msg_importance(msg)].len--;
  729. msg_set_ack(msg, ack);
  730. msg_set_seqno(msg, seqno);
  731. seqno = mod(seqno + 1);
  732. msg_set_bcast_ack(msg, link->owner->bclink.last_in);
  733. link->rcv_unacked = 0;
  734. __skb_queue_tail(&link->transmq, skb);
  735. tipc_bearer_send(link->owner->net, link->bearer_id,
  736. skb, link->media_addr);
  737. }
  738. link->snd_nxt = seqno;
  739. }
  740. void tipc_link_advance_backlog(struct tipc_link *l, struct sk_buff_head *xmitq)
  741. {
  742. struct sk_buff *skb, *_skb;
  743. struct tipc_msg *hdr;
  744. u16 seqno = l->snd_nxt;
  745. u16 ack = l->rcv_nxt - 1;
  746. while (skb_queue_len(&l->transmq) < l->window) {
  747. skb = skb_peek(&l->backlogq);
  748. if (!skb)
  749. break;
  750. _skb = skb_clone(skb, GFP_ATOMIC);
  751. if (!_skb)
  752. break;
  753. __skb_dequeue(&l->backlogq);
  754. hdr = buf_msg(skb);
  755. l->backlog[msg_importance(hdr)].len--;
  756. __skb_queue_tail(&l->transmq, skb);
  757. __skb_queue_tail(xmitq, _skb);
  758. msg_set_ack(hdr, ack);
  759. msg_set_seqno(hdr, seqno);
  760. msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
  761. l->rcv_unacked = 0;
  762. seqno++;
  763. }
  764. l->snd_nxt = seqno;
  765. }
  766. static void link_retransmit_failure(struct tipc_link *l_ptr,
  767. struct sk_buff *buf)
  768. {
  769. struct tipc_msg *msg = buf_msg(buf);
  770. struct net *net = l_ptr->owner->net;
  771. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  772. if (l_ptr->addr) {
  773. /* Handle failure on standard link */
  774. link_print(l_ptr, "Resetting link ");
  775. pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
  776. msg_user(msg), msg_type(msg), msg_size(msg),
  777. msg_errcode(msg));
  778. pr_info("sqno %u, prev: %x, src: %x\n",
  779. msg_seqno(msg), msg_prevnode(msg), msg_orignode(msg));
  780. } else {
  781. /* Handle failure on broadcast link */
  782. struct tipc_node *n_ptr;
  783. char addr_string[16];
  784. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  785. pr_cont("Outstanding acks: %lu\n",
  786. (unsigned long) TIPC_SKB_CB(buf)->handle);
  787. n_ptr = tipc_bclink_retransmit_to(net);
  788. tipc_addr_string_fill(addr_string, n_ptr->addr);
  789. pr_info("Broadcast link info for %s\n", addr_string);
  790. pr_info("Reception permitted: %d, Acked: %u\n",
  791. n_ptr->bclink.recv_permitted,
  792. n_ptr->bclink.acked);
  793. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  794. n_ptr->bclink.last_in,
  795. n_ptr->bclink.oos_state,
  796. n_ptr->bclink.last_sent);
  797. n_ptr->action_flags |= TIPC_BCAST_RESET;
  798. l_ptr->stale_count = 0;
  799. }
  800. }
  801. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
  802. u32 retransmits)
  803. {
  804. struct tipc_msg *msg;
  805. if (!skb)
  806. return;
  807. msg = buf_msg(skb);
  808. /* Detect repeated retransmit failures */
  809. if (l_ptr->last_retransm == msg_seqno(msg)) {
  810. if (++l_ptr->stale_count > 100) {
  811. link_retransmit_failure(l_ptr, skb);
  812. return;
  813. }
  814. } else {
  815. l_ptr->last_retransm = msg_seqno(msg);
  816. l_ptr->stale_count = 1;
  817. }
  818. skb_queue_walk_from(&l_ptr->transmq, skb) {
  819. if (!retransmits)
  820. break;
  821. msg = buf_msg(skb);
  822. msg_set_ack(msg, mod(l_ptr->rcv_nxt - 1));
  823. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  824. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
  825. l_ptr->media_addr);
  826. retransmits--;
  827. l_ptr->stats.retransmitted++;
  828. }
  829. }
  830. static int tipc_link_retransm(struct tipc_link *l, int retransm,
  831. struct sk_buff_head *xmitq)
  832. {
  833. struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
  834. struct tipc_msg *hdr;
  835. if (!skb)
  836. return 0;
  837. /* Detect repeated retransmit failures on same packet */
  838. if (likely(l->last_retransm != buf_seqno(skb))) {
  839. l->last_retransm = buf_seqno(skb);
  840. l->stale_count = 1;
  841. } else if (++l->stale_count > 100) {
  842. link_retransmit_failure(l, skb);
  843. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  844. }
  845. skb_queue_walk(&l->transmq, skb) {
  846. if (!retransm)
  847. return 0;
  848. hdr = buf_msg(skb);
  849. _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
  850. if (!_skb)
  851. return 0;
  852. hdr = buf_msg(_skb);
  853. msg_set_ack(hdr, l->rcv_nxt - 1);
  854. msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
  855. _skb->priority = TC_PRIO_CONTROL;
  856. __skb_queue_tail(xmitq, _skb);
  857. retransm--;
  858. l->stats.retransmitted++;
  859. }
  860. return 0;
  861. }
  862. /* tipc_data_input - deliver data and name distr msgs to upper layer
  863. *
  864. * Consumes buffer if message is of right type
  865. * Node lock must be held
  866. */
  867. static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb,
  868. struct sk_buff_head *inputq)
  869. {
  870. struct tipc_node *node = link->owner;
  871. switch (msg_user(buf_msg(skb))) {
  872. case TIPC_LOW_IMPORTANCE:
  873. case TIPC_MEDIUM_IMPORTANCE:
  874. case TIPC_HIGH_IMPORTANCE:
  875. case TIPC_CRITICAL_IMPORTANCE:
  876. case CONN_MANAGER:
  877. __skb_queue_tail(inputq, skb);
  878. return true;
  879. case NAME_DISTRIBUTOR:
  880. node->bclink.recv_permitted = true;
  881. skb_queue_tail(link->namedq, skb);
  882. return true;
  883. case MSG_BUNDLER:
  884. case TUNNEL_PROTOCOL:
  885. case MSG_FRAGMENTER:
  886. case BCAST_PROTOCOL:
  887. return false;
  888. default:
  889. pr_warn("Dropping received illegal msg type\n");
  890. kfree_skb(skb);
  891. return false;
  892. };
  893. }
  894. /* tipc_link_input - process packet that has passed link protocol check
  895. *
  896. * Consumes buffer
  897. */
  898. static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
  899. struct sk_buff_head *inputq)
  900. {
  901. struct tipc_node *node = l->owner;
  902. struct tipc_msg *hdr = buf_msg(skb);
  903. struct sk_buff **reasm_skb = &l->reasm_buf;
  904. struct sk_buff *iskb;
  905. int usr = msg_user(hdr);
  906. int rc = 0;
  907. int pos = 0;
  908. int ipos = 0;
  909. if (unlikely(usr == TUNNEL_PROTOCOL)) {
  910. if (msg_type(hdr) == SYNCH_MSG) {
  911. __skb_queue_purge(&l->deferdq);
  912. goto drop;
  913. }
  914. if (!tipc_msg_extract(skb, &iskb, &ipos))
  915. return rc;
  916. kfree_skb(skb);
  917. skb = iskb;
  918. hdr = buf_msg(skb);
  919. if (less(msg_seqno(hdr), l->drop_point))
  920. goto drop;
  921. if (tipc_data_input(l, skb, inputq))
  922. return rc;
  923. usr = msg_user(hdr);
  924. reasm_skb = &l->failover_reasm_skb;
  925. }
  926. if (usr == MSG_BUNDLER) {
  927. l->stats.recv_bundles++;
  928. l->stats.recv_bundled += msg_msgcnt(hdr);
  929. while (tipc_msg_extract(skb, &iskb, &pos))
  930. tipc_data_input(l, iskb, inputq);
  931. return 0;
  932. } else if (usr == MSG_FRAGMENTER) {
  933. l->stats.recv_fragments++;
  934. if (tipc_buf_append(reasm_skb, &skb)) {
  935. l->stats.recv_fragmented++;
  936. tipc_data_input(l, skb, inputq);
  937. } else if (!*reasm_skb) {
  938. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  939. }
  940. return 0;
  941. } else if (usr == BCAST_PROTOCOL) {
  942. tipc_link_sync_rcv(node, skb);
  943. return 0;
  944. }
  945. drop:
  946. kfree_skb(skb);
  947. return 0;
  948. }
  949. static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
  950. {
  951. bool released = false;
  952. struct sk_buff *skb, *tmp;
  953. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  954. if (more(buf_seqno(skb), acked))
  955. break;
  956. __skb_unlink(skb, &l->transmq);
  957. kfree_skb(skb);
  958. released = true;
  959. }
  960. return released;
  961. }
  962. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  963. * @link: the link that should handle the message
  964. * @skb: TIPC packet
  965. * @xmitq: queue to place packets to be sent after this call
  966. */
  967. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  968. struct sk_buff_head *xmitq)
  969. {
  970. struct sk_buff_head *arrvq = &l->deferdq;
  971. struct sk_buff_head tmpq;
  972. struct tipc_msg *hdr;
  973. u16 seqno, rcv_nxt;
  974. int rc = 0;
  975. __skb_queue_head_init(&tmpq);
  976. if (unlikely(!__tipc_skb_queue_sorted(arrvq, skb))) {
  977. if (!(skb_queue_len(arrvq) % TIPC_NACK_INTV))
  978. tipc_link_build_proto_msg(l, STATE_MSG, 0,
  979. 0, 0, 0, xmitq);
  980. return rc;
  981. }
  982. while ((skb = skb_peek(arrvq))) {
  983. hdr = buf_msg(skb);
  984. /* Verify and update link state */
  985. if (unlikely(msg_user(hdr) == LINK_PROTOCOL)) {
  986. __skb_dequeue(arrvq);
  987. rc = tipc_link_proto_rcv(l, skb, xmitq);
  988. continue;
  989. }
  990. if (unlikely(!link_is_up(l))) {
  991. rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  992. if (!link_is_up(l)) {
  993. kfree_skb(__skb_dequeue(arrvq));
  994. goto exit;
  995. }
  996. }
  997. l->silent_intv_cnt = 0;
  998. /* Forward queues and wake up waiting users */
  999. if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
  1000. tipc_link_advance_backlog(l, xmitq);
  1001. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1002. link_prepare_wakeup(l);
  1003. }
  1004. /* Defer reception if there is a gap in the sequence */
  1005. seqno = msg_seqno(hdr);
  1006. rcv_nxt = l->rcv_nxt;
  1007. if (unlikely(less(rcv_nxt, seqno))) {
  1008. l->stats.deferred_recv++;
  1009. goto exit;
  1010. }
  1011. __skb_dequeue(arrvq);
  1012. /* Drop if packet already received */
  1013. if (unlikely(more(rcv_nxt, seqno))) {
  1014. l->stats.duplicates++;
  1015. kfree_skb(skb);
  1016. goto exit;
  1017. }
  1018. /* Packet can be delivered */
  1019. l->rcv_nxt++;
  1020. l->stats.recv_info++;
  1021. if (unlikely(!tipc_data_input(l, skb, &tmpq)))
  1022. rc = tipc_link_input(l, skb, &tmpq);
  1023. /* Ack at regular intervals */
  1024. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
  1025. l->rcv_unacked = 0;
  1026. l->stats.sent_acks++;
  1027. tipc_link_build_proto_msg(l, STATE_MSG,
  1028. 0, 0, 0, 0, xmitq);
  1029. }
  1030. }
  1031. exit:
  1032. tipc_skb_queue_splice_tail(&tmpq, l->inputq);
  1033. return rc;
  1034. }
  1035. /**
  1036. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1037. *
  1038. * Returns increase in queue length (i.e. 0 or 1)
  1039. */
  1040. u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
  1041. {
  1042. struct sk_buff *skb1;
  1043. u16 seq_no = buf_seqno(skb);
  1044. /* Empty queue ? */
  1045. if (skb_queue_empty(list)) {
  1046. __skb_queue_tail(list, skb);
  1047. return 1;
  1048. }
  1049. /* Last ? */
  1050. if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
  1051. __skb_queue_tail(list, skb);
  1052. return 1;
  1053. }
  1054. /* Locate insertion point in queue, then insert; discard if duplicate */
  1055. skb_queue_walk(list, skb1) {
  1056. u16 curr_seqno = buf_seqno(skb1);
  1057. if (seq_no == curr_seqno) {
  1058. kfree_skb(skb);
  1059. return 0;
  1060. }
  1061. if (less(seq_no, curr_seqno))
  1062. break;
  1063. }
  1064. __skb_queue_before(list, skb1, skb);
  1065. return 1;
  1066. }
  1067. /*
  1068. * Send protocol message to the other endpoint.
  1069. */
  1070. void tipc_link_proto_xmit(struct tipc_link *l, u32 msg_typ, int probe_msg,
  1071. u32 gap, u32 tolerance, u32 priority)
  1072. {
  1073. struct sk_buff *skb = NULL;
  1074. struct sk_buff_head xmitq;
  1075. __skb_queue_head_init(&xmitq);
  1076. tipc_link_build_proto_msg(l, msg_typ, probe_msg, gap,
  1077. tolerance, priority, &xmitq);
  1078. skb = __skb_dequeue(&xmitq);
  1079. if (!skb)
  1080. return;
  1081. tipc_bearer_send(l->owner->net, l->bearer_id, skb, l->media_addr);
  1082. l->rcv_unacked = 0;
  1083. kfree_skb(skb);
  1084. }
  1085. /* tipc_link_build_proto_msg: prepare link protocol message for transmission
  1086. */
  1087. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1088. u16 rcvgap, int tolerance, int priority,
  1089. struct sk_buff_head *xmitq)
  1090. {
  1091. struct sk_buff *skb = NULL;
  1092. struct tipc_msg *hdr = l->pmsg;
  1093. u16 snd_nxt = l->snd_nxt;
  1094. u16 rcv_nxt = l->rcv_nxt;
  1095. u16 rcv_last = rcv_nxt - 1;
  1096. int node_up = l->owner->bclink.recv_permitted;
  1097. /* Don't send protocol message during reset or link failover */
  1098. if (tipc_link_is_blocked(l))
  1099. return;
  1100. msg_set_type(hdr, mtyp);
  1101. msg_set_net_plane(hdr, l->net_plane);
  1102. msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
  1103. msg_set_last_bcast(hdr, tipc_bclink_get_last_sent(l->owner->net));
  1104. msg_set_link_tolerance(hdr, tolerance);
  1105. msg_set_linkprio(hdr, priority);
  1106. msg_set_redundant_link(hdr, node_up);
  1107. msg_set_seq_gap(hdr, 0);
  1108. /* Compatibility: created msg must not be in sequence with pkt flow */
  1109. msg_set_seqno(hdr, snd_nxt + U16_MAX / 2);
  1110. if (mtyp == STATE_MSG) {
  1111. if (!tipc_link_is_up(l))
  1112. return;
  1113. msg_set_next_sent(hdr, snd_nxt);
  1114. /* Override rcvgap if there are packets in deferred queue */
  1115. if (!skb_queue_empty(&l->deferdq))
  1116. rcvgap = buf_seqno(skb_peek(&l->deferdq)) - rcv_nxt;
  1117. if (rcvgap) {
  1118. msg_set_seq_gap(hdr, rcvgap);
  1119. l->stats.sent_nacks++;
  1120. }
  1121. msg_set_ack(hdr, rcv_last);
  1122. msg_set_probe(hdr, probe);
  1123. if (probe)
  1124. l->stats.sent_probes++;
  1125. l->stats.sent_states++;
  1126. } else {
  1127. /* RESET_MSG or ACTIVATE_MSG */
  1128. msg_set_max_pkt(hdr, l->advertised_mtu);
  1129. msg_set_ack(hdr, l->rcv_nxt - 1);
  1130. msg_set_next_sent(hdr, 1);
  1131. }
  1132. skb = tipc_buf_acquire(msg_size(hdr));
  1133. if (!skb)
  1134. return;
  1135. skb_copy_to_linear_data(skb, hdr, msg_size(hdr));
  1136. skb->priority = TC_PRIO_CONTROL;
  1137. __skb_queue_tail(xmitq, skb);
  1138. }
  1139. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1140. * with contents of the link's tranmsit and backlog queues.
  1141. */
  1142. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1143. int mtyp, struct sk_buff_head *xmitq)
  1144. {
  1145. struct sk_buff *skb, *tnlskb;
  1146. struct tipc_msg *hdr, tnlhdr;
  1147. struct sk_buff_head *queue = &l->transmq;
  1148. struct sk_buff_head tmpxq, tnlq;
  1149. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1150. if (!tnl)
  1151. return;
  1152. skb_queue_head_init(&tnlq);
  1153. skb_queue_head_init(&tmpxq);
  1154. /* At least one packet required for safe algorithm => add dummy */
  1155. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1156. BASIC_H_SIZE, 0, l->addr, link_own_addr(l),
  1157. 0, 0, TIPC_ERR_NO_PORT);
  1158. if (!skb) {
  1159. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1160. return;
  1161. }
  1162. skb_queue_tail(&tnlq, skb);
  1163. tipc_link_xmit(l, &tnlq, &tmpxq);
  1164. __skb_queue_purge(&tmpxq);
  1165. /* Initialize reusable tunnel packet header */
  1166. tipc_msg_init(link_own_addr(l), &tnlhdr, TUNNEL_PROTOCOL,
  1167. mtyp, INT_H_SIZE, l->addr);
  1168. pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
  1169. msg_set_msgcnt(&tnlhdr, pktcnt);
  1170. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1171. tnl:
  1172. /* Wrap each packet into a tunnel packet */
  1173. skb_queue_walk(queue, skb) {
  1174. hdr = buf_msg(skb);
  1175. if (queue == &l->backlogq)
  1176. msg_set_seqno(hdr, seqno++);
  1177. pktlen = msg_size(hdr);
  1178. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1179. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE);
  1180. if (!tnlskb) {
  1181. pr_warn("%sunable to send packet\n", link_co_err);
  1182. return;
  1183. }
  1184. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1185. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1186. __skb_queue_tail(&tnlq, tnlskb);
  1187. }
  1188. if (queue != &l->backlogq) {
  1189. queue = &l->backlogq;
  1190. goto tnl;
  1191. }
  1192. tipc_link_xmit(tnl, &tnlq, xmitq);
  1193. if (mtyp == FAILOVER_MSG) {
  1194. tnl->drop_point = l->rcv_nxt;
  1195. tnl->failover_reasm_skb = l->reasm_buf;
  1196. l->reasm_buf = NULL;
  1197. }
  1198. }
  1199. /* tipc_link_proto_rcv(): receive link level protocol message :
  1200. * Note that network plane id propagates through the network, and may
  1201. * change at any time. The node with lowest numerical id determines
  1202. * network plane
  1203. */
  1204. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1205. struct sk_buff_head *xmitq)
  1206. {
  1207. struct tipc_msg *hdr = buf_msg(skb);
  1208. u16 rcvgap = 0;
  1209. u16 nacked_gap = msg_seq_gap(hdr);
  1210. u16 peers_snd_nxt = msg_next_sent(hdr);
  1211. u16 peers_tol = msg_link_tolerance(hdr);
  1212. u16 peers_prio = msg_linkprio(hdr);
  1213. char *if_name;
  1214. int rc = 0;
  1215. if (tipc_link_is_blocked(l))
  1216. goto exit;
  1217. if (link_own_addr(l) > msg_prevnode(hdr))
  1218. l->net_plane = msg_net_plane(hdr);
  1219. switch (msg_type(hdr)) {
  1220. case RESET_MSG:
  1221. /* Ignore duplicate RESET with old session number */
  1222. if ((less_eq(msg_session(hdr), l->peer_session)) &&
  1223. (l->peer_session != WILDCARD_SESSION))
  1224. break;
  1225. /* fall thru' */
  1226. case ACTIVATE_MSG:
  1227. /* Complete own link name with peer's interface name */
  1228. if_name = strrchr(l->name, ':') + 1;
  1229. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  1230. break;
  1231. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  1232. break;
  1233. strncpy(if_name, msg_data(hdr), TIPC_MAX_IF_NAME);
  1234. /* Update own tolerance if peer indicates a non-zero value */
  1235. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1236. l->tolerance = peers_tol;
  1237. /* Update own priority if peer's priority is higher */
  1238. if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  1239. l->priority = peers_prio;
  1240. if (msg_type(hdr) == RESET_MSG) {
  1241. rc |= tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1242. } else if (!link_is_up(l)) {
  1243. tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1244. rc |= tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1245. }
  1246. l->peer_session = msg_session(hdr);
  1247. l->peer_bearer_id = msg_bearer_id(hdr);
  1248. if (l->mtu > msg_max_pkt(hdr))
  1249. l->mtu = msg_max_pkt(hdr);
  1250. break;
  1251. case STATE_MSG:
  1252. /* Update own tolerance if peer indicates a non-zero value */
  1253. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1254. l->tolerance = peers_tol;
  1255. l->silent_intv_cnt = 0;
  1256. l->stats.recv_states++;
  1257. if (msg_probe(hdr))
  1258. l->stats.recv_probes++;
  1259. rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1260. if (!link_is_up(l))
  1261. break;
  1262. /* Send NACK if peer has sent pkts we haven't received yet */
  1263. if (more(peers_snd_nxt, l->rcv_nxt))
  1264. rcvgap = peers_snd_nxt - l->rcv_nxt;
  1265. if (rcvgap || (msg_probe(hdr)))
  1266. tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
  1267. 0, 0, xmitq);
  1268. tipc_link_release_pkts(l, msg_ack(hdr));
  1269. /* If NACK, retransmit will now start at right position */
  1270. if (nacked_gap) {
  1271. rc = tipc_link_retransm(l, nacked_gap, xmitq);
  1272. l->stats.recv_nacks++;
  1273. }
  1274. tipc_link_advance_backlog(l, xmitq);
  1275. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1276. link_prepare_wakeup(l);
  1277. }
  1278. exit:
  1279. kfree_skb(skb);
  1280. return rc;
  1281. }
  1282. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1283. {
  1284. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
  1285. l->window = win;
  1286. l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
  1287. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
  1288. l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
  1289. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
  1290. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1291. }
  1292. /* tipc_link_find_owner - locate owner node of link by link's name
  1293. * @net: the applicable net namespace
  1294. * @name: pointer to link name string
  1295. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1296. *
  1297. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1298. */
  1299. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1300. const char *link_name,
  1301. unsigned int *bearer_id)
  1302. {
  1303. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1304. struct tipc_link *l_ptr;
  1305. struct tipc_node *n_ptr;
  1306. struct tipc_node *found_node = NULL;
  1307. int i;
  1308. *bearer_id = 0;
  1309. rcu_read_lock();
  1310. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1311. tipc_node_lock(n_ptr);
  1312. for (i = 0; i < MAX_BEARERS; i++) {
  1313. l_ptr = n_ptr->links[i].link;
  1314. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1315. *bearer_id = i;
  1316. found_node = n_ptr;
  1317. break;
  1318. }
  1319. }
  1320. tipc_node_unlock(n_ptr);
  1321. if (found_node)
  1322. break;
  1323. }
  1324. rcu_read_unlock();
  1325. return found_node;
  1326. }
  1327. /**
  1328. * link_reset_statistics - reset link statistics
  1329. * @l_ptr: pointer to link
  1330. */
  1331. static void link_reset_statistics(struct tipc_link *l_ptr)
  1332. {
  1333. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1334. l_ptr->stats.sent_info = l_ptr->snd_nxt;
  1335. l_ptr->stats.recv_info = l_ptr->rcv_nxt;
  1336. }
  1337. static void link_print(struct tipc_link *l, const char *str)
  1338. {
  1339. struct sk_buff *hskb = skb_peek(&l->transmq);
  1340. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt;
  1341. u16 tail = l->snd_nxt - 1;
  1342. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  1343. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  1344. skb_queue_len(&l->transmq), head, tail,
  1345. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  1346. }
  1347. /* Parse and validate nested (link) properties valid for media, bearer and link
  1348. */
  1349. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1350. {
  1351. int err;
  1352. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1353. tipc_nl_prop_policy);
  1354. if (err)
  1355. return err;
  1356. if (props[TIPC_NLA_PROP_PRIO]) {
  1357. u32 prio;
  1358. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1359. if (prio > TIPC_MAX_LINK_PRI)
  1360. return -EINVAL;
  1361. }
  1362. if (props[TIPC_NLA_PROP_TOL]) {
  1363. u32 tol;
  1364. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1365. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1366. return -EINVAL;
  1367. }
  1368. if (props[TIPC_NLA_PROP_WIN]) {
  1369. u32 win;
  1370. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1371. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1372. return -EINVAL;
  1373. }
  1374. return 0;
  1375. }
  1376. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  1377. {
  1378. int err;
  1379. int res = 0;
  1380. int bearer_id;
  1381. char *name;
  1382. struct tipc_link *link;
  1383. struct tipc_node *node;
  1384. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1385. struct net *net = sock_net(skb->sk);
  1386. if (!info->attrs[TIPC_NLA_LINK])
  1387. return -EINVAL;
  1388. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1389. info->attrs[TIPC_NLA_LINK],
  1390. tipc_nl_link_policy);
  1391. if (err)
  1392. return err;
  1393. if (!attrs[TIPC_NLA_LINK_NAME])
  1394. return -EINVAL;
  1395. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1396. if (strcmp(name, tipc_bclink_name) == 0)
  1397. return tipc_nl_bc_link_set(net, attrs);
  1398. node = tipc_link_find_owner(net, name, &bearer_id);
  1399. if (!node)
  1400. return -EINVAL;
  1401. tipc_node_lock(node);
  1402. link = node->links[bearer_id].link;
  1403. if (!link) {
  1404. res = -EINVAL;
  1405. goto out;
  1406. }
  1407. if (attrs[TIPC_NLA_LINK_PROP]) {
  1408. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1409. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1410. props);
  1411. if (err) {
  1412. res = err;
  1413. goto out;
  1414. }
  1415. if (props[TIPC_NLA_PROP_TOL]) {
  1416. u32 tol;
  1417. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1418. link->tolerance = tol;
  1419. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
  1420. }
  1421. if (props[TIPC_NLA_PROP_PRIO]) {
  1422. u32 prio;
  1423. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1424. link->priority = prio;
  1425. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
  1426. }
  1427. if (props[TIPC_NLA_PROP_WIN]) {
  1428. u32 win;
  1429. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1430. tipc_link_set_queue_limits(link, win);
  1431. }
  1432. }
  1433. out:
  1434. tipc_node_unlock(node);
  1435. return res;
  1436. }
  1437. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1438. {
  1439. int i;
  1440. struct nlattr *stats;
  1441. struct nla_map {
  1442. u32 key;
  1443. u32 val;
  1444. };
  1445. struct nla_map map[] = {
  1446. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  1447. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1448. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1449. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1450. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1451. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  1452. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1453. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1454. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1455. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1456. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1457. s->msg_length_counts : 1},
  1458. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1459. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1460. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1461. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1462. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1463. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1464. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1465. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1466. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1467. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1468. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1469. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1470. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1471. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1472. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1473. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1474. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1475. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1476. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1477. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1478. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1479. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1480. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1481. };
  1482. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1483. if (!stats)
  1484. return -EMSGSIZE;
  1485. for (i = 0; i < ARRAY_SIZE(map); i++)
  1486. if (nla_put_u32(skb, map[i].key, map[i].val))
  1487. goto msg_full;
  1488. nla_nest_end(skb, stats);
  1489. return 0;
  1490. msg_full:
  1491. nla_nest_cancel(skb, stats);
  1492. return -EMSGSIZE;
  1493. }
  1494. /* Caller should hold appropriate locks to protect the link */
  1495. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1496. struct tipc_link *link, int nlflags)
  1497. {
  1498. int err;
  1499. void *hdr;
  1500. struct nlattr *attrs;
  1501. struct nlattr *prop;
  1502. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1503. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1504. nlflags, TIPC_NL_LINK_GET);
  1505. if (!hdr)
  1506. return -EMSGSIZE;
  1507. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1508. if (!attrs)
  1509. goto msg_full;
  1510. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1511. goto attr_msg_full;
  1512. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1513. tipc_cluster_mask(tn->own_addr)))
  1514. goto attr_msg_full;
  1515. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1516. goto attr_msg_full;
  1517. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->rcv_nxt))
  1518. goto attr_msg_full;
  1519. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->snd_nxt))
  1520. goto attr_msg_full;
  1521. if (tipc_link_is_up(link))
  1522. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1523. goto attr_msg_full;
  1524. if (tipc_link_is_active(link))
  1525. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1526. goto attr_msg_full;
  1527. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1528. if (!prop)
  1529. goto attr_msg_full;
  1530. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1531. goto prop_msg_full;
  1532. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1533. goto prop_msg_full;
  1534. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1535. link->window))
  1536. goto prop_msg_full;
  1537. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1538. goto prop_msg_full;
  1539. nla_nest_end(msg->skb, prop);
  1540. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1541. if (err)
  1542. goto attr_msg_full;
  1543. nla_nest_end(msg->skb, attrs);
  1544. genlmsg_end(msg->skb, hdr);
  1545. return 0;
  1546. prop_msg_full:
  1547. nla_nest_cancel(msg->skb, prop);
  1548. attr_msg_full:
  1549. nla_nest_cancel(msg->skb, attrs);
  1550. msg_full:
  1551. genlmsg_cancel(msg->skb, hdr);
  1552. return -EMSGSIZE;
  1553. }
  1554. /* Caller should hold node lock */
  1555. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1556. struct tipc_node *node, u32 *prev_link)
  1557. {
  1558. u32 i;
  1559. int err;
  1560. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1561. *prev_link = i;
  1562. if (!node->links[i].link)
  1563. continue;
  1564. err = __tipc_nl_add_link(net, msg,
  1565. node->links[i].link, NLM_F_MULTI);
  1566. if (err)
  1567. return err;
  1568. }
  1569. *prev_link = 0;
  1570. return 0;
  1571. }
  1572. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1573. {
  1574. struct net *net = sock_net(skb->sk);
  1575. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1576. struct tipc_node *node;
  1577. struct tipc_nl_msg msg;
  1578. u32 prev_node = cb->args[0];
  1579. u32 prev_link = cb->args[1];
  1580. int done = cb->args[2];
  1581. int err;
  1582. if (done)
  1583. return 0;
  1584. msg.skb = skb;
  1585. msg.portid = NETLINK_CB(cb->skb).portid;
  1586. msg.seq = cb->nlh->nlmsg_seq;
  1587. rcu_read_lock();
  1588. if (prev_node) {
  1589. node = tipc_node_find(net, prev_node);
  1590. if (!node) {
  1591. /* We never set seq or call nl_dump_check_consistent()
  1592. * this means that setting prev_seq here will cause the
  1593. * consistence check to fail in the netlink callback
  1594. * handler. Resulting in the last NLMSG_DONE message
  1595. * having the NLM_F_DUMP_INTR flag set.
  1596. */
  1597. cb->prev_seq = 1;
  1598. goto out;
  1599. }
  1600. tipc_node_put(node);
  1601. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1602. list) {
  1603. tipc_node_lock(node);
  1604. err = __tipc_nl_add_node_links(net, &msg, node,
  1605. &prev_link);
  1606. tipc_node_unlock(node);
  1607. if (err)
  1608. goto out;
  1609. prev_node = node->addr;
  1610. }
  1611. } else {
  1612. err = tipc_nl_add_bc_link(net, &msg);
  1613. if (err)
  1614. goto out;
  1615. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1616. tipc_node_lock(node);
  1617. err = __tipc_nl_add_node_links(net, &msg, node,
  1618. &prev_link);
  1619. tipc_node_unlock(node);
  1620. if (err)
  1621. goto out;
  1622. prev_node = node->addr;
  1623. }
  1624. }
  1625. done = 1;
  1626. out:
  1627. rcu_read_unlock();
  1628. cb->args[0] = prev_node;
  1629. cb->args[1] = prev_link;
  1630. cb->args[2] = done;
  1631. return skb->len;
  1632. }
  1633. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  1634. {
  1635. struct net *net = genl_info_net(info);
  1636. struct tipc_nl_msg msg;
  1637. char *name;
  1638. int err;
  1639. msg.portid = info->snd_portid;
  1640. msg.seq = info->snd_seq;
  1641. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1642. return -EINVAL;
  1643. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1644. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1645. if (!msg.skb)
  1646. return -ENOMEM;
  1647. if (strcmp(name, tipc_bclink_name) == 0) {
  1648. err = tipc_nl_add_bc_link(net, &msg);
  1649. if (err) {
  1650. nlmsg_free(msg.skb);
  1651. return err;
  1652. }
  1653. } else {
  1654. int bearer_id;
  1655. struct tipc_node *node;
  1656. struct tipc_link *link;
  1657. node = tipc_link_find_owner(net, name, &bearer_id);
  1658. if (!node)
  1659. return -EINVAL;
  1660. tipc_node_lock(node);
  1661. link = node->links[bearer_id].link;
  1662. if (!link) {
  1663. tipc_node_unlock(node);
  1664. nlmsg_free(msg.skb);
  1665. return -EINVAL;
  1666. }
  1667. err = __tipc_nl_add_link(net, &msg, link, 0);
  1668. tipc_node_unlock(node);
  1669. if (err) {
  1670. nlmsg_free(msg.skb);
  1671. return err;
  1672. }
  1673. }
  1674. return genlmsg_reply(msg.skb, info);
  1675. }
  1676. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  1677. {
  1678. int err;
  1679. char *link_name;
  1680. unsigned int bearer_id;
  1681. struct tipc_link *link;
  1682. struct tipc_node *node;
  1683. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1684. struct net *net = sock_net(skb->sk);
  1685. if (!info->attrs[TIPC_NLA_LINK])
  1686. return -EINVAL;
  1687. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1688. info->attrs[TIPC_NLA_LINK],
  1689. tipc_nl_link_policy);
  1690. if (err)
  1691. return err;
  1692. if (!attrs[TIPC_NLA_LINK_NAME])
  1693. return -EINVAL;
  1694. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1695. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1696. err = tipc_bclink_reset_stats(net);
  1697. if (err)
  1698. return err;
  1699. return 0;
  1700. }
  1701. node = tipc_link_find_owner(net, link_name, &bearer_id);
  1702. if (!node)
  1703. return -EINVAL;
  1704. tipc_node_lock(node);
  1705. link = node->links[bearer_id].link;
  1706. if (!link) {
  1707. tipc_node_unlock(node);
  1708. return -EINVAL;
  1709. }
  1710. link_reset_statistics(link);
  1711. tipc_node_unlock(node);
  1712. return 0;
  1713. }