link.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  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. __skb_queue_purge(list);
  959. return -ENOBUFS;
  960. }
  961. rc = link_schedule_user(l, hdr);
  962. }
  963. if (pkt_cnt > 1) {
  964. l->stats.sent_fragmented++;
  965. l->stats.sent_fragments += pkt_cnt;
  966. }
  967. /* Prepare each packet for sending, and add to relevant queue: */
  968. while ((skb = __skb_dequeue(list))) {
  969. if (likely(skb_queue_len(transmq) < cwin)) {
  970. hdr = buf_msg(skb);
  971. msg_set_seqno(hdr, seqno);
  972. msg_set_ack(hdr, ack);
  973. msg_set_bcast_ack(hdr, bc_ack);
  974. _skb = skb_clone(skb, GFP_ATOMIC);
  975. if (!_skb) {
  976. kfree_skb(skb);
  977. __skb_queue_purge(list);
  978. return -ENOBUFS;
  979. }
  980. __skb_queue_tail(transmq, skb);
  981. tipc_link_set_skb_retransmit_time(skb, l);
  982. __skb_queue_tail(xmitq, _skb);
  983. TIPC_SKB_CB(skb)->ackers = l->ackers;
  984. l->rcv_unacked = 0;
  985. l->stats.sent_pkts++;
  986. seqno++;
  987. continue;
  988. }
  989. if (tipc_msg_try_bundle(l->backlog[imp].target_bskb, &skb,
  990. mss, l->addr, &new_bundle)) {
  991. if (skb) {
  992. /* Keep a ref. to the skb for next try */
  993. l->backlog[imp].target_bskb = skb;
  994. l->backlog[imp].len++;
  995. __skb_queue_tail(backlogq, skb);
  996. } else {
  997. if (new_bundle) {
  998. l->stats.sent_bundles++;
  999. l->stats.sent_bundled++;
  1000. }
  1001. l->stats.sent_bundled++;
  1002. }
  1003. continue;
  1004. }
  1005. l->backlog[imp].target_bskb = NULL;
  1006. l->backlog[imp].len += (1 + skb_queue_len(list));
  1007. __skb_queue_tail(backlogq, skb);
  1008. skb_queue_splice_tail_init(list, backlogq);
  1009. }
  1010. l->snd_nxt = seqno;
  1011. return rc;
  1012. }
  1013. static void tipc_link_update_cwin(struct tipc_link *l, int released,
  1014. bool retransmitted)
  1015. {
  1016. int bklog_len = skb_queue_len(&l->backlogq);
  1017. struct sk_buff_head *txq = &l->transmq;
  1018. int txq_len = skb_queue_len(txq);
  1019. u16 cwin = l->window;
  1020. /* Enter fast recovery */
  1021. if (unlikely(retransmitted)) {
  1022. l->ssthresh = max_t(u16, l->window / 2, 300);
  1023. l->window = min_t(u16, l->ssthresh, l->window);
  1024. return;
  1025. }
  1026. /* Enter slow start */
  1027. if (unlikely(!released)) {
  1028. l->ssthresh = max_t(u16, l->window / 2, 300);
  1029. l->window = l->min_win;
  1030. return;
  1031. }
  1032. /* Don't increase window if no pressure on the transmit queue */
  1033. if (txq_len + bklog_len < cwin)
  1034. return;
  1035. /* Don't increase window if there are holes the transmit queue */
  1036. if (txq_len && l->snd_nxt - buf_seqno(skb_peek(txq)) != txq_len)
  1037. return;
  1038. l->cong_acks += released;
  1039. /* Slow start */
  1040. if (cwin <= l->ssthresh) {
  1041. l->window = min_t(u16, cwin + released, l->max_win);
  1042. return;
  1043. }
  1044. /* Congestion avoidance */
  1045. if (l->cong_acks < cwin)
  1046. return;
  1047. l->window = min_t(u16, ++cwin, l->max_win);
  1048. l->cong_acks = 0;
  1049. }
  1050. static void tipc_link_advance_backlog(struct tipc_link *l,
  1051. struct sk_buff_head *xmitq)
  1052. {
  1053. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  1054. struct sk_buff_head *txq = &l->transmq;
  1055. struct sk_buff *skb, *_skb;
  1056. u16 ack = l->rcv_nxt - 1;
  1057. u16 seqno = l->snd_nxt;
  1058. struct tipc_msg *hdr;
  1059. u16 cwin = l->window;
  1060. u32 imp;
  1061. while (skb_queue_len(txq) < cwin) {
  1062. skb = skb_peek(&l->backlogq);
  1063. if (!skb)
  1064. break;
  1065. _skb = skb_clone(skb, GFP_ATOMIC);
  1066. if (!_skb)
  1067. break;
  1068. __skb_dequeue(&l->backlogq);
  1069. hdr = buf_msg(skb);
  1070. imp = msg_importance(hdr);
  1071. l->backlog[imp].len--;
  1072. if (unlikely(skb == l->backlog[imp].target_bskb))
  1073. l->backlog[imp].target_bskb = NULL;
  1074. __skb_queue_tail(&l->transmq, skb);
  1075. tipc_link_set_skb_retransmit_time(skb, l);
  1076. __skb_queue_tail(xmitq, _skb);
  1077. TIPC_SKB_CB(skb)->ackers = l->ackers;
  1078. msg_set_seqno(hdr, seqno);
  1079. msg_set_ack(hdr, ack);
  1080. msg_set_bcast_ack(hdr, bc_ack);
  1081. l->rcv_unacked = 0;
  1082. l->stats.sent_pkts++;
  1083. seqno++;
  1084. }
  1085. l->snd_nxt = seqno;
  1086. }
  1087. /**
  1088. * link_retransmit_failure() - Detect repeated retransmit failures
  1089. * @l: tipc link sender
  1090. * @r: tipc link receiver (= l in case of unicast)
  1091. * @rc: returned code
  1092. *
  1093. * Return: true if the repeated retransmit failures happens, otherwise
  1094. * false
  1095. */
  1096. static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
  1097. int *rc)
  1098. {
  1099. struct sk_buff *skb = skb_peek(&l->transmq);
  1100. struct tipc_msg *hdr;
  1101. if (!skb)
  1102. return false;
  1103. if (!TIPC_SKB_CB(skb)->retr_cnt)
  1104. return false;
  1105. if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
  1106. msecs_to_jiffies(r->tolerance * 10)))
  1107. return false;
  1108. hdr = buf_msg(skb);
  1109. if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
  1110. return false;
  1111. pr_warn("Retransmission failure on link <%s>\n", l->name);
  1112. link_print(l, "State of link ");
  1113. pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
  1114. msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
  1115. pr_info("sqno %u, prev: %x, dest: %x\n",
  1116. msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
  1117. pr_info("retr_stamp %d, retr_cnt %d\n",
  1118. jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
  1119. TIPC_SKB_CB(skb)->retr_cnt);
  1120. trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
  1121. trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
  1122. trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
  1123. if (link_is_bc_sndlink(l)) {
  1124. r->state = LINK_RESET;
  1125. *rc |= TIPC_LINK_DOWN_EVT;
  1126. } else {
  1127. *rc |= tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1128. }
  1129. return true;
  1130. }
  1131. /* tipc_data_input - deliver data and name distr msgs to upper layer
  1132. *
  1133. * Consumes buffer if message is of right type
  1134. * Node lock must be held
  1135. */
  1136. static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
  1137. struct sk_buff_head *inputq)
  1138. {
  1139. struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
  1140. struct tipc_msg *hdr = buf_msg(skb);
  1141. switch (msg_user(hdr)) {
  1142. case TIPC_LOW_IMPORTANCE:
  1143. case TIPC_MEDIUM_IMPORTANCE:
  1144. case TIPC_HIGH_IMPORTANCE:
  1145. case TIPC_CRITICAL_IMPORTANCE:
  1146. if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
  1147. skb_queue_tail(mc_inputq, skb);
  1148. return true;
  1149. }
  1150. fallthrough;
  1151. case CONN_MANAGER:
  1152. skb_queue_tail(inputq, skb);
  1153. return true;
  1154. case GROUP_PROTOCOL:
  1155. skb_queue_tail(mc_inputq, skb);
  1156. return true;
  1157. case NAME_DISTRIBUTOR:
  1158. l->bc_rcvlink->state = LINK_ESTABLISHED;
  1159. skb_queue_tail(l->namedq, skb);
  1160. return true;
  1161. case MSG_BUNDLER:
  1162. case TUNNEL_PROTOCOL:
  1163. case MSG_FRAGMENTER:
  1164. case BCAST_PROTOCOL:
  1165. return false;
  1166. #ifdef CONFIG_TIPC_CRYPTO
  1167. case MSG_CRYPTO:
  1168. if (sysctl_tipc_key_exchange_enabled &&
  1169. TIPC_SKB_CB(skb)->decrypted) {
  1170. tipc_crypto_msg_rcv(l->net, skb);
  1171. return true;
  1172. }
  1173. fallthrough;
  1174. #endif
  1175. default:
  1176. pr_warn("Dropping received illegal msg type\n");
  1177. kfree_skb(skb);
  1178. return true;
  1179. }
  1180. }
  1181. /* tipc_link_input - process packet that has passed link protocol check
  1182. *
  1183. * Consumes buffer
  1184. */
  1185. static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
  1186. struct sk_buff_head *inputq,
  1187. struct sk_buff **reasm_skb)
  1188. {
  1189. struct tipc_msg *hdr = buf_msg(skb);
  1190. struct sk_buff *iskb;
  1191. struct sk_buff_head tmpq;
  1192. int usr = msg_user(hdr);
  1193. int pos = 0;
  1194. if (usr == MSG_BUNDLER) {
  1195. skb_queue_head_init(&tmpq);
  1196. l->stats.recv_bundles++;
  1197. l->stats.recv_bundled += msg_msgcnt(hdr);
  1198. while (tipc_msg_extract(skb, &iskb, &pos))
  1199. tipc_data_input(l, iskb, &tmpq);
  1200. tipc_skb_queue_splice_tail(&tmpq, inputq);
  1201. return 0;
  1202. } else if (usr == MSG_FRAGMENTER) {
  1203. l->stats.recv_fragments++;
  1204. if (tipc_buf_append(reasm_skb, &skb)) {
  1205. l->stats.recv_fragmented++;
  1206. tipc_data_input(l, skb, inputq);
  1207. } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
  1208. pr_warn_ratelimited("Unable to build fragment list\n");
  1209. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1210. }
  1211. return 0;
  1212. } else if (usr == BCAST_PROTOCOL) {
  1213. tipc_bcast_lock(l->net);
  1214. tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
  1215. tipc_bcast_unlock(l->net);
  1216. }
  1217. kfree_skb(skb);
  1218. return 0;
  1219. }
  1220. /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
  1221. * inner message along with the ones in the old link's
  1222. * deferdq
  1223. * @l: tunnel link
  1224. * @skb: TUNNEL_PROTOCOL message
  1225. * @inputq: queue to put messages ready for delivery
  1226. */
  1227. static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
  1228. struct sk_buff_head *inputq)
  1229. {
  1230. struct sk_buff **reasm_skb = &l->failover_reasm_skb;
  1231. struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
  1232. struct sk_buff_head *fdefq = &l->failover_deferdq;
  1233. struct tipc_msg *hdr = buf_msg(skb);
  1234. struct sk_buff *iskb;
  1235. int ipos = 0;
  1236. int rc = 0;
  1237. u16 seqno;
  1238. if (msg_type(hdr) == SYNCH_MSG) {
  1239. kfree_skb(skb);
  1240. return 0;
  1241. }
  1242. /* Not a fragment? */
  1243. if (likely(!msg_nof_fragms(hdr))) {
  1244. if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
  1245. pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
  1246. skb_queue_len(fdefq));
  1247. return 0;
  1248. }
  1249. kfree_skb(skb);
  1250. } else {
  1251. /* Set fragment type for buf_append */
  1252. if (msg_fragm_no(hdr) == 1)
  1253. msg_set_type(hdr, FIRST_FRAGMENT);
  1254. else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
  1255. msg_set_type(hdr, FRAGMENT);
  1256. else
  1257. msg_set_type(hdr, LAST_FRAGMENT);
  1258. if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
  1259. /* Successful but non-complete reassembly? */
  1260. if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
  1261. return 0;
  1262. pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
  1263. return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  1264. }
  1265. iskb = skb;
  1266. }
  1267. do {
  1268. seqno = buf_seqno(iskb);
  1269. if (unlikely(less(seqno, l->drop_point))) {
  1270. kfree_skb(iskb);
  1271. continue;
  1272. }
  1273. if (unlikely(seqno != l->drop_point)) {
  1274. __tipc_skb_queue_sorted(fdefq, seqno, iskb);
  1275. continue;
  1276. }
  1277. l->drop_point++;
  1278. if (!tipc_data_input(l, iskb, inputq))
  1279. rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
  1280. if (unlikely(rc))
  1281. break;
  1282. } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
  1283. return rc;
  1284. }
  1285. /**
  1286. * tipc_get_gap_ack_blks - get Gap ACK blocks from PROTOCOL/STATE_MSG
  1287. * @ga: returned pointer to the Gap ACK blocks if any
  1288. * @l: the tipc link
  1289. * @hdr: the PROTOCOL/STATE_MSG header
  1290. * @uc: desired Gap ACK blocks type, i.e. unicast (= 1) or broadcast (= 0)
  1291. *
  1292. * Return: the total Gap ACK blocks size
  1293. */
  1294. u16 tipc_get_gap_ack_blks(struct tipc_gap_ack_blks **ga, struct tipc_link *l,
  1295. struct tipc_msg *hdr, bool uc)
  1296. {
  1297. struct tipc_gap_ack_blks *p;
  1298. u16 sz = 0;
  1299. /* Does peer support the Gap ACK blocks feature? */
  1300. if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
  1301. p = (struct tipc_gap_ack_blks *)msg_data(hdr);
  1302. sz = ntohs(p->len);
  1303. /* Sanity check */
  1304. if (sz == struct_size(p, gacks, size_add(p->ugack_cnt, p->bgack_cnt))) {
  1305. /* Good, check if the desired type exists */
  1306. if ((uc && p->ugack_cnt) || (!uc && p->bgack_cnt))
  1307. goto ok;
  1308. /* Backward compatible: peer might not support bc, but uc? */
  1309. } else if (uc && sz == struct_size(p, gacks, p->ugack_cnt)) {
  1310. if (p->ugack_cnt) {
  1311. p->bgack_cnt = 0;
  1312. goto ok;
  1313. }
  1314. }
  1315. }
  1316. /* Other cases: ignore! */
  1317. p = NULL;
  1318. ok:
  1319. *ga = p;
  1320. return sz;
  1321. }
  1322. static u8 __tipc_build_gap_ack_blks(struct tipc_gap_ack_blks *ga,
  1323. struct tipc_link *l, u8 start_index)
  1324. {
  1325. struct tipc_gap_ack *gacks = &ga->gacks[start_index];
  1326. struct sk_buff *skb = skb_peek(&l->deferdq);
  1327. u16 expect, seqno = 0;
  1328. u8 n = 0;
  1329. if (!skb)
  1330. return 0;
  1331. expect = buf_seqno(skb);
  1332. skb_queue_walk(&l->deferdq, skb) {
  1333. seqno = buf_seqno(skb);
  1334. if (unlikely(more(seqno, expect))) {
  1335. gacks[n].ack = htons(expect - 1);
  1336. gacks[n].gap = htons(seqno - expect);
  1337. if (++n >= MAX_GAP_ACK_BLKS / 2) {
  1338. pr_info_ratelimited("Gacks on %s: %d, ql: %d!\n",
  1339. l->name, n,
  1340. skb_queue_len(&l->deferdq));
  1341. return n;
  1342. }
  1343. } else if (unlikely(less(seqno, expect))) {
  1344. pr_warn("Unexpected skb in deferdq!\n");
  1345. continue;
  1346. }
  1347. expect = seqno + 1;
  1348. }
  1349. /* last block */
  1350. gacks[n].ack = htons(seqno);
  1351. gacks[n].gap = 0;
  1352. n++;
  1353. return n;
  1354. }
  1355. /* tipc_build_gap_ack_blks - build Gap ACK blocks
  1356. * @l: tipc unicast link
  1357. * @hdr: the tipc message buffer to store the Gap ACK blocks after built
  1358. *
  1359. * The function builds Gap ACK blocks for both the unicast & broadcast receiver
  1360. * links of a certain peer, the buffer after built has the network data format
  1361. * as found at the struct tipc_gap_ack_blks definition.
  1362. *
  1363. * returns the actual allocated memory size
  1364. */
  1365. static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr)
  1366. {
  1367. struct tipc_link *bcl = l->bc_rcvlink;
  1368. struct tipc_gap_ack_blks *ga;
  1369. u16 len;
  1370. ga = (struct tipc_gap_ack_blks *)msg_data(hdr);
  1371. /* Start with broadcast link first */
  1372. tipc_bcast_lock(bcl->net);
  1373. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1374. msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
  1375. ga->bgack_cnt = __tipc_build_gap_ack_blks(ga, bcl, 0);
  1376. tipc_bcast_unlock(bcl->net);
  1377. /* Now for unicast link, but an explicit NACK only (???) */
  1378. ga->ugack_cnt = (msg_seq_gap(hdr)) ?
  1379. __tipc_build_gap_ack_blks(ga, l, ga->bgack_cnt) : 0;
  1380. /* Total len */
  1381. len = struct_size(ga, gacks, size_add(ga->bgack_cnt, ga->ugack_cnt));
  1382. ga->len = htons(len);
  1383. return len;
  1384. }
  1385. /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
  1386. * acked packets, also doing retransmissions if
  1387. * gaps found
  1388. * @l: tipc link with transmq queue to be advanced
  1389. * @r: tipc link "receiver" i.e. in case of broadcast (= "l" if unicast)
  1390. * @acked: seqno of last packet acked by peer without any gaps before
  1391. * @gap: # of gap packets
  1392. * @ga: buffer pointer to Gap ACK blocks from peer
  1393. * @xmitq: queue for accumulating the retransmitted packets if any
  1394. * @retransmitted: returned boolean value if a retransmission is really issued
  1395. * @rc: returned code e.g. TIPC_LINK_DOWN_EVT if a repeated retransmit failures
  1396. * happens (- unlikely case)
  1397. *
  1398. * Return: the number of packets released from the link transmq
  1399. */
  1400. static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
  1401. u16 acked, u16 gap,
  1402. struct tipc_gap_ack_blks *ga,
  1403. struct sk_buff_head *xmitq,
  1404. bool *retransmitted, int *rc)
  1405. {
  1406. struct tipc_gap_ack_blks *last_ga = r->last_ga, *this_ga = NULL;
  1407. struct tipc_gap_ack *gacks = NULL;
  1408. struct sk_buff *skb, *_skb, *tmp;
  1409. struct tipc_msg *hdr;
  1410. u32 qlen = skb_queue_len(&l->transmq);
  1411. u16 nacked = acked, ngap = gap, gack_cnt = 0;
  1412. u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
  1413. u16 ack = l->rcv_nxt - 1;
  1414. u16 seqno, n = 0;
  1415. u16 end = r->acked, start = end, offset = r->last_gap;
  1416. u16 si = (last_ga) ? last_ga->start_index : 0;
  1417. bool is_uc = !link_is_bc_sndlink(l);
  1418. bool bc_has_acked = false;
  1419. trace_tipc_link_retrans(r, acked + 1, acked + gap, &l->transmq);
  1420. /* Determine Gap ACK blocks if any for the particular link */
  1421. if (ga && is_uc) {
  1422. /* Get the Gap ACKs, uc part */
  1423. gack_cnt = ga->ugack_cnt;
  1424. gacks = &ga->gacks[ga->bgack_cnt];
  1425. } else if (ga) {
  1426. /* Copy the Gap ACKs, bc part, for later renewal if needed */
  1427. this_ga = kmemdup(ga, struct_size(ga, gacks, ga->bgack_cnt),
  1428. GFP_ATOMIC);
  1429. if (likely(this_ga)) {
  1430. this_ga->start_index = 0;
  1431. /* Start with the bc Gap ACKs */
  1432. gack_cnt = this_ga->bgack_cnt;
  1433. gacks = &this_ga->gacks[0];
  1434. } else {
  1435. /* Hmm, we can get in trouble..., simply ignore it */
  1436. pr_warn_ratelimited("Ignoring bc Gap ACKs, no memory\n");
  1437. }
  1438. }
  1439. /* Advance the link transmq */
  1440. skb_queue_walk_safe(&l->transmq, skb, tmp) {
  1441. seqno = buf_seqno(skb);
  1442. next_gap_ack:
  1443. if (less_eq(seqno, nacked)) {
  1444. if (is_uc)
  1445. goto release;
  1446. /* Skip packets peer has already acked */
  1447. if (!more(seqno, r->acked))
  1448. continue;
  1449. /* Get the next of last Gap ACK blocks */
  1450. while (more(seqno, end)) {
  1451. if (!last_ga || si >= last_ga->bgack_cnt)
  1452. break;
  1453. start = end + offset + 1;
  1454. end = ntohs(last_ga->gacks[si].ack);
  1455. offset = ntohs(last_ga->gacks[si].gap);
  1456. si++;
  1457. WARN_ONCE(more(start, end) ||
  1458. (!offset &&
  1459. si < last_ga->bgack_cnt) ||
  1460. si > MAX_GAP_ACK_BLKS,
  1461. "Corrupted Gap ACK: %d %d %d %d %d\n",
  1462. start, end, offset, si,
  1463. last_ga->bgack_cnt);
  1464. }
  1465. /* Check against the last Gap ACK block */
  1466. if (tipc_in_range(seqno, start, end))
  1467. continue;
  1468. /* Update/release the packet peer is acking */
  1469. bc_has_acked = true;
  1470. if (--TIPC_SKB_CB(skb)->ackers)
  1471. continue;
  1472. release:
  1473. /* release skb */
  1474. __skb_unlink(skb, &l->transmq);
  1475. kfree_skb(skb);
  1476. } else if (less_eq(seqno, nacked + ngap)) {
  1477. /* First gap: check if repeated retrans failures? */
  1478. if (unlikely(seqno == acked + 1 &&
  1479. link_retransmit_failure(l, r, rc))) {
  1480. /* Ignore this bc Gap ACKs if any */
  1481. kfree(this_ga);
  1482. this_ga = NULL;
  1483. break;
  1484. }
  1485. /* retransmit skb if unrestricted*/
  1486. if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
  1487. continue;
  1488. tipc_link_set_skb_retransmit_time(skb, l);
  1489. _skb = pskb_copy(skb, GFP_ATOMIC);
  1490. if (!_skb)
  1491. continue;
  1492. hdr = buf_msg(_skb);
  1493. msg_set_ack(hdr, ack);
  1494. msg_set_bcast_ack(hdr, bc_ack);
  1495. _skb->priority = TC_PRIO_CONTROL;
  1496. __skb_queue_tail(xmitq, _skb);
  1497. l->stats.retransmitted++;
  1498. if (!is_uc)
  1499. r->stats.retransmitted++;
  1500. *retransmitted = true;
  1501. /* Increase actual retrans counter & mark first time */
  1502. if (!TIPC_SKB_CB(skb)->retr_cnt++)
  1503. TIPC_SKB_CB(skb)->retr_stamp = jiffies;
  1504. } else {
  1505. /* retry with Gap ACK blocks if any */
  1506. if (n >= gack_cnt)
  1507. break;
  1508. nacked = ntohs(gacks[n].ack);
  1509. ngap = ntohs(gacks[n].gap);
  1510. n++;
  1511. goto next_gap_ack;
  1512. }
  1513. }
  1514. /* Renew last Gap ACK blocks for bc if needed */
  1515. if (bc_has_acked) {
  1516. if (this_ga) {
  1517. kfree(last_ga);
  1518. r->last_ga = this_ga;
  1519. r->last_gap = gap;
  1520. } else if (last_ga) {
  1521. if (less(acked, start)) {
  1522. si--;
  1523. offset = start - acked - 1;
  1524. } else if (less(acked, end)) {
  1525. acked = end;
  1526. }
  1527. if (si < last_ga->bgack_cnt) {
  1528. last_ga->start_index = si;
  1529. r->last_gap = offset;
  1530. } else {
  1531. kfree(last_ga);
  1532. r->last_ga = NULL;
  1533. r->last_gap = 0;
  1534. }
  1535. } else {
  1536. r->last_gap = 0;
  1537. }
  1538. r->acked = acked;
  1539. } else {
  1540. kfree(this_ga);
  1541. }
  1542. return qlen - skb_queue_len(&l->transmq);
  1543. }
  1544. /* tipc_link_build_state_msg: prepare link state message for transmission
  1545. *
  1546. * Note that sending of broadcast ack is coordinated among nodes, to reduce
  1547. * risk of ack storms towards the sender
  1548. */
  1549. int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1550. {
  1551. if (!l)
  1552. return 0;
  1553. /* Broadcast ACK must be sent via a unicast link => defer to caller */
  1554. if (link_is_bc_rcvlink(l)) {
  1555. if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
  1556. return 0;
  1557. l->rcv_unacked = 0;
  1558. /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
  1559. l->snd_nxt = l->rcv_nxt;
  1560. return TIPC_LINK_SND_STATE;
  1561. }
  1562. /* Unicast ACK */
  1563. l->rcv_unacked = 0;
  1564. l->stats.sent_acks++;
  1565. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
  1566. return 0;
  1567. }
  1568. /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
  1569. */
  1570. void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
  1571. {
  1572. int mtyp = RESET_MSG;
  1573. struct sk_buff *skb;
  1574. if (l->state == LINK_ESTABLISHING)
  1575. mtyp = ACTIVATE_MSG;
  1576. tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
  1577. /* Inform peer that this endpoint is going down if applicable */
  1578. skb = skb_peek_tail(xmitq);
  1579. if (skb && (l->state == LINK_RESET))
  1580. msg_set_peer_stopping(buf_msg(skb), 1);
  1581. }
  1582. /* tipc_link_build_nack_msg: prepare link nack message for transmission
  1583. * Note that sending of broadcast NACK is coordinated among nodes, to
  1584. * reduce the risk of NACK storms towards the sender
  1585. */
  1586. static int tipc_link_build_nack_msg(struct tipc_link *l,
  1587. struct sk_buff_head *xmitq)
  1588. {
  1589. u32 def_cnt = ++l->stats.deferred_recv;
  1590. struct sk_buff_head *dfq = &l->deferdq;
  1591. u32 defq_len = skb_queue_len(dfq);
  1592. int match1, match2;
  1593. if (link_is_bc_rcvlink(l)) {
  1594. match1 = def_cnt & 0xf;
  1595. match2 = tipc_own_addr(l->net) & 0xf;
  1596. if (match1 == match2)
  1597. return TIPC_LINK_SND_STATE;
  1598. return 0;
  1599. }
  1600. if (defq_len >= 3 && !((defq_len - 3) % 16)) {
  1601. u16 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1602. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0,
  1603. rcvgap, 0, 0, xmitq);
  1604. }
  1605. return 0;
  1606. }
  1607. /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
  1608. * @l: the link that should handle the message
  1609. * @skb: TIPC packet
  1610. * @xmitq: queue to place packets to be sent after this call
  1611. */
  1612. int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
  1613. struct sk_buff_head *xmitq)
  1614. {
  1615. struct sk_buff_head *defq = &l->deferdq;
  1616. struct tipc_msg *hdr = buf_msg(skb);
  1617. u16 seqno, rcv_nxt, win_lim;
  1618. int released = 0;
  1619. int rc = 0;
  1620. /* Verify and update link state */
  1621. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  1622. return tipc_link_proto_rcv(l, skb, xmitq);
  1623. /* Don't send probe at next timeout expiration */
  1624. l->silent_intv_cnt = 0;
  1625. do {
  1626. hdr = buf_msg(skb);
  1627. seqno = msg_seqno(hdr);
  1628. rcv_nxt = l->rcv_nxt;
  1629. win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
  1630. if (unlikely(!tipc_link_is_up(l))) {
  1631. if (l->state == LINK_ESTABLISHING)
  1632. rc = TIPC_LINK_UP_EVT;
  1633. kfree_skb(skb);
  1634. break;
  1635. }
  1636. /* Drop if outside receive window */
  1637. if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
  1638. l->stats.duplicates++;
  1639. kfree_skb(skb);
  1640. break;
  1641. }
  1642. released += tipc_link_advance_transmq(l, l, msg_ack(hdr), 0,
  1643. NULL, NULL, NULL, NULL);
  1644. /* Defer delivery if sequence gap */
  1645. if (unlikely(seqno != rcv_nxt)) {
  1646. if (!__tipc_skb_queue_sorted(defq, seqno, skb))
  1647. l->stats.duplicates++;
  1648. rc |= tipc_link_build_nack_msg(l, xmitq);
  1649. break;
  1650. }
  1651. /* Deliver packet */
  1652. l->rcv_nxt++;
  1653. l->stats.recv_pkts++;
  1654. if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
  1655. rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
  1656. else if (!tipc_data_input(l, skb, l->inputq))
  1657. rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
  1658. if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
  1659. rc |= tipc_link_build_state_msg(l, xmitq);
  1660. if (unlikely(rc & ~TIPC_LINK_SND_STATE))
  1661. break;
  1662. } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
  1663. /* Forward queues and wake up waiting users */
  1664. if (released) {
  1665. tipc_link_update_cwin(l, released, 0);
  1666. tipc_link_advance_backlog(l, xmitq);
  1667. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  1668. link_prepare_wakeup(l);
  1669. }
  1670. return rc;
  1671. }
  1672. static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
  1673. bool probe_reply, u16 rcvgap,
  1674. int tolerance, int priority,
  1675. struct sk_buff_head *xmitq)
  1676. {
  1677. struct tipc_mon_state *mstate = &l->mon_state;
  1678. struct sk_buff_head *dfq = &l->deferdq;
  1679. struct tipc_link *bcl = l->bc_rcvlink;
  1680. struct tipc_msg *hdr;
  1681. struct sk_buff *skb;
  1682. bool node_up = tipc_link_is_up(bcl);
  1683. u16 glen = 0, bc_rcvgap = 0;
  1684. int dlen = 0;
  1685. void *data;
  1686. /* Don't send protocol message during reset or link failover */
  1687. if (tipc_link_is_blocked(l))
  1688. return;
  1689. if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
  1690. return;
  1691. if ((probe || probe_reply) && !skb_queue_empty(dfq))
  1692. rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
  1693. skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
  1694. tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
  1695. l->addr, tipc_own_addr(l->net), 0, 0, 0);
  1696. if (!skb)
  1697. return;
  1698. hdr = buf_msg(skb);
  1699. data = msg_data(hdr);
  1700. msg_set_session(hdr, l->session);
  1701. msg_set_bearer_id(hdr, l->bearer_id);
  1702. msg_set_net_plane(hdr, l->net_plane);
  1703. msg_set_next_sent(hdr, l->snd_nxt);
  1704. msg_set_ack(hdr, l->rcv_nxt - 1);
  1705. msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
  1706. msg_set_bc_ack_invalid(hdr, !node_up);
  1707. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  1708. msg_set_link_tolerance(hdr, tolerance);
  1709. msg_set_linkprio(hdr, priority);
  1710. msg_set_redundant_link(hdr, node_up);
  1711. msg_set_seq_gap(hdr, 0);
  1712. msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
  1713. if (mtyp == STATE_MSG) {
  1714. if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
  1715. msg_set_seqno(hdr, l->snd_nxt_state++);
  1716. msg_set_seq_gap(hdr, rcvgap);
  1717. bc_rcvgap = link_bc_rcv_gap(bcl);
  1718. msg_set_bc_gap(hdr, bc_rcvgap);
  1719. msg_set_probe(hdr, probe);
  1720. msg_set_is_keepalive(hdr, probe || probe_reply);
  1721. if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
  1722. glen = tipc_build_gap_ack_blks(l, hdr);
  1723. tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
  1724. msg_set_size(hdr, INT_H_SIZE + glen + dlen);
  1725. skb_trim(skb, INT_H_SIZE + glen + dlen);
  1726. l->stats.sent_states++;
  1727. l->rcv_unacked = 0;
  1728. } else {
  1729. /* RESET_MSG or ACTIVATE_MSG */
  1730. if (mtyp == ACTIVATE_MSG) {
  1731. msg_set_dest_session_valid(hdr, 1);
  1732. msg_set_dest_session(hdr, l->peer_session);
  1733. }
  1734. msg_set_max_pkt(hdr, l->advertised_mtu);
  1735. strcpy(data, l->if_name);
  1736. msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1737. skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
  1738. }
  1739. if (probe)
  1740. l->stats.sent_probes++;
  1741. if (rcvgap)
  1742. l->stats.sent_nacks++;
  1743. if (bc_rcvgap)
  1744. bcl->stats.sent_nacks++;
  1745. skb->priority = TC_PRIO_CONTROL;
  1746. __skb_queue_tail(xmitq, skb);
  1747. trace_tipc_proto_build(skb, false, l->name);
  1748. }
  1749. void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
  1750. struct sk_buff_head *xmitq)
  1751. {
  1752. u32 onode = tipc_own_addr(l->net);
  1753. struct tipc_msg *hdr, *ihdr;
  1754. struct sk_buff_head tnlq;
  1755. struct sk_buff *skb;
  1756. u32 dnode = l->addr;
  1757. __skb_queue_head_init(&tnlq);
  1758. skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
  1759. INT_H_SIZE, BASIC_H_SIZE,
  1760. dnode, onode, 0, 0, 0);
  1761. if (!skb) {
  1762. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1763. return;
  1764. }
  1765. hdr = buf_msg(skb);
  1766. msg_set_msgcnt(hdr, 1);
  1767. msg_set_bearer_id(hdr, l->peer_bearer_id);
  1768. ihdr = (struct tipc_msg *)msg_data(hdr);
  1769. tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1770. BASIC_H_SIZE, dnode);
  1771. msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
  1772. __skb_queue_tail(&tnlq, skb);
  1773. tipc_link_xmit(l, &tnlq, xmitq);
  1774. }
  1775. /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
  1776. * with contents of the link's transmit and backlog queues.
  1777. */
  1778. void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1779. int mtyp, struct sk_buff_head *xmitq)
  1780. {
  1781. struct sk_buff_head *fdefq = &tnl->failover_deferdq;
  1782. struct sk_buff *skb, *tnlskb;
  1783. struct tipc_msg *hdr, tnlhdr;
  1784. struct sk_buff_head *queue = &l->transmq;
  1785. struct sk_buff_head tmpxq, tnlq, frags;
  1786. u16 pktlen, pktcnt, seqno = l->snd_nxt;
  1787. bool pktcnt_need_update = false;
  1788. u16 syncpt;
  1789. int rc;
  1790. if (!tnl)
  1791. return;
  1792. __skb_queue_head_init(&tnlq);
  1793. /* Link Synching:
  1794. * From now on, send only one single ("dummy") SYNCH message
  1795. * to peer. The SYNCH message does not contain any data, just
  1796. * a header conveying the synch point to the peer.
  1797. */
  1798. if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
  1799. tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
  1800. INT_H_SIZE, 0, l->addr,
  1801. tipc_own_addr(l->net),
  1802. 0, 0, 0);
  1803. if (!tnlskb) {
  1804. pr_warn("%sunable to create dummy SYNCH_MSG\n",
  1805. link_co_err);
  1806. return;
  1807. }
  1808. hdr = buf_msg(tnlskb);
  1809. syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
  1810. msg_set_syncpt(hdr, syncpt);
  1811. msg_set_bearer_id(hdr, l->peer_bearer_id);
  1812. __skb_queue_tail(&tnlq, tnlskb);
  1813. tipc_link_xmit(tnl, &tnlq, xmitq);
  1814. return;
  1815. }
  1816. __skb_queue_head_init(&tmpxq);
  1817. __skb_queue_head_init(&frags);
  1818. /* At least one packet required for safe algorithm => add dummy */
  1819. skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
  1820. BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
  1821. 0, 0, TIPC_ERR_NO_PORT);
  1822. if (!skb) {
  1823. pr_warn("%sunable to create tunnel packet\n", link_co_err);
  1824. return;
  1825. }
  1826. __skb_queue_tail(&tnlq, skb);
  1827. tipc_link_xmit(l, &tnlq, &tmpxq);
  1828. __skb_queue_purge(&tmpxq);
  1829. /* Initialize reusable tunnel packet header */
  1830. tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
  1831. mtyp, INT_H_SIZE, l->addr);
  1832. if (mtyp == SYNCH_MSG)
  1833. pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
  1834. else
  1835. pktcnt = skb_queue_len(&l->transmq);
  1836. pktcnt += skb_queue_len(&l->backlogq);
  1837. msg_set_msgcnt(&tnlhdr, pktcnt);
  1838. msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
  1839. tnl:
  1840. /* Wrap each packet into a tunnel packet */
  1841. skb_queue_walk(queue, skb) {
  1842. hdr = buf_msg(skb);
  1843. if (queue == &l->backlogq)
  1844. msg_set_seqno(hdr, seqno++);
  1845. pktlen = msg_size(hdr);
  1846. /* Tunnel link MTU is not large enough? This could be
  1847. * due to:
  1848. * 1) Link MTU has just changed or set differently;
  1849. * 2) Or FAILOVER on the top of a SYNCH message
  1850. *
  1851. * The 2nd case should not happen if peer supports
  1852. * TIPC_TUNNEL_ENHANCED
  1853. */
  1854. if (pktlen > tnl->mtu - INT_H_SIZE) {
  1855. if (mtyp == FAILOVER_MSG &&
  1856. (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
  1857. rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
  1858. &frags);
  1859. if (rc) {
  1860. pr_warn("%sunable to frag msg: rc %d\n",
  1861. link_co_err, rc);
  1862. return;
  1863. }
  1864. pktcnt += skb_queue_len(&frags) - 1;
  1865. pktcnt_need_update = true;
  1866. skb_queue_splice_tail_init(&frags, &tnlq);
  1867. continue;
  1868. }
  1869. /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
  1870. * => Just warn it and return!
  1871. */
  1872. pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
  1873. link_co_err, msg_user(hdr),
  1874. msg_type(hdr), msg_size(hdr));
  1875. return;
  1876. }
  1877. msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
  1878. tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
  1879. if (!tnlskb) {
  1880. pr_warn("%sunable to send packet\n", link_co_err);
  1881. return;
  1882. }
  1883. skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
  1884. skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
  1885. __skb_queue_tail(&tnlq, tnlskb);
  1886. }
  1887. if (queue != &l->backlogq) {
  1888. queue = &l->backlogq;
  1889. goto tnl;
  1890. }
  1891. if (pktcnt_need_update)
  1892. skb_queue_walk(&tnlq, skb) {
  1893. hdr = buf_msg(skb);
  1894. msg_set_msgcnt(hdr, pktcnt);
  1895. }
  1896. tipc_link_xmit(tnl, &tnlq, xmitq);
  1897. if (mtyp == FAILOVER_MSG) {
  1898. tnl->drop_point = l->rcv_nxt;
  1899. tnl->failover_reasm_skb = l->reasm_buf;
  1900. l->reasm_buf = NULL;
  1901. /* Failover the link's deferdq */
  1902. if (unlikely(!skb_queue_empty(fdefq))) {
  1903. pr_warn("Link failover deferdq not empty: %d!\n",
  1904. skb_queue_len(fdefq));
  1905. __skb_queue_purge(fdefq);
  1906. }
  1907. skb_queue_splice_init(&l->deferdq, fdefq);
  1908. }
  1909. }
  1910. /**
  1911. * tipc_link_failover_prepare() - prepare tnl for link failover
  1912. *
  1913. * This is a special version of the precursor - tipc_link_tnl_prepare(),
  1914. * see the tipc_node_link_failover() for details
  1915. *
  1916. * @l: failover link
  1917. * @tnl: tunnel link
  1918. * @xmitq: queue for messages to be xmited
  1919. */
  1920. void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
  1921. struct sk_buff_head *xmitq)
  1922. {
  1923. struct sk_buff_head *fdefq = &tnl->failover_deferdq;
  1924. tipc_link_create_dummy_tnl_msg(tnl, xmitq);
  1925. /* This failover link endpoint was never established before,
  1926. * so it has not received anything from peer.
  1927. * Otherwise, it must be a normal failover situation or the
  1928. * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
  1929. * would have to start over from scratch instead.
  1930. */
  1931. tnl->drop_point = 1;
  1932. tnl->failover_reasm_skb = NULL;
  1933. /* Initiate the link's failover deferdq */
  1934. if (unlikely(!skb_queue_empty(fdefq))) {
  1935. pr_warn("Link failover deferdq not empty: %d!\n",
  1936. skb_queue_len(fdefq));
  1937. __skb_queue_purge(fdefq);
  1938. }
  1939. }
  1940. /* tipc_link_validate_msg(): validate message against current link state
  1941. * Returns true if message should be accepted, otherwise false
  1942. */
  1943. bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
  1944. {
  1945. u16 curr_session = l->peer_session;
  1946. u16 session = msg_session(hdr);
  1947. int mtyp = msg_type(hdr);
  1948. if (msg_user(hdr) != LINK_PROTOCOL)
  1949. return true;
  1950. switch (mtyp) {
  1951. case RESET_MSG:
  1952. if (!l->in_session)
  1953. return true;
  1954. /* Accept only RESET with new session number */
  1955. return more(session, curr_session);
  1956. case ACTIVATE_MSG:
  1957. if (!l->in_session)
  1958. return true;
  1959. /* Accept only ACTIVATE with new or current session number */
  1960. return !less(session, curr_session);
  1961. case STATE_MSG:
  1962. /* Accept only STATE with current session number */
  1963. if (!l->in_session)
  1964. return false;
  1965. if (session != curr_session)
  1966. return false;
  1967. /* Extra sanity check */
  1968. if (!tipc_link_is_up(l) && msg_ack(hdr))
  1969. return false;
  1970. if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
  1971. return true;
  1972. /* Accept only STATE with new sequence number */
  1973. return !less(msg_seqno(hdr), l->rcv_nxt_state);
  1974. default:
  1975. return false;
  1976. }
  1977. }
  1978. /* tipc_link_proto_rcv(): receive link level protocol message :
  1979. * Note that network plane id propagates through the network, and may
  1980. * change at any time. The node with lowest numerical id determines
  1981. * network plane
  1982. */
  1983. static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
  1984. struct sk_buff_head *xmitq)
  1985. {
  1986. struct tipc_msg *hdr = buf_msg(skb);
  1987. struct tipc_gap_ack_blks *ga = NULL;
  1988. bool reply = msg_probe(hdr), retransmitted = false;
  1989. u32 dlen = msg_data_sz(hdr), glen = 0, msg_max;
  1990. u16 peers_snd_nxt = msg_next_sent(hdr);
  1991. u16 peers_tol = msg_link_tolerance(hdr);
  1992. u16 peers_prio = msg_linkprio(hdr);
  1993. u16 gap = msg_seq_gap(hdr);
  1994. u16 ack = msg_ack(hdr);
  1995. u16 rcv_nxt = l->rcv_nxt;
  1996. u16 rcvgap = 0;
  1997. int mtyp = msg_type(hdr);
  1998. int rc = 0, released;
  1999. char *if_name;
  2000. void *data;
  2001. trace_tipc_proto_rcv(skb, false, l->name);
  2002. if (dlen > U16_MAX)
  2003. goto exit;
  2004. if (tipc_link_is_blocked(l) || !xmitq)
  2005. goto exit;
  2006. if (tipc_own_addr(l->net) > msg_prevnode(hdr))
  2007. l->net_plane = msg_net_plane(hdr);
  2008. if (skb_linearize(skb))
  2009. goto exit;
  2010. hdr = buf_msg(skb);
  2011. data = msg_data(hdr);
  2012. if (!tipc_link_validate_msg(l, hdr)) {
  2013. trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
  2014. trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
  2015. goto exit;
  2016. }
  2017. switch (mtyp) {
  2018. case RESET_MSG:
  2019. case ACTIVATE_MSG:
  2020. msg_max = msg_max_pkt(hdr);
  2021. if (msg_max < tipc_bearer_min_mtu(l->net, l->bearer_id))
  2022. break;
  2023. /* Complete own link name with peer's interface name */
  2024. if_name = strrchr(l->name, ':') + 1;
  2025. if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
  2026. break;
  2027. if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
  2028. break;
  2029. strncpy(if_name, data, TIPC_MAX_IF_NAME);
  2030. /* Update own tolerance if peer indicates a non-zero value */
  2031. if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  2032. l->tolerance = peers_tol;
  2033. l->bc_rcvlink->tolerance = peers_tol;
  2034. }
  2035. /* Update own priority if peer's priority is higher */
  2036. if (tipc_in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
  2037. l->priority = peers_prio;
  2038. /* If peer is going down we want full re-establish cycle */
  2039. if (msg_peer_stopping(hdr)) {
  2040. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  2041. break;
  2042. }
  2043. /* If this endpoint was re-created while peer was ESTABLISHING
  2044. * it doesn't know current session number. Force re-synch.
  2045. */
  2046. if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
  2047. l->session != msg_dest_session(hdr)) {
  2048. if (less(l->session, msg_dest_session(hdr)))
  2049. l->session = msg_dest_session(hdr) + 1;
  2050. break;
  2051. }
  2052. /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
  2053. if (mtyp == RESET_MSG || !tipc_link_is_up(l))
  2054. rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
  2055. /* ACTIVATE_MSG takes up link if it was already locally reset */
  2056. if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
  2057. rc = TIPC_LINK_UP_EVT;
  2058. l->peer_session = msg_session(hdr);
  2059. l->in_session = true;
  2060. l->peer_bearer_id = msg_bearer_id(hdr);
  2061. if (l->mtu > msg_max)
  2062. l->mtu = msg_max;
  2063. break;
  2064. case STATE_MSG:
  2065. /* Validate Gap ACK blocks, drop if invalid */
  2066. glen = tipc_get_gap_ack_blks(&ga, l, hdr, true);
  2067. if (glen > dlen)
  2068. break;
  2069. l->rcv_nxt_state = msg_seqno(hdr) + 1;
  2070. /* Update own tolerance if peer indicates a non-zero value */
  2071. if (tipc_in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
  2072. l->tolerance = peers_tol;
  2073. l->bc_rcvlink->tolerance = peers_tol;
  2074. }
  2075. /* Update own prio if peer indicates a different value */
  2076. if ((peers_prio != l->priority) &&
  2077. tipc_in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
  2078. l->priority = peers_prio;
  2079. rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
  2080. }
  2081. l->silent_intv_cnt = 0;
  2082. l->stats.recv_states++;
  2083. if (msg_probe(hdr))
  2084. l->stats.recv_probes++;
  2085. if (!tipc_link_is_up(l)) {
  2086. if (l->state == LINK_ESTABLISHING)
  2087. rc = TIPC_LINK_UP_EVT;
  2088. break;
  2089. }
  2090. tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
  2091. &l->mon_state, l->bearer_id);
  2092. /* Send NACK if peer has sent pkts we haven't received yet */
  2093. if ((reply || msg_is_keepalive(hdr)) &&
  2094. more(peers_snd_nxt, rcv_nxt) &&
  2095. !tipc_link_is_synching(l) &&
  2096. skb_queue_empty(&l->deferdq))
  2097. rcvgap = peers_snd_nxt - l->rcv_nxt;
  2098. if (rcvgap || reply)
  2099. tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
  2100. rcvgap, 0, 0, xmitq);
  2101. released = tipc_link_advance_transmq(l, l, ack, gap, ga, xmitq,
  2102. &retransmitted, &rc);
  2103. if (gap)
  2104. l->stats.recv_nacks++;
  2105. if (released || retransmitted)
  2106. tipc_link_update_cwin(l, released, retransmitted);
  2107. if (released)
  2108. tipc_link_advance_backlog(l, xmitq);
  2109. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  2110. link_prepare_wakeup(l);
  2111. }
  2112. exit:
  2113. kfree_skb(skb);
  2114. return rc;
  2115. }
  2116. /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
  2117. */
  2118. static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
  2119. u16 peers_snd_nxt,
  2120. struct sk_buff_head *xmitq)
  2121. {
  2122. struct sk_buff *skb;
  2123. struct tipc_msg *hdr;
  2124. struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
  2125. u16 ack = l->rcv_nxt - 1;
  2126. u16 gap_to = peers_snd_nxt - 1;
  2127. skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
  2128. 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
  2129. if (!skb)
  2130. return false;
  2131. hdr = buf_msg(skb);
  2132. msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
  2133. msg_set_bcast_ack(hdr, ack);
  2134. msg_set_bcgap_after(hdr, ack);
  2135. if (dfrd_skb)
  2136. gap_to = buf_seqno(dfrd_skb) - 1;
  2137. msg_set_bcgap_to(hdr, gap_to);
  2138. msg_set_non_seq(hdr, bcast);
  2139. __skb_queue_tail(xmitq, skb);
  2140. return true;
  2141. }
  2142. /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
  2143. *
  2144. * Give a newly added peer node the sequence number where it should
  2145. * start receiving and acking broadcast packets.
  2146. */
  2147. static void tipc_link_build_bc_init_msg(struct tipc_link *l,
  2148. struct sk_buff_head *xmitq)
  2149. {
  2150. struct sk_buff_head list;
  2151. __skb_queue_head_init(&list);
  2152. if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
  2153. return;
  2154. msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
  2155. tipc_link_xmit(l, &list, xmitq);
  2156. }
  2157. /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
  2158. */
  2159. void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
  2160. {
  2161. int mtyp = msg_type(hdr);
  2162. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  2163. if (tipc_link_is_up(l))
  2164. return;
  2165. if (msg_user(hdr) == BCAST_PROTOCOL) {
  2166. l->rcv_nxt = peers_snd_nxt;
  2167. l->state = LINK_ESTABLISHED;
  2168. return;
  2169. }
  2170. if (l->peer_caps & TIPC_BCAST_SYNCH)
  2171. return;
  2172. if (msg_peer_node_is_up(hdr))
  2173. return;
  2174. /* Compatibility: accept older, less safe initial synch data */
  2175. if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
  2176. l->rcv_nxt = peers_snd_nxt;
  2177. }
  2178. /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
  2179. */
  2180. int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
  2181. struct sk_buff_head *xmitq)
  2182. {
  2183. u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
  2184. int rc = 0;
  2185. if (!tipc_link_is_up(l))
  2186. return rc;
  2187. if (!msg_peer_node_is_up(hdr))
  2188. return rc;
  2189. /* Open when peer acknowledges our bcast init msg (pkt #1) */
  2190. if (msg_ack(hdr))
  2191. l->bc_peer_is_up = true;
  2192. if (!l->bc_peer_is_up)
  2193. return rc;
  2194. /* Ignore if peers_snd_nxt goes beyond receive window */
  2195. if (more(peers_snd_nxt, l->rcv_nxt + l->window))
  2196. return rc;
  2197. l->snd_nxt = peers_snd_nxt;
  2198. if (link_bc_rcv_gap(l))
  2199. rc |= TIPC_LINK_SND_STATE;
  2200. /* Return now if sender supports nack via STATE messages */
  2201. if (l->peer_caps & TIPC_BCAST_STATE_NACK)
  2202. return rc;
  2203. /* Otherwise, be backwards compatible */
  2204. if (!more(peers_snd_nxt, l->rcv_nxt)) {
  2205. l->nack_state = BC_NACK_SND_CONDITIONAL;
  2206. return 0;
  2207. }
  2208. /* Don't NACK if one was recently sent or peeked */
  2209. if (l->nack_state == BC_NACK_SND_SUPPRESS) {
  2210. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  2211. return 0;
  2212. }
  2213. /* Conditionally delay NACK sending until next synch rcv */
  2214. if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
  2215. l->nack_state = BC_NACK_SND_UNCONDITIONAL;
  2216. if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
  2217. return 0;
  2218. }
  2219. /* Send NACK now but suppress next one */
  2220. tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
  2221. l->nack_state = BC_NACK_SND_SUPPRESS;
  2222. return 0;
  2223. }
  2224. int tipc_link_bc_ack_rcv(struct tipc_link *r, u16 acked, u16 gap,
  2225. struct tipc_gap_ack_blks *ga,
  2226. struct sk_buff_head *xmitq,
  2227. struct sk_buff_head *retrq)
  2228. {
  2229. struct tipc_link *l = r->bc_sndlink;
  2230. bool unused = false;
  2231. int rc = 0;
  2232. if (!tipc_link_is_up(r) || !r->bc_peer_is_up)
  2233. return 0;
  2234. if (gap) {
  2235. l->stats.recv_nacks++;
  2236. r->stats.recv_nacks++;
  2237. }
  2238. if (less(acked, r->acked) || (acked == r->acked && !gap && !ga))
  2239. return 0;
  2240. trace_tipc_link_bc_ack(r, acked, gap, &l->transmq);
  2241. tipc_link_advance_transmq(l, r, acked, gap, ga, retrq, &unused, &rc);
  2242. tipc_link_advance_backlog(l, xmitq);
  2243. if (unlikely(!skb_queue_empty(&l->wakeupq)))
  2244. link_prepare_wakeup(l);
  2245. return rc;
  2246. }
  2247. /* tipc_link_bc_nack_rcv(): receive broadcast nack message
  2248. * This function is here for backwards compatibility, since
  2249. * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
  2250. */
  2251. int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
  2252. struct sk_buff_head *xmitq)
  2253. {
  2254. struct tipc_msg *hdr = buf_msg(skb);
  2255. u32 dnode = msg_destnode(hdr);
  2256. int mtyp = msg_type(hdr);
  2257. u16 acked = msg_bcast_ack(hdr);
  2258. u16 from = acked + 1;
  2259. u16 to = msg_bcgap_to(hdr);
  2260. u16 peers_snd_nxt = to + 1;
  2261. int rc = 0;
  2262. kfree_skb(skb);
  2263. if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
  2264. return 0;
  2265. if (mtyp != STATE_MSG)
  2266. return 0;
  2267. if (dnode == tipc_own_addr(l->net)) {
  2268. rc = tipc_link_bc_ack_rcv(l, acked, to - acked, NULL, xmitq,
  2269. xmitq);
  2270. l->stats.recv_nacks++;
  2271. return rc;
  2272. }
  2273. /* Msg for other node => suppress own NACK at next sync if applicable */
  2274. if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
  2275. l->nack_state = BC_NACK_SND_SUPPRESS;
  2276. return 0;
  2277. }
  2278. void tipc_link_set_queue_limits(struct tipc_link *l, u32 min_win, u32 max_win)
  2279. {
  2280. int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
  2281. l->min_win = min_win;
  2282. l->ssthresh = max_win;
  2283. l->max_win = max_win;
  2284. l->window = min_win;
  2285. l->backlog[TIPC_LOW_IMPORTANCE].limit = min_win * 2;
  2286. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = min_win * 4;
  2287. l->backlog[TIPC_HIGH_IMPORTANCE].limit = min_win * 6;
  2288. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = min_win * 8;
  2289. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  2290. }
  2291. /**
  2292. * tipc_link_reset_stats - reset link statistics
  2293. * @l: pointer to link
  2294. */
  2295. void tipc_link_reset_stats(struct tipc_link *l)
  2296. {
  2297. memset(&l->stats, 0, sizeof(l->stats));
  2298. }
  2299. static void link_print(struct tipc_link *l, const char *str)
  2300. {
  2301. struct sk_buff *hskb = skb_peek(&l->transmq);
  2302. u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
  2303. u16 tail = l->snd_nxt - 1;
  2304. pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
  2305. pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
  2306. skb_queue_len(&l->transmq), head, tail,
  2307. skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
  2308. }
  2309. /* Parse and validate nested (link) properties valid for media, bearer and link
  2310. */
  2311. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  2312. {
  2313. int err;
  2314. err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
  2315. tipc_nl_prop_policy, NULL);
  2316. if (err)
  2317. return err;
  2318. if (props[TIPC_NLA_PROP_PRIO]) {
  2319. u32 prio;
  2320. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2321. if (prio > TIPC_MAX_LINK_PRI)
  2322. return -EINVAL;
  2323. }
  2324. if (props[TIPC_NLA_PROP_TOL]) {
  2325. u32 tol;
  2326. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2327. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  2328. return -EINVAL;
  2329. }
  2330. if (props[TIPC_NLA_PROP_WIN]) {
  2331. u32 max_win;
  2332. max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2333. if (max_win < TIPC_DEF_LINK_WIN || max_win > TIPC_MAX_LINK_WIN)
  2334. return -EINVAL;
  2335. }
  2336. return 0;
  2337. }
  2338. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  2339. {
  2340. int i;
  2341. struct nlattr *stats;
  2342. struct nla_map {
  2343. u32 key;
  2344. u32 val;
  2345. };
  2346. struct nla_map map[] = {
  2347. {TIPC_NLA_STATS_RX_INFO, 0},
  2348. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  2349. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  2350. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  2351. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  2352. {TIPC_NLA_STATS_TX_INFO, 0},
  2353. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  2354. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  2355. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  2356. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  2357. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  2358. s->msg_length_counts : 1},
  2359. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  2360. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  2361. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  2362. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  2363. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  2364. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  2365. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  2366. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  2367. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  2368. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  2369. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  2370. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  2371. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  2372. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  2373. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  2374. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  2375. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  2376. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  2377. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  2378. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  2379. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  2380. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  2381. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  2382. };
  2383. stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
  2384. if (!stats)
  2385. return -EMSGSIZE;
  2386. for (i = 0; i < ARRAY_SIZE(map); i++)
  2387. if (nla_put_u32(skb, map[i].key, map[i].val))
  2388. goto msg_full;
  2389. nla_nest_end(skb, stats);
  2390. return 0;
  2391. msg_full:
  2392. nla_nest_cancel(skb, stats);
  2393. return -EMSGSIZE;
  2394. }
  2395. /* Caller should hold appropriate locks to protect the link */
  2396. int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  2397. struct tipc_link *link, int nlflags)
  2398. {
  2399. u32 self = tipc_own_addr(net);
  2400. struct nlattr *attrs;
  2401. struct nlattr *prop;
  2402. void *hdr;
  2403. int err;
  2404. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  2405. nlflags, TIPC_NL_LINK_GET);
  2406. if (!hdr)
  2407. return -EMSGSIZE;
  2408. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
  2409. if (!attrs)
  2410. goto msg_full;
  2411. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  2412. goto attr_msg_full;
  2413. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
  2414. goto attr_msg_full;
  2415. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  2416. goto attr_msg_full;
  2417. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
  2418. goto attr_msg_full;
  2419. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
  2420. goto attr_msg_full;
  2421. if (tipc_link_is_up(link))
  2422. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2423. goto attr_msg_full;
  2424. if (link->active)
  2425. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  2426. goto attr_msg_full;
  2427. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
  2428. if (!prop)
  2429. goto attr_msg_full;
  2430. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2431. goto prop_msg_full;
  2432. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  2433. goto prop_msg_full;
  2434. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  2435. link->window))
  2436. goto prop_msg_full;
  2437. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2438. goto prop_msg_full;
  2439. nla_nest_end(msg->skb, prop);
  2440. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  2441. if (err)
  2442. goto attr_msg_full;
  2443. nla_nest_end(msg->skb, attrs);
  2444. genlmsg_end(msg->skb, hdr);
  2445. return 0;
  2446. prop_msg_full:
  2447. nla_nest_cancel(msg->skb, prop);
  2448. attr_msg_full:
  2449. nla_nest_cancel(msg->skb, attrs);
  2450. msg_full:
  2451. genlmsg_cancel(msg->skb, hdr);
  2452. return -EMSGSIZE;
  2453. }
  2454. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  2455. struct tipc_stats *stats)
  2456. {
  2457. int i;
  2458. struct nlattr *nest;
  2459. struct nla_map {
  2460. __u32 key;
  2461. __u32 val;
  2462. };
  2463. struct nla_map map[] = {
  2464. {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
  2465. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  2466. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  2467. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  2468. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  2469. {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
  2470. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  2471. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  2472. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  2473. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  2474. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  2475. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  2476. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  2477. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  2478. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  2479. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  2480. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  2481. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  2482. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  2483. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  2484. };
  2485. nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
  2486. if (!nest)
  2487. return -EMSGSIZE;
  2488. for (i = 0; i < ARRAY_SIZE(map); i++)
  2489. if (nla_put_u32(skb, map[i].key, map[i].val))
  2490. goto msg_full;
  2491. nla_nest_end(skb, nest);
  2492. return 0;
  2493. msg_full:
  2494. nla_nest_cancel(skb, nest);
  2495. return -EMSGSIZE;
  2496. }
  2497. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg,
  2498. struct tipc_link *bcl)
  2499. {
  2500. int err;
  2501. void *hdr;
  2502. struct nlattr *attrs;
  2503. struct nlattr *prop;
  2504. u32 bc_mode = tipc_bcast_get_mode(net);
  2505. u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
  2506. if (!bcl)
  2507. return 0;
  2508. tipc_bcast_lock(net);
  2509. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  2510. NLM_F_MULTI, TIPC_NL_LINK_GET);
  2511. if (!hdr) {
  2512. tipc_bcast_unlock(net);
  2513. return -EMSGSIZE;
  2514. }
  2515. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
  2516. if (!attrs)
  2517. goto msg_full;
  2518. /* The broadcast link is always up */
  2519. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2520. goto attr_msg_full;
  2521. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  2522. goto attr_msg_full;
  2523. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  2524. goto attr_msg_full;
  2525. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
  2526. goto attr_msg_full;
  2527. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
  2528. goto attr_msg_full;
  2529. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
  2530. if (!prop)
  2531. goto attr_msg_full;
  2532. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->max_win))
  2533. goto prop_msg_full;
  2534. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
  2535. goto prop_msg_full;
  2536. if (bc_mode & BCLINK_MODE_SEL)
  2537. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
  2538. bc_ratio))
  2539. goto prop_msg_full;
  2540. nla_nest_end(msg->skb, prop);
  2541. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  2542. if (err)
  2543. goto attr_msg_full;
  2544. tipc_bcast_unlock(net);
  2545. nla_nest_end(msg->skb, attrs);
  2546. genlmsg_end(msg->skb, hdr);
  2547. return 0;
  2548. prop_msg_full:
  2549. nla_nest_cancel(msg->skb, prop);
  2550. attr_msg_full:
  2551. nla_nest_cancel(msg->skb, attrs);
  2552. msg_full:
  2553. tipc_bcast_unlock(net);
  2554. genlmsg_cancel(msg->skb, hdr);
  2555. return -EMSGSIZE;
  2556. }
  2557. void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
  2558. struct sk_buff_head *xmitq)
  2559. {
  2560. l->tolerance = tol;
  2561. if (l->bc_rcvlink)
  2562. l->bc_rcvlink->tolerance = tol;
  2563. if (tipc_link_is_up(l))
  2564. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
  2565. }
  2566. void tipc_link_set_prio(struct tipc_link *l, u32 prio,
  2567. struct sk_buff_head *xmitq)
  2568. {
  2569. l->priority = prio;
  2570. tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
  2571. }
  2572. void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
  2573. {
  2574. l->abort_limit = limit;
  2575. }
  2576. /**
  2577. * tipc_link_dump - dump TIPC link data
  2578. * @l: tipc link to be dumped
  2579. * @dqueues: bitmask to decide if any link queue to be dumped?
  2580. * - TIPC_DUMP_NONE: don't dump link queues
  2581. * - TIPC_DUMP_TRANSMQ: dump link transmq queue
  2582. * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
  2583. * - TIPC_DUMP_DEFERDQ: dump link deferd queue
  2584. * - TIPC_DUMP_INPUTQ: dump link input queue
  2585. * - TIPC_DUMP_WAKEUP: dump link wakeup queue
  2586. * - TIPC_DUMP_ALL: dump all the link queues above
  2587. * @buf: returned buffer of dump data in format
  2588. */
  2589. int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
  2590. {
  2591. int i = 0;
  2592. size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
  2593. struct sk_buff_head *list;
  2594. struct sk_buff *hskb, *tskb;
  2595. u32 len;
  2596. if (!l) {
  2597. i += scnprintf(buf, sz, "link data: (null)\n");
  2598. return i;
  2599. }
  2600. i += scnprintf(buf, sz, "link data: %x", l->addr);
  2601. i += scnprintf(buf + i, sz - i, " %x", l->state);
  2602. i += scnprintf(buf + i, sz - i, " %u", l->in_session);
  2603. i += scnprintf(buf + i, sz - i, " %u", l->session);
  2604. i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
  2605. i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
  2606. i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
  2607. i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
  2608. i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
  2609. i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
  2610. i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
  2611. i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
  2612. i += scnprintf(buf + i, sz - i, " %u", 0);
  2613. i += scnprintf(buf + i, sz - i, " %u", 0);
  2614. i += scnprintf(buf + i, sz - i, " %u", l->acked);
  2615. list = &l->transmq;
  2616. len = skb_queue_len(list);
  2617. hskb = skb_peek(list);
  2618. tskb = skb_peek_tail(list);
  2619. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2620. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2621. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2622. list = &l->deferdq;
  2623. len = skb_queue_len(list);
  2624. hskb = skb_peek(list);
  2625. tskb = skb_peek_tail(list);
  2626. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2627. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2628. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2629. list = &l->backlogq;
  2630. len = skb_queue_len(list);
  2631. hskb = skb_peek(list);
  2632. tskb = skb_peek_tail(list);
  2633. i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
  2634. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2635. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2636. list = l->inputq;
  2637. len = skb_queue_len(list);
  2638. hskb = skb_peek(list);
  2639. tskb = skb_peek_tail(list);
  2640. i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
  2641. (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
  2642. (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
  2643. if (dqueues & TIPC_DUMP_TRANSMQ) {
  2644. i += scnprintf(buf + i, sz - i, "transmq: ");
  2645. i += tipc_list_dump(&l->transmq, false, buf + i);
  2646. }
  2647. if (dqueues & TIPC_DUMP_BACKLOGQ) {
  2648. i += scnprintf(buf + i, sz - i,
  2649. "backlogq: <%u %u %u %u %u>, ",
  2650. l->backlog[TIPC_LOW_IMPORTANCE].len,
  2651. l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
  2652. l->backlog[TIPC_HIGH_IMPORTANCE].len,
  2653. l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
  2654. l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
  2655. i += tipc_list_dump(&l->backlogq, false, buf + i);
  2656. }
  2657. if (dqueues & TIPC_DUMP_DEFERDQ) {
  2658. i += scnprintf(buf + i, sz - i, "deferdq: ");
  2659. i += tipc_list_dump(&l->deferdq, false, buf + i);
  2660. }
  2661. if (dqueues & TIPC_DUMP_INPUTQ) {
  2662. i += scnprintf(buf + i, sz - i, "inputq: ");
  2663. i += tipc_list_dump(l->inputq, false, buf + i);
  2664. }
  2665. if (dqueues & TIPC_DUMP_WAKEUP) {
  2666. i += scnprintf(buf + i, sz - i, "wakeup: ");
  2667. i += tipc_list_dump(&l->wakeupq, false, buf + i);
  2668. }
  2669. return i;
  2670. }