link.c 74 KB

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