link.c 62 KB

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