link.c 74 KB

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