link.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  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 "trace.h"
  46. #include "crypto.h"
  47. #include <linux/pkt_sched.h>
  48. struct tipc_stats {
  49. u32 sent_pkts;
  50. u32 recv_pkts;
  51. u32 sent_states;
  52. u32 recv_states;
  53. u32 sent_probes;
  54. u32 recv_probes;
  55. u32 sent_nacks;
  56. u32 recv_nacks;
  57. u32 sent_acks;
  58. u32 sent_bundled;
  59. u32 sent_bundles;
  60. u32 recv_bundled;
  61. u32 recv_bundles;
  62. u32 retransmitted;
  63. u32 sent_fragmented;
  64. u32 sent_fragments;
  65. u32 recv_fragmented;
  66. u32 recv_fragments;
  67. u32 link_congs; /* # port sends blocked by congestion */
  68. u32 deferred_recv;
  69. u32 duplicates;
  70. u32 max_queue_sz; /* send queue size high water mark */
  71. u32 accu_queue_sz; /* used for send queue size profiling */
  72. u32 queue_sz_counts; /* used for send queue size profiling */
  73. u32 msg_length_counts; /* used for message length profiling */
  74. u32 msg_lengths_total; /* used for message length profiling */
  75. u32 msg_length_profile[7]; /* used for msg. length profiling */
  76. };
  77. /**
  78. * struct tipc_link - TIPC link data structure
  79. * @addr: network address of link's peer node
  80. * @name: link name character string
  81. * @net: pointer to namespace struct
  82. * @peer_session: link session # being used by peer end of link
  83. * @peer_bearer_id: bearer id used by link's peer endpoint
  84. * @bearer_id: local bearer id used by link
  85. * @tolerance: minimum link continuity loss needed to reset link [in ms]
  86. * @abort_limit: # of unacknowledged continuity probes needed to reset link
  87. * @state: current state of link FSM
  88. * @peer_caps: bitmap describing capabilities of peer node
  89. * @silent_intv_cnt: # of timer intervals without any reception from peer
  90. * @priority: current link priority
  91. * @net_plane: current link network plane ('A' through 'H')
  92. * @mon_state: cookie with information needed by link monitor
  93. * @mtu: current maximum packet size for this link
  94. * @advertised_mtu: advertised own mtu when link is being established
  95. * @backlogq: queue for messages waiting to be sent
  96. * @ackers: # of peers that needs to ack each packet before it can be released
  97. * @acked: # last packet acked by a certain peer. Used for broadcast.
  98. * @rcv_nxt: next sequence number to expect for inbound messages
  99. * @inputq: buffer queue for messages to be delivered upwards
  100. * @namedq: buffer queue for name table messages to be delivered upwards
  101. * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
  102. * @reasm_buf: head of partially reassembled inbound message fragments
  103. * @stats: collects statistics regarding link activity
  104. * @session: session to be used by link
  105. * @snd_nxt_state: next send seq number
  106. * @rcv_nxt_state: next rcv seq number
  107. * @in_session: have received ACTIVATE_MSG from peer
  108. * @active: link is active
  109. * @if_name: associated interface name
  110. * @rst_cnt: link reset counter
  111. * @drop_point: seq number for failover handling (FIXME)
  112. * @failover_reasm_skb: saved failover msg ptr (FIXME)
  113. * @failover_deferdq: deferred message queue for failover processing (FIXME)
  114. * @transmq: the link's transmit queue
  115. * @backlog: link's backlog by priority (importance)
  116. * @snd_nxt: next sequence number to be used
  117. * @rcv_unacked: # messages read by user, but not yet acked back to peer
  118. * @deferdq: deferred receive queue
  119. * @window: sliding window size for congestion handling
  120. * @min_win: minimal send window to be used by link
  121. * @ssthresh: slow start threshold for congestion handling
  122. * @max_win: maximal send window to be used by link
  123. * @cong_acks: congestion acks for congestion avoidance (FIXME)
  124. * @checkpoint: seq number for congestion window size handling
  125. * @reasm_tnlmsg: fragmentation/reassembly area for tunnel protocol message
  126. * @last_gap: last gap ack blocks for bcast (FIXME)
  127. * @last_ga: ptr to gap ack blocks
  128. * @bc_rcvlink: the peer specific link used for broadcast reception
  129. * @bc_sndlink: the namespace global link used for broadcast sending
  130. * @nack_state: bcast nack state
  131. * @bc_peer_is_up: peer has acked the bcast init msg
  132. */
  133. struct tipc_link {
  134. u32 addr;
  135. char name[TIPC_MAX_LINK_NAME];
  136. struct net *net;
  137. /* Management and link supervision data */
  138. u16 peer_session;
  139. u16 session;
  140. u16 snd_nxt_state;
  141. u16 rcv_nxt_state;
  142. u32 peer_bearer_id;
  143. u32 bearer_id;
  144. u32 tolerance;
  145. u32 abort_limit;
  146. u32 state;
  147. u16 peer_caps;
  148. bool in_session;
  149. bool active;
  150. u32 silent_intv_cnt;
  151. char if_name[TIPC_MAX_IF_NAME];
  152. u32 priority;
  153. char net_plane;
  154. struct tipc_mon_state mon_state;
  155. u16 rst_cnt;
  156. /* Failover/synch */
  157. u16 drop_point;
  158. struct sk_buff *failover_reasm_skb;
  159. struct sk_buff_head failover_deferdq;
  160. /* Max packet negotiation */
  161. u16 mtu;
  162. u16 advertised_mtu;
  163. /* Sending */
  164. struct sk_buff_head transmq;
  165. struct sk_buff_head backlogq;
  166. struct {
  167. u16 len;
  168. u16 limit;
  169. struct sk_buff *target_bskb;
  170. } backlog[5];
  171. u16 snd_nxt;
  172. /* Reception */
  173. u16 rcv_nxt;
  174. u32 rcv_unacked;
  175. struct sk_buff_head deferdq;
  176. struct sk_buff_head *inputq;
  177. struct sk_buff_head *namedq;
  178. /* Congestion handling */
  179. struct sk_buff_head wakeupq;
  180. u16 window;
  181. u16 min_win;
  182. u16 ssthresh;
  183. u16 max_win;
  184. u16 cong_acks;
  185. u16 checkpoint;
  186. /* Fragmentation/reassembly */
  187. struct sk_buff *reasm_buf;
  188. struct sk_buff *reasm_tnlmsg;
  189. /* Broadcast */
  190. u16 ackers;
  191. u16 acked;
  192. u16 last_gap;
  193. struct tipc_gap_ack_blks *last_ga;
  194. struct tipc_link *bc_rcvlink;
  195. struct tipc_link *bc_sndlink;
  196. u8 nack_state;
  197. bool bc_peer_is_up;
  198. /* Statistics */
  199. struct tipc_stats stats;
  200. };
  201. /*
  202. * Error message prefixes
  203. */
  204. static const char *link_co_err = "Link tunneling error, ";
  205. static const char *link_rst_msg = "Resetting link ";
  206. /* Send states for broadcast NACKs
  207. */
  208. enum {
  209. BC_NACK_SND_CONDITIONAL,
  210. BC_NACK_SND_UNCONDITIONAL,
  211. BC_NACK_SND_SUPPRESS,
  212. };
  213. #define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
  214. #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
  215. /* Link FSM states:
  216. */
  217. enum {
  218. LINK_ESTABLISHED = 0xe,
  219. LINK_ESTABLISHING = 0xe << 4,
  220. LINK_RESET = 0x1 << 8,
  221. LINK_RESETTING = 0x2 << 12,
  222. LINK_PEER_RESET = 0xd << 16,
  223. LINK_FAILINGOVER = 0xf << 20,
  224. LINK_SYNCHING = 0xc << 24
  225. };
  226. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  227. struct sk_buff_head *xmitq);
  228. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  229. bool probe_reply, u16 rcvgap,
  230. int tolerance, int priority,
  231. struct sk_buff_head *xmitq);
  232. static void link_print(struct tipc_link *l, const char *str);
  233. static int tipc_link_build_nack_msg(struct tipc_link *l,
  234. struct sk_buff_head *xmitq);
  235. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  236. struct sk_buff_head *xmitq);
  237. static u8 __tipc_build_gap_ack_blks(struct tipc_gap_ack_blks *ga,
  238. struct tipc_link *l, u8 start_index);
  239. static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr);
  240. static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
  241. u16 acked, u16 gap,
  242. struct tipc_gap_ack_blks *ga,
  243. struct sk_buff_head *xmitq,
  244. bool *retransmitted, int *rc);
  245. static void tipc_link_update_cwin(struct tipc_link *l, int released,
  246. bool retransmitted);
  247. /*
  248. * Simple non-static link routines (i.e. referenced outside this file)
  249. */
  250. bool tipc_link_is_up(struct tipc_link *l)
  251. {
  252. return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
  253. }
  254. bool tipc_link_peer_is_down(struct tipc_link *l)
  255. {
  256. return l->state == LINK_PEER_RESET;
  257. }
  258. bool tipc_link_is_reset(struct tipc_link *l)
  259. {
  260. return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
  261. }
  262. bool tipc_link_is_establishing(struct tipc_link *l)
  263. {
  264. return l->state == LINK_ESTABLISHING;
  265. }
  266. bool tipc_link_is_synching(struct tipc_link *l)
  267. {
  268. return l->state == LINK_SYNCHING;
  269. }
  270. bool tipc_link_is_failingover(struct tipc_link *l)
  271. {
  272. return l->state == LINK_FAILINGOVER;
  273. }
  274. bool tipc_link_is_blocked(struct tipc_link *l)
  275. {
  276. return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
  277. }
  278. static bool link_is_bc_sndlink(struct tipc_link *l)
  279. {
  280. return !l->bc_sndlink;
  281. }
  282. static bool link_is_bc_rcvlink(struct tipc_link *l)
  283. {
  284. return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
  285. }
  286. void tipc_link_set_active(struct tipc_link *l, bool active)
  287. {
  288. l->active = active;
  289. }
  290. u32 tipc_link_id(struct tipc_link *l)
  291. {
  292. return l->peer_bearer_id << 16 | l->bearer_id;
  293. }
  294. int tipc_link_min_win(struct tipc_link *l)
  295. {
  296. return l->min_win;
  297. }
  298. int tipc_link_max_win(struct tipc_link *l)
  299. {
  300. return l->max_win;
  301. }
  302. int tipc_link_prio(struct tipc_link *l)
  303. {
  304. return l->priority;
  305. }
  306. unsigned long tipc_link_tolerance(struct tipc_link *l)
  307. {
  308. return l->tolerance;
  309. }
  310. struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
  311. {
  312. return l->inputq;
  313. }
  314. char tipc_link_plane(struct tipc_link *l)
  315. {
  316. return l->net_plane;
  317. }
  318. struct net *tipc_link_net(struct tipc_link *l)
  319. {
  320. return l->net;
  321. }
  322. void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
  323. {
  324. l->peer_caps = capabilities;
  325. }
  326. void tipc_link_add_bc_peer(struct tipc_link *snd_l,
  327. struct tipc_link *uc_l,
  328. struct sk_buff_head *xmitq)
  329. {
  330. struct tipc_link *rcv_l = uc_l->bc_rcvlink;
  331. snd_l->ackers++;
  332. rcv_l->acked = snd_l->snd_nxt - 1;
  333. snd_l->state = LINK_ESTABLISHED;
  334. tipc_link_build_bc_init_msg(uc_l, xmitq);
  335. }
  336. void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
  337. struct tipc_link *rcv_l,
  338. struct sk_buff_head *xmitq)
  339. {
  340. u16 ack = snd_l->snd_nxt - 1;
  341. snd_l->ackers--;
  342. rcv_l->bc_peer_is_up = true;
  343. rcv_l->state = LINK_ESTABLISHED;
  344. tipc_link_bc_ack_rcv(rcv_l, ack, 0, NULL, xmitq, NULL);
  345. trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
  346. tipc_link_reset(rcv_l);
  347. rcv_l->state = LINK_RESET;
  348. if (!snd_l->ackers) {
  349. trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
  350. tipc_link_reset(snd_l);
  351. snd_l->state = LINK_RESET;
  352. __skb_queue_purge(xmitq);
  353. }
  354. }
  355. int tipc_link_bc_peers(struct tipc_link *l)
  356. {
  357. return l->ackers;
  358. }
  359. static u16 link_bc_rcv_gap(struct tipc_link *l)
  360. {
  361. struct sk_buff *skb = skb_peek(&l->deferdq);
  362. u16 gap = 0;
  363. if (more(l->snd_nxt, l->rcv_nxt))
  364. gap = l->snd_nxt - l->rcv_nxt;
  365. if (skb)
  366. gap = buf_seqno(skb) - l->rcv_nxt;
  367. return gap;
  368. }
  369. void tipc_link_set_mtu(struct tipc_link *l, int mtu)
  370. {
  371. l->mtu = mtu;
  372. }
  373. int tipc_link_mtu(struct tipc_link *l)
  374. {
  375. return l->mtu;
  376. }
  377. int tipc_link_mss(struct tipc_link *l)
  378. {
  379. #ifdef CONFIG_TIPC_CRYPTO
  380. return l->mtu - INT_H_SIZE - EMSG_OVERHEAD;
  381. #else
  382. return l->mtu - INT_H_SIZE;
  383. #endif
  384. }
  385. u16 tipc_link_rcv_nxt(struct tipc_link *l)
  386. {
  387. return l->rcv_nxt;
  388. }
  389. u16 tipc_link_acked(struct tipc_link *l)
  390. {
  391. return l->acked;
  392. }
  393. char *tipc_link_name(struct tipc_link *l)
  394. {
  395. return l->name;
  396. }
  397. u32 tipc_link_state(struct tipc_link *l)
  398. {
  399. return l->state;
  400. }
  401. /**
  402. * tipc_link_create - create a new link
  403. * @net: pointer to associated network namespace
  404. * @if_name: associated interface name
  405. * @bearer_id: id (index) of associated bearer
  406. * @tolerance: link tolerance to be used by link
  407. * @net_plane: network plane (A,B,c..) this link belongs to
  408. * @mtu: mtu to be advertised by link
  409. * @priority: priority to be used by link
  410. * @min_win: minimal send window to be used by link
  411. * @max_win: maximal send window to be used by link
  412. * @session: session to be used by link
  413. * @peer: node id of peer node
  414. * @peer_caps: bitmap describing peer node capabilities
  415. * @bc_sndlink: the namespace global link used for broadcast sending
  416. * @bc_rcvlink: the peer specific link used for broadcast reception
  417. * @inputq: queue to put messages ready for delivery
  418. * @namedq: queue to put binding table update messages ready for delivery
  419. * @link: return value, pointer to put the created link
  420. * @self: local unicast link id
  421. * @peer_id: 128-bit ID of peer
  422. *
  423. * Return: true if link was created, otherwise false
  424. */
  425. bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
  426. int tolerance, char net_plane, u32 mtu, int priority,
  427. u32 min_win, u32 max_win, u32 session, u32 self,
  428. u32 peer, u8 *peer_id, u16 peer_caps,
  429. struct tipc_link *bc_sndlink,
  430. struct tipc_link *bc_rcvlink,
  431. struct sk_buff_head *inputq,
  432. struct sk_buff_head *namedq,
  433. struct tipc_link **link)
  434. {
  435. char peer_str[NODE_ID_STR_LEN] = {0,};
  436. char self_str[NODE_ID_STR_LEN] = {0,};
  437. struct tipc_link *l;
  438. l = kzalloc(sizeof(*l), GFP_ATOMIC);
  439. if (!l)
  440. return false;
  441. *link = l;
  442. l->session = session;
  443. /* Set link name for unicast links only */
  444. if (peer_id) {
  445. tipc_nodeid2string(self_str, tipc_own_id(net));
  446. if (strlen(self_str) > 16)
  447. sprintf(self_str, "%x", self);
  448. tipc_nodeid2string(peer_str, peer_id);
  449. if (strlen(peer_str) > 16)
  450. sprintf(peer_str, "%x", peer);
  451. }
  452. /* Peer i/f name will be completed by reset/activate message */
  453. snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
  454. self_str, if_name, peer_str);
  455. strcpy(l->if_name, if_name);
  456. l->addr = peer;
  457. l->peer_caps = peer_caps;
  458. l->net = net;
  459. l->in_session = false;
  460. l->bearer_id = bearer_id;
  461. l->tolerance = tolerance;
  462. if (bc_rcvlink)
  463. bc_rcvlink->tolerance = tolerance;
  464. l->net_plane = net_plane;
  465. l->advertised_mtu = mtu;
  466. l->mtu = mtu;
  467. l->priority = priority;
  468. tipc_link_set_queue_limits(l, min_win, max_win);
  469. l->ackers = 1;
  470. l->bc_sndlink = bc_sndlink;
  471. l->bc_rcvlink = bc_rcvlink;
  472. l->inputq = inputq;
  473. l->namedq = namedq;
  474. l->state = LINK_RESETTING;
  475. __skb_queue_head_init(&l->transmq);
  476. __skb_queue_head_init(&l->backlogq);
  477. __skb_queue_head_init(&l->deferdq);
  478. __skb_queue_head_init(&l->failover_deferdq);
  479. skb_queue_head_init(&l->wakeupq);
  480. skb_queue_head_init(l->inputq);
  481. return true;
  482. }
  483. /**
  484. * tipc_link_bc_create - create new link to be used for broadcast
  485. * @net: pointer to associated network namespace
  486. * @mtu: mtu to be used initially if no peers
  487. * @min_win: minimal send window to be used by link
  488. * @max_win: maximal send window to be used by link
  489. * @inputq: queue to put messages ready for delivery
  490. * @namedq: queue to put binding table update messages ready for delivery
  491. * @link: return value, pointer to put the created link
  492. * @ownnode: identity of own node
  493. * @peer: node id of peer node
  494. * @peer_id: 128-bit ID of peer
  495. * @peer_caps: bitmap describing peer node capabilities
  496. * @bc_sndlink: the namespace global link used for broadcast sending
  497. *
  498. * Return: true if link was created, otherwise false
  499. */
  500. bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer, u8 *peer_id,
  501. int mtu, u32 min_win, u32 max_win, u16 peer_caps,
  502. struct sk_buff_head *inputq,
  503. struct sk_buff_head *namedq,
  504. struct tipc_link *bc_sndlink,
  505. struct tipc_link **link)
  506. {
  507. struct tipc_link *l;
  508. if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, min_win,
  509. max_win, 0, ownnode, peer, NULL, peer_caps,
  510. bc_sndlink, NULL, inputq, namedq, link))
  511. return false;
  512. l = *link;
  513. if (peer_id) {
  514. char peer_str[NODE_ID_STR_LEN] = {0,};
  515. tipc_nodeid2string(peer_str, peer_id);
  516. if (strlen(peer_str) > 16)
  517. sprintf(peer_str, "%x", peer);
  518. /* Broadcast receiver link name: "broadcast-link:<peer>" */
  519. snprintf(l->name, sizeof(l->name), "%s:%s", tipc_bclink_name,
  520. peer_str);
  521. } else {
  522. strcpy(l->name, tipc_bclink_name);
  523. }
  524. trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
  525. tipc_link_reset(l);
  526. l->state = LINK_RESET;
  527. l->ackers = 0;
  528. l->bc_rcvlink = l;
  529. /* Broadcast send link is always up */
  530. if (link_is_bc_sndlink(l))
  531. l->state = LINK_ESTABLISHED;
  532. /* Disable replicast if even a single peer doesn't support it */
  533. if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
  534. tipc_bcast_toggle_rcast(net, false);
  535. return true;
  536. }
  537. /**
  538. * tipc_link_fsm_evt - link finite state machine
  539. * @l: pointer to link
  540. * @evt: state machine event to be processed
  541. */
  542. int tipc_link_fsm_evt(struct tipc_link *l, int evt)
  543. {
  544. int rc = 0;
  545. int old_state = l->state;
  546. switch (l->state) {
  547. case LINK_RESETTING:
  548. switch (evt) {
  549. case LINK_PEER_RESET_EVT:
  550. l->state = LINK_PEER_RESET;
  551. break;
  552. case LINK_RESET_EVT:
  553. l->state = LINK_RESET;
  554. break;
  555. case LINK_FAILURE_EVT:
  556. case LINK_FAILOVER_BEGIN_EVT:
  557. case LINK_ESTABLISH_EVT:
  558. case LINK_FAILOVER_END_EVT:
  559. case LINK_SYNCH_BEGIN_EVT:
  560. case LINK_SYNCH_END_EVT:
  561. default:
  562. goto illegal_evt;
  563. }
  564. break;
  565. case LINK_RESET:
  566. switch (evt) {
  567. case LINK_PEER_RESET_EVT:
  568. l->state = LINK_ESTABLISHING;
  569. break;
  570. case LINK_FAILOVER_BEGIN_EVT:
  571. l->state = LINK_FAILINGOVER;
  572. break;
  573. case LINK_FAILURE_EVT:
  574. case LINK_RESET_EVT:
  575. case LINK_ESTABLISH_EVT:
  576. case LINK_FAILOVER_END_EVT:
  577. break;
  578. case LINK_SYNCH_BEGIN_EVT:
  579. case LINK_SYNCH_END_EVT:
  580. default:
  581. goto illegal_evt;
  582. }
  583. break;
  584. case LINK_PEER_RESET:
  585. switch (evt) {
  586. case LINK_RESET_EVT:
  587. l->state = LINK_ESTABLISHING;
  588. break;
  589. case LINK_PEER_RESET_EVT:
  590. case LINK_ESTABLISH_EVT:
  591. case LINK_FAILURE_EVT:
  592. break;
  593. case LINK_SYNCH_BEGIN_EVT:
  594. case LINK_SYNCH_END_EVT:
  595. case LINK_FAILOVER_BEGIN_EVT:
  596. case LINK_FAILOVER_END_EVT:
  597. default:
  598. goto illegal_evt;
  599. }
  600. break;
  601. case LINK_FAILINGOVER:
  602. switch (evt) {
  603. case LINK_FAILOVER_END_EVT:
  604. l->state = LINK_RESET;
  605. break;
  606. case LINK_PEER_RESET_EVT:
  607. case LINK_RESET_EVT:
  608. case LINK_ESTABLISH_EVT:
  609. case LINK_FAILURE_EVT:
  610. break;
  611. case LINK_FAILOVER_BEGIN_EVT:
  612. case LINK_SYNCH_BEGIN_EVT:
  613. case LINK_SYNCH_END_EVT:
  614. default:
  615. goto illegal_evt;
  616. }
  617. break;
  618. case LINK_ESTABLISHING:
  619. switch (evt) {
  620. case LINK_ESTABLISH_EVT:
  621. l->state = LINK_ESTABLISHED;
  622. break;
  623. case LINK_FAILOVER_BEGIN_EVT:
  624. l->state = LINK_FAILINGOVER;
  625. break;
  626. case LINK_RESET_EVT:
  627. l->state = LINK_RESET;
  628. break;
  629. case LINK_FAILURE_EVT:
  630. case LINK_PEER_RESET_EVT:
  631. case LINK_SYNCH_BEGIN_EVT:
  632. case LINK_FAILOVER_END_EVT:
  633. break;
  634. case LINK_SYNCH_END_EVT:
  635. default:
  636. goto illegal_evt;
  637. }
  638. break;
  639. case LINK_ESTABLISHED:
  640. switch (evt) {
  641. case LINK_PEER_RESET_EVT:
  642. l->state = LINK_PEER_RESET;
  643. rc |= TIPC_LINK_DOWN_EVT;
  644. break;
  645. case LINK_FAILURE_EVT:
  646. l->state = LINK_RESETTING;
  647. rc |= TIPC_LINK_DOWN_EVT;
  648. break;
  649. case LINK_RESET_EVT:
  650. l->state = LINK_RESET;
  651. break;
  652. case LINK_ESTABLISH_EVT:
  653. case LINK_SYNCH_END_EVT:
  654. break;
  655. case LINK_SYNCH_BEGIN_EVT:
  656. l->state = LINK_SYNCHING;
  657. break;
  658. case LINK_FAILOVER_BEGIN_EVT:
  659. case LINK_FAILOVER_END_EVT:
  660. default:
  661. goto illegal_evt;
  662. }
  663. break;
  664. case LINK_SYNCHING:
  665. switch (evt) {
  666. case LINK_PEER_RESET_EVT:
  667. l->state = LINK_PEER_RESET;
  668. rc |= TIPC_LINK_DOWN_EVT;
  669. break;
  670. case LINK_FAILURE_EVT:
  671. l->state = LINK_RESETTING;
  672. rc |= TIPC_LINK_DOWN_EVT;
  673. break;
  674. case LINK_RESET_EVT:
  675. l->state = LINK_RESET;
  676. break;
  677. case LINK_ESTABLISH_EVT:
  678. case LINK_SYNCH_BEGIN_EVT:
  679. break;
  680. case LINK_SYNCH_END_EVT:
  681. l->state = LINK_ESTABLISHED;
  682. break;
  683. case LINK_FAILOVER_BEGIN_EVT:
  684. case LINK_FAILOVER_END_EVT:
  685. default:
  686. goto illegal_evt;
  687. }
  688. break;
  689. default:
  690. pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
  691. }
  692. trace_tipc_link_fsm(l->name, old_state, l->state, evt);
  693. return rc;
  694. illegal_evt:
  695. pr_err("Illegal FSM event %x in state %x on link %s\n",
  696. evt, l->state, l->name);
  697. trace_tipc_link_fsm(l->name, old_state, l->state, evt);
  698. return rc;
  699. }
  700. /* link_profile_stats - update statistical profiling of traffic
  701. */
  702. static void link_profile_stats(struct tipc_link *l)
  703. {
  704. struct sk_buff *skb;
  705. struct tipc_msg *msg;
  706. int length;
  707. /* Update counters used in statistical profiling of send traffic */
  708. l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
  709. l->stats.queue_sz_counts++;
  710. skb = skb_peek(&l->transmq);
  711. if (!skb)
  712. return;
  713. msg = buf_msg(skb);
  714. length = msg_size(msg);
  715. if (msg_user(msg) == MSG_FRAGMENTER) {
  716. if (msg_type(msg) != FIRST_FRAGMENT)
  717. return;
  718. length = msg_size(msg_inner_hdr(msg));
  719. }
  720. l->stats.msg_lengths_total += length;
  721. l->stats.msg_length_counts++;
  722. if (length <= 64)
  723. l->stats.msg_length_profile[0]++;
  724. else if (length <= 256)
  725. l->stats.msg_length_profile[1]++;
  726. else if (length <= 1024)
  727. l->stats.msg_length_profile[2]++;
  728. else if (length <= 4096)
  729. l->stats.msg_length_profile[3]++;
  730. else if (length <= 16384)
  731. l->stats.msg_length_profile[4]++;
  732. else if (length <= 32768)
  733. l->stats.msg_length_profile[5]++;
  734. else
  735. l->stats.msg_length_profile[6]++;
  736. }
  737. /**
  738. * tipc_link_too_silent - check if link is "too silent"
  739. * @l: tipc link to be checked
  740. *
  741. * Return: true if the link 'silent_intv_cnt' is about to reach the
  742. * 'abort_limit' value, otherwise false
  743. */
  744. bool tipc_link_too_silent(struct tipc_link *l)
  745. {
  746. return (l->silent_intv_cnt + 2 > l->abort_limit);
  747. }
  748. /* tipc_link_timeout - perform periodic task as instructed from node timeout
  749. */
  750. int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
  751. {
  752. int mtyp = 0;
  753. int rc = 0;
  754. bool state = false;
  755. bool probe = false;
  756. bool setup = false;
  757. u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
  758. u16 bc_acked = l->bc_rcvlink->acked;
  759. struct tipc_mon_state *mstate = &l->mon_state;
  760. trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
  761. trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
  762. switch (l->state) {
  763. case LINK_ESTABLISHED:
  764. case LINK_SYNCHING:
  765. mtyp = STATE_MSG;
  766. link_profile_stats(l);
  767. tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
  768. if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
  769. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  770. state = bc_acked != bc_snt;
  771. state |= l->bc_rcvlink->rcv_unacked;
  772. state |= l->rcv_unacked;
  773. state |= !skb_queue_empty(&l->transmq);
  774. probe = mstate->probing;
  775. probe |= l->silent_intv_cnt;
  776. if (probe || mstate->monitoring)
  777. l->silent_intv_cnt++;
  778. probe |= !skb_queue_empty(&l->deferdq);
  779. if (l->snd_nxt == l->checkpoint) {
  780. tipc_link_update_cwin(l, 0, 0);
  781. probe = true;
  782. }
  783. l->checkpoint = l->snd_nxt;
  784. break;
  785. case LINK_RESET:
  786. setup = l->rst_cnt++ <= 4;
  787. setup |= !(l->rst_cnt % 16);
  788. mtyp = RESET_MSG;
  789. break;
  790. case LINK_ESTABLISHING:
  791. setup = true;
  792. mtyp = ACTIVATE_MSG;
  793. break;
  794. case LINK_PEER_RESET:
  795. case LINK_RESETTING:
  796. case LINK_FAILINGOVER:
  797. break;
  798. default:
  799. break;
  800. }
  801. if (state || probe || setup)
  802. tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
  803. return rc;
  804. }
  805. /**
  806. * link_schedule_user - schedule a message sender for wakeup after congestion
  807. * @l: congested link
  808. * @hdr: header of message that is being sent
  809. * Create pseudo msg to send back to user when congestion abates
  810. */
  811. static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
  812. {
  813. u32 dnode = tipc_own_addr(l->net);
  814. u32 dport = msg_origport(hdr);
  815. struct sk_buff *skb;
  816. /* Create and schedule wakeup pseudo message */
  817. skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
  818. dnode, l->addr, dport, 0, 0);
  819. if (!skb)
  820. return -ENOBUFS;
  821. msg_set_dest_droppable(buf_msg(skb), true);
  822. TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
  823. skb_queue_tail(&l->wakeupq, skb);
  824. l->stats.link_congs++;
  825. trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
  826. return -ELINKCONG;
  827. }
  828. /**
  829. * link_prepare_wakeup - prepare users for wakeup after congestion
  830. * @l: congested link
  831. * Wake up a number of waiting users, as permitted by available space
  832. * in the send queue
  833. */
  834. static void link_prepare_wakeup(struct tipc_link *l)
  835. {
  836. struct sk_buff_head *wakeupq = &l->wakeupq;
  837. struct sk_buff_head *inputq = l->inputq;
  838. struct sk_buff *skb, *tmp;
  839. struct sk_buff_head tmpq;
  840. int avail[5] = {0,};
  841. int imp = 0;
  842. __skb_queue_head_init(&tmpq);
  843. for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
  844. avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
  845. skb_queue_walk_safe(wakeupq, skb, tmp) {
  846. imp = TIPC_SKB_CB(skb)->chain_imp;
  847. if (avail[imp] <= 0)
  848. continue;
  849. avail[imp]--;
  850. __skb_unlink(skb, wakeupq);
  851. __skb_queue_tail(&tmpq, skb);
  852. }
  853. spin_lock_bh(&inputq->lock);
  854. skb_queue_splice_tail(&tmpq, inputq);
  855. spin_unlock_bh(&inputq->lock);
  856. }
  857. /**
  858. * tipc_link_set_skb_retransmit_time - set the time at which retransmission of
  859. * the given skb should be next attempted
  860. * @skb: skb to set a future retransmission time for
  861. * @l: link the skb will be transmitted on
  862. */
  863. static void tipc_link_set_skb_retransmit_time(struct sk_buff *skb,
  864. struct tipc_link *l)
  865. {
  866. if (link_is_bc_sndlink(l))
  867. TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
  868. else
  869. TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
  870. }
  871. void tipc_link_reset(struct tipc_link *l)
  872. {
  873. struct sk_buff_head list;
  874. u32 imp;
  875. __skb_queue_head_init(&list);
  876. l->in_session = false;
  877. /* Force re-synch of peer session number before establishing */
  878. l->peer_session--;
  879. l->session++;
  880. l->mtu = l->advertised_mtu;
  881. spin_lock_bh(&l->wakeupq.lock);
  882. skb_queue_splice_init(&l->wakeupq, &list);
  883. spin_unlock_bh(&l->wakeupq.lock);
  884. spin_lock_bh(&l->inputq->lock);
  885. skb_queue_splice_init(&list, l->inputq);
  886. spin_unlock_bh(&l->inputq->lock);
  887. __skb_queue_purge(&l->transmq);
  888. __skb_queue_purge(&l->deferdq);
  889. __skb_queue_purge(&l->backlogq);
  890. __skb_queue_purge(&l->failover_deferdq);
  891. for (imp = 0; imp <= TIPC_SYSTEM_IMPORTANCE; imp++) {
  892. l->backlog[imp].len = 0;
  893. l->backlog[imp].target_bskb = NULL;
  894. }
  895. kfree_skb(l->reasm_buf);
  896. kfree_skb(l->reasm_tnlmsg);
  897. kfree_skb(l->failover_reasm_skb);
  898. l->reasm_buf = NULL;
  899. l->reasm_tnlmsg = NULL;
  900. l->failover_reasm_skb = NULL;
  901. l->rcv_unacked = 0;
  902. l->snd_nxt = 1;
  903. l->rcv_nxt = 1;
  904. l->snd_nxt_state = 1;
  905. l->rcv_nxt_state = 1;
  906. l->acked = 0;
  907. l->last_gap = 0;
  908. kfree(l->last_ga);
  909. l->last_ga = NULL;
  910. l->silent_intv_cnt = 0;
  911. l->rst_cnt = 0;
  912. l->bc_peer_is_up = false;
  913. memset(&l->mon_state, 0, sizeof(l->mon_state));
  914. tipc_link_reset_stats(l);
  915. }
  916. /**
  917. * tipc_link_xmit(): enqueue buffer list according to queue situation
  918. * @l: link to use
  919. * @list: chain of buffers containing message
  920. * @xmitq: returned list of packets to be sent by caller
  921. *
  922. * Consumes the buffer chain.
  923. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  924. * Return: 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  925. */
  926. int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
  927. struct sk_buff_head *xmitq)
  928. {
  929. struct sk_buff_head *backlogq = &l->backlogq;
  930. struct sk_buff_head *transmq = &l->transmq;
  931. struct sk_buff *skb, *_skb;
  932. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  933. u16 ack = l->rcv_nxt - 1;
  934. u16 seqno = l->snd_nxt;
  935. int pkt_cnt = skb_queue_len(list);
  936. unsigned int mss = tipc_link_mss(l);
  937. unsigned int cwin = l->window;
  938. unsigned int mtu = l->mtu;
  939. struct tipc_msg *hdr;
  940. bool new_bundle;
  941. int rc = 0;
  942. int imp;
  943. if (pkt_cnt <= 0)
  944. return 0;
  945. hdr = buf_msg(skb_peek(list));
  946. if (unlikely(msg_size(hdr) > mtu)) {
  947. pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
  948. skb_queue_len(list), msg_user(hdr),
  949. msg_type(hdr), msg_size(hdr), mtu);
  950. __skb_queue_purge(list);
  951. return -EMSGSIZE;
  952. }
  953. imp = msg_importance(hdr);
  954. /* Allow oversubscription of one data msg per source at congestion */
  955. if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
  956. if (imp == TIPC_SYSTEM_IMPORTANCE) {
  957. pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
  958. return -ENOBUFS;
  959. }
  960. rc = link_schedule_user(l, hdr);
  961. }
  962. if (pkt_cnt > 1) {
  963. l->stats.sent_fragmented++;
  964. l->stats.sent_fragments += pkt_cnt;
  965. }
  966. /* Prepare each packet for sending, and add to relevant queue: */
  967. while ((skb = __skb_dequeue(list))) {
  968. if (likely(skb_queue_len(transmq) < cwin)) {
  969. hdr = buf_msg(skb);
  970. msg_set_seqno(hdr, seqno);
  971. msg_set_ack(hdr, ack);
  972. msg_set_bcast_ack(hdr, bc_ack);
  973. _skb = skb_clone(skb, GFP_ATOMIC);
  974. if (!_skb) {
  975. kfree_skb(skb);
  976. __skb_queue_purge(list);
  977. return -ENOBUFS;
  978. }
  979. __skb_queue_tail(transmq, skb);
  980. tipc_link_set_skb_retransmit_time(skb, l);
  981. __skb_queue_tail(xmitq, _skb);
  982. TIPC_SKB_CB(skb)->ackers = l->ackers;
  983. l->rcv_unacked = 0;
  984. l->stats.sent_pkts++;
  985. seqno++;
  986. continue;
  987. }
  988. if (tipc_msg_try_bundle(l->backlog[imp].target_bskb, &skb,
  989. mss, l->addr, &new_bundle)) {
  990. if (skb) {
  991. /* Keep a ref. to the skb for next try */
  992. l->backlog[imp].target_bskb = skb;
  993. l->backlog[imp].len++;
  994. __skb_queue_tail(backlogq, skb);
  995. } else {
  996. if (new_bundle) {
  997. l->stats.sent_bundles++;
  998. l->stats.sent_bundled++;
  999. }
  1000. l->stats.sent_bundled++;
  1001. }
  1002. continue;
  1003. }
  1004. l->backlog[imp].target_bskb = NULL;
  1005. l->backlog[imp].len += (1 + skb_queue_len(list));
  1006. __skb_queue_tail(backlogq, skb);
  1007. skb_queue_splice_tail_init(list, backlogq);
  1008. }
  1009. l->snd_nxt = seqno;
  1010. return rc;
  1011. }
  1012. static void tipc_link_update_cwin(struct tipc_link *l, int released,
  1013. bool retransmitted)
  1014. {
  1015. int bklog_len = skb_queue_len(&l->backlogq);
  1016. struct sk_buff_head *txq = &l->transmq;
  1017. int txq_len = skb_queue_len(txq);
  1018. u16 cwin = l->window;
  1019. /* Enter fast recovery */
  1020. if (unlikely(retransmitted)) {
  1021. l->ssthresh = max_t(u16, l->window / 2, 300);
  1022. l->window = min_t(u16, l->ssthresh, l->window);
  1023. return;
  1024. }
  1025. /* Enter slow start */
  1026. if (unlikely(!released)) {
  1027. l->ssthresh = max_t(u16, l->window / 2, 300);
  1028. l->window = l->min_win;
  1029. return;
  1030. }
  1031. /* Don't increase window if no pressure on the transmit queue */
  1032. if (txq_len + bklog_len < cwin)
  1033. return;
  1034. /* Don't increase window if there are holes the transmit queue */
  1035. if (txq_len && l->snd_nxt - buf_seqno(skb_peek(txq)) != txq_len)
  1036. return;
  1037. l->cong_acks += released;
  1038. /* Slow start */
  1039. if (cwin <= l->ssthresh) {
  1040. l->window = min_t(u16, cwin + released, l->max_win);
  1041. return;
  1042. }
  1043. /* Congestion avoidance */
  1044. if (l->cong_acks < cwin)
  1045. return;
  1046. l->window = min_t(u16, ++cwin, l->max_win);
  1047. l->cong_acks = 0;
  1048. }
  1049. static void tipc_link_advance_backlog(struct tipc_link *l,
  1050. struct sk_buff_head *xmitq)
  1051. {
  1052. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  1053. struct sk_buff_head *txq = &l->transmq;
  1054. struct sk_buff *skb, *_skb;
  1055. u16 ack = l->rcv_nxt - 1;
  1056. u16 seqno = l->snd_nxt;
  1057. struct tipc_msg *hdr;
  1058. u16 cwin = l->window;
  1059. u32 imp;
  1060. while (skb_queue_len(txq) < cwin) {
  1061. skb = skb_peek(&l->backlogq);
  1062. if (!skb)
  1063. break;
  1064. _skb = skb_clone(skb, GFP_ATOMIC);
  1065. if (!_skb)
  1066. break;
  1067. __skb_dequeue(&l->backlogq);
  1068. hdr = buf_msg(skb);
  1069. imp = msg_importance(hdr);
  1070. l->backlog[imp].len--;
  1071. if (unlikely(skb == l->backlog[imp].target_bskb))
  1072. l->backlog[imp].target_bskb = NULL;
  1073. __skb_queue_tail(&l->transmq, skb);
  1074. tipc_link_set_skb_retransmit_time(skb, l);
  1075. __skb_queue_tail(xmitq, _skb);
  1076. TIPC_SKB_CB(skb)->ackers = l->ackers;
  1077. msg_set_seqno(hdr, seqno);
  1078. msg_set_ack(hdr, ack);
  1079. msg_set_bcast_ack(hdr, bc_ack);
  1080. l->rcv_unacked = 0;
  1081. l->stats.sent_pkts++;
  1082. seqno++;
  1083. }
  1084. l->snd_nxt = seqno;
  1085. }
  1086. /**
  1087. * link_retransmit_failure() - Detect repeated retransmit failures
  1088. * @l: tipc link sender
  1089. * @r: tipc link receiver (= l in case of unicast)
  1090. * @rc: returned code
  1091. *
  1092. * Return: true if the repeated retransmit failures happens, otherwise
  1093. * false
  1094. */
  1095. static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
  1096. int *rc)
  1097. {
  1098. struct sk_buff *skb = skb_peek(&l->transmq);
  1099. struct tipc_msg *hdr;
  1100. if (!skb)
  1101. return false;
  1102. if (!TIPC_SKB_CB(skb)->retr_cnt)
  1103. return false;
  1104. if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
  1105. msecs_to_jiffies(r->tolerance * 10)))
  1106. return false;
  1107. hdr = buf_msg(skb);
  1108. if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
  1109. return false;
  1110. pr_warn("Retransmission failure on link <%s>\n", l->name);
  1111. link_print(l, "State of link ");
  1112. pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
  1113. msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
  1114. pr_info("sqno %u, prev: %x, dest: %x\n",
  1115. msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
  1116. pr_info("retr_stamp %d, retr_cnt %d\n",
  1117. jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
  1118. TIPC_SKB_CB(skb)->retr_cnt);
  1119. trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
  1120. trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
  1121. trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
  1122. if (link_is_bc_sndlink(l)) {
  1123. r->state = LINK_RESET;
  1124. *rc |= TIPC_LINK_DOWN_EVT;
  1125. } else {
  1126. *rc |= tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1127. }
  1128. return true;
  1129. }
  1130. /* tipc_data_input - deliver data and name distr msgs to upper layer
  1131. *
  1132. * Consumes buffer if message is of right type
  1133. * Node lock must be held
  1134. */
  1135. static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
  1136. struct sk_buff_head *inputq)
  1137. {
  1138. struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
  1139. struct tipc_msg *hdr = buf_msg(skb);
  1140. switch (msg_user(hdr)) {
  1141. case TIPC_LOW_IMPORTANCE:
  1142. case TIPC_MEDIUM_IMPORTANCE:
  1143. case TIPC_HIGH_IMPORTANCE:
  1144. case TIPC_CRITICAL_IMPORTANCE:
  1145. if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
  1146. skb_queue_tail(mc_inputq, skb);
  1147. return true;
  1148. }
  1149. fallthrough;
  1150. case CONN_MANAGER:
  1151. skb_queue_tail(inputq, skb);
  1152. return true;
  1153. case GROUP_PROTOCOL:
  1154. skb_queue_tail(mc_inputq, skb);
  1155. return true;
  1156. case NAME_DISTRIBUTOR:
  1157. l->bc_rcvlink->state = LINK_ESTABLISHED;
  1158. skb_queue_tail(l->namedq, skb);
  1159. return true;
  1160. case MSG_BUNDLER:
  1161. case TUNNEL_PROTOCOL:
  1162. case MSG_FRAGMENTER:
  1163. case BCAST_PROTOCOL:
  1164. return false;
  1165. #ifdef CONFIG_TIPC_CRYPTO
  1166. case MSG_CRYPTO:
  1167. if (sysctl_tipc_key_exchange_enabled &&
  1168. TIPC_SKB_CB(skb)->decrypted) {
  1169. tipc_crypto_msg_rcv(l->net, skb);
  1170. return true;
  1171. }
  1172. fallthrough;
  1173. #endif
  1174. default:
  1175. pr_warn("Dropping received illegal msg type\n");
  1176. kfree_skb(skb);
  1177. return true;
  1178. }
  1179. }
  1180. /* tipc_link_input - process packet that has passed link protocol check
  1181. *
  1182. * Consumes buffer
  1183. */
  1184. static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
  1185. struct sk_buff_head *inputq,
  1186. struct sk_buff **reasm_skb)
  1187. {
  1188. struct tipc_msg *hdr = buf_msg(skb);
  1189. struct sk_buff *iskb;
  1190. struct sk_buff_head tmpq;
  1191. int usr = msg_user(hdr);
  1192. int pos = 0;
  1193. if (usr == MSG_BUNDLER) {
  1194. skb_queue_head_init(&tmpq);
  1195. l->stats.recv_bundles++;
  1196. l->stats.recv_bundled += msg_msgcnt(hdr);
  1197. while (tipc_msg_extract(skb, &iskb, &pos))
  1198. tipc_data_input(l, iskb, &tmpq);
  1199. tipc_skb_queue_splice_tail(&tmpq, inputq);
  1200. return 0;
  1201. } else if (usr == MSG_FRAGMENTER) {
  1202. l->stats.recv_fragments++;
  1203. if (tipc_buf_append(reasm_skb, &skb)) {
  1204. l->stats.recv_fragmented++;
  1205. tipc_data_input(l, skb, inputq);
  1206. } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
  1207. pr_warn_ratelimited("Unable to build fragment list\n");
  1208. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1209. }
  1210. return 0;
  1211. } else if (usr == BCAST_PROTOCOL) {
  1212. tipc_bcast_lock(l->net);
  1213. tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
  1214. tipc_bcast_unlock(l->net);
  1215. }
  1216. kfree_skb(skb);
  1217. return 0;
  1218. }
  1219. /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
  1220. * inner message along with the ones in the old link's
  1221. * deferdq
  1222. * @l: tunnel link
  1223. * @skb: TUNNEL_PROTOCOL message
  1224. * @inputq: queue to put messages ready for delivery
  1225. */
  1226. static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
  1227. struct sk_buff_head *inputq)
  1228. {
  1229. struct sk_buff **reasm_skb = &l->failover_reasm_skb;
  1230. struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
  1231. struct sk_buff_head *fdefq = &l->failover_deferdq;
  1232. struct tipc_msg *hdr = buf_msg(skb);
  1233. struct sk_buff *iskb;
  1234. int ipos = 0;
  1235. int rc = 0;
  1236. u16 seqno;
  1237. if (msg_type(hdr) == SYNCH_MSG) {
  1238. kfree_skb(skb);
  1239. return 0;
  1240. }
  1241. /* Not a fragment? */
  1242. if (likely(!msg_nof_fragms(hdr))) {
  1243. if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
  1244. pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
  1245. skb_queue_len(fdefq));
  1246. return 0;
  1247. }
  1248. kfree_skb(skb);
  1249. } else {
  1250. /* Set fragment type for buf_append */
  1251. if (msg_fragm_no(hdr) == 1)
  1252. msg_set_type(hdr, FIRST_FRAGMENT);
  1253. else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
  1254. msg_set_type(hdr, FRAGMENT);
  1255. else
  1256. msg_set_type(hdr, LAST_FRAGMENT);
  1257. if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
  1258. /* Successful but non-complete reassembly? */
  1259. if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
  1260. return 0;
  1261. pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
  1262. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1263. }
  1264. iskb = skb;
  1265. }
  1266. do {
  1267. seqno = buf_seqno(iskb);
  1268. if (unlikely(less(seqno, l->drop_point))) {
  1269. kfree_skb(iskb);
  1270. continue;
  1271. }
  1272. if (unlikely(seqno != l->drop_point)) {
  1273. __tipc_skb_queue_sorted(fdefq, seqno, iskb);
  1274. continue;
  1275. }
  1276. l->drop_point++;
  1277. if (!tipc_data_input(l, iskb, inputq))
  1278. rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
  1279. if (unlikely(rc))
  1280. break;
  1281. } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
  1282. return rc;
  1283. }
  1284. /**
  1285. * tipc_get_gap_ack_blks - get Gap ACK blocks from PROTOCOL/STATE_MSG
  1286. * @ga: returned pointer to the Gap ACK blocks if any
  1287. * @l: the tipc link
  1288. * @hdr: the PROTOCOL/STATE_MSG header
  1289. * @uc: desired Gap ACK blocks type, i.e. unicast (= 1) or broadcast (= 0)
  1290. *
  1291. * Return: the total Gap ACK blocks size
  1292. */
  1293. u16 tipc_get_gap_ack_blks(struct tipc_gap_ack_blks **ga, struct tipc_link *l,
  1294. struct tipc_msg *hdr, bool uc)
  1295. {
  1296. struct tipc_gap_ack_blks *p;
  1297. u16 sz = 0;
  1298. /* Does peer support the Gap ACK blocks feature? */
  1299. if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
  1300. p = (struct tipc_gap_ack_blks *)msg_data(hdr);
  1301. sz = ntohs(p->len);
  1302. /* Sanity check */
  1303. if (sz == struct_size(p, gacks, size_add(p->ugack_cnt, p->bgack_cnt))) {
  1304. /* Good, check if the desired type exists */
  1305. if ((uc && p->ugack_cnt) || (!uc && p->bgack_cnt))
  1306. goto ok;
  1307. /* Backward compatible: peer might not support bc, but uc? */
  1308. } else if (uc && sz == struct_size(p, gacks, p->ugack_cnt)) {
  1309. if (p->ugack_cnt) {
  1310. p->bgack_cnt = 0;
  1311. goto ok;
  1312. }
  1313. }
  1314. }
  1315. /* Other cases: ignore! */
  1316. p = NULL;
  1317. ok:
  1318. *ga = p;
  1319. return sz;
  1320. }
  1321. static u8 __tipc_build_gap_ack_blks(struct tipc_gap_ack_blks *ga,
  1322. struct tipc_link *l, u8 start_index)
  1323. {
  1324. struct tipc_gap_ack *gacks = &ga->gacks[start_index];
  1325. struct sk_buff *skb = skb_peek(&l->deferdq);
  1326. u16 expect, seqno = 0;
  1327. u8 n = 0;
  1328. if (!skb)
  1329. return 0;
  1330. expect = buf_seqno(skb);
  1331. skb_queue_walk(&l->deferdq, skb) {
  1332. seqno = buf_seqno(skb);
  1333. if (unlikely(more(seqno, expect))) {
  1334. gacks[n].ack = htons(expect - 1);
  1335. gacks[n].gap = htons(seqno - expect);
  1336. if (++n >= MAX_GAP_ACK_BLKS / 2) {
  1337. pr_info_ratelimited("Gacks on %s: %d, ql: %d!\n",
  1338. l->name, n,
  1339. skb_queue_len(&l->deferdq));
  1340. return n;
  1341. }
  1342. } else if (unlikely(less(seqno, expect))) {
  1343. pr_warn("Unexpected skb in deferdq!\n");
  1344. continue;
  1345. }
  1346. expect = seqno + 1;
  1347. }
  1348. /* last block */
  1349. gacks[n].ack = htons(seqno);
  1350. gacks[n].gap = 0;
  1351. n++;
  1352. return n;
  1353. }
  1354. /* tipc_build_gap_ack_blks - build Gap ACK blocks
  1355. * @l: tipc unicast link
  1356. * @hdr: the tipc message buffer to store the Gap ACK blocks after built
  1357. *
  1358. * The function builds Gap ACK blocks for both the unicast & broadcast receiver
  1359. * links of a certain peer, the buffer after built has the network data format
  1360. * as found at the struct tipc_gap_ack_blks definition.
  1361. *
  1362. * returns the actual allocated memory size
  1363. */
  1364. static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr)
  1365. {
  1366. struct tipc_link *bcl = l->bc_rcvlink;
  1367. struct tipc_gap_ack_blks *ga;
  1368. u16 len;
  1369. ga = (struct tipc_gap_ack_blks *)msg_data(hdr);
  1370. /* Start with broadcast link first */
  1371. tipc_bcast_lock(bcl->net);
  1372. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1373. msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
  1374. ga->bgack_cnt = __tipc_build_gap_ack_blks(ga, bcl, 0);
  1375. tipc_bcast_unlock(bcl->net);
  1376. /* Now for unicast link, but an explicit NACK only (???) */
  1377. ga->ugack_cnt = (msg_seq_gap(hdr)) ?
  1378. __tipc_build_gap_ack_blks(ga, l, ga->bgack_cnt) : 0;
  1379. /* Total len */
  1380. len = struct_size(ga, gacks, size_add(ga->bgack_cnt, ga->ugack_cnt));
  1381. ga->len = htons(len);
  1382. return len;
  1383. }
  1384. /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
  1385. * acked packets, also doing retransmissions if
  1386. * gaps found
  1387. * @l: tipc link with transmq queue to be advanced
  1388. * @r: tipc link "receiver" i.e. in case of broadcast (= "l" if unicast)
  1389. * @acked: seqno of last packet acked by peer without any gaps before
  1390. * @gap: # of gap packets
  1391. * @ga: buffer pointer to Gap ACK blocks from peer
  1392. * @xmitq: queue for accumulating the retransmitted packets if any
  1393. * @retransmitted: returned boolean value if a retransmission is really issued
  1394. * @rc: returned code e.g. TIPC_LINK_DOWN_EVT if a repeated retransmit failures
  1395. * happens (- unlikely case)
  1396. *
  1397. * Return: the number of packets released from the link transmq
  1398. */
  1399. static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
  1400. u16 acked, u16 gap,
  1401. struct tipc_gap_ack_blks *ga,
  1402. struct sk_buff_head *xmitq,
  1403. bool *retransmitted, int *rc)
  1404. {
  1405. struct tipc_gap_ack_blks *last_ga = r->last_ga, *this_ga = NULL;
  1406. struct tipc_gap_ack *gacks = NULL;
  1407. struct sk_buff *skb, *_skb, *tmp;
  1408. struct tipc_msg *hdr;
  1409. u32 qlen = skb_queue_len(&l->transmq);
  1410. u16 nacked = acked, ngap = gap, gack_cnt = 0;
  1411. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  1412. u16 ack = l->rcv_nxt - 1;
  1413. u16 seqno, n = 0;
  1414. u16 end = r->acked, start = end, offset = r->last_gap;
  1415. u16 si = (last_ga) ? last_ga->start_index : 0;
  1416. bool is_uc = !link_is_bc_sndlink(l);
  1417. bool bc_has_acked = false;
  1418. trace_tipc_link_retrans(r, acked + 1, acked + gap, &l->transmq);
  1419. /* Determine Gap ACK blocks if any for the particular link */
  1420. if (ga && is_uc) {
  1421. /* Get the Gap ACKs, uc part */
  1422. gack_cnt = ga->ugack_cnt;
  1423. gacks = &ga->gacks[ga->bgack_cnt];
  1424. } else if (ga) {
  1425. /* Copy the Gap ACKs, bc part, for later renewal if needed */
  1426. this_ga = kmemdup(ga, struct_size(ga, gacks, ga->bgack_cnt),
  1427. GFP_ATOMIC);
  1428. if (likely(this_ga)) {
  1429. this_ga->start_index = 0;
  1430. /* Start with the bc Gap ACKs */
  1431. gack_cnt = this_ga->bgack_cnt;
  1432. gacks = &this_ga->gacks[0];
  1433. } else {
  1434. /* Hmm, we can get in trouble..., simply ignore it */
  1435. pr_warn_ratelimited("Ignoring bc Gap ACKs, no memory\n");
  1436. }
  1437. }
  1438. /* Advance the link transmq */
  1439. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  1440. seqno = buf_seqno(skb);
  1441. next_gap_ack:
  1442. if (less_eq(seqno, nacked)) {
  1443. if (is_uc)
  1444. goto release;
  1445. /* Skip packets peer has already acked */
  1446. if (!more(seqno, r->acked))
  1447. continue;
  1448. /* Get the next of last Gap ACK blocks */
  1449. while (more(seqno, end)) {
  1450. if (!last_ga || si >= last_ga->bgack_cnt)
  1451. break;
  1452. start = end + offset + 1;
  1453. end = ntohs(last_ga->gacks[si].ack);
  1454. offset = ntohs(last_ga->gacks[si].gap);
  1455. si++;
  1456. WARN_ONCE(more(start, end) ||
  1457. (!offset &&
  1458. si < last_ga->bgack_cnt) ||
  1459. si > MAX_GAP_ACK_BLKS,
  1460. "Corrupted Gap ACK: %d %d %d %d %d\n",
  1461. start, end, offset, si,
  1462. last_ga->bgack_cnt);
  1463. }
  1464. /* Check against the last Gap ACK block */
  1465. if (tipc_in_range(seqno, start, end))
  1466. continue;
  1467. /* Update/release the packet peer is acking */
  1468. bc_has_acked = true;
  1469. if (--TIPC_SKB_CB(skb)->ackers)
  1470. continue;
  1471. release:
  1472. /* release skb */
  1473. __skb_unlink(skb, &l->transmq);
  1474. kfree_skb(skb);
  1475. } else if (less_eq(seqno, nacked + ngap)) {
  1476. /* First gap: check if repeated retrans failures? */
  1477. if (unlikely(seqno == acked + 1 &&
  1478. link_retransmit_failure(l, r, rc))) {
  1479. /* Ignore this bc Gap ACKs if any */
  1480. kfree(this_ga);
  1481. this_ga = NULL;
  1482. break;
  1483. }
  1484. /* retransmit skb if unrestricted*/
  1485. if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
  1486. continue;
  1487. tipc_link_set_skb_retransmit_time(skb, l);
  1488. _skb = pskb_copy(skb, GFP_ATOMIC);
  1489. if (!_skb)
  1490. continue;
  1491. hdr = buf_msg(_skb);
  1492. msg_set_ack(hdr, ack);
  1493. msg_set_bcast_ack(hdr, bc_ack);
  1494. _skb->priority = TC_PRIO_CONTROL;
  1495. __skb_queue_tail(xmitq, _skb);
  1496. l->stats.retransmitted++;
  1497. if (!is_uc)
  1498. r->stats.retransmitted++;
  1499. *retransmitted = true;
  1500. /* Increase actual retrans counter & mark first time */
  1501. if (!TIPC_SKB_CB(skb)->retr_cnt++)
  1502. TIPC_SKB_CB(skb)->retr_stamp = jiffies;
  1503. } else {
  1504. /* retry with Gap ACK blocks if any */
  1505. if (n >= gack_cnt)
  1506. break;
  1507. nacked = ntohs(gacks[n].ack);
  1508. ngap = ntohs(gacks[n].gap);
  1509. n++;
  1510. goto next_gap_ack;
  1511. }
  1512. }
  1513. /* Renew last Gap ACK blocks for bc if needed */
  1514. if (bc_has_acked) {
  1515. if (this_ga) {
  1516. kfree(last_ga);
  1517. r->last_ga = this_ga;
  1518. r->last_gap = gap;
  1519. } else if (last_ga) {
  1520. if (less(acked, start)) {
  1521. si--;
  1522. offset = start - acked - 1;
  1523. } else if (less(acked, end)) {
  1524. acked = end;
  1525. }
  1526. if (si < last_ga->bgack_cnt) {
  1527. last_ga->start_index = si;
  1528. r->last_gap = offset;
  1529. } else {
  1530. kfree(last_ga);
  1531. r->last_ga = NULL;
  1532. r->last_gap = 0;
  1533. }
  1534. } else {
  1535. r->last_gap = 0;
  1536. }
  1537. r->acked = acked;
  1538. } else {
  1539. kfree(this_ga);
  1540. }
  1541. return qlen - skb_queue_len(&l->transmq);
  1542. }
  1543. /* tipc_link_build_state_msg: prepare link state message for transmission
  1544. *
  1545. * Note that sending of broadcast ack is coordinated among nodes, to reduce
  1546. * risk of ack storms towards the sender
  1547. */
  1548. int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1549. {
  1550. if (!l)
  1551. return 0;
  1552. /* Broadcast ACK must be sent via a unicast link => defer to caller */
  1553. if (link_is_bc_rcvlink(l)) {
  1554. if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
  1555. return 0;
  1556. l->rcv_unacked = 0;
  1557. /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
  1558. l->snd_nxt = l->rcv_nxt;
  1559. return TIPC_LINK_SND_STATE;
  1560. }
  1561. /* Unicast ACK */
  1562. l->rcv_unacked = 0;
  1563. l->stats.sent_acks++;
  1564. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
  1565. return 0;
  1566. }
  1567. /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
  1568. */
  1569. void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1570. {
  1571. int mtyp = RESET_MSG;
  1572. struct sk_buff *skb;
  1573. if (l->state == LINK_ESTABLISHING)
  1574. mtyp = ACTIVATE_MSG;
  1575. tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
  1576. /* Inform peer that this endpoint is going down if applicable */
  1577. skb = skb_peek_tail(xmitq);
  1578. if (skb && (l->state == LINK_RESET))
  1579. msg_set_peer_stopping(buf_msg(skb), 1);
  1580. }
  1581. /* tipc_link_build_nack_msg: prepare link nack message for transmission
  1582. * Note that sending of broadcast NACK is coordinated among nodes, to
  1583. * reduce the risk of NACK storms towards the sender
  1584. */
  1585. static int tipc_link_build_nack_msg(struct tipc_link *l,
  1586. struct sk_buff_head *xmitq)
  1587. {
  1588. u32 def_cnt = ++l->stats.deferred_recv;
  1589. struct sk_buff_head *dfq = &l->deferdq;
  1590. u32 defq_len = skb_queue_len(dfq);
  1591. int match1, match2;
  1592. if (link_is_bc_rcvlink(l)) {
  1593. match1 = def_cnt & 0xf;
  1594. match2 = tipc_own_addr(l->net) & 0xf;
  1595. if (match1 == match2)
  1596. return TIPC_LINK_SND_STATE;
  1597. return 0;
  1598. }
  1599. if (defq_len >= 3 && !((defq_len - 3) % 16)) {
  1600. u16 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1601. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0,
  1602. rcvgap, 0, 0, xmitq);
  1603. }
  1604. return 0;
  1605. }
  1606. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  1607. * @l: the link that should handle the message
  1608. * @skb: TIPC packet
  1609. * @xmitq: queue to place packets to be sent after this call
  1610. */
  1611. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  1612. struct sk_buff_head *xmitq)
  1613. {
  1614. struct sk_buff_head *defq = &l->deferdq;
  1615. struct tipc_msg *hdr = buf_msg(skb);
  1616. u16 seqno, rcv_nxt, win_lim;
  1617. int released = 0;
  1618. int rc = 0;
  1619. /* Verify and update link state */
  1620. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  1621. return tipc_link_proto_rcv(l, skb, xmitq);
  1622. /* Don't send probe at next timeout expiration */
  1623. l->silent_intv_cnt = 0;
  1624. do {
  1625. hdr = buf_msg(skb);
  1626. seqno = msg_seqno(hdr);
  1627. rcv_nxt = l->rcv_nxt;
  1628. win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
  1629. if (unlikely(!tipc_link_is_up(l))) {
  1630. if (l->state == LINK_ESTABLISHING)
  1631. rc = TIPC_LINK_UP_EVT;
  1632. kfree_skb(skb);
  1633. break;
  1634. }
  1635. /* Drop if outside receive window */
  1636. if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
  1637. l->stats.duplicates++;
  1638. kfree_skb(skb);
  1639. break;
  1640. }
  1641. released += tipc_link_advance_transmq(l, l, msg_ack(hdr), 0,
  1642. NULL, NULL, NULL, NULL);
  1643. /* Defer delivery if sequence gap */
  1644. if (unlikely(seqno != rcv_nxt)) {
  1645. if (!__tipc_skb_queue_sorted(defq, seqno, skb))
  1646. l->stats.duplicates++;
  1647. rc |= tipc_link_build_nack_msg(l, xmitq);
  1648. break;
  1649. }
  1650. /* Deliver packet */
  1651. l->rcv_nxt++;
  1652. l->stats.recv_pkts++;
  1653. if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
  1654. rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
  1655. else if (!tipc_data_input(l, skb, l->inputq))
  1656. rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
  1657. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
  1658. rc |= tipc_link_build_state_msg(l, xmitq);
  1659. if (unlikely(rc & ~TIPC_LINK_SND_STATE))
  1660. break;
  1661. } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
  1662. /* Forward queues and wake up waiting users */
  1663. if (released) {
  1664. tipc_link_update_cwin(l, released, 0);
  1665. tipc_link_advance_backlog(l, xmitq);
  1666. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1667. link_prepare_wakeup(l);
  1668. }
  1669. return rc;
  1670. }
  1671. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1672. bool probe_reply, u16 rcvgap,
  1673. int tolerance, int priority,
  1674. struct sk_buff_head *xmitq)
  1675. {
  1676. struct tipc_mon_state *mstate = &l->mon_state;
  1677. struct sk_buff_head *dfq = &l->deferdq;
  1678. struct tipc_link *bcl = l->bc_rcvlink;
  1679. struct tipc_msg *hdr;
  1680. struct sk_buff *skb;
  1681. bool node_up = tipc_link_is_up(bcl);
  1682. u16 glen = 0, bc_rcvgap = 0;
  1683. int dlen = 0;
  1684. void *data;
  1685. /* Don't send protocol message during reset or link failover */
  1686. if (tipc_link_is_blocked(l))
  1687. return;
  1688. if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
  1689. return;
  1690. if ((probe || probe_reply) && !skb_queue_empty(dfq))
  1691. rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1692. skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
  1693. tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
  1694. l->addr, tipc_own_addr(l->net), 0, 0, 0);
  1695. if (!skb)
  1696. return;
  1697. hdr = buf_msg(skb);
  1698. data = msg_data(hdr);
  1699. msg_set_session(hdr, l->session);
  1700. msg_set_bearer_id(hdr, l->bearer_id);
  1701. msg_set_net_plane(hdr, l->net_plane);
  1702. msg_set_next_sent(hdr, l->snd_nxt);
  1703. msg_set_ack(hdr, l->rcv_nxt - 1);
  1704. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1705. msg_set_bc_ack_invalid(hdr, !node_up);
  1706. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1707. msg_set_link_tolerance(hdr, tolerance);
  1708. msg_set_linkprio(hdr, priority);
  1709. msg_set_redundant_link(hdr, node_up);
  1710. msg_set_seq_gap(hdr, 0);
  1711. msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
  1712. if (mtyp == STATE_MSG) {
  1713. if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
  1714. msg_set_seqno(hdr, l->snd_nxt_state++);
  1715. msg_set_seq_gap(hdr, rcvgap);
  1716. bc_rcvgap = link_bc_rcv_gap(bcl);
  1717. msg_set_bc_gap(hdr, bc_rcvgap);
  1718. msg_set_probe(hdr, probe);
  1719. msg_set_is_keepalive(hdr, probe || probe_reply);
  1720. if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
  1721. glen = tipc_build_gap_ack_blks(l, hdr);
  1722. tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
  1723. msg_set_size(hdr, INT_H_SIZE + glen + dlen);
  1724. skb_trim(skb, INT_H_SIZE + glen + dlen);
  1725. l->stats.sent_states++;
  1726. l->rcv_unacked = 0;
  1727. } else {
  1728. /* RESET_MSG or ACTIVATE_MSG */
  1729. if (mtyp == ACTIVATE_MSG) {
  1730. msg_set_dest_session_valid(hdr, 1);
  1731. msg_set_dest_session(hdr, l->peer_session);
  1732. }
  1733. msg_set_max_pkt(hdr, l->advertised_mtu);
  1734. strcpy(data, l->if_name);
  1735. msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1736. skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1737. }
  1738. if (probe)
  1739. l->stats.sent_probes++;
  1740. if (rcvgap)
  1741. l->stats.sent_nacks++;
  1742. if (bc_rcvgap)
  1743. bcl->stats.sent_nacks++;
  1744. skb->priority = TC_PRIO_CONTROL;
  1745. __skb_queue_tail(xmitq, skb);
  1746. trace_tipc_proto_build(skb, false, l->name);
  1747. }
  1748. void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
  1749. struct sk_buff_head *xmitq)
  1750. {
  1751. u32 onode = tipc_own_addr(l->net);
  1752. struct tipc_msg *hdr, *ihdr;
  1753. struct sk_buff_head tnlq;
  1754. struct sk_buff *skb;
  1755. u32 dnode = l->addr;
  1756. __skb_queue_head_init(&tnlq);
  1757. skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
  1758. INT_H_SIZE, BASIC_H_SIZE,
  1759. dnode, onode, 0, 0, 0);
  1760. if (!skb) {
  1761. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1762. return;
  1763. }
  1764. hdr = buf_msg(skb);
  1765. msg_set_msgcnt(hdr, 1);
  1766. msg_set_bearer_id(hdr, l->peer_bearer_id);
  1767. ihdr = (struct tipc_msg *)msg_data(hdr);
  1768. tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1769. BASIC_H_SIZE, dnode);
  1770. msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
  1771. __skb_queue_tail(&tnlq, skb);
  1772. tipc_link_xmit(l, &tnlq, xmitq);
  1773. }
  1774. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1775. * with contents of the link's transmit and backlog queues.
  1776. */
  1777. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1778. int mtyp, struct sk_buff_head *xmitq)
  1779. {
  1780. struct sk_buff_head *fdefq = &tnl->failover_deferdq;
  1781. struct sk_buff *skb, *tnlskb;
  1782. struct tipc_msg *hdr, tnlhdr;
  1783. struct sk_buff_head *queue = &l->transmq;
  1784. struct sk_buff_head tmpxq, tnlq, frags;
  1785. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1786. bool pktcnt_need_update = false;
  1787. u16 syncpt;
  1788. int rc;
  1789. if (!tnl)
  1790. return;
  1791. __skb_queue_head_init(&tnlq);
  1792. /* Link Synching:
  1793. * From now on, send only one single ("dummy") SYNCH message
  1794. * to peer. The SYNCH message does not contain any data, just
  1795. * a header conveying the synch point to the peer.
  1796. */
  1797. if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
  1798. tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
  1799. INT_H_SIZE, 0, l->addr,
  1800. tipc_own_addr(l->net),
  1801. 0, 0, 0);
  1802. if (!tnlskb) {
  1803. pr_warn("%sunable to create dummy SYNCH_MSG\n",
  1804. link_co_err);
  1805. return;
  1806. }
  1807. hdr = buf_msg(tnlskb);
  1808. syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
  1809. msg_set_syncpt(hdr, syncpt);
  1810. msg_set_bearer_id(hdr, l->peer_bearer_id);
  1811. __skb_queue_tail(&tnlq, tnlskb);
  1812. tipc_link_xmit(tnl, &tnlq, xmitq);
  1813. return;
  1814. }
  1815. __skb_queue_head_init(&tmpxq);
  1816. __skb_queue_head_init(&frags);
  1817. /* At least one packet required for safe algorithm => add dummy */
  1818. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1819. BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
  1820. 0, 0, TIPC_ERR_NO_PORT);
  1821. if (!skb) {
  1822. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1823. return;
  1824. }
  1825. __skb_queue_tail(&tnlq, skb);
  1826. tipc_link_xmit(l, &tnlq, &tmpxq);
  1827. __skb_queue_purge(&tmpxq);
  1828. /* Initialize reusable tunnel packet header */
  1829. tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
  1830. mtyp, INT_H_SIZE, l->addr);
  1831. if (mtyp == SYNCH_MSG)
  1832. pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
  1833. else
  1834. pktcnt = skb_queue_len(&l->transmq);
  1835. pktcnt += skb_queue_len(&l->backlogq);
  1836. msg_set_msgcnt(&tnlhdr, pktcnt);
  1837. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1838. tnl:
  1839. /* Wrap each packet into a tunnel packet */
  1840. skb_queue_walk(queue, skb) {
  1841. hdr = buf_msg(skb);
  1842. if (queue == &l->backlogq)
  1843. msg_set_seqno(hdr, seqno++);
  1844. pktlen = msg_size(hdr);
  1845. /* Tunnel link MTU is not large enough? This could be
  1846. * due to:
  1847. * 1) Link MTU has just changed or set differently;
  1848. * 2) Or FAILOVER on the top of a SYNCH message
  1849. *
  1850. * The 2nd case should not happen if peer supports
  1851. * TIPC_TUNNEL_ENHANCED
  1852. */
  1853. if (pktlen > tnl->mtu - INT_H_SIZE) {
  1854. if (mtyp == FAILOVER_MSG &&
  1855. (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
  1856. rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
  1857. &frags);
  1858. if (rc) {
  1859. pr_warn("%sunable to frag msg: rc %d\n",
  1860. link_co_err, rc);
  1861. return;
  1862. }
  1863. pktcnt += skb_queue_len(&frags) - 1;
  1864. pktcnt_need_update = true;
  1865. skb_queue_splice_tail_init(&frags, &tnlq);
  1866. continue;
  1867. }
  1868. /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
  1869. * => Just warn it and return!
  1870. */
  1871. pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
  1872. link_co_err, msg_user(hdr),
  1873. msg_type(hdr), msg_size(hdr));
  1874. return;
  1875. }
  1876. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1877. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
  1878. if (!tnlskb) {
  1879. pr_warn("%sunable to send packet\n", link_co_err);
  1880. return;
  1881. }
  1882. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1883. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1884. __skb_queue_tail(&tnlq, tnlskb);
  1885. }
  1886. if (queue != &l->backlogq) {
  1887. queue = &l->backlogq;
  1888. goto tnl;
  1889. }
  1890. if (pktcnt_need_update)
  1891. skb_queue_walk(&tnlq, skb) {
  1892. hdr = buf_msg(skb);
  1893. msg_set_msgcnt(hdr, pktcnt);
  1894. }
  1895. tipc_link_xmit(tnl, &tnlq, xmitq);
  1896. if (mtyp == FAILOVER_MSG) {
  1897. tnl->drop_point = l->rcv_nxt;
  1898. tnl->failover_reasm_skb = l->reasm_buf;
  1899. l->reasm_buf = NULL;
  1900. /* Failover the link's deferdq */
  1901. if (unlikely(!skb_queue_empty(fdefq))) {
  1902. pr_warn("Link failover deferdq not empty: %d!\n",
  1903. skb_queue_len(fdefq));
  1904. __skb_queue_purge(fdefq);
  1905. }
  1906. skb_queue_splice_init(&l->deferdq, fdefq);
  1907. }
  1908. }
  1909. /**
  1910. * tipc_link_failover_prepare() - prepare tnl for link failover
  1911. *
  1912. * This is a special version of the precursor - tipc_link_tnl_prepare(),
  1913. * see the tipc_node_link_failover() for details
  1914. *
  1915. * @l: failover link
  1916. * @tnl: tunnel link
  1917. * @xmitq: queue for messages to be xmited
  1918. */
  1919. void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1920. struct sk_buff_head *xmitq)
  1921. {
  1922. struct sk_buff_head *fdefq = &tnl->failover_deferdq;
  1923. tipc_link_create_dummy_tnl_msg(tnl, xmitq);
  1924. /* This failover link endpoint was never established before,
  1925. * so it has not received anything from peer.
  1926. * Otherwise, it must be a normal failover situation or the
  1927. * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
  1928. * would have to start over from scratch instead.
  1929. */
  1930. tnl->drop_point = 1;
  1931. tnl->failover_reasm_skb = NULL;
  1932. /* Initiate the link's failover deferdq */
  1933. if (unlikely(!skb_queue_empty(fdefq))) {
  1934. pr_warn("Link failover deferdq not empty: %d!\n",
  1935. skb_queue_len(fdefq));
  1936. __skb_queue_purge(fdefq);
  1937. }
  1938. }
  1939. /* tipc_link_validate_msg(): validate message against current link state
  1940. * Returns true if message should be accepted, otherwise false
  1941. */
  1942. bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
  1943. {
  1944. u16 curr_session = l->peer_session;
  1945. u16 session = msg_session(hdr);
  1946. int mtyp = msg_type(hdr);
  1947. if (msg_user(hdr) != LINK_PROTOCOL)
  1948. return true;
  1949. switch (mtyp) {
  1950. case RESET_MSG:
  1951. if (!l->in_session)
  1952. return true;
  1953. /* Accept only RESET with new session number */
  1954. return more(session, curr_session);
  1955. case ACTIVATE_MSG:
  1956. if (!l->in_session)
  1957. return true;
  1958. /* Accept only ACTIVATE with new or current session number */
  1959. return !less(session, curr_session);
  1960. case STATE_MSG:
  1961. /* Accept only STATE with current session number */
  1962. if (!l->in_session)
  1963. return false;
  1964. if (session != curr_session)
  1965. return false;
  1966. /* Extra sanity check */
  1967. if (!tipc_link_is_up(l) && msg_ack(hdr))
  1968. return false;
  1969. if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
  1970. return true;
  1971. /* Accept only STATE with new sequence number */
  1972. return !less(msg_seqno(hdr), l->rcv_nxt_state);
  1973. default:
  1974. return false;
  1975. }
  1976. }
  1977. /* tipc_link_proto_rcv(): receive link level protocol message :
  1978. * Note that network plane id propagates through the network, and may
  1979. * change at any time. The node with lowest numerical id determines
  1980. * network plane
  1981. */
  1982. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1983. struct sk_buff_head *xmitq)
  1984. {
  1985. struct tipc_msg *hdr = buf_msg(skb);
  1986. struct tipc_gap_ack_blks *ga = NULL;
  1987. bool reply = msg_probe(hdr), retransmitted = false;
  1988. u32 dlen = msg_data_sz(hdr), glen = 0, msg_max;
  1989. u16 peers_snd_nxt = msg_next_sent(hdr);
  1990. u16 peers_tol = msg_link_tolerance(hdr);
  1991. u16 peers_prio = msg_linkprio(hdr);
  1992. u16 gap = msg_seq_gap(hdr);
  1993. u16 ack = msg_ack(hdr);
  1994. u16 rcv_nxt = l->rcv_nxt;
  1995. u16 rcvgap = 0;
  1996. int mtyp = msg_type(hdr);
  1997. int rc = 0, released;
  1998. char *if_name;
  1999. void *data;
  2000. trace_tipc_proto_rcv(skb, false, l->name);
  2001. if (dlen > U16_MAX)
  2002. goto exit;
  2003. if (tipc_link_is_blocked(l) || !xmitq)
  2004. goto exit;
  2005. if (tipc_own_addr(l->net) > msg_prevnode(hdr))
  2006. l->net_plane = msg_net_plane(hdr);
  2007. if (skb_linearize(skb))
  2008. goto exit;
  2009. hdr = buf_msg(skb);
  2010. data = msg_data(hdr);
  2011. if (!tipc_link_validate_msg(l, hdr)) {
  2012. trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
  2013. trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
  2014. goto exit;
  2015. }
  2016. switch (mtyp) {
  2017. case RESET_MSG:
  2018. case ACTIVATE_MSG:
  2019. msg_max = msg_max_pkt(hdr);
  2020. if (msg_max < tipc_bearer_min_mtu(l->net, l->bearer_id))
  2021. break;
  2022. /* Complete own link name with peer's interface name */
  2023. if_name = strrchr(l->name, ':') + 1;
  2024. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  2025. break;
  2026. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  2027. break;
  2028. strncpy(if_name, data, TIPC_MAX_IF_NAME);
  2029. /* Update own tolerance if peer indicates a non-zero value */
  2030. if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  2031. l->tolerance = peers_tol;
  2032. l->bc_rcvlink->tolerance = peers_tol;
  2033. }
  2034. /* Update own priority if peer's priority is higher */
  2035. if (tipc_in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  2036. l->priority = peers_prio;
  2037. /* If peer is going down we want full re-establish cycle */
  2038. if (msg_peer_stopping(hdr)) {
  2039. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  2040. break;
  2041. }
  2042. /* If this endpoint was re-created while peer was ESTABLISHING
  2043. * it doesn't know current session number. Force re-synch.
  2044. */
  2045. if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
  2046. l->session != msg_dest_session(hdr)) {
  2047. if (less(l->session, msg_dest_session(hdr)))
  2048. l->session = msg_dest_session(hdr) + 1;
  2049. break;
  2050. }
  2051. /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
  2052. if (mtyp == RESET_MSG || !tipc_link_is_up(l))
  2053. rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  2054. /* ACTIVATE_MSG takes up link if it was already locally reset */
  2055. if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
  2056. rc = TIPC_LINK_UP_EVT;
  2057. l->peer_session = msg_session(hdr);
  2058. l->in_session = true;
  2059. l->peer_bearer_id = msg_bearer_id(hdr);
  2060. if (l->mtu > msg_max)
  2061. l->mtu = msg_max;
  2062. break;
  2063. case STATE_MSG:
  2064. /* Validate Gap ACK blocks, drop if invalid */
  2065. glen = tipc_get_gap_ack_blks(&ga, l, hdr, true);
  2066. if (glen > dlen)
  2067. break;
  2068. l->rcv_nxt_state = msg_seqno(hdr) + 1;
  2069. /* Update own tolerance if peer indicates a non-zero value */
  2070. if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  2071. l->tolerance = peers_tol;
  2072. l->bc_rcvlink->tolerance = peers_tol;
  2073. }
  2074. /* Update own prio if peer indicates a different value */
  2075. if ((peers_prio != l->priority) &&
  2076. tipc_in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
  2077. l->priority = peers_prio;
  2078. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  2079. }
  2080. l->silent_intv_cnt = 0;
  2081. l->stats.recv_states++;
  2082. if (msg_probe(hdr))
  2083. l->stats.recv_probes++;
  2084. if (!tipc_link_is_up(l)) {
  2085. if (l->state == LINK_ESTABLISHING)
  2086. rc = TIPC_LINK_UP_EVT;
  2087. break;
  2088. }
  2089. tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
  2090. &l->mon_state, l->bearer_id);
  2091. /* Send NACK if peer has sent pkts we haven't received yet */
  2092. if ((reply || msg_is_keepalive(hdr)) &&
  2093. more(peers_snd_nxt, rcv_nxt) &&
  2094. !tipc_link_is_synching(l) &&
  2095. skb_queue_empty(&l->deferdq))
  2096. rcvgap = peers_snd_nxt - l->rcv_nxt;
  2097. if (rcvgap || reply)
  2098. tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
  2099. rcvgap, 0, 0, xmitq);
  2100. released = tipc_link_advance_transmq(l, l, ack, gap, ga, xmitq,
  2101. &retransmitted, &rc);
  2102. if (gap)
  2103. l->stats.recv_nacks++;
  2104. if (released || retransmitted)
  2105. tipc_link_update_cwin(l, released, retransmitted);
  2106. if (released)
  2107. tipc_link_advance_backlog(l, xmitq);
  2108. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  2109. link_prepare_wakeup(l);
  2110. }
  2111. exit:
  2112. kfree_skb(skb);
  2113. return rc;
  2114. }
  2115. /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
  2116. */
  2117. static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
  2118. u16 peers_snd_nxt,
  2119. struct sk_buff_head *xmitq)
  2120. {
  2121. struct sk_buff *skb;
  2122. struct tipc_msg *hdr;
  2123. struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
  2124. u16 ack = l->rcv_nxt - 1;
  2125. u16 gap_to = peers_snd_nxt - 1;
  2126. skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
  2127. 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
  2128. if (!skb)
  2129. return false;
  2130. hdr = buf_msg(skb);
  2131. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  2132. msg_set_bcast_ack(hdr, ack);
  2133. msg_set_bcgap_after(hdr, ack);
  2134. if (dfrd_skb)
  2135. gap_to = buf_seqno(dfrd_skb) - 1;
  2136. msg_set_bcgap_to(hdr, gap_to);
  2137. msg_set_non_seq(hdr, bcast);
  2138. __skb_queue_tail(xmitq, skb);
  2139. return true;
  2140. }
  2141. /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
  2142. *
  2143. * Give a newly added peer node the sequence number where it should
  2144. * start receiving and acking broadcast packets.
  2145. */
  2146. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  2147. struct sk_buff_head *xmitq)
  2148. {
  2149. struct sk_buff_head list;
  2150. __skb_queue_head_init(&list);
  2151. if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
  2152. return;
  2153. msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
  2154. tipc_link_xmit(l, &list, xmitq);
  2155. }
  2156. /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
  2157. */
  2158. void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
  2159. {
  2160. int mtyp = msg_type(hdr);
  2161. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  2162. if (tipc_link_is_up(l))
  2163. return;
  2164. if (msg_user(hdr) == BCAST_PROTOCOL) {
  2165. l->rcv_nxt = peers_snd_nxt;
  2166. l->state = LINK_ESTABLISHED;
  2167. return;
  2168. }
  2169. if (l->peer_caps & TIPC_BCAST_SYNCH)
  2170. return;
  2171. if (msg_peer_node_is_up(hdr))
  2172. return;
  2173. /* Compatibility: accept older, less safe initial synch data */
  2174. if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
  2175. l->rcv_nxt = peers_snd_nxt;
  2176. }
  2177. /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
  2178. */
  2179. int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
  2180. struct sk_buff_head *xmitq)
  2181. {
  2182. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  2183. int rc = 0;
  2184. if (!tipc_link_is_up(l))
  2185. return rc;
  2186. if (!msg_peer_node_is_up(hdr))
  2187. return rc;
  2188. /* Open when peer acknowledges our bcast init msg (pkt #1) */
  2189. if (msg_ack(hdr))
  2190. l->bc_peer_is_up = true;
  2191. if (!l->bc_peer_is_up)
  2192. return rc;
  2193. /* Ignore if peers_snd_nxt goes beyond receive window */
  2194. if (more(peers_snd_nxt, l->rcv_nxt + l->window))
  2195. return rc;
  2196. l->snd_nxt = peers_snd_nxt;
  2197. if (link_bc_rcv_gap(l))
  2198. rc |= TIPC_LINK_SND_STATE;
  2199. /* Return now if sender supports nack via STATE messages */
  2200. if (l->peer_caps & TIPC_BCAST_STATE_NACK)
  2201. return rc;
  2202. /* Otherwise, be backwards compatible */
  2203. if (!more(peers_snd_nxt, l->rcv_nxt)) {
  2204. l->nack_state = BC_NACK_SND_CONDITIONAL;
  2205. return 0;
  2206. }
  2207. /* Don't NACK if one was recently sent or peeked */
  2208. if (l->nack_state == BC_NACK_SND_SUPPRESS) {
  2209. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  2210. return 0;
  2211. }
  2212. /* Conditionally delay NACK sending until next synch rcv */
  2213. if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
  2214. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  2215. if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
  2216. return 0;
  2217. }
  2218. /* Send NACK now but suppress next one */
  2219. tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
  2220. l->nack_state = BC_NACK_SND_SUPPRESS;
  2221. return 0;
  2222. }
  2223. int tipc_link_bc_ack_rcv(struct tipc_link *r, u16 acked, u16 gap,
  2224. struct tipc_gap_ack_blks *ga,
  2225. struct sk_buff_head *xmitq,
  2226. struct sk_buff_head *retrq)
  2227. {
  2228. struct tipc_link *l = r->bc_sndlink;
  2229. bool unused = false;
  2230. int rc = 0;
  2231. if (!tipc_link_is_up(r) || !r->bc_peer_is_up)
  2232. return 0;
  2233. if (gap) {
  2234. l->stats.recv_nacks++;
  2235. r->stats.recv_nacks++;
  2236. }
  2237. if (less(acked, r->acked) || (acked == r->acked && !gap && !ga))
  2238. return 0;
  2239. trace_tipc_link_bc_ack(r, acked, gap, &l->transmq);
  2240. tipc_link_advance_transmq(l, r, acked, gap, ga, retrq, &unused, &rc);
  2241. tipc_link_advance_backlog(l, xmitq);
  2242. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  2243. link_prepare_wakeup(l);
  2244. return rc;
  2245. }
  2246. /* tipc_link_bc_nack_rcv(): receive broadcast nack message
  2247. * This function is here for backwards compatibility, since
  2248. * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
  2249. */
  2250. int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
  2251. struct sk_buff_head *xmitq)
  2252. {
  2253. struct tipc_msg *hdr = buf_msg(skb);
  2254. u32 dnode = msg_destnode(hdr);
  2255. int mtyp = msg_type(hdr);
  2256. u16 acked = msg_bcast_ack(hdr);
  2257. u16 from = acked + 1;
  2258. u16 to = msg_bcgap_to(hdr);
  2259. u16 peers_snd_nxt = to + 1;
  2260. int rc = 0;
  2261. kfree_skb(skb);
  2262. if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
  2263. return 0;
  2264. if (mtyp != STATE_MSG)
  2265. return 0;
  2266. if (dnode == tipc_own_addr(l->net)) {
  2267. rc = tipc_link_bc_ack_rcv(l, acked, to - acked, NULL, xmitq,
  2268. xmitq);
  2269. l->stats.recv_nacks++;
  2270. return rc;
  2271. }
  2272. /* Msg for other node => suppress own NACK at next sync if applicable */
  2273. if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
  2274. l->nack_state = BC_NACK_SND_SUPPRESS;
  2275. return 0;
  2276. }
  2277. void tipc_link_set_queue_limits(struct tipc_link *l, u32 min_win, u32 max_win)
  2278. {
  2279. int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
  2280. l->min_win = min_win;
  2281. l->ssthresh = max_win;
  2282. l->max_win = max_win;
  2283. l->window = min_win;
  2284. l->backlog[TIPC_LOW_IMPORTANCE].limit = min_win * 2;
  2285. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = min_win * 4;
  2286. l->backlog[TIPC_HIGH_IMPORTANCE].limit = min_win * 6;
  2287. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = min_win * 8;
  2288. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  2289. }
  2290. /**
  2291. * tipc_link_reset_stats - reset link statistics
  2292. * @l: pointer to link
  2293. */
  2294. void tipc_link_reset_stats(struct tipc_link *l)
  2295. {
  2296. memset(&l->stats, 0, sizeof(l->stats));
  2297. }
  2298. static void link_print(struct tipc_link *l, const char *str)
  2299. {
  2300. struct sk_buff *hskb = skb_peek(&l->transmq);
  2301. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
  2302. u16 tail = l->snd_nxt - 1;
  2303. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  2304. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  2305. skb_queue_len(&l->transmq), head, tail,
  2306. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  2307. }
  2308. /* Parse and validate nested (link) properties valid for media, bearer and link
  2309. */
  2310. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  2311. {
  2312. int err;
  2313. err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
  2314. tipc_nl_prop_policy, NULL);
  2315. if (err)
  2316. return err;
  2317. if (props[TIPC_NLA_PROP_PRIO]) {
  2318. u32 prio;
  2319. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2320. if (prio > TIPC_MAX_LINK_PRI)
  2321. return -EINVAL;
  2322. }
  2323. if (props[TIPC_NLA_PROP_TOL]) {
  2324. u32 tol;
  2325. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2326. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  2327. return -EINVAL;
  2328. }
  2329. if (props[TIPC_NLA_PROP_WIN]) {
  2330. u32 max_win;
  2331. max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2332. if (max_win < TIPC_DEF_LINK_WIN || max_win > TIPC_MAX_LINK_WIN)
  2333. return -EINVAL;
  2334. }
  2335. return 0;
  2336. }
  2337. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  2338. {
  2339. int i;
  2340. struct nlattr *stats;
  2341. struct nla_map {
  2342. u32 key;
  2343. u32 val;
  2344. };
  2345. struct nla_map map[] = {
  2346. {TIPC_NLA_STATS_RX_INFO, 0},
  2347. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  2348. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  2349. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  2350. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  2351. {TIPC_NLA_STATS_TX_INFO, 0},
  2352. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  2353. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  2354. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  2355. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  2356. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  2357. s->msg_length_counts : 1},
  2358. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  2359. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  2360. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  2361. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  2362. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  2363. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  2364. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  2365. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  2366. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  2367. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  2368. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  2369. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  2370. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  2371. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  2372. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  2373. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  2374. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  2375. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  2376. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  2377. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  2378. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  2379. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  2380. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  2381. };
  2382. stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
  2383. if (!stats)
  2384. return -EMSGSIZE;
  2385. for (i = 0; i < ARRAY_SIZE(map); i++)
  2386. if (nla_put_u32(skb, map[i].key, map[i].val))
  2387. goto msg_full;
  2388. nla_nest_end(skb, stats);
  2389. return 0;
  2390. msg_full:
  2391. nla_nest_cancel(skb, stats);
  2392. return -EMSGSIZE;
  2393. }
  2394. /* Caller should hold appropriate locks to protect the link */
  2395. int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  2396. struct tipc_link *link, int nlflags)
  2397. {
  2398. u32 self = tipc_own_addr(net);
  2399. struct nlattr *attrs;
  2400. struct nlattr *prop;
  2401. void *hdr;
  2402. int err;
  2403. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  2404. nlflags, TIPC_NL_LINK_GET);
  2405. if (!hdr)
  2406. return -EMSGSIZE;
  2407. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
  2408. if (!attrs)
  2409. goto msg_full;
  2410. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  2411. goto attr_msg_full;
  2412. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
  2413. goto attr_msg_full;
  2414. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  2415. goto attr_msg_full;
  2416. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
  2417. goto attr_msg_full;
  2418. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
  2419. goto attr_msg_full;
  2420. if (tipc_link_is_up(link))
  2421. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2422. goto attr_msg_full;
  2423. if (link->active)
  2424. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  2425. goto attr_msg_full;
  2426. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
  2427. if (!prop)
  2428. goto attr_msg_full;
  2429. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2430. goto prop_msg_full;
  2431. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  2432. goto prop_msg_full;
  2433. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  2434. link->window))
  2435. goto prop_msg_full;
  2436. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2437. goto prop_msg_full;
  2438. nla_nest_end(msg->skb, prop);
  2439. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  2440. if (err)
  2441. goto attr_msg_full;
  2442. nla_nest_end(msg->skb, attrs);
  2443. genlmsg_end(msg->skb, hdr);
  2444. return 0;
  2445. prop_msg_full:
  2446. nla_nest_cancel(msg->skb, prop);
  2447. attr_msg_full:
  2448. nla_nest_cancel(msg->skb, attrs);
  2449. msg_full:
  2450. genlmsg_cancel(msg->skb, hdr);
  2451. return -EMSGSIZE;
  2452. }
  2453. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  2454. struct tipc_stats *stats)
  2455. {
  2456. int i;
  2457. struct nlattr *nest;
  2458. struct nla_map {
  2459. __u32 key;
  2460. __u32 val;
  2461. };
  2462. struct nla_map map[] = {
  2463. {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
  2464. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  2465. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  2466. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  2467. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  2468. {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
  2469. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  2470. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  2471. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  2472. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  2473. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  2474. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  2475. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  2476. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  2477. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  2478. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  2479. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  2480. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  2481. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  2482. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  2483. };
  2484. nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
  2485. if (!nest)
  2486. return -EMSGSIZE;
  2487. for (i = 0; i < ARRAY_SIZE(map); i++)
  2488. if (nla_put_u32(skb, map[i].key, map[i].val))
  2489. goto msg_full;
  2490. nla_nest_end(skb, nest);
  2491. return 0;
  2492. msg_full:
  2493. nla_nest_cancel(skb, nest);
  2494. return -EMSGSIZE;
  2495. }
  2496. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg,
  2497. struct tipc_link *bcl)
  2498. {
  2499. int err;
  2500. void *hdr;
  2501. struct nlattr *attrs;
  2502. struct nlattr *prop;
  2503. u32 bc_mode = tipc_bcast_get_mode(net);
  2504. u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
  2505. if (!bcl)
  2506. return 0;
  2507. tipc_bcast_lock(net);
  2508. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  2509. NLM_F_MULTI, TIPC_NL_LINK_GET);
  2510. if (!hdr) {
  2511. tipc_bcast_unlock(net);
  2512. return -EMSGSIZE;
  2513. }
  2514. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
  2515. if (!attrs)
  2516. goto msg_full;
  2517. /* The broadcast link is always up */
  2518. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2519. goto attr_msg_full;
  2520. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  2521. goto attr_msg_full;
  2522. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  2523. goto attr_msg_full;
  2524. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
  2525. goto attr_msg_full;
  2526. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
  2527. goto attr_msg_full;
  2528. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
  2529. if (!prop)
  2530. goto attr_msg_full;
  2531. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->max_win))
  2532. goto prop_msg_full;
  2533. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
  2534. goto prop_msg_full;
  2535. if (bc_mode & BCLINK_MODE_SEL)
  2536. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
  2537. bc_ratio))
  2538. goto prop_msg_full;
  2539. nla_nest_end(msg->skb, prop);
  2540. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  2541. if (err)
  2542. goto attr_msg_full;
  2543. tipc_bcast_unlock(net);
  2544. nla_nest_end(msg->skb, attrs);
  2545. genlmsg_end(msg->skb, hdr);
  2546. return 0;
  2547. prop_msg_full:
  2548. nla_nest_cancel(msg->skb, prop);
  2549. attr_msg_full:
  2550. nla_nest_cancel(msg->skb, attrs);
  2551. msg_full:
  2552. tipc_bcast_unlock(net);
  2553. genlmsg_cancel(msg->skb, hdr);
  2554. return -EMSGSIZE;
  2555. }
  2556. void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
  2557. struct sk_buff_head *xmitq)
  2558. {
  2559. l->tolerance = tol;
  2560. if (l->bc_rcvlink)
  2561. l->bc_rcvlink->tolerance = tol;
  2562. if (tipc_link_is_up(l))
  2563. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
  2564. }
  2565. void tipc_link_set_prio(struct tipc_link *l, u32 prio,
  2566. struct sk_buff_head *xmitq)
  2567. {
  2568. l->priority = prio;
  2569. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
  2570. }
  2571. void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
  2572. {
  2573. l->abort_limit = limit;
  2574. }
  2575. /**
  2576. * tipc_link_dump - dump TIPC link data
  2577. * @l: tipc link to be dumped
  2578. * @dqueues: bitmask to decide if any link queue to be dumped?
  2579. * - TIPC_DUMP_NONE: don't dump link queues
  2580. * - TIPC_DUMP_TRANSMQ: dump link transmq queue
  2581. * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
  2582. * - TIPC_DUMP_DEFERDQ: dump link deferd queue
  2583. * - TIPC_DUMP_INPUTQ: dump link input queue
  2584. * - TIPC_DUMP_WAKEUP: dump link wakeup queue
  2585. * - TIPC_DUMP_ALL: dump all the link queues above
  2586. * @buf: returned buffer of dump data in format
  2587. */
  2588. int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
  2589. {
  2590. int i = 0;
  2591. size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
  2592. struct sk_buff_head *list;
  2593. struct sk_buff *hskb, *tskb;
  2594. u32 len;
  2595. if (!l) {
  2596. i += scnprintf(buf, sz, "link data: (null)\n");
  2597. return i;
  2598. }
  2599. i += scnprintf(buf, sz, "link data: %x", l->addr);
  2600. i += scnprintf(buf + i, sz - i, " %x", l->state);
  2601. i += scnprintf(buf + i, sz - i, " %u", l->in_session);
  2602. i += scnprintf(buf + i, sz - i, " %u", l->session);
  2603. i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
  2604. i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
  2605. i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
  2606. i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
  2607. i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
  2608. i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
  2609. i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
  2610. i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
  2611. i += scnprintf(buf + i, sz - i, " %u", 0);
  2612. i += scnprintf(buf + i, sz - i, " %u", 0);
  2613. i += scnprintf(buf + i, sz - i, " %u", l->acked);
  2614. list = &l->transmq;
  2615. len = skb_queue_len(list);
  2616. hskb = skb_peek(list);
  2617. tskb = skb_peek_tail(list);
  2618. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2619. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2620. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2621. list = &l->deferdq;
  2622. len = skb_queue_len(list);
  2623. hskb = skb_peek(list);
  2624. tskb = skb_peek_tail(list);
  2625. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2626. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2627. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2628. list = &l->backlogq;
  2629. len = skb_queue_len(list);
  2630. hskb = skb_peek(list);
  2631. tskb = skb_peek_tail(list);
  2632. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2633. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2634. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2635. list = l->inputq;
  2636. len = skb_queue_len(list);
  2637. hskb = skb_peek(list);
  2638. tskb = skb_peek_tail(list);
  2639. i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
  2640. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2641. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2642. if (dqueues & TIPC_DUMP_TRANSMQ) {
  2643. i += scnprintf(buf + i, sz - i, "transmq: ");
  2644. i += tipc_list_dump(&l->transmq, false, buf + i);
  2645. }
  2646. if (dqueues & TIPC_DUMP_BACKLOGQ) {
  2647. i += scnprintf(buf + i, sz - i,
  2648. "backlogq: <%u %u %u %u %u>, ",
  2649. l->backlog[TIPC_LOW_IMPORTANCE].len,
  2650. l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
  2651. l->backlog[TIPC_HIGH_IMPORTANCE].len,
  2652. l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
  2653. l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
  2654. i += tipc_list_dump(&l->backlogq, false, buf + i);
  2655. }
  2656. if (dqueues & TIPC_DUMP_DEFERDQ) {
  2657. i += scnprintf(buf + i, sz - i, "deferdq: ");
  2658. i += tipc_list_dump(&l->deferdq, false, buf + i);
  2659. }
  2660. if (dqueues & TIPC_DUMP_INPUTQ) {
  2661. i += scnprintf(buf + i, sz - i, "inputq: ");
  2662. i += tipc_list_dump(l->inputq, false, buf + i);
  2663. }
  2664. if (dqueues & TIPC_DUMP_WAKEUP) {
  2665. i += scnprintf(buf + i, sz - i, "wakeup: ");
  2666. i += tipc_list_dump(&l->wakeupq, false, buf + i);
  2667. }
  2668. return i;
  2669. }