link.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748
  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 tolerance);
  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(struct tipc_link *l_ptr)
  154. {
  155. struct sk_buff *skb;
  156. tipc_node_lock(l_ptr->owner);
  157. /* update counters used in statistical profiling of send traffic */
  158. l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
  159. l_ptr->stats.queue_sz_counts++;
  160. skb = skb_peek(&l_ptr->outqueue);
  161. if (skb) {
  162. struct tipc_msg *msg = buf_msg(skb);
  163. u32 length = msg_size(msg);
  164. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  165. (msg_type(msg) == FIRST_FRAGMENT)) {
  166. length = msg_size(msg_get_wrapped(msg));
  167. }
  168. if (length) {
  169. l_ptr->stats.msg_lengths_total += length;
  170. l_ptr->stats.msg_length_counts++;
  171. if (length <= 64)
  172. l_ptr->stats.msg_length_profile[0]++;
  173. else if (length <= 256)
  174. l_ptr->stats.msg_length_profile[1]++;
  175. else if (length <= 1024)
  176. l_ptr->stats.msg_length_profile[2]++;
  177. else if (length <= 4096)
  178. l_ptr->stats.msg_length_profile[3]++;
  179. else if (length <= 16384)
  180. l_ptr->stats.msg_length_profile[4]++;
  181. else if (length <= 32768)
  182. l_ptr->stats.msg_length_profile[5]++;
  183. else
  184. l_ptr->stats.msg_length_profile[6]++;
  185. }
  186. }
  187. /* do all other link processing performed on a periodic basis */
  188. link_state_event(l_ptr, TIMEOUT_EVT);
  189. if (l_ptr->next_out)
  190. tipc_link_push_packets(l_ptr);
  191. tipc_node_unlock(l_ptr->owner);
  192. }
  193. static void link_set_timer(struct tipc_link *l_ptr, u32 time)
  194. {
  195. k_start_timer(&l_ptr->timer, time);
  196. }
  197. /**
  198. * tipc_link_create - create a new link
  199. * @n_ptr: pointer to associated node
  200. * @b_ptr: pointer to associated bearer
  201. * @media_addr: media address to use when sending messages over link
  202. *
  203. * Returns pointer to link.
  204. */
  205. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  206. struct tipc_bearer *b_ptr,
  207. const struct tipc_media_addr *media_addr)
  208. {
  209. struct tipc_link *l_ptr;
  210. struct tipc_msg *msg;
  211. char *if_name;
  212. char addr_string[16];
  213. u32 peer = n_ptr->addr;
  214. if (n_ptr->link_cnt >= MAX_BEARERS) {
  215. tipc_addr_string_fill(addr_string, n_ptr->addr);
  216. pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
  217. n_ptr->link_cnt, addr_string, MAX_BEARERS);
  218. return NULL;
  219. }
  220. if (n_ptr->links[b_ptr->identity]) {
  221. tipc_addr_string_fill(addr_string, n_ptr->addr);
  222. pr_err("Attempt to establish second link on <%s> to %s\n",
  223. b_ptr->name, addr_string);
  224. return NULL;
  225. }
  226. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  227. if (!l_ptr) {
  228. pr_warn("Link creation failed, no memory\n");
  229. return NULL;
  230. }
  231. l_ptr->addr = peer;
  232. if_name = strchr(b_ptr->name, ':') + 1;
  233. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  234. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  235. tipc_node(tipc_own_addr),
  236. if_name,
  237. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  238. /* note: peer i/f name is updated by reset/activate message */
  239. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  240. l_ptr->owner = n_ptr;
  241. l_ptr->checkpoint = 1;
  242. l_ptr->peer_session = INVALID_SESSION;
  243. l_ptr->bearer_id = b_ptr->identity;
  244. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  245. l_ptr->state = RESET_UNKNOWN;
  246. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  247. msg = l_ptr->pmsg;
  248. tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
  249. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  250. msg_set_session(msg, (tipc_random & 0xffff));
  251. msg_set_bearer_id(msg, b_ptr->identity);
  252. strcpy((char *)msg_data(msg), if_name);
  253. l_ptr->priority = b_ptr->priority;
  254. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  255. l_ptr->net_plane = b_ptr->net_plane;
  256. link_init_max_pkt(l_ptr);
  257. l_ptr->next_out_no = 1;
  258. __skb_queue_head_init(&l_ptr->outqueue);
  259. __skb_queue_head_init(&l_ptr->deferred_queue);
  260. skb_queue_head_init(&l_ptr->waiting_sks);
  261. link_reset_statistics(l_ptr);
  262. tipc_node_attach_link(n_ptr, l_ptr);
  263. k_init_timer(&l_ptr->timer, (Handler)link_timeout,
  264. (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. u32 cont_intv = l_ptr->continuity_interval;
  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 tolerance)
  1650. {
  1651. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  1652. return;
  1653. l_ptr->tolerance = tolerance;
  1654. l_ptr->continuity_interval =
  1655. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  1656. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 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. /**
  1990. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  1991. * @dest: network address of destination node
  1992. * @selector: used to select from set of active links
  1993. *
  1994. * If no active link can be found, uses default maximum packet size.
  1995. */
  1996. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  1997. {
  1998. struct tipc_node *n_ptr;
  1999. struct tipc_link *l_ptr;
  2000. u32 res = MAX_PKT_DEFAULT;
  2001. if (dest == tipc_own_addr)
  2002. return MAX_MSG_SIZE;
  2003. n_ptr = tipc_node_find(dest);
  2004. if (n_ptr) {
  2005. tipc_node_lock(n_ptr);
  2006. l_ptr = n_ptr->active_links[selector & 1];
  2007. if (l_ptr)
  2008. res = l_ptr->max_pkt;
  2009. tipc_node_unlock(n_ptr);
  2010. }
  2011. return res;
  2012. }
  2013. static void link_print(struct tipc_link *l_ptr, const char *str)
  2014. {
  2015. struct tipc_bearer *b_ptr;
  2016. rcu_read_lock();
  2017. b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
  2018. if (b_ptr)
  2019. pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
  2020. rcu_read_unlock();
  2021. if (link_working_unknown(l_ptr))
  2022. pr_cont(":WU\n");
  2023. else if (link_reset_reset(l_ptr))
  2024. pr_cont(":RR\n");
  2025. else if (link_reset_unknown(l_ptr))
  2026. pr_cont(":RU\n");
  2027. else if (link_working_working(l_ptr))
  2028. pr_cont(":WW\n");
  2029. else
  2030. pr_cont("\n");
  2031. }
  2032. /* Parse and validate nested (link) properties valid for media, bearer and link
  2033. */
  2034. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  2035. {
  2036. int err;
  2037. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  2038. tipc_nl_prop_policy);
  2039. if (err)
  2040. return err;
  2041. if (props[TIPC_NLA_PROP_PRIO]) {
  2042. u32 prio;
  2043. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2044. if (prio > TIPC_MAX_LINK_PRI)
  2045. return -EINVAL;
  2046. }
  2047. if (props[TIPC_NLA_PROP_TOL]) {
  2048. u32 tol;
  2049. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2050. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  2051. return -EINVAL;
  2052. }
  2053. if (props[TIPC_NLA_PROP_WIN]) {
  2054. u32 win;
  2055. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2056. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  2057. return -EINVAL;
  2058. }
  2059. return 0;
  2060. }
  2061. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  2062. {
  2063. int err;
  2064. int res = 0;
  2065. int bearer_id;
  2066. char *name;
  2067. struct tipc_link *link;
  2068. struct tipc_node *node;
  2069. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2070. if (!info->attrs[TIPC_NLA_LINK])
  2071. return -EINVAL;
  2072. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  2073. info->attrs[TIPC_NLA_LINK],
  2074. tipc_nl_link_policy);
  2075. if (err)
  2076. return err;
  2077. if (!attrs[TIPC_NLA_LINK_NAME])
  2078. return -EINVAL;
  2079. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2080. node = tipc_link_find_owner(name, &bearer_id);
  2081. if (!node)
  2082. return -EINVAL;
  2083. tipc_node_lock(node);
  2084. link = node->links[bearer_id];
  2085. if (!link) {
  2086. res = -EINVAL;
  2087. goto out;
  2088. }
  2089. if (attrs[TIPC_NLA_LINK_PROP]) {
  2090. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  2091. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  2092. props);
  2093. if (err) {
  2094. res = err;
  2095. goto out;
  2096. }
  2097. if (props[TIPC_NLA_PROP_TOL]) {
  2098. u32 tol;
  2099. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2100. link_set_supervision_props(link, tol);
  2101. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
  2102. }
  2103. if (props[TIPC_NLA_PROP_PRIO]) {
  2104. u32 prio;
  2105. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2106. link->priority = prio;
  2107. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
  2108. }
  2109. if (props[TIPC_NLA_PROP_WIN]) {
  2110. u32 win;
  2111. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2112. tipc_link_set_queue_limits(link, win);
  2113. }
  2114. }
  2115. out:
  2116. tipc_node_unlock(node);
  2117. return res;
  2118. }
  2119. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  2120. {
  2121. int i;
  2122. struct nlattr *stats;
  2123. struct nla_map {
  2124. u32 key;
  2125. u32 val;
  2126. };
  2127. struct nla_map map[] = {
  2128. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  2129. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  2130. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  2131. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  2132. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  2133. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  2134. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  2135. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  2136. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  2137. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  2138. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  2139. s->msg_length_counts : 1},
  2140. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  2141. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  2142. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  2143. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  2144. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  2145. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  2146. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  2147. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  2148. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  2149. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  2150. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  2151. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  2152. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  2153. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  2154. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  2155. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  2156. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  2157. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  2158. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  2159. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  2160. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  2161. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  2162. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  2163. };
  2164. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  2165. if (!stats)
  2166. return -EMSGSIZE;
  2167. for (i = 0; i < ARRAY_SIZE(map); i++)
  2168. if (nla_put_u32(skb, map[i].key, map[i].val))
  2169. goto msg_full;
  2170. nla_nest_end(skb, stats);
  2171. return 0;
  2172. msg_full:
  2173. nla_nest_cancel(skb, stats);
  2174. return -EMSGSIZE;
  2175. }
  2176. /* Caller should hold appropriate locks to protect the link */
  2177. static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
  2178. {
  2179. int err;
  2180. void *hdr;
  2181. struct nlattr *attrs;
  2182. struct nlattr *prop;
  2183. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
  2184. NLM_F_MULTI, TIPC_NL_LINK_GET);
  2185. if (!hdr)
  2186. return -EMSGSIZE;
  2187. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  2188. if (!attrs)
  2189. goto msg_full;
  2190. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  2191. goto attr_msg_full;
  2192. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  2193. tipc_cluster_mask(tipc_own_addr)))
  2194. goto attr_msg_full;
  2195. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
  2196. goto attr_msg_full;
  2197. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
  2198. goto attr_msg_full;
  2199. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
  2200. goto attr_msg_full;
  2201. if (tipc_link_is_up(link))
  2202. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  2203. goto attr_msg_full;
  2204. if (tipc_link_is_active(link))
  2205. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  2206. goto attr_msg_full;
  2207. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  2208. if (!prop)
  2209. goto attr_msg_full;
  2210. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2211. goto prop_msg_full;
  2212. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  2213. goto prop_msg_full;
  2214. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  2215. link->queue_limit[TIPC_LOW_IMPORTANCE]))
  2216. goto prop_msg_full;
  2217. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  2218. goto prop_msg_full;
  2219. nla_nest_end(msg->skb, prop);
  2220. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  2221. if (err)
  2222. goto attr_msg_full;
  2223. nla_nest_end(msg->skb, attrs);
  2224. genlmsg_end(msg->skb, hdr);
  2225. return 0;
  2226. prop_msg_full:
  2227. nla_nest_cancel(msg->skb, prop);
  2228. attr_msg_full:
  2229. nla_nest_cancel(msg->skb, attrs);
  2230. msg_full:
  2231. genlmsg_cancel(msg->skb, hdr);
  2232. return -EMSGSIZE;
  2233. }
  2234. /* Caller should hold node lock */
  2235. static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
  2236. struct tipc_node *node,
  2237. u32 *prev_link)
  2238. {
  2239. u32 i;
  2240. int err;
  2241. for (i = *prev_link; i < MAX_BEARERS; i++) {
  2242. *prev_link = i;
  2243. if (!node->links[i])
  2244. continue;
  2245. err = __tipc_nl_add_link(msg, node->links[i]);
  2246. if (err)
  2247. return err;
  2248. }
  2249. *prev_link = 0;
  2250. return 0;
  2251. }
  2252. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2253. {
  2254. struct tipc_node *node;
  2255. struct tipc_nl_msg msg;
  2256. u32 prev_node = cb->args[0];
  2257. u32 prev_link = cb->args[1];
  2258. int done = cb->args[2];
  2259. int err;
  2260. if (done)
  2261. return 0;
  2262. msg.skb = skb;
  2263. msg.portid = NETLINK_CB(cb->skb).portid;
  2264. msg.seq = cb->nlh->nlmsg_seq;
  2265. rcu_read_lock();
  2266. if (prev_node) {
  2267. node = tipc_node_find(prev_node);
  2268. if (!node) {
  2269. /* We never set seq or call nl_dump_check_consistent()
  2270. * this means that setting prev_seq here will cause the
  2271. * consistence check to fail in the netlink callback
  2272. * handler. Resulting in the last NLMSG_DONE message
  2273. * having the NLM_F_DUMP_INTR flag set.
  2274. */
  2275. cb->prev_seq = 1;
  2276. goto out;
  2277. }
  2278. list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
  2279. tipc_node_lock(node);
  2280. err = __tipc_nl_add_node_links(&msg, node, &prev_link);
  2281. tipc_node_unlock(node);
  2282. if (err)
  2283. goto out;
  2284. prev_node = node->addr;
  2285. }
  2286. } else {
  2287. err = tipc_nl_add_bc_link(&msg);
  2288. if (err)
  2289. goto out;
  2290. list_for_each_entry_rcu(node, &tipc_node_list, list) {
  2291. tipc_node_lock(node);
  2292. err = __tipc_nl_add_node_links(&msg, node, &prev_link);
  2293. tipc_node_unlock(node);
  2294. if (err)
  2295. goto out;
  2296. prev_node = node->addr;
  2297. }
  2298. }
  2299. done = 1;
  2300. out:
  2301. rcu_read_unlock();
  2302. cb->args[0] = prev_node;
  2303. cb->args[1] = prev_link;
  2304. cb->args[2] = done;
  2305. return skb->len;
  2306. }
  2307. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  2308. {
  2309. struct sk_buff *ans_skb;
  2310. struct tipc_nl_msg msg;
  2311. struct tipc_link *link;
  2312. struct tipc_node *node;
  2313. char *name;
  2314. int bearer_id;
  2315. int err;
  2316. if (!info->attrs[TIPC_NLA_LINK_NAME])
  2317. return -EINVAL;
  2318. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  2319. node = tipc_link_find_owner(name, &bearer_id);
  2320. if (!node)
  2321. return -EINVAL;
  2322. ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  2323. if (!ans_skb)
  2324. return -ENOMEM;
  2325. msg.skb = ans_skb;
  2326. msg.portid = info->snd_portid;
  2327. msg.seq = info->snd_seq;
  2328. tipc_node_lock(node);
  2329. link = node->links[bearer_id];
  2330. if (!link) {
  2331. err = -EINVAL;
  2332. goto err_out;
  2333. }
  2334. err = __tipc_nl_add_link(&msg, link);
  2335. if (err)
  2336. goto err_out;
  2337. tipc_node_unlock(node);
  2338. return genlmsg_reply(ans_skb, info);
  2339. err_out:
  2340. tipc_node_unlock(node);
  2341. nlmsg_free(ans_skb);
  2342. return err;
  2343. }
  2344. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  2345. {
  2346. int err;
  2347. char *link_name;
  2348. unsigned int bearer_id;
  2349. struct tipc_link *link;
  2350. struct tipc_node *node;
  2351. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2352. if (!info->attrs[TIPC_NLA_LINK])
  2353. return -EINVAL;
  2354. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  2355. info->attrs[TIPC_NLA_LINK],
  2356. tipc_nl_link_policy);
  2357. if (err)
  2358. return err;
  2359. if (!attrs[TIPC_NLA_LINK_NAME])
  2360. return -EINVAL;
  2361. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2362. if (strcmp(link_name, tipc_bclink_name) == 0) {
  2363. err = tipc_bclink_reset_stats();
  2364. if (err)
  2365. return err;
  2366. return 0;
  2367. }
  2368. node = tipc_link_find_owner(link_name, &bearer_id);
  2369. if (!node)
  2370. return -EINVAL;
  2371. tipc_node_lock(node);
  2372. link = node->links[bearer_id];
  2373. if (!link) {
  2374. tipc_node_unlock(node);
  2375. return -EINVAL;
  2376. }
  2377. link_reset_statistics(link);
  2378. tipc_node_unlock(node);
  2379. return 0;
  2380. }