link.c 73 KB

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