link.c 57 KB

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