link.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2016, 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 "monitor.h"
  45. #include <linux/pkt_sched.h>
  46. struct tipc_stats {
  47. u32 sent_pkts;
  48. u32 recv_pkts;
  49. u32 sent_states;
  50. u32 recv_states;
  51. u32 sent_probes;
  52. u32 recv_probes;
  53. u32 sent_nacks;
  54. u32 recv_nacks;
  55. u32 sent_acks;
  56. u32 sent_bundled;
  57. u32 sent_bundles;
  58. u32 recv_bundled;
  59. u32 recv_bundles;
  60. u32 retransmitted;
  61. u32 sent_fragmented;
  62. u32 sent_fragments;
  63. u32 recv_fragmented;
  64. u32 recv_fragments;
  65. u32 link_congs; /* # port sends blocked by congestion */
  66. u32 deferred_recv;
  67. u32 duplicates;
  68. u32 max_queue_sz; /* send queue size high water mark */
  69. u32 accu_queue_sz; /* used for send queue size profiling */
  70. u32 queue_sz_counts; /* used for send queue size profiling */
  71. u32 msg_length_counts; /* used for message length profiling */
  72. u32 msg_lengths_total; /* used for message length profiling */
  73. u32 msg_length_profile[7]; /* used for msg. length profiling */
  74. };
  75. /**
  76. * struct tipc_link - TIPC link data structure
  77. * @addr: network address of link's peer node
  78. * @name: link name character string
  79. * @media_addr: media address to use when sending messages over link
  80. * @timer: link timer
  81. * @net: pointer to namespace struct
  82. * @refcnt: reference counter for permanent references (owner node & timer)
  83. * @peer_session: link session # being used by peer end of link
  84. * @peer_bearer_id: bearer id used by link's peer endpoint
  85. * @bearer_id: local bearer id used by link
  86. * @tolerance: minimum link continuity loss needed to reset link [in ms]
  87. * @abort_limit: # of unacknowledged continuity probes needed to reset link
  88. * @state: current state of link FSM
  89. * @peer_caps: bitmap describing capabilities of peer node
  90. * @silent_intv_cnt: # of timer intervals without any reception from peer
  91. * @proto_msg: template for control messages generated by link
  92. * @pmsg: convenience pointer to "proto_msg" field
  93. * @priority: current link priority
  94. * @net_plane: current link network plane ('A' through 'H')
  95. * @mon_state: cookie with information needed by link monitor
  96. * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
  97. * @exp_msg_count: # of tunnelled messages expected during link changeover
  98. * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
  99. * @mtu: current maximum packet size for this link
  100. * @advertised_mtu: advertised own mtu when link is being established
  101. * @transmitq: queue for sent, non-acked messages
  102. * @backlogq: queue for messages waiting to be sent
  103. * @snt_nxt: next sequence number to use for outbound messages
  104. * @last_retransmitted: sequence number of most recently retransmitted message
  105. * @stale_cnt: counter for number of identical retransmit attempts
  106. * @stale_limit: time when repeated identical retransmits must force link reset
  107. * @ackers: # of peers that needs to ack each packet before it can be released
  108. * @acked: # last packet acked by a certain peer. Used for broadcast.
  109. * @rcv_nxt: next sequence number to expect for inbound messages
  110. * @deferred_queue: deferred queue saved OOS b'cast message received from node
  111. * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
  112. * @inputq: buffer queue for messages to be delivered upwards
  113. * @namedq: buffer queue for name table messages to be delivered upwards
  114. * @next_out: ptr to first unsent outbound message in queue
  115. * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
  116. * @long_msg_seq_no: next identifier to use for outbound fragmented messages
  117. * @reasm_buf: head of partially reassembled inbound message fragments
  118. * @bc_rcvr: marks that this is a broadcast receiver link
  119. * @stats: collects statistics regarding link activity
  120. */
  121. struct tipc_link {
  122. u32 addr;
  123. char name[TIPC_MAX_LINK_NAME];
  124. struct net *net;
  125. /* Management and link supervision data */
  126. u16 peer_session;
  127. u16 session;
  128. u16 snd_nxt_state;
  129. u16 rcv_nxt_state;
  130. u32 peer_bearer_id;
  131. u32 bearer_id;
  132. u32 tolerance;
  133. u32 abort_limit;
  134. u32 state;
  135. u16 peer_caps;
  136. bool in_session;
  137. bool active;
  138. u32 silent_intv_cnt;
  139. char if_name[TIPC_MAX_IF_NAME];
  140. u32 priority;
  141. char net_plane;
  142. struct tipc_mon_state mon_state;
  143. u16 rst_cnt;
  144. /* Failover/synch */
  145. u16 drop_point;
  146. struct sk_buff *failover_reasm_skb;
  147. /* Max packet negotiation */
  148. u16 mtu;
  149. u16 advertised_mtu;
  150. /* Sending */
  151. struct sk_buff_head transmq;
  152. struct sk_buff_head backlogq;
  153. struct {
  154. u16 len;
  155. u16 limit;
  156. struct sk_buff *target_bskb;
  157. } backlog[5];
  158. u16 snd_nxt;
  159. u16 last_retransm;
  160. u16 window;
  161. u16 stale_cnt;
  162. unsigned long stale_limit;
  163. /* Reception */
  164. u16 rcv_nxt;
  165. u32 rcv_unacked;
  166. struct sk_buff_head deferdq;
  167. struct sk_buff_head *inputq;
  168. struct sk_buff_head *namedq;
  169. /* Congestion handling */
  170. struct sk_buff_head wakeupq;
  171. /* Fragmentation/reassembly */
  172. struct sk_buff *reasm_buf;
  173. /* Broadcast */
  174. u16 ackers;
  175. u16 acked;
  176. struct tipc_link *bc_rcvlink;
  177. struct tipc_link *bc_sndlink;
  178. unsigned long prev_retr;
  179. u16 prev_from;
  180. u16 prev_to;
  181. u8 nack_state;
  182. bool bc_peer_is_up;
  183. /* Statistics */
  184. struct tipc_stats stats;
  185. };
  186. /*
  187. * Error message prefixes
  188. */
  189. static const char *link_co_err = "Link tunneling error, ";
  190. static const char *link_rst_msg = "Resetting link ";
  191. /* Send states for broadcast NACKs
  192. */
  193. enum {
  194. BC_NACK_SND_CONDITIONAL,
  195. BC_NACK_SND_UNCONDITIONAL,
  196. BC_NACK_SND_SUPPRESS,
  197. };
  198. #define TIPC_BC_RETR_LIMIT 10 /* [ms] */
  199. /*
  200. * Interval between NACKs when packets arrive out of order
  201. */
  202. #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
  203. /* Link FSM states:
  204. */
  205. enum {
  206. LINK_ESTABLISHED = 0xe,
  207. LINK_ESTABLISHING = 0xe << 4,
  208. LINK_RESET = 0x1 << 8,
  209. LINK_RESETTING = 0x2 << 12,
  210. LINK_PEER_RESET = 0xd << 16,
  211. LINK_FAILINGOVER = 0xf << 20,
  212. LINK_SYNCHING = 0xc << 24
  213. };
  214. /* Link FSM state checking routines
  215. */
  216. static int link_is_up(struct tipc_link *l)
  217. {
  218. return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
  219. }
  220. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  221. struct sk_buff_head *xmitq);
  222. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  223. bool probe_reply, u16 rcvgap,
  224. int tolerance, int priority,
  225. struct sk_buff_head *xmitq);
  226. static void link_print(struct tipc_link *l, const char *str);
  227. static int tipc_link_build_nack_msg(struct tipc_link *l,
  228. struct sk_buff_head *xmitq);
  229. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  230. struct sk_buff_head *xmitq);
  231. static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
  232. /*
  233. * Simple non-static link routines (i.e. referenced outside this file)
  234. */
  235. bool tipc_link_is_up(struct tipc_link *l)
  236. {
  237. return link_is_up(l);
  238. }
  239. bool tipc_link_peer_is_down(struct tipc_link *l)
  240. {
  241. return l->state == LINK_PEER_RESET;
  242. }
  243. bool tipc_link_is_reset(struct tipc_link *l)
  244. {
  245. return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
  246. }
  247. bool tipc_link_is_establishing(struct tipc_link *l)
  248. {
  249. return l->state == LINK_ESTABLISHING;
  250. }
  251. bool tipc_link_is_synching(struct tipc_link *l)
  252. {
  253. return l->state == LINK_SYNCHING;
  254. }
  255. bool tipc_link_is_failingover(struct tipc_link *l)
  256. {
  257. return l->state == LINK_FAILINGOVER;
  258. }
  259. bool tipc_link_is_blocked(struct tipc_link *l)
  260. {
  261. return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
  262. }
  263. static bool link_is_bc_sndlink(struct tipc_link *l)
  264. {
  265. return !l->bc_sndlink;
  266. }
  267. static bool link_is_bc_rcvlink(struct tipc_link *l)
  268. {
  269. return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
  270. }
  271. void tipc_link_set_active(struct tipc_link *l, bool active)
  272. {
  273. l->active = active;
  274. }
  275. u32 tipc_link_id(struct tipc_link *l)
  276. {
  277. return l->peer_bearer_id << 16 | l->bearer_id;
  278. }
  279. int tipc_link_window(struct tipc_link *l)
  280. {
  281. return l->window;
  282. }
  283. int tipc_link_prio(struct tipc_link *l)
  284. {
  285. return l->priority;
  286. }
  287. unsigned long tipc_link_tolerance(struct tipc_link *l)
  288. {
  289. return l->tolerance;
  290. }
  291. struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
  292. {
  293. return l->inputq;
  294. }
  295. char tipc_link_plane(struct tipc_link *l)
  296. {
  297. return l->net_plane;
  298. }
  299. void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
  300. {
  301. l->peer_caps = capabilities;
  302. }
  303. void tipc_link_add_bc_peer(struct tipc_link *snd_l,
  304. struct tipc_link *uc_l,
  305. struct sk_buff_head *xmitq)
  306. {
  307. struct tipc_link *rcv_l = uc_l->bc_rcvlink;
  308. snd_l->ackers++;
  309. rcv_l->acked = snd_l->snd_nxt - 1;
  310. snd_l->state = LINK_ESTABLISHED;
  311. tipc_link_build_bc_init_msg(uc_l, xmitq);
  312. }
  313. void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
  314. struct tipc_link *rcv_l,
  315. struct sk_buff_head *xmitq)
  316. {
  317. u16 ack = snd_l->snd_nxt - 1;
  318. snd_l->ackers--;
  319. rcv_l->bc_peer_is_up = true;
  320. rcv_l->state = LINK_ESTABLISHED;
  321. tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
  322. tipc_link_reset(rcv_l);
  323. rcv_l->state = LINK_RESET;
  324. if (!snd_l->ackers) {
  325. tipc_link_reset(snd_l);
  326. snd_l->state = LINK_RESET;
  327. __skb_queue_purge(xmitq);
  328. }
  329. }
  330. int tipc_link_bc_peers(struct tipc_link *l)
  331. {
  332. return l->ackers;
  333. }
  334. static u16 link_bc_rcv_gap(struct tipc_link *l)
  335. {
  336. struct sk_buff *skb = skb_peek(&l->deferdq);
  337. u16 gap = 0;
  338. if (more(l->snd_nxt, l->rcv_nxt))
  339. gap = l->snd_nxt - l->rcv_nxt;
  340. if (skb)
  341. gap = buf_seqno(skb) - l->rcv_nxt;
  342. return gap;
  343. }
  344. void tipc_link_set_mtu(struct tipc_link *l, int mtu)
  345. {
  346. l->mtu = mtu;
  347. }
  348. int tipc_link_mtu(struct tipc_link *l)
  349. {
  350. return l->mtu;
  351. }
  352. u16 tipc_link_rcv_nxt(struct tipc_link *l)
  353. {
  354. return l->rcv_nxt;
  355. }
  356. u16 tipc_link_acked(struct tipc_link *l)
  357. {
  358. return l->acked;
  359. }
  360. char *tipc_link_name(struct tipc_link *l)
  361. {
  362. return l->name;
  363. }
  364. u32 tipc_link_state(struct tipc_link *l)
  365. {
  366. return l->state;
  367. }
  368. /**
  369. * tipc_link_create - create a new link
  370. * @n: pointer to associated node
  371. * @if_name: associated interface name
  372. * @bearer_id: id (index) of associated bearer
  373. * @tolerance: link tolerance to be used by link
  374. * @net_plane: network plane (A,B,c..) this link belongs to
  375. * @mtu: mtu to be advertised by link
  376. * @priority: priority to be used by link
  377. * @window: send window to be used by link
  378. * @session: session to be used by link
  379. * @ownnode: identity of own node
  380. * @peer: node id of peer node
  381. * @peer_caps: bitmap describing peer node capabilities
  382. * @bc_sndlink: the namespace global link used for broadcast sending
  383. * @bc_rcvlink: the peer specific link used for broadcast reception
  384. * @inputq: queue to put messages ready for delivery
  385. * @namedq: queue to put binding table update messages ready for delivery
  386. * @link: return value, pointer to put the created link
  387. *
  388. * Returns true if link was created, otherwise false
  389. */
  390. bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
  391. int tolerance, char net_plane, u32 mtu, int priority,
  392. int window, u32 session, u32 self,
  393. u32 peer, u8 *peer_id, u16 peer_caps,
  394. struct tipc_link *bc_sndlink,
  395. struct tipc_link *bc_rcvlink,
  396. struct sk_buff_head *inputq,
  397. struct sk_buff_head *namedq,
  398. struct tipc_link **link)
  399. {
  400. char peer_str[NODE_ID_STR_LEN] = {0,};
  401. char self_str[NODE_ID_STR_LEN] = {0,};
  402. struct tipc_link *l;
  403. l = kzalloc(sizeof(*l), GFP_ATOMIC);
  404. if (!l)
  405. return false;
  406. *link = l;
  407. l->session = session;
  408. /* Set link name for unicast links only */
  409. if (peer_id) {
  410. tipc_nodeid2string(self_str, tipc_own_id(net));
  411. if (strlen(self_str) > 16)
  412. sprintf(self_str, "%x", self);
  413. tipc_nodeid2string(peer_str, peer_id);
  414. if (strlen(peer_str) > 16)
  415. sprintf(peer_str, "%x", peer);
  416. }
  417. /* Peer i/f name will be completed by reset/activate message */
  418. snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
  419. self_str, if_name, peer_str);
  420. strcpy(l->if_name, if_name);
  421. l->addr = peer;
  422. l->peer_caps = peer_caps;
  423. l->net = net;
  424. l->in_session = false;
  425. l->bearer_id = bearer_id;
  426. l->tolerance = tolerance;
  427. if (bc_rcvlink)
  428. bc_rcvlink->tolerance = tolerance;
  429. l->net_plane = net_plane;
  430. l->advertised_mtu = mtu;
  431. l->mtu = mtu;
  432. l->priority = priority;
  433. tipc_link_set_queue_limits(l, window);
  434. l->ackers = 1;
  435. l->bc_sndlink = bc_sndlink;
  436. l->bc_rcvlink = bc_rcvlink;
  437. l->inputq = inputq;
  438. l->namedq = namedq;
  439. l->state = LINK_RESETTING;
  440. __skb_queue_head_init(&l->transmq);
  441. __skb_queue_head_init(&l->backlogq);
  442. __skb_queue_head_init(&l->deferdq);
  443. skb_queue_head_init(&l->wakeupq);
  444. skb_queue_head_init(l->inputq);
  445. return true;
  446. }
  447. /**
  448. * tipc_link_bc_create - create new link to be used for broadcast
  449. * @n: pointer to associated node
  450. * @mtu: mtu to be used initially if no peers
  451. * @window: send window to be used
  452. * @inputq: queue to put messages ready for delivery
  453. * @namedq: queue to put binding table update messages ready for delivery
  454. * @link: return value, pointer to put the created link
  455. *
  456. * Returns true if link was created, otherwise false
  457. */
  458. bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
  459. int mtu, int window, u16 peer_caps,
  460. struct sk_buff_head *inputq,
  461. struct sk_buff_head *namedq,
  462. struct tipc_link *bc_sndlink,
  463. struct tipc_link **link)
  464. {
  465. struct tipc_link *l;
  466. if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
  467. 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
  468. NULL, inputq, namedq, link))
  469. return false;
  470. l = *link;
  471. strcpy(l->name, tipc_bclink_name);
  472. tipc_link_reset(l);
  473. l->state = LINK_RESET;
  474. l->ackers = 0;
  475. l->bc_rcvlink = l;
  476. /* Broadcast send link is always up */
  477. if (link_is_bc_sndlink(l))
  478. l->state = LINK_ESTABLISHED;
  479. /* Disable replicast if even a single peer doesn't support it */
  480. if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
  481. tipc_bcast_disable_rcast(net);
  482. return true;
  483. }
  484. /**
  485. * tipc_link_fsm_evt - link finite state machine
  486. * @l: pointer to link
  487. * @evt: state machine event to be processed
  488. */
  489. int tipc_link_fsm_evt(struct tipc_link *l, int evt)
  490. {
  491. int rc = 0;
  492. switch (l->state) {
  493. case LINK_RESETTING:
  494. switch (evt) {
  495. case LINK_PEER_RESET_EVT:
  496. l->state = LINK_PEER_RESET;
  497. break;
  498. case LINK_RESET_EVT:
  499. l->state = LINK_RESET;
  500. break;
  501. case LINK_FAILURE_EVT:
  502. case LINK_FAILOVER_BEGIN_EVT:
  503. case LINK_ESTABLISH_EVT:
  504. case LINK_FAILOVER_END_EVT:
  505. case LINK_SYNCH_BEGIN_EVT:
  506. case LINK_SYNCH_END_EVT:
  507. default:
  508. goto illegal_evt;
  509. }
  510. break;
  511. case LINK_RESET:
  512. switch (evt) {
  513. case LINK_PEER_RESET_EVT:
  514. l->state = LINK_ESTABLISHING;
  515. break;
  516. case LINK_FAILOVER_BEGIN_EVT:
  517. l->state = LINK_FAILINGOVER;
  518. case LINK_FAILURE_EVT:
  519. case LINK_RESET_EVT:
  520. case LINK_ESTABLISH_EVT:
  521. case LINK_FAILOVER_END_EVT:
  522. break;
  523. case LINK_SYNCH_BEGIN_EVT:
  524. case LINK_SYNCH_END_EVT:
  525. default:
  526. goto illegal_evt;
  527. }
  528. break;
  529. case LINK_PEER_RESET:
  530. switch (evt) {
  531. case LINK_RESET_EVT:
  532. l->state = LINK_ESTABLISHING;
  533. break;
  534. case LINK_PEER_RESET_EVT:
  535. case LINK_ESTABLISH_EVT:
  536. case LINK_FAILURE_EVT:
  537. break;
  538. case LINK_SYNCH_BEGIN_EVT:
  539. case LINK_SYNCH_END_EVT:
  540. case LINK_FAILOVER_BEGIN_EVT:
  541. case LINK_FAILOVER_END_EVT:
  542. default:
  543. goto illegal_evt;
  544. }
  545. break;
  546. case LINK_FAILINGOVER:
  547. switch (evt) {
  548. case LINK_FAILOVER_END_EVT:
  549. l->state = LINK_RESET;
  550. break;
  551. case LINK_PEER_RESET_EVT:
  552. case LINK_RESET_EVT:
  553. case LINK_ESTABLISH_EVT:
  554. case LINK_FAILURE_EVT:
  555. break;
  556. case LINK_FAILOVER_BEGIN_EVT:
  557. case LINK_SYNCH_BEGIN_EVT:
  558. case LINK_SYNCH_END_EVT:
  559. default:
  560. goto illegal_evt;
  561. }
  562. break;
  563. case LINK_ESTABLISHING:
  564. switch (evt) {
  565. case LINK_ESTABLISH_EVT:
  566. l->state = LINK_ESTABLISHED;
  567. break;
  568. case LINK_FAILOVER_BEGIN_EVT:
  569. l->state = LINK_FAILINGOVER;
  570. break;
  571. case LINK_RESET_EVT:
  572. l->state = LINK_RESET;
  573. break;
  574. case LINK_FAILURE_EVT:
  575. case LINK_PEER_RESET_EVT:
  576. case LINK_SYNCH_BEGIN_EVT:
  577. case LINK_FAILOVER_END_EVT:
  578. break;
  579. case LINK_SYNCH_END_EVT:
  580. default:
  581. goto illegal_evt;
  582. }
  583. break;
  584. case LINK_ESTABLISHED:
  585. switch (evt) {
  586. case LINK_PEER_RESET_EVT:
  587. l->state = LINK_PEER_RESET;
  588. rc |= TIPC_LINK_DOWN_EVT;
  589. break;
  590. case LINK_FAILURE_EVT:
  591. l->state = LINK_RESETTING;
  592. rc |= TIPC_LINK_DOWN_EVT;
  593. break;
  594. case LINK_RESET_EVT:
  595. l->state = LINK_RESET;
  596. break;
  597. case LINK_ESTABLISH_EVT:
  598. case LINK_SYNCH_END_EVT:
  599. break;
  600. case LINK_SYNCH_BEGIN_EVT:
  601. l->state = LINK_SYNCHING;
  602. break;
  603. case LINK_FAILOVER_BEGIN_EVT:
  604. case LINK_FAILOVER_END_EVT:
  605. default:
  606. goto illegal_evt;
  607. }
  608. break;
  609. case LINK_SYNCHING:
  610. switch (evt) {
  611. case LINK_PEER_RESET_EVT:
  612. l->state = LINK_PEER_RESET;
  613. rc |= TIPC_LINK_DOWN_EVT;
  614. break;
  615. case LINK_FAILURE_EVT:
  616. l->state = LINK_RESETTING;
  617. rc |= TIPC_LINK_DOWN_EVT;
  618. break;
  619. case LINK_RESET_EVT:
  620. l->state = LINK_RESET;
  621. break;
  622. case LINK_ESTABLISH_EVT:
  623. case LINK_SYNCH_BEGIN_EVT:
  624. break;
  625. case LINK_SYNCH_END_EVT:
  626. l->state = LINK_ESTABLISHED;
  627. break;
  628. case LINK_FAILOVER_BEGIN_EVT:
  629. case LINK_FAILOVER_END_EVT:
  630. default:
  631. goto illegal_evt;
  632. }
  633. break;
  634. default:
  635. pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
  636. }
  637. return rc;
  638. illegal_evt:
  639. pr_err("Illegal FSM event %x in state %x on link %s\n",
  640. evt, l->state, l->name);
  641. return rc;
  642. }
  643. /* link_profile_stats - update statistical profiling of traffic
  644. */
  645. static void link_profile_stats(struct tipc_link *l)
  646. {
  647. struct sk_buff *skb;
  648. struct tipc_msg *msg;
  649. int length;
  650. /* Update counters used in statistical profiling of send traffic */
  651. l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
  652. l->stats.queue_sz_counts++;
  653. skb = skb_peek(&l->transmq);
  654. if (!skb)
  655. return;
  656. msg = buf_msg(skb);
  657. length = msg_size(msg);
  658. if (msg_user(msg) == MSG_FRAGMENTER) {
  659. if (msg_type(msg) != FIRST_FRAGMENT)
  660. return;
  661. length = msg_size(msg_get_wrapped(msg));
  662. }
  663. l->stats.msg_lengths_total += length;
  664. l->stats.msg_length_counts++;
  665. if (length <= 64)
  666. l->stats.msg_length_profile[0]++;
  667. else if (length <= 256)
  668. l->stats.msg_length_profile[1]++;
  669. else if (length <= 1024)
  670. l->stats.msg_length_profile[2]++;
  671. else if (length <= 4096)
  672. l->stats.msg_length_profile[3]++;
  673. else if (length <= 16384)
  674. l->stats.msg_length_profile[4]++;
  675. else if (length <= 32768)
  676. l->stats.msg_length_profile[5]++;
  677. else
  678. l->stats.msg_length_profile[6]++;
  679. }
  680. /* tipc_link_timeout - perform periodic task as instructed from node timeout
  681. */
  682. int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
  683. {
  684. int mtyp = 0;
  685. int rc = 0;
  686. bool state = false;
  687. bool probe = false;
  688. bool setup = false;
  689. u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
  690. u16 bc_acked = l->bc_rcvlink->acked;
  691. struct tipc_mon_state *mstate = &l->mon_state;
  692. switch (l->state) {
  693. case LINK_ESTABLISHED:
  694. case LINK_SYNCHING:
  695. mtyp = STATE_MSG;
  696. link_profile_stats(l);
  697. tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
  698. if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
  699. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  700. state = bc_acked != bc_snt;
  701. state |= l->bc_rcvlink->rcv_unacked;
  702. state |= l->rcv_unacked;
  703. state |= !skb_queue_empty(&l->transmq);
  704. state |= !skb_queue_empty(&l->deferdq);
  705. probe = mstate->probing;
  706. probe |= l->silent_intv_cnt;
  707. if (probe || mstate->monitoring)
  708. l->silent_intv_cnt++;
  709. break;
  710. case LINK_RESET:
  711. setup = l->rst_cnt++ <= 4;
  712. setup |= !(l->rst_cnt % 16);
  713. mtyp = RESET_MSG;
  714. break;
  715. case LINK_ESTABLISHING:
  716. setup = true;
  717. mtyp = ACTIVATE_MSG;
  718. break;
  719. case LINK_PEER_RESET:
  720. case LINK_RESETTING:
  721. case LINK_FAILINGOVER:
  722. break;
  723. default:
  724. break;
  725. }
  726. if (state || probe || setup)
  727. tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
  728. return rc;
  729. }
  730. /**
  731. * link_schedule_user - schedule a message sender for wakeup after congestion
  732. * @l: congested link
  733. * @hdr: header of message that is being sent
  734. * Create pseudo msg to send back to user when congestion abates
  735. */
  736. static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
  737. {
  738. u32 dnode = tipc_own_addr(l->net);
  739. u32 dport = msg_origport(hdr);
  740. struct sk_buff *skb;
  741. /* Create and schedule wakeup pseudo message */
  742. skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
  743. dnode, l->addr, dport, 0, 0);
  744. if (!skb)
  745. return -ENOBUFS;
  746. msg_set_dest_droppable(buf_msg(skb), true);
  747. TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
  748. skb_queue_tail(&l->wakeupq, skb);
  749. l->stats.link_congs++;
  750. return -ELINKCONG;
  751. }
  752. /**
  753. * link_prepare_wakeup - prepare users for wakeup after congestion
  754. * @l: congested link
  755. * Wake up a number of waiting users, as permitted by available space
  756. * in the send queue
  757. */
  758. static void link_prepare_wakeup(struct tipc_link *l)
  759. {
  760. struct sk_buff_head *wakeupq = &l->wakeupq;
  761. struct sk_buff_head *inputq = l->inputq;
  762. struct sk_buff *skb, *tmp;
  763. struct sk_buff_head tmpq;
  764. int avail[5] = {0,};
  765. int imp = 0;
  766. __skb_queue_head_init(&tmpq);
  767. for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
  768. avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
  769. skb_queue_walk_safe(wakeupq, skb, tmp) {
  770. imp = TIPC_SKB_CB(skb)->chain_imp;
  771. if (avail[imp] <= 0)
  772. continue;
  773. avail[imp]--;
  774. __skb_unlink(skb, wakeupq);
  775. __skb_queue_tail(&tmpq, skb);
  776. }
  777. spin_lock_bh(&inputq->lock);
  778. skb_queue_splice_tail(&tmpq, inputq);
  779. spin_unlock_bh(&inputq->lock);
  780. }
  781. void tipc_link_reset(struct tipc_link *l)
  782. {
  783. struct sk_buff_head list;
  784. u32 imp;
  785. __skb_queue_head_init(&list);
  786. l->in_session = false;
  787. l->session++;
  788. l->mtu = l->advertised_mtu;
  789. spin_lock_bh(&l->wakeupq.lock);
  790. skb_queue_splice_init(&l->wakeupq, &list);
  791. spin_unlock_bh(&l->wakeupq.lock);
  792. spin_lock_bh(&l->inputq->lock);
  793. skb_queue_splice_init(&list, l->inputq);
  794. spin_unlock_bh(&l->inputq->lock);
  795. __skb_queue_purge(&l->transmq);
  796. __skb_queue_purge(&l->deferdq);
  797. __skb_queue_purge(&l->backlogq);
  798. for (imp = 0; imp <= TIPC_SYSTEM_IMPORTANCE; imp++) {
  799. l->backlog[imp].len = 0;
  800. l->backlog[imp].target_bskb = NULL;
  801. }
  802. kfree_skb(l->reasm_buf);
  803. kfree_skb(l->failover_reasm_skb);
  804. l->reasm_buf = NULL;
  805. l->failover_reasm_skb = NULL;
  806. l->rcv_unacked = 0;
  807. l->snd_nxt = 1;
  808. l->rcv_nxt = 1;
  809. l->snd_nxt_state = 1;
  810. l->rcv_nxt_state = 1;
  811. l->acked = 0;
  812. l->silent_intv_cnt = 0;
  813. l->rst_cnt = 0;
  814. l->stale_cnt = 0;
  815. l->bc_peer_is_up = false;
  816. memset(&l->mon_state, 0, sizeof(l->mon_state));
  817. tipc_link_reset_stats(l);
  818. }
  819. /**
  820. * tipc_link_xmit(): enqueue buffer list according to queue situation
  821. * @link: link to use
  822. * @list: chain of buffers containing message
  823. * @xmitq: returned list of packets to be sent by caller
  824. *
  825. * Consumes the buffer chain.
  826. * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  827. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  828. */
  829. int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
  830. struct sk_buff_head *xmitq)
  831. {
  832. unsigned int maxwin = l->window;
  833. unsigned int mtu = l->mtu;
  834. u16 ack = l->rcv_nxt - 1;
  835. u16 seqno = l->snd_nxt;
  836. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  837. struct sk_buff_head *transmq = &l->transmq;
  838. struct sk_buff_head *backlogq = &l->backlogq;
  839. struct sk_buff *skb, *_skb, **tskb;
  840. int pkt_cnt = skb_queue_len(list);
  841. struct tipc_msg *hdr;
  842. int rc = 0;
  843. int imp;
  844. if (pkt_cnt <= 0)
  845. return 0;
  846. hdr = buf_msg(skb_peek(list));
  847. if (unlikely(msg_size(hdr) > mtu)) {
  848. __skb_queue_purge(list);
  849. return -EMSGSIZE;
  850. }
  851. imp = msg_importance(hdr);
  852. /* Allow oversubscription of one data msg per source at congestion */
  853. if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
  854. if (imp == TIPC_SYSTEM_IMPORTANCE) {
  855. pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
  856. return -ENOBUFS;
  857. }
  858. rc = link_schedule_user(l, hdr);
  859. }
  860. if (pkt_cnt > 1) {
  861. l->stats.sent_fragmented++;
  862. l->stats.sent_fragments += pkt_cnt;
  863. }
  864. /* Prepare each packet for sending, and add to relevant queue: */
  865. while (skb_queue_len(list)) {
  866. skb = skb_peek(list);
  867. hdr = buf_msg(skb);
  868. msg_set_seqno(hdr, seqno);
  869. msg_set_ack(hdr, ack);
  870. msg_set_bcast_ack(hdr, bc_ack);
  871. if (likely(skb_queue_len(transmq) < maxwin)) {
  872. _skb = skb_clone(skb, GFP_ATOMIC);
  873. if (!_skb) {
  874. __skb_queue_purge(list);
  875. return -ENOBUFS;
  876. }
  877. __skb_dequeue(list);
  878. __skb_queue_tail(transmq, skb);
  879. __skb_queue_tail(xmitq, _skb);
  880. TIPC_SKB_CB(skb)->ackers = l->ackers;
  881. l->rcv_unacked = 0;
  882. l->stats.sent_pkts++;
  883. seqno++;
  884. continue;
  885. }
  886. tskb = &l->backlog[imp].target_bskb;
  887. if (tipc_msg_bundle(*tskb, hdr, mtu)) {
  888. kfree_skb(__skb_dequeue(list));
  889. l->stats.sent_bundled++;
  890. continue;
  891. }
  892. if (tipc_msg_make_bundle(tskb, hdr, mtu, l->addr)) {
  893. kfree_skb(__skb_dequeue(list));
  894. __skb_queue_tail(backlogq, *tskb);
  895. l->backlog[imp].len++;
  896. l->stats.sent_bundled++;
  897. l->stats.sent_bundles++;
  898. continue;
  899. }
  900. l->backlog[imp].target_bskb = NULL;
  901. l->backlog[imp].len += skb_queue_len(list);
  902. skb_queue_splice_tail_init(list, backlogq);
  903. }
  904. l->snd_nxt = seqno;
  905. return rc;
  906. }
  907. static void tipc_link_advance_backlog(struct tipc_link *l,
  908. struct sk_buff_head *xmitq)
  909. {
  910. struct sk_buff *skb, *_skb;
  911. struct tipc_msg *hdr;
  912. u16 seqno = l->snd_nxt;
  913. u16 ack = l->rcv_nxt - 1;
  914. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  915. u32 imp;
  916. while (skb_queue_len(&l->transmq) < l->window) {
  917. skb = skb_peek(&l->backlogq);
  918. if (!skb)
  919. break;
  920. _skb = skb_clone(skb, GFP_ATOMIC);
  921. if (!_skb)
  922. break;
  923. __skb_dequeue(&l->backlogq);
  924. hdr = buf_msg(skb);
  925. imp = msg_importance(hdr);
  926. l->backlog[imp].len--;
  927. if (unlikely(skb == l->backlog[imp].target_bskb))
  928. l->backlog[imp].target_bskb = NULL;
  929. __skb_queue_tail(&l->transmq, skb);
  930. __skb_queue_tail(xmitq, _skb);
  931. TIPC_SKB_CB(skb)->ackers = l->ackers;
  932. msg_set_seqno(hdr, seqno);
  933. msg_set_ack(hdr, ack);
  934. msg_set_bcast_ack(hdr, bc_ack);
  935. l->rcv_unacked = 0;
  936. l->stats.sent_pkts++;
  937. seqno++;
  938. }
  939. l->snd_nxt = seqno;
  940. }
  941. static void link_retransmit_failure(struct tipc_link *l, struct sk_buff *skb)
  942. {
  943. struct tipc_msg *hdr = buf_msg(skb);
  944. pr_warn("Retransmission failure on link <%s>\n", l->name);
  945. link_print(l, "State of link ");
  946. pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
  947. msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
  948. pr_info("sqno %u, prev: %x, src: %x\n",
  949. msg_seqno(hdr), msg_prevnode(hdr), msg_orignode(hdr));
  950. }
  951. /* tipc_link_retrans() - retransmit one or more packets
  952. * @l: the link to transmit on
  953. * @r: the receiving link ordering the retransmit. Same as l if unicast
  954. * @from: retransmit from (inclusive) this sequence number
  955. * @to: retransmit to (inclusive) this sequence number
  956. * xmitq: queue for accumulating the retransmitted packets
  957. */
  958. static int tipc_link_retrans(struct tipc_link *l, struct tipc_link *r,
  959. u16 from, u16 to, struct sk_buff_head *xmitq)
  960. {
  961. struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
  962. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  963. u16 ack = l->rcv_nxt - 1;
  964. struct tipc_msg *hdr;
  965. if (!skb)
  966. return 0;
  967. /* Detect repeated retransmit failures on same packet */
  968. if (r->last_retransm != buf_seqno(skb)) {
  969. r->last_retransm = buf_seqno(skb);
  970. r->stale_limit = jiffies + msecs_to_jiffies(r->tolerance);
  971. r->stale_cnt = 0;
  972. } else if (++r->stale_cnt > 99 && time_after(jiffies, r->stale_limit)) {
  973. link_retransmit_failure(l, skb);
  974. if (link_is_bc_sndlink(l))
  975. return TIPC_LINK_DOWN_EVT;
  976. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  977. }
  978. skb_queue_walk(&l->transmq, skb) {
  979. hdr = buf_msg(skb);
  980. if (less(msg_seqno(hdr), from))
  981. continue;
  982. if (more(msg_seqno(hdr), to))
  983. break;
  984. _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
  985. if (!_skb)
  986. return 0;
  987. hdr = buf_msg(_skb);
  988. msg_set_ack(hdr, ack);
  989. msg_set_bcast_ack(hdr, bc_ack);
  990. _skb->priority = TC_PRIO_CONTROL;
  991. __skb_queue_tail(xmitq, _skb);
  992. l->stats.retransmitted++;
  993. }
  994. return 0;
  995. }
  996. /* tipc_data_input - deliver data and name distr msgs to upper layer
  997. *
  998. * Consumes buffer if message is of right type
  999. * Node lock must be held
  1000. */
  1001. static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
  1002. struct sk_buff_head *inputq)
  1003. {
  1004. struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
  1005. struct tipc_msg *hdr = buf_msg(skb);
  1006. switch (msg_user(hdr)) {
  1007. case TIPC_LOW_IMPORTANCE:
  1008. case TIPC_MEDIUM_IMPORTANCE:
  1009. case TIPC_HIGH_IMPORTANCE:
  1010. case TIPC_CRITICAL_IMPORTANCE:
  1011. if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
  1012. skb_queue_tail(mc_inputq, skb);
  1013. return true;
  1014. }
  1015. /* else: fall through */
  1016. case CONN_MANAGER:
  1017. skb_queue_tail(inputq, skb);
  1018. return true;
  1019. case GROUP_PROTOCOL:
  1020. skb_queue_tail(mc_inputq, skb);
  1021. return true;
  1022. case NAME_DISTRIBUTOR:
  1023. l->bc_rcvlink->state = LINK_ESTABLISHED;
  1024. skb_queue_tail(l->namedq, skb);
  1025. return true;
  1026. case MSG_BUNDLER:
  1027. case TUNNEL_PROTOCOL:
  1028. case MSG_FRAGMENTER:
  1029. case BCAST_PROTOCOL:
  1030. return false;
  1031. default:
  1032. pr_warn("Dropping received illegal msg type\n");
  1033. kfree_skb(skb);
  1034. return true;
  1035. };
  1036. }
  1037. /* tipc_link_input - process packet that has passed link protocol check
  1038. *
  1039. * Consumes buffer
  1040. */
  1041. static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
  1042. struct sk_buff_head *inputq)
  1043. {
  1044. struct tipc_msg *hdr = buf_msg(skb);
  1045. struct sk_buff **reasm_skb = &l->reasm_buf;
  1046. struct sk_buff *iskb;
  1047. struct sk_buff_head tmpq;
  1048. int usr = msg_user(hdr);
  1049. int rc = 0;
  1050. int pos = 0;
  1051. int ipos = 0;
  1052. if (unlikely(usr == TUNNEL_PROTOCOL)) {
  1053. if (msg_type(hdr) == SYNCH_MSG) {
  1054. __skb_queue_purge(&l->deferdq);
  1055. goto drop;
  1056. }
  1057. if (!tipc_msg_extract(skb, &iskb, &ipos))
  1058. return rc;
  1059. kfree_skb(skb);
  1060. skb = iskb;
  1061. hdr = buf_msg(skb);
  1062. if (less(msg_seqno(hdr), l->drop_point))
  1063. goto drop;
  1064. if (tipc_data_input(l, skb, inputq))
  1065. return rc;
  1066. usr = msg_user(hdr);
  1067. reasm_skb = &l->failover_reasm_skb;
  1068. }
  1069. if (usr == MSG_BUNDLER) {
  1070. skb_queue_head_init(&tmpq);
  1071. l->stats.recv_bundles++;
  1072. l->stats.recv_bundled += msg_msgcnt(hdr);
  1073. while (tipc_msg_extract(skb, &iskb, &pos))
  1074. tipc_data_input(l, iskb, &tmpq);
  1075. tipc_skb_queue_splice_tail(&tmpq, inputq);
  1076. return 0;
  1077. } else if (usr == MSG_FRAGMENTER) {
  1078. l->stats.recv_fragments++;
  1079. if (tipc_buf_append(reasm_skb, &skb)) {
  1080. l->stats.recv_fragmented++;
  1081. tipc_data_input(l, skb, inputq);
  1082. } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
  1083. pr_warn_ratelimited("Unable to build fragment list\n");
  1084. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1085. }
  1086. return 0;
  1087. } else if (usr == BCAST_PROTOCOL) {
  1088. tipc_bcast_lock(l->net);
  1089. tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
  1090. tipc_bcast_unlock(l->net);
  1091. }
  1092. drop:
  1093. kfree_skb(skb);
  1094. return 0;
  1095. }
  1096. static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
  1097. {
  1098. bool released = false;
  1099. struct sk_buff *skb, *tmp;
  1100. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  1101. if (more(buf_seqno(skb), acked))
  1102. break;
  1103. __skb_unlink(skb, &l->transmq);
  1104. kfree_skb(skb);
  1105. released = true;
  1106. }
  1107. return released;
  1108. }
  1109. /* tipc_link_build_state_msg: prepare link state message for transmission
  1110. *
  1111. * Note that sending of broadcast ack is coordinated among nodes, to reduce
  1112. * risk of ack storms towards the sender
  1113. */
  1114. int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1115. {
  1116. if (!l)
  1117. return 0;
  1118. /* Broadcast ACK must be sent via a unicast link => defer to caller */
  1119. if (link_is_bc_rcvlink(l)) {
  1120. if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
  1121. return 0;
  1122. l->rcv_unacked = 0;
  1123. /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
  1124. l->snd_nxt = l->rcv_nxt;
  1125. return TIPC_LINK_SND_STATE;
  1126. }
  1127. /* Unicast ACK */
  1128. l->rcv_unacked = 0;
  1129. l->stats.sent_acks++;
  1130. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
  1131. return 0;
  1132. }
  1133. /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
  1134. */
  1135. void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1136. {
  1137. int mtyp = RESET_MSG;
  1138. struct sk_buff *skb;
  1139. if (l->state == LINK_ESTABLISHING)
  1140. mtyp = ACTIVATE_MSG;
  1141. tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
  1142. /* Inform peer that this endpoint is going down if applicable */
  1143. skb = skb_peek_tail(xmitq);
  1144. if (skb && (l->state == LINK_RESET))
  1145. msg_set_peer_stopping(buf_msg(skb), 1);
  1146. }
  1147. /* tipc_link_build_nack_msg: prepare link nack message for transmission
  1148. * Note that sending of broadcast NACK is coordinated among nodes, to
  1149. * reduce the risk of NACK storms towards the sender
  1150. */
  1151. static int tipc_link_build_nack_msg(struct tipc_link *l,
  1152. struct sk_buff_head *xmitq)
  1153. {
  1154. u32 def_cnt = ++l->stats.deferred_recv;
  1155. int match1, match2;
  1156. if (link_is_bc_rcvlink(l)) {
  1157. match1 = def_cnt & 0xf;
  1158. match2 = tipc_own_addr(l->net) & 0xf;
  1159. if (match1 == match2)
  1160. return TIPC_LINK_SND_STATE;
  1161. return 0;
  1162. }
  1163. if ((skb_queue_len(&l->deferdq) == 1) || !(def_cnt % TIPC_NACK_INTV))
  1164. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
  1165. return 0;
  1166. }
  1167. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  1168. * @l: the link that should handle the message
  1169. * @skb: TIPC packet
  1170. * @xmitq: queue to place packets to be sent after this call
  1171. */
  1172. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  1173. struct sk_buff_head *xmitq)
  1174. {
  1175. struct sk_buff_head *defq = &l->deferdq;
  1176. struct tipc_msg *hdr;
  1177. u16 seqno, rcv_nxt, win_lim;
  1178. int rc = 0;
  1179. do {
  1180. hdr = buf_msg(skb);
  1181. seqno = msg_seqno(hdr);
  1182. rcv_nxt = l->rcv_nxt;
  1183. win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
  1184. /* Verify and update link state */
  1185. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  1186. return tipc_link_proto_rcv(l, skb, xmitq);
  1187. if (unlikely(!link_is_up(l))) {
  1188. if (l->state == LINK_ESTABLISHING)
  1189. rc = TIPC_LINK_UP_EVT;
  1190. goto drop;
  1191. }
  1192. /* Don't send probe at next timeout expiration */
  1193. l->silent_intv_cnt = 0;
  1194. /* Drop if outside receive window */
  1195. if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
  1196. l->stats.duplicates++;
  1197. goto drop;
  1198. }
  1199. /* Forward queues and wake up waiting users */
  1200. if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
  1201. l->stale_cnt = 0;
  1202. tipc_link_advance_backlog(l, xmitq);
  1203. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1204. link_prepare_wakeup(l);
  1205. }
  1206. /* Defer delivery if sequence gap */
  1207. if (unlikely(seqno != rcv_nxt)) {
  1208. __tipc_skb_queue_sorted(defq, seqno, skb);
  1209. rc |= tipc_link_build_nack_msg(l, xmitq);
  1210. break;
  1211. }
  1212. /* Deliver packet */
  1213. l->rcv_nxt++;
  1214. l->stats.recv_pkts++;
  1215. if (!tipc_data_input(l, skb, l->inputq))
  1216. rc |= tipc_link_input(l, skb, l->inputq);
  1217. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
  1218. rc |= tipc_link_build_state_msg(l, xmitq);
  1219. if (unlikely(rc & ~TIPC_LINK_SND_STATE))
  1220. break;
  1221. } while ((skb = __skb_dequeue(defq)));
  1222. return rc;
  1223. drop:
  1224. kfree_skb(skb);
  1225. return rc;
  1226. }
  1227. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1228. bool probe_reply, u16 rcvgap,
  1229. int tolerance, int priority,
  1230. struct sk_buff_head *xmitq)
  1231. {
  1232. struct tipc_link *bcl = l->bc_rcvlink;
  1233. struct sk_buff *skb;
  1234. struct tipc_msg *hdr;
  1235. struct sk_buff_head *dfq = &l->deferdq;
  1236. bool node_up = link_is_up(bcl);
  1237. struct tipc_mon_state *mstate = &l->mon_state;
  1238. int dlen = 0;
  1239. void *data;
  1240. /* Don't send protocol message during reset or link failover */
  1241. if (tipc_link_is_blocked(l))
  1242. return;
  1243. if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
  1244. return;
  1245. if (!skb_queue_empty(dfq))
  1246. rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1247. skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
  1248. tipc_max_domain_size, l->addr,
  1249. tipc_own_addr(l->net), 0, 0, 0);
  1250. if (!skb)
  1251. return;
  1252. hdr = buf_msg(skb);
  1253. data = msg_data(hdr);
  1254. msg_set_session(hdr, l->session);
  1255. msg_set_bearer_id(hdr, l->bearer_id);
  1256. msg_set_net_plane(hdr, l->net_plane);
  1257. msg_set_next_sent(hdr, l->snd_nxt);
  1258. msg_set_ack(hdr, l->rcv_nxt - 1);
  1259. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1260. msg_set_bc_ack_invalid(hdr, !node_up);
  1261. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1262. msg_set_link_tolerance(hdr, tolerance);
  1263. msg_set_linkprio(hdr, priority);
  1264. msg_set_redundant_link(hdr, node_up);
  1265. msg_set_seq_gap(hdr, 0);
  1266. msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
  1267. if (mtyp == STATE_MSG) {
  1268. if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
  1269. msg_set_seqno(hdr, l->snd_nxt_state++);
  1270. msg_set_seq_gap(hdr, rcvgap);
  1271. msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
  1272. msg_set_probe(hdr, probe);
  1273. msg_set_is_keepalive(hdr, probe || probe_reply);
  1274. tipc_mon_prep(l->net, data, &dlen, mstate, l->bearer_id);
  1275. msg_set_size(hdr, INT_H_SIZE + dlen);
  1276. skb_trim(skb, INT_H_SIZE + dlen);
  1277. l->stats.sent_states++;
  1278. l->rcv_unacked = 0;
  1279. } else {
  1280. /* RESET_MSG or ACTIVATE_MSG */
  1281. msg_set_max_pkt(hdr, l->advertised_mtu);
  1282. strcpy(data, l->if_name);
  1283. msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1284. skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1285. }
  1286. if (probe)
  1287. l->stats.sent_probes++;
  1288. if (rcvgap)
  1289. l->stats.sent_nacks++;
  1290. skb->priority = TC_PRIO_CONTROL;
  1291. __skb_queue_tail(xmitq, skb);
  1292. }
  1293. void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
  1294. struct sk_buff_head *xmitq)
  1295. {
  1296. u32 onode = tipc_own_addr(l->net);
  1297. struct tipc_msg *hdr, *ihdr;
  1298. struct sk_buff_head tnlq;
  1299. struct sk_buff *skb;
  1300. u32 dnode = l->addr;
  1301. __skb_queue_head_init(&tnlq);
  1302. skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
  1303. INT_H_SIZE, BASIC_H_SIZE,
  1304. dnode, onode, 0, 0, 0);
  1305. if (!skb) {
  1306. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1307. return;
  1308. }
  1309. hdr = buf_msg(skb);
  1310. msg_set_msgcnt(hdr, 1);
  1311. msg_set_bearer_id(hdr, l->peer_bearer_id);
  1312. ihdr = (struct tipc_msg *)msg_data(hdr);
  1313. tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1314. BASIC_H_SIZE, dnode);
  1315. msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
  1316. __skb_queue_tail(&tnlq, skb);
  1317. tipc_link_xmit(l, &tnlq, xmitq);
  1318. }
  1319. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1320. * with contents of the link's transmit and backlog queues.
  1321. */
  1322. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1323. int mtyp, struct sk_buff_head *xmitq)
  1324. {
  1325. struct sk_buff *skb, *tnlskb;
  1326. struct tipc_msg *hdr, tnlhdr;
  1327. struct sk_buff_head *queue = &l->transmq;
  1328. struct sk_buff_head tmpxq, tnlq;
  1329. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1330. if (!tnl)
  1331. return;
  1332. __skb_queue_head_init(&tnlq);
  1333. __skb_queue_head_init(&tmpxq);
  1334. /* At least one packet required for safe algorithm => add dummy */
  1335. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1336. BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
  1337. 0, 0, TIPC_ERR_NO_PORT);
  1338. if (!skb) {
  1339. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1340. return;
  1341. }
  1342. __skb_queue_tail(&tnlq, skb);
  1343. tipc_link_xmit(l, &tnlq, &tmpxq);
  1344. __skb_queue_purge(&tmpxq);
  1345. /* Initialize reusable tunnel packet header */
  1346. tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
  1347. mtyp, INT_H_SIZE, l->addr);
  1348. pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
  1349. msg_set_msgcnt(&tnlhdr, pktcnt);
  1350. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1351. tnl:
  1352. /* Wrap each packet into a tunnel packet */
  1353. skb_queue_walk(queue, skb) {
  1354. hdr = buf_msg(skb);
  1355. if (queue == &l->backlogq)
  1356. msg_set_seqno(hdr, seqno++);
  1357. pktlen = msg_size(hdr);
  1358. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1359. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
  1360. if (!tnlskb) {
  1361. pr_warn("%sunable to send packet\n", link_co_err);
  1362. return;
  1363. }
  1364. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1365. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1366. __skb_queue_tail(&tnlq, tnlskb);
  1367. }
  1368. if (queue != &l->backlogq) {
  1369. queue = &l->backlogq;
  1370. goto tnl;
  1371. }
  1372. tipc_link_xmit(tnl, &tnlq, xmitq);
  1373. if (mtyp == FAILOVER_MSG) {
  1374. tnl->drop_point = l->rcv_nxt;
  1375. tnl->failover_reasm_skb = l->reasm_buf;
  1376. l->reasm_buf = NULL;
  1377. }
  1378. }
  1379. /* tipc_link_validate_msg(): validate message against current link state
  1380. * Returns true if message should be accepted, otherwise false
  1381. */
  1382. bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
  1383. {
  1384. u16 curr_session = l->peer_session;
  1385. u16 session = msg_session(hdr);
  1386. int mtyp = msg_type(hdr);
  1387. if (msg_user(hdr) != LINK_PROTOCOL)
  1388. return true;
  1389. switch (mtyp) {
  1390. case RESET_MSG:
  1391. if (!l->in_session)
  1392. return true;
  1393. /* Accept only RESET with new session number */
  1394. return more(session, curr_session);
  1395. case ACTIVATE_MSG:
  1396. if (!l->in_session)
  1397. return true;
  1398. /* Accept only ACTIVATE with new or current session number */
  1399. return !less(session, curr_session);
  1400. case STATE_MSG:
  1401. /* Accept only STATE with current session number */
  1402. if (!l->in_session)
  1403. return false;
  1404. if (session != curr_session)
  1405. return false;
  1406. /* Extra sanity check */
  1407. if (!link_is_up(l) && msg_ack(hdr))
  1408. return false;
  1409. if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
  1410. return true;
  1411. /* Accept only STATE with new sequence number */
  1412. return !less(msg_seqno(hdr), l->rcv_nxt_state);
  1413. default:
  1414. return false;
  1415. }
  1416. }
  1417. /* tipc_link_proto_rcv(): receive link level protocol message :
  1418. * Note that network plane id propagates through the network, and may
  1419. * change at any time. The node with lowest numerical id determines
  1420. * network plane
  1421. */
  1422. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1423. struct sk_buff_head *xmitq)
  1424. {
  1425. struct tipc_msg *hdr = buf_msg(skb);
  1426. u16 rcvgap = 0;
  1427. u16 ack = msg_ack(hdr);
  1428. u16 gap = msg_seq_gap(hdr);
  1429. u16 peers_snd_nxt = msg_next_sent(hdr);
  1430. u16 peers_tol = msg_link_tolerance(hdr);
  1431. u16 peers_prio = msg_linkprio(hdr);
  1432. u16 rcv_nxt = l->rcv_nxt;
  1433. u16 dlen = msg_data_sz(hdr);
  1434. int mtyp = msg_type(hdr);
  1435. bool reply = msg_probe(hdr);
  1436. void *data;
  1437. char *if_name;
  1438. int rc = 0;
  1439. if (tipc_link_is_blocked(l) || !xmitq)
  1440. goto exit;
  1441. if (tipc_own_addr(l->net) > msg_prevnode(hdr))
  1442. l->net_plane = msg_net_plane(hdr);
  1443. skb_linearize(skb);
  1444. hdr = buf_msg(skb);
  1445. data = msg_data(hdr);
  1446. if (!tipc_link_validate_msg(l, hdr))
  1447. goto exit;
  1448. switch (mtyp) {
  1449. case RESET_MSG:
  1450. case ACTIVATE_MSG:
  1451. /* Complete own link name with peer's interface name */
  1452. if_name = strrchr(l->name, ':') + 1;
  1453. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  1454. break;
  1455. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  1456. break;
  1457. strncpy(if_name, data, TIPC_MAX_IF_NAME);
  1458. /* Update own tolerance if peer indicates a non-zero value */
  1459. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  1460. l->tolerance = peers_tol;
  1461. l->bc_rcvlink->tolerance = peers_tol;
  1462. }
  1463. /* Update own priority if peer's priority is higher */
  1464. if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  1465. l->priority = peers_prio;
  1466. /* If peer is going down we want full re-establish cycle */
  1467. if (msg_peer_stopping(hdr)) {
  1468. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1469. break;
  1470. }
  1471. /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
  1472. if (mtyp == RESET_MSG || !link_is_up(l))
  1473. rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  1474. /* ACTIVATE_MSG takes up link if it was already locally reset */
  1475. if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
  1476. rc = TIPC_LINK_UP_EVT;
  1477. l->peer_session = msg_session(hdr);
  1478. l->in_session = true;
  1479. l->peer_bearer_id = msg_bearer_id(hdr);
  1480. if (l->mtu > msg_max_pkt(hdr))
  1481. l->mtu = msg_max_pkt(hdr);
  1482. break;
  1483. case STATE_MSG:
  1484. l->rcv_nxt_state = msg_seqno(hdr) + 1;
  1485. /* Update own tolerance if peer indicates a non-zero value */
  1486. if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  1487. l->tolerance = peers_tol;
  1488. l->bc_rcvlink->tolerance = peers_tol;
  1489. }
  1490. /* Update own prio if peer indicates a different value */
  1491. if ((peers_prio != l->priority) &&
  1492. in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
  1493. l->priority = peers_prio;
  1494. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1495. }
  1496. l->silent_intv_cnt = 0;
  1497. l->stats.recv_states++;
  1498. if (msg_probe(hdr))
  1499. l->stats.recv_probes++;
  1500. if (!link_is_up(l)) {
  1501. if (l->state == LINK_ESTABLISHING)
  1502. rc = TIPC_LINK_UP_EVT;
  1503. break;
  1504. }
  1505. tipc_mon_rcv(l->net, data, dlen, l->addr,
  1506. &l->mon_state, l->bearer_id);
  1507. /* Send NACK if peer has sent pkts we haven't received yet */
  1508. if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
  1509. rcvgap = peers_snd_nxt - l->rcv_nxt;
  1510. if (rcvgap || reply)
  1511. tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
  1512. rcvgap, 0, 0, xmitq);
  1513. tipc_link_release_pkts(l, ack);
  1514. /* If NACK, retransmit will now start at right position */
  1515. if (gap) {
  1516. rc = tipc_link_retrans(l, l, ack + 1, ack + gap, xmitq);
  1517. l->stats.recv_nacks++;
  1518. }
  1519. tipc_link_advance_backlog(l, xmitq);
  1520. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1521. link_prepare_wakeup(l);
  1522. }
  1523. exit:
  1524. kfree_skb(skb);
  1525. return rc;
  1526. }
  1527. /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
  1528. */
  1529. static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
  1530. u16 peers_snd_nxt,
  1531. struct sk_buff_head *xmitq)
  1532. {
  1533. struct sk_buff *skb;
  1534. struct tipc_msg *hdr;
  1535. struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
  1536. u16 ack = l->rcv_nxt - 1;
  1537. u16 gap_to = peers_snd_nxt - 1;
  1538. skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
  1539. 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
  1540. if (!skb)
  1541. return false;
  1542. hdr = buf_msg(skb);
  1543. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1544. msg_set_bcast_ack(hdr, ack);
  1545. msg_set_bcgap_after(hdr, ack);
  1546. if (dfrd_skb)
  1547. gap_to = buf_seqno(dfrd_skb) - 1;
  1548. msg_set_bcgap_to(hdr, gap_to);
  1549. msg_set_non_seq(hdr, bcast);
  1550. __skb_queue_tail(xmitq, skb);
  1551. return true;
  1552. }
  1553. /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
  1554. *
  1555. * Give a newly added peer node the sequence number where it should
  1556. * start receiving and acking broadcast packets.
  1557. */
  1558. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  1559. struct sk_buff_head *xmitq)
  1560. {
  1561. struct sk_buff_head list;
  1562. __skb_queue_head_init(&list);
  1563. if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
  1564. return;
  1565. msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
  1566. tipc_link_xmit(l, &list, xmitq);
  1567. }
  1568. /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
  1569. */
  1570. void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
  1571. {
  1572. int mtyp = msg_type(hdr);
  1573. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  1574. if (link_is_up(l))
  1575. return;
  1576. if (msg_user(hdr) == BCAST_PROTOCOL) {
  1577. l->rcv_nxt = peers_snd_nxt;
  1578. l->state = LINK_ESTABLISHED;
  1579. return;
  1580. }
  1581. if (l->peer_caps & TIPC_BCAST_SYNCH)
  1582. return;
  1583. if (msg_peer_node_is_up(hdr))
  1584. return;
  1585. /* Compatibility: accept older, less safe initial synch data */
  1586. if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
  1587. l->rcv_nxt = peers_snd_nxt;
  1588. }
  1589. /* link_bc_retr eval()- check if the indicated range can be retransmitted now
  1590. * - Adjust permitted range if there is overlap with previous retransmission
  1591. */
  1592. static bool link_bc_retr_eval(struct tipc_link *l, u16 *from, u16 *to)
  1593. {
  1594. unsigned long elapsed = jiffies_to_msecs(jiffies - l->prev_retr);
  1595. if (less(*to, *from))
  1596. return false;
  1597. /* New retransmission request */
  1598. if ((elapsed > TIPC_BC_RETR_LIMIT) ||
  1599. less(*to, l->prev_from) || more(*from, l->prev_to)) {
  1600. l->prev_from = *from;
  1601. l->prev_to = *to;
  1602. l->prev_retr = jiffies;
  1603. return true;
  1604. }
  1605. /* Inside range of previous retransmit */
  1606. if (!less(*from, l->prev_from) && !more(*to, l->prev_to))
  1607. return false;
  1608. /* Fully or partially outside previous range => exclude overlap */
  1609. if (less(*from, l->prev_from)) {
  1610. *to = l->prev_from - 1;
  1611. l->prev_from = *from;
  1612. }
  1613. if (more(*to, l->prev_to)) {
  1614. *from = l->prev_to + 1;
  1615. l->prev_to = *to;
  1616. }
  1617. l->prev_retr = jiffies;
  1618. return true;
  1619. }
  1620. /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
  1621. */
  1622. int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
  1623. struct sk_buff_head *xmitq)
  1624. {
  1625. struct tipc_link *snd_l = l->bc_sndlink;
  1626. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  1627. u16 from = msg_bcast_ack(hdr) + 1;
  1628. u16 to = from + msg_bc_gap(hdr) - 1;
  1629. int rc = 0;
  1630. if (!link_is_up(l))
  1631. return rc;
  1632. if (!msg_peer_node_is_up(hdr))
  1633. return rc;
  1634. /* Open when peer ackowledges our bcast init msg (pkt #1) */
  1635. if (msg_ack(hdr))
  1636. l->bc_peer_is_up = true;
  1637. if (!l->bc_peer_is_up)
  1638. return rc;
  1639. l->stats.recv_nacks++;
  1640. /* Ignore if peers_snd_nxt goes beyond receive window */
  1641. if (more(peers_snd_nxt, l->rcv_nxt + l->window))
  1642. return rc;
  1643. if (link_bc_retr_eval(snd_l, &from, &to))
  1644. rc = tipc_link_retrans(snd_l, l, from, to, xmitq);
  1645. l->snd_nxt = peers_snd_nxt;
  1646. if (link_bc_rcv_gap(l))
  1647. rc |= TIPC_LINK_SND_STATE;
  1648. /* Return now if sender supports nack via STATE messages */
  1649. if (l->peer_caps & TIPC_BCAST_STATE_NACK)
  1650. return rc;
  1651. /* Otherwise, be backwards compatible */
  1652. if (!more(peers_snd_nxt, l->rcv_nxt)) {
  1653. l->nack_state = BC_NACK_SND_CONDITIONAL;
  1654. return 0;
  1655. }
  1656. /* Don't NACK if one was recently sent or peeked */
  1657. if (l->nack_state == BC_NACK_SND_SUPPRESS) {
  1658. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  1659. return 0;
  1660. }
  1661. /* Conditionally delay NACK sending until next synch rcv */
  1662. if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
  1663. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  1664. if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
  1665. return 0;
  1666. }
  1667. /* Send NACK now but suppress next one */
  1668. tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
  1669. l->nack_state = BC_NACK_SND_SUPPRESS;
  1670. return 0;
  1671. }
  1672. void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
  1673. struct sk_buff_head *xmitq)
  1674. {
  1675. struct sk_buff *skb, *tmp;
  1676. struct tipc_link *snd_l = l->bc_sndlink;
  1677. if (!link_is_up(l) || !l->bc_peer_is_up)
  1678. return;
  1679. if (!more(acked, l->acked))
  1680. return;
  1681. /* Skip over packets peer has already acked */
  1682. skb_queue_walk(&snd_l->transmq, skb) {
  1683. if (more(buf_seqno(skb), l->acked))
  1684. break;
  1685. }
  1686. /* Update/release the packets peer is acking now */
  1687. skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
  1688. if (more(buf_seqno(skb), acked))
  1689. break;
  1690. if (!--TIPC_SKB_CB(skb)->ackers) {
  1691. __skb_unlink(skb, &snd_l->transmq);
  1692. kfree_skb(skb);
  1693. }
  1694. }
  1695. l->acked = acked;
  1696. tipc_link_advance_backlog(snd_l, xmitq);
  1697. if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
  1698. link_prepare_wakeup(snd_l);
  1699. }
  1700. /* tipc_link_bc_nack_rcv(): receive broadcast nack message
  1701. * This function is here for backwards compatibility, since
  1702. * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
  1703. */
  1704. int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
  1705. struct sk_buff_head *xmitq)
  1706. {
  1707. struct tipc_msg *hdr = buf_msg(skb);
  1708. u32 dnode = msg_destnode(hdr);
  1709. int mtyp = msg_type(hdr);
  1710. u16 acked = msg_bcast_ack(hdr);
  1711. u16 from = acked + 1;
  1712. u16 to = msg_bcgap_to(hdr);
  1713. u16 peers_snd_nxt = to + 1;
  1714. int rc = 0;
  1715. kfree_skb(skb);
  1716. if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
  1717. return 0;
  1718. if (mtyp != STATE_MSG)
  1719. return 0;
  1720. if (dnode == tipc_own_addr(l->net)) {
  1721. tipc_link_bc_ack_rcv(l, acked, xmitq);
  1722. rc = tipc_link_retrans(l->bc_sndlink, l, from, to, xmitq);
  1723. l->stats.recv_nacks++;
  1724. return rc;
  1725. }
  1726. /* Msg for other node => suppress own NACK at next sync if applicable */
  1727. if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
  1728. l->nack_state = BC_NACK_SND_SUPPRESS;
  1729. return 0;
  1730. }
  1731. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1732. {
  1733. int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
  1734. l->window = win;
  1735. l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
  1736. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
  1737. l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
  1738. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
  1739. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1740. }
  1741. /**
  1742. * link_reset_stats - reset link statistics
  1743. * @l: pointer to link
  1744. */
  1745. void tipc_link_reset_stats(struct tipc_link *l)
  1746. {
  1747. memset(&l->stats, 0, sizeof(l->stats));
  1748. }
  1749. static void link_print(struct tipc_link *l, const char *str)
  1750. {
  1751. struct sk_buff *hskb = skb_peek(&l->transmq);
  1752. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
  1753. u16 tail = l->snd_nxt - 1;
  1754. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  1755. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  1756. skb_queue_len(&l->transmq), head, tail,
  1757. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  1758. }
  1759. /* Parse and validate nested (link) properties valid for media, bearer and link
  1760. */
  1761. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1762. {
  1763. int err;
  1764. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1765. tipc_nl_prop_policy, NULL);
  1766. if (err)
  1767. return err;
  1768. if (props[TIPC_NLA_PROP_PRIO]) {
  1769. u32 prio;
  1770. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1771. if (prio > TIPC_MAX_LINK_PRI)
  1772. return -EINVAL;
  1773. }
  1774. if (props[TIPC_NLA_PROP_TOL]) {
  1775. u32 tol;
  1776. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1777. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1778. return -EINVAL;
  1779. }
  1780. if (props[TIPC_NLA_PROP_WIN]) {
  1781. u32 win;
  1782. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1783. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1784. return -EINVAL;
  1785. }
  1786. return 0;
  1787. }
  1788. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1789. {
  1790. int i;
  1791. struct nlattr *stats;
  1792. struct nla_map {
  1793. u32 key;
  1794. u32 val;
  1795. };
  1796. struct nla_map map[] = {
  1797. {TIPC_NLA_STATS_RX_INFO, 0},
  1798. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1799. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1800. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1801. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1802. {TIPC_NLA_STATS_TX_INFO, 0},
  1803. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1804. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1805. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1806. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1807. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1808. s->msg_length_counts : 1},
  1809. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1810. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1811. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1812. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1813. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1814. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1815. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1816. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1817. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1818. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1819. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1820. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1821. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1822. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1823. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1824. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1825. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1826. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1827. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1828. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1829. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1830. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1831. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1832. };
  1833. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1834. if (!stats)
  1835. return -EMSGSIZE;
  1836. for (i = 0; i < ARRAY_SIZE(map); i++)
  1837. if (nla_put_u32(skb, map[i].key, map[i].val))
  1838. goto msg_full;
  1839. nla_nest_end(skb, stats);
  1840. return 0;
  1841. msg_full:
  1842. nla_nest_cancel(skb, stats);
  1843. return -EMSGSIZE;
  1844. }
  1845. /* Caller should hold appropriate locks to protect the link */
  1846. int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1847. struct tipc_link *link, int nlflags)
  1848. {
  1849. u32 self = tipc_own_addr(net);
  1850. struct nlattr *attrs;
  1851. struct nlattr *prop;
  1852. void *hdr;
  1853. int err;
  1854. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1855. nlflags, TIPC_NL_LINK_GET);
  1856. if (!hdr)
  1857. return -EMSGSIZE;
  1858. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1859. if (!attrs)
  1860. goto msg_full;
  1861. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1862. goto attr_msg_full;
  1863. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
  1864. goto attr_msg_full;
  1865. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1866. goto attr_msg_full;
  1867. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
  1868. goto attr_msg_full;
  1869. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
  1870. goto attr_msg_full;
  1871. if (tipc_link_is_up(link))
  1872. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1873. goto attr_msg_full;
  1874. if (link->active)
  1875. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1876. goto attr_msg_full;
  1877. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1878. if (!prop)
  1879. goto attr_msg_full;
  1880. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1881. goto prop_msg_full;
  1882. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1883. goto prop_msg_full;
  1884. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1885. link->window))
  1886. goto prop_msg_full;
  1887. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1888. goto prop_msg_full;
  1889. nla_nest_end(msg->skb, prop);
  1890. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1891. if (err)
  1892. goto attr_msg_full;
  1893. nla_nest_end(msg->skb, attrs);
  1894. genlmsg_end(msg->skb, hdr);
  1895. return 0;
  1896. prop_msg_full:
  1897. nla_nest_cancel(msg->skb, prop);
  1898. attr_msg_full:
  1899. nla_nest_cancel(msg->skb, attrs);
  1900. msg_full:
  1901. genlmsg_cancel(msg->skb, hdr);
  1902. return -EMSGSIZE;
  1903. }
  1904. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  1905. struct tipc_stats *stats)
  1906. {
  1907. int i;
  1908. struct nlattr *nest;
  1909. struct nla_map {
  1910. __u32 key;
  1911. __u32 val;
  1912. };
  1913. struct nla_map map[] = {
  1914. {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
  1915. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  1916. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  1917. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  1918. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  1919. {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
  1920. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  1921. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  1922. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  1923. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  1924. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  1925. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  1926. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  1927. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  1928. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  1929. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  1930. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  1931. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  1932. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  1933. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  1934. };
  1935. nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1936. if (!nest)
  1937. return -EMSGSIZE;
  1938. for (i = 0; i < ARRAY_SIZE(map); i++)
  1939. if (nla_put_u32(skb, map[i].key, map[i].val))
  1940. goto msg_full;
  1941. nla_nest_end(skb, nest);
  1942. return 0;
  1943. msg_full:
  1944. nla_nest_cancel(skb, nest);
  1945. return -EMSGSIZE;
  1946. }
  1947. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
  1948. {
  1949. int err;
  1950. void *hdr;
  1951. struct nlattr *attrs;
  1952. struct nlattr *prop;
  1953. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1954. struct tipc_link *bcl = tn->bcl;
  1955. if (!bcl)
  1956. return 0;
  1957. tipc_bcast_lock(net);
  1958. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1959. NLM_F_MULTI, TIPC_NL_LINK_GET);
  1960. if (!hdr) {
  1961. tipc_bcast_unlock(net);
  1962. return -EMSGSIZE;
  1963. }
  1964. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1965. if (!attrs)
  1966. goto msg_full;
  1967. /* The broadcast link is always up */
  1968. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1969. goto attr_msg_full;
  1970. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  1971. goto attr_msg_full;
  1972. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  1973. goto attr_msg_full;
  1974. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
  1975. goto attr_msg_full;
  1976. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
  1977. goto attr_msg_full;
  1978. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1979. if (!prop)
  1980. goto attr_msg_full;
  1981. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
  1982. goto prop_msg_full;
  1983. nla_nest_end(msg->skb, prop);
  1984. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  1985. if (err)
  1986. goto attr_msg_full;
  1987. tipc_bcast_unlock(net);
  1988. nla_nest_end(msg->skb, attrs);
  1989. genlmsg_end(msg->skb, hdr);
  1990. return 0;
  1991. prop_msg_full:
  1992. nla_nest_cancel(msg->skb, prop);
  1993. attr_msg_full:
  1994. nla_nest_cancel(msg->skb, attrs);
  1995. msg_full:
  1996. tipc_bcast_unlock(net);
  1997. genlmsg_cancel(msg->skb, hdr);
  1998. return -EMSGSIZE;
  1999. }
  2000. void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
  2001. struct sk_buff_head *xmitq)
  2002. {
  2003. l->tolerance = tol;
  2004. if (l->bc_rcvlink)
  2005. l->bc_rcvlink->tolerance = tol;
  2006. if (link_is_up(l))
  2007. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
  2008. }
  2009. void tipc_link_set_prio(struct tipc_link *l, u32 prio,
  2010. struct sk_buff_head *xmitq)
  2011. {
  2012. l->priority = prio;
  2013. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
  2014. }
  2015. void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
  2016. {
  2017. l->abort_limit = limit;
  2018. }