link.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2015, Ericsson AB
  5. * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "subscr.h"
  38. #include "link.h"
  39. #include "bcast.h"
  40. #include "socket.h"
  41. #include "name_distr.h"
  42. #include "discover.h"
  43. #include "netlink.h"
  44. #include <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. * State value stored in 'failover_pkts'
  85. */
  86. #define FIRST_FAILOVER 0xffffu
  87. static void link_handle_out_of_seq_msg(struct tipc_link *link,
  88. struct sk_buff *skb);
  89. static void tipc_link_proto_rcv(struct tipc_link *link,
  90. struct sk_buff *skb);
  91. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
  92. static void link_state_event(struct tipc_link *l_ptr, u32 event);
  93. static void link_reset_statistics(struct tipc_link *l_ptr);
  94. static void link_print(struct tipc_link *l_ptr, const char *str);
  95. static void tipc_link_sync_xmit(struct tipc_link *l);
  96. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
  97. static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
  98. static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
  99. static bool tipc_link_failover_rcv(struct tipc_link *l, struct sk_buff **skb);
  100. /*
  101. * Simple link routines
  102. */
  103. static unsigned int align(unsigned int i)
  104. {
  105. return (i + 3) & ~3u;
  106. }
  107. static void tipc_link_release(struct kref *kref)
  108. {
  109. kfree(container_of(kref, struct tipc_link, ref));
  110. }
  111. static void tipc_link_get(struct tipc_link *l_ptr)
  112. {
  113. kref_get(&l_ptr->ref);
  114. }
  115. static void tipc_link_put(struct tipc_link *l_ptr)
  116. {
  117. kref_put(&l_ptr->ref, tipc_link_release);
  118. }
  119. static struct tipc_link *tipc_parallel_link(struct tipc_link *l)
  120. {
  121. if (l->owner->active_links[0] != l)
  122. return l->owner->active_links[0];
  123. return l->owner->active_links[1];
  124. }
  125. /*
  126. * Simple non-static link routines (i.e. referenced outside this file)
  127. */
  128. int tipc_link_is_up(struct tipc_link *l_ptr)
  129. {
  130. if (!l_ptr)
  131. return 0;
  132. return link_working_working(l_ptr) || link_working_unknown(l_ptr);
  133. }
  134. int tipc_link_is_active(struct tipc_link *l_ptr)
  135. {
  136. return (l_ptr->owner->active_links[0] == l_ptr) ||
  137. (l_ptr->owner->active_links[1] == l_ptr);
  138. }
  139. /**
  140. * link_timeout - handle expiration of link timer
  141. * @l_ptr: pointer to link
  142. */
  143. static void link_timeout(unsigned long data)
  144. {
  145. struct tipc_link *l_ptr = (struct tipc_link *)data;
  146. struct sk_buff *skb;
  147. tipc_node_lock(l_ptr->owner);
  148. /* update counters used in statistical profiling of send traffic */
  149. l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->transmq);
  150. l_ptr->stats.queue_sz_counts++;
  151. skb = skb_peek(&l_ptr->transmq);
  152. if (skb) {
  153. struct tipc_msg *msg = buf_msg(skb);
  154. u32 length = msg_size(msg);
  155. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  156. (msg_type(msg) == FIRST_FRAGMENT)) {
  157. length = msg_size(msg_get_wrapped(msg));
  158. }
  159. if (length) {
  160. l_ptr->stats.msg_lengths_total += length;
  161. l_ptr->stats.msg_length_counts++;
  162. if (length <= 64)
  163. l_ptr->stats.msg_length_profile[0]++;
  164. else if (length <= 256)
  165. l_ptr->stats.msg_length_profile[1]++;
  166. else if (length <= 1024)
  167. l_ptr->stats.msg_length_profile[2]++;
  168. else if (length <= 4096)
  169. l_ptr->stats.msg_length_profile[3]++;
  170. else if (length <= 16384)
  171. l_ptr->stats.msg_length_profile[4]++;
  172. else if (length <= 32768)
  173. l_ptr->stats.msg_length_profile[5]++;
  174. else
  175. l_ptr->stats.msg_length_profile[6]++;
  176. }
  177. }
  178. /* do all other link processing performed on a periodic basis */
  179. link_state_event(l_ptr, TIMEOUT_EVT);
  180. if (skb_queue_len(&l_ptr->backlogq))
  181. tipc_link_push_packets(l_ptr);
  182. tipc_node_unlock(l_ptr->owner);
  183. tipc_link_put(l_ptr);
  184. }
  185. static void link_set_timer(struct tipc_link *link, unsigned long time)
  186. {
  187. if (!mod_timer(&link->timer, jiffies + time))
  188. tipc_link_get(link);
  189. }
  190. /**
  191. * tipc_link_create - create a new link
  192. * @n_ptr: pointer to associated node
  193. * @b_ptr: pointer to associated bearer
  194. * @media_addr: media address to use when sending messages over link
  195. *
  196. * Returns pointer to link.
  197. */
  198. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  199. struct tipc_bearer *b_ptr,
  200. const struct tipc_media_addr *media_addr)
  201. {
  202. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  203. struct tipc_link *l_ptr;
  204. struct tipc_msg *msg;
  205. char *if_name;
  206. char addr_string[16];
  207. u32 peer = n_ptr->addr;
  208. if (n_ptr->link_cnt >= MAX_BEARERS) {
  209. tipc_addr_string_fill(addr_string, n_ptr->addr);
  210. pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
  211. n_ptr->link_cnt, addr_string, MAX_BEARERS);
  212. return NULL;
  213. }
  214. if (n_ptr->links[b_ptr->identity]) {
  215. tipc_addr_string_fill(addr_string, n_ptr->addr);
  216. pr_err("Attempt to establish second link on <%s> to %s\n",
  217. b_ptr->name, addr_string);
  218. return NULL;
  219. }
  220. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  221. if (!l_ptr) {
  222. pr_warn("Link creation failed, no memory\n");
  223. return NULL;
  224. }
  225. kref_init(&l_ptr->ref);
  226. l_ptr->addr = peer;
  227. if_name = strchr(b_ptr->name, ':') + 1;
  228. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  229. tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
  230. tipc_node(tn->own_addr),
  231. if_name,
  232. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  233. /* note: peer i/f name is updated by reset/activate message */
  234. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  235. l_ptr->owner = n_ptr;
  236. l_ptr->checkpoint = 1;
  237. l_ptr->peer_session = INVALID_SESSION;
  238. l_ptr->bearer_id = b_ptr->identity;
  239. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  240. l_ptr->state = RESET_UNKNOWN;
  241. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  242. msg = l_ptr->pmsg;
  243. tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
  244. l_ptr->addr);
  245. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  246. msg_set_session(msg, (tn->random & 0xffff));
  247. msg_set_bearer_id(msg, b_ptr->identity);
  248. strcpy((char *)msg_data(msg), if_name);
  249. l_ptr->net_plane = b_ptr->net_plane;
  250. l_ptr->advertised_mtu = b_ptr->mtu;
  251. l_ptr->mtu = l_ptr->advertised_mtu;
  252. l_ptr->priority = b_ptr->priority;
  253. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  254. l_ptr->next_out_no = 1;
  255. __skb_queue_head_init(&l_ptr->transmq);
  256. __skb_queue_head_init(&l_ptr->backlogq);
  257. __skb_queue_head_init(&l_ptr->deferdq);
  258. skb_queue_head_init(&l_ptr->wakeupq);
  259. skb_queue_head_init(&l_ptr->inputq);
  260. skb_queue_head_init(&l_ptr->namedq);
  261. link_reset_statistics(l_ptr);
  262. tipc_node_attach_link(n_ptr, l_ptr);
  263. setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
  264. link_state_event(l_ptr, STARTING_EVT);
  265. return l_ptr;
  266. }
  267. /**
  268. * tipc_link_delete - Delete a link
  269. * @l: link to be deleted
  270. */
  271. void tipc_link_delete(struct tipc_link *l)
  272. {
  273. tipc_link_reset(l);
  274. if (del_timer(&l->timer))
  275. tipc_link_put(l);
  276. l->flags |= LINK_STOPPED;
  277. /* Delete link now, or when timer is finished: */
  278. tipc_link_reset_fragments(l);
  279. tipc_node_detach_link(l->owner, l);
  280. tipc_link_put(l);
  281. }
  282. void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
  283. bool shutting_down)
  284. {
  285. struct tipc_net *tn = net_generic(net, tipc_net_id);
  286. struct tipc_link *link;
  287. struct tipc_node *node;
  288. rcu_read_lock();
  289. list_for_each_entry_rcu(node, &tn->node_list, list) {
  290. tipc_node_lock(node);
  291. link = node->links[bearer_id];
  292. if (link)
  293. tipc_link_delete(link);
  294. tipc_node_unlock(node);
  295. }
  296. rcu_read_unlock();
  297. }
  298. /**
  299. * link_schedule_user - schedule a message sender for wakeup after congestion
  300. * @link: congested link
  301. * @list: message that was attempted sent
  302. * Create pseudo msg to send back to user when congestion abates
  303. * Only consumes message if there is an error
  304. */
  305. static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
  306. {
  307. struct tipc_msg *msg = buf_msg(skb_peek(list));
  308. int imp = msg_importance(msg);
  309. u32 oport = msg_origport(msg);
  310. u32 addr = link_own_addr(link);
  311. struct sk_buff *skb;
  312. /* This really cannot happen... */
  313. if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
  314. pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
  315. tipc_link_reset(link);
  316. goto err;
  317. }
  318. /* Non-blocking sender: */
  319. if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
  320. return -ELINKCONG;
  321. /* Create and schedule wakeup pseudo message */
  322. skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
  323. addr, addr, oport, 0, 0);
  324. if (!skb)
  325. goto err;
  326. TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
  327. TIPC_SKB_CB(skb)->chain_imp = imp;
  328. skb_queue_tail(&link->wakeupq, skb);
  329. link->stats.link_congs++;
  330. return -ELINKCONG;
  331. err:
  332. __skb_queue_purge(list);
  333. return -ENOBUFS;
  334. }
  335. /**
  336. * link_prepare_wakeup - prepare users for wakeup after congestion
  337. * @link: congested link
  338. * Move a number of waiting users, as permitted by available space in
  339. * the send queue, from link wait queue to node wait queue for wakeup
  340. */
  341. void link_prepare_wakeup(struct tipc_link *l)
  342. {
  343. int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
  344. int imp, lim;
  345. struct sk_buff *skb, *tmp;
  346. skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
  347. imp = TIPC_SKB_CB(skb)->chain_imp;
  348. lim = l->window + l->backlog[imp].limit;
  349. pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
  350. if ((pnd[imp] + l->backlog[imp].len) >= lim)
  351. break;
  352. skb_unlink(skb, &l->wakeupq);
  353. skb_queue_tail(&l->inputq, skb);
  354. l->owner->inputq = &l->inputq;
  355. l->owner->action_flags |= TIPC_MSG_EVT;
  356. }
  357. }
  358. /**
  359. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  360. * @l_ptr: pointer to link
  361. */
  362. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  363. {
  364. kfree_skb(l_ptr->reasm_buf);
  365. l_ptr->reasm_buf = NULL;
  366. }
  367. static void tipc_link_purge_backlog(struct tipc_link *l)
  368. {
  369. __skb_queue_purge(&l->backlogq);
  370. l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
  371. l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
  372. l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
  373. l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
  374. l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
  375. }
  376. /**
  377. * tipc_link_purge_queues - purge all pkt queues associated with link
  378. * @l_ptr: pointer to link
  379. */
  380. void tipc_link_purge_queues(struct tipc_link *l_ptr)
  381. {
  382. __skb_queue_purge(&l_ptr->deferdq);
  383. __skb_queue_purge(&l_ptr->transmq);
  384. tipc_link_purge_backlog(l_ptr);
  385. tipc_link_reset_fragments(l_ptr);
  386. }
  387. void tipc_link_reset(struct tipc_link *l_ptr)
  388. {
  389. u32 prev_state = l_ptr->state;
  390. int was_active_link = tipc_link_is_active(l_ptr);
  391. struct tipc_node *owner = l_ptr->owner;
  392. struct tipc_link *pl = tipc_parallel_link(l_ptr);
  393. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  394. /* Link is down, accept any session */
  395. l_ptr->peer_session = INVALID_SESSION;
  396. /* Prepare for renewed mtu size negotiation */
  397. l_ptr->mtu = l_ptr->advertised_mtu;
  398. l_ptr->state = RESET_UNKNOWN;
  399. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  400. return;
  401. tipc_node_link_down(l_ptr->owner, l_ptr);
  402. tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
  403. if (was_active_link && tipc_node_is_up(l_ptr->owner) && (pl != l_ptr)) {
  404. l_ptr->flags |= LINK_FAILINGOVER;
  405. l_ptr->failover_checkpt = l_ptr->next_in_no;
  406. pl->failover_pkts = FIRST_FAILOVER;
  407. pl->failover_checkpt = l_ptr->next_in_no;
  408. pl->failover_skb = l_ptr->reasm_buf;
  409. } else {
  410. kfree_skb(l_ptr->reasm_buf);
  411. }
  412. /* Clean up all queues, except inputq: */
  413. __skb_queue_purge(&l_ptr->transmq);
  414. __skb_queue_purge(&l_ptr->deferdq);
  415. if (!owner->inputq)
  416. owner->inputq = &l_ptr->inputq;
  417. skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq);
  418. if (!skb_queue_empty(owner->inputq))
  419. owner->action_flags |= TIPC_MSG_EVT;
  420. tipc_link_purge_backlog(l_ptr);
  421. l_ptr->reasm_buf = NULL;
  422. l_ptr->rcv_unacked = 0;
  423. l_ptr->checkpoint = 1;
  424. l_ptr->next_out_no = 1;
  425. l_ptr->fsm_msg_cnt = 0;
  426. l_ptr->stale_count = 0;
  427. link_reset_statistics(l_ptr);
  428. }
  429. void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
  430. {
  431. struct tipc_net *tn = net_generic(net, tipc_net_id);
  432. struct tipc_link *l_ptr;
  433. struct tipc_node *n_ptr;
  434. rcu_read_lock();
  435. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  436. tipc_node_lock(n_ptr);
  437. l_ptr = n_ptr->links[bearer_id];
  438. if (l_ptr)
  439. tipc_link_reset(l_ptr);
  440. tipc_node_unlock(n_ptr);
  441. }
  442. rcu_read_unlock();
  443. }
  444. static void link_activate(struct tipc_link *link)
  445. {
  446. struct tipc_node *node = link->owner;
  447. link->next_in_no = 1;
  448. link->stats.recv_info = 1;
  449. tipc_node_link_up(node, link);
  450. tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
  451. }
  452. /**
  453. * link_state_event - link finite state machine
  454. * @l_ptr: pointer to link
  455. * @event: state machine event to process
  456. */
  457. static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
  458. {
  459. struct tipc_link *other;
  460. unsigned long cont_intv = l_ptr->cont_intv;
  461. if (l_ptr->flags & LINK_STOPPED)
  462. return;
  463. if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
  464. return; /* Not yet. */
  465. if (l_ptr->flags & LINK_FAILINGOVER) {
  466. if (event == TIMEOUT_EVT)
  467. link_set_timer(l_ptr, cont_intv);
  468. return;
  469. }
  470. switch (l_ptr->state) {
  471. case WORKING_WORKING:
  472. switch (event) {
  473. case TRAFFIC_MSG_EVT:
  474. case ACTIVATE_MSG:
  475. break;
  476. case TIMEOUT_EVT:
  477. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  478. l_ptr->checkpoint = l_ptr->next_in_no;
  479. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  480. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  481. 0, 0, 0, 0);
  482. l_ptr->fsm_msg_cnt++;
  483. }
  484. link_set_timer(l_ptr, cont_intv);
  485. break;
  486. }
  487. l_ptr->state = WORKING_UNKNOWN;
  488. l_ptr->fsm_msg_cnt = 0;
  489. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
  490. l_ptr->fsm_msg_cnt++;
  491. link_set_timer(l_ptr, cont_intv / 4);
  492. break;
  493. case RESET_MSG:
  494. pr_debug("%s<%s>, requested by peer\n",
  495. link_rst_msg, l_ptr->name);
  496. tipc_link_reset(l_ptr);
  497. l_ptr->state = RESET_RESET;
  498. l_ptr->fsm_msg_cnt = 0;
  499. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  500. 0, 0, 0, 0);
  501. l_ptr->fsm_msg_cnt++;
  502. link_set_timer(l_ptr, cont_intv);
  503. break;
  504. default:
  505. pr_debug("%s%u in WW state\n", link_unk_evt, event);
  506. }
  507. break;
  508. case WORKING_UNKNOWN:
  509. switch (event) {
  510. case TRAFFIC_MSG_EVT:
  511. case ACTIVATE_MSG:
  512. l_ptr->state = WORKING_WORKING;
  513. l_ptr->fsm_msg_cnt = 0;
  514. link_set_timer(l_ptr, cont_intv);
  515. break;
  516. case RESET_MSG:
  517. pr_debug("%s<%s>, requested by peer while probing\n",
  518. link_rst_msg, l_ptr->name);
  519. tipc_link_reset(l_ptr);
  520. l_ptr->state = RESET_RESET;
  521. l_ptr->fsm_msg_cnt = 0;
  522. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  523. 0, 0, 0, 0);
  524. l_ptr->fsm_msg_cnt++;
  525. link_set_timer(l_ptr, cont_intv);
  526. break;
  527. case TIMEOUT_EVT:
  528. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  529. l_ptr->state = WORKING_WORKING;
  530. l_ptr->fsm_msg_cnt = 0;
  531. l_ptr->checkpoint = l_ptr->next_in_no;
  532. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  533. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  534. 0, 0, 0, 0);
  535. l_ptr->fsm_msg_cnt++;
  536. }
  537. link_set_timer(l_ptr, cont_intv);
  538. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  539. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  540. 1, 0, 0, 0);
  541. l_ptr->fsm_msg_cnt++;
  542. link_set_timer(l_ptr, cont_intv / 4);
  543. } else { /* Link has failed */
  544. pr_debug("%s<%s>, peer not responding\n",
  545. link_rst_msg, l_ptr->name);
  546. tipc_link_reset(l_ptr);
  547. l_ptr->state = RESET_UNKNOWN;
  548. l_ptr->fsm_msg_cnt = 0;
  549. tipc_link_proto_xmit(l_ptr, RESET_MSG,
  550. 0, 0, 0, 0);
  551. l_ptr->fsm_msg_cnt++;
  552. link_set_timer(l_ptr, cont_intv);
  553. }
  554. break;
  555. default:
  556. pr_err("%s%u in WU state\n", link_unk_evt, event);
  557. }
  558. break;
  559. case RESET_UNKNOWN:
  560. switch (event) {
  561. case TRAFFIC_MSG_EVT:
  562. break;
  563. case ACTIVATE_MSG:
  564. other = l_ptr->owner->active_links[0];
  565. if (other && link_working_unknown(other))
  566. break;
  567. l_ptr->state = WORKING_WORKING;
  568. l_ptr->fsm_msg_cnt = 0;
  569. link_activate(l_ptr);
  570. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
  571. l_ptr->fsm_msg_cnt++;
  572. if (l_ptr->owner->working_links == 1)
  573. tipc_link_sync_xmit(l_ptr);
  574. link_set_timer(l_ptr, cont_intv);
  575. break;
  576. case RESET_MSG:
  577. l_ptr->state = RESET_RESET;
  578. l_ptr->fsm_msg_cnt = 0;
  579. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  580. 1, 0, 0, 0);
  581. l_ptr->fsm_msg_cnt++;
  582. link_set_timer(l_ptr, cont_intv);
  583. break;
  584. case STARTING_EVT:
  585. l_ptr->flags |= LINK_STARTED;
  586. l_ptr->fsm_msg_cnt++;
  587. link_set_timer(l_ptr, cont_intv);
  588. break;
  589. case TIMEOUT_EVT:
  590. tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0);
  591. l_ptr->fsm_msg_cnt++;
  592. link_set_timer(l_ptr, cont_intv);
  593. break;
  594. default:
  595. pr_err("%s%u in RU state\n", link_unk_evt, event);
  596. }
  597. break;
  598. case RESET_RESET:
  599. switch (event) {
  600. case TRAFFIC_MSG_EVT:
  601. case ACTIVATE_MSG:
  602. other = l_ptr->owner->active_links[0];
  603. if (other && link_working_unknown(other))
  604. break;
  605. l_ptr->state = WORKING_WORKING;
  606. l_ptr->fsm_msg_cnt = 0;
  607. link_activate(l_ptr);
  608. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
  609. l_ptr->fsm_msg_cnt++;
  610. if (l_ptr->owner->working_links == 1)
  611. tipc_link_sync_xmit(l_ptr);
  612. link_set_timer(l_ptr, cont_intv);
  613. break;
  614. case RESET_MSG:
  615. break;
  616. case TIMEOUT_EVT:
  617. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  618. 0, 0, 0, 0);
  619. l_ptr->fsm_msg_cnt++;
  620. link_set_timer(l_ptr, cont_intv);
  621. break;
  622. default:
  623. pr_err("%s%u in RR state\n", link_unk_evt, event);
  624. }
  625. break;
  626. default:
  627. pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
  628. }
  629. }
  630. /**
  631. * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
  632. * @link: link to use
  633. * @list: chain of buffers containing message
  634. *
  635. * Consumes the buffer chain, except when returning -ELINKCONG,
  636. * since the caller then may want to make more send attempts.
  637. * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
  638. * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
  639. */
  640. int __tipc_link_xmit(struct net *net, struct tipc_link *link,
  641. struct sk_buff_head *list)
  642. {
  643. struct tipc_msg *msg = buf_msg(skb_peek(list));
  644. unsigned int maxwin = link->window;
  645. unsigned int imp = msg_importance(msg);
  646. uint mtu = link->mtu;
  647. uint ack = mod(link->next_in_no - 1);
  648. uint seqno = link->next_out_no;
  649. uint bc_last_in = link->owner->bclink.last_in;
  650. struct tipc_media_addr *addr = &link->media_addr;
  651. struct sk_buff_head *transmq = &link->transmq;
  652. struct sk_buff_head *backlogq = &link->backlogq;
  653. struct sk_buff *skb, *tmp;
  654. /* Match backlog limit against msg importance: */
  655. if (unlikely(link->backlog[imp].len >= link->backlog[imp].limit))
  656. return link_schedule_user(link, list);
  657. if (unlikely(msg_size(msg) > mtu)) {
  658. __skb_queue_purge(list);
  659. return -EMSGSIZE;
  660. }
  661. /* Prepare each packet for sending, and add to relevant queue: */
  662. skb_queue_walk_safe(list, skb, tmp) {
  663. __skb_unlink(skb, list);
  664. msg = buf_msg(skb);
  665. msg_set_seqno(msg, seqno);
  666. msg_set_ack(msg, ack);
  667. msg_set_bcast_ack(msg, bc_last_in);
  668. if (likely(skb_queue_len(transmq) < maxwin)) {
  669. __skb_queue_tail(transmq, skb);
  670. tipc_bearer_send(net, link->bearer_id, skb, addr);
  671. link->rcv_unacked = 0;
  672. seqno++;
  673. continue;
  674. }
  675. if (tipc_msg_bundle(skb_peek_tail(backlogq), skb, mtu)) {
  676. link->stats.sent_bundled++;
  677. continue;
  678. }
  679. if (tipc_msg_make_bundle(&skb, mtu, link->addr)) {
  680. link->stats.sent_bundled++;
  681. link->stats.sent_bundles++;
  682. imp = msg_importance(buf_msg(skb));
  683. }
  684. __skb_queue_tail(backlogq, skb);
  685. link->backlog[imp].len++;
  686. seqno++;
  687. }
  688. link->next_out_no = seqno;
  689. return 0;
  690. }
  691. static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
  692. {
  693. skb_queue_head_init(list);
  694. __skb_queue_tail(list, skb);
  695. }
  696. static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
  697. {
  698. struct sk_buff_head head;
  699. skb2list(skb, &head);
  700. return __tipc_link_xmit(link->owner->net, link, &head);
  701. }
  702. /* tipc_link_xmit_skb(): send single buffer to destination
  703. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  704. * messages, which will not be rejected
  705. * The only exception is datagram messages rerouted after secondary
  706. * lookup, which are rare and safe to dispose of anyway.
  707. * TODO: Return real return value, and let callers use
  708. * tipc_wait_for_sendpkt() where applicable
  709. */
  710. int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  711. u32 selector)
  712. {
  713. struct sk_buff_head head;
  714. int rc;
  715. skb2list(skb, &head);
  716. rc = tipc_link_xmit(net, &head, dnode, selector);
  717. if (rc == -ELINKCONG)
  718. kfree_skb(skb);
  719. return 0;
  720. }
  721. /**
  722. * tipc_link_xmit() is the general link level function for message sending
  723. * @net: the applicable net namespace
  724. * @list: chain of buffers containing message
  725. * @dsz: amount of user data to be sent
  726. * @dnode: address of destination node
  727. * @selector: a number used for deterministic link selection
  728. * Consumes the buffer chain, except when returning -ELINKCONG
  729. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  730. */
  731. int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
  732. u32 selector)
  733. {
  734. struct tipc_link *link = NULL;
  735. struct tipc_node *node;
  736. int rc = -EHOSTUNREACH;
  737. node = tipc_node_find(net, dnode);
  738. if (node) {
  739. tipc_node_lock(node);
  740. link = node->active_links[selector & 1];
  741. if (link)
  742. rc = __tipc_link_xmit(net, link, list);
  743. tipc_node_unlock(node);
  744. tipc_node_put(node);
  745. }
  746. if (link)
  747. return rc;
  748. if (likely(in_own_node(net, dnode))) {
  749. tipc_sk_rcv(net, list);
  750. return 0;
  751. }
  752. __skb_queue_purge(list);
  753. return rc;
  754. }
  755. /*
  756. * tipc_link_sync_xmit - synchronize broadcast link endpoints.
  757. *
  758. * Give a newly added peer node the sequence number where it should
  759. * start receiving and acking broadcast packets.
  760. *
  761. * Called with node locked
  762. */
  763. static void tipc_link_sync_xmit(struct tipc_link *link)
  764. {
  765. struct sk_buff *skb;
  766. struct tipc_msg *msg;
  767. skb = tipc_buf_acquire(INT_H_SIZE);
  768. if (!skb)
  769. return;
  770. msg = buf_msg(skb);
  771. tipc_msg_init(link_own_addr(link), msg, BCAST_PROTOCOL, STATE_MSG,
  772. INT_H_SIZE, link->addr);
  773. msg_set_last_bcast(msg, link->owner->bclink.acked);
  774. __tipc_link_xmit_skb(link, skb);
  775. }
  776. /*
  777. * tipc_link_sync_rcv - synchronize broadcast link endpoints.
  778. * Receive the sequence number where we should start receiving and
  779. * acking broadcast packets from a newly added peer node, and open
  780. * up for reception of such packets.
  781. *
  782. * Called with node locked
  783. */
  784. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
  785. {
  786. struct tipc_msg *msg = buf_msg(buf);
  787. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  788. n->bclink.recv_permitted = true;
  789. kfree_skb(buf);
  790. }
  791. /*
  792. * tipc_link_push_packets - push unsent packets to bearer
  793. *
  794. * Push out the unsent messages of a link where congestion
  795. * has abated. Node is locked.
  796. *
  797. * Called with node locked
  798. */
  799. void tipc_link_push_packets(struct tipc_link *link)
  800. {
  801. struct sk_buff *skb;
  802. struct tipc_msg *msg;
  803. unsigned int ack = mod(link->next_in_no - 1);
  804. while (skb_queue_len(&link->transmq) < link->window) {
  805. skb = __skb_dequeue(&link->backlogq);
  806. if (!skb)
  807. break;
  808. msg = buf_msg(skb);
  809. link->backlog[msg_importance(msg)].len--;
  810. msg_set_ack(msg, ack);
  811. msg_set_bcast_ack(msg, link->owner->bclink.last_in);
  812. link->rcv_unacked = 0;
  813. __skb_queue_tail(&link->transmq, skb);
  814. tipc_bearer_send(link->owner->net, link->bearer_id,
  815. skb, &link->media_addr);
  816. }
  817. }
  818. void tipc_link_reset_all(struct tipc_node *node)
  819. {
  820. char addr_string[16];
  821. u32 i;
  822. tipc_node_lock(node);
  823. pr_warn("Resetting all links to %s\n",
  824. tipc_addr_string_fill(addr_string, node->addr));
  825. for (i = 0; i < MAX_BEARERS; i++) {
  826. if (node->links[i]) {
  827. link_print(node->links[i], "Resetting link\n");
  828. tipc_link_reset(node->links[i]);
  829. }
  830. }
  831. tipc_node_unlock(node);
  832. }
  833. static void link_retransmit_failure(struct tipc_link *l_ptr,
  834. struct sk_buff *buf)
  835. {
  836. struct tipc_msg *msg = buf_msg(buf);
  837. struct net *net = l_ptr->owner->net;
  838. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  839. if (l_ptr->addr) {
  840. /* Handle failure on standard link */
  841. link_print(l_ptr, "Resetting link\n");
  842. tipc_link_reset(l_ptr);
  843. } else {
  844. /* Handle failure on broadcast link */
  845. struct tipc_node *n_ptr;
  846. char addr_string[16];
  847. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  848. pr_cont("Outstanding acks: %lu\n",
  849. (unsigned long) TIPC_SKB_CB(buf)->handle);
  850. n_ptr = tipc_bclink_retransmit_to(net);
  851. tipc_addr_string_fill(addr_string, n_ptr->addr);
  852. pr_info("Broadcast link info for %s\n", addr_string);
  853. pr_info("Reception permitted: %d, Acked: %u\n",
  854. n_ptr->bclink.recv_permitted,
  855. n_ptr->bclink.acked);
  856. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  857. n_ptr->bclink.last_in,
  858. n_ptr->bclink.oos_state,
  859. n_ptr->bclink.last_sent);
  860. n_ptr->action_flags |= TIPC_BCAST_RESET;
  861. l_ptr->stale_count = 0;
  862. }
  863. }
  864. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
  865. u32 retransmits)
  866. {
  867. struct tipc_msg *msg;
  868. if (!skb)
  869. return;
  870. msg = buf_msg(skb);
  871. /* Detect repeated retransmit failures */
  872. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  873. if (++l_ptr->stale_count > 100) {
  874. link_retransmit_failure(l_ptr, skb);
  875. return;
  876. }
  877. } else {
  878. l_ptr->last_retransmitted = msg_seqno(msg);
  879. l_ptr->stale_count = 1;
  880. }
  881. skb_queue_walk_from(&l_ptr->transmq, skb) {
  882. if (!retransmits)
  883. break;
  884. msg = buf_msg(skb);
  885. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  886. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  887. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
  888. &l_ptr->media_addr);
  889. retransmits--;
  890. l_ptr->stats.retransmitted++;
  891. }
  892. }
  893. /* link_synch(): check if all packets arrived before the synch
  894. * point have been consumed
  895. * Returns true if the parallel links are synched, otherwise false
  896. */
  897. static bool link_synch(struct tipc_link *l)
  898. {
  899. unsigned int post_synch;
  900. struct tipc_link *pl;
  901. pl = tipc_parallel_link(l);
  902. if (pl == l)
  903. goto synched;
  904. /* Was last pre-synch packet added to input queue ? */
  905. if (less_eq(pl->next_in_no, l->synch_point))
  906. return false;
  907. /* Is it still in the input queue ? */
  908. post_synch = mod(pl->next_in_no - l->synch_point) - 1;
  909. if (skb_queue_len(&pl->inputq) > post_synch)
  910. return false;
  911. synched:
  912. l->flags &= ~LINK_SYNCHING;
  913. return true;
  914. }
  915. static void link_retrieve_defq(struct tipc_link *link,
  916. struct sk_buff_head *list)
  917. {
  918. u32 seq_no;
  919. if (skb_queue_empty(&link->deferdq))
  920. return;
  921. seq_no = buf_seqno(skb_peek(&link->deferdq));
  922. if (seq_no == mod(link->next_in_no))
  923. skb_queue_splice_tail_init(&link->deferdq, list);
  924. }
  925. /**
  926. * tipc_rcv - process TIPC packets/messages arriving from off-node
  927. * @net: the applicable net namespace
  928. * @skb: TIPC packet
  929. * @b_ptr: pointer to bearer message arrived on
  930. *
  931. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  932. * structure (i.e. cannot be NULL), but bearer can be inactive.
  933. */
  934. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
  935. {
  936. struct tipc_net *tn = net_generic(net, tipc_net_id);
  937. struct sk_buff_head head;
  938. struct tipc_node *n_ptr;
  939. struct tipc_link *l_ptr;
  940. struct sk_buff *skb1, *tmp;
  941. struct tipc_msg *msg;
  942. u32 seq_no;
  943. u32 ackd;
  944. u32 released;
  945. skb2list(skb, &head);
  946. while ((skb = __skb_dequeue(&head))) {
  947. /* Ensure message is well-formed */
  948. if (unlikely(!tipc_msg_validate(skb)))
  949. goto discard;
  950. /* Handle arrival of a non-unicast link message */
  951. msg = buf_msg(skb);
  952. if (unlikely(msg_non_seq(msg))) {
  953. if (msg_user(msg) == LINK_CONFIG)
  954. tipc_disc_rcv(net, skb, b_ptr);
  955. else
  956. tipc_bclink_rcv(net, skb);
  957. continue;
  958. }
  959. /* Discard unicast link messages destined for another node */
  960. if (unlikely(!msg_short(msg) &&
  961. (msg_destnode(msg) != tn->own_addr)))
  962. goto discard;
  963. /* Locate neighboring node that sent message */
  964. n_ptr = tipc_node_find(net, msg_prevnode(msg));
  965. if (unlikely(!n_ptr))
  966. goto discard;
  967. tipc_node_lock(n_ptr);
  968. /* Locate unicast link endpoint that should handle message */
  969. l_ptr = n_ptr->links[b_ptr->identity];
  970. if (unlikely(!l_ptr))
  971. goto unlock;
  972. /* Verify that communication with node is currently allowed */
  973. if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
  974. msg_user(msg) == LINK_PROTOCOL &&
  975. (msg_type(msg) == RESET_MSG ||
  976. msg_type(msg) == ACTIVATE_MSG) &&
  977. !msg_redundant_link(msg))
  978. n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
  979. if (tipc_node_blocked(n_ptr))
  980. goto unlock;
  981. /* Validate message sequence number info */
  982. seq_no = msg_seqno(msg);
  983. ackd = msg_ack(msg);
  984. /* Release acked messages */
  985. if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
  986. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  987. released = 0;
  988. skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
  989. if (more(buf_seqno(skb1), ackd))
  990. break;
  991. __skb_unlink(skb1, &l_ptr->transmq);
  992. kfree_skb(skb1);
  993. released = 1;
  994. }
  995. /* Try sending any messages link endpoint has pending */
  996. if (unlikely(skb_queue_len(&l_ptr->backlogq)))
  997. tipc_link_push_packets(l_ptr);
  998. if (released && !skb_queue_empty(&l_ptr->wakeupq))
  999. link_prepare_wakeup(l_ptr);
  1000. /* Process the incoming packet */
  1001. if (unlikely(!link_working_working(l_ptr))) {
  1002. if (msg_user(msg) == LINK_PROTOCOL) {
  1003. tipc_link_proto_rcv(l_ptr, skb);
  1004. link_retrieve_defq(l_ptr, &head);
  1005. skb = NULL;
  1006. goto unlock;
  1007. }
  1008. /* Traffic message. Conditionally activate link */
  1009. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1010. if (link_working_working(l_ptr)) {
  1011. /* Re-insert buffer in front of queue */
  1012. __skb_queue_head(&head, skb);
  1013. skb = NULL;
  1014. goto unlock;
  1015. }
  1016. goto unlock;
  1017. }
  1018. /* Link is now in state WORKING_WORKING */
  1019. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1020. link_handle_out_of_seq_msg(l_ptr, skb);
  1021. link_retrieve_defq(l_ptr, &head);
  1022. skb = NULL;
  1023. goto unlock;
  1024. }
  1025. /* Synchronize with parallel link if applicable */
  1026. if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
  1027. link_handle_out_of_seq_msg(l_ptr, skb);
  1028. if (link_synch(l_ptr))
  1029. link_retrieve_defq(l_ptr, &head);
  1030. skb = NULL;
  1031. goto unlock;
  1032. }
  1033. l_ptr->next_in_no++;
  1034. if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
  1035. link_retrieve_defq(l_ptr, &head);
  1036. if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
  1037. l_ptr->stats.sent_acks++;
  1038. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0);
  1039. }
  1040. tipc_link_input(l_ptr, skb);
  1041. skb = NULL;
  1042. unlock:
  1043. tipc_node_unlock(n_ptr);
  1044. tipc_node_put(n_ptr);
  1045. discard:
  1046. if (unlikely(skb))
  1047. kfree_skb(skb);
  1048. }
  1049. }
  1050. /* tipc_data_input - deliver data and name distr msgs to upper layer
  1051. *
  1052. * Consumes buffer if message is of right type
  1053. * Node lock must be held
  1054. */
  1055. static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
  1056. {
  1057. struct tipc_node *node = link->owner;
  1058. struct tipc_msg *msg = buf_msg(skb);
  1059. u32 dport = msg_destport(msg);
  1060. switch (msg_user(msg)) {
  1061. case TIPC_LOW_IMPORTANCE:
  1062. case TIPC_MEDIUM_IMPORTANCE:
  1063. case TIPC_HIGH_IMPORTANCE:
  1064. case TIPC_CRITICAL_IMPORTANCE:
  1065. case CONN_MANAGER:
  1066. if (tipc_skb_queue_tail(&link->inputq, skb, dport)) {
  1067. node->inputq = &link->inputq;
  1068. node->action_flags |= TIPC_MSG_EVT;
  1069. }
  1070. return true;
  1071. case NAME_DISTRIBUTOR:
  1072. node->bclink.recv_permitted = true;
  1073. node->namedq = &link->namedq;
  1074. skb_queue_tail(&link->namedq, skb);
  1075. if (skb_queue_len(&link->namedq) == 1)
  1076. node->action_flags |= TIPC_NAMED_MSG_EVT;
  1077. return true;
  1078. case MSG_BUNDLER:
  1079. case TUNNEL_PROTOCOL:
  1080. case MSG_FRAGMENTER:
  1081. case BCAST_PROTOCOL:
  1082. return false;
  1083. default:
  1084. pr_warn("Dropping received illegal msg type\n");
  1085. kfree_skb(skb);
  1086. return false;
  1087. };
  1088. }
  1089. /* tipc_link_input - process packet that has passed link protocol check
  1090. *
  1091. * Consumes buffer
  1092. * Node lock must be held
  1093. */
  1094. static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
  1095. {
  1096. struct tipc_node *node = link->owner;
  1097. struct tipc_msg *msg = buf_msg(skb);
  1098. struct sk_buff *iskb;
  1099. int pos = 0;
  1100. if (likely(tipc_data_input(link, skb)))
  1101. return;
  1102. switch (msg_user(msg)) {
  1103. case TUNNEL_PROTOCOL:
  1104. if (msg_dup(msg)) {
  1105. link->flags |= LINK_SYNCHING;
  1106. link->synch_point = msg_seqno(msg_get_wrapped(msg));
  1107. kfree_skb(skb);
  1108. break;
  1109. }
  1110. if (!tipc_link_failover_rcv(link, &skb))
  1111. break;
  1112. if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
  1113. tipc_data_input(link, skb);
  1114. break;
  1115. }
  1116. case MSG_BUNDLER:
  1117. link->stats.recv_bundles++;
  1118. link->stats.recv_bundled += msg_msgcnt(msg);
  1119. while (tipc_msg_extract(skb, &iskb, &pos))
  1120. tipc_data_input(link, iskb);
  1121. break;
  1122. case MSG_FRAGMENTER:
  1123. link->stats.recv_fragments++;
  1124. if (tipc_buf_append(&link->reasm_buf, &skb)) {
  1125. link->stats.recv_fragmented++;
  1126. tipc_data_input(link, skb);
  1127. } else if (!link->reasm_buf) {
  1128. tipc_link_reset(link);
  1129. }
  1130. break;
  1131. case BCAST_PROTOCOL:
  1132. tipc_link_sync_rcv(node, skb);
  1133. break;
  1134. default:
  1135. break;
  1136. };
  1137. }
  1138. /**
  1139. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1140. *
  1141. * Returns increase in queue length (i.e. 0 or 1)
  1142. */
  1143. u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
  1144. {
  1145. struct sk_buff *skb1;
  1146. u32 seq_no = buf_seqno(skb);
  1147. /* Empty queue ? */
  1148. if (skb_queue_empty(list)) {
  1149. __skb_queue_tail(list, skb);
  1150. return 1;
  1151. }
  1152. /* Last ? */
  1153. if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
  1154. __skb_queue_tail(list, skb);
  1155. return 1;
  1156. }
  1157. /* Locate insertion point in queue, then insert; discard if duplicate */
  1158. skb_queue_walk(list, skb1) {
  1159. u32 curr_seqno = buf_seqno(skb1);
  1160. if (seq_no == curr_seqno) {
  1161. kfree_skb(skb);
  1162. return 0;
  1163. }
  1164. if (less(seq_no, curr_seqno))
  1165. break;
  1166. }
  1167. __skb_queue_before(list, skb1, skb);
  1168. return 1;
  1169. }
  1170. /*
  1171. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1172. */
  1173. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1174. struct sk_buff *buf)
  1175. {
  1176. u32 seq_no = buf_seqno(buf);
  1177. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1178. tipc_link_proto_rcv(l_ptr, buf);
  1179. return;
  1180. }
  1181. /* Record OOS packet arrival (force mismatch on next timeout) */
  1182. l_ptr->checkpoint--;
  1183. /*
  1184. * Discard packet if a duplicate; otherwise add it to deferred queue
  1185. * and notify peer of gap as per protocol specification
  1186. */
  1187. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1188. l_ptr->stats.duplicates++;
  1189. kfree_skb(buf);
  1190. return;
  1191. }
  1192. if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
  1193. l_ptr->stats.deferred_recv++;
  1194. if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
  1195. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0);
  1196. } else {
  1197. l_ptr->stats.duplicates++;
  1198. }
  1199. }
  1200. /*
  1201. * Send protocol message to the other endpoint.
  1202. */
  1203. void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
  1204. u32 gap, u32 tolerance, u32 priority)
  1205. {
  1206. struct sk_buff *buf = NULL;
  1207. struct tipc_msg *msg = l_ptr->pmsg;
  1208. u32 msg_size = sizeof(l_ptr->proto_msg);
  1209. int r_flag;
  1210. /* Don't send protocol message during link failover */
  1211. if (l_ptr->flags & LINK_FAILINGOVER)
  1212. return;
  1213. /* Abort non-RESET send if communication with node is prohibited */
  1214. if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
  1215. return;
  1216. /* Create protocol message with "out-of-sequence" sequence number */
  1217. msg_set_type(msg, msg_typ);
  1218. msg_set_net_plane(msg, l_ptr->net_plane);
  1219. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1220. msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
  1221. if (msg_typ == STATE_MSG) {
  1222. u32 next_sent = mod(l_ptr->next_out_no);
  1223. if (!tipc_link_is_up(l_ptr))
  1224. return;
  1225. if (skb_queue_len(&l_ptr->backlogq))
  1226. next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
  1227. msg_set_next_sent(msg, next_sent);
  1228. if (!skb_queue_empty(&l_ptr->deferdq)) {
  1229. u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
  1230. gap = mod(rec - mod(l_ptr->next_in_no));
  1231. }
  1232. msg_set_seq_gap(msg, gap);
  1233. if (gap)
  1234. l_ptr->stats.sent_nacks++;
  1235. msg_set_link_tolerance(msg, tolerance);
  1236. msg_set_linkprio(msg, priority);
  1237. msg_set_max_pkt(msg, l_ptr->mtu);
  1238. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1239. msg_set_probe(msg, probe_msg != 0);
  1240. if (probe_msg)
  1241. l_ptr->stats.sent_probes++;
  1242. l_ptr->stats.sent_states++;
  1243. } else { /* RESET_MSG or ACTIVATE_MSG */
  1244. msg_set_ack(msg, mod(l_ptr->failover_checkpt - 1));
  1245. msg_set_seq_gap(msg, 0);
  1246. msg_set_next_sent(msg, 1);
  1247. msg_set_probe(msg, 0);
  1248. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1249. msg_set_linkprio(msg, l_ptr->priority);
  1250. msg_set_max_pkt(msg, l_ptr->advertised_mtu);
  1251. }
  1252. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1253. msg_set_redundant_link(msg, r_flag);
  1254. msg_set_linkprio(msg, l_ptr->priority);
  1255. msg_set_size(msg, msg_size);
  1256. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1257. buf = tipc_buf_acquire(msg_size);
  1258. if (!buf)
  1259. return;
  1260. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1261. buf->priority = TC_PRIO_CONTROL;
  1262. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
  1263. &l_ptr->media_addr);
  1264. l_ptr->rcv_unacked = 0;
  1265. kfree_skb(buf);
  1266. }
  1267. /*
  1268. * Receive protocol message :
  1269. * Note that network plane id propagates through the network, and may
  1270. * change at any time. The node with lowest address rules
  1271. */
  1272. static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
  1273. struct sk_buff *buf)
  1274. {
  1275. u32 rec_gap = 0;
  1276. u32 msg_tol;
  1277. struct tipc_msg *msg = buf_msg(buf);
  1278. if (l_ptr->flags & LINK_FAILINGOVER)
  1279. goto exit;
  1280. if (l_ptr->net_plane != msg_net_plane(msg))
  1281. if (link_own_addr(l_ptr) > msg_prevnode(msg))
  1282. l_ptr->net_plane = msg_net_plane(msg);
  1283. switch (msg_type(msg)) {
  1284. case RESET_MSG:
  1285. if (!link_working_unknown(l_ptr) &&
  1286. (l_ptr->peer_session != INVALID_SESSION)) {
  1287. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1288. break; /* duplicate or old reset: ignore */
  1289. }
  1290. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1291. link_working_unknown(l_ptr))) {
  1292. /*
  1293. * peer has lost contact -- don't allow peer's links
  1294. * to reactivate before we recognize loss & clean up
  1295. */
  1296. l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
  1297. }
  1298. link_state_event(l_ptr, RESET_MSG);
  1299. /* fall thru' */
  1300. case ACTIVATE_MSG:
  1301. /* Update link settings according other endpoint's values */
  1302. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1303. msg_tol = msg_link_tolerance(msg);
  1304. if (msg_tol > l_ptr->tolerance)
  1305. link_set_supervision_props(l_ptr, msg_tol);
  1306. if (msg_linkprio(msg) > l_ptr->priority)
  1307. l_ptr->priority = msg_linkprio(msg);
  1308. if (l_ptr->mtu > msg_max_pkt(msg))
  1309. l_ptr->mtu = msg_max_pkt(msg);
  1310. /* Synchronize broadcast link info, if not done previously */
  1311. if (!tipc_node_is_up(l_ptr->owner)) {
  1312. l_ptr->owner->bclink.last_sent =
  1313. l_ptr->owner->bclink.last_in =
  1314. msg_last_bcast(msg);
  1315. l_ptr->owner->bclink.oos_state = 0;
  1316. }
  1317. l_ptr->peer_session = msg_session(msg);
  1318. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1319. if (msg_type(msg) == ACTIVATE_MSG)
  1320. link_state_event(l_ptr, ACTIVATE_MSG);
  1321. break;
  1322. case STATE_MSG:
  1323. msg_tol = msg_link_tolerance(msg);
  1324. if (msg_tol)
  1325. link_set_supervision_props(l_ptr, msg_tol);
  1326. if (msg_linkprio(msg) &&
  1327. (msg_linkprio(msg) != l_ptr->priority)) {
  1328. pr_debug("%s<%s>, priority change %u->%u\n",
  1329. link_rst_msg, l_ptr->name,
  1330. l_ptr->priority, msg_linkprio(msg));
  1331. l_ptr->priority = msg_linkprio(msg);
  1332. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1333. break;
  1334. }
  1335. /* Record reception; force mismatch at next timeout: */
  1336. l_ptr->checkpoint--;
  1337. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1338. l_ptr->stats.recv_states++;
  1339. if (link_reset_unknown(l_ptr))
  1340. break;
  1341. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1342. rec_gap = mod(msg_next_sent(msg) -
  1343. mod(l_ptr->next_in_no));
  1344. }
  1345. if (msg_probe(msg))
  1346. l_ptr->stats.recv_probes++;
  1347. /* Protocol message before retransmits, reduce loss risk */
  1348. if (l_ptr->owner->bclink.recv_permitted)
  1349. tipc_bclink_update_link_state(l_ptr->owner,
  1350. msg_last_bcast(msg));
  1351. if (rec_gap || (msg_probe(msg))) {
  1352. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0,
  1353. rec_gap, 0, 0);
  1354. }
  1355. if (msg_seq_gap(msg)) {
  1356. l_ptr->stats.recv_nacks++;
  1357. tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
  1358. msg_seq_gap(msg));
  1359. }
  1360. break;
  1361. }
  1362. exit:
  1363. kfree_skb(buf);
  1364. }
  1365. /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
  1366. * a different bearer. Owner node is locked.
  1367. */
  1368. static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
  1369. struct tipc_msg *tunnel_hdr,
  1370. struct tipc_msg *msg,
  1371. u32 selector)
  1372. {
  1373. struct tipc_link *tunnel;
  1374. struct sk_buff *skb;
  1375. u32 length = msg_size(msg);
  1376. tunnel = l_ptr->owner->active_links[selector & 1];
  1377. if (!tipc_link_is_up(tunnel)) {
  1378. pr_warn("%stunnel link no longer available\n", link_co_err);
  1379. return;
  1380. }
  1381. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1382. skb = tipc_buf_acquire(length + INT_H_SIZE);
  1383. if (!skb) {
  1384. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1385. return;
  1386. }
  1387. skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
  1388. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
  1389. __tipc_link_xmit_skb(tunnel, skb);
  1390. }
  1391. /* tipc_link_failover_send_queue(): A link has gone down, but a second
  1392. * link is still active. We can do failover. Tunnel the failing link's
  1393. * whole send queue via the remaining link. This way, we don't lose
  1394. * any packets, and sequence order is preserved for subsequent traffic
  1395. * sent over the remaining link. Owner node is locked.
  1396. */
  1397. void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
  1398. {
  1399. int msgcount;
  1400. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1401. struct tipc_msg tunnel_hdr;
  1402. struct sk_buff *skb;
  1403. int split_bundles;
  1404. if (!tunnel)
  1405. return;
  1406. tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, TUNNEL_PROTOCOL,
  1407. FAILOVER_MSG, INT_H_SIZE, l_ptr->addr);
  1408. skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
  1409. tipc_link_purge_backlog(l_ptr);
  1410. msgcount = skb_queue_len(&l_ptr->transmq);
  1411. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1412. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1413. if (skb_queue_empty(&l_ptr->transmq)) {
  1414. skb = tipc_buf_acquire(INT_H_SIZE);
  1415. if (skb) {
  1416. skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
  1417. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1418. __tipc_link_xmit_skb(tunnel, skb);
  1419. } else {
  1420. pr_warn("%sunable to send changeover msg\n",
  1421. link_co_err);
  1422. }
  1423. return;
  1424. }
  1425. split_bundles = (l_ptr->owner->active_links[0] !=
  1426. l_ptr->owner->active_links[1]);
  1427. skb_queue_walk(&l_ptr->transmq, skb) {
  1428. struct tipc_msg *msg = buf_msg(skb);
  1429. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1430. struct tipc_msg *m = msg_get_wrapped(msg);
  1431. unchar *pos = (unchar *)m;
  1432. msgcount = msg_msgcnt(msg);
  1433. while (msgcount--) {
  1434. msg_set_seqno(m, msg_seqno(msg));
  1435. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
  1436. msg_link_selector(m));
  1437. pos += align(msg_size(m));
  1438. m = (struct tipc_msg *)pos;
  1439. }
  1440. } else {
  1441. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
  1442. msg_link_selector(msg));
  1443. }
  1444. }
  1445. }
  1446. /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
  1447. * duplicate of the first link's send queue via the new link. This way, we
  1448. * are guaranteed that currently queued packets from a socket are delivered
  1449. * before future traffic from the same socket, even if this is using the
  1450. * new link. The last arriving copy of each duplicate packet is dropped at
  1451. * the receiving end by the regular protocol check, so packet cardinality
  1452. * and sequence order is preserved per sender/receiver socket pair.
  1453. * Owner node is locked.
  1454. */
  1455. void tipc_link_dup_queue_xmit(struct tipc_link *link,
  1456. struct tipc_link *tnl)
  1457. {
  1458. struct sk_buff *skb;
  1459. struct tipc_msg tnl_hdr;
  1460. struct sk_buff_head *queue = &link->transmq;
  1461. int mcnt;
  1462. tipc_msg_init(link_own_addr(link), &tnl_hdr, TUNNEL_PROTOCOL,
  1463. SYNCH_MSG, INT_H_SIZE, link->addr);
  1464. mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
  1465. msg_set_msgcnt(&tnl_hdr, mcnt);
  1466. msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
  1467. tunnel_queue:
  1468. skb_queue_walk(queue, skb) {
  1469. struct sk_buff *outskb;
  1470. struct tipc_msg *msg = buf_msg(skb);
  1471. u32 len = msg_size(msg);
  1472. msg_set_ack(msg, mod(link->next_in_no - 1));
  1473. msg_set_bcast_ack(msg, link->owner->bclink.last_in);
  1474. msg_set_size(&tnl_hdr, len + INT_H_SIZE);
  1475. outskb = tipc_buf_acquire(len + INT_H_SIZE);
  1476. if (outskb == NULL) {
  1477. pr_warn("%sunable to send duplicate msg\n",
  1478. link_co_err);
  1479. return;
  1480. }
  1481. skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
  1482. skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
  1483. skb->data, len);
  1484. __tipc_link_xmit_skb(tnl, outskb);
  1485. if (!tipc_link_is_up(link))
  1486. return;
  1487. }
  1488. if (queue == &link->backlogq)
  1489. return;
  1490. queue = &link->backlogq;
  1491. goto tunnel_queue;
  1492. }
  1493. /* tipc_link_failover_rcv(): Receive a tunnelled FAILOVER_MSG packet
  1494. * Owner node is locked.
  1495. */
  1496. static bool tipc_link_failover_rcv(struct tipc_link *link,
  1497. struct sk_buff **skb)
  1498. {
  1499. struct tipc_msg *msg = buf_msg(*skb);
  1500. struct sk_buff *iskb = NULL;
  1501. struct tipc_link *pl = NULL;
  1502. int bearer_id = msg_bearer_id(msg);
  1503. int pos = 0;
  1504. if (msg_type(msg) != FAILOVER_MSG) {
  1505. pr_warn("%sunknown tunnel pkt received\n", link_co_err);
  1506. goto exit;
  1507. }
  1508. if (bearer_id >= MAX_BEARERS)
  1509. goto exit;
  1510. if (bearer_id == link->bearer_id)
  1511. goto exit;
  1512. pl = link->owner->links[bearer_id];
  1513. if (pl && tipc_link_is_up(pl))
  1514. tipc_link_reset(pl);
  1515. if (link->failover_pkts == FIRST_FAILOVER)
  1516. link->failover_pkts = msg_msgcnt(msg);
  1517. /* Should we expect an inner packet? */
  1518. if (!link->failover_pkts)
  1519. goto exit;
  1520. if (!tipc_msg_extract(*skb, &iskb, &pos)) {
  1521. pr_warn("%sno inner failover pkt\n", link_co_err);
  1522. *skb = NULL;
  1523. goto exit;
  1524. }
  1525. link->failover_pkts--;
  1526. *skb = NULL;
  1527. /* Was this packet already delivered? */
  1528. if (less(buf_seqno(iskb), link->failover_checkpt)) {
  1529. kfree_skb(iskb);
  1530. iskb = NULL;
  1531. goto exit;
  1532. }
  1533. if (msg_user(buf_msg(iskb)) == MSG_FRAGMENTER) {
  1534. link->stats.recv_fragments++;
  1535. tipc_buf_append(&link->failover_skb, &iskb);
  1536. }
  1537. exit:
  1538. if (!link->failover_pkts && pl)
  1539. pl->flags &= ~LINK_FAILINGOVER;
  1540. kfree_skb(*skb);
  1541. *skb = iskb;
  1542. return *skb;
  1543. }
  1544. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
  1545. {
  1546. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  1547. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1548. return;
  1549. l_ptr->tolerance = tol;
  1550. l_ptr->cont_intv = msecs_to_jiffies(intv);
  1551. l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
  1552. }
  1553. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1554. {
  1555. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
  1556. l->window = win;
  1557. l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
  1558. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
  1559. l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
  1560. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
  1561. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1562. }
  1563. /* tipc_link_find_owner - locate owner node of link by link's name
  1564. * @net: the applicable net namespace
  1565. * @name: pointer to link name string
  1566. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1567. *
  1568. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1569. */
  1570. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1571. const char *link_name,
  1572. unsigned int *bearer_id)
  1573. {
  1574. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1575. struct tipc_link *l_ptr;
  1576. struct tipc_node *n_ptr;
  1577. struct tipc_node *found_node = NULL;
  1578. int i;
  1579. *bearer_id = 0;
  1580. rcu_read_lock();
  1581. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1582. tipc_node_lock(n_ptr);
  1583. for (i = 0; i < MAX_BEARERS; i++) {
  1584. l_ptr = n_ptr->links[i];
  1585. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1586. *bearer_id = i;
  1587. found_node = n_ptr;
  1588. break;
  1589. }
  1590. }
  1591. tipc_node_unlock(n_ptr);
  1592. if (found_node)
  1593. break;
  1594. }
  1595. rcu_read_unlock();
  1596. return found_node;
  1597. }
  1598. /**
  1599. * link_reset_statistics - reset link statistics
  1600. * @l_ptr: pointer to link
  1601. */
  1602. static void link_reset_statistics(struct tipc_link *l_ptr)
  1603. {
  1604. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1605. l_ptr->stats.sent_info = l_ptr->next_out_no;
  1606. l_ptr->stats.recv_info = l_ptr->next_in_no;
  1607. }
  1608. static void link_print(struct tipc_link *l_ptr, const char *str)
  1609. {
  1610. struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
  1611. struct tipc_bearer *b_ptr;
  1612. rcu_read_lock();
  1613. b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
  1614. if (b_ptr)
  1615. pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
  1616. rcu_read_unlock();
  1617. if (link_working_unknown(l_ptr))
  1618. pr_cont(":WU\n");
  1619. else if (link_reset_reset(l_ptr))
  1620. pr_cont(":RR\n");
  1621. else if (link_reset_unknown(l_ptr))
  1622. pr_cont(":RU\n");
  1623. else if (link_working_working(l_ptr))
  1624. pr_cont(":WW\n");
  1625. else
  1626. pr_cont("\n");
  1627. }
  1628. /* Parse and validate nested (link) properties valid for media, bearer and link
  1629. */
  1630. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1631. {
  1632. int err;
  1633. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1634. tipc_nl_prop_policy);
  1635. if (err)
  1636. return err;
  1637. if (props[TIPC_NLA_PROP_PRIO]) {
  1638. u32 prio;
  1639. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1640. if (prio > TIPC_MAX_LINK_PRI)
  1641. return -EINVAL;
  1642. }
  1643. if (props[TIPC_NLA_PROP_TOL]) {
  1644. u32 tol;
  1645. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1646. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1647. return -EINVAL;
  1648. }
  1649. if (props[TIPC_NLA_PROP_WIN]) {
  1650. u32 win;
  1651. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1652. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1653. return -EINVAL;
  1654. }
  1655. return 0;
  1656. }
  1657. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  1658. {
  1659. int err;
  1660. int res = 0;
  1661. int bearer_id;
  1662. char *name;
  1663. struct tipc_link *link;
  1664. struct tipc_node *node;
  1665. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1666. struct net *net = sock_net(skb->sk);
  1667. if (!info->attrs[TIPC_NLA_LINK])
  1668. return -EINVAL;
  1669. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1670. info->attrs[TIPC_NLA_LINK],
  1671. tipc_nl_link_policy);
  1672. if (err)
  1673. return err;
  1674. if (!attrs[TIPC_NLA_LINK_NAME])
  1675. return -EINVAL;
  1676. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1677. node = tipc_link_find_owner(net, name, &bearer_id);
  1678. if (!node)
  1679. return -EINVAL;
  1680. tipc_node_lock(node);
  1681. link = node->links[bearer_id];
  1682. if (!link) {
  1683. res = -EINVAL;
  1684. goto out;
  1685. }
  1686. if (attrs[TIPC_NLA_LINK_PROP]) {
  1687. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1688. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1689. props);
  1690. if (err) {
  1691. res = err;
  1692. goto out;
  1693. }
  1694. if (props[TIPC_NLA_PROP_TOL]) {
  1695. u32 tol;
  1696. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1697. link_set_supervision_props(link, tol);
  1698. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
  1699. }
  1700. if (props[TIPC_NLA_PROP_PRIO]) {
  1701. u32 prio;
  1702. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1703. link->priority = prio;
  1704. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
  1705. }
  1706. if (props[TIPC_NLA_PROP_WIN]) {
  1707. u32 win;
  1708. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1709. tipc_link_set_queue_limits(link, win);
  1710. }
  1711. }
  1712. out:
  1713. tipc_node_unlock(node);
  1714. return res;
  1715. }
  1716. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1717. {
  1718. int i;
  1719. struct nlattr *stats;
  1720. struct nla_map {
  1721. u32 key;
  1722. u32 val;
  1723. };
  1724. struct nla_map map[] = {
  1725. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  1726. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1727. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1728. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1729. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1730. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  1731. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1732. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1733. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1734. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1735. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1736. s->msg_length_counts : 1},
  1737. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1738. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1739. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1740. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1741. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1742. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1743. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1744. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1745. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1746. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1747. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1748. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1749. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1750. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1751. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1752. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1753. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1754. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1755. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1756. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1757. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1758. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1759. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1760. };
  1761. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1762. if (!stats)
  1763. return -EMSGSIZE;
  1764. for (i = 0; i < ARRAY_SIZE(map); i++)
  1765. if (nla_put_u32(skb, map[i].key, map[i].val))
  1766. goto msg_full;
  1767. nla_nest_end(skb, stats);
  1768. return 0;
  1769. msg_full:
  1770. nla_nest_cancel(skb, stats);
  1771. return -EMSGSIZE;
  1772. }
  1773. /* Caller should hold appropriate locks to protect the link */
  1774. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1775. struct tipc_link *link)
  1776. {
  1777. int err;
  1778. void *hdr;
  1779. struct nlattr *attrs;
  1780. struct nlattr *prop;
  1781. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1782. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1783. NLM_F_MULTI, TIPC_NL_LINK_GET);
  1784. if (!hdr)
  1785. return -EMSGSIZE;
  1786. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1787. if (!attrs)
  1788. goto msg_full;
  1789. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1790. goto attr_msg_full;
  1791. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1792. tipc_cluster_mask(tn->own_addr)))
  1793. goto attr_msg_full;
  1794. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
  1795. goto attr_msg_full;
  1796. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
  1797. goto attr_msg_full;
  1798. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
  1799. goto attr_msg_full;
  1800. if (tipc_link_is_up(link))
  1801. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1802. goto attr_msg_full;
  1803. if (tipc_link_is_active(link))
  1804. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1805. goto attr_msg_full;
  1806. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1807. if (!prop)
  1808. goto attr_msg_full;
  1809. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1810. goto prop_msg_full;
  1811. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1812. goto prop_msg_full;
  1813. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1814. link->window))
  1815. goto prop_msg_full;
  1816. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1817. goto prop_msg_full;
  1818. nla_nest_end(msg->skb, prop);
  1819. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1820. if (err)
  1821. goto attr_msg_full;
  1822. nla_nest_end(msg->skb, attrs);
  1823. genlmsg_end(msg->skb, hdr);
  1824. return 0;
  1825. prop_msg_full:
  1826. nla_nest_cancel(msg->skb, prop);
  1827. attr_msg_full:
  1828. nla_nest_cancel(msg->skb, attrs);
  1829. msg_full:
  1830. genlmsg_cancel(msg->skb, hdr);
  1831. return -EMSGSIZE;
  1832. }
  1833. /* Caller should hold node lock */
  1834. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1835. struct tipc_node *node, u32 *prev_link)
  1836. {
  1837. u32 i;
  1838. int err;
  1839. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1840. *prev_link = i;
  1841. if (!node->links[i])
  1842. continue;
  1843. err = __tipc_nl_add_link(net, msg, node->links[i]);
  1844. if (err)
  1845. return err;
  1846. }
  1847. *prev_link = 0;
  1848. return 0;
  1849. }
  1850. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1851. {
  1852. struct net *net = sock_net(skb->sk);
  1853. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1854. struct tipc_node *node;
  1855. struct tipc_nl_msg msg;
  1856. u32 prev_node = cb->args[0];
  1857. u32 prev_link = cb->args[1];
  1858. int done = cb->args[2];
  1859. int err;
  1860. if (done)
  1861. return 0;
  1862. msg.skb = skb;
  1863. msg.portid = NETLINK_CB(cb->skb).portid;
  1864. msg.seq = cb->nlh->nlmsg_seq;
  1865. rcu_read_lock();
  1866. if (prev_node) {
  1867. node = tipc_node_find(net, prev_node);
  1868. if (!node) {
  1869. /* We never set seq or call nl_dump_check_consistent()
  1870. * this means that setting prev_seq here will cause the
  1871. * consistence check to fail in the netlink callback
  1872. * handler. Resulting in the last NLMSG_DONE message
  1873. * having the NLM_F_DUMP_INTR flag set.
  1874. */
  1875. cb->prev_seq = 1;
  1876. goto out;
  1877. }
  1878. tipc_node_put(node);
  1879. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1880. list) {
  1881. tipc_node_lock(node);
  1882. err = __tipc_nl_add_node_links(net, &msg, node,
  1883. &prev_link);
  1884. tipc_node_unlock(node);
  1885. tipc_node_put(node);
  1886. if (err)
  1887. goto out;
  1888. prev_node = node->addr;
  1889. }
  1890. } else {
  1891. err = tipc_nl_add_bc_link(net, &msg);
  1892. if (err)
  1893. goto out;
  1894. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1895. tipc_node_lock(node);
  1896. err = __tipc_nl_add_node_links(net, &msg, node,
  1897. &prev_link);
  1898. tipc_node_unlock(node);
  1899. if (err)
  1900. goto out;
  1901. prev_node = node->addr;
  1902. }
  1903. }
  1904. done = 1;
  1905. out:
  1906. rcu_read_unlock();
  1907. cb->args[0] = prev_node;
  1908. cb->args[1] = prev_link;
  1909. cb->args[2] = done;
  1910. return skb->len;
  1911. }
  1912. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  1913. {
  1914. struct net *net = genl_info_net(info);
  1915. struct sk_buff *ans_skb;
  1916. struct tipc_nl_msg msg;
  1917. struct tipc_link *link;
  1918. struct tipc_node *node;
  1919. char *name;
  1920. int bearer_id;
  1921. int err;
  1922. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1923. return -EINVAL;
  1924. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1925. node = tipc_link_find_owner(net, name, &bearer_id);
  1926. if (!node)
  1927. return -EINVAL;
  1928. ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1929. if (!ans_skb)
  1930. return -ENOMEM;
  1931. msg.skb = ans_skb;
  1932. msg.portid = info->snd_portid;
  1933. msg.seq = info->snd_seq;
  1934. tipc_node_lock(node);
  1935. link = node->links[bearer_id];
  1936. if (!link) {
  1937. err = -EINVAL;
  1938. goto err_out;
  1939. }
  1940. err = __tipc_nl_add_link(net, &msg, link);
  1941. if (err)
  1942. goto err_out;
  1943. tipc_node_unlock(node);
  1944. return genlmsg_reply(ans_skb, info);
  1945. err_out:
  1946. tipc_node_unlock(node);
  1947. nlmsg_free(ans_skb);
  1948. return err;
  1949. }
  1950. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  1951. {
  1952. int err;
  1953. char *link_name;
  1954. unsigned int bearer_id;
  1955. struct tipc_link *link;
  1956. struct tipc_node *node;
  1957. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1958. struct net *net = sock_net(skb->sk);
  1959. if (!info->attrs[TIPC_NLA_LINK])
  1960. return -EINVAL;
  1961. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1962. info->attrs[TIPC_NLA_LINK],
  1963. tipc_nl_link_policy);
  1964. if (err)
  1965. return err;
  1966. if (!attrs[TIPC_NLA_LINK_NAME])
  1967. return -EINVAL;
  1968. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1969. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1970. err = tipc_bclink_reset_stats(net);
  1971. if (err)
  1972. return err;
  1973. return 0;
  1974. }
  1975. node = tipc_link_find_owner(net, link_name, &bearer_id);
  1976. if (!node)
  1977. return -EINVAL;
  1978. tipc_node_lock(node);
  1979. link = node->links[bearer_id];
  1980. if (!link) {
  1981. tipc_node_unlock(node);
  1982. return -EINVAL;
  1983. }
  1984. link_reset_statistics(link);
  1985. tipc_node_unlock(node);
  1986. return 0;
  1987. }