link.c 53 KB

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