link.c 60 KB

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