link.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2014, 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 "link.h"
  38. #include "port.h"
  39. #include "name_distr.h"
  40. #include "discover.h"
  41. #include "config.h"
  42. #include <linux/pkt_sched.h>
  43. /*
  44. * Error message prefixes
  45. */
  46. static const char *link_co_err = "Link changeover error, ";
  47. static const char *link_rst_msg = "Resetting link ";
  48. static const char *link_unk_evt = "Unknown link event ";
  49. /*
  50. * Out-of-range value for link session numbers
  51. */
  52. #define INVALID_SESSION 0x10000
  53. /*
  54. * Link state events:
  55. */
  56. #define STARTING_EVT 856384768 /* link processing trigger */
  57. #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
  58. #define TIMEOUT_EVT 560817u /* link timer expired */
  59. /*
  60. * The following two 'message types' is really just implementation
  61. * data conveniently stored in the message header.
  62. * They must not be considered part of the protocol
  63. */
  64. #define OPEN_MSG 0
  65. #define CLOSED_MSG 1
  66. /*
  67. * State value stored in 'exp_msg_count'
  68. */
  69. #define START_CHANGEOVER 100000u
  70. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  71. struct sk_buff *buf);
  72. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
  73. static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
  74. struct sk_buff **buf);
  75. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
  76. static int link_send_sections_long(struct tipc_port *sender,
  77. struct iovec const *msg_sect,
  78. unsigned int len, u32 destnode);
  79. static void link_state_event(struct tipc_link *l_ptr, u32 event);
  80. static void link_reset_statistics(struct tipc_link *l_ptr);
  81. static void link_print(struct tipc_link *l_ptr, const char *str);
  82. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
  83. static void tipc_link_send_sync(struct tipc_link *l);
  84. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
  85. /*
  86. * Simple link routines
  87. */
  88. static unsigned int align(unsigned int i)
  89. {
  90. return (i + 3) & ~3u;
  91. }
  92. static void link_init_max_pkt(struct tipc_link *l_ptr)
  93. {
  94. u32 max_pkt;
  95. max_pkt = (l_ptr->b_ptr->mtu & ~3);
  96. if (max_pkt > MAX_MSG_SIZE)
  97. max_pkt = MAX_MSG_SIZE;
  98. l_ptr->max_pkt_target = max_pkt;
  99. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  100. l_ptr->max_pkt = l_ptr->max_pkt_target;
  101. else
  102. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  103. l_ptr->max_pkt_probes = 0;
  104. }
  105. static u32 link_next_sent(struct tipc_link *l_ptr)
  106. {
  107. if (l_ptr->next_out)
  108. return buf_seqno(l_ptr->next_out);
  109. return mod(l_ptr->next_out_no);
  110. }
  111. static u32 link_last_sent(struct tipc_link *l_ptr)
  112. {
  113. return mod(link_next_sent(l_ptr) - 1);
  114. }
  115. /*
  116. * Simple non-static link routines (i.e. referenced outside this file)
  117. */
  118. int tipc_link_is_up(struct tipc_link *l_ptr)
  119. {
  120. if (!l_ptr)
  121. return 0;
  122. return link_working_working(l_ptr) || link_working_unknown(l_ptr);
  123. }
  124. int tipc_link_is_active(struct tipc_link *l_ptr)
  125. {
  126. return (l_ptr->owner->active_links[0] == l_ptr) ||
  127. (l_ptr->owner->active_links[1] == l_ptr);
  128. }
  129. /**
  130. * link_timeout - handle expiration of link timer
  131. * @l_ptr: pointer to link
  132. *
  133. * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
  134. * with tipc_link_delete(). (There is no risk that the node will be deleted by
  135. * another thread because tipc_link_delete() always cancels the link timer before
  136. * tipc_node_delete() is called.)
  137. */
  138. static void link_timeout(struct tipc_link *l_ptr)
  139. {
  140. tipc_node_lock(l_ptr->owner);
  141. /* update counters used in statistical profiling of send traffic */
  142. l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
  143. l_ptr->stats.queue_sz_counts++;
  144. if (l_ptr->first_out) {
  145. struct tipc_msg *msg = buf_msg(l_ptr->first_out);
  146. u32 length = msg_size(msg);
  147. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  148. (msg_type(msg) == FIRST_FRAGMENT)) {
  149. length = msg_size(msg_get_wrapped(msg));
  150. }
  151. if (length) {
  152. l_ptr->stats.msg_lengths_total += length;
  153. l_ptr->stats.msg_length_counts++;
  154. if (length <= 64)
  155. l_ptr->stats.msg_length_profile[0]++;
  156. else if (length <= 256)
  157. l_ptr->stats.msg_length_profile[1]++;
  158. else if (length <= 1024)
  159. l_ptr->stats.msg_length_profile[2]++;
  160. else if (length <= 4096)
  161. l_ptr->stats.msg_length_profile[3]++;
  162. else if (length <= 16384)
  163. l_ptr->stats.msg_length_profile[4]++;
  164. else if (length <= 32768)
  165. l_ptr->stats.msg_length_profile[5]++;
  166. else
  167. l_ptr->stats.msg_length_profile[6]++;
  168. }
  169. }
  170. /* do all other link processing performed on a periodic basis */
  171. link_state_event(l_ptr, TIMEOUT_EVT);
  172. if (l_ptr->next_out)
  173. tipc_link_push_queue(l_ptr);
  174. tipc_node_unlock(l_ptr->owner);
  175. }
  176. static void link_set_timer(struct tipc_link *l_ptr, u32 time)
  177. {
  178. k_start_timer(&l_ptr->timer, time);
  179. }
  180. /**
  181. * tipc_link_create - create a new link
  182. * @n_ptr: pointer to associated node
  183. * @b_ptr: pointer to associated bearer
  184. * @media_addr: media address to use when sending messages over link
  185. *
  186. * Returns pointer to link.
  187. */
  188. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  189. struct tipc_bearer *b_ptr,
  190. const struct tipc_media_addr *media_addr)
  191. {
  192. struct tipc_link *l_ptr;
  193. struct tipc_msg *msg;
  194. char *if_name;
  195. char addr_string[16];
  196. u32 peer = n_ptr->addr;
  197. if (n_ptr->link_cnt >= 2) {
  198. tipc_addr_string_fill(addr_string, n_ptr->addr);
  199. pr_err("Attempt to establish third link to %s\n", addr_string);
  200. return NULL;
  201. }
  202. if (n_ptr->links[b_ptr->identity]) {
  203. tipc_addr_string_fill(addr_string, n_ptr->addr);
  204. pr_err("Attempt to establish second link on <%s> to %s\n",
  205. b_ptr->name, addr_string);
  206. return NULL;
  207. }
  208. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  209. if (!l_ptr) {
  210. pr_warn("Link creation failed, no memory\n");
  211. return NULL;
  212. }
  213. l_ptr->addr = peer;
  214. if_name = strchr(b_ptr->name, ':') + 1;
  215. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  216. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  217. tipc_node(tipc_own_addr),
  218. if_name,
  219. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  220. /* note: peer i/f name is updated by reset/activate message */
  221. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  222. l_ptr->owner = n_ptr;
  223. l_ptr->checkpoint = 1;
  224. l_ptr->peer_session = INVALID_SESSION;
  225. l_ptr->b_ptr = b_ptr;
  226. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  227. l_ptr->state = RESET_UNKNOWN;
  228. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  229. msg = l_ptr->pmsg;
  230. tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
  231. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  232. msg_set_session(msg, (tipc_random & 0xffff));
  233. msg_set_bearer_id(msg, b_ptr->identity);
  234. strcpy((char *)msg_data(msg), if_name);
  235. l_ptr->priority = b_ptr->priority;
  236. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  237. link_init_max_pkt(l_ptr);
  238. l_ptr->next_out_no = 1;
  239. INIT_LIST_HEAD(&l_ptr->waiting_ports);
  240. link_reset_statistics(l_ptr);
  241. tipc_node_attach_link(n_ptr, l_ptr);
  242. k_init_timer(&l_ptr->timer, (Handler)link_timeout,
  243. (unsigned long)l_ptr);
  244. list_add_tail(&l_ptr->link_list, &b_ptr->links);
  245. link_state_event(l_ptr, STARTING_EVT);
  246. return l_ptr;
  247. }
  248. /**
  249. * tipc_link_delete - delete a link
  250. * @l_ptr: pointer to link
  251. *
  252. * Note: 'tipc_net_lock' is write_locked, bearer is locked.
  253. * This routine must not grab the node lock until after link timer cancellation
  254. * to avoid a potential deadlock situation.
  255. */
  256. void tipc_link_delete(struct tipc_link *l_ptr)
  257. {
  258. if (!l_ptr) {
  259. pr_err("Attempt to delete non-existent link\n");
  260. return;
  261. }
  262. k_cancel_timer(&l_ptr->timer);
  263. tipc_node_lock(l_ptr->owner);
  264. tipc_link_reset(l_ptr);
  265. tipc_node_detach_link(l_ptr->owner, l_ptr);
  266. tipc_link_purge_queues(l_ptr);
  267. list_del_init(&l_ptr->link_list);
  268. tipc_node_unlock(l_ptr->owner);
  269. k_term_timer(&l_ptr->timer);
  270. kfree(l_ptr);
  271. }
  272. /**
  273. * link_schedule_port - schedule port for deferred sending
  274. * @l_ptr: pointer to link
  275. * @origport: reference to sending port
  276. * @sz: amount of data to be sent
  277. *
  278. * Schedules port for renewed sending of messages after link congestion
  279. * has abated.
  280. */
  281. static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
  282. {
  283. struct tipc_port *p_ptr;
  284. spin_lock_bh(&tipc_port_list_lock);
  285. p_ptr = tipc_port_lock(origport);
  286. if (p_ptr) {
  287. if (!p_ptr->wakeup)
  288. goto exit;
  289. if (!list_empty(&p_ptr->wait_list))
  290. goto exit;
  291. p_ptr->congested = 1;
  292. p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
  293. list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
  294. l_ptr->stats.link_congs++;
  295. exit:
  296. tipc_port_unlock(p_ptr);
  297. }
  298. spin_unlock_bh(&tipc_port_list_lock);
  299. return -ELINKCONG;
  300. }
  301. void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
  302. {
  303. struct tipc_port *p_ptr;
  304. struct tipc_port *temp_p_ptr;
  305. int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
  306. if (all)
  307. win = 100000;
  308. if (win <= 0)
  309. return;
  310. if (!spin_trylock_bh(&tipc_port_list_lock))
  311. return;
  312. if (link_congested(l_ptr))
  313. goto exit;
  314. list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
  315. wait_list) {
  316. if (win <= 0)
  317. break;
  318. list_del_init(&p_ptr->wait_list);
  319. spin_lock_bh(p_ptr->lock);
  320. p_ptr->congested = 0;
  321. p_ptr->wakeup(p_ptr);
  322. win -= p_ptr->waiting_pkts;
  323. spin_unlock_bh(p_ptr->lock);
  324. }
  325. exit:
  326. spin_unlock_bh(&tipc_port_list_lock);
  327. }
  328. /**
  329. * link_release_outqueue - purge link's outbound message queue
  330. * @l_ptr: pointer to link
  331. */
  332. static void link_release_outqueue(struct tipc_link *l_ptr)
  333. {
  334. kfree_skb_list(l_ptr->first_out);
  335. l_ptr->first_out = NULL;
  336. l_ptr->out_queue_size = 0;
  337. }
  338. /**
  339. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  340. * @l_ptr: pointer to link
  341. */
  342. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  343. {
  344. kfree_skb(l_ptr->reasm_head);
  345. l_ptr->reasm_head = NULL;
  346. l_ptr->reasm_tail = NULL;
  347. }
  348. /**
  349. * tipc_link_purge_queues - purge all pkt queues associated with link
  350. * @l_ptr: pointer to link
  351. */
  352. void tipc_link_purge_queues(struct tipc_link *l_ptr)
  353. {
  354. kfree_skb_list(l_ptr->oldest_deferred_in);
  355. kfree_skb_list(l_ptr->first_out);
  356. tipc_link_reset_fragments(l_ptr);
  357. kfree_skb(l_ptr->proto_msg_queue);
  358. l_ptr->proto_msg_queue = NULL;
  359. }
  360. void tipc_link_reset(struct tipc_link *l_ptr)
  361. {
  362. u32 prev_state = l_ptr->state;
  363. u32 checkpoint = l_ptr->next_in_no;
  364. int was_active_link = tipc_link_is_active(l_ptr);
  365. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  366. /* Link is down, accept any session */
  367. l_ptr->peer_session = INVALID_SESSION;
  368. /* Prepare for max packet size negotiation */
  369. link_init_max_pkt(l_ptr);
  370. l_ptr->state = RESET_UNKNOWN;
  371. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  372. return;
  373. tipc_node_link_down(l_ptr->owner, l_ptr);
  374. tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
  375. if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
  376. l_ptr->reset_checkpoint = checkpoint;
  377. l_ptr->exp_msg_count = START_CHANGEOVER;
  378. }
  379. /* Clean up all queues: */
  380. link_release_outqueue(l_ptr);
  381. kfree_skb(l_ptr->proto_msg_queue);
  382. l_ptr->proto_msg_queue = NULL;
  383. kfree_skb_list(l_ptr->oldest_deferred_in);
  384. if (!list_empty(&l_ptr->waiting_ports))
  385. tipc_link_wakeup_ports(l_ptr, 1);
  386. l_ptr->retransm_queue_head = 0;
  387. l_ptr->retransm_queue_size = 0;
  388. l_ptr->last_out = NULL;
  389. l_ptr->first_out = NULL;
  390. l_ptr->next_out = NULL;
  391. l_ptr->unacked_window = 0;
  392. l_ptr->checkpoint = 1;
  393. l_ptr->next_out_no = 1;
  394. l_ptr->deferred_inqueue_sz = 0;
  395. l_ptr->oldest_deferred_in = NULL;
  396. l_ptr->newest_deferred_in = NULL;
  397. l_ptr->fsm_msg_cnt = 0;
  398. l_ptr->stale_count = 0;
  399. link_reset_statistics(l_ptr);
  400. }
  401. void tipc_link_reset_list(struct tipc_bearer *b_ptr)
  402. {
  403. struct tipc_link *l_ptr;
  404. list_for_each_entry(l_ptr, &b_ptr->links, link_list) {
  405. struct tipc_node *n_ptr = l_ptr->owner;
  406. spin_lock_bh(&n_ptr->lock);
  407. tipc_link_reset(l_ptr);
  408. spin_unlock_bh(&n_ptr->lock);
  409. }
  410. }
  411. static void link_activate(struct tipc_link *l_ptr)
  412. {
  413. l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
  414. tipc_node_link_up(l_ptr->owner, l_ptr);
  415. tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
  416. }
  417. /**
  418. * link_state_event - link finite state machine
  419. * @l_ptr: pointer to link
  420. * @event: state machine event to process
  421. */
  422. static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
  423. {
  424. struct tipc_link *other;
  425. u32 cont_intv = l_ptr->continuity_interval;
  426. if (!l_ptr->started && (event != STARTING_EVT))
  427. return; /* Not yet. */
  428. /* Check whether changeover is going on */
  429. if (l_ptr->exp_msg_count) {
  430. if (event == TIMEOUT_EVT)
  431. link_set_timer(l_ptr, cont_intv);
  432. return;
  433. }
  434. switch (l_ptr->state) {
  435. case WORKING_WORKING:
  436. switch (event) {
  437. case TRAFFIC_MSG_EVT:
  438. case ACTIVATE_MSG:
  439. break;
  440. case TIMEOUT_EVT:
  441. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  442. l_ptr->checkpoint = l_ptr->next_in_no;
  443. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  444. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  445. 0, 0, 0, 0, 0);
  446. l_ptr->fsm_msg_cnt++;
  447. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  448. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  449. 1, 0, 0, 0, 0);
  450. l_ptr->fsm_msg_cnt++;
  451. }
  452. link_set_timer(l_ptr, cont_intv);
  453. break;
  454. }
  455. l_ptr->state = WORKING_UNKNOWN;
  456. l_ptr->fsm_msg_cnt = 0;
  457. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  458. l_ptr->fsm_msg_cnt++;
  459. link_set_timer(l_ptr, cont_intv / 4);
  460. break;
  461. case RESET_MSG:
  462. pr_info("%s<%s>, requested by peer\n", link_rst_msg,
  463. l_ptr->name);
  464. tipc_link_reset(l_ptr);
  465. l_ptr->state = RESET_RESET;
  466. l_ptr->fsm_msg_cnt = 0;
  467. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  468. l_ptr->fsm_msg_cnt++;
  469. link_set_timer(l_ptr, cont_intv);
  470. break;
  471. default:
  472. pr_err("%s%u in WW state\n", link_unk_evt, event);
  473. }
  474. break;
  475. case WORKING_UNKNOWN:
  476. switch (event) {
  477. case TRAFFIC_MSG_EVT:
  478. case ACTIVATE_MSG:
  479. l_ptr->state = WORKING_WORKING;
  480. l_ptr->fsm_msg_cnt = 0;
  481. link_set_timer(l_ptr, cont_intv);
  482. break;
  483. case RESET_MSG:
  484. pr_info("%s<%s>, requested by peer while probing\n",
  485. link_rst_msg, l_ptr->name);
  486. tipc_link_reset(l_ptr);
  487. l_ptr->state = RESET_RESET;
  488. l_ptr->fsm_msg_cnt = 0;
  489. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  490. l_ptr->fsm_msg_cnt++;
  491. link_set_timer(l_ptr, cont_intv);
  492. break;
  493. case TIMEOUT_EVT:
  494. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  495. l_ptr->state = WORKING_WORKING;
  496. l_ptr->fsm_msg_cnt = 0;
  497. l_ptr->checkpoint = l_ptr->next_in_no;
  498. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  499. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  500. 0, 0, 0, 0, 0);
  501. l_ptr->fsm_msg_cnt++;
  502. }
  503. link_set_timer(l_ptr, cont_intv);
  504. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  505. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  506. 1, 0, 0, 0, 0);
  507. l_ptr->fsm_msg_cnt++;
  508. link_set_timer(l_ptr, cont_intv / 4);
  509. } else { /* Link has failed */
  510. pr_warn("%s<%s>, peer not responding\n",
  511. link_rst_msg, l_ptr->name);
  512. tipc_link_reset(l_ptr);
  513. l_ptr->state = RESET_UNKNOWN;
  514. l_ptr->fsm_msg_cnt = 0;
  515. tipc_link_send_proto_msg(l_ptr, RESET_MSG,
  516. 0, 0, 0, 0, 0);
  517. l_ptr->fsm_msg_cnt++;
  518. link_set_timer(l_ptr, cont_intv);
  519. }
  520. break;
  521. default:
  522. pr_err("%s%u in WU state\n", link_unk_evt, event);
  523. }
  524. break;
  525. case RESET_UNKNOWN:
  526. switch (event) {
  527. case TRAFFIC_MSG_EVT:
  528. break;
  529. case ACTIVATE_MSG:
  530. other = l_ptr->owner->active_links[0];
  531. if (other && link_working_unknown(other))
  532. break;
  533. l_ptr->state = WORKING_WORKING;
  534. l_ptr->fsm_msg_cnt = 0;
  535. link_activate(l_ptr);
  536. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  537. l_ptr->fsm_msg_cnt++;
  538. if (l_ptr->owner->working_links == 1)
  539. tipc_link_send_sync(l_ptr);
  540. link_set_timer(l_ptr, cont_intv);
  541. break;
  542. case RESET_MSG:
  543. l_ptr->state = RESET_RESET;
  544. l_ptr->fsm_msg_cnt = 0;
  545. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
  546. l_ptr->fsm_msg_cnt++;
  547. link_set_timer(l_ptr, cont_intv);
  548. break;
  549. case STARTING_EVT:
  550. l_ptr->started = 1;
  551. /* fall through */
  552. case TIMEOUT_EVT:
  553. tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  554. l_ptr->fsm_msg_cnt++;
  555. link_set_timer(l_ptr, cont_intv);
  556. break;
  557. default:
  558. pr_err("%s%u in RU state\n", link_unk_evt, event);
  559. }
  560. break;
  561. case RESET_RESET:
  562. switch (event) {
  563. case TRAFFIC_MSG_EVT:
  564. case ACTIVATE_MSG:
  565. other = l_ptr->owner->active_links[0];
  566. if (other && link_working_unknown(other))
  567. break;
  568. l_ptr->state = WORKING_WORKING;
  569. l_ptr->fsm_msg_cnt = 0;
  570. link_activate(l_ptr);
  571. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  572. l_ptr->fsm_msg_cnt++;
  573. if (l_ptr->owner->working_links == 1)
  574. tipc_link_send_sync(l_ptr);
  575. link_set_timer(l_ptr, cont_intv);
  576. break;
  577. case RESET_MSG:
  578. break;
  579. case TIMEOUT_EVT:
  580. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  581. l_ptr->fsm_msg_cnt++;
  582. link_set_timer(l_ptr, cont_intv);
  583. break;
  584. default:
  585. pr_err("%s%u in RR state\n", link_unk_evt, event);
  586. }
  587. break;
  588. default:
  589. pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
  590. }
  591. }
  592. /*
  593. * link_bundle_buf(): Append contents of a buffer to
  594. * the tail of an existing one.
  595. */
  596. static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
  597. struct sk_buff *buf)
  598. {
  599. struct tipc_msg *bundler_msg = buf_msg(bundler);
  600. struct tipc_msg *msg = buf_msg(buf);
  601. u32 size = msg_size(msg);
  602. u32 bundle_size = msg_size(bundler_msg);
  603. u32 to_pos = align(bundle_size);
  604. u32 pad = to_pos - bundle_size;
  605. if (msg_user(bundler_msg) != MSG_BUNDLER)
  606. return 0;
  607. if (msg_type(bundler_msg) != OPEN_MSG)
  608. return 0;
  609. if (skb_tailroom(bundler) < (pad + size))
  610. return 0;
  611. if (l_ptr->max_pkt < (to_pos + size))
  612. return 0;
  613. skb_put(bundler, pad + size);
  614. skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
  615. msg_set_size(bundler_msg, to_pos + size);
  616. msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
  617. kfree_skb(buf);
  618. l_ptr->stats.sent_bundled++;
  619. return 1;
  620. }
  621. static void link_add_to_outqueue(struct tipc_link *l_ptr,
  622. struct sk_buff *buf,
  623. struct tipc_msg *msg)
  624. {
  625. u32 ack = mod(l_ptr->next_in_no - 1);
  626. u32 seqno = mod(l_ptr->next_out_no++);
  627. msg_set_word(msg, 2, ((ack << 16) | seqno));
  628. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  629. buf->next = NULL;
  630. if (l_ptr->first_out) {
  631. l_ptr->last_out->next = buf;
  632. l_ptr->last_out = buf;
  633. } else
  634. l_ptr->first_out = l_ptr->last_out = buf;
  635. l_ptr->out_queue_size++;
  636. if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
  637. l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
  638. }
  639. static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
  640. struct sk_buff *buf_chain,
  641. u32 long_msgno)
  642. {
  643. struct sk_buff *buf;
  644. struct tipc_msg *msg;
  645. if (!l_ptr->next_out)
  646. l_ptr->next_out = buf_chain;
  647. while (buf_chain) {
  648. buf = buf_chain;
  649. buf_chain = buf_chain->next;
  650. msg = buf_msg(buf);
  651. msg_set_long_msgno(msg, long_msgno);
  652. link_add_to_outqueue(l_ptr, buf, msg);
  653. }
  654. }
  655. /*
  656. * tipc_link_send_buf() is the 'full path' for messages, called from
  657. * inside TIPC when the 'fast path' in tipc_send_buf
  658. * has failed, and from link_send()
  659. */
  660. int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  661. {
  662. struct tipc_msg *msg = buf_msg(buf);
  663. u32 size = msg_size(msg);
  664. u32 dsz = msg_data_sz(msg);
  665. u32 queue_size = l_ptr->out_queue_size;
  666. u32 imp = tipc_msg_tot_importance(msg);
  667. u32 queue_limit = l_ptr->queue_limit[imp];
  668. u32 max_packet = l_ptr->max_pkt;
  669. /* Match msg importance against queue limits: */
  670. if (unlikely(queue_size >= queue_limit)) {
  671. if (imp <= TIPC_CRITICAL_IMPORTANCE) {
  672. link_schedule_port(l_ptr, msg_origport(msg), size);
  673. kfree_skb(buf);
  674. return -ELINKCONG;
  675. }
  676. kfree_skb(buf);
  677. if (imp > CONN_MANAGER) {
  678. pr_warn("%s<%s>, send queue full", link_rst_msg,
  679. l_ptr->name);
  680. tipc_link_reset(l_ptr);
  681. }
  682. return dsz;
  683. }
  684. /* Fragmentation needed ? */
  685. if (size > max_packet)
  686. return link_send_long_buf(l_ptr, buf);
  687. /* Packet can be queued or sent. */
  688. if (likely(!link_congested(l_ptr))) {
  689. link_add_to_outqueue(l_ptr, buf, msg);
  690. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  691. l_ptr->unacked_window = 0;
  692. return dsz;
  693. }
  694. /* Congestion: can message be bundled ? */
  695. if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
  696. (msg_user(msg) != MSG_FRAGMENTER)) {
  697. /* Try adding message to an existing bundle */
  698. if (l_ptr->next_out &&
  699. link_bundle_buf(l_ptr, l_ptr->last_out, buf))
  700. return dsz;
  701. /* Try creating a new bundle */
  702. if (size <= max_packet * 2 / 3) {
  703. struct sk_buff *bundler = tipc_buf_acquire(max_packet);
  704. struct tipc_msg bundler_hdr;
  705. if (bundler) {
  706. tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
  707. INT_H_SIZE, l_ptr->addr);
  708. skb_copy_to_linear_data(bundler, &bundler_hdr,
  709. INT_H_SIZE);
  710. skb_trim(bundler, INT_H_SIZE);
  711. link_bundle_buf(l_ptr, bundler, buf);
  712. buf = bundler;
  713. msg = buf_msg(buf);
  714. l_ptr->stats.sent_bundles++;
  715. }
  716. }
  717. }
  718. if (!l_ptr->next_out)
  719. l_ptr->next_out = buf;
  720. link_add_to_outqueue(l_ptr, buf, msg);
  721. return dsz;
  722. }
  723. /*
  724. * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
  725. * not been selected yet, and the the owner node is not locked
  726. * Called by TIPC internal users, e.g. the name distributor
  727. */
  728. int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
  729. {
  730. struct tipc_link *l_ptr;
  731. struct tipc_node *n_ptr;
  732. int res = -ELINKCONG;
  733. read_lock_bh(&tipc_net_lock);
  734. n_ptr = tipc_node_find(dest);
  735. if (n_ptr) {
  736. tipc_node_lock(n_ptr);
  737. l_ptr = n_ptr->active_links[selector & 1];
  738. if (l_ptr)
  739. res = tipc_link_send_buf(l_ptr, buf);
  740. else
  741. kfree_skb(buf);
  742. tipc_node_unlock(n_ptr);
  743. } else {
  744. kfree_skb(buf);
  745. }
  746. read_unlock_bh(&tipc_net_lock);
  747. return res;
  748. }
  749. /*
  750. * tipc_link_send_sync - synchronize broadcast link endpoints.
  751. *
  752. * Give a newly added peer node the sequence number where it should
  753. * start receiving and acking broadcast packets.
  754. *
  755. * Called with node locked
  756. */
  757. static void tipc_link_send_sync(struct tipc_link *l)
  758. {
  759. struct sk_buff *buf;
  760. struct tipc_msg *msg;
  761. buf = tipc_buf_acquire(INT_H_SIZE);
  762. if (!buf)
  763. return;
  764. msg = buf_msg(buf);
  765. tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
  766. msg_set_last_bcast(msg, l->owner->bclink.acked);
  767. link_add_chain_to_outqueue(l, buf, 0);
  768. tipc_link_push_queue(l);
  769. }
  770. /*
  771. * tipc_link_recv_sync - synchronize broadcast link endpoints.
  772. * Receive the sequence number where we should start receiving and
  773. * acking broadcast packets from a newly added peer node, and open
  774. * up for reception of such packets.
  775. *
  776. * Called with node locked
  777. */
  778. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
  779. {
  780. struct tipc_msg *msg = buf_msg(buf);
  781. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  782. n->bclink.recv_permitted = true;
  783. kfree_skb(buf);
  784. }
  785. /*
  786. * tipc_link_send_names - send name table entries to new neighbor
  787. *
  788. * Send routine for bulk delivery of name table messages when contact
  789. * with a new neighbor occurs. No link congestion checking is performed
  790. * because name table messages *must* be delivered. The messages must be
  791. * small enough not to require fragmentation.
  792. * Called without any locks held.
  793. */
  794. void tipc_link_send_names(struct list_head *message_list, u32 dest)
  795. {
  796. struct tipc_node *n_ptr;
  797. struct tipc_link *l_ptr;
  798. struct sk_buff *buf;
  799. struct sk_buff *temp_buf;
  800. if (list_empty(message_list))
  801. return;
  802. read_lock_bh(&tipc_net_lock);
  803. n_ptr = tipc_node_find(dest);
  804. if (n_ptr) {
  805. tipc_node_lock(n_ptr);
  806. l_ptr = n_ptr->active_links[0];
  807. if (l_ptr) {
  808. /* convert circular list to linear list */
  809. ((struct sk_buff *)message_list->prev)->next = NULL;
  810. link_add_chain_to_outqueue(l_ptr,
  811. (struct sk_buff *)message_list->next, 0);
  812. tipc_link_push_queue(l_ptr);
  813. INIT_LIST_HEAD(message_list);
  814. }
  815. tipc_node_unlock(n_ptr);
  816. }
  817. read_unlock_bh(&tipc_net_lock);
  818. /* discard the messages if they couldn't be sent */
  819. list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
  820. list_del((struct list_head *)buf);
  821. kfree_skb(buf);
  822. }
  823. }
  824. /*
  825. * link_send_buf_fast: Entry for data messages where the
  826. * destination link is known and the header is complete,
  827. * inclusive total message length. Very time critical.
  828. * Link is locked. Returns user data length.
  829. */
  830. static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
  831. u32 *used_max_pkt)
  832. {
  833. struct tipc_msg *msg = buf_msg(buf);
  834. int res = msg_data_sz(msg);
  835. if (likely(!link_congested(l_ptr))) {
  836. if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
  837. link_add_to_outqueue(l_ptr, buf, msg);
  838. tipc_bearer_send(l_ptr->b_ptr, buf,
  839. &l_ptr->media_addr);
  840. l_ptr->unacked_window = 0;
  841. return res;
  842. }
  843. else
  844. *used_max_pkt = l_ptr->max_pkt;
  845. }
  846. return tipc_link_send_buf(l_ptr, buf); /* All other cases */
  847. }
  848. /*
  849. * tipc_link_send_sections_fast: Entry for messages where the
  850. * destination processor is known and the header is complete,
  851. * except for total message length.
  852. * Returns user data length or errno.
  853. */
  854. int tipc_link_send_sections_fast(struct tipc_port *sender,
  855. struct iovec const *msg_sect,
  856. unsigned int len, u32 destaddr)
  857. {
  858. struct tipc_msg *hdr = &sender->phdr;
  859. struct tipc_link *l_ptr;
  860. struct sk_buff *buf;
  861. struct tipc_node *node;
  862. int res;
  863. u32 selector = msg_origport(hdr) & 1;
  864. again:
  865. /*
  866. * Try building message using port's max_pkt hint.
  867. * (Must not hold any locks while building message.)
  868. */
  869. res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
  870. /* Exit if build request was invalid */
  871. if (unlikely(res < 0))
  872. return res;
  873. read_lock_bh(&tipc_net_lock);
  874. node = tipc_node_find(destaddr);
  875. if (likely(node)) {
  876. tipc_node_lock(node);
  877. l_ptr = node->active_links[selector];
  878. if (likely(l_ptr)) {
  879. if (likely(buf)) {
  880. res = link_send_buf_fast(l_ptr, buf,
  881. &sender->max_pkt);
  882. exit:
  883. tipc_node_unlock(node);
  884. read_unlock_bh(&tipc_net_lock);
  885. return res;
  886. }
  887. /* Exit if link (or bearer) is congested */
  888. if (link_congested(l_ptr)) {
  889. res = link_schedule_port(l_ptr,
  890. sender->ref, res);
  891. goto exit;
  892. }
  893. /*
  894. * Message size exceeds max_pkt hint; update hint,
  895. * then re-try fast path or fragment the message
  896. */
  897. sender->max_pkt = l_ptr->max_pkt;
  898. tipc_node_unlock(node);
  899. read_unlock_bh(&tipc_net_lock);
  900. if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
  901. goto again;
  902. return link_send_sections_long(sender, msg_sect, len,
  903. destaddr);
  904. }
  905. tipc_node_unlock(node);
  906. }
  907. read_unlock_bh(&tipc_net_lock);
  908. /* Couldn't find a link to the destination node */
  909. if (buf)
  910. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  911. if (res >= 0)
  912. return tipc_port_reject_sections(sender, hdr, msg_sect,
  913. len, TIPC_ERR_NO_NODE);
  914. return res;
  915. }
  916. /*
  917. * link_send_sections_long(): Entry for long messages where the
  918. * destination node is known and the header is complete,
  919. * inclusive total message length.
  920. * Link and bearer congestion status have been checked to be ok,
  921. * and are ignored if they change.
  922. *
  923. * Note that fragments do not use the full link MTU so that they won't have
  924. * to undergo refragmentation if link changeover causes them to be sent
  925. * over another link with an additional tunnel header added as prefix.
  926. * (Refragmentation will still occur if the other link has a smaller MTU.)
  927. *
  928. * Returns user data length or errno.
  929. */
  930. static int link_send_sections_long(struct tipc_port *sender,
  931. struct iovec const *msg_sect,
  932. unsigned int len, u32 destaddr)
  933. {
  934. struct tipc_link *l_ptr;
  935. struct tipc_node *node;
  936. struct tipc_msg *hdr = &sender->phdr;
  937. u32 dsz = len;
  938. u32 max_pkt, fragm_sz, rest;
  939. struct tipc_msg fragm_hdr;
  940. struct sk_buff *buf, *buf_chain, *prev;
  941. u32 fragm_crs, fragm_rest, hsz, sect_rest;
  942. const unchar __user *sect_crs;
  943. int curr_sect;
  944. u32 fragm_no;
  945. int res = 0;
  946. again:
  947. fragm_no = 1;
  948. max_pkt = sender->max_pkt - INT_H_SIZE;
  949. /* leave room for tunnel header in case of link changeover */
  950. fragm_sz = max_pkt - INT_H_SIZE;
  951. /* leave room for fragmentation header in each fragment */
  952. rest = dsz;
  953. fragm_crs = 0;
  954. fragm_rest = 0;
  955. sect_rest = 0;
  956. sect_crs = NULL;
  957. curr_sect = -1;
  958. /* Prepare reusable fragment header */
  959. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  960. INT_H_SIZE, msg_destnode(hdr));
  961. msg_set_size(&fragm_hdr, max_pkt);
  962. msg_set_fragm_no(&fragm_hdr, 1);
  963. /* Prepare header of first fragment */
  964. buf_chain = buf = tipc_buf_acquire(max_pkt);
  965. if (!buf)
  966. return -ENOMEM;
  967. buf->next = NULL;
  968. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  969. hsz = msg_hdr_sz(hdr);
  970. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
  971. /* Chop up message */
  972. fragm_crs = INT_H_SIZE + hsz;
  973. fragm_rest = fragm_sz - hsz;
  974. do { /* For all sections */
  975. u32 sz;
  976. if (!sect_rest) {
  977. sect_rest = msg_sect[++curr_sect].iov_len;
  978. sect_crs = msg_sect[curr_sect].iov_base;
  979. }
  980. if (sect_rest < fragm_rest)
  981. sz = sect_rest;
  982. else
  983. sz = fragm_rest;
  984. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  985. res = -EFAULT;
  986. error:
  987. kfree_skb_list(buf_chain);
  988. return res;
  989. }
  990. sect_crs += sz;
  991. sect_rest -= sz;
  992. fragm_crs += sz;
  993. fragm_rest -= sz;
  994. rest -= sz;
  995. if (!fragm_rest && rest) {
  996. /* Initiate new fragment: */
  997. if (rest <= fragm_sz) {
  998. fragm_sz = rest;
  999. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1000. } else {
  1001. msg_set_type(&fragm_hdr, FRAGMENT);
  1002. }
  1003. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1004. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1005. prev = buf;
  1006. buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1007. if (!buf) {
  1008. res = -ENOMEM;
  1009. goto error;
  1010. }
  1011. buf->next = NULL;
  1012. prev->next = buf;
  1013. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1014. fragm_crs = INT_H_SIZE;
  1015. fragm_rest = fragm_sz;
  1016. }
  1017. } while (rest > 0);
  1018. /*
  1019. * Now we have a buffer chain. Select a link and check
  1020. * that packet size is still OK
  1021. */
  1022. node = tipc_node_find(destaddr);
  1023. if (likely(node)) {
  1024. tipc_node_lock(node);
  1025. l_ptr = node->active_links[sender->ref & 1];
  1026. if (!l_ptr) {
  1027. tipc_node_unlock(node);
  1028. goto reject;
  1029. }
  1030. if (l_ptr->max_pkt < max_pkt) {
  1031. sender->max_pkt = l_ptr->max_pkt;
  1032. tipc_node_unlock(node);
  1033. kfree_skb_list(buf_chain);
  1034. goto again;
  1035. }
  1036. } else {
  1037. reject:
  1038. kfree_skb_list(buf_chain);
  1039. return tipc_port_reject_sections(sender, hdr, msg_sect,
  1040. len, TIPC_ERR_NO_NODE);
  1041. }
  1042. /* Append chain of fragments to send queue & send them */
  1043. l_ptr->long_msg_seq_no++;
  1044. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  1045. l_ptr->stats.sent_fragments += fragm_no;
  1046. l_ptr->stats.sent_fragmented++;
  1047. tipc_link_push_queue(l_ptr);
  1048. tipc_node_unlock(node);
  1049. return dsz;
  1050. }
  1051. /*
  1052. * tipc_link_push_packet: Push one unsent packet to the media
  1053. */
  1054. static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
  1055. {
  1056. struct sk_buff *buf = l_ptr->first_out;
  1057. u32 r_q_size = l_ptr->retransm_queue_size;
  1058. u32 r_q_head = l_ptr->retransm_queue_head;
  1059. /* Step to position where retransmission failed, if any, */
  1060. /* consider that buffers may have been released in meantime */
  1061. if (r_q_size && buf) {
  1062. u32 last = lesser(mod(r_q_head + r_q_size),
  1063. link_last_sent(l_ptr));
  1064. u32 first = buf_seqno(buf);
  1065. while (buf && less(first, r_q_head)) {
  1066. first = mod(first + 1);
  1067. buf = buf->next;
  1068. }
  1069. l_ptr->retransm_queue_head = r_q_head = first;
  1070. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1071. }
  1072. /* Continue retransmission now, if there is anything: */
  1073. if (r_q_size && buf) {
  1074. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1075. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1076. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1077. l_ptr->retransm_queue_head = mod(++r_q_head);
  1078. l_ptr->retransm_queue_size = --r_q_size;
  1079. l_ptr->stats.retransmitted++;
  1080. return 0;
  1081. }
  1082. /* Send deferred protocol message, if any: */
  1083. buf = l_ptr->proto_msg_queue;
  1084. if (buf) {
  1085. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1086. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1087. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1088. l_ptr->unacked_window = 0;
  1089. kfree_skb(buf);
  1090. l_ptr->proto_msg_queue = NULL;
  1091. return 0;
  1092. }
  1093. /* Send one deferred data message, if send window not full: */
  1094. buf = l_ptr->next_out;
  1095. if (buf) {
  1096. struct tipc_msg *msg = buf_msg(buf);
  1097. u32 next = msg_seqno(msg);
  1098. u32 first = buf_seqno(l_ptr->first_out);
  1099. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1100. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1101. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1102. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1103. if (msg_user(msg) == MSG_BUNDLER)
  1104. msg_set_type(msg, CLOSED_MSG);
  1105. l_ptr->next_out = buf->next;
  1106. return 0;
  1107. }
  1108. }
  1109. return 1;
  1110. }
  1111. /*
  1112. * push_queue(): push out the unsent messages of a link where
  1113. * congestion has abated. Node is locked
  1114. */
  1115. void tipc_link_push_queue(struct tipc_link *l_ptr)
  1116. {
  1117. u32 res;
  1118. do {
  1119. res = tipc_link_push_packet(l_ptr);
  1120. } while (!res);
  1121. }
  1122. static void link_reset_all(unsigned long addr)
  1123. {
  1124. struct tipc_node *n_ptr;
  1125. char addr_string[16];
  1126. u32 i;
  1127. read_lock_bh(&tipc_net_lock);
  1128. n_ptr = tipc_node_find((u32)addr);
  1129. if (!n_ptr) {
  1130. read_unlock_bh(&tipc_net_lock);
  1131. return; /* node no longer exists */
  1132. }
  1133. tipc_node_lock(n_ptr);
  1134. pr_warn("Resetting all links to %s\n",
  1135. tipc_addr_string_fill(addr_string, n_ptr->addr));
  1136. for (i = 0; i < MAX_BEARERS; i++) {
  1137. if (n_ptr->links[i]) {
  1138. link_print(n_ptr->links[i], "Resetting link\n");
  1139. tipc_link_reset(n_ptr->links[i]);
  1140. }
  1141. }
  1142. tipc_node_unlock(n_ptr);
  1143. read_unlock_bh(&tipc_net_lock);
  1144. }
  1145. static void link_retransmit_failure(struct tipc_link *l_ptr,
  1146. struct sk_buff *buf)
  1147. {
  1148. struct tipc_msg *msg = buf_msg(buf);
  1149. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1150. if (l_ptr->addr) {
  1151. /* Handle failure on standard link */
  1152. link_print(l_ptr, "Resetting link\n");
  1153. tipc_link_reset(l_ptr);
  1154. } else {
  1155. /* Handle failure on broadcast link */
  1156. struct tipc_node *n_ptr;
  1157. char addr_string[16];
  1158. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  1159. pr_cont("Outstanding acks: %lu\n",
  1160. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1161. n_ptr = tipc_bclink_retransmit_to();
  1162. tipc_node_lock(n_ptr);
  1163. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1164. pr_info("Broadcast link info for %s\n", addr_string);
  1165. pr_info("Reception permitted: %d, Acked: %u\n",
  1166. n_ptr->bclink.recv_permitted,
  1167. n_ptr->bclink.acked);
  1168. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  1169. n_ptr->bclink.last_in,
  1170. n_ptr->bclink.oos_state,
  1171. n_ptr->bclink.last_sent);
  1172. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1173. tipc_node_unlock(n_ptr);
  1174. l_ptr->stale_count = 0;
  1175. }
  1176. }
  1177. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
  1178. u32 retransmits)
  1179. {
  1180. struct tipc_msg *msg;
  1181. if (!buf)
  1182. return;
  1183. msg = buf_msg(buf);
  1184. /* Detect repeated retransmit failures */
  1185. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1186. if (++l_ptr->stale_count > 100) {
  1187. link_retransmit_failure(l_ptr, buf);
  1188. return;
  1189. }
  1190. } else {
  1191. l_ptr->last_retransmitted = msg_seqno(msg);
  1192. l_ptr->stale_count = 1;
  1193. }
  1194. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1195. msg = buf_msg(buf);
  1196. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1197. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1198. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1199. buf = buf->next;
  1200. retransmits--;
  1201. l_ptr->stats.retransmitted++;
  1202. }
  1203. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1204. }
  1205. /**
  1206. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1207. */
  1208. static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
  1209. struct sk_buff *buf)
  1210. {
  1211. u32 seq_no;
  1212. if (l_ptr->oldest_deferred_in == NULL)
  1213. return buf;
  1214. seq_no = buf_seqno(l_ptr->oldest_deferred_in);
  1215. if (seq_no == mod(l_ptr->next_in_no)) {
  1216. l_ptr->newest_deferred_in->next = buf;
  1217. buf = l_ptr->oldest_deferred_in;
  1218. l_ptr->oldest_deferred_in = NULL;
  1219. l_ptr->deferred_inqueue_sz = 0;
  1220. }
  1221. return buf;
  1222. }
  1223. /**
  1224. * link_recv_buf_validate - validate basic format of received message
  1225. *
  1226. * This routine ensures a TIPC message has an acceptable header, and at least
  1227. * as much data as the header indicates it should. The routine also ensures
  1228. * that the entire message header is stored in the main fragment of the message
  1229. * buffer, to simplify future access to message header fields.
  1230. *
  1231. * Note: Having extra info present in the message header or data areas is OK.
  1232. * TIPC will ignore the excess, under the assumption that it is optional info
  1233. * introduced by a later release of the protocol.
  1234. */
  1235. static int link_recv_buf_validate(struct sk_buff *buf)
  1236. {
  1237. static u32 min_data_hdr_size[8] = {
  1238. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  1239. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1240. };
  1241. struct tipc_msg *msg;
  1242. u32 tipc_hdr[2];
  1243. u32 size;
  1244. u32 hdr_size;
  1245. u32 min_hdr_size;
  1246. if (unlikely(buf->len < MIN_H_SIZE))
  1247. return 0;
  1248. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1249. if (msg == NULL)
  1250. return 0;
  1251. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1252. return 0;
  1253. size = msg_size(msg);
  1254. hdr_size = msg_hdr_sz(msg);
  1255. min_hdr_size = msg_isdata(msg) ?
  1256. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1257. if (unlikely((hdr_size < min_hdr_size) ||
  1258. (size < hdr_size) ||
  1259. (buf->len < size) ||
  1260. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1261. return 0;
  1262. return pskb_may_pull(buf, hdr_size);
  1263. }
  1264. /**
  1265. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1266. * @head: pointer to message buffer chain
  1267. * @tb_ptr: pointer to bearer message arrived on
  1268. *
  1269. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1270. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1271. */
  1272. void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
  1273. {
  1274. read_lock_bh(&tipc_net_lock);
  1275. while (head) {
  1276. struct tipc_node *n_ptr;
  1277. struct tipc_link *l_ptr;
  1278. struct sk_buff *crs;
  1279. struct sk_buff *buf = head;
  1280. struct tipc_msg *msg;
  1281. u32 seq_no;
  1282. u32 ackd;
  1283. u32 released = 0;
  1284. int type;
  1285. head = head->next;
  1286. buf->next = NULL;
  1287. /* Ensure bearer is still enabled */
  1288. if (unlikely(!b_ptr->active))
  1289. goto discard;
  1290. /* Ensure message is well-formed */
  1291. if (unlikely(!link_recv_buf_validate(buf)))
  1292. goto discard;
  1293. /* Ensure message data is a single contiguous unit */
  1294. if (unlikely(skb_linearize(buf)))
  1295. goto discard;
  1296. /* Handle arrival of a non-unicast link message */
  1297. msg = buf_msg(buf);
  1298. if (unlikely(msg_non_seq(msg))) {
  1299. if (msg_user(msg) == LINK_CONFIG)
  1300. tipc_disc_recv_msg(buf, b_ptr);
  1301. else
  1302. tipc_bclink_recv_pkt(buf);
  1303. continue;
  1304. }
  1305. /* Discard unicast link messages destined for another node */
  1306. if (unlikely(!msg_short(msg) &&
  1307. (msg_destnode(msg) != tipc_own_addr)))
  1308. goto discard;
  1309. /* Locate neighboring node that sent message */
  1310. n_ptr = tipc_node_find(msg_prevnode(msg));
  1311. if (unlikely(!n_ptr))
  1312. goto discard;
  1313. tipc_node_lock(n_ptr);
  1314. /* Locate unicast link endpoint that should handle message */
  1315. l_ptr = n_ptr->links[b_ptr->identity];
  1316. if (unlikely(!l_ptr))
  1317. goto unlock_discard;
  1318. /* Verify that communication with node is currently allowed */
  1319. if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
  1320. msg_user(msg) == LINK_PROTOCOL &&
  1321. (msg_type(msg) == RESET_MSG ||
  1322. msg_type(msg) == ACTIVATE_MSG) &&
  1323. !msg_redundant_link(msg))
  1324. n_ptr->block_setup &= ~WAIT_PEER_DOWN;
  1325. if (n_ptr->block_setup)
  1326. goto unlock_discard;
  1327. /* Validate message sequence number info */
  1328. seq_no = msg_seqno(msg);
  1329. ackd = msg_ack(msg);
  1330. /* Release acked messages */
  1331. if (n_ptr->bclink.recv_permitted)
  1332. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1333. crs = l_ptr->first_out;
  1334. while ((crs != l_ptr->next_out) &&
  1335. less_eq(buf_seqno(crs), ackd)) {
  1336. struct sk_buff *next = crs->next;
  1337. kfree_skb(crs);
  1338. crs = next;
  1339. released++;
  1340. }
  1341. if (released) {
  1342. l_ptr->first_out = crs;
  1343. l_ptr->out_queue_size -= released;
  1344. }
  1345. /* Try sending any messages link endpoint has pending */
  1346. if (unlikely(l_ptr->next_out))
  1347. tipc_link_push_queue(l_ptr);
  1348. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1349. tipc_link_wakeup_ports(l_ptr, 0);
  1350. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1351. l_ptr->stats.sent_acks++;
  1352. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1353. }
  1354. /* Now (finally!) process the incoming message */
  1355. protocol_check:
  1356. if (unlikely(!link_working_working(l_ptr))) {
  1357. if (msg_user(msg) == LINK_PROTOCOL) {
  1358. link_recv_proto_msg(l_ptr, buf);
  1359. head = link_insert_deferred_queue(l_ptr, head);
  1360. tipc_node_unlock(n_ptr);
  1361. continue;
  1362. }
  1363. /* Traffic message. Conditionally activate link */
  1364. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1365. if (link_working_working(l_ptr)) {
  1366. /* Re-insert buffer in front of queue */
  1367. buf->next = head;
  1368. head = buf;
  1369. tipc_node_unlock(n_ptr);
  1370. continue;
  1371. }
  1372. goto unlock_discard;
  1373. }
  1374. /* Link is now in state WORKING_WORKING */
  1375. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1376. link_handle_out_of_seq_msg(l_ptr, buf);
  1377. head = link_insert_deferred_queue(l_ptr, head);
  1378. tipc_node_unlock(n_ptr);
  1379. continue;
  1380. }
  1381. l_ptr->next_in_no++;
  1382. if (unlikely(l_ptr->oldest_deferred_in))
  1383. head = link_insert_deferred_queue(l_ptr, head);
  1384. deliver:
  1385. if (likely(msg_isdata(msg))) {
  1386. tipc_node_unlock(n_ptr);
  1387. tipc_port_recv_msg(buf);
  1388. continue;
  1389. }
  1390. switch (msg_user(msg)) {
  1391. int ret;
  1392. case MSG_BUNDLER:
  1393. l_ptr->stats.recv_bundles++;
  1394. l_ptr->stats.recv_bundled += msg_msgcnt(msg);
  1395. tipc_node_unlock(n_ptr);
  1396. tipc_link_recv_bundle(buf);
  1397. continue;
  1398. case NAME_DISTRIBUTOR:
  1399. n_ptr->bclink.recv_permitted = true;
  1400. tipc_node_unlock(n_ptr);
  1401. tipc_named_recv(buf);
  1402. continue;
  1403. case BCAST_PROTOCOL:
  1404. tipc_link_recv_sync(n_ptr, buf);
  1405. tipc_node_unlock(n_ptr);
  1406. continue;
  1407. case CONN_MANAGER:
  1408. tipc_node_unlock(n_ptr);
  1409. tipc_port_recv_proto_msg(buf);
  1410. continue;
  1411. case MSG_FRAGMENTER:
  1412. l_ptr->stats.recv_fragments++;
  1413. ret = tipc_link_frag_rcv(&l_ptr->reasm_head,
  1414. &l_ptr->reasm_tail,
  1415. &buf);
  1416. if (ret == LINK_REASM_COMPLETE) {
  1417. l_ptr->stats.recv_fragmented++;
  1418. msg = buf_msg(buf);
  1419. goto deliver;
  1420. }
  1421. if (ret == LINK_REASM_ERROR)
  1422. tipc_link_reset(l_ptr);
  1423. tipc_node_unlock(n_ptr);
  1424. continue;
  1425. case CHANGEOVER_PROTOCOL:
  1426. type = msg_type(msg);
  1427. if (tipc_link_tunnel_rcv(&l_ptr, &buf)) {
  1428. msg = buf_msg(buf);
  1429. seq_no = msg_seqno(msg);
  1430. if (type == ORIGINAL_MSG)
  1431. goto deliver;
  1432. goto protocol_check;
  1433. }
  1434. break;
  1435. default:
  1436. kfree_skb(buf);
  1437. buf = NULL;
  1438. break;
  1439. }
  1440. tipc_node_unlock(n_ptr);
  1441. tipc_net_route_msg(buf);
  1442. continue;
  1443. unlock_discard:
  1444. tipc_node_unlock(n_ptr);
  1445. discard:
  1446. kfree_skb(buf);
  1447. }
  1448. read_unlock_bh(&tipc_net_lock);
  1449. }
  1450. /**
  1451. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1452. *
  1453. * Returns increase in queue length (i.e. 0 or 1)
  1454. */
  1455. u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
  1456. struct sk_buff *buf)
  1457. {
  1458. struct sk_buff *queue_buf;
  1459. struct sk_buff **prev;
  1460. u32 seq_no = buf_seqno(buf);
  1461. buf->next = NULL;
  1462. /* Empty queue ? */
  1463. if (*head == NULL) {
  1464. *head = *tail = buf;
  1465. return 1;
  1466. }
  1467. /* Last ? */
  1468. if (less(buf_seqno(*tail), seq_no)) {
  1469. (*tail)->next = buf;
  1470. *tail = buf;
  1471. return 1;
  1472. }
  1473. /* Locate insertion point in queue, then insert; discard if duplicate */
  1474. prev = head;
  1475. queue_buf = *head;
  1476. for (;;) {
  1477. u32 curr_seqno = buf_seqno(queue_buf);
  1478. if (seq_no == curr_seqno) {
  1479. kfree_skb(buf);
  1480. return 0;
  1481. }
  1482. if (less(seq_no, curr_seqno))
  1483. break;
  1484. prev = &queue_buf->next;
  1485. queue_buf = queue_buf->next;
  1486. }
  1487. buf->next = queue_buf;
  1488. *prev = buf;
  1489. return 1;
  1490. }
  1491. /*
  1492. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1493. */
  1494. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1495. struct sk_buff *buf)
  1496. {
  1497. u32 seq_no = buf_seqno(buf);
  1498. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1499. link_recv_proto_msg(l_ptr, buf);
  1500. return;
  1501. }
  1502. /* Record OOS packet arrival (force mismatch on next timeout) */
  1503. l_ptr->checkpoint--;
  1504. /*
  1505. * Discard packet if a duplicate; otherwise add it to deferred queue
  1506. * and notify peer of gap as per protocol specification
  1507. */
  1508. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1509. l_ptr->stats.duplicates++;
  1510. kfree_skb(buf);
  1511. return;
  1512. }
  1513. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1514. &l_ptr->newest_deferred_in, buf)) {
  1515. l_ptr->deferred_inqueue_sz++;
  1516. l_ptr->stats.deferred_recv++;
  1517. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1518. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1519. } else
  1520. l_ptr->stats.duplicates++;
  1521. }
  1522. /*
  1523. * Send protocol message to the other endpoint.
  1524. */
  1525. void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
  1526. int probe_msg, u32 gap, u32 tolerance,
  1527. u32 priority, u32 ack_mtu)
  1528. {
  1529. struct sk_buff *buf = NULL;
  1530. struct tipc_msg *msg = l_ptr->pmsg;
  1531. u32 msg_size = sizeof(l_ptr->proto_msg);
  1532. int r_flag;
  1533. /* Discard any previous message that was deferred due to congestion */
  1534. if (l_ptr->proto_msg_queue) {
  1535. kfree_skb(l_ptr->proto_msg_queue);
  1536. l_ptr->proto_msg_queue = NULL;
  1537. }
  1538. /* Don't send protocol message during link changeover */
  1539. if (l_ptr->exp_msg_count)
  1540. return;
  1541. /* Abort non-RESET send if communication with node is prohibited */
  1542. if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
  1543. return;
  1544. /* Create protocol message with "out-of-sequence" sequence number */
  1545. msg_set_type(msg, msg_typ);
  1546. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1547. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1548. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1549. if (msg_typ == STATE_MSG) {
  1550. u32 next_sent = mod(l_ptr->next_out_no);
  1551. if (!tipc_link_is_up(l_ptr))
  1552. return;
  1553. if (l_ptr->next_out)
  1554. next_sent = buf_seqno(l_ptr->next_out);
  1555. msg_set_next_sent(msg, next_sent);
  1556. if (l_ptr->oldest_deferred_in) {
  1557. u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
  1558. gap = mod(rec - mod(l_ptr->next_in_no));
  1559. }
  1560. msg_set_seq_gap(msg, gap);
  1561. if (gap)
  1562. l_ptr->stats.sent_nacks++;
  1563. msg_set_link_tolerance(msg, tolerance);
  1564. msg_set_linkprio(msg, priority);
  1565. msg_set_max_pkt(msg, ack_mtu);
  1566. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1567. msg_set_probe(msg, probe_msg != 0);
  1568. if (probe_msg) {
  1569. u32 mtu = l_ptr->max_pkt;
  1570. if ((mtu < l_ptr->max_pkt_target) &&
  1571. link_working_working(l_ptr) &&
  1572. l_ptr->fsm_msg_cnt) {
  1573. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1574. if (l_ptr->max_pkt_probes == 10) {
  1575. l_ptr->max_pkt_target = (msg_size - 4);
  1576. l_ptr->max_pkt_probes = 0;
  1577. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1578. }
  1579. l_ptr->max_pkt_probes++;
  1580. }
  1581. l_ptr->stats.sent_probes++;
  1582. }
  1583. l_ptr->stats.sent_states++;
  1584. } else { /* RESET_MSG or ACTIVATE_MSG */
  1585. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1586. msg_set_seq_gap(msg, 0);
  1587. msg_set_next_sent(msg, 1);
  1588. msg_set_probe(msg, 0);
  1589. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1590. msg_set_linkprio(msg, l_ptr->priority);
  1591. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1592. }
  1593. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1594. msg_set_redundant_link(msg, r_flag);
  1595. msg_set_linkprio(msg, l_ptr->priority);
  1596. msg_set_size(msg, msg_size);
  1597. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1598. buf = tipc_buf_acquire(msg_size);
  1599. if (!buf)
  1600. return;
  1601. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1602. buf->priority = TC_PRIO_CONTROL;
  1603. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1604. l_ptr->unacked_window = 0;
  1605. kfree_skb(buf);
  1606. }
  1607. /*
  1608. * Receive protocol message :
  1609. * Note that network plane id propagates through the network, and may
  1610. * change at any time. The node with lowest address rules
  1611. */
  1612. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
  1613. {
  1614. u32 rec_gap = 0;
  1615. u32 max_pkt_info;
  1616. u32 max_pkt_ack;
  1617. u32 msg_tol;
  1618. struct tipc_msg *msg = buf_msg(buf);
  1619. /* Discard protocol message during link changeover */
  1620. if (l_ptr->exp_msg_count)
  1621. goto exit;
  1622. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1623. l_ptr->checkpoint--;
  1624. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1625. if (tipc_own_addr > msg_prevnode(msg))
  1626. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1627. switch (msg_type(msg)) {
  1628. case RESET_MSG:
  1629. if (!link_working_unknown(l_ptr) &&
  1630. (l_ptr->peer_session != INVALID_SESSION)) {
  1631. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1632. break; /* duplicate or old reset: ignore */
  1633. }
  1634. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1635. link_working_unknown(l_ptr))) {
  1636. /*
  1637. * peer has lost contact -- don't allow peer's links
  1638. * to reactivate before we recognize loss & clean up
  1639. */
  1640. l_ptr->owner->block_setup = WAIT_NODE_DOWN;
  1641. }
  1642. link_state_event(l_ptr, RESET_MSG);
  1643. /* fall thru' */
  1644. case ACTIVATE_MSG:
  1645. /* Update link settings according other endpoint's values */
  1646. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1647. msg_tol = msg_link_tolerance(msg);
  1648. if (msg_tol > l_ptr->tolerance)
  1649. link_set_supervision_props(l_ptr, msg_tol);
  1650. if (msg_linkprio(msg) > l_ptr->priority)
  1651. l_ptr->priority = msg_linkprio(msg);
  1652. max_pkt_info = msg_max_pkt(msg);
  1653. if (max_pkt_info) {
  1654. if (max_pkt_info < l_ptr->max_pkt_target)
  1655. l_ptr->max_pkt_target = max_pkt_info;
  1656. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1657. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1658. } else {
  1659. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1660. }
  1661. /* Synchronize broadcast link info, if not done previously */
  1662. if (!tipc_node_is_up(l_ptr->owner)) {
  1663. l_ptr->owner->bclink.last_sent =
  1664. l_ptr->owner->bclink.last_in =
  1665. msg_last_bcast(msg);
  1666. l_ptr->owner->bclink.oos_state = 0;
  1667. }
  1668. l_ptr->peer_session = msg_session(msg);
  1669. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1670. if (msg_type(msg) == ACTIVATE_MSG)
  1671. link_state_event(l_ptr, ACTIVATE_MSG);
  1672. break;
  1673. case STATE_MSG:
  1674. msg_tol = msg_link_tolerance(msg);
  1675. if (msg_tol)
  1676. link_set_supervision_props(l_ptr, msg_tol);
  1677. if (msg_linkprio(msg) &&
  1678. (msg_linkprio(msg) != l_ptr->priority)) {
  1679. pr_warn("%s<%s>, priority change %u->%u\n",
  1680. link_rst_msg, l_ptr->name, l_ptr->priority,
  1681. msg_linkprio(msg));
  1682. l_ptr->priority = msg_linkprio(msg);
  1683. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1684. break;
  1685. }
  1686. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1687. l_ptr->stats.recv_states++;
  1688. if (link_reset_unknown(l_ptr))
  1689. break;
  1690. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1691. rec_gap = mod(msg_next_sent(msg) -
  1692. mod(l_ptr->next_in_no));
  1693. }
  1694. max_pkt_ack = msg_max_pkt(msg);
  1695. if (max_pkt_ack > l_ptr->max_pkt) {
  1696. l_ptr->max_pkt = max_pkt_ack;
  1697. l_ptr->max_pkt_probes = 0;
  1698. }
  1699. max_pkt_ack = 0;
  1700. if (msg_probe(msg)) {
  1701. l_ptr->stats.recv_probes++;
  1702. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1703. max_pkt_ack = msg_size(msg);
  1704. }
  1705. /* Protocol message before retransmits, reduce loss risk */
  1706. if (l_ptr->owner->bclink.recv_permitted)
  1707. tipc_bclink_update_link_state(l_ptr->owner,
  1708. msg_last_bcast(msg));
  1709. if (rec_gap || (msg_probe(msg))) {
  1710. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1711. 0, rec_gap, 0, 0, max_pkt_ack);
  1712. }
  1713. if (msg_seq_gap(msg)) {
  1714. l_ptr->stats.recv_nacks++;
  1715. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1716. msg_seq_gap(msg));
  1717. }
  1718. break;
  1719. }
  1720. exit:
  1721. kfree_skb(buf);
  1722. }
  1723. /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
  1724. * a different bearer. Owner node is locked.
  1725. */
  1726. static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
  1727. struct tipc_msg *tunnel_hdr,
  1728. struct tipc_msg *msg,
  1729. u32 selector)
  1730. {
  1731. struct tipc_link *tunnel;
  1732. struct sk_buff *buf;
  1733. u32 length = msg_size(msg);
  1734. tunnel = l_ptr->owner->active_links[selector & 1];
  1735. if (!tipc_link_is_up(tunnel)) {
  1736. pr_warn("%stunnel link no longer available\n", link_co_err);
  1737. return;
  1738. }
  1739. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1740. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1741. if (!buf) {
  1742. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1743. return;
  1744. }
  1745. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1746. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1747. tipc_link_send_buf(tunnel, buf);
  1748. }
  1749. /* tipc_link_failover_send_queue(): A link has gone down, but a second
  1750. * link is still active. We can do failover. Tunnel the failing link's
  1751. * whole send queue via the remaining link. This way, we don't lose
  1752. * any packets, and sequence order is preserved for subsequent traffic
  1753. * sent over the remaining link. Owner node is locked.
  1754. */
  1755. void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
  1756. {
  1757. u32 msgcount = l_ptr->out_queue_size;
  1758. struct sk_buff *crs = l_ptr->first_out;
  1759. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1760. struct tipc_msg tunnel_hdr;
  1761. int split_bundles;
  1762. if (!tunnel)
  1763. return;
  1764. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1765. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1766. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1767. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1768. if (!l_ptr->first_out) {
  1769. struct sk_buff *buf;
  1770. buf = tipc_buf_acquire(INT_H_SIZE);
  1771. if (buf) {
  1772. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1773. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1774. tipc_link_send_buf(tunnel, buf);
  1775. } else {
  1776. pr_warn("%sunable to send changeover msg\n",
  1777. link_co_err);
  1778. }
  1779. return;
  1780. }
  1781. split_bundles = (l_ptr->owner->active_links[0] !=
  1782. l_ptr->owner->active_links[1]);
  1783. while (crs) {
  1784. struct tipc_msg *msg = buf_msg(crs);
  1785. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1786. struct tipc_msg *m = msg_get_wrapped(msg);
  1787. unchar *pos = (unchar *)m;
  1788. msgcount = msg_msgcnt(msg);
  1789. while (msgcount--) {
  1790. msg_set_seqno(m, msg_seqno(msg));
  1791. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
  1792. msg_link_selector(m));
  1793. pos += align(msg_size(m));
  1794. m = (struct tipc_msg *)pos;
  1795. }
  1796. } else {
  1797. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
  1798. msg_link_selector(msg));
  1799. }
  1800. crs = crs->next;
  1801. }
  1802. }
  1803. /* tipc_link_dup_send_queue(): A second link has become active. Tunnel a
  1804. * duplicate of the first link's send queue via the new link. This way, we
  1805. * are guaranteed that currently queued packets from a socket are delivered
  1806. * before future traffic from the same socket, even if this is using the
  1807. * new link. The last arriving copy of each duplicate packet is dropped at
  1808. * the receiving end by the regular protocol check, so packet cardinality
  1809. * and sequence order is preserved per sender/receiver socket pair.
  1810. * Owner node is locked.
  1811. */
  1812. void tipc_link_dup_send_queue(struct tipc_link *l_ptr,
  1813. struct tipc_link *tunnel)
  1814. {
  1815. struct sk_buff *iter;
  1816. struct tipc_msg tunnel_hdr;
  1817. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1818. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1819. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1820. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1821. iter = l_ptr->first_out;
  1822. while (iter) {
  1823. struct sk_buff *outbuf;
  1824. struct tipc_msg *msg = buf_msg(iter);
  1825. u32 length = msg_size(msg);
  1826. if (msg_user(msg) == MSG_BUNDLER)
  1827. msg_set_type(msg, CLOSED_MSG);
  1828. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1829. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1830. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1831. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1832. if (outbuf == NULL) {
  1833. pr_warn("%sunable to send duplicate msg\n",
  1834. link_co_err);
  1835. return;
  1836. }
  1837. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1838. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1839. length);
  1840. tipc_link_send_buf(tunnel, outbuf);
  1841. if (!tipc_link_is_up(l_ptr))
  1842. return;
  1843. iter = iter->next;
  1844. }
  1845. }
  1846. /**
  1847. * buf_extract - extracts embedded TIPC message from another message
  1848. * @skb: encapsulating message buffer
  1849. * @from_pos: offset to extract from
  1850. *
  1851. * Returns a new message buffer containing an embedded message. The
  1852. * encapsulating message itself is left unchanged.
  1853. */
  1854. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1855. {
  1856. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1857. u32 size = msg_size(msg);
  1858. struct sk_buff *eb;
  1859. eb = tipc_buf_acquire(size);
  1860. if (eb)
  1861. skb_copy_to_linear_data(eb, msg, size);
  1862. return eb;
  1863. }
  1864. /* tipc_link_tunnel_rcv(): Receive a tunneled packet, sent
  1865. * via other link as result of a failover (ORIGINAL_MSG) or
  1866. * a new active link (DUPLICATE_MSG). Failover packets are
  1867. * returned to the active link for delivery upwards.
  1868. * Owner node is locked.
  1869. */
  1870. static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
  1871. struct sk_buff **buf)
  1872. {
  1873. struct sk_buff *tunnel_buf = *buf;
  1874. struct tipc_link *dest_link;
  1875. struct tipc_msg *msg;
  1876. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  1877. u32 msg_typ = msg_type(tunnel_msg);
  1878. u32 msg_count = msg_msgcnt(tunnel_msg);
  1879. u32 bearer_id = msg_bearer_id(tunnel_msg);
  1880. if (bearer_id >= MAX_BEARERS)
  1881. goto exit;
  1882. dest_link = (*l_ptr)->owner->links[bearer_id];
  1883. if (!dest_link)
  1884. goto exit;
  1885. if (dest_link == *l_ptr) {
  1886. pr_err("Unexpected changeover message on link <%s>\n",
  1887. (*l_ptr)->name);
  1888. goto exit;
  1889. }
  1890. *l_ptr = dest_link;
  1891. msg = msg_get_wrapped(tunnel_msg);
  1892. if (msg_typ == DUPLICATE_MSG) {
  1893. if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
  1894. goto exit;
  1895. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1896. if (*buf == NULL) {
  1897. pr_warn("%sduplicate msg dropped\n", link_co_err);
  1898. goto exit;
  1899. }
  1900. kfree_skb(tunnel_buf);
  1901. return 1;
  1902. }
  1903. /* First original message ?: */
  1904. if (tipc_link_is_up(dest_link)) {
  1905. pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
  1906. dest_link->name);
  1907. tipc_link_reset(dest_link);
  1908. dest_link->exp_msg_count = msg_count;
  1909. if (!msg_count)
  1910. goto exit;
  1911. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  1912. dest_link->exp_msg_count = msg_count;
  1913. if (!msg_count)
  1914. goto exit;
  1915. }
  1916. /* Receive original message */
  1917. if (dest_link->exp_msg_count == 0) {
  1918. pr_warn("%sgot too many tunnelled messages\n", link_co_err);
  1919. goto exit;
  1920. }
  1921. dest_link->exp_msg_count--;
  1922. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  1923. goto exit;
  1924. } else {
  1925. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1926. if (*buf != NULL) {
  1927. kfree_skb(tunnel_buf);
  1928. return 1;
  1929. } else {
  1930. pr_warn("%soriginal msg dropped\n", link_co_err);
  1931. }
  1932. }
  1933. exit:
  1934. *buf = NULL;
  1935. kfree_skb(tunnel_buf);
  1936. return 0;
  1937. }
  1938. /*
  1939. * Bundler functionality:
  1940. */
  1941. void tipc_link_recv_bundle(struct sk_buff *buf)
  1942. {
  1943. u32 msgcount = msg_msgcnt(buf_msg(buf));
  1944. u32 pos = INT_H_SIZE;
  1945. struct sk_buff *obuf;
  1946. while (msgcount--) {
  1947. obuf = buf_extract(buf, pos);
  1948. if (obuf == NULL) {
  1949. pr_warn("Link unable to unbundle message(s)\n");
  1950. break;
  1951. }
  1952. pos += align(msg_size(buf_msg(obuf)));
  1953. tipc_net_route_msg(obuf);
  1954. }
  1955. kfree_skb(buf);
  1956. }
  1957. /*
  1958. * Fragmentation/defragmentation:
  1959. */
  1960. /*
  1961. * link_send_long_buf: Entry for buffers needing fragmentation.
  1962. * The buffer is complete, inclusive total message length.
  1963. * Returns user data length.
  1964. */
  1965. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  1966. {
  1967. struct sk_buff *buf_chain = NULL;
  1968. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  1969. struct tipc_msg *inmsg = buf_msg(buf);
  1970. struct tipc_msg fragm_hdr;
  1971. u32 insize = msg_size(inmsg);
  1972. u32 dsz = msg_data_sz(inmsg);
  1973. unchar *crs = buf->data;
  1974. u32 rest = insize;
  1975. u32 pack_sz = l_ptr->max_pkt;
  1976. u32 fragm_sz = pack_sz - INT_H_SIZE;
  1977. u32 fragm_no = 0;
  1978. u32 destaddr;
  1979. if (msg_short(inmsg))
  1980. destaddr = l_ptr->addr;
  1981. else
  1982. destaddr = msg_destnode(inmsg);
  1983. /* Prepare reusable fragment header: */
  1984. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  1985. INT_H_SIZE, destaddr);
  1986. /* Chop up message: */
  1987. while (rest > 0) {
  1988. struct sk_buff *fragm;
  1989. if (rest <= fragm_sz) {
  1990. fragm_sz = rest;
  1991. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1992. }
  1993. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1994. if (fragm == NULL) {
  1995. kfree_skb(buf);
  1996. kfree_skb_list(buf_chain);
  1997. return -ENOMEM;
  1998. }
  1999. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2000. fragm_no++;
  2001. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2002. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2003. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2004. fragm_sz);
  2005. buf_chain_tail->next = fragm;
  2006. buf_chain_tail = fragm;
  2007. rest -= fragm_sz;
  2008. crs += fragm_sz;
  2009. msg_set_type(&fragm_hdr, FRAGMENT);
  2010. }
  2011. kfree_skb(buf);
  2012. /* Append chain of fragments to send queue & send them */
  2013. l_ptr->long_msg_seq_no++;
  2014. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  2015. l_ptr->stats.sent_fragments += fragm_no;
  2016. l_ptr->stats.sent_fragmented++;
  2017. tipc_link_push_queue(l_ptr);
  2018. return dsz;
  2019. }
  2020. /* tipc_link_frag_rcv(): Called with node lock on. Returns
  2021. * the reassembled buffer if message is complete.
  2022. */
  2023. int tipc_link_frag_rcv(struct sk_buff **head, struct sk_buff **tail,
  2024. struct sk_buff **fbuf)
  2025. {
  2026. struct sk_buff *frag = *fbuf;
  2027. struct tipc_msg *msg = buf_msg(frag);
  2028. u32 fragid = msg_type(msg);
  2029. bool headstolen;
  2030. int delta;
  2031. skb_pull(frag, msg_hdr_sz(msg));
  2032. if (fragid == FIRST_FRAGMENT) {
  2033. if (*head || skb_unclone(frag, GFP_ATOMIC))
  2034. goto out_free;
  2035. *head = frag;
  2036. skb_frag_list_init(*head);
  2037. *fbuf = NULL;
  2038. return 0;
  2039. } else if (*head &&
  2040. skb_try_coalesce(*head, frag, &headstolen, &delta)) {
  2041. kfree_skb_partial(frag, headstolen);
  2042. } else {
  2043. if (!*head)
  2044. goto out_free;
  2045. if (!skb_has_frag_list(*head))
  2046. skb_shinfo(*head)->frag_list = frag;
  2047. else
  2048. (*tail)->next = frag;
  2049. *tail = frag;
  2050. (*head)->truesize += frag->truesize;
  2051. }
  2052. if (fragid == LAST_FRAGMENT) {
  2053. *fbuf = *head;
  2054. *tail = *head = NULL;
  2055. return LINK_REASM_COMPLETE;
  2056. }
  2057. *fbuf = NULL;
  2058. return 0;
  2059. out_free:
  2060. pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
  2061. kfree_skb(*fbuf);
  2062. *fbuf = NULL;
  2063. return LINK_REASM_ERROR;
  2064. }
  2065. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
  2066. {
  2067. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  2068. return;
  2069. l_ptr->tolerance = tolerance;
  2070. l_ptr->continuity_interval =
  2071. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2072. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2073. }
  2074. void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
  2075. {
  2076. /* Data messages from this node, inclusive FIRST_FRAGM */
  2077. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2078. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2079. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2080. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2081. /* Transiting data messages,inclusive FIRST_FRAGM */
  2082. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2083. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2084. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2085. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2086. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2087. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2088. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2089. /* FRAGMENT and LAST_FRAGMENT packets */
  2090. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2091. }
  2092. /**
  2093. * link_find_link - locate link by name
  2094. * @name: ptr to link name string
  2095. * @node: ptr to area to be filled with ptr to associated node
  2096. *
  2097. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2098. * this also prevents link deletion.
  2099. *
  2100. * Returns pointer to link (or 0 if invalid link name).
  2101. */
  2102. static struct tipc_link *link_find_link(const char *name,
  2103. struct tipc_node **node)
  2104. {
  2105. struct tipc_link *l_ptr;
  2106. struct tipc_node *n_ptr;
  2107. int i;
  2108. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  2109. for (i = 0; i < MAX_BEARERS; i++) {
  2110. l_ptr = n_ptr->links[i];
  2111. if (l_ptr && !strcmp(l_ptr->name, name))
  2112. goto found;
  2113. }
  2114. }
  2115. l_ptr = NULL;
  2116. n_ptr = NULL;
  2117. found:
  2118. *node = n_ptr;
  2119. return l_ptr;
  2120. }
  2121. /**
  2122. * link_value_is_valid -- validate proposed link tolerance/priority/window
  2123. *
  2124. * @cmd: value type (TIPC_CMD_SET_LINK_*)
  2125. * @new_value: the new value
  2126. *
  2127. * Returns 1 if value is within range, 0 if not.
  2128. */
  2129. static int link_value_is_valid(u16 cmd, u32 new_value)
  2130. {
  2131. switch (cmd) {
  2132. case TIPC_CMD_SET_LINK_TOL:
  2133. return (new_value >= TIPC_MIN_LINK_TOL) &&
  2134. (new_value <= TIPC_MAX_LINK_TOL);
  2135. case TIPC_CMD_SET_LINK_PRI:
  2136. return (new_value <= TIPC_MAX_LINK_PRI);
  2137. case TIPC_CMD_SET_LINK_WINDOW:
  2138. return (new_value >= TIPC_MIN_LINK_WIN) &&
  2139. (new_value <= TIPC_MAX_LINK_WIN);
  2140. }
  2141. return 0;
  2142. }
  2143. /**
  2144. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  2145. * @name: ptr to link, bearer, or media name
  2146. * @new_value: new value of link, bearer, or media setting
  2147. * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  2148. *
  2149. * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
  2150. *
  2151. * Returns 0 if value updated and negative value on error.
  2152. */
  2153. static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
  2154. {
  2155. struct tipc_node *node;
  2156. struct tipc_link *l_ptr;
  2157. struct tipc_bearer *b_ptr;
  2158. struct tipc_media *m_ptr;
  2159. int res = 0;
  2160. l_ptr = link_find_link(name, &node);
  2161. if (l_ptr) {
  2162. /*
  2163. * acquire node lock for tipc_link_send_proto_msg().
  2164. * see "TIPC locking policy" in net.c.
  2165. */
  2166. tipc_node_lock(node);
  2167. switch (cmd) {
  2168. case TIPC_CMD_SET_LINK_TOL:
  2169. link_set_supervision_props(l_ptr, new_value);
  2170. tipc_link_send_proto_msg(l_ptr,
  2171. STATE_MSG, 0, 0, new_value, 0, 0);
  2172. break;
  2173. case TIPC_CMD_SET_LINK_PRI:
  2174. l_ptr->priority = new_value;
  2175. tipc_link_send_proto_msg(l_ptr,
  2176. STATE_MSG, 0, 0, 0, new_value, 0);
  2177. break;
  2178. case TIPC_CMD_SET_LINK_WINDOW:
  2179. tipc_link_set_queue_limits(l_ptr, new_value);
  2180. break;
  2181. default:
  2182. res = -EINVAL;
  2183. break;
  2184. }
  2185. tipc_node_unlock(node);
  2186. return res;
  2187. }
  2188. b_ptr = tipc_bearer_find(name);
  2189. if (b_ptr) {
  2190. switch (cmd) {
  2191. case TIPC_CMD_SET_LINK_TOL:
  2192. b_ptr->tolerance = new_value;
  2193. break;
  2194. case TIPC_CMD_SET_LINK_PRI:
  2195. b_ptr->priority = new_value;
  2196. break;
  2197. case TIPC_CMD_SET_LINK_WINDOW:
  2198. b_ptr->window = new_value;
  2199. break;
  2200. default:
  2201. res = -EINVAL;
  2202. break;
  2203. }
  2204. return res;
  2205. }
  2206. m_ptr = tipc_media_find(name);
  2207. if (!m_ptr)
  2208. return -ENODEV;
  2209. switch (cmd) {
  2210. case TIPC_CMD_SET_LINK_TOL:
  2211. m_ptr->tolerance = new_value;
  2212. break;
  2213. case TIPC_CMD_SET_LINK_PRI:
  2214. m_ptr->priority = new_value;
  2215. break;
  2216. case TIPC_CMD_SET_LINK_WINDOW:
  2217. m_ptr->window = new_value;
  2218. break;
  2219. default:
  2220. res = -EINVAL;
  2221. break;
  2222. }
  2223. return res;
  2224. }
  2225. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2226. u16 cmd)
  2227. {
  2228. struct tipc_link_config *args;
  2229. u32 new_value;
  2230. int res;
  2231. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2232. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2233. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2234. new_value = ntohl(args->value);
  2235. if (!link_value_is_valid(cmd, new_value))
  2236. return tipc_cfg_reply_error_string(
  2237. "cannot change, value invalid");
  2238. if (!strcmp(args->name, tipc_bclink_name)) {
  2239. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2240. (tipc_bclink_set_queue_limits(new_value) == 0))
  2241. return tipc_cfg_reply_none();
  2242. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2243. " (cannot change setting on broadcast link)");
  2244. }
  2245. read_lock_bh(&tipc_net_lock);
  2246. res = link_cmd_set_value(args->name, new_value, cmd);
  2247. read_unlock_bh(&tipc_net_lock);
  2248. if (res)
  2249. return tipc_cfg_reply_error_string("cannot change link setting");
  2250. return tipc_cfg_reply_none();
  2251. }
  2252. /**
  2253. * link_reset_statistics - reset link statistics
  2254. * @l_ptr: pointer to link
  2255. */
  2256. static void link_reset_statistics(struct tipc_link *l_ptr)
  2257. {
  2258. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2259. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2260. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2261. }
  2262. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2263. {
  2264. char *link_name;
  2265. struct tipc_link *l_ptr;
  2266. struct tipc_node *node;
  2267. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2268. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2269. link_name = (char *)TLV_DATA(req_tlv_area);
  2270. if (!strcmp(link_name, tipc_bclink_name)) {
  2271. if (tipc_bclink_reset_stats())
  2272. return tipc_cfg_reply_error_string("link not found");
  2273. return tipc_cfg_reply_none();
  2274. }
  2275. read_lock_bh(&tipc_net_lock);
  2276. l_ptr = link_find_link(link_name, &node);
  2277. if (!l_ptr) {
  2278. read_unlock_bh(&tipc_net_lock);
  2279. return tipc_cfg_reply_error_string("link not found");
  2280. }
  2281. tipc_node_lock(node);
  2282. link_reset_statistics(l_ptr);
  2283. tipc_node_unlock(node);
  2284. read_unlock_bh(&tipc_net_lock);
  2285. return tipc_cfg_reply_none();
  2286. }
  2287. /**
  2288. * percent - convert count to a percentage of total (rounding up or down)
  2289. */
  2290. static u32 percent(u32 count, u32 total)
  2291. {
  2292. return (count * 100 + (total / 2)) / total;
  2293. }
  2294. /**
  2295. * tipc_link_stats - print link statistics
  2296. * @name: link name
  2297. * @buf: print buffer area
  2298. * @buf_size: size of print buffer area
  2299. *
  2300. * Returns length of print buffer data string (or 0 if error)
  2301. */
  2302. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2303. {
  2304. struct tipc_link *l;
  2305. struct tipc_stats *s;
  2306. struct tipc_node *node;
  2307. char *status;
  2308. u32 profile_total = 0;
  2309. int ret;
  2310. if (!strcmp(name, tipc_bclink_name))
  2311. return tipc_bclink_stats(buf, buf_size);
  2312. read_lock_bh(&tipc_net_lock);
  2313. l = link_find_link(name, &node);
  2314. if (!l) {
  2315. read_unlock_bh(&tipc_net_lock);
  2316. return 0;
  2317. }
  2318. tipc_node_lock(node);
  2319. s = &l->stats;
  2320. if (tipc_link_is_active(l))
  2321. status = "ACTIVE";
  2322. else if (tipc_link_is_up(l))
  2323. status = "STANDBY";
  2324. else
  2325. status = "DEFUNCT";
  2326. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  2327. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2328. " Window:%u packets\n",
  2329. l->name, status, l->max_pkt, l->priority,
  2330. l->tolerance, l->queue_limit[0]);
  2331. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2332. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2333. l->next_in_no - s->recv_info, s->recv_fragments,
  2334. s->recv_fragmented, s->recv_bundles,
  2335. s->recv_bundled);
  2336. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2337. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2338. l->next_out_no - s->sent_info, s->sent_fragments,
  2339. s->sent_fragmented, s->sent_bundles,
  2340. s->sent_bundled);
  2341. profile_total = s->msg_length_counts;
  2342. if (!profile_total)
  2343. profile_total = 1;
  2344. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2345. " TX profile sample:%u packets average:%u octets\n"
  2346. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2347. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2348. s->msg_length_counts,
  2349. s->msg_lengths_total / profile_total,
  2350. percent(s->msg_length_profile[0], profile_total),
  2351. percent(s->msg_length_profile[1], profile_total),
  2352. percent(s->msg_length_profile[2], profile_total),
  2353. percent(s->msg_length_profile[3], profile_total),
  2354. percent(s->msg_length_profile[4], profile_total),
  2355. percent(s->msg_length_profile[5], profile_total),
  2356. percent(s->msg_length_profile[6], profile_total));
  2357. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2358. " RX states:%u probes:%u naks:%u defs:%u"
  2359. " dups:%u\n", s->recv_states, s->recv_probes,
  2360. s->recv_nacks, s->deferred_recv, s->duplicates);
  2361. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2362. " TX states:%u probes:%u naks:%u acks:%u"
  2363. " dups:%u\n", s->sent_states, s->sent_probes,
  2364. s->sent_nacks, s->sent_acks, s->retransmitted);
  2365. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2366. " Congestion link:%u Send queue"
  2367. " max:%u avg:%u\n", s->link_congs,
  2368. s->max_queue_sz, s->queue_sz_counts ?
  2369. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  2370. tipc_node_unlock(node);
  2371. read_unlock_bh(&tipc_net_lock);
  2372. return ret;
  2373. }
  2374. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2375. {
  2376. struct sk_buff *buf;
  2377. struct tlv_desc *rep_tlv;
  2378. int str_len;
  2379. int pb_len;
  2380. char *pb;
  2381. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2382. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2383. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  2384. if (!buf)
  2385. return NULL;
  2386. rep_tlv = (struct tlv_desc *)buf->data;
  2387. pb = TLV_DATA(rep_tlv);
  2388. pb_len = ULTRA_STRING_MAX_LEN;
  2389. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2390. pb, pb_len);
  2391. if (!str_len) {
  2392. kfree_skb(buf);
  2393. return tipc_cfg_reply_error_string("link not found");
  2394. }
  2395. str_len += 1; /* for "\0" */
  2396. skb_put(buf, TLV_SPACE(str_len));
  2397. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2398. return buf;
  2399. }
  2400. /**
  2401. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2402. * @dest: network address of destination node
  2403. * @selector: used to select from set of active links
  2404. *
  2405. * If no active link can be found, uses default maximum packet size.
  2406. */
  2407. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2408. {
  2409. struct tipc_node *n_ptr;
  2410. struct tipc_link *l_ptr;
  2411. u32 res = MAX_PKT_DEFAULT;
  2412. if (dest == tipc_own_addr)
  2413. return MAX_MSG_SIZE;
  2414. read_lock_bh(&tipc_net_lock);
  2415. n_ptr = tipc_node_find(dest);
  2416. if (n_ptr) {
  2417. tipc_node_lock(n_ptr);
  2418. l_ptr = n_ptr->active_links[selector & 1];
  2419. if (l_ptr)
  2420. res = l_ptr->max_pkt;
  2421. tipc_node_unlock(n_ptr);
  2422. }
  2423. read_unlock_bh(&tipc_net_lock);
  2424. return res;
  2425. }
  2426. static void link_print(struct tipc_link *l_ptr, const char *str)
  2427. {
  2428. pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
  2429. if (link_working_unknown(l_ptr))
  2430. pr_cont(":WU\n");
  2431. else if (link_reset_reset(l_ptr))
  2432. pr_cont(":RR\n");
  2433. else if (link_reset_unknown(l_ptr))
  2434. pr_cont(":RU\n");
  2435. else if (link_working_working(l_ptr))
  2436. pr_cont(":WW\n");
  2437. else
  2438. pr_cont("\n");
  2439. }