link.c 60 KB

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