link.c 61 KB

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