link.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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. struct sk_buff_head tmpq;
  906. int usr = msg_user(hdr);
  907. int rc = 0;
  908. int pos = 0;
  909. int ipos = 0;
  910. if (unlikely(usr == TUNNEL_PROTOCOL)) {
  911. if (msg_type(hdr) == SYNCH_MSG) {
  912. __skb_queue_purge(&l->deferdq);
  913. goto drop;
  914. }
  915. if (!tipc_msg_extract(skb, &iskb, &ipos))
  916. return rc;
  917. kfree_skb(skb);
  918. skb = iskb;
  919. hdr = buf_msg(skb);
  920. if (less(msg_seqno(hdr), l->drop_point))
  921. goto drop;
  922. if (tipc_data_input(l, skb, inputq))
  923. return rc;
  924. usr = msg_user(hdr);
  925. reasm_skb = &l->failover_reasm_skb;
  926. }
  927. if (usr == MSG_BUNDLER) {
  928. skb_queue_head_init(&tmpq);
  929. l->stats.recv_bundles++;
  930. l->stats.recv_bundled += msg_msgcnt(hdr);
  931. while (tipc_msg_extract(skb, &iskb, &pos))
  932. tipc_data_input(l, iskb, &tmpq);
  933. tipc_skb_queue_splice_tail(&tmpq, inputq);
  934. return 0;
  935. } else if (usr == MSG_FRAGMENTER) {
  936. l->stats.recv_fragments++;
  937. if (tipc_buf_append(reasm_skb, &skb)) {
  938. l->stats.recv_fragmented++;
  939. tipc_data_input(l, skb, inputq);
  940. } else if (!*reasm_skb) {
  941. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  942. }
  943. return 0;
  944. } else if (usr == BCAST_PROTOCOL) {
  945. tipc_link_sync_rcv(node, skb);
  946. return 0;
  947. }
  948. drop:
  949. kfree_skb(skb);
  950. return 0;
  951. }
  952. static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
  953. {
  954. bool released = false;
  955. struct sk_buff *skb, *tmp;
  956. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  957. if (more(buf_seqno(skb), acked))
  958. break;
  959. __skb_unlink(skb, &l->transmq);
  960. kfree_skb(skb);
  961. released = true;
  962. }
  963. return released;
  964. }
  965. /* tipc_link_build_ack_msg: prepare link acknowledge message for transmission
  966. */
  967. void tipc_link_build_ack_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  968. {
  969. l->rcv_unacked = 0;
  970. l->stats.sent_acks++;
  971. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
  972. }
  973. /* tipc_link_build_nack_msg: prepare link nack message for transmission
  974. */
  975. static void tipc_link_build_nack_msg(struct tipc_link *l,
  976. struct sk_buff_head *xmitq)
  977. {
  978. u32 def_cnt = ++l->stats.deferred_recv;
  979. if ((skb_queue_len(&l->deferdq) == 1) || !(def_cnt % TIPC_NACK_INTV))
  980. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
  981. }
  982. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  983. * @l: the link that should handle the message
  984. * @skb: TIPC packet
  985. * @xmitq: queue to place packets to be sent after this call
  986. */
  987. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  988. struct sk_buff_head *xmitq)
  989. {
  990. struct sk_buff_head *defq = &l->deferdq;
  991. struct tipc_msg *hdr;
  992. u16 seqno, rcv_nxt, win_lim;
  993. int rc = 0;
  994. do {
  995. hdr = buf_msg(skb);
  996. seqno = msg_seqno(hdr);
  997. rcv_nxt = l->rcv_nxt;
  998. win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
  999. /* Verify and update link state */
  1000. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  1001. return tipc_link_proto_rcv(l, skb, xmitq);
  1002. if (unlikely(!link_is_up(l))) {
  1003. rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1004. if (!link_is_up(l))
  1005. goto drop;
  1006. }
  1007. /* Don't send probe at next timeout expiration */
  1008. l->silent_intv_cnt = 0;
  1009. /* Drop if outside receive window */
  1010. if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
  1011. l->stats.duplicates++;
  1012. goto drop;
  1013. }
  1014. /* Forward queues and wake up waiting users */
  1015. if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
  1016. tipc_link_advance_backlog(l, xmitq);
  1017. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1018. link_prepare_wakeup(l);
  1019. }
  1020. /* Defer delivery if sequence gap */
  1021. if (unlikely(seqno != rcv_nxt)) {
  1022. __tipc_skb_queue_sorted(defq, seqno, skb);
  1023. tipc_link_build_nack_msg(l, xmitq);
  1024. break;
  1025. }
  1026. /* Deliver packet */
  1027. l->rcv_nxt++;
  1028. l->stats.recv_info++;
  1029. if (!tipc_data_input(l, skb, l->inputq))
  1030. rc = tipc_link_input(l, skb, l->inputq);
  1031. if (unlikely(rc))
  1032. break;
  1033. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
  1034. tipc_link_build_ack_msg(l, xmitq);
  1035. } while ((skb = __skb_dequeue(defq)));
  1036. return rc;
  1037. drop:
  1038. kfree_skb(skb);
  1039. return rc;
  1040. }
  1041. /**
  1042. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1043. *
  1044. * Returns increase in queue length (i.e. 0 or 1)
  1045. */
  1046. u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
  1047. {
  1048. struct sk_buff *skb1;
  1049. u16 seq_no = buf_seqno(skb);
  1050. /* Empty queue ? */
  1051. if (skb_queue_empty(list)) {
  1052. __skb_queue_tail(list, skb);
  1053. return 1;
  1054. }
  1055. /* Last ? */
  1056. if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
  1057. __skb_queue_tail(list, skb);
  1058. return 1;
  1059. }
  1060. /* Locate insertion point in queue, then insert; discard if duplicate */
  1061. skb_queue_walk(list, skb1) {
  1062. u16 curr_seqno = buf_seqno(skb1);
  1063. if (seq_no == curr_seqno) {
  1064. kfree_skb(skb);
  1065. return 0;
  1066. }
  1067. if (less(seq_no, curr_seqno))
  1068. break;
  1069. }
  1070. __skb_queue_before(list, skb1, skb);
  1071. return 1;
  1072. }
  1073. /*
  1074. * Send protocol message to the other endpoint.
  1075. */
  1076. void tipc_link_proto_xmit(struct tipc_link *l, u32 msg_typ, int probe_msg,
  1077. u32 gap, u32 tolerance, u32 priority)
  1078. {
  1079. struct sk_buff *skb = NULL;
  1080. struct sk_buff_head xmitq;
  1081. __skb_queue_head_init(&xmitq);
  1082. tipc_link_build_proto_msg(l, msg_typ, probe_msg, gap,
  1083. tolerance, priority, &xmitq);
  1084. skb = __skb_dequeue(&xmitq);
  1085. if (!skb)
  1086. return;
  1087. tipc_bearer_send(l->owner->net, l->bearer_id, skb, l->media_addr);
  1088. l->rcv_unacked = 0;
  1089. kfree_skb(skb);
  1090. }
  1091. /* tipc_link_build_proto_msg: prepare link protocol message for transmission
  1092. */
  1093. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1094. u16 rcvgap, int tolerance, int priority,
  1095. struct sk_buff_head *xmitq)
  1096. {
  1097. struct sk_buff *skb = NULL;
  1098. struct tipc_msg *hdr = l->pmsg;
  1099. u16 snd_nxt = l->snd_nxt;
  1100. u16 rcv_nxt = l->rcv_nxt;
  1101. u16 rcv_last = rcv_nxt - 1;
  1102. int node_up = l->owner->bclink.recv_permitted;
  1103. /* Don't send protocol message during reset or link failover */
  1104. if (tipc_link_is_blocked(l))
  1105. return;
  1106. msg_set_type(hdr, mtyp);
  1107. msg_set_net_plane(hdr, l->net_plane);
  1108. msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
  1109. msg_set_last_bcast(hdr, tipc_bclink_get_last_sent(l->owner->net));
  1110. msg_set_link_tolerance(hdr, tolerance);
  1111. msg_set_linkprio(hdr, priority);
  1112. msg_set_redundant_link(hdr, node_up);
  1113. msg_set_seq_gap(hdr, 0);
  1114. /* Compatibility: created msg must not be in sequence with pkt flow */
  1115. msg_set_seqno(hdr, snd_nxt + U16_MAX / 2);
  1116. if (mtyp == STATE_MSG) {
  1117. if (!tipc_link_is_up(l))
  1118. return;
  1119. msg_set_next_sent(hdr, snd_nxt);
  1120. /* Override rcvgap if there are packets in deferred queue */
  1121. if (!skb_queue_empty(&l->deferdq))
  1122. rcvgap = buf_seqno(skb_peek(&l->deferdq)) - rcv_nxt;
  1123. if (rcvgap) {
  1124. msg_set_seq_gap(hdr, rcvgap);
  1125. l->stats.sent_nacks++;
  1126. }
  1127. msg_set_ack(hdr, rcv_last);
  1128. msg_set_probe(hdr, probe);
  1129. if (probe)
  1130. l->stats.sent_probes++;
  1131. l->stats.sent_states++;
  1132. } else {
  1133. /* RESET_MSG or ACTIVATE_MSG */
  1134. msg_set_max_pkt(hdr, l->advertised_mtu);
  1135. msg_set_ack(hdr, l->rcv_nxt - 1);
  1136. msg_set_next_sent(hdr, 1);
  1137. }
  1138. skb = tipc_buf_acquire(msg_size(hdr));
  1139. if (!skb)
  1140. return;
  1141. skb_copy_to_linear_data(skb, hdr, msg_size(hdr));
  1142. skb->priority = TC_PRIO_CONTROL;
  1143. __skb_queue_tail(xmitq, skb);
  1144. }
  1145. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1146. * with contents of the link's transmit and backlog queues.
  1147. */
  1148. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1149. int mtyp, struct sk_buff_head *xmitq)
  1150. {
  1151. struct sk_buff *skb, *tnlskb;
  1152. struct tipc_msg *hdr, tnlhdr;
  1153. struct sk_buff_head *queue = &l->transmq;
  1154. struct sk_buff_head tmpxq, tnlq;
  1155. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1156. if (!tnl)
  1157. return;
  1158. skb_queue_head_init(&tnlq);
  1159. skb_queue_head_init(&tmpxq);
  1160. /* At least one packet required for safe algorithm => add dummy */
  1161. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1162. BASIC_H_SIZE, 0, l->addr, link_own_addr(l),
  1163. 0, 0, TIPC_ERR_NO_PORT);
  1164. if (!skb) {
  1165. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1166. return;
  1167. }
  1168. skb_queue_tail(&tnlq, skb);
  1169. tipc_link_xmit(l, &tnlq, &tmpxq);
  1170. __skb_queue_purge(&tmpxq);
  1171. /* Initialize reusable tunnel packet header */
  1172. tipc_msg_init(link_own_addr(l), &tnlhdr, TUNNEL_PROTOCOL,
  1173. mtyp, INT_H_SIZE, l->addr);
  1174. pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
  1175. msg_set_msgcnt(&tnlhdr, pktcnt);
  1176. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1177. tnl:
  1178. /* Wrap each packet into a tunnel packet */
  1179. skb_queue_walk(queue, skb) {
  1180. hdr = buf_msg(skb);
  1181. if (queue == &l->backlogq)
  1182. msg_set_seqno(hdr, seqno++);
  1183. pktlen = msg_size(hdr);
  1184. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1185. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE);
  1186. if (!tnlskb) {
  1187. pr_warn("%sunable to send packet\n", link_co_err);
  1188. return;
  1189. }
  1190. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1191. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1192. __skb_queue_tail(&tnlq, tnlskb);
  1193. }
  1194. if (queue != &l->backlogq) {
  1195. queue = &l->backlogq;
  1196. goto tnl;
  1197. }
  1198. tipc_link_xmit(tnl, &tnlq, xmitq);
  1199. if (mtyp == FAILOVER_MSG) {
  1200. tnl->drop_point = l->rcv_nxt;
  1201. tnl->failover_reasm_skb = l->reasm_buf;
  1202. l->reasm_buf = NULL;
  1203. }
  1204. }
  1205. /* tipc_link_proto_rcv(): receive link level protocol message :
  1206. * Note that network plane id propagates through the network, and may
  1207. * change at any time. The node with lowest numerical id determines
  1208. * network plane
  1209. */
  1210. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1211. struct sk_buff_head *xmitq)
  1212. {
  1213. struct tipc_msg *hdr = buf_msg(skb);
  1214. u16 rcvgap = 0;
  1215. u16 nacked_gap = msg_seq_gap(hdr);
  1216. u16 peers_snd_nxt = msg_next_sent(hdr);
  1217. u16 peers_tol = msg_link_tolerance(hdr);
  1218. u16 peers_prio = msg_linkprio(hdr);
  1219. u16 rcv_nxt = l->rcv_nxt;
  1220. char *if_name;
  1221. int rc = 0;
  1222. if (tipc_link_is_blocked(l))
  1223. goto exit;
  1224. if (link_own_addr(l) > msg_prevnode(hdr))
  1225. l->net_plane = msg_net_plane(hdr);
  1226. switch (msg_type(hdr)) {
  1227. case RESET_MSG:
  1228. /* Ignore duplicate RESET with old session number */
  1229. if ((less_eq(msg_session(hdr), l->peer_session)) &&
  1230. (l->peer_session != WILDCARD_SESSION))
  1231. break;
  1232. /* fall thru' */
  1233. case ACTIVATE_MSG:
  1234. /* Complete own link name with peer's interface name */
  1235. if_name = strrchr(l->name, ':') + 1;
  1236. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  1237. break;
  1238. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  1239. break;
  1240. strncpy(if_name, msg_data(hdr), TIPC_MAX_IF_NAME);
  1241. /* Update own tolerance if peer indicates a non-zero value */
  1242. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1243. l->tolerance = peers_tol;
  1244. /* Update own priority if peer's priority is higher */
  1245. if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  1246. l->priority = peers_prio;
  1247. if (msg_type(hdr) == RESET_MSG) {
  1248. rc |= tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1249. } else if (!link_is_up(l)) {
  1250. tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1251. rc |= tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1252. }
  1253. l->peer_session = msg_session(hdr);
  1254. l->peer_bearer_id = msg_bearer_id(hdr);
  1255. if (l->mtu > msg_max_pkt(hdr))
  1256. l->mtu = msg_max_pkt(hdr);
  1257. break;
  1258. case STATE_MSG:
  1259. /* Update own tolerance if peer indicates a non-zero value */
  1260. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
  1261. l->tolerance = peers_tol;
  1262. l->silent_intv_cnt = 0;
  1263. l->stats.recv_states++;
  1264. if (msg_probe(hdr))
  1265. l->stats.recv_probes++;
  1266. rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1267. if (!link_is_up(l))
  1268. break;
  1269. /* Send NACK if peer has sent pkts we haven't received yet */
  1270. if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
  1271. rcvgap = peers_snd_nxt - l->rcv_nxt;
  1272. if (rcvgap || (msg_probe(hdr)))
  1273. tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
  1274. 0, 0, xmitq);
  1275. tipc_link_release_pkts(l, msg_ack(hdr));
  1276. /* If NACK, retransmit will now start at right position */
  1277. if (nacked_gap) {
  1278. rc = tipc_link_retransm(l, nacked_gap, xmitq);
  1279. l->stats.recv_nacks++;
  1280. }
  1281. tipc_link_advance_backlog(l, xmitq);
  1282. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1283. link_prepare_wakeup(l);
  1284. }
  1285. exit:
  1286. kfree_skb(skb);
  1287. return rc;
  1288. }
  1289. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1290. {
  1291. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
  1292. l->window = win;
  1293. l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
  1294. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
  1295. l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
  1296. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
  1297. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1298. }
  1299. /* tipc_link_find_owner - locate owner node of link by link's name
  1300. * @net: the applicable net namespace
  1301. * @name: pointer to link name string
  1302. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1303. *
  1304. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1305. */
  1306. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1307. const char *link_name,
  1308. unsigned int *bearer_id)
  1309. {
  1310. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1311. struct tipc_link *l_ptr;
  1312. struct tipc_node *n_ptr;
  1313. struct tipc_node *found_node = NULL;
  1314. int i;
  1315. *bearer_id = 0;
  1316. rcu_read_lock();
  1317. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1318. tipc_node_lock(n_ptr);
  1319. for (i = 0; i < MAX_BEARERS; i++) {
  1320. l_ptr = n_ptr->links[i].link;
  1321. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1322. *bearer_id = i;
  1323. found_node = n_ptr;
  1324. break;
  1325. }
  1326. }
  1327. tipc_node_unlock(n_ptr);
  1328. if (found_node)
  1329. break;
  1330. }
  1331. rcu_read_unlock();
  1332. return found_node;
  1333. }
  1334. /**
  1335. * link_reset_statistics - reset link statistics
  1336. * @l_ptr: pointer to link
  1337. */
  1338. static void link_reset_statistics(struct tipc_link *l_ptr)
  1339. {
  1340. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1341. l_ptr->stats.sent_info = l_ptr->snd_nxt;
  1342. l_ptr->stats.recv_info = l_ptr->rcv_nxt;
  1343. }
  1344. static void link_print(struct tipc_link *l, const char *str)
  1345. {
  1346. struct sk_buff *hskb = skb_peek(&l->transmq);
  1347. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt;
  1348. u16 tail = l->snd_nxt - 1;
  1349. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  1350. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  1351. skb_queue_len(&l->transmq), head, tail,
  1352. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  1353. }
  1354. /* Parse and validate nested (link) properties valid for media, bearer and link
  1355. */
  1356. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1357. {
  1358. int err;
  1359. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1360. tipc_nl_prop_policy);
  1361. if (err)
  1362. return err;
  1363. if (props[TIPC_NLA_PROP_PRIO]) {
  1364. u32 prio;
  1365. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1366. if (prio > TIPC_MAX_LINK_PRI)
  1367. return -EINVAL;
  1368. }
  1369. if (props[TIPC_NLA_PROP_TOL]) {
  1370. u32 tol;
  1371. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1372. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1373. return -EINVAL;
  1374. }
  1375. if (props[TIPC_NLA_PROP_WIN]) {
  1376. u32 win;
  1377. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1378. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1379. return -EINVAL;
  1380. }
  1381. return 0;
  1382. }
  1383. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  1384. {
  1385. int err;
  1386. int res = 0;
  1387. int bearer_id;
  1388. char *name;
  1389. struct tipc_link *link;
  1390. struct tipc_node *node;
  1391. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1392. struct net *net = sock_net(skb->sk);
  1393. if (!info->attrs[TIPC_NLA_LINK])
  1394. return -EINVAL;
  1395. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1396. info->attrs[TIPC_NLA_LINK],
  1397. tipc_nl_link_policy);
  1398. if (err)
  1399. return err;
  1400. if (!attrs[TIPC_NLA_LINK_NAME])
  1401. return -EINVAL;
  1402. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1403. if (strcmp(name, tipc_bclink_name) == 0)
  1404. return tipc_nl_bc_link_set(net, attrs);
  1405. node = tipc_link_find_owner(net, name, &bearer_id);
  1406. if (!node)
  1407. return -EINVAL;
  1408. tipc_node_lock(node);
  1409. link = node->links[bearer_id].link;
  1410. if (!link) {
  1411. res = -EINVAL;
  1412. goto out;
  1413. }
  1414. if (attrs[TIPC_NLA_LINK_PROP]) {
  1415. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1416. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1417. props);
  1418. if (err) {
  1419. res = err;
  1420. goto out;
  1421. }
  1422. if (props[TIPC_NLA_PROP_TOL]) {
  1423. u32 tol;
  1424. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1425. link->tolerance = tol;
  1426. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
  1427. }
  1428. if (props[TIPC_NLA_PROP_PRIO]) {
  1429. u32 prio;
  1430. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1431. link->priority = prio;
  1432. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
  1433. }
  1434. if (props[TIPC_NLA_PROP_WIN]) {
  1435. u32 win;
  1436. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1437. tipc_link_set_queue_limits(link, win);
  1438. }
  1439. }
  1440. out:
  1441. tipc_node_unlock(node);
  1442. return res;
  1443. }
  1444. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1445. {
  1446. int i;
  1447. struct nlattr *stats;
  1448. struct nla_map {
  1449. u32 key;
  1450. u32 val;
  1451. };
  1452. struct nla_map map[] = {
  1453. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  1454. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1455. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1456. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1457. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1458. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  1459. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1460. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1461. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1462. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1463. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1464. s->msg_length_counts : 1},
  1465. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1466. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1467. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1468. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1469. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1470. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1471. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1472. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1473. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1474. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1475. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1476. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1477. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1478. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1479. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1480. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1481. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1482. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1483. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1484. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1485. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1486. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1487. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1488. };
  1489. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1490. if (!stats)
  1491. return -EMSGSIZE;
  1492. for (i = 0; i < ARRAY_SIZE(map); i++)
  1493. if (nla_put_u32(skb, map[i].key, map[i].val))
  1494. goto msg_full;
  1495. nla_nest_end(skb, stats);
  1496. return 0;
  1497. msg_full:
  1498. nla_nest_cancel(skb, stats);
  1499. return -EMSGSIZE;
  1500. }
  1501. /* Caller should hold appropriate locks to protect the link */
  1502. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1503. struct tipc_link *link, int nlflags)
  1504. {
  1505. int err;
  1506. void *hdr;
  1507. struct nlattr *attrs;
  1508. struct nlattr *prop;
  1509. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1510. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1511. nlflags, TIPC_NL_LINK_GET);
  1512. if (!hdr)
  1513. return -EMSGSIZE;
  1514. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1515. if (!attrs)
  1516. goto msg_full;
  1517. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1518. goto attr_msg_full;
  1519. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1520. tipc_cluster_mask(tn->own_addr)))
  1521. goto attr_msg_full;
  1522. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1523. goto attr_msg_full;
  1524. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->rcv_nxt))
  1525. goto attr_msg_full;
  1526. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->snd_nxt))
  1527. goto attr_msg_full;
  1528. if (tipc_link_is_up(link))
  1529. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1530. goto attr_msg_full;
  1531. if (tipc_link_is_active(link))
  1532. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1533. goto attr_msg_full;
  1534. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1535. if (!prop)
  1536. goto attr_msg_full;
  1537. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1538. goto prop_msg_full;
  1539. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1540. goto prop_msg_full;
  1541. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1542. link->window))
  1543. goto prop_msg_full;
  1544. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1545. goto prop_msg_full;
  1546. nla_nest_end(msg->skb, prop);
  1547. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1548. if (err)
  1549. goto attr_msg_full;
  1550. nla_nest_end(msg->skb, attrs);
  1551. genlmsg_end(msg->skb, hdr);
  1552. return 0;
  1553. prop_msg_full:
  1554. nla_nest_cancel(msg->skb, prop);
  1555. attr_msg_full:
  1556. nla_nest_cancel(msg->skb, attrs);
  1557. msg_full:
  1558. genlmsg_cancel(msg->skb, hdr);
  1559. return -EMSGSIZE;
  1560. }
  1561. /* Caller should hold node lock */
  1562. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1563. struct tipc_node *node, u32 *prev_link)
  1564. {
  1565. u32 i;
  1566. int err;
  1567. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1568. *prev_link = i;
  1569. if (!node->links[i].link)
  1570. continue;
  1571. err = __tipc_nl_add_link(net, msg,
  1572. node->links[i].link, NLM_F_MULTI);
  1573. if (err)
  1574. return err;
  1575. }
  1576. *prev_link = 0;
  1577. return 0;
  1578. }
  1579. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1580. {
  1581. struct net *net = sock_net(skb->sk);
  1582. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1583. struct tipc_node *node;
  1584. struct tipc_nl_msg msg;
  1585. u32 prev_node = cb->args[0];
  1586. u32 prev_link = cb->args[1];
  1587. int done = cb->args[2];
  1588. int err;
  1589. if (done)
  1590. return 0;
  1591. msg.skb = skb;
  1592. msg.portid = NETLINK_CB(cb->skb).portid;
  1593. msg.seq = cb->nlh->nlmsg_seq;
  1594. rcu_read_lock();
  1595. if (prev_node) {
  1596. node = tipc_node_find(net, prev_node);
  1597. if (!node) {
  1598. /* We never set seq or call nl_dump_check_consistent()
  1599. * this means that setting prev_seq here will cause the
  1600. * consistence check to fail in the netlink callback
  1601. * handler. Resulting in the last NLMSG_DONE message
  1602. * having the NLM_F_DUMP_INTR flag set.
  1603. */
  1604. cb->prev_seq = 1;
  1605. goto out;
  1606. }
  1607. tipc_node_put(node);
  1608. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1609. list) {
  1610. tipc_node_lock(node);
  1611. err = __tipc_nl_add_node_links(net, &msg, node,
  1612. &prev_link);
  1613. tipc_node_unlock(node);
  1614. if (err)
  1615. goto out;
  1616. prev_node = node->addr;
  1617. }
  1618. } else {
  1619. err = tipc_nl_add_bc_link(net, &msg);
  1620. if (err)
  1621. goto out;
  1622. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1623. tipc_node_lock(node);
  1624. err = __tipc_nl_add_node_links(net, &msg, node,
  1625. &prev_link);
  1626. tipc_node_unlock(node);
  1627. if (err)
  1628. goto out;
  1629. prev_node = node->addr;
  1630. }
  1631. }
  1632. done = 1;
  1633. out:
  1634. rcu_read_unlock();
  1635. cb->args[0] = prev_node;
  1636. cb->args[1] = prev_link;
  1637. cb->args[2] = done;
  1638. return skb->len;
  1639. }
  1640. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  1641. {
  1642. struct net *net = genl_info_net(info);
  1643. struct tipc_nl_msg msg;
  1644. char *name;
  1645. int err;
  1646. msg.portid = info->snd_portid;
  1647. msg.seq = info->snd_seq;
  1648. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1649. return -EINVAL;
  1650. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1651. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1652. if (!msg.skb)
  1653. return -ENOMEM;
  1654. if (strcmp(name, tipc_bclink_name) == 0) {
  1655. err = tipc_nl_add_bc_link(net, &msg);
  1656. if (err) {
  1657. nlmsg_free(msg.skb);
  1658. return err;
  1659. }
  1660. } else {
  1661. int bearer_id;
  1662. struct tipc_node *node;
  1663. struct tipc_link *link;
  1664. node = tipc_link_find_owner(net, name, &bearer_id);
  1665. if (!node)
  1666. return -EINVAL;
  1667. tipc_node_lock(node);
  1668. link = node->links[bearer_id].link;
  1669. if (!link) {
  1670. tipc_node_unlock(node);
  1671. nlmsg_free(msg.skb);
  1672. return -EINVAL;
  1673. }
  1674. err = __tipc_nl_add_link(net, &msg, link, 0);
  1675. tipc_node_unlock(node);
  1676. if (err) {
  1677. nlmsg_free(msg.skb);
  1678. return err;
  1679. }
  1680. }
  1681. return genlmsg_reply(msg.skb, info);
  1682. }
  1683. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  1684. {
  1685. int err;
  1686. char *link_name;
  1687. unsigned int bearer_id;
  1688. struct tipc_link *link;
  1689. struct tipc_node *node;
  1690. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1691. struct net *net = sock_net(skb->sk);
  1692. if (!info->attrs[TIPC_NLA_LINK])
  1693. return -EINVAL;
  1694. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1695. info->attrs[TIPC_NLA_LINK],
  1696. tipc_nl_link_policy);
  1697. if (err)
  1698. return err;
  1699. if (!attrs[TIPC_NLA_LINK_NAME])
  1700. return -EINVAL;
  1701. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1702. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1703. err = tipc_bclink_reset_stats(net);
  1704. if (err)
  1705. return err;
  1706. return 0;
  1707. }
  1708. node = tipc_link_find_owner(net, link_name, &bearer_id);
  1709. if (!node)
  1710. return -EINVAL;
  1711. tipc_node_lock(node);
  1712. link = node->links[bearer_id].link;
  1713. if (!link) {
  1714. tipc_node_unlock(node);
  1715. return -EINVAL;
  1716. }
  1717. link_reset_statistics(link);
  1718. tipc_node_unlock(node);
  1719. return 0;
  1720. }