link.c 72 KB

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