link.c 82 KB

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