link.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012-2014, 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 "link.h"
  38. #include "bcast.h"
  39. #include "socket.h"
  40. #include "name_distr.h"
  41. #include "discover.h"
  42. #include "config.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 net *net,
  95. struct tipc_link *l_ptr,
  96. struct sk_buff *buf);
  97. static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
  98. struct sk_buff *buf);
  99. static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
  100. struct sk_buff **buf);
  101. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
  102. static void link_state_event(struct tipc_link *l_ptr, u32 event);
  103. static void link_reset_statistics(struct tipc_link *l_ptr);
  104. static void link_print(struct tipc_link *l_ptr, const char *str);
  105. static void tipc_link_sync_xmit(struct tipc_link *l);
  106. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
  107. static int tipc_link_input(struct net *net, struct tipc_link *l,
  108. struct sk_buff *buf);
  109. static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
  110. struct sk_buff **buf);
  111. /*
  112. * Simple link routines
  113. */
  114. static unsigned int align(unsigned int i)
  115. {
  116. return (i + 3) & ~3u;
  117. }
  118. static void link_init_max_pkt(struct tipc_link *l_ptr)
  119. {
  120. struct tipc_node *node = l_ptr->owner;
  121. struct tipc_net *tn = net_generic(node->net, tipc_net_id);
  122. struct tipc_bearer *b_ptr;
  123. u32 max_pkt;
  124. rcu_read_lock();
  125. b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
  126. if (!b_ptr) {
  127. rcu_read_unlock();
  128. return;
  129. }
  130. max_pkt = (b_ptr->mtu & ~3);
  131. rcu_read_unlock();
  132. if (max_pkt > MAX_MSG_SIZE)
  133. max_pkt = MAX_MSG_SIZE;
  134. l_ptr->max_pkt_target = max_pkt;
  135. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  136. l_ptr->max_pkt = l_ptr->max_pkt_target;
  137. else
  138. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  139. l_ptr->max_pkt_probes = 0;
  140. }
  141. /*
  142. * Simple non-static link routines (i.e. referenced outside this file)
  143. */
  144. int tipc_link_is_up(struct tipc_link *l_ptr)
  145. {
  146. if (!l_ptr)
  147. return 0;
  148. return link_working_working(l_ptr) || link_working_unknown(l_ptr);
  149. }
  150. int tipc_link_is_active(struct tipc_link *l_ptr)
  151. {
  152. return (l_ptr->owner->active_links[0] == l_ptr) ||
  153. (l_ptr->owner->active_links[1] == l_ptr);
  154. }
  155. /**
  156. * link_timeout - handle expiration of link timer
  157. * @l_ptr: pointer to link
  158. */
  159. static void link_timeout(unsigned long data)
  160. {
  161. struct tipc_link *l_ptr = (struct tipc_link *)data;
  162. struct sk_buff *skb;
  163. tipc_node_lock(l_ptr->owner);
  164. /* update counters used in statistical profiling of send traffic */
  165. l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
  166. l_ptr->stats.queue_sz_counts++;
  167. skb = skb_peek(&l_ptr->outqueue);
  168. if (skb) {
  169. struct tipc_msg *msg = buf_msg(skb);
  170. u32 length = msg_size(msg);
  171. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  172. (msg_type(msg) == FIRST_FRAGMENT)) {
  173. length = msg_size(msg_get_wrapped(msg));
  174. }
  175. if (length) {
  176. l_ptr->stats.msg_lengths_total += length;
  177. l_ptr->stats.msg_length_counts++;
  178. if (length <= 64)
  179. l_ptr->stats.msg_length_profile[0]++;
  180. else if (length <= 256)
  181. l_ptr->stats.msg_length_profile[1]++;
  182. else if (length <= 1024)
  183. l_ptr->stats.msg_length_profile[2]++;
  184. else if (length <= 4096)
  185. l_ptr->stats.msg_length_profile[3]++;
  186. else if (length <= 16384)
  187. l_ptr->stats.msg_length_profile[4]++;
  188. else if (length <= 32768)
  189. l_ptr->stats.msg_length_profile[5]++;
  190. else
  191. l_ptr->stats.msg_length_profile[6]++;
  192. }
  193. }
  194. /* do all other link processing performed on a periodic basis */
  195. link_state_event(l_ptr, TIMEOUT_EVT);
  196. if (l_ptr->next_out)
  197. tipc_link_push_packets(l_ptr);
  198. tipc_node_unlock(l_ptr->owner);
  199. }
  200. static void link_set_timer(struct tipc_link *link, unsigned long time)
  201. {
  202. mod_timer(&link->timer, jiffies + time);
  203. }
  204. /**
  205. * tipc_link_create - create a new link
  206. * @n_ptr: pointer to associated node
  207. * @b_ptr: pointer to associated bearer
  208. * @media_addr: media address to use when sending messages over link
  209. *
  210. * Returns pointer to link.
  211. */
  212. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  213. struct tipc_bearer *b_ptr,
  214. const struct tipc_media_addr *media_addr)
  215. {
  216. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  217. struct tipc_link *l_ptr;
  218. struct tipc_msg *msg;
  219. char *if_name;
  220. char addr_string[16];
  221. u32 peer = n_ptr->addr;
  222. if (n_ptr->link_cnt >= MAX_BEARERS) {
  223. tipc_addr_string_fill(addr_string, n_ptr->addr);
  224. pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
  225. n_ptr->link_cnt, addr_string, MAX_BEARERS);
  226. return NULL;
  227. }
  228. if (n_ptr->links[b_ptr->identity]) {
  229. tipc_addr_string_fill(addr_string, n_ptr->addr);
  230. pr_err("Attempt to establish second link on <%s> to %s\n",
  231. b_ptr->name, addr_string);
  232. return NULL;
  233. }
  234. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  235. if (!l_ptr) {
  236. pr_warn("Link creation failed, no memory\n");
  237. return NULL;
  238. }
  239. l_ptr->addr = peer;
  240. if_name = strchr(b_ptr->name, ':') + 1;
  241. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  242. tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
  243. tipc_node(tn->own_addr),
  244. if_name,
  245. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  246. /* note: peer i/f name is updated by reset/activate message */
  247. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  248. l_ptr->owner = n_ptr;
  249. l_ptr->checkpoint = 1;
  250. l_ptr->peer_session = INVALID_SESSION;
  251. l_ptr->bearer_id = b_ptr->identity;
  252. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  253. l_ptr->state = RESET_UNKNOWN;
  254. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  255. msg = l_ptr->pmsg;
  256. tipc_msg_init(n_ptr->net, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
  257. l_ptr->addr);
  258. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  259. msg_set_session(msg, (tn->random & 0xffff));
  260. msg_set_bearer_id(msg, b_ptr->identity);
  261. strcpy((char *)msg_data(msg), if_name);
  262. l_ptr->priority = b_ptr->priority;
  263. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  264. l_ptr->net_plane = b_ptr->net_plane;
  265. link_init_max_pkt(l_ptr);
  266. l_ptr->next_out_no = 1;
  267. __skb_queue_head_init(&l_ptr->outqueue);
  268. __skb_queue_head_init(&l_ptr->deferred_queue);
  269. skb_queue_head_init(&l_ptr->waiting_sks);
  270. link_reset_statistics(l_ptr);
  271. tipc_node_attach_link(n_ptr, l_ptr);
  272. setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
  273. link_state_event(l_ptr, STARTING_EVT);
  274. return l_ptr;
  275. }
  276. void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
  277. bool shutting_down)
  278. {
  279. struct tipc_net *tn = net_generic(net, tipc_net_id);
  280. struct tipc_link *l_ptr;
  281. struct tipc_node *n_ptr;
  282. rcu_read_lock();
  283. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  284. tipc_node_lock(n_ptr);
  285. l_ptr = n_ptr->links[bearer_id];
  286. if (l_ptr) {
  287. tipc_link_reset(l_ptr);
  288. if (shutting_down || !tipc_node_is_up(n_ptr)) {
  289. tipc_node_detach_link(l_ptr->owner, l_ptr);
  290. tipc_link_reset_fragments(l_ptr);
  291. tipc_node_unlock(n_ptr);
  292. /* Nobody else can access this link now: */
  293. del_timer_sync(&l_ptr->timer);
  294. kfree(l_ptr);
  295. } else {
  296. /* Detach/delete when failover is finished: */
  297. l_ptr->flags |= LINK_STOPPED;
  298. tipc_node_unlock(n_ptr);
  299. del_timer_sync(&l_ptr->timer);
  300. }
  301. continue;
  302. }
  303. tipc_node_unlock(n_ptr);
  304. }
  305. rcu_read_unlock();
  306. }
  307. /**
  308. * link_schedule_user - schedule user for wakeup after congestion
  309. * @link: congested link
  310. * @oport: sending port
  311. * @chain_sz: size of buffer chain that was attempted sent
  312. * @imp: importance of message attempted sent
  313. * Create pseudo msg to send back to user when congestion abates
  314. */
  315. static bool link_schedule_user(struct tipc_link *link, u32 oport,
  316. uint chain_sz, uint imp)
  317. {
  318. struct net *net = link->owner->net;
  319. struct tipc_net *tn = net_generic(net, tipc_net_id);
  320. struct sk_buff *buf;
  321. buf = tipc_msg_create(net, SOCK_WAKEUP, 0, INT_H_SIZE, 0, tn->own_addr,
  322. tn->own_addr, oport, 0, 0);
  323. if (!buf)
  324. return false;
  325. TIPC_SKB_CB(buf)->chain_sz = chain_sz;
  326. TIPC_SKB_CB(buf)->chain_imp = imp;
  327. skb_queue_tail(&link->waiting_sks, buf);
  328. link->stats.link_congs++;
  329. return true;
  330. }
  331. /**
  332. * link_prepare_wakeup - prepare users for wakeup after congestion
  333. * @link: congested link
  334. * Move a number of waiting users, as permitted by available space in
  335. * the send queue, from link wait queue to node wait queue for wakeup
  336. */
  337. static void link_prepare_wakeup(struct tipc_link *link)
  338. {
  339. uint pend_qsz = skb_queue_len(&link->outqueue);
  340. struct sk_buff *skb, *tmp;
  341. skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
  342. if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
  343. break;
  344. pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
  345. skb_unlink(skb, &link->waiting_sks);
  346. skb_queue_tail(&link->owner->waiting_sks, skb);
  347. }
  348. }
  349. /**
  350. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  351. * @l_ptr: pointer to link
  352. */
  353. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  354. {
  355. kfree_skb(l_ptr->reasm_buf);
  356. l_ptr->reasm_buf = NULL;
  357. }
  358. /**
  359. * tipc_link_purge_queues - purge all pkt queues associated with link
  360. * @l_ptr: pointer to link
  361. */
  362. void tipc_link_purge_queues(struct tipc_link *l_ptr)
  363. {
  364. __skb_queue_purge(&l_ptr->deferred_queue);
  365. __skb_queue_purge(&l_ptr->outqueue);
  366. tipc_link_reset_fragments(l_ptr);
  367. }
  368. void tipc_link_reset(struct tipc_link *l_ptr)
  369. {
  370. u32 prev_state = l_ptr->state;
  371. u32 checkpoint = l_ptr->next_in_no;
  372. int was_active_link = tipc_link_is_active(l_ptr);
  373. struct tipc_node *owner = l_ptr->owner;
  374. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  375. /* Link is down, accept any session */
  376. l_ptr->peer_session = INVALID_SESSION;
  377. /* Prepare for max packet size negotiation */
  378. link_init_max_pkt(l_ptr);
  379. l_ptr->state = RESET_UNKNOWN;
  380. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  381. return;
  382. tipc_node_link_down(l_ptr->owner, l_ptr);
  383. tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
  384. if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
  385. l_ptr->reset_checkpoint = checkpoint;
  386. l_ptr->exp_msg_count = START_CHANGEOVER;
  387. }
  388. /* Clean up all queues: */
  389. __skb_queue_purge(&l_ptr->outqueue);
  390. __skb_queue_purge(&l_ptr->deferred_queue);
  391. if (!skb_queue_empty(&l_ptr->waiting_sks)) {
  392. skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
  393. owner->action_flags |= TIPC_WAKEUP_USERS;
  394. }
  395. l_ptr->next_out = NULL;
  396. l_ptr->unacked_window = 0;
  397. l_ptr->checkpoint = 1;
  398. l_ptr->next_out_no = 1;
  399. l_ptr->fsm_msg_cnt = 0;
  400. l_ptr->stale_count = 0;
  401. link_reset_statistics(l_ptr);
  402. }
  403. void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
  404. {
  405. struct tipc_net *tn = net_generic(net, tipc_net_id);
  406. struct tipc_link *l_ptr;
  407. struct tipc_node *n_ptr;
  408. rcu_read_lock();
  409. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  410. tipc_node_lock(n_ptr);
  411. l_ptr = n_ptr->links[bearer_id];
  412. if (l_ptr)
  413. tipc_link_reset(l_ptr);
  414. tipc_node_unlock(n_ptr);
  415. }
  416. rcu_read_unlock();
  417. }
  418. static void link_activate(struct tipc_link *link)
  419. {
  420. struct tipc_node *node = link->owner;
  421. link->next_in_no = 1;
  422. link->stats.recv_info = 1;
  423. tipc_node_link_up(node, link);
  424. tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
  425. }
  426. /**
  427. * link_state_event - link finite state machine
  428. * @l_ptr: pointer to link
  429. * @event: state machine event to process
  430. */
  431. static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
  432. {
  433. struct tipc_link *other;
  434. unsigned long cont_intv = l_ptr->cont_intv;
  435. if (l_ptr->flags & LINK_STOPPED)
  436. return;
  437. if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
  438. return; /* Not yet. */
  439. /* Check whether changeover is going on */
  440. if (l_ptr->exp_msg_count) {
  441. if (event == TIMEOUT_EVT)
  442. link_set_timer(l_ptr, cont_intv);
  443. return;
  444. }
  445. switch (l_ptr->state) {
  446. case WORKING_WORKING:
  447. switch (event) {
  448. case TRAFFIC_MSG_EVT:
  449. case ACTIVATE_MSG:
  450. break;
  451. case TIMEOUT_EVT:
  452. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  453. l_ptr->checkpoint = l_ptr->next_in_no;
  454. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  455. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  456. 0, 0, 0, 0, 0);
  457. l_ptr->fsm_msg_cnt++;
  458. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  459. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  460. 1, 0, 0, 0, 0);
  461. l_ptr->fsm_msg_cnt++;
  462. }
  463. link_set_timer(l_ptr, cont_intv);
  464. break;
  465. }
  466. l_ptr->state = WORKING_UNKNOWN;
  467. l_ptr->fsm_msg_cnt = 0;
  468. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  469. l_ptr->fsm_msg_cnt++;
  470. link_set_timer(l_ptr, cont_intv / 4);
  471. break;
  472. case RESET_MSG:
  473. pr_info("%s<%s>, requested by peer\n", link_rst_msg,
  474. l_ptr->name);
  475. tipc_link_reset(l_ptr);
  476. l_ptr->state = RESET_RESET;
  477. l_ptr->fsm_msg_cnt = 0;
  478. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  479. 0, 0, 0, 0, 0);
  480. l_ptr->fsm_msg_cnt++;
  481. link_set_timer(l_ptr, cont_intv);
  482. break;
  483. default:
  484. pr_err("%s%u in WW state\n", link_unk_evt, event);
  485. }
  486. break;
  487. case WORKING_UNKNOWN:
  488. switch (event) {
  489. case TRAFFIC_MSG_EVT:
  490. case ACTIVATE_MSG:
  491. l_ptr->state = WORKING_WORKING;
  492. l_ptr->fsm_msg_cnt = 0;
  493. link_set_timer(l_ptr, cont_intv);
  494. break;
  495. case RESET_MSG:
  496. pr_info("%s<%s>, requested by peer while probing\n",
  497. link_rst_msg, l_ptr->name);
  498. tipc_link_reset(l_ptr);
  499. l_ptr->state = RESET_RESET;
  500. l_ptr->fsm_msg_cnt = 0;
  501. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  502. 0, 0, 0, 0, 0);
  503. l_ptr->fsm_msg_cnt++;
  504. link_set_timer(l_ptr, cont_intv);
  505. break;
  506. case TIMEOUT_EVT:
  507. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  508. l_ptr->state = WORKING_WORKING;
  509. l_ptr->fsm_msg_cnt = 0;
  510. l_ptr->checkpoint = l_ptr->next_in_no;
  511. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  512. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  513. 0, 0, 0, 0, 0);
  514. l_ptr->fsm_msg_cnt++;
  515. }
  516. link_set_timer(l_ptr, cont_intv);
  517. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  518. tipc_link_proto_xmit(l_ptr, STATE_MSG,
  519. 1, 0, 0, 0, 0);
  520. l_ptr->fsm_msg_cnt++;
  521. link_set_timer(l_ptr, cont_intv / 4);
  522. } else { /* Link has failed */
  523. pr_warn("%s<%s>, peer not responding\n",
  524. link_rst_msg, l_ptr->name);
  525. tipc_link_reset(l_ptr);
  526. l_ptr->state = RESET_UNKNOWN;
  527. l_ptr->fsm_msg_cnt = 0;
  528. tipc_link_proto_xmit(l_ptr, RESET_MSG,
  529. 0, 0, 0, 0, 0);
  530. l_ptr->fsm_msg_cnt++;
  531. link_set_timer(l_ptr, cont_intv);
  532. }
  533. break;
  534. default:
  535. pr_err("%s%u in WU state\n", link_unk_evt, event);
  536. }
  537. break;
  538. case RESET_UNKNOWN:
  539. switch (event) {
  540. case TRAFFIC_MSG_EVT:
  541. break;
  542. case ACTIVATE_MSG:
  543. other = l_ptr->owner->active_links[0];
  544. if (other && link_working_unknown(other))
  545. break;
  546. l_ptr->state = WORKING_WORKING;
  547. l_ptr->fsm_msg_cnt = 0;
  548. link_activate(l_ptr);
  549. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  550. l_ptr->fsm_msg_cnt++;
  551. if (l_ptr->owner->working_links == 1)
  552. tipc_link_sync_xmit(l_ptr);
  553. link_set_timer(l_ptr, cont_intv);
  554. break;
  555. case RESET_MSG:
  556. l_ptr->state = RESET_RESET;
  557. l_ptr->fsm_msg_cnt = 0;
  558. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  559. 1, 0, 0, 0, 0);
  560. l_ptr->fsm_msg_cnt++;
  561. link_set_timer(l_ptr, cont_intv);
  562. break;
  563. case STARTING_EVT:
  564. l_ptr->flags |= LINK_STARTED;
  565. /* fall through */
  566. case TIMEOUT_EVT:
  567. tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  568. l_ptr->fsm_msg_cnt++;
  569. link_set_timer(l_ptr, cont_intv);
  570. break;
  571. default:
  572. pr_err("%s%u in RU state\n", link_unk_evt, event);
  573. }
  574. break;
  575. case RESET_RESET:
  576. switch (event) {
  577. case TRAFFIC_MSG_EVT:
  578. case ACTIVATE_MSG:
  579. other = l_ptr->owner->active_links[0];
  580. if (other && link_working_unknown(other))
  581. break;
  582. l_ptr->state = WORKING_WORKING;
  583. l_ptr->fsm_msg_cnt = 0;
  584. link_activate(l_ptr);
  585. tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  586. l_ptr->fsm_msg_cnt++;
  587. if (l_ptr->owner->working_links == 1)
  588. tipc_link_sync_xmit(l_ptr);
  589. link_set_timer(l_ptr, cont_intv);
  590. break;
  591. case RESET_MSG:
  592. break;
  593. case TIMEOUT_EVT:
  594. tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
  595. 0, 0, 0, 0, 0);
  596. l_ptr->fsm_msg_cnt++;
  597. link_set_timer(l_ptr, cont_intv);
  598. break;
  599. default:
  600. pr_err("%s%u in RR state\n", link_unk_evt, event);
  601. }
  602. break;
  603. default:
  604. pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
  605. }
  606. }
  607. /* tipc_link_cong: determine return value and how to treat the
  608. * sent buffer during link congestion.
  609. * - For plain, errorless user data messages we keep the buffer and
  610. * return -ELINKONG.
  611. * - For all other messages we discard the buffer and return -EHOSTUNREACH
  612. * - For TIPC internal messages we also reset the link
  613. */
  614. static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
  615. {
  616. struct sk_buff *skb = skb_peek(list);
  617. struct tipc_msg *msg = buf_msg(skb);
  618. uint imp = tipc_msg_tot_importance(msg);
  619. u32 oport = msg_tot_origport(msg);
  620. if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
  621. pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
  622. tipc_link_reset(link);
  623. goto drop;
  624. }
  625. if (unlikely(msg_errcode(msg)))
  626. goto drop;
  627. if (unlikely(msg_reroute_cnt(msg)))
  628. goto drop;
  629. if (TIPC_SKB_CB(skb)->wakeup_pending)
  630. return -ELINKCONG;
  631. if (link_schedule_user(link, oport, skb_queue_len(list), imp))
  632. return -ELINKCONG;
  633. drop:
  634. __skb_queue_purge(list);
  635. return -EHOSTUNREACH;
  636. }
  637. /**
  638. * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
  639. * @link: link to use
  640. * @list: chain of buffers containing message
  641. *
  642. * Consumes the buffer chain, except when returning -ELINKCONG
  643. * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
  644. * user data messages) or -EHOSTUNREACH (all other messages/senders)
  645. * Only the socket functions tipc_send_stream() and tipc_send_packet() need
  646. * to act on the return value, since they may need to do more send attempts.
  647. */
  648. int __tipc_link_xmit(struct net *net, struct tipc_link *link,
  649. struct sk_buff_head *list)
  650. {
  651. struct tipc_msg *msg = buf_msg(skb_peek(list));
  652. uint psz = msg_size(msg);
  653. uint sndlim = link->queue_limit[0];
  654. uint imp = tipc_msg_tot_importance(msg);
  655. uint mtu = link->max_pkt;
  656. uint ack = mod(link->next_in_no - 1);
  657. uint seqno = link->next_out_no;
  658. uint bc_last_in = link->owner->bclink.last_in;
  659. struct tipc_media_addr *addr = &link->media_addr;
  660. struct sk_buff_head *outqueue = &link->outqueue;
  661. struct sk_buff *skb, *tmp;
  662. /* Match queue limits against msg importance: */
  663. if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
  664. return tipc_link_cong(link, list);
  665. /* Has valid packet limit been used ? */
  666. if (unlikely(psz > mtu)) {
  667. __skb_queue_purge(list);
  668. return -EMSGSIZE;
  669. }
  670. /* Prepare each packet for sending, and add to outqueue: */
  671. skb_queue_walk_safe(list, skb, tmp) {
  672. __skb_unlink(skb, list);
  673. msg = buf_msg(skb);
  674. msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
  675. msg_set_bcast_ack(msg, bc_last_in);
  676. if (skb_queue_len(outqueue) < sndlim) {
  677. __skb_queue_tail(outqueue, skb);
  678. tipc_bearer_send(net, link->bearer_id,
  679. skb, addr);
  680. link->next_out = NULL;
  681. link->unacked_window = 0;
  682. } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
  683. link->stats.sent_bundled++;
  684. continue;
  685. } else if (tipc_msg_make_bundle(net, outqueue, skb, mtu,
  686. link->addr)) {
  687. link->stats.sent_bundled++;
  688. link->stats.sent_bundles++;
  689. if (!link->next_out)
  690. link->next_out = skb_peek_tail(outqueue);
  691. } else {
  692. __skb_queue_tail(outqueue, skb);
  693. if (!link->next_out)
  694. link->next_out = skb;
  695. }
  696. seqno++;
  697. }
  698. link->next_out_no = seqno;
  699. return 0;
  700. }
  701. static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
  702. {
  703. __skb_queue_head_init(list);
  704. __skb_queue_tail(list, skb);
  705. }
  706. static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
  707. {
  708. struct sk_buff_head head;
  709. skb2list(skb, &head);
  710. return __tipc_link_xmit(link->owner->net, link, &head);
  711. }
  712. int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  713. u32 selector)
  714. {
  715. struct sk_buff_head head;
  716. skb2list(skb, &head);
  717. return tipc_link_xmit(net, &head, dnode, selector);
  718. }
  719. /**
  720. * tipc_link_xmit() is the general link level function for message sending
  721. * @net: the applicable net namespace
  722. * @list: chain of buffers containing message
  723. * @dsz: amount of user data to be sent
  724. * @dnode: address of destination node
  725. * @selector: a number used for deterministic link selection
  726. * Consumes the buffer chain, except when returning -ELINKCONG
  727. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  728. */
  729. int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
  730. u32 selector)
  731. {
  732. struct tipc_link *link = NULL;
  733. struct tipc_node *node;
  734. int rc = -EHOSTUNREACH;
  735. node = tipc_node_find(net, dnode);
  736. if (node) {
  737. tipc_node_lock(node);
  738. link = node->active_links[selector & 1];
  739. if (link)
  740. rc = __tipc_link_xmit(net, link, list);
  741. tipc_node_unlock(node);
  742. }
  743. if (link)
  744. return rc;
  745. if (likely(in_own_node(net, dnode))) {
  746. /* As a node local message chain never contains more than one
  747. * buffer, we just need to dequeue one SKB buffer from the
  748. * head list.
  749. */
  750. return tipc_sk_rcv(net, __skb_dequeue(list));
  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->owner->net, 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. struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
  792. const struct sk_buff *skb)
  793. {
  794. if (skb_queue_is_last(list, skb))
  795. return NULL;
  796. return skb->next;
  797. }
  798. /*
  799. * tipc_link_push_packets - push unsent packets to bearer
  800. *
  801. * Push out the unsent messages of a link where congestion
  802. * has abated. Node is locked.
  803. *
  804. * Called with node locked
  805. */
  806. void tipc_link_push_packets(struct tipc_link *l_ptr)
  807. {
  808. struct sk_buff_head *outqueue = &l_ptr->outqueue;
  809. struct sk_buff *skb = l_ptr->next_out;
  810. struct tipc_msg *msg;
  811. u32 next, first;
  812. skb_queue_walk_from(outqueue, skb) {
  813. msg = buf_msg(skb);
  814. next = msg_seqno(msg);
  815. first = buf_seqno(skb_peek(outqueue));
  816. if (mod(next - first) < l_ptr->queue_limit[0]) {
  817. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  818. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  819. if (msg_user(msg) == MSG_BUNDLER)
  820. TIPC_SKB_CB(skb)->bundling = false;
  821. tipc_bearer_send(l_ptr->owner->net,
  822. l_ptr->bearer_id, skb,
  823. &l_ptr->media_addr);
  824. l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
  825. } else {
  826. break;
  827. }
  828. }
  829. }
  830. void tipc_link_reset_all(struct tipc_node *node)
  831. {
  832. char addr_string[16];
  833. u32 i;
  834. tipc_node_lock(node);
  835. pr_warn("Resetting all links to %s\n",
  836. tipc_addr_string_fill(addr_string, node->addr));
  837. for (i = 0; i < MAX_BEARERS; i++) {
  838. if (node->links[i]) {
  839. link_print(node->links[i], "Resetting link\n");
  840. tipc_link_reset(node->links[i]);
  841. }
  842. }
  843. tipc_node_unlock(node);
  844. }
  845. static void link_retransmit_failure(struct tipc_link *l_ptr,
  846. struct sk_buff *buf)
  847. {
  848. struct tipc_msg *msg = buf_msg(buf);
  849. struct net *net = l_ptr->owner->net;
  850. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  851. if (l_ptr->addr) {
  852. /* Handle failure on standard link */
  853. link_print(l_ptr, "Resetting link\n");
  854. tipc_link_reset(l_ptr);
  855. } else {
  856. /* Handle failure on broadcast link */
  857. struct tipc_node *n_ptr;
  858. char addr_string[16];
  859. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  860. pr_cont("Outstanding acks: %lu\n",
  861. (unsigned long) TIPC_SKB_CB(buf)->handle);
  862. n_ptr = tipc_bclink_retransmit_to(net);
  863. tipc_node_lock(n_ptr);
  864. tipc_addr_string_fill(addr_string, n_ptr->addr);
  865. pr_info("Broadcast link info for %s\n", addr_string);
  866. pr_info("Reception permitted: %d, Acked: %u\n",
  867. n_ptr->bclink.recv_permitted,
  868. n_ptr->bclink.acked);
  869. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  870. n_ptr->bclink.last_in,
  871. n_ptr->bclink.oos_state,
  872. n_ptr->bclink.last_sent);
  873. tipc_node_unlock(n_ptr);
  874. tipc_bclink_set_flags(net, TIPC_BCLINK_RESET);
  875. l_ptr->stale_count = 0;
  876. }
  877. }
  878. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
  879. u32 retransmits)
  880. {
  881. struct tipc_msg *msg;
  882. if (!skb)
  883. return;
  884. msg = buf_msg(skb);
  885. /* Detect repeated retransmit failures */
  886. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  887. if (++l_ptr->stale_count > 100) {
  888. link_retransmit_failure(l_ptr, skb);
  889. return;
  890. }
  891. } else {
  892. l_ptr->last_retransmitted = msg_seqno(msg);
  893. l_ptr->stale_count = 1;
  894. }
  895. skb_queue_walk_from(&l_ptr->outqueue, skb) {
  896. if (!retransmits || skb == l_ptr->next_out)
  897. break;
  898. msg = buf_msg(skb);
  899. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  900. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  901. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
  902. &l_ptr->media_addr);
  903. retransmits--;
  904. l_ptr->stats.retransmitted++;
  905. }
  906. }
  907. static void link_retrieve_defq(struct tipc_link *link,
  908. struct sk_buff_head *list)
  909. {
  910. u32 seq_no;
  911. if (skb_queue_empty(&link->deferred_queue))
  912. return;
  913. seq_no = buf_seqno(skb_peek(&link->deferred_queue));
  914. if (seq_no == mod(link->next_in_no))
  915. skb_queue_splice_tail_init(&link->deferred_queue, list);
  916. }
  917. /**
  918. * link_recv_buf_validate - validate basic format of received message
  919. *
  920. * This routine ensures a TIPC message has an acceptable header, and at least
  921. * as much data as the header indicates it should. The routine also ensures
  922. * that the entire message header is stored in the main fragment of the message
  923. * buffer, to simplify future access to message header fields.
  924. *
  925. * Note: Having extra info present in the message header or data areas is OK.
  926. * TIPC will ignore the excess, under the assumption that it is optional info
  927. * introduced by a later release of the protocol.
  928. */
  929. static int link_recv_buf_validate(struct sk_buff *buf)
  930. {
  931. static u32 min_data_hdr_size[8] = {
  932. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  933. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  934. };
  935. struct tipc_msg *msg;
  936. u32 tipc_hdr[2];
  937. u32 size;
  938. u32 hdr_size;
  939. u32 min_hdr_size;
  940. /* If this packet comes from the defer queue, the skb has already
  941. * been validated
  942. */
  943. if (unlikely(TIPC_SKB_CB(buf)->deferred))
  944. return 1;
  945. if (unlikely(buf->len < MIN_H_SIZE))
  946. return 0;
  947. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  948. if (msg == NULL)
  949. return 0;
  950. if (unlikely(msg_version(msg) != TIPC_VERSION))
  951. return 0;
  952. size = msg_size(msg);
  953. hdr_size = msg_hdr_sz(msg);
  954. min_hdr_size = msg_isdata(msg) ?
  955. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  956. if (unlikely((hdr_size < min_hdr_size) ||
  957. (size < hdr_size) ||
  958. (buf->len < size) ||
  959. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  960. return 0;
  961. return pskb_may_pull(buf, hdr_size);
  962. }
  963. /**
  964. * tipc_rcv - process TIPC packets/messages arriving from off-node
  965. * @net: the applicable net namespace
  966. * @skb: TIPC packet
  967. * @b_ptr: pointer to bearer message arrived on
  968. *
  969. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  970. * structure (i.e. cannot be NULL), but bearer can be inactive.
  971. */
  972. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
  973. {
  974. struct tipc_net *tn = net_generic(net, tipc_net_id);
  975. struct sk_buff_head head;
  976. struct tipc_node *n_ptr;
  977. struct tipc_link *l_ptr;
  978. struct sk_buff *skb1, *tmp;
  979. struct tipc_msg *msg;
  980. u32 seq_no;
  981. u32 ackd;
  982. u32 released;
  983. skb2list(skb, &head);
  984. while ((skb = __skb_dequeue(&head))) {
  985. /* Ensure message is well-formed */
  986. if (unlikely(!link_recv_buf_validate(skb)))
  987. goto discard;
  988. /* Ensure message data is a single contiguous unit */
  989. if (unlikely(skb_linearize(skb)))
  990. goto discard;
  991. /* Handle arrival of a non-unicast link message */
  992. msg = buf_msg(skb);
  993. if (unlikely(msg_non_seq(msg))) {
  994. if (msg_user(msg) == LINK_CONFIG)
  995. tipc_disc_rcv(net, skb, b_ptr);
  996. else
  997. tipc_bclink_rcv(net, skb);
  998. continue;
  999. }
  1000. /* Discard unicast link messages destined for another node */
  1001. if (unlikely(!msg_short(msg) &&
  1002. (msg_destnode(msg) != tn->own_addr)))
  1003. goto discard;
  1004. /* Locate neighboring node that sent message */
  1005. n_ptr = tipc_node_find(net, msg_prevnode(msg));
  1006. if (unlikely(!n_ptr))
  1007. goto discard;
  1008. tipc_node_lock(n_ptr);
  1009. /* Locate unicast link endpoint that should handle message */
  1010. l_ptr = n_ptr->links[b_ptr->identity];
  1011. if (unlikely(!l_ptr))
  1012. goto unlock_discard;
  1013. /* Verify that communication with node is currently allowed */
  1014. if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
  1015. msg_user(msg) == LINK_PROTOCOL &&
  1016. (msg_type(msg) == RESET_MSG ||
  1017. msg_type(msg) == ACTIVATE_MSG) &&
  1018. !msg_redundant_link(msg))
  1019. n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
  1020. if (tipc_node_blocked(n_ptr))
  1021. goto unlock_discard;
  1022. /* Validate message sequence number info */
  1023. seq_no = msg_seqno(msg);
  1024. ackd = msg_ack(msg);
  1025. /* Release acked messages */
  1026. if (n_ptr->bclink.recv_permitted)
  1027. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1028. released = 0;
  1029. skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
  1030. if (skb1 == l_ptr->next_out ||
  1031. more(buf_seqno(skb1), ackd))
  1032. break;
  1033. __skb_unlink(skb1, &l_ptr->outqueue);
  1034. kfree_skb(skb1);
  1035. released = 1;
  1036. }
  1037. /* Try sending any messages link endpoint has pending */
  1038. if (unlikely(l_ptr->next_out))
  1039. tipc_link_push_packets(l_ptr);
  1040. if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
  1041. link_prepare_wakeup(l_ptr);
  1042. l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
  1043. }
  1044. /* Process the incoming packet */
  1045. if (unlikely(!link_working_working(l_ptr))) {
  1046. if (msg_user(msg) == LINK_PROTOCOL) {
  1047. tipc_link_proto_rcv(net, l_ptr, skb);
  1048. link_retrieve_defq(l_ptr, &head);
  1049. tipc_node_unlock(n_ptr);
  1050. continue;
  1051. }
  1052. /* Traffic message. Conditionally activate link */
  1053. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1054. if (link_working_working(l_ptr)) {
  1055. /* Re-insert buffer in front of queue */
  1056. __skb_queue_head(&head, skb);
  1057. tipc_node_unlock(n_ptr);
  1058. continue;
  1059. }
  1060. goto unlock_discard;
  1061. }
  1062. /* Link is now in state WORKING_WORKING */
  1063. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1064. link_handle_out_of_seq_msg(net, l_ptr, skb);
  1065. link_retrieve_defq(l_ptr, &head);
  1066. tipc_node_unlock(n_ptr);
  1067. continue;
  1068. }
  1069. l_ptr->next_in_no++;
  1070. if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
  1071. link_retrieve_defq(l_ptr, &head);
  1072. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1073. l_ptr->stats.sent_acks++;
  1074. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1075. }
  1076. if (tipc_link_prepare_input(net, l_ptr, &skb)) {
  1077. tipc_node_unlock(n_ptr);
  1078. continue;
  1079. }
  1080. tipc_node_unlock(n_ptr);
  1081. if (tipc_link_input(net, l_ptr, skb) != 0)
  1082. goto discard;
  1083. continue;
  1084. unlock_discard:
  1085. tipc_node_unlock(n_ptr);
  1086. discard:
  1087. kfree_skb(skb);
  1088. }
  1089. }
  1090. /**
  1091. * tipc_link_prepare_input - process TIPC link messages
  1092. *
  1093. * returns nonzero if the message was consumed
  1094. *
  1095. * Node lock must be held
  1096. */
  1097. static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
  1098. struct sk_buff **buf)
  1099. {
  1100. struct tipc_node *n;
  1101. struct tipc_msg *msg;
  1102. int res = -EINVAL;
  1103. n = l->owner;
  1104. msg = buf_msg(*buf);
  1105. switch (msg_user(msg)) {
  1106. case CHANGEOVER_PROTOCOL:
  1107. if (tipc_link_tunnel_rcv(net, n, buf))
  1108. res = 0;
  1109. break;
  1110. case MSG_FRAGMENTER:
  1111. l->stats.recv_fragments++;
  1112. if (tipc_buf_append(&l->reasm_buf, buf)) {
  1113. l->stats.recv_fragmented++;
  1114. res = 0;
  1115. } else if (!l->reasm_buf) {
  1116. tipc_link_reset(l);
  1117. }
  1118. break;
  1119. case MSG_BUNDLER:
  1120. l->stats.recv_bundles++;
  1121. l->stats.recv_bundled += msg_msgcnt(msg);
  1122. res = 0;
  1123. break;
  1124. case NAME_DISTRIBUTOR:
  1125. n->bclink.recv_permitted = true;
  1126. res = 0;
  1127. break;
  1128. case BCAST_PROTOCOL:
  1129. tipc_link_sync_rcv(n, *buf);
  1130. break;
  1131. default:
  1132. res = 0;
  1133. }
  1134. return res;
  1135. }
  1136. /**
  1137. * tipc_link_input - Deliver message too higher layers
  1138. */
  1139. static int tipc_link_input(struct net *net, struct tipc_link *l,
  1140. struct sk_buff *buf)
  1141. {
  1142. struct tipc_msg *msg = buf_msg(buf);
  1143. int res = 0;
  1144. switch (msg_user(msg)) {
  1145. case TIPC_LOW_IMPORTANCE:
  1146. case TIPC_MEDIUM_IMPORTANCE:
  1147. case TIPC_HIGH_IMPORTANCE:
  1148. case TIPC_CRITICAL_IMPORTANCE:
  1149. case CONN_MANAGER:
  1150. tipc_sk_rcv(net, buf);
  1151. break;
  1152. case NAME_DISTRIBUTOR:
  1153. tipc_named_rcv(net, buf);
  1154. break;
  1155. case MSG_BUNDLER:
  1156. tipc_link_bundle_rcv(net, buf);
  1157. break;
  1158. default:
  1159. res = -EINVAL;
  1160. }
  1161. return res;
  1162. }
  1163. /**
  1164. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1165. *
  1166. * Returns increase in queue length (i.e. 0 or 1)
  1167. */
  1168. u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
  1169. {
  1170. struct sk_buff *skb1;
  1171. u32 seq_no = buf_seqno(skb);
  1172. /* Empty queue ? */
  1173. if (skb_queue_empty(list)) {
  1174. __skb_queue_tail(list, skb);
  1175. return 1;
  1176. }
  1177. /* Last ? */
  1178. if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
  1179. __skb_queue_tail(list, skb);
  1180. return 1;
  1181. }
  1182. /* Locate insertion point in queue, then insert; discard if duplicate */
  1183. skb_queue_walk(list, skb1) {
  1184. u32 curr_seqno = buf_seqno(skb1);
  1185. if (seq_no == curr_seqno) {
  1186. kfree_skb(skb);
  1187. return 0;
  1188. }
  1189. if (less(seq_no, curr_seqno))
  1190. break;
  1191. }
  1192. __skb_queue_before(list, skb1, skb);
  1193. return 1;
  1194. }
  1195. /*
  1196. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1197. */
  1198. static void link_handle_out_of_seq_msg(struct net *net,
  1199. struct tipc_link *l_ptr,
  1200. struct sk_buff *buf)
  1201. {
  1202. u32 seq_no = buf_seqno(buf);
  1203. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1204. tipc_link_proto_rcv(net, l_ptr, buf);
  1205. return;
  1206. }
  1207. /* Record OOS packet arrival (force mismatch on next timeout) */
  1208. l_ptr->checkpoint--;
  1209. /*
  1210. * Discard packet if a duplicate; otherwise add it to deferred queue
  1211. * and notify peer of gap as per protocol specification
  1212. */
  1213. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1214. l_ptr->stats.duplicates++;
  1215. kfree_skb(buf);
  1216. return;
  1217. }
  1218. if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
  1219. l_ptr->stats.deferred_recv++;
  1220. TIPC_SKB_CB(buf)->deferred = true;
  1221. if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
  1222. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1223. } else {
  1224. l_ptr->stats.duplicates++;
  1225. }
  1226. }
  1227. /*
  1228. * Send protocol message to the other endpoint.
  1229. */
  1230. void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
  1231. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1232. {
  1233. struct sk_buff *buf = NULL;
  1234. struct tipc_msg *msg = l_ptr->pmsg;
  1235. u32 msg_size = sizeof(l_ptr->proto_msg);
  1236. int r_flag;
  1237. /* Don't send protocol message during link changeover */
  1238. if (l_ptr->exp_msg_count)
  1239. return;
  1240. /* Abort non-RESET send if communication with node is prohibited */
  1241. if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
  1242. return;
  1243. /* Create protocol message with "out-of-sequence" sequence number */
  1244. msg_set_type(msg, msg_typ);
  1245. msg_set_net_plane(msg, l_ptr->net_plane);
  1246. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1247. msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
  1248. if (msg_typ == STATE_MSG) {
  1249. u32 next_sent = mod(l_ptr->next_out_no);
  1250. if (!tipc_link_is_up(l_ptr))
  1251. return;
  1252. if (l_ptr->next_out)
  1253. next_sent = buf_seqno(l_ptr->next_out);
  1254. msg_set_next_sent(msg, next_sent);
  1255. if (!skb_queue_empty(&l_ptr->deferred_queue)) {
  1256. u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
  1257. gap = mod(rec - mod(l_ptr->next_in_no));
  1258. }
  1259. msg_set_seq_gap(msg, gap);
  1260. if (gap)
  1261. l_ptr->stats.sent_nacks++;
  1262. msg_set_link_tolerance(msg, tolerance);
  1263. msg_set_linkprio(msg, priority);
  1264. msg_set_max_pkt(msg, ack_mtu);
  1265. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1266. msg_set_probe(msg, probe_msg != 0);
  1267. if (probe_msg) {
  1268. u32 mtu = l_ptr->max_pkt;
  1269. if ((mtu < l_ptr->max_pkt_target) &&
  1270. link_working_working(l_ptr) &&
  1271. l_ptr->fsm_msg_cnt) {
  1272. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1273. if (l_ptr->max_pkt_probes == 10) {
  1274. l_ptr->max_pkt_target = (msg_size - 4);
  1275. l_ptr->max_pkt_probes = 0;
  1276. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1277. }
  1278. l_ptr->max_pkt_probes++;
  1279. }
  1280. l_ptr->stats.sent_probes++;
  1281. }
  1282. l_ptr->stats.sent_states++;
  1283. } else { /* RESET_MSG or ACTIVATE_MSG */
  1284. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1285. msg_set_seq_gap(msg, 0);
  1286. msg_set_next_sent(msg, 1);
  1287. msg_set_probe(msg, 0);
  1288. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1289. msg_set_linkprio(msg, l_ptr->priority);
  1290. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1291. }
  1292. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1293. msg_set_redundant_link(msg, r_flag);
  1294. msg_set_linkprio(msg, l_ptr->priority);
  1295. msg_set_size(msg, msg_size);
  1296. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1297. buf = tipc_buf_acquire(msg_size);
  1298. if (!buf)
  1299. return;
  1300. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1301. buf->priority = TC_PRIO_CONTROL;
  1302. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
  1303. &l_ptr->media_addr);
  1304. l_ptr->unacked_window = 0;
  1305. kfree_skb(buf);
  1306. }
  1307. /*
  1308. * Receive protocol message :
  1309. * Note that network plane id propagates through the network, and may
  1310. * change at any time. The node with lowest address rules
  1311. */
  1312. static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
  1313. struct sk_buff *buf)
  1314. {
  1315. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1316. u32 rec_gap = 0;
  1317. u32 max_pkt_info;
  1318. u32 max_pkt_ack;
  1319. u32 msg_tol;
  1320. struct tipc_msg *msg = buf_msg(buf);
  1321. /* Discard protocol message during link changeover */
  1322. if (l_ptr->exp_msg_count)
  1323. goto exit;
  1324. if (l_ptr->net_plane != msg_net_plane(msg))
  1325. if (tn->own_addr > msg_prevnode(msg))
  1326. l_ptr->net_plane = msg_net_plane(msg);
  1327. switch (msg_type(msg)) {
  1328. case RESET_MSG:
  1329. if (!link_working_unknown(l_ptr) &&
  1330. (l_ptr->peer_session != INVALID_SESSION)) {
  1331. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1332. break; /* duplicate or old reset: ignore */
  1333. }
  1334. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1335. link_working_unknown(l_ptr))) {
  1336. /*
  1337. * peer has lost contact -- don't allow peer's links
  1338. * to reactivate before we recognize loss & clean up
  1339. */
  1340. l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
  1341. }
  1342. link_state_event(l_ptr, RESET_MSG);
  1343. /* fall thru' */
  1344. case ACTIVATE_MSG:
  1345. /* Update link settings according other endpoint's values */
  1346. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1347. msg_tol = msg_link_tolerance(msg);
  1348. if (msg_tol > l_ptr->tolerance)
  1349. link_set_supervision_props(l_ptr, msg_tol);
  1350. if (msg_linkprio(msg) > l_ptr->priority)
  1351. l_ptr->priority = msg_linkprio(msg);
  1352. max_pkt_info = msg_max_pkt(msg);
  1353. if (max_pkt_info) {
  1354. if (max_pkt_info < l_ptr->max_pkt_target)
  1355. l_ptr->max_pkt_target = max_pkt_info;
  1356. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1357. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1358. } else {
  1359. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1360. }
  1361. /* Synchronize broadcast link info, if not done previously */
  1362. if (!tipc_node_is_up(l_ptr->owner)) {
  1363. l_ptr->owner->bclink.last_sent =
  1364. l_ptr->owner->bclink.last_in =
  1365. msg_last_bcast(msg);
  1366. l_ptr->owner->bclink.oos_state = 0;
  1367. }
  1368. l_ptr->peer_session = msg_session(msg);
  1369. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1370. if (msg_type(msg) == ACTIVATE_MSG)
  1371. link_state_event(l_ptr, ACTIVATE_MSG);
  1372. break;
  1373. case STATE_MSG:
  1374. msg_tol = msg_link_tolerance(msg);
  1375. if (msg_tol)
  1376. link_set_supervision_props(l_ptr, msg_tol);
  1377. if (msg_linkprio(msg) &&
  1378. (msg_linkprio(msg) != l_ptr->priority)) {
  1379. pr_warn("%s<%s>, priority change %u->%u\n",
  1380. link_rst_msg, l_ptr->name, l_ptr->priority,
  1381. msg_linkprio(msg));
  1382. l_ptr->priority = msg_linkprio(msg);
  1383. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1384. break;
  1385. }
  1386. /* Record reception; force mismatch at next timeout: */
  1387. l_ptr->checkpoint--;
  1388. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1389. l_ptr->stats.recv_states++;
  1390. if (link_reset_unknown(l_ptr))
  1391. break;
  1392. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1393. rec_gap = mod(msg_next_sent(msg) -
  1394. mod(l_ptr->next_in_no));
  1395. }
  1396. max_pkt_ack = msg_max_pkt(msg);
  1397. if (max_pkt_ack > l_ptr->max_pkt) {
  1398. l_ptr->max_pkt = max_pkt_ack;
  1399. l_ptr->max_pkt_probes = 0;
  1400. }
  1401. max_pkt_ack = 0;
  1402. if (msg_probe(msg)) {
  1403. l_ptr->stats.recv_probes++;
  1404. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1405. max_pkt_ack = msg_size(msg);
  1406. }
  1407. /* Protocol message before retransmits, reduce loss risk */
  1408. if (l_ptr->owner->bclink.recv_permitted)
  1409. tipc_bclink_update_link_state(net, l_ptr->owner,
  1410. msg_last_bcast(msg));
  1411. if (rec_gap || (msg_probe(msg))) {
  1412. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
  1413. 0, max_pkt_ack);
  1414. }
  1415. if (msg_seq_gap(msg)) {
  1416. l_ptr->stats.recv_nacks++;
  1417. tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
  1418. msg_seq_gap(msg));
  1419. }
  1420. break;
  1421. }
  1422. exit:
  1423. kfree_skb(buf);
  1424. }
  1425. /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
  1426. * a different bearer. Owner node is locked.
  1427. */
  1428. static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
  1429. struct tipc_msg *tunnel_hdr,
  1430. struct tipc_msg *msg,
  1431. u32 selector)
  1432. {
  1433. struct tipc_link *tunnel;
  1434. struct sk_buff *skb;
  1435. u32 length = msg_size(msg);
  1436. tunnel = l_ptr->owner->active_links[selector & 1];
  1437. if (!tipc_link_is_up(tunnel)) {
  1438. pr_warn("%stunnel link no longer available\n", link_co_err);
  1439. return;
  1440. }
  1441. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1442. skb = tipc_buf_acquire(length + INT_H_SIZE);
  1443. if (!skb) {
  1444. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1445. return;
  1446. }
  1447. skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
  1448. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
  1449. __tipc_link_xmit_skb(tunnel, skb);
  1450. }
  1451. /* tipc_link_failover_send_queue(): A link has gone down, but a second
  1452. * link is still active. We can do failover. Tunnel the failing link's
  1453. * whole send queue via the remaining link. This way, we don't lose
  1454. * any packets, and sequence order is preserved for subsequent traffic
  1455. * sent over the remaining link. Owner node is locked.
  1456. */
  1457. void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
  1458. {
  1459. u32 msgcount = skb_queue_len(&l_ptr->outqueue);
  1460. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1461. struct tipc_msg tunnel_hdr;
  1462. struct sk_buff *skb;
  1463. int split_bundles;
  1464. if (!tunnel)
  1465. return;
  1466. tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
  1467. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1468. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1469. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1470. if (skb_queue_empty(&l_ptr->outqueue)) {
  1471. skb = tipc_buf_acquire(INT_H_SIZE);
  1472. if (skb) {
  1473. skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
  1474. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1475. __tipc_link_xmit_skb(tunnel, skb);
  1476. } else {
  1477. pr_warn("%sunable to send changeover msg\n",
  1478. link_co_err);
  1479. }
  1480. return;
  1481. }
  1482. split_bundles = (l_ptr->owner->active_links[0] !=
  1483. l_ptr->owner->active_links[1]);
  1484. skb_queue_walk(&l_ptr->outqueue, skb) {
  1485. struct tipc_msg *msg = buf_msg(skb);
  1486. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1487. struct tipc_msg *m = msg_get_wrapped(msg);
  1488. unchar *pos = (unchar *)m;
  1489. msgcount = msg_msgcnt(msg);
  1490. while (msgcount--) {
  1491. msg_set_seqno(m, msg_seqno(msg));
  1492. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
  1493. msg_link_selector(m));
  1494. pos += align(msg_size(m));
  1495. m = (struct tipc_msg *)pos;
  1496. }
  1497. } else {
  1498. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
  1499. msg_link_selector(msg));
  1500. }
  1501. }
  1502. }
  1503. /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
  1504. * duplicate of the first link's send queue via the new link. This way, we
  1505. * are guaranteed that currently queued packets from a socket are delivered
  1506. * before future traffic from the same socket, even if this is using the
  1507. * new link. The last arriving copy of each duplicate packet is dropped at
  1508. * the receiving end by the regular protocol check, so packet cardinality
  1509. * and sequence order is preserved per sender/receiver socket pair.
  1510. * Owner node is locked.
  1511. */
  1512. void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
  1513. struct tipc_link *tunnel)
  1514. {
  1515. struct sk_buff *skb;
  1516. struct tipc_msg tunnel_hdr;
  1517. tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
  1518. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1519. msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
  1520. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1521. skb_queue_walk(&l_ptr->outqueue, skb) {
  1522. struct sk_buff *outskb;
  1523. struct tipc_msg *msg = buf_msg(skb);
  1524. u32 length = msg_size(msg);
  1525. if (msg_user(msg) == MSG_BUNDLER)
  1526. msg_set_type(msg, CLOSED_MSG);
  1527. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1528. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1529. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1530. outskb = tipc_buf_acquire(length + INT_H_SIZE);
  1531. if (outskb == NULL) {
  1532. pr_warn("%sunable to send duplicate msg\n",
  1533. link_co_err);
  1534. return;
  1535. }
  1536. skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
  1537. skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
  1538. length);
  1539. __tipc_link_xmit_skb(tunnel, outskb);
  1540. if (!tipc_link_is_up(l_ptr))
  1541. return;
  1542. }
  1543. }
  1544. /**
  1545. * buf_extract - extracts embedded TIPC message from another message
  1546. * @skb: encapsulating message buffer
  1547. * @from_pos: offset to extract from
  1548. *
  1549. * Returns a new message buffer containing an embedded message. The
  1550. * encapsulating message itself is left unchanged.
  1551. */
  1552. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1553. {
  1554. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1555. u32 size = msg_size(msg);
  1556. struct sk_buff *eb;
  1557. eb = tipc_buf_acquire(size);
  1558. if (eb)
  1559. skb_copy_to_linear_data(eb, msg, size);
  1560. return eb;
  1561. }
  1562. /* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
  1563. * Owner node is locked.
  1564. */
  1565. static void tipc_link_dup_rcv(struct net *net, struct tipc_link *l_ptr,
  1566. struct sk_buff *t_buf)
  1567. {
  1568. struct sk_buff *buf;
  1569. if (!tipc_link_is_up(l_ptr))
  1570. return;
  1571. buf = buf_extract(t_buf, INT_H_SIZE);
  1572. if (buf == NULL) {
  1573. pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
  1574. return;
  1575. }
  1576. /* Add buffer to deferred queue, if applicable: */
  1577. link_handle_out_of_seq_msg(net, l_ptr, buf);
  1578. }
  1579. /* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
  1580. * Owner node is locked.
  1581. */
  1582. static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
  1583. struct sk_buff *t_buf)
  1584. {
  1585. struct tipc_msg *t_msg = buf_msg(t_buf);
  1586. struct sk_buff *buf = NULL;
  1587. struct tipc_msg *msg;
  1588. if (tipc_link_is_up(l_ptr))
  1589. tipc_link_reset(l_ptr);
  1590. /* First failover packet? */
  1591. if (l_ptr->exp_msg_count == START_CHANGEOVER)
  1592. l_ptr->exp_msg_count = msg_msgcnt(t_msg);
  1593. /* Should there be an inner packet? */
  1594. if (l_ptr->exp_msg_count) {
  1595. l_ptr->exp_msg_count--;
  1596. buf = buf_extract(t_buf, INT_H_SIZE);
  1597. if (buf == NULL) {
  1598. pr_warn("%sno inner failover pkt\n", link_co_err);
  1599. goto exit;
  1600. }
  1601. msg = buf_msg(buf);
  1602. if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
  1603. kfree_skb(buf);
  1604. buf = NULL;
  1605. goto exit;
  1606. }
  1607. if (msg_user(msg) == MSG_FRAGMENTER) {
  1608. l_ptr->stats.recv_fragments++;
  1609. tipc_buf_append(&l_ptr->reasm_buf, &buf);
  1610. }
  1611. }
  1612. exit:
  1613. if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
  1614. tipc_node_detach_link(l_ptr->owner, l_ptr);
  1615. kfree(l_ptr);
  1616. }
  1617. return buf;
  1618. }
  1619. /* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
  1620. * via other link as result of a failover (ORIGINAL_MSG) or
  1621. * a new active link (DUPLICATE_MSG). Failover packets are
  1622. * returned to the active link for delivery upwards.
  1623. * Owner node is locked.
  1624. */
  1625. static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
  1626. struct sk_buff **buf)
  1627. {
  1628. struct sk_buff *t_buf = *buf;
  1629. struct tipc_link *l_ptr;
  1630. struct tipc_msg *t_msg = buf_msg(t_buf);
  1631. u32 bearer_id = msg_bearer_id(t_msg);
  1632. *buf = NULL;
  1633. if (bearer_id >= MAX_BEARERS)
  1634. goto exit;
  1635. l_ptr = n_ptr->links[bearer_id];
  1636. if (!l_ptr)
  1637. goto exit;
  1638. if (msg_type(t_msg) == DUPLICATE_MSG)
  1639. tipc_link_dup_rcv(net, l_ptr, t_buf);
  1640. else if (msg_type(t_msg) == ORIGINAL_MSG)
  1641. *buf = tipc_link_failover_rcv(l_ptr, t_buf);
  1642. else
  1643. pr_warn("%sunknown tunnel pkt received\n", link_co_err);
  1644. exit:
  1645. kfree_skb(t_buf);
  1646. return *buf != NULL;
  1647. }
  1648. /*
  1649. * Bundler functionality:
  1650. */
  1651. void tipc_link_bundle_rcv(struct net *net, struct sk_buff *buf)
  1652. {
  1653. u32 msgcount = msg_msgcnt(buf_msg(buf));
  1654. u32 pos = INT_H_SIZE;
  1655. struct sk_buff *obuf;
  1656. struct tipc_msg *omsg;
  1657. while (msgcount--) {
  1658. obuf = buf_extract(buf, pos);
  1659. if (obuf == NULL) {
  1660. pr_warn("Link unable to unbundle message(s)\n");
  1661. break;
  1662. }
  1663. omsg = buf_msg(obuf);
  1664. pos += align(msg_size(omsg));
  1665. if (msg_isdata(omsg)) {
  1666. if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
  1667. tipc_sk_mcast_rcv(net, obuf);
  1668. else
  1669. tipc_sk_rcv(net, obuf);
  1670. } else if (msg_user(omsg) == CONN_MANAGER) {
  1671. tipc_sk_rcv(net, obuf);
  1672. } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
  1673. tipc_named_rcv(net, obuf);
  1674. } else {
  1675. pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
  1676. kfree_skb(obuf);
  1677. }
  1678. }
  1679. kfree_skb(buf);
  1680. }
  1681. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
  1682. {
  1683. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  1684. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1685. return;
  1686. l_ptr->tolerance = tol;
  1687. l_ptr->cont_intv = msecs_to_jiffies(intv);
  1688. l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
  1689. }
  1690. void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
  1691. {
  1692. /* Data messages from this node, inclusive FIRST_FRAGM */
  1693. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  1694. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  1695. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  1696. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  1697. /* Transiting data messages,inclusive FIRST_FRAGM */
  1698. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  1699. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  1700. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  1701. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  1702. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  1703. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  1704. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  1705. /* FRAGMENT and LAST_FRAGMENT packets */
  1706. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  1707. }
  1708. /* tipc_link_find_owner - locate owner node of link by link's name
  1709. * @net: the applicable net namespace
  1710. * @name: pointer to link name string
  1711. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1712. *
  1713. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1714. */
  1715. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1716. const char *link_name,
  1717. unsigned int *bearer_id)
  1718. {
  1719. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1720. struct tipc_link *l_ptr;
  1721. struct tipc_node *n_ptr;
  1722. struct tipc_node *found_node = NULL;
  1723. int i;
  1724. *bearer_id = 0;
  1725. rcu_read_lock();
  1726. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1727. tipc_node_lock(n_ptr);
  1728. for (i = 0; i < MAX_BEARERS; i++) {
  1729. l_ptr = n_ptr->links[i];
  1730. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1731. *bearer_id = i;
  1732. found_node = n_ptr;
  1733. break;
  1734. }
  1735. }
  1736. tipc_node_unlock(n_ptr);
  1737. if (found_node)
  1738. break;
  1739. }
  1740. rcu_read_unlock();
  1741. return found_node;
  1742. }
  1743. /**
  1744. * link_value_is_valid -- validate proposed link tolerance/priority/window
  1745. *
  1746. * @cmd: value type (TIPC_CMD_SET_LINK_*)
  1747. * @new_value: the new value
  1748. *
  1749. * Returns 1 if value is within range, 0 if not.
  1750. */
  1751. static int link_value_is_valid(u16 cmd, u32 new_value)
  1752. {
  1753. switch (cmd) {
  1754. case TIPC_CMD_SET_LINK_TOL:
  1755. return (new_value >= TIPC_MIN_LINK_TOL) &&
  1756. (new_value <= TIPC_MAX_LINK_TOL);
  1757. case TIPC_CMD_SET_LINK_PRI:
  1758. return (new_value <= TIPC_MAX_LINK_PRI);
  1759. case TIPC_CMD_SET_LINK_WINDOW:
  1760. return (new_value >= TIPC_MIN_LINK_WIN) &&
  1761. (new_value <= TIPC_MAX_LINK_WIN);
  1762. }
  1763. return 0;
  1764. }
  1765. /**
  1766. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  1767. * @net: the applicable net namespace
  1768. * @name: ptr to link, bearer, or media name
  1769. * @new_value: new value of link, bearer, or media setting
  1770. * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  1771. *
  1772. * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
  1773. *
  1774. * Returns 0 if value updated and negative value on error.
  1775. */
  1776. static int link_cmd_set_value(struct net *net, const char *name, u32 new_value,
  1777. u16 cmd)
  1778. {
  1779. struct tipc_node *node;
  1780. struct tipc_link *l_ptr;
  1781. struct tipc_bearer *b_ptr;
  1782. struct tipc_media *m_ptr;
  1783. int bearer_id;
  1784. int res = 0;
  1785. node = tipc_link_find_owner(net, name, &bearer_id);
  1786. if (node) {
  1787. tipc_node_lock(node);
  1788. l_ptr = node->links[bearer_id];
  1789. if (l_ptr) {
  1790. switch (cmd) {
  1791. case TIPC_CMD_SET_LINK_TOL:
  1792. link_set_supervision_props(l_ptr, new_value);
  1793. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
  1794. new_value, 0, 0);
  1795. break;
  1796. case TIPC_CMD_SET_LINK_PRI:
  1797. l_ptr->priority = new_value;
  1798. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
  1799. 0, new_value, 0);
  1800. break;
  1801. case TIPC_CMD_SET_LINK_WINDOW:
  1802. tipc_link_set_queue_limits(l_ptr, new_value);
  1803. break;
  1804. default:
  1805. res = -EINVAL;
  1806. break;
  1807. }
  1808. }
  1809. tipc_node_unlock(node);
  1810. return res;
  1811. }
  1812. b_ptr = tipc_bearer_find(net, name);
  1813. if (b_ptr) {
  1814. switch (cmd) {
  1815. case TIPC_CMD_SET_LINK_TOL:
  1816. b_ptr->tolerance = new_value;
  1817. break;
  1818. case TIPC_CMD_SET_LINK_PRI:
  1819. b_ptr->priority = new_value;
  1820. break;
  1821. case TIPC_CMD_SET_LINK_WINDOW:
  1822. b_ptr->window = new_value;
  1823. break;
  1824. default:
  1825. res = -EINVAL;
  1826. break;
  1827. }
  1828. return res;
  1829. }
  1830. m_ptr = tipc_media_find(name);
  1831. if (!m_ptr)
  1832. return -ENODEV;
  1833. switch (cmd) {
  1834. case TIPC_CMD_SET_LINK_TOL:
  1835. m_ptr->tolerance = new_value;
  1836. break;
  1837. case TIPC_CMD_SET_LINK_PRI:
  1838. m_ptr->priority = new_value;
  1839. break;
  1840. case TIPC_CMD_SET_LINK_WINDOW:
  1841. m_ptr->window = new_value;
  1842. break;
  1843. default:
  1844. res = -EINVAL;
  1845. break;
  1846. }
  1847. return res;
  1848. }
  1849. struct sk_buff *tipc_link_cmd_config(struct net *net, const void *req_tlv_area,
  1850. int req_tlv_space, u16 cmd)
  1851. {
  1852. struct tipc_link_config *args;
  1853. u32 new_value;
  1854. int res;
  1855. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  1856. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  1857. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  1858. new_value = ntohl(args->value);
  1859. if (!link_value_is_valid(cmd, new_value))
  1860. return tipc_cfg_reply_error_string(
  1861. "cannot change, value invalid");
  1862. if (!strcmp(args->name, tipc_bclink_name)) {
  1863. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  1864. (tipc_bclink_set_queue_limits(net, new_value) == 0))
  1865. return tipc_cfg_reply_none();
  1866. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  1867. " (cannot change setting on broadcast link)");
  1868. }
  1869. res = link_cmd_set_value(net, args->name, new_value, cmd);
  1870. if (res)
  1871. return tipc_cfg_reply_error_string("cannot change link setting");
  1872. return tipc_cfg_reply_none();
  1873. }
  1874. /**
  1875. * link_reset_statistics - reset link statistics
  1876. * @l_ptr: pointer to link
  1877. */
  1878. static void link_reset_statistics(struct tipc_link *l_ptr)
  1879. {
  1880. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1881. l_ptr->stats.sent_info = l_ptr->next_out_no;
  1882. l_ptr->stats.recv_info = l_ptr->next_in_no;
  1883. }
  1884. struct sk_buff *tipc_link_cmd_reset_stats(struct net *net,
  1885. const void *req_tlv_area,
  1886. int req_tlv_space)
  1887. {
  1888. char *link_name;
  1889. struct tipc_link *l_ptr;
  1890. struct tipc_node *node;
  1891. unsigned int bearer_id;
  1892. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  1893. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  1894. link_name = (char *)TLV_DATA(req_tlv_area);
  1895. if (!strcmp(link_name, tipc_bclink_name)) {
  1896. if (tipc_bclink_reset_stats(net))
  1897. return tipc_cfg_reply_error_string("link not found");
  1898. return tipc_cfg_reply_none();
  1899. }
  1900. node = tipc_link_find_owner(net, link_name, &bearer_id);
  1901. if (!node)
  1902. return tipc_cfg_reply_error_string("link not found");
  1903. tipc_node_lock(node);
  1904. l_ptr = node->links[bearer_id];
  1905. if (!l_ptr) {
  1906. tipc_node_unlock(node);
  1907. return tipc_cfg_reply_error_string("link not found");
  1908. }
  1909. link_reset_statistics(l_ptr);
  1910. tipc_node_unlock(node);
  1911. return tipc_cfg_reply_none();
  1912. }
  1913. /**
  1914. * percent - convert count to a percentage of total (rounding up or down)
  1915. */
  1916. static u32 percent(u32 count, u32 total)
  1917. {
  1918. return (count * 100 + (total / 2)) / total;
  1919. }
  1920. /**
  1921. * tipc_link_stats - print link statistics
  1922. * @net: the applicable net namespace
  1923. * @name: link name
  1924. * @buf: print buffer area
  1925. * @buf_size: size of print buffer area
  1926. *
  1927. * Returns length of print buffer data string (or 0 if error)
  1928. */
  1929. static int tipc_link_stats(struct net *net, const char *name, char *buf,
  1930. const u32 buf_size)
  1931. {
  1932. struct tipc_link *l;
  1933. struct tipc_stats *s;
  1934. struct tipc_node *node;
  1935. char *status;
  1936. u32 profile_total = 0;
  1937. unsigned int bearer_id;
  1938. int ret;
  1939. if (!strcmp(name, tipc_bclink_name))
  1940. return tipc_bclink_stats(net, buf, buf_size);
  1941. node = tipc_link_find_owner(net, name, &bearer_id);
  1942. if (!node)
  1943. return 0;
  1944. tipc_node_lock(node);
  1945. l = node->links[bearer_id];
  1946. if (!l) {
  1947. tipc_node_unlock(node);
  1948. return 0;
  1949. }
  1950. s = &l->stats;
  1951. if (tipc_link_is_active(l))
  1952. status = "ACTIVE";
  1953. else if (tipc_link_is_up(l))
  1954. status = "STANDBY";
  1955. else
  1956. status = "DEFUNCT";
  1957. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  1958. " %s MTU:%u Priority:%u Tolerance:%u ms"
  1959. " Window:%u packets\n",
  1960. l->name, status, l->max_pkt, l->priority,
  1961. l->tolerance, l->queue_limit[0]);
  1962. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1963. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  1964. l->next_in_no - s->recv_info, s->recv_fragments,
  1965. s->recv_fragmented, s->recv_bundles,
  1966. s->recv_bundled);
  1967. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1968. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  1969. l->next_out_no - s->sent_info, s->sent_fragments,
  1970. s->sent_fragmented, s->sent_bundles,
  1971. s->sent_bundled);
  1972. profile_total = s->msg_length_counts;
  1973. if (!profile_total)
  1974. profile_total = 1;
  1975. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1976. " TX profile sample:%u packets average:%u octets\n"
  1977. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  1978. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  1979. s->msg_length_counts,
  1980. s->msg_lengths_total / profile_total,
  1981. percent(s->msg_length_profile[0], profile_total),
  1982. percent(s->msg_length_profile[1], profile_total),
  1983. percent(s->msg_length_profile[2], profile_total),
  1984. percent(s->msg_length_profile[3], profile_total),
  1985. percent(s->msg_length_profile[4], profile_total),
  1986. percent(s->msg_length_profile[5], profile_total),
  1987. percent(s->msg_length_profile[6], profile_total));
  1988. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1989. " RX states:%u probes:%u naks:%u defs:%u"
  1990. " dups:%u\n", s->recv_states, s->recv_probes,
  1991. s->recv_nacks, s->deferred_recv, s->duplicates);
  1992. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1993. " TX states:%u probes:%u naks:%u acks:%u"
  1994. " dups:%u\n", s->sent_states, s->sent_probes,
  1995. s->sent_nacks, s->sent_acks, s->retransmitted);
  1996. ret += tipc_snprintf(buf + ret, buf_size - ret,
  1997. " Congestion link:%u Send queue"
  1998. " max:%u avg:%u\n", s->link_congs,
  1999. s->max_queue_sz, s->queue_sz_counts ?
  2000. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  2001. tipc_node_unlock(node);
  2002. return ret;
  2003. }
  2004. struct sk_buff *tipc_link_cmd_show_stats(struct net *net,
  2005. const void *req_tlv_area,
  2006. int req_tlv_space)
  2007. {
  2008. struct sk_buff *buf;
  2009. struct tlv_desc *rep_tlv;
  2010. int str_len;
  2011. int pb_len;
  2012. char *pb;
  2013. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2014. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2015. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  2016. if (!buf)
  2017. return NULL;
  2018. rep_tlv = (struct tlv_desc *)buf->data;
  2019. pb = TLV_DATA(rep_tlv);
  2020. pb_len = ULTRA_STRING_MAX_LEN;
  2021. str_len = tipc_link_stats(net, (char *)TLV_DATA(req_tlv_area),
  2022. pb, pb_len);
  2023. if (!str_len) {
  2024. kfree_skb(buf);
  2025. return tipc_cfg_reply_error_string("link not found");
  2026. }
  2027. str_len += 1; /* for "\0" */
  2028. skb_put(buf, TLV_SPACE(str_len));
  2029. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2030. return buf;
  2031. }
  2032. static void link_print(struct tipc_link *l_ptr, const char *str)
  2033. {
  2034. struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
  2035. struct tipc_bearer *b_ptr;
  2036. rcu_read_lock();
  2037. b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
  2038. if (b_ptr)
  2039. pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
  2040. rcu_read_unlock();
  2041. if (link_working_unknown(l_ptr))
  2042. pr_cont(":WU\n");
  2043. else if (link_reset_reset(l_ptr))
  2044. pr_cont(":RR\n");
  2045. else if (link_reset_unknown(l_ptr))
  2046. pr_cont(":RU\n");
  2047. else if (link_working_working(l_ptr))
  2048. pr_cont(":WW\n");
  2049. else
  2050. pr_cont("\n");
  2051. }
  2052. /* Parse and validate nested (link) properties valid for media, bearer and link
  2053. */
  2054. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  2055. {
  2056. int err;
  2057. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  2058. tipc_nl_prop_policy);
  2059. if (err)
  2060. return err;
  2061. if (props[TIPC_NLA_PROP_PRIO]) {
  2062. u32 prio;
  2063. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2064. if (prio > TIPC_MAX_LINK_PRI)
  2065. return -EINVAL;
  2066. }
  2067. if (props[TIPC_NLA_PROP_TOL]) {
  2068. u32 tol;
  2069. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2070. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  2071. return -EINVAL;
  2072. }
  2073. if (props[TIPC_NLA_PROP_WIN]) {
  2074. u32 win;
  2075. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2076. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  2077. return -EINVAL;
  2078. }
  2079. return 0;
  2080. }
  2081. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  2082. {
  2083. int err;
  2084. int res = 0;
  2085. int bearer_id;
  2086. char *name;
  2087. struct tipc_link *link;
  2088. struct tipc_node *node;
  2089. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2090. struct net *net = genl_info_net(info);
  2091. if (!info->attrs[TIPC_NLA_LINK])
  2092. return -EINVAL;
  2093. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  2094. info->attrs[TIPC_NLA_LINK],
  2095. tipc_nl_link_policy);
  2096. if (err)
  2097. return err;
  2098. if (!attrs[TIPC_NLA_LINK_NAME])
  2099. return -EINVAL;
  2100. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2101. node = tipc_link_find_owner(net, name, &bearer_id);
  2102. if (!node)
  2103. return -EINVAL;
  2104. tipc_node_lock(node);
  2105. link = node->links[bearer_id];
  2106. if (!link) {
  2107. res = -EINVAL;
  2108. goto out;
  2109. }
  2110. if (attrs[TIPC_NLA_LINK_PROP]) {
  2111. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  2112. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  2113. props);
  2114. if (err) {
  2115. res = err;
  2116. goto out;
  2117. }
  2118. if (props[TIPC_NLA_PROP_TOL]) {
  2119. u32 tol;
  2120. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2121. link_set_supervision_props(link, tol);
  2122. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
  2123. }
  2124. if (props[TIPC_NLA_PROP_PRIO]) {
  2125. u32 prio;
  2126. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2127. link->priority = prio;
  2128. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
  2129. }
  2130. if (props[TIPC_NLA_PROP_WIN]) {
  2131. u32 win;
  2132. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2133. tipc_link_set_queue_limits(link, win);
  2134. }
  2135. }
  2136. out:
  2137. tipc_node_unlock(node);
  2138. return res;
  2139. }
  2140. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  2141. {
  2142. int i;
  2143. struct nlattr *stats;
  2144. struct nla_map {
  2145. u32 key;
  2146. u32 val;
  2147. };
  2148. struct nla_map map[] = {
  2149. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  2150. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  2151. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  2152. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  2153. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  2154. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  2155. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  2156. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  2157. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  2158. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  2159. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  2160. s->msg_length_counts : 1},
  2161. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  2162. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  2163. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  2164. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  2165. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  2166. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  2167. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  2168. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  2169. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  2170. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  2171. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  2172. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  2173. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  2174. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  2175. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  2176. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  2177. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  2178. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  2179. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  2180. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  2181. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  2182. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  2183. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  2184. };
  2185. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  2186. if (!stats)
  2187. return -EMSGSIZE;
  2188. for (i = 0; i < ARRAY_SIZE(map); i++)
  2189. if (nla_put_u32(skb, map[i].key, map[i].val))
  2190. goto msg_full;
  2191. nla_nest_end(skb, stats);
  2192. return 0;
  2193. msg_full:
  2194. nla_nest_cancel(skb, stats);
  2195. return -EMSGSIZE;
  2196. }
  2197. /* Caller should hold appropriate locks to protect the link */
  2198. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  2199. struct tipc_link *link)
  2200. {
  2201. int err;
  2202. void *hdr;
  2203. struct nlattr *attrs;
  2204. struct nlattr *prop;
  2205. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2206. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
  2207. NLM_F_MULTI, TIPC_NL_LINK_GET);
  2208. if (!hdr)
  2209. return -EMSGSIZE;
  2210. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  2211. if (!attrs)
  2212. goto msg_full;
  2213. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  2214. goto attr_msg_full;
  2215. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  2216. tipc_cluster_mask(tn->own_addr)))
  2217. goto attr_msg_full;
  2218. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
  2219. goto attr_msg_full;
  2220. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
  2221. goto attr_msg_full;
  2222. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
  2223. goto attr_msg_full;
  2224. if (tipc_link_is_up(link))
  2225. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2226. goto attr_msg_full;
  2227. if (tipc_link_is_active(link))
  2228. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  2229. goto attr_msg_full;
  2230. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  2231. if (!prop)
  2232. goto attr_msg_full;
  2233. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2234. goto prop_msg_full;
  2235. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  2236. goto prop_msg_full;
  2237. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  2238. link->queue_limit[TIPC_LOW_IMPORTANCE]))
  2239. goto prop_msg_full;
  2240. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2241. goto prop_msg_full;
  2242. nla_nest_end(msg->skb, prop);
  2243. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  2244. if (err)
  2245. goto attr_msg_full;
  2246. nla_nest_end(msg->skb, attrs);
  2247. genlmsg_end(msg->skb, hdr);
  2248. return 0;
  2249. prop_msg_full:
  2250. nla_nest_cancel(msg->skb, prop);
  2251. attr_msg_full:
  2252. nla_nest_cancel(msg->skb, attrs);
  2253. msg_full:
  2254. genlmsg_cancel(msg->skb, hdr);
  2255. return -EMSGSIZE;
  2256. }
  2257. /* Caller should hold node lock */
  2258. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  2259. struct tipc_node *node, u32 *prev_link)
  2260. {
  2261. u32 i;
  2262. int err;
  2263. for (i = *prev_link; i < MAX_BEARERS; i++) {
  2264. *prev_link = i;
  2265. if (!node->links[i])
  2266. continue;
  2267. err = __tipc_nl_add_link(net, msg, node->links[i]);
  2268. if (err)
  2269. return err;
  2270. }
  2271. *prev_link = 0;
  2272. return 0;
  2273. }
  2274. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2275. {
  2276. struct net *net = sock_net(skb->sk);
  2277. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2278. struct tipc_node *node;
  2279. struct tipc_nl_msg msg;
  2280. u32 prev_node = cb->args[0];
  2281. u32 prev_link = cb->args[1];
  2282. int done = cb->args[2];
  2283. int err;
  2284. if (done)
  2285. return 0;
  2286. msg.skb = skb;
  2287. msg.portid = NETLINK_CB(cb->skb).portid;
  2288. msg.seq = cb->nlh->nlmsg_seq;
  2289. rcu_read_lock();
  2290. if (prev_node) {
  2291. node = tipc_node_find(net, prev_node);
  2292. if (!node) {
  2293. /* We never set seq or call nl_dump_check_consistent()
  2294. * this means that setting prev_seq here will cause the
  2295. * consistence check to fail in the netlink callback
  2296. * handler. Resulting in the last NLMSG_DONE message
  2297. * having the NLM_F_DUMP_INTR flag set.
  2298. */
  2299. cb->prev_seq = 1;
  2300. goto out;
  2301. }
  2302. list_for_each_entry_continue_rcu(node, &tn->node_list,
  2303. list) {
  2304. tipc_node_lock(node);
  2305. err = __tipc_nl_add_node_links(net, &msg, node,
  2306. &prev_link);
  2307. tipc_node_unlock(node);
  2308. if (err)
  2309. goto out;
  2310. prev_node = node->addr;
  2311. }
  2312. } else {
  2313. err = tipc_nl_add_bc_link(net, &msg);
  2314. if (err)
  2315. goto out;
  2316. list_for_each_entry_rcu(node, &tn->node_list, list) {
  2317. tipc_node_lock(node);
  2318. err = __tipc_nl_add_node_links(net, &msg, node,
  2319. &prev_link);
  2320. tipc_node_unlock(node);
  2321. if (err)
  2322. goto out;
  2323. prev_node = node->addr;
  2324. }
  2325. }
  2326. done = 1;
  2327. out:
  2328. rcu_read_unlock();
  2329. cb->args[0] = prev_node;
  2330. cb->args[1] = prev_link;
  2331. cb->args[2] = done;
  2332. return skb->len;
  2333. }
  2334. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  2335. {
  2336. struct net *net = genl_info_net(info);
  2337. struct sk_buff *ans_skb;
  2338. struct tipc_nl_msg msg;
  2339. struct tipc_link *link;
  2340. struct tipc_node *node;
  2341. char *name;
  2342. int bearer_id;
  2343. int err;
  2344. if (!info->attrs[TIPC_NLA_LINK_NAME])
  2345. return -EINVAL;
  2346. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  2347. node = tipc_link_find_owner(net, name, &bearer_id);
  2348. if (!node)
  2349. return -EINVAL;
  2350. ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  2351. if (!ans_skb)
  2352. return -ENOMEM;
  2353. msg.skb = ans_skb;
  2354. msg.portid = info->snd_portid;
  2355. msg.seq = info->snd_seq;
  2356. tipc_node_lock(node);
  2357. link = node->links[bearer_id];
  2358. if (!link) {
  2359. err = -EINVAL;
  2360. goto err_out;
  2361. }
  2362. err = __tipc_nl_add_link(net, &msg, link);
  2363. if (err)
  2364. goto err_out;
  2365. tipc_node_unlock(node);
  2366. return genlmsg_reply(ans_skb, info);
  2367. err_out:
  2368. tipc_node_unlock(node);
  2369. nlmsg_free(ans_skb);
  2370. return err;
  2371. }
  2372. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  2373. {
  2374. int err;
  2375. char *link_name;
  2376. unsigned int bearer_id;
  2377. struct tipc_link *link;
  2378. struct tipc_node *node;
  2379. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2380. struct net *net = genl_info_net(info);
  2381. if (!info->attrs[TIPC_NLA_LINK])
  2382. return -EINVAL;
  2383. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  2384. info->attrs[TIPC_NLA_LINK],
  2385. tipc_nl_link_policy);
  2386. if (err)
  2387. return err;
  2388. if (!attrs[TIPC_NLA_LINK_NAME])
  2389. return -EINVAL;
  2390. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2391. if (strcmp(link_name, tipc_bclink_name) == 0) {
  2392. err = tipc_bclink_reset_stats(net);
  2393. if (err)
  2394. return err;
  2395. return 0;
  2396. }
  2397. node = tipc_link_find_owner(net, link_name, &bearer_id);
  2398. if (!node)
  2399. return -EINVAL;
  2400. tipc_node_lock(node);
  2401. link = node->links[bearer_id];
  2402. if (!link) {
  2403. tipc_node_unlock(node);
  2404. return -EINVAL;
  2405. }
  2406. link_reset_statistics(link);
  2407. tipc_node_unlock(node);
  2408. return 0;
  2409. }