link.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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. case LINK_SYNCH_END_EVT:
  330. break;
  331. case LINK_SYNCH_BEGIN_EVT:
  332. l->state = LINK_SYNCHING;
  333. break;
  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. u16 rcv_nxt = l->rcv_nxt;
  1214. char *if_name;
  1215. int rc = 0;
  1216. if (tipc_link_is_blocked(l))
  1217. goto exit;
  1218. if (link_own_addr(l) > msg_prevnode(hdr))
  1219. l->net_plane = msg_net_plane(hdr);
  1220. switch (msg_type(hdr)) {
  1221. case RESET_MSG:
  1222. /* Ignore duplicate RESET with old session number */
  1223. if ((less_eq(msg_session(hdr), l->peer_session)) &&
  1224. (l->peer_session != WILDCARD_SESSION))
  1225. break;
  1226. /* fall thru' */
  1227. case ACTIVATE_MSG:
  1228. /* Complete own link name with peer's interface name */
  1229. if_name = strrchr(l->name, ':') + 1;
  1230. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  1231. break;
  1232. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  1233. break;
  1234. strncpy(if_name, msg_data(hdr), TIPC_MAX_IF_NAME);
  1235. /* Update own tolerance if peer indicates a non-zero value */
  1236. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1237. l->tolerance = peers_tol;
  1238. /* Update own priority if peer's priority is higher */
  1239. if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  1240. l->priority = peers_prio;
  1241. if (msg_type(hdr) == RESET_MSG) {
  1242. rc |= tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1243. } else if (!link_is_up(l)) {
  1244. tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1245. rc |= tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1246. }
  1247. l->peer_session = msg_session(hdr);
  1248. l->peer_bearer_id = msg_bearer_id(hdr);
  1249. if (l->mtu > msg_max_pkt(hdr))
  1250. l->mtu = msg_max_pkt(hdr);
  1251. break;
  1252. case STATE_MSG:
  1253. /* Update own tolerance if peer indicates a non-zero value */
  1254. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1255. l->tolerance = peers_tol;
  1256. l->silent_intv_cnt = 0;
  1257. l->stats.recv_states++;
  1258. if (msg_probe(hdr))
  1259. l->stats.recv_probes++;
  1260. rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1261. if (!link_is_up(l))
  1262. break;
  1263. /* Send NACK if peer has sent pkts we haven't received yet */
  1264. if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
  1265. rcvgap = peers_snd_nxt - l->rcv_nxt;
  1266. if (rcvgap || (msg_probe(hdr)))
  1267. tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
  1268. 0, 0, xmitq);
  1269. tipc_link_release_pkts(l, msg_ack(hdr));
  1270. /* If NACK, retransmit will now start at right position */
  1271. if (nacked_gap) {
  1272. rc = tipc_link_retransm(l, nacked_gap, xmitq);
  1273. l->stats.recv_nacks++;
  1274. }
  1275. tipc_link_advance_backlog(l, xmitq);
  1276. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1277. link_prepare_wakeup(l);
  1278. }
  1279. exit:
  1280. kfree_skb(skb);
  1281. return rc;
  1282. }
  1283. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1284. {
  1285. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
  1286. l->window = win;
  1287. l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
  1288. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
  1289. l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
  1290. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
  1291. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1292. }
  1293. /* tipc_link_find_owner - locate owner node of link by link's name
  1294. * @net: the applicable net namespace
  1295. * @name: pointer to link name string
  1296. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1297. *
  1298. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1299. */
  1300. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1301. const char *link_name,
  1302. unsigned int *bearer_id)
  1303. {
  1304. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1305. struct tipc_link *l_ptr;
  1306. struct tipc_node *n_ptr;
  1307. struct tipc_node *found_node = NULL;
  1308. int i;
  1309. *bearer_id = 0;
  1310. rcu_read_lock();
  1311. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1312. tipc_node_lock(n_ptr);
  1313. for (i = 0; i < MAX_BEARERS; i++) {
  1314. l_ptr = n_ptr->links[i].link;
  1315. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1316. *bearer_id = i;
  1317. found_node = n_ptr;
  1318. break;
  1319. }
  1320. }
  1321. tipc_node_unlock(n_ptr);
  1322. if (found_node)
  1323. break;
  1324. }
  1325. rcu_read_unlock();
  1326. return found_node;
  1327. }
  1328. /**
  1329. * link_reset_statistics - reset link statistics
  1330. * @l_ptr: pointer to link
  1331. */
  1332. static void link_reset_statistics(struct tipc_link *l_ptr)
  1333. {
  1334. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1335. l_ptr->stats.sent_info = l_ptr->snd_nxt;
  1336. l_ptr->stats.recv_info = l_ptr->rcv_nxt;
  1337. }
  1338. static void link_print(struct tipc_link *l, const char *str)
  1339. {
  1340. struct sk_buff *hskb = skb_peek(&l->transmq);
  1341. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt;
  1342. u16 tail = l->snd_nxt - 1;
  1343. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  1344. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  1345. skb_queue_len(&l->transmq), head, tail,
  1346. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  1347. }
  1348. /* Parse and validate nested (link) properties valid for media, bearer and link
  1349. */
  1350. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1351. {
  1352. int err;
  1353. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1354. tipc_nl_prop_policy);
  1355. if (err)
  1356. return err;
  1357. if (props[TIPC_NLA_PROP_PRIO]) {
  1358. u32 prio;
  1359. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1360. if (prio > TIPC_MAX_LINK_PRI)
  1361. return -EINVAL;
  1362. }
  1363. if (props[TIPC_NLA_PROP_TOL]) {
  1364. u32 tol;
  1365. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1366. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1367. return -EINVAL;
  1368. }
  1369. if (props[TIPC_NLA_PROP_WIN]) {
  1370. u32 win;
  1371. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1372. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1373. return -EINVAL;
  1374. }
  1375. return 0;
  1376. }
  1377. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  1378. {
  1379. int err;
  1380. int res = 0;
  1381. int bearer_id;
  1382. char *name;
  1383. struct tipc_link *link;
  1384. struct tipc_node *node;
  1385. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1386. struct net *net = sock_net(skb->sk);
  1387. if (!info->attrs[TIPC_NLA_LINK])
  1388. return -EINVAL;
  1389. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1390. info->attrs[TIPC_NLA_LINK],
  1391. tipc_nl_link_policy);
  1392. if (err)
  1393. return err;
  1394. if (!attrs[TIPC_NLA_LINK_NAME])
  1395. return -EINVAL;
  1396. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1397. if (strcmp(name, tipc_bclink_name) == 0)
  1398. return tipc_nl_bc_link_set(net, attrs);
  1399. node = tipc_link_find_owner(net, name, &bearer_id);
  1400. if (!node)
  1401. return -EINVAL;
  1402. tipc_node_lock(node);
  1403. link = node->links[bearer_id].link;
  1404. if (!link) {
  1405. res = -EINVAL;
  1406. goto out;
  1407. }
  1408. if (attrs[TIPC_NLA_LINK_PROP]) {
  1409. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1410. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1411. props);
  1412. if (err) {
  1413. res = err;
  1414. goto out;
  1415. }
  1416. if (props[TIPC_NLA_PROP_TOL]) {
  1417. u32 tol;
  1418. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1419. link->tolerance = tol;
  1420. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
  1421. }
  1422. if (props[TIPC_NLA_PROP_PRIO]) {
  1423. u32 prio;
  1424. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1425. link->priority = prio;
  1426. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
  1427. }
  1428. if (props[TIPC_NLA_PROP_WIN]) {
  1429. u32 win;
  1430. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1431. tipc_link_set_queue_limits(link, win);
  1432. }
  1433. }
  1434. out:
  1435. tipc_node_unlock(node);
  1436. return res;
  1437. }
  1438. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1439. {
  1440. int i;
  1441. struct nlattr *stats;
  1442. struct nla_map {
  1443. u32 key;
  1444. u32 val;
  1445. };
  1446. struct nla_map map[] = {
  1447. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  1448. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1449. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1450. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1451. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1452. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  1453. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1454. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1455. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1456. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1457. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1458. s->msg_length_counts : 1},
  1459. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1460. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1461. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1462. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1463. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1464. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1465. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1466. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1467. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1468. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1469. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1470. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1471. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1472. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1473. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1474. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1475. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1476. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1477. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1478. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1479. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1480. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1481. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1482. };
  1483. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1484. if (!stats)
  1485. return -EMSGSIZE;
  1486. for (i = 0; i < ARRAY_SIZE(map); i++)
  1487. if (nla_put_u32(skb, map[i].key, map[i].val))
  1488. goto msg_full;
  1489. nla_nest_end(skb, stats);
  1490. return 0;
  1491. msg_full:
  1492. nla_nest_cancel(skb, stats);
  1493. return -EMSGSIZE;
  1494. }
  1495. /* Caller should hold appropriate locks to protect the link */
  1496. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1497. struct tipc_link *link, int nlflags)
  1498. {
  1499. int err;
  1500. void *hdr;
  1501. struct nlattr *attrs;
  1502. struct nlattr *prop;
  1503. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1504. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1505. nlflags, TIPC_NL_LINK_GET);
  1506. if (!hdr)
  1507. return -EMSGSIZE;
  1508. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1509. if (!attrs)
  1510. goto msg_full;
  1511. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1512. goto attr_msg_full;
  1513. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1514. tipc_cluster_mask(tn->own_addr)))
  1515. goto attr_msg_full;
  1516. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1517. goto attr_msg_full;
  1518. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->rcv_nxt))
  1519. goto attr_msg_full;
  1520. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->snd_nxt))
  1521. goto attr_msg_full;
  1522. if (tipc_link_is_up(link))
  1523. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1524. goto attr_msg_full;
  1525. if (tipc_link_is_active(link))
  1526. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1527. goto attr_msg_full;
  1528. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1529. if (!prop)
  1530. goto attr_msg_full;
  1531. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1532. goto prop_msg_full;
  1533. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1534. goto prop_msg_full;
  1535. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1536. link->window))
  1537. goto prop_msg_full;
  1538. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1539. goto prop_msg_full;
  1540. nla_nest_end(msg->skb, prop);
  1541. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1542. if (err)
  1543. goto attr_msg_full;
  1544. nla_nest_end(msg->skb, attrs);
  1545. genlmsg_end(msg->skb, hdr);
  1546. return 0;
  1547. prop_msg_full:
  1548. nla_nest_cancel(msg->skb, prop);
  1549. attr_msg_full:
  1550. nla_nest_cancel(msg->skb, attrs);
  1551. msg_full:
  1552. genlmsg_cancel(msg->skb, hdr);
  1553. return -EMSGSIZE;
  1554. }
  1555. /* Caller should hold node lock */
  1556. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1557. struct tipc_node *node, u32 *prev_link)
  1558. {
  1559. u32 i;
  1560. int err;
  1561. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1562. *prev_link = i;
  1563. if (!node->links[i].link)
  1564. continue;
  1565. err = __tipc_nl_add_link(net, msg,
  1566. node->links[i].link, NLM_F_MULTI);
  1567. if (err)
  1568. return err;
  1569. }
  1570. *prev_link = 0;
  1571. return 0;
  1572. }
  1573. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1574. {
  1575. struct net *net = sock_net(skb->sk);
  1576. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1577. struct tipc_node *node;
  1578. struct tipc_nl_msg msg;
  1579. u32 prev_node = cb->args[0];
  1580. u32 prev_link = cb->args[1];
  1581. int done = cb->args[2];
  1582. int err;
  1583. if (done)
  1584. return 0;
  1585. msg.skb = skb;
  1586. msg.portid = NETLINK_CB(cb->skb).portid;
  1587. msg.seq = cb->nlh->nlmsg_seq;
  1588. rcu_read_lock();
  1589. if (prev_node) {
  1590. node = tipc_node_find(net, prev_node);
  1591. if (!node) {
  1592. /* We never set seq or call nl_dump_check_consistent()
  1593. * this means that setting prev_seq here will cause the
  1594. * consistence check to fail in the netlink callback
  1595. * handler. Resulting in the last NLMSG_DONE message
  1596. * having the NLM_F_DUMP_INTR flag set.
  1597. */
  1598. cb->prev_seq = 1;
  1599. goto out;
  1600. }
  1601. tipc_node_put(node);
  1602. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1603. list) {
  1604. tipc_node_lock(node);
  1605. err = __tipc_nl_add_node_links(net, &msg, node,
  1606. &prev_link);
  1607. tipc_node_unlock(node);
  1608. if (err)
  1609. goto out;
  1610. prev_node = node->addr;
  1611. }
  1612. } else {
  1613. err = tipc_nl_add_bc_link(net, &msg);
  1614. if (err)
  1615. goto out;
  1616. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1617. tipc_node_lock(node);
  1618. err = __tipc_nl_add_node_links(net, &msg, node,
  1619. &prev_link);
  1620. tipc_node_unlock(node);
  1621. if (err)
  1622. goto out;
  1623. prev_node = node->addr;
  1624. }
  1625. }
  1626. done = 1;
  1627. out:
  1628. rcu_read_unlock();
  1629. cb->args[0] = prev_node;
  1630. cb->args[1] = prev_link;
  1631. cb->args[2] = done;
  1632. return skb->len;
  1633. }
  1634. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  1635. {
  1636. struct net *net = genl_info_net(info);
  1637. struct tipc_nl_msg msg;
  1638. char *name;
  1639. int err;
  1640. msg.portid = info->snd_portid;
  1641. msg.seq = info->snd_seq;
  1642. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1643. return -EINVAL;
  1644. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1645. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1646. if (!msg.skb)
  1647. return -ENOMEM;
  1648. if (strcmp(name, tipc_bclink_name) == 0) {
  1649. err = tipc_nl_add_bc_link(net, &msg);
  1650. if (err) {
  1651. nlmsg_free(msg.skb);
  1652. return err;
  1653. }
  1654. } else {
  1655. int bearer_id;
  1656. struct tipc_node *node;
  1657. struct tipc_link *link;
  1658. node = tipc_link_find_owner(net, name, &bearer_id);
  1659. if (!node)
  1660. return -EINVAL;
  1661. tipc_node_lock(node);
  1662. link = node->links[bearer_id].link;
  1663. if (!link) {
  1664. tipc_node_unlock(node);
  1665. nlmsg_free(msg.skb);
  1666. return -EINVAL;
  1667. }
  1668. err = __tipc_nl_add_link(net, &msg, link, 0);
  1669. tipc_node_unlock(node);
  1670. if (err) {
  1671. nlmsg_free(msg.skb);
  1672. return err;
  1673. }
  1674. }
  1675. return genlmsg_reply(msg.skb, info);
  1676. }
  1677. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  1678. {
  1679. int err;
  1680. char *link_name;
  1681. unsigned int bearer_id;
  1682. struct tipc_link *link;
  1683. struct tipc_node *node;
  1684. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1685. struct net *net = sock_net(skb->sk);
  1686. if (!info->attrs[TIPC_NLA_LINK])
  1687. return -EINVAL;
  1688. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1689. info->attrs[TIPC_NLA_LINK],
  1690. tipc_nl_link_policy);
  1691. if (err)
  1692. return err;
  1693. if (!attrs[TIPC_NLA_LINK_NAME])
  1694. return -EINVAL;
  1695. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1696. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1697. err = tipc_bclink_reset_stats(net);
  1698. if (err)
  1699. return err;
  1700. return 0;
  1701. }
  1702. node = tipc_link_find_owner(net, link_name, &bearer_id);
  1703. if (!node)
  1704. return -EINVAL;
  1705. tipc_node_lock(node);
  1706. link = node->links[bearer_id].link;
  1707. if (!link) {
  1708. tipc_node_unlock(node);
  1709. return -EINVAL;
  1710. }
  1711. link_reset_statistics(link);
  1712. tipc_node_unlock(node);
  1713. return 0;
  1714. }