link.c 60 KB

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