link.c 52 KB

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