link.c 83 KB

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