link.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012, 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 "port.h"
  39. #include "name_distr.h"
  40. #include "discover.h"
  41. #include "config.h"
  42. #include <linux/pkt_sched.h>
  43. /*
  44. * Error message prefixes
  45. */
  46. static const char *link_co_err = "Link changeover error, ";
  47. static const char *link_rst_msg = "Resetting link ";
  48. static const char *link_unk_evt = "Unknown link event ";
  49. /*
  50. * Out-of-range value for link session numbers
  51. */
  52. #define INVALID_SESSION 0x10000
  53. /*
  54. * Link state events:
  55. */
  56. #define STARTING_EVT 856384768 /* link processing trigger */
  57. #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
  58. #define TIMEOUT_EVT 560817u /* link timer expired */
  59. /*
  60. * The following two 'message types' is really just implementation
  61. * data conveniently stored in the message header.
  62. * They must not be considered part of the protocol
  63. */
  64. #define OPEN_MSG 0
  65. #define CLOSED_MSG 1
  66. /*
  67. * State value stored in 'exp_msg_count'
  68. */
  69. #define START_CHANGEOVER 100000u
  70. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  71. struct sk_buff *buf);
  72. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
  73. static int link_recv_changeover_msg(struct tipc_link **l_ptr,
  74. struct sk_buff **buf);
  75. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
  76. static int link_send_sections_long(struct tipc_port *sender,
  77. struct iovec const *msg_sect,
  78. unsigned int len, u32 destnode);
  79. static void link_state_event(struct tipc_link *l_ptr, u32 event);
  80. static void link_reset_statistics(struct tipc_link *l_ptr);
  81. static void link_print(struct tipc_link *l_ptr, const char *str);
  82. static void link_start(struct tipc_link *l_ptr);
  83. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
  84. static void tipc_link_send_sync(struct tipc_link *l);
  85. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
  86. /*
  87. * Simple link routines
  88. */
  89. static unsigned int align(unsigned int i)
  90. {
  91. return (i + 3) & ~3u;
  92. }
  93. static void link_init_max_pkt(struct tipc_link *l_ptr)
  94. {
  95. u32 max_pkt;
  96. max_pkt = (l_ptr->b_ptr->mtu & ~3);
  97. if (max_pkt > MAX_MSG_SIZE)
  98. max_pkt = MAX_MSG_SIZE;
  99. l_ptr->max_pkt_target = max_pkt;
  100. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  101. l_ptr->max_pkt = l_ptr->max_pkt_target;
  102. else
  103. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  104. l_ptr->max_pkt_probes = 0;
  105. }
  106. static u32 link_next_sent(struct tipc_link *l_ptr)
  107. {
  108. if (l_ptr->next_out)
  109. return buf_seqno(l_ptr->next_out);
  110. return mod(l_ptr->next_out_no);
  111. }
  112. static u32 link_last_sent(struct tipc_link *l_ptr)
  113. {
  114. return mod(link_next_sent(l_ptr) - 1);
  115. }
  116. /*
  117. * Simple non-static link routines (i.e. referenced outside this file)
  118. */
  119. int tipc_link_is_up(struct tipc_link *l_ptr)
  120. {
  121. if (!l_ptr)
  122. return 0;
  123. return link_working_working(l_ptr) || link_working_unknown(l_ptr);
  124. }
  125. int tipc_link_is_active(struct tipc_link *l_ptr)
  126. {
  127. return (l_ptr->owner->active_links[0] == l_ptr) ||
  128. (l_ptr->owner->active_links[1] == l_ptr);
  129. }
  130. /**
  131. * link_timeout - handle expiration of link timer
  132. * @l_ptr: pointer to link
  133. *
  134. * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
  135. * with tipc_link_delete(). (There is no risk that the node will be deleted by
  136. * another thread because tipc_link_delete() always cancels the link timer before
  137. * tipc_node_delete() is called.)
  138. */
  139. static void link_timeout(struct tipc_link *l_ptr)
  140. {
  141. tipc_node_lock(l_ptr->owner);
  142. /* update counters used in statistical profiling of send traffic */
  143. l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
  144. l_ptr->stats.queue_sz_counts++;
  145. if (l_ptr->first_out) {
  146. struct tipc_msg *msg = buf_msg(l_ptr->first_out);
  147. u32 length = msg_size(msg);
  148. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  149. (msg_type(msg) == FIRST_FRAGMENT)) {
  150. length = msg_size(msg_get_wrapped(msg));
  151. }
  152. if (length) {
  153. l_ptr->stats.msg_lengths_total += length;
  154. l_ptr->stats.msg_length_counts++;
  155. if (length <= 64)
  156. l_ptr->stats.msg_length_profile[0]++;
  157. else if (length <= 256)
  158. l_ptr->stats.msg_length_profile[1]++;
  159. else if (length <= 1024)
  160. l_ptr->stats.msg_length_profile[2]++;
  161. else if (length <= 4096)
  162. l_ptr->stats.msg_length_profile[3]++;
  163. else if (length <= 16384)
  164. l_ptr->stats.msg_length_profile[4]++;
  165. else if (length <= 32768)
  166. l_ptr->stats.msg_length_profile[5]++;
  167. else
  168. l_ptr->stats.msg_length_profile[6]++;
  169. }
  170. }
  171. /* do all other link processing performed on a periodic basis */
  172. link_state_event(l_ptr, TIMEOUT_EVT);
  173. if (l_ptr->next_out)
  174. tipc_link_push_queue(l_ptr);
  175. tipc_node_unlock(l_ptr->owner);
  176. }
  177. static void link_set_timer(struct tipc_link *l_ptr, u32 time)
  178. {
  179. k_start_timer(&l_ptr->timer, time);
  180. }
  181. /**
  182. * tipc_link_create - create a new link
  183. * @n_ptr: pointer to associated node
  184. * @b_ptr: pointer to associated bearer
  185. * @media_addr: media address to use when sending messages over link
  186. *
  187. * Returns pointer to link.
  188. */
  189. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  190. struct tipc_bearer *b_ptr,
  191. const struct tipc_media_addr *media_addr)
  192. {
  193. struct tipc_link *l_ptr;
  194. struct tipc_msg *msg;
  195. char *if_name;
  196. char addr_string[16];
  197. u32 peer = n_ptr->addr;
  198. if (n_ptr->link_cnt >= 2) {
  199. tipc_addr_string_fill(addr_string, n_ptr->addr);
  200. pr_err("Attempt to establish third link to %s\n", addr_string);
  201. return NULL;
  202. }
  203. if (n_ptr->links[b_ptr->identity]) {
  204. tipc_addr_string_fill(addr_string, n_ptr->addr);
  205. pr_err("Attempt to establish second link on <%s> to %s\n",
  206. b_ptr->name, addr_string);
  207. return NULL;
  208. }
  209. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  210. if (!l_ptr) {
  211. pr_warn("Link creation failed, no memory\n");
  212. return NULL;
  213. }
  214. l_ptr->addr = peer;
  215. if_name = strchr(b_ptr->name, ':') + 1;
  216. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  217. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  218. tipc_node(tipc_own_addr),
  219. if_name,
  220. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  221. /* note: peer i/f name is updated by reset/activate message */
  222. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  223. l_ptr->owner = n_ptr;
  224. l_ptr->checkpoint = 1;
  225. l_ptr->peer_session = INVALID_SESSION;
  226. l_ptr->b_ptr = b_ptr;
  227. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  228. l_ptr->state = RESET_UNKNOWN;
  229. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  230. msg = l_ptr->pmsg;
  231. tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
  232. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  233. msg_set_session(msg, (tipc_random & 0xffff));
  234. msg_set_bearer_id(msg, b_ptr->identity);
  235. strcpy((char *)msg_data(msg), if_name);
  236. l_ptr->priority = b_ptr->priority;
  237. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  238. link_init_max_pkt(l_ptr);
  239. l_ptr->next_out_no = 1;
  240. INIT_LIST_HEAD(&l_ptr->waiting_ports);
  241. link_reset_statistics(l_ptr);
  242. tipc_node_attach_link(n_ptr, l_ptr);
  243. k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
  244. list_add_tail(&l_ptr->link_list, &b_ptr->links);
  245. tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
  246. return l_ptr;
  247. }
  248. /**
  249. * tipc_link_delete - delete a link
  250. * @l_ptr: pointer to link
  251. *
  252. * Note: 'tipc_net_lock' is write_locked, bearer is locked.
  253. * This routine must not grab the node lock until after link timer cancellation
  254. * to avoid a potential deadlock situation.
  255. */
  256. void tipc_link_delete(struct tipc_link *l_ptr)
  257. {
  258. if (!l_ptr) {
  259. pr_err("Attempt to delete non-existent link\n");
  260. return;
  261. }
  262. k_cancel_timer(&l_ptr->timer);
  263. tipc_node_lock(l_ptr->owner);
  264. tipc_link_reset(l_ptr);
  265. tipc_node_detach_link(l_ptr->owner, l_ptr);
  266. tipc_link_stop(l_ptr);
  267. list_del_init(&l_ptr->link_list);
  268. tipc_node_unlock(l_ptr->owner);
  269. k_term_timer(&l_ptr->timer);
  270. kfree(l_ptr);
  271. }
  272. static void link_start(struct tipc_link *l_ptr)
  273. {
  274. tipc_node_lock(l_ptr->owner);
  275. link_state_event(l_ptr, STARTING_EVT);
  276. tipc_node_unlock(l_ptr->owner);
  277. }
  278. /**
  279. * link_schedule_port - schedule port for deferred sending
  280. * @l_ptr: pointer to link
  281. * @origport: reference to sending port
  282. * @sz: amount of data to be sent
  283. *
  284. * Schedules port for renewed sending of messages after link congestion
  285. * has abated.
  286. */
  287. static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
  288. {
  289. struct tipc_port *p_ptr;
  290. spin_lock_bh(&tipc_port_list_lock);
  291. p_ptr = tipc_port_lock(origport);
  292. if (p_ptr) {
  293. if (!p_ptr->wakeup)
  294. goto exit;
  295. if (!list_empty(&p_ptr->wait_list))
  296. goto exit;
  297. p_ptr->congested = 1;
  298. p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
  299. list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
  300. l_ptr->stats.link_congs++;
  301. exit:
  302. tipc_port_unlock(p_ptr);
  303. }
  304. spin_unlock_bh(&tipc_port_list_lock);
  305. return -ELINKCONG;
  306. }
  307. void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
  308. {
  309. struct tipc_port *p_ptr;
  310. struct tipc_port *temp_p_ptr;
  311. int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
  312. if (all)
  313. win = 100000;
  314. if (win <= 0)
  315. return;
  316. if (!spin_trylock_bh(&tipc_port_list_lock))
  317. return;
  318. if (link_congested(l_ptr))
  319. goto exit;
  320. list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
  321. wait_list) {
  322. if (win <= 0)
  323. break;
  324. list_del_init(&p_ptr->wait_list);
  325. spin_lock_bh(p_ptr->lock);
  326. p_ptr->congested = 0;
  327. p_ptr->wakeup(p_ptr);
  328. win -= p_ptr->waiting_pkts;
  329. spin_unlock_bh(p_ptr->lock);
  330. }
  331. exit:
  332. spin_unlock_bh(&tipc_port_list_lock);
  333. }
  334. /**
  335. * link_release_outqueue - purge link's outbound message queue
  336. * @l_ptr: pointer to link
  337. */
  338. static void link_release_outqueue(struct tipc_link *l_ptr)
  339. {
  340. struct sk_buff *buf = l_ptr->first_out;
  341. struct sk_buff *next;
  342. while (buf) {
  343. next = buf->next;
  344. kfree_skb(buf);
  345. buf = next;
  346. }
  347. l_ptr->first_out = NULL;
  348. l_ptr->out_queue_size = 0;
  349. }
  350. /**
  351. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  352. * @l_ptr: pointer to link
  353. */
  354. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  355. {
  356. struct sk_buff *buf = l_ptr->defragm_buf;
  357. struct sk_buff *next;
  358. while (buf) {
  359. next = buf->next;
  360. kfree_skb(buf);
  361. buf = next;
  362. }
  363. l_ptr->defragm_buf = NULL;
  364. }
  365. /**
  366. * tipc_link_stop - purge all inbound and outbound messages associated with link
  367. * @l_ptr: pointer to link
  368. */
  369. void tipc_link_stop(struct tipc_link *l_ptr)
  370. {
  371. struct sk_buff *buf;
  372. struct sk_buff *next;
  373. buf = l_ptr->oldest_deferred_in;
  374. while (buf) {
  375. next = buf->next;
  376. kfree_skb(buf);
  377. buf = next;
  378. }
  379. buf = l_ptr->first_out;
  380. while (buf) {
  381. next = buf->next;
  382. kfree_skb(buf);
  383. buf = next;
  384. }
  385. tipc_link_reset_fragments(l_ptr);
  386. kfree_skb(l_ptr->proto_msg_queue);
  387. l_ptr->proto_msg_queue = NULL;
  388. }
  389. void tipc_link_reset(struct tipc_link *l_ptr)
  390. {
  391. struct sk_buff *buf;
  392. u32 prev_state = l_ptr->state;
  393. u32 checkpoint = l_ptr->next_in_no;
  394. int was_active_link = tipc_link_is_active(l_ptr);
  395. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  396. /* Link is down, accept any session */
  397. l_ptr->peer_session = INVALID_SESSION;
  398. /* Prepare for max packet size negotiation */
  399. link_init_max_pkt(l_ptr);
  400. l_ptr->state = RESET_UNKNOWN;
  401. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  402. return;
  403. tipc_node_link_down(l_ptr->owner, l_ptr);
  404. tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
  405. if (was_active_link && tipc_node_active_links(l_ptr->owner) &&
  406. l_ptr->owner->permit_changeover) {
  407. l_ptr->reset_checkpoint = checkpoint;
  408. l_ptr->exp_msg_count = START_CHANGEOVER;
  409. }
  410. /* Clean up all queues: */
  411. link_release_outqueue(l_ptr);
  412. kfree_skb(l_ptr->proto_msg_queue);
  413. l_ptr->proto_msg_queue = NULL;
  414. buf = l_ptr->oldest_deferred_in;
  415. while (buf) {
  416. struct sk_buff *next = buf->next;
  417. kfree_skb(buf);
  418. buf = next;
  419. }
  420. if (!list_empty(&l_ptr->waiting_ports))
  421. tipc_link_wakeup_ports(l_ptr, 1);
  422. l_ptr->retransm_queue_head = 0;
  423. l_ptr->retransm_queue_size = 0;
  424. l_ptr->last_out = NULL;
  425. l_ptr->first_out = NULL;
  426. l_ptr->next_out = NULL;
  427. l_ptr->unacked_window = 0;
  428. l_ptr->checkpoint = 1;
  429. l_ptr->next_out_no = 1;
  430. l_ptr->deferred_inqueue_sz = 0;
  431. l_ptr->oldest_deferred_in = NULL;
  432. l_ptr->newest_deferred_in = NULL;
  433. l_ptr->fsm_msg_cnt = 0;
  434. l_ptr->stale_count = 0;
  435. link_reset_statistics(l_ptr);
  436. }
  437. static void link_activate(struct tipc_link *l_ptr)
  438. {
  439. l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
  440. tipc_node_link_up(l_ptr->owner, l_ptr);
  441. tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
  442. }
  443. /**
  444. * link_state_event - link finite state machine
  445. * @l_ptr: pointer to link
  446. * @event: state machine event to process
  447. */
  448. static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
  449. {
  450. struct tipc_link *other;
  451. u32 cont_intv = l_ptr->continuity_interval;
  452. if (!l_ptr->started && (event != STARTING_EVT))
  453. return; /* Not yet. */
  454. if (link_blocked(l_ptr)) {
  455. if (event == TIMEOUT_EVT)
  456. link_set_timer(l_ptr, cont_intv);
  457. return; /* Changeover going on */
  458. }
  459. switch (l_ptr->state) {
  460. case WORKING_WORKING:
  461. switch (event) {
  462. case TRAFFIC_MSG_EVT:
  463. case ACTIVATE_MSG:
  464. break;
  465. case TIMEOUT_EVT:
  466. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  467. l_ptr->checkpoint = l_ptr->next_in_no;
  468. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  469. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  470. 0, 0, 0, 0, 0);
  471. l_ptr->fsm_msg_cnt++;
  472. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  473. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  474. 1, 0, 0, 0, 0);
  475. l_ptr->fsm_msg_cnt++;
  476. }
  477. link_set_timer(l_ptr, cont_intv);
  478. break;
  479. }
  480. l_ptr->state = WORKING_UNKNOWN;
  481. l_ptr->fsm_msg_cnt = 0;
  482. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  483. l_ptr->fsm_msg_cnt++;
  484. link_set_timer(l_ptr, cont_intv / 4);
  485. break;
  486. case RESET_MSG:
  487. pr_info("%s<%s>, requested by peer\n", link_rst_msg,
  488. l_ptr->name);
  489. tipc_link_reset(l_ptr);
  490. l_ptr->state = RESET_RESET;
  491. l_ptr->fsm_msg_cnt = 0;
  492. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  493. l_ptr->fsm_msg_cnt++;
  494. link_set_timer(l_ptr, cont_intv);
  495. break;
  496. default:
  497. pr_err("%s%u in WW state\n", link_unk_evt, event);
  498. }
  499. break;
  500. case WORKING_UNKNOWN:
  501. switch (event) {
  502. case TRAFFIC_MSG_EVT:
  503. case ACTIVATE_MSG:
  504. l_ptr->state = WORKING_WORKING;
  505. l_ptr->fsm_msg_cnt = 0;
  506. link_set_timer(l_ptr, cont_intv);
  507. break;
  508. case RESET_MSG:
  509. pr_info("%s<%s>, requested by peer while probing\n",
  510. link_rst_msg, l_ptr->name);
  511. tipc_link_reset(l_ptr);
  512. l_ptr->state = RESET_RESET;
  513. l_ptr->fsm_msg_cnt = 0;
  514. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  515. l_ptr->fsm_msg_cnt++;
  516. link_set_timer(l_ptr, cont_intv);
  517. break;
  518. case TIMEOUT_EVT:
  519. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  520. l_ptr->state = WORKING_WORKING;
  521. l_ptr->fsm_msg_cnt = 0;
  522. l_ptr->checkpoint = l_ptr->next_in_no;
  523. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  524. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  525. 0, 0, 0, 0, 0);
  526. l_ptr->fsm_msg_cnt++;
  527. }
  528. link_set_timer(l_ptr, cont_intv);
  529. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  530. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  531. 1, 0, 0, 0, 0);
  532. l_ptr->fsm_msg_cnt++;
  533. link_set_timer(l_ptr, cont_intv / 4);
  534. } else { /* Link has failed */
  535. pr_warn("%s<%s>, peer not responding\n",
  536. link_rst_msg, l_ptr->name);
  537. tipc_link_reset(l_ptr);
  538. l_ptr->state = RESET_UNKNOWN;
  539. l_ptr->fsm_msg_cnt = 0;
  540. tipc_link_send_proto_msg(l_ptr, RESET_MSG,
  541. 0, 0, 0, 0, 0);
  542. l_ptr->fsm_msg_cnt++;
  543. link_set_timer(l_ptr, cont_intv);
  544. }
  545. break;
  546. default:
  547. pr_err("%s%u in WU state\n", link_unk_evt, event);
  548. }
  549. break;
  550. case RESET_UNKNOWN:
  551. switch (event) {
  552. case TRAFFIC_MSG_EVT:
  553. break;
  554. case ACTIVATE_MSG:
  555. other = l_ptr->owner->active_links[0];
  556. if (other && link_working_unknown(other))
  557. break;
  558. l_ptr->state = WORKING_WORKING;
  559. l_ptr->fsm_msg_cnt = 0;
  560. link_activate(l_ptr);
  561. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  562. l_ptr->fsm_msg_cnt++;
  563. if (l_ptr->owner->working_links == 1)
  564. tipc_link_send_sync(l_ptr);
  565. link_set_timer(l_ptr, cont_intv);
  566. break;
  567. case RESET_MSG:
  568. l_ptr->state = RESET_RESET;
  569. l_ptr->fsm_msg_cnt = 0;
  570. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
  571. l_ptr->fsm_msg_cnt++;
  572. link_set_timer(l_ptr, cont_intv);
  573. break;
  574. case STARTING_EVT:
  575. l_ptr->started = 1;
  576. /* fall through */
  577. case TIMEOUT_EVT:
  578. tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  579. l_ptr->fsm_msg_cnt++;
  580. link_set_timer(l_ptr, cont_intv);
  581. break;
  582. default:
  583. pr_err("%s%u in RU state\n", link_unk_evt, event);
  584. }
  585. break;
  586. case RESET_RESET:
  587. switch (event) {
  588. case TRAFFIC_MSG_EVT:
  589. case ACTIVATE_MSG:
  590. other = l_ptr->owner->active_links[0];
  591. if (other && link_working_unknown(other))
  592. break;
  593. l_ptr->state = WORKING_WORKING;
  594. l_ptr->fsm_msg_cnt = 0;
  595. link_activate(l_ptr);
  596. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  597. l_ptr->fsm_msg_cnt++;
  598. if (l_ptr->owner->working_links == 1)
  599. tipc_link_send_sync(l_ptr);
  600. link_set_timer(l_ptr, cont_intv);
  601. break;
  602. case RESET_MSG:
  603. break;
  604. case TIMEOUT_EVT:
  605. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  606. l_ptr->fsm_msg_cnt++;
  607. link_set_timer(l_ptr, cont_intv);
  608. break;
  609. default:
  610. pr_err("%s%u in RR state\n", link_unk_evt, event);
  611. }
  612. break;
  613. default:
  614. pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
  615. }
  616. }
  617. /*
  618. * link_bundle_buf(): Append contents of a buffer to
  619. * the tail of an existing one.
  620. */
  621. static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
  622. struct sk_buff *buf)
  623. {
  624. struct tipc_msg *bundler_msg = buf_msg(bundler);
  625. struct tipc_msg *msg = buf_msg(buf);
  626. u32 size = msg_size(msg);
  627. u32 bundle_size = msg_size(bundler_msg);
  628. u32 to_pos = align(bundle_size);
  629. u32 pad = to_pos - bundle_size;
  630. if (msg_user(bundler_msg) != MSG_BUNDLER)
  631. return 0;
  632. if (msg_type(bundler_msg) != OPEN_MSG)
  633. return 0;
  634. if (skb_tailroom(bundler) < (pad + size))
  635. return 0;
  636. if (l_ptr->max_pkt < (to_pos + size))
  637. return 0;
  638. skb_put(bundler, pad + size);
  639. skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
  640. msg_set_size(bundler_msg, to_pos + size);
  641. msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
  642. kfree_skb(buf);
  643. l_ptr->stats.sent_bundled++;
  644. return 1;
  645. }
  646. static void link_add_to_outqueue(struct tipc_link *l_ptr,
  647. struct sk_buff *buf,
  648. struct tipc_msg *msg)
  649. {
  650. u32 ack = mod(l_ptr->next_in_no - 1);
  651. u32 seqno = mod(l_ptr->next_out_no++);
  652. msg_set_word(msg, 2, ((ack << 16) | seqno));
  653. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  654. buf->next = NULL;
  655. if (l_ptr->first_out) {
  656. l_ptr->last_out->next = buf;
  657. l_ptr->last_out = buf;
  658. } else
  659. l_ptr->first_out = l_ptr->last_out = buf;
  660. l_ptr->out_queue_size++;
  661. if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
  662. l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
  663. }
  664. static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
  665. struct sk_buff *buf_chain,
  666. u32 long_msgno)
  667. {
  668. struct sk_buff *buf;
  669. struct tipc_msg *msg;
  670. if (!l_ptr->next_out)
  671. l_ptr->next_out = buf_chain;
  672. while (buf_chain) {
  673. buf = buf_chain;
  674. buf_chain = buf_chain->next;
  675. msg = buf_msg(buf);
  676. msg_set_long_msgno(msg, long_msgno);
  677. link_add_to_outqueue(l_ptr, buf, msg);
  678. }
  679. }
  680. /*
  681. * tipc_link_send_buf() is the 'full path' for messages, called from
  682. * inside TIPC when the 'fast path' in tipc_send_buf
  683. * has failed, and from link_send()
  684. */
  685. int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  686. {
  687. struct tipc_msg *msg = buf_msg(buf);
  688. u32 size = msg_size(msg);
  689. u32 dsz = msg_data_sz(msg);
  690. u32 queue_size = l_ptr->out_queue_size;
  691. u32 imp = tipc_msg_tot_importance(msg);
  692. u32 queue_limit = l_ptr->queue_limit[imp];
  693. u32 max_packet = l_ptr->max_pkt;
  694. /* Match msg importance against queue limits: */
  695. if (unlikely(queue_size >= queue_limit)) {
  696. if (imp <= TIPC_CRITICAL_IMPORTANCE) {
  697. link_schedule_port(l_ptr, msg_origport(msg), size);
  698. kfree_skb(buf);
  699. return -ELINKCONG;
  700. }
  701. kfree_skb(buf);
  702. if (imp > CONN_MANAGER) {
  703. pr_warn("%s<%s>, send queue full", link_rst_msg,
  704. l_ptr->name);
  705. tipc_link_reset(l_ptr);
  706. }
  707. return dsz;
  708. }
  709. /* Fragmentation needed ? */
  710. if (size > max_packet)
  711. return link_send_long_buf(l_ptr, buf);
  712. /* Packet can be queued or sent. */
  713. if (likely(!tipc_bearer_blocked(l_ptr->b_ptr) &&
  714. !link_congested(l_ptr))) {
  715. link_add_to_outqueue(l_ptr, buf, msg);
  716. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  717. l_ptr->unacked_window = 0;
  718. return dsz;
  719. }
  720. /* Congestion: can message be bundled ? */
  721. if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
  722. (msg_user(msg) != MSG_FRAGMENTER)) {
  723. /* Try adding message to an existing bundle */
  724. if (l_ptr->next_out &&
  725. link_bundle_buf(l_ptr, l_ptr->last_out, buf))
  726. return dsz;
  727. /* Try creating a new bundle */
  728. if (size <= max_packet * 2 / 3) {
  729. struct sk_buff *bundler = tipc_buf_acquire(max_packet);
  730. struct tipc_msg bundler_hdr;
  731. if (bundler) {
  732. tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
  733. INT_H_SIZE, l_ptr->addr);
  734. skb_copy_to_linear_data(bundler, &bundler_hdr,
  735. INT_H_SIZE);
  736. skb_trim(bundler, INT_H_SIZE);
  737. link_bundle_buf(l_ptr, bundler, buf);
  738. buf = bundler;
  739. msg = buf_msg(buf);
  740. l_ptr->stats.sent_bundles++;
  741. }
  742. }
  743. }
  744. if (!l_ptr->next_out)
  745. l_ptr->next_out = buf;
  746. link_add_to_outqueue(l_ptr, buf, msg);
  747. return dsz;
  748. }
  749. /*
  750. * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
  751. * not been selected yet, and the the owner node is not locked
  752. * Called by TIPC internal users, e.g. the name distributor
  753. */
  754. int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
  755. {
  756. struct tipc_link *l_ptr;
  757. struct tipc_node *n_ptr;
  758. int res = -ELINKCONG;
  759. read_lock_bh(&tipc_net_lock);
  760. n_ptr = tipc_node_find(dest);
  761. if (n_ptr) {
  762. tipc_node_lock(n_ptr);
  763. l_ptr = n_ptr->active_links[selector & 1];
  764. if (l_ptr)
  765. res = tipc_link_send_buf(l_ptr, buf);
  766. else
  767. kfree_skb(buf);
  768. tipc_node_unlock(n_ptr);
  769. } else {
  770. kfree_skb(buf);
  771. }
  772. read_unlock_bh(&tipc_net_lock);
  773. return res;
  774. }
  775. /*
  776. * tipc_link_send_sync - synchronize broadcast link endpoints.
  777. *
  778. * Give a newly added peer node the sequence number where it should
  779. * start receiving and acking broadcast packets.
  780. *
  781. * Called with node locked
  782. */
  783. static void tipc_link_send_sync(struct tipc_link *l)
  784. {
  785. struct sk_buff *buf;
  786. struct tipc_msg *msg;
  787. buf = tipc_buf_acquire(INT_H_SIZE);
  788. if (!buf)
  789. return;
  790. msg = buf_msg(buf);
  791. tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
  792. msg_set_last_bcast(msg, l->owner->bclink.acked);
  793. link_add_chain_to_outqueue(l, buf, 0);
  794. tipc_link_push_queue(l);
  795. }
  796. /*
  797. * tipc_link_recv_sync - synchronize broadcast link endpoints.
  798. * Receive the sequence number where we should start receiving and
  799. * acking broadcast packets from a newly added peer node, and open
  800. * up for reception of such packets.
  801. *
  802. * Called with node locked
  803. */
  804. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
  805. {
  806. struct tipc_msg *msg = buf_msg(buf);
  807. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  808. n->bclink.recv_permitted = true;
  809. kfree_skb(buf);
  810. }
  811. /*
  812. * tipc_link_send_names - send name table entries to new neighbor
  813. *
  814. * Send routine for bulk delivery of name table messages when contact
  815. * with a new neighbor occurs. No link congestion checking is performed
  816. * because name table messages *must* be delivered. The messages must be
  817. * small enough not to require fragmentation.
  818. * Called without any locks held.
  819. */
  820. void tipc_link_send_names(struct list_head *message_list, u32 dest)
  821. {
  822. struct tipc_node *n_ptr;
  823. struct tipc_link *l_ptr;
  824. struct sk_buff *buf;
  825. struct sk_buff *temp_buf;
  826. if (list_empty(message_list))
  827. return;
  828. read_lock_bh(&tipc_net_lock);
  829. n_ptr = tipc_node_find(dest);
  830. if (n_ptr) {
  831. tipc_node_lock(n_ptr);
  832. l_ptr = n_ptr->active_links[0];
  833. if (l_ptr) {
  834. /* convert circular list to linear list */
  835. ((struct sk_buff *)message_list->prev)->next = NULL;
  836. link_add_chain_to_outqueue(l_ptr,
  837. (struct sk_buff *)message_list->next, 0);
  838. tipc_link_push_queue(l_ptr);
  839. INIT_LIST_HEAD(message_list);
  840. }
  841. tipc_node_unlock(n_ptr);
  842. }
  843. read_unlock_bh(&tipc_net_lock);
  844. /* discard the messages if they couldn't be sent */
  845. list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
  846. list_del((struct list_head *)buf);
  847. kfree_skb(buf);
  848. }
  849. }
  850. /*
  851. * link_send_buf_fast: Entry for data messages where the
  852. * destination link is known and the header is complete,
  853. * inclusive total message length. Very time critical.
  854. * Link is locked. Returns user data length.
  855. */
  856. static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
  857. u32 *used_max_pkt)
  858. {
  859. struct tipc_msg *msg = buf_msg(buf);
  860. int res = msg_data_sz(msg);
  861. if (likely(!link_congested(l_ptr))) {
  862. if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
  863. if (likely(!tipc_bearer_blocked(l_ptr->b_ptr))) {
  864. link_add_to_outqueue(l_ptr, buf, msg);
  865. tipc_bearer_send(l_ptr->b_ptr, buf,
  866. &l_ptr->media_addr);
  867. l_ptr->unacked_window = 0;
  868. return res;
  869. }
  870. } else
  871. *used_max_pkt = l_ptr->max_pkt;
  872. }
  873. return tipc_link_send_buf(l_ptr, buf); /* All other cases */
  874. }
  875. /*
  876. * tipc_link_send_sections_fast: Entry for messages where the
  877. * destination processor is known and the header is complete,
  878. * except for total message length.
  879. * Returns user data length or errno.
  880. */
  881. int tipc_link_send_sections_fast(struct tipc_port *sender,
  882. struct iovec const *msg_sect,
  883. unsigned int len, u32 destaddr)
  884. {
  885. struct tipc_msg *hdr = &sender->phdr;
  886. struct tipc_link *l_ptr;
  887. struct sk_buff *buf;
  888. struct tipc_node *node;
  889. int res;
  890. u32 selector = msg_origport(hdr) & 1;
  891. again:
  892. /*
  893. * Try building message using port's max_pkt hint.
  894. * (Must not hold any locks while building message.)
  895. */
  896. res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
  897. /* Exit if build request was invalid */
  898. if (unlikely(res < 0))
  899. return res;
  900. read_lock_bh(&tipc_net_lock);
  901. node = tipc_node_find(destaddr);
  902. if (likely(node)) {
  903. tipc_node_lock(node);
  904. l_ptr = node->active_links[selector];
  905. if (likely(l_ptr)) {
  906. if (likely(buf)) {
  907. res = link_send_buf_fast(l_ptr, buf,
  908. &sender->max_pkt);
  909. exit:
  910. tipc_node_unlock(node);
  911. read_unlock_bh(&tipc_net_lock);
  912. return res;
  913. }
  914. /* Exit if link (or bearer) is congested */
  915. if (link_congested(l_ptr) ||
  916. tipc_bearer_blocked(l_ptr->b_ptr)) {
  917. res = link_schedule_port(l_ptr,
  918. sender->ref, res);
  919. goto exit;
  920. }
  921. /*
  922. * Message size exceeds max_pkt hint; update hint,
  923. * then re-try fast path or fragment the message
  924. */
  925. sender->max_pkt = l_ptr->max_pkt;
  926. tipc_node_unlock(node);
  927. read_unlock_bh(&tipc_net_lock);
  928. if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
  929. goto again;
  930. return link_send_sections_long(sender, msg_sect, len,
  931. destaddr);
  932. }
  933. tipc_node_unlock(node);
  934. }
  935. read_unlock_bh(&tipc_net_lock);
  936. /* Couldn't find a link to the destination node */
  937. if (buf)
  938. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  939. if (res >= 0)
  940. return tipc_port_reject_sections(sender, hdr, msg_sect,
  941. len, TIPC_ERR_NO_NODE);
  942. return res;
  943. }
  944. /*
  945. * link_send_sections_long(): Entry for long messages where the
  946. * destination node is known and the header is complete,
  947. * inclusive total message length.
  948. * Link and bearer congestion status have been checked to be ok,
  949. * and are ignored if they change.
  950. *
  951. * Note that fragments do not use the full link MTU so that they won't have
  952. * to undergo refragmentation if link changeover causes them to be sent
  953. * over another link with an additional tunnel header added as prefix.
  954. * (Refragmentation will still occur if the other link has a smaller MTU.)
  955. *
  956. * Returns user data length or errno.
  957. */
  958. static int link_send_sections_long(struct tipc_port *sender,
  959. struct iovec const *msg_sect,
  960. unsigned int len, u32 destaddr)
  961. {
  962. struct tipc_link *l_ptr;
  963. struct tipc_node *node;
  964. struct tipc_msg *hdr = &sender->phdr;
  965. u32 dsz = len;
  966. u32 max_pkt, fragm_sz, rest;
  967. struct tipc_msg fragm_hdr;
  968. struct sk_buff *buf, *buf_chain, *prev;
  969. u32 fragm_crs, fragm_rest, hsz, sect_rest;
  970. const unchar __user *sect_crs;
  971. int curr_sect;
  972. u32 fragm_no;
  973. int res = 0;
  974. again:
  975. fragm_no = 1;
  976. max_pkt = sender->max_pkt - INT_H_SIZE;
  977. /* leave room for tunnel header in case of link changeover */
  978. fragm_sz = max_pkt - INT_H_SIZE;
  979. /* leave room for fragmentation header in each fragment */
  980. rest = dsz;
  981. fragm_crs = 0;
  982. fragm_rest = 0;
  983. sect_rest = 0;
  984. sect_crs = NULL;
  985. curr_sect = -1;
  986. /* Prepare reusable fragment header */
  987. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  988. INT_H_SIZE, msg_destnode(hdr));
  989. msg_set_size(&fragm_hdr, max_pkt);
  990. msg_set_fragm_no(&fragm_hdr, 1);
  991. /* Prepare header of first fragment */
  992. buf_chain = buf = tipc_buf_acquire(max_pkt);
  993. if (!buf)
  994. return -ENOMEM;
  995. buf->next = NULL;
  996. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  997. hsz = msg_hdr_sz(hdr);
  998. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
  999. /* Chop up message */
  1000. fragm_crs = INT_H_SIZE + hsz;
  1001. fragm_rest = fragm_sz - hsz;
  1002. do { /* For all sections */
  1003. u32 sz;
  1004. if (!sect_rest) {
  1005. sect_rest = msg_sect[++curr_sect].iov_len;
  1006. sect_crs = msg_sect[curr_sect].iov_base;
  1007. }
  1008. if (sect_rest < fragm_rest)
  1009. sz = sect_rest;
  1010. else
  1011. sz = fragm_rest;
  1012. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  1013. res = -EFAULT;
  1014. error:
  1015. for (; buf_chain; buf_chain = buf) {
  1016. buf = buf_chain->next;
  1017. kfree_skb(buf_chain);
  1018. }
  1019. return res;
  1020. }
  1021. sect_crs += sz;
  1022. sect_rest -= sz;
  1023. fragm_crs += sz;
  1024. fragm_rest -= sz;
  1025. rest -= sz;
  1026. if (!fragm_rest && rest) {
  1027. /* Initiate new fragment: */
  1028. if (rest <= fragm_sz) {
  1029. fragm_sz = rest;
  1030. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1031. } else {
  1032. msg_set_type(&fragm_hdr, FRAGMENT);
  1033. }
  1034. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1035. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1036. prev = buf;
  1037. buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1038. if (!buf) {
  1039. res = -ENOMEM;
  1040. goto error;
  1041. }
  1042. buf->next = NULL;
  1043. prev->next = buf;
  1044. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1045. fragm_crs = INT_H_SIZE;
  1046. fragm_rest = fragm_sz;
  1047. }
  1048. } while (rest > 0);
  1049. /*
  1050. * Now we have a buffer chain. Select a link and check
  1051. * that packet size is still OK
  1052. */
  1053. node = tipc_node_find(destaddr);
  1054. if (likely(node)) {
  1055. tipc_node_lock(node);
  1056. l_ptr = node->active_links[sender->ref & 1];
  1057. if (!l_ptr) {
  1058. tipc_node_unlock(node);
  1059. goto reject;
  1060. }
  1061. if (l_ptr->max_pkt < max_pkt) {
  1062. sender->max_pkt = l_ptr->max_pkt;
  1063. tipc_node_unlock(node);
  1064. for (; buf_chain; buf_chain = buf) {
  1065. buf = buf_chain->next;
  1066. kfree_skb(buf_chain);
  1067. }
  1068. goto again;
  1069. }
  1070. } else {
  1071. reject:
  1072. for (; buf_chain; buf_chain = buf) {
  1073. buf = buf_chain->next;
  1074. kfree_skb(buf_chain);
  1075. }
  1076. return tipc_port_reject_sections(sender, hdr, msg_sect,
  1077. len, TIPC_ERR_NO_NODE);
  1078. }
  1079. /* Append chain of fragments to send queue & send them */
  1080. l_ptr->long_msg_seq_no++;
  1081. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  1082. l_ptr->stats.sent_fragments += fragm_no;
  1083. l_ptr->stats.sent_fragmented++;
  1084. tipc_link_push_queue(l_ptr);
  1085. tipc_node_unlock(node);
  1086. return dsz;
  1087. }
  1088. /*
  1089. * tipc_link_push_packet: Push one unsent packet to the media
  1090. */
  1091. u32 tipc_link_push_packet(struct tipc_link *l_ptr)
  1092. {
  1093. struct sk_buff *buf = l_ptr->first_out;
  1094. u32 r_q_size = l_ptr->retransm_queue_size;
  1095. u32 r_q_head = l_ptr->retransm_queue_head;
  1096. /* Step to position where retransmission failed, if any, */
  1097. /* consider that buffers may have been released in meantime */
  1098. if (r_q_size && buf) {
  1099. u32 last = lesser(mod(r_q_head + r_q_size),
  1100. link_last_sent(l_ptr));
  1101. u32 first = buf_seqno(buf);
  1102. while (buf && less(first, r_q_head)) {
  1103. first = mod(first + 1);
  1104. buf = buf->next;
  1105. }
  1106. l_ptr->retransm_queue_head = r_q_head = first;
  1107. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1108. }
  1109. /* Continue retransmission now, if there is anything: */
  1110. if (r_q_size && buf) {
  1111. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1112. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1113. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1114. l_ptr->retransm_queue_head = mod(++r_q_head);
  1115. l_ptr->retransm_queue_size = --r_q_size;
  1116. l_ptr->stats.retransmitted++;
  1117. return 0;
  1118. }
  1119. /* Send deferred protocol message, if any: */
  1120. buf = l_ptr->proto_msg_queue;
  1121. if (buf) {
  1122. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1123. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1124. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1125. l_ptr->unacked_window = 0;
  1126. kfree_skb(buf);
  1127. l_ptr->proto_msg_queue = NULL;
  1128. return 0;
  1129. }
  1130. /* Send one deferred data message, if send window not full: */
  1131. buf = l_ptr->next_out;
  1132. if (buf) {
  1133. struct tipc_msg *msg = buf_msg(buf);
  1134. u32 next = msg_seqno(msg);
  1135. u32 first = buf_seqno(l_ptr->first_out);
  1136. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1137. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1138. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1139. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1140. if (msg_user(msg) == MSG_BUNDLER)
  1141. msg_set_type(msg, CLOSED_MSG);
  1142. l_ptr->next_out = buf->next;
  1143. return 0;
  1144. }
  1145. }
  1146. return 1;
  1147. }
  1148. /*
  1149. * push_queue(): push out the unsent messages of a link where
  1150. * congestion has abated. Node is locked
  1151. */
  1152. void tipc_link_push_queue(struct tipc_link *l_ptr)
  1153. {
  1154. u32 res;
  1155. if (tipc_bearer_blocked(l_ptr->b_ptr))
  1156. return;
  1157. do {
  1158. res = tipc_link_push_packet(l_ptr);
  1159. } while (!res);
  1160. }
  1161. static void link_reset_all(unsigned long addr)
  1162. {
  1163. struct tipc_node *n_ptr;
  1164. char addr_string[16];
  1165. u32 i;
  1166. read_lock_bh(&tipc_net_lock);
  1167. n_ptr = tipc_node_find((u32)addr);
  1168. if (!n_ptr) {
  1169. read_unlock_bh(&tipc_net_lock);
  1170. return; /* node no longer exists */
  1171. }
  1172. tipc_node_lock(n_ptr);
  1173. pr_warn("Resetting all links to %s\n",
  1174. tipc_addr_string_fill(addr_string, n_ptr->addr));
  1175. for (i = 0; i < MAX_BEARERS; i++) {
  1176. if (n_ptr->links[i]) {
  1177. link_print(n_ptr->links[i], "Resetting link\n");
  1178. tipc_link_reset(n_ptr->links[i]);
  1179. }
  1180. }
  1181. tipc_node_unlock(n_ptr);
  1182. read_unlock_bh(&tipc_net_lock);
  1183. }
  1184. static void link_retransmit_failure(struct tipc_link *l_ptr,
  1185. struct sk_buff *buf)
  1186. {
  1187. struct tipc_msg *msg = buf_msg(buf);
  1188. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1189. if (l_ptr->addr) {
  1190. /* Handle failure on standard link */
  1191. link_print(l_ptr, "Resetting link\n");
  1192. tipc_link_reset(l_ptr);
  1193. } else {
  1194. /* Handle failure on broadcast link */
  1195. struct tipc_node *n_ptr;
  1196. char addr_string[16];
  1197. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  1198. pr_cont("Outstanding acks: %lu\n",
  1199. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1200. n_ptr = tipc_bclink_retransmit_to();
  1201. tipc_node_lock(n_ptr);
  1202. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1203. pr_info("Broadcast link info for %s\n", addr_string);
  1204. pr_info("Reception permitted: %d, Acked: %u\n",
  1205. n_ptr->bclink.recv_permitted,
  1206. n_ptr->bclink.acked);
  1207. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  1208. n_ptr->bclink.last_in,
  1209. n_ptr->bclink.oos_state,
  1210. n_ptr->bclink.last_sent);
  1211. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1212. tipc_node_unlock(n_ptr);
  1213. l_ptr->stale_count = 0;
  1214. }
  1215. }
  1216. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
  1217. u32 retransmits)
  1218. {
  1219. struct tipc_msg *msg;
  1220. if (!buf)
  1221. return;
  1222. msg = buf_msg(buf);
  1223. if (tipc_bearer_blocked(l_ptr->b_ptr)) {
  1224. if (l_ptr->retransm_queue_size == 0) {
  1225. l_ptr->retransm_queue_head = msg_seqno(msg);
  1226. l_ptr->retransm_queue_size = retransmits;
  1227. } else {
  1228. pr_err("Unexpected retransmit on link %s (qsize=%d)\n",
  1229. l_ptr->name, l_ptr->retransm_queue_size);
  1230. }
  1231. return;
  1232. } else {
  1233. /* Detect repeated retransmit failures on unblocked bearer */
  1234. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1235. if (++l_ptr->stale_count > 100) {
  1236. link_retransmit_failure(l_ptr, buf);
  1237. return;
  1238. }
  1239. } else {
  1240. l_ptr->last_retransmitted = msg_seqno(msg);
  1241. l_ptr->stale_count = 1;
  1242. }
  1243. }
  1244. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1245. msg = buf_msg(buf);
  1246. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1247. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1248. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1249. buf = buf->next;
  1250. retransmits--;
  1251. l_ptr->stats.retransmitted++;
  1252. }
  1253. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1254. }
  1255. /**
  1256. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1257. */
  1258. static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
  1259. struct sk_buff *buf)
  1260. {
  1261. u32 seq_no;
  1262. if (l_ptr->oldest_deferred_in == NULL)
  1263. return buf;
  1264. seq_no = buf_seqno(l_ptr->oldest_deferred_in);
  1265. if (seq_no == mod(l_ptr->next_in_no)) {
  1266. l_ptr->newest_deferred_in->next = buf;
  1267. buf = l_ptr->oldest_deferred_in;
  1268. l_ptr->oldest_deferred_in = NULL;
  1269. l_ptr->deferred_inqueue_sz = 0;
  1270. }
  1271. return buf;
  1272. }
  1273. /**
  1274. * link_recv_buf_validate - validate basic format of received message
  1275. *
  1276. * This routine ensures a TIPC message has an acceptable header, and at least
  1277. * as much data as the header indicates it should. The routine also ensures
  1278. * that the entire message header is stored in the main fragment of the message
  1279. * buffer, to simplify future access to message header fields.
  1280. *
  1281. * Note: Having extra info present in the message header or data areas is OK.
  1282. * TIPC will ignore the excess, under the assumption that it is optional info
  1283. * introduced by a later release of the protocol.
  1284. */
  1285. static int link_recv_buf_validate(struct sk_buff *buf)
  1286. {
  1287. static u32 min_data_hdr_size[8] = {
  1288. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  1289. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1290. };
  1291. struct tipc_msg *msg;
  1292. u32 tipc_hdr[2];
  1293. u32 size;
  1294. u32 hdr_size;
  1295. u32 min_hdr_size;
  1296. if (unlikely(buf->len < MIN_H_SIZE))
  1297. return 0;
  1298. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1299. if (msg == NULL)
  1300. return 0;
  1301. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1302. return 0;
  1303. size = msg_size(msg);
  1304. hdr_size = msg_hdr_sz(msg);
  1305. min_hdr_size = msg_isdata(msg) ?
  1306. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1307. if (unlikely((hdr_size < min_hdr_size) ||
  1308. (size < hdr_size) ||
  1309. (buf->len < size) ||
  1310. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1311. return 0;
  1312. return pskb_may_pull(buf, hdr_size);
  1313. }
  1314. /**
  1315. * tipc_recv_msg - process TIPC messages arriving from off-node
  1316. * @head: pointer to message buffer chain
  1317. * @tb_ptr: pointer to bearer message arrived on
  1318. *
  1319. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1320. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1321. */
  1322. void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
  1323. {
  1324. read_lock_bh(&tipc_net_lock);
  1325. while (head) {
  1326. struct tipc_node *n_ptr;
  1327. struct tipc_link *l_ptr;
  1328. struct sk_buff *crs;
  1329. struct sk_buff *buf = head;
  1330. struct tipc_msg *msg;
  1331. u32 seq_no;
  1332. u32 ackd;
  1333. u32 released = 0;
  1334. int type;
  1335. head = head->next;
  1336. /* Ensure bearer is still enabled */
  1337. if (unlikely(!b_ptr->active))
  1338. goto cont;
  1339. /* Ensure message is well-formed */
  1340. if (unlikely(!link_recv_buf_validate(buf)))
  1341. goto cont;
  1342. /* Ensure message data is a single contiguous unit */
  1343. if (unlikely(skb_linearize(buf)))
  1344. goto cont;
  1345. /* Handle arrival of a non-unicast link message */
  1346. msg = buf_msg(buf);
  1347. if (unlikely(msg_non_seq(msg))) {
  1348. if (msg_user(msg) == LINK_CONFIG)
  1349. tipc_disc_recv_msg(buf, b_ptr);
  1350. else
  1351. tipc_bclink_recv_pkt(buf);
  1352. continue;
  1353. }
  1354. /* Discard unicast link messages destined for another node */
  1355. if (unlikely(!msg_short(msg) &&
  1356. (msg_destnode(msg) != tipc_own_addr)))
  1357. goto cont;
  1358. /* Locate neighboring node that sent message */
  1359. n_ptr = tipc_node_find(msg_prevnode(msg));
  1360. if (unlikely(!n_ptr))
  1361. goto cont;
  1362. tipc_node_lock(n_ptr);
  1363. /* Locate unicast link endpoint that should handle message */
  1364. l_ptr = n_ptr->links[b_ptr->identity];
  1365. if (unlikely(!l_ptr)) {
  1366. tipc_node_unlock(n_ptr);
  1367. goto cont;
  1368. }
  1369. /* Verify that communication with node is currently allowed */
  1370. if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
  1371. msg_user(msg) == LINK_PROTOCOL &&
  1372. (msg_type(msg) == RESET_MSG ||
  1373. msg_type(msg) == ACTIVATE_MSG) &&
  1374. !msg_redundant_link(msg))
  1375. n_ptr->block_setup &= ~WAIT_PEER_DOWN;
  1376. if (n_ptr->block_setup) {
  1377. tipc_node_unlock(n_ptr);
  1378. goto cont;
  1379. }
  1380. /* Validate message sequence number info */
  1381. seq_no = msg_seqno(msg);
  1382. ackd = msg_ack(msg);
  1383. /* Release acked messages */
  1384. if (n_ptr->bclink.recv_permitted)
  1385. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1386. crs = l_ptr->first_out;
  1387. while ((crs != l_ptr->next_out) &&
  1388. less_eq(buf_seqno(crs), ackd)) {
  1389. struct sk_buff *next = crs->next;
  1390. kfree_skb(crs);
  1391. crs = next;
  1392. released++;
  1393. }
  1394. if (released) {
  1395. l_ptr->first_out = crs;
  1396. l_ptr->out_queue_size -= released;
  1397. }
  1398. /* Try sending any messages link endpoint has pending */
  1399. if (unlikely(l_ptr->next_out))
  1400. tipc_link_push_queue(l_ptr);
  1401. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1402. tipc_link_wakeup_ports(l_ptr, 0);
  1403. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1404. l_ptr->stats.sent_acks++;
  1405. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1406. }
  1407. /* Now (finally!) process the incoming message */
  1408. protocol_check:
  1409. if (likely(link_working_working(l_ptr))) {
  1410. if (likely(seq_no == mod(l_ptr->next_in_no))) {
  1411. l_ptr->next_in_no++;
  1412. if (unlikely(l_ptr->oldest_deferred_in))
  1413. head = link_insert_deferred_queue(l_ptr,
  1414. head);
  1415. deliver:
  1416. if (likely(msg_isdata(msg))) {
  1417. tipc_node_unlock(n_ptr);
  1418. tipc_port_recv_msg(buf);
  1419. continue;
  1420. }
  1421. switch (msg_user(msg)) {
  1422. int ret;
  1423. case MSG_BUNDLER:
  1424. l_ptr->stats.recv_bundles++;
  1425. l_ptr->stats.recv_bundled +=
  1426. msg_msgcnt(msg);
  1427. tipc_node_unlock(n_ptr);
  1428. tipc_link_recv_bundle(buf);
  1429. continue;
  1430. case NAME_DISTRIBUTOR:
  1431. n_ptr->bclink.recv_permitted = true;
  1432. tipc_node_unlock(n_ptr);
  1433. tipc_named_recv(buf);
  1434. continue;
  1435. case BCAST_PROTOCOL:
  1436. tipc_link_recv_sync(n_ptr, buf);
  1437. tipc_node_unlock(n_ptr);
  1438. continue;
  1439. case CONN_MANAGER:
  1440. tipc_node_unlock(n_ptr);
  1441. tipc_port_recv_proto_msg(buf);
  1442. continue;
  1443. case MSG_FRAGMENTER:
  1444. l_ptr->stats.recv_fragments++;
  1445. ret = tipc_link_recv_fragment(
  1446. &l_ptr->defragm_buf,
  1447. &buf, &msg);
  1448. if (ret == 1) {
  1449. l_ptr->stats.recv_fragmented++;
  1450. goto deliver;
  1451. }
  1452. if (ret == -1)
  1453. l_ptr->next_in_no--;
  1454. break;
  1455. case CHANGEOVER_PROTOCOL:
  1456. type = msg_type(msg);
  1457. if (link_recv_changeover_msg(&l_ptr,
  1458. &buf)) {
  1459. msg = buf_msg(buf);
  1460. seq_no = msg_seqno(msg);
  1461. if (type == ORIGINAL_MSG)
  1462. goto deliver;
  1463. goto protocol_check;
  1464. }
  1465. break;
  1466. default:
  1467. kfree_skb(buf);
  1468. buf = NULL;
  1469. break;
  1470. }
  1471. tipc_node_unlock(n_ptr);
  1472. tipc_net_route_msg(buf);
  1473. continue;
  1474. }
  1475. link_handle_out_of_seq_msg(l_ptr, buf);
  1476. head = link_insert_deferred_queue(l_ptr, head);
  1477. tipc_node_unlock(n_ptr);
  1478. continue;
  1479. }
  1480. /* Link is not in state WORKING_WORKING */
  1481. if (msg_user(msg) == LINK_PROTOCOL) {
  1482. link_recv_proto_msg(l_ptr, buf);
  1483. head = link_insert_deferred_queue(l_ptr, head);
  1484. tipc_node_unlock(n_ptr);
  1485. continue;
  1486. }
  1487. /* Traffic message. Conditionally activate link */
  1488. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1489. if (link_working_working(l_ptr)) {
  1490. /* Re-insert buffer in front of queue */
  1491. buf->next = head;
  1492. head = buf;
  1493. tipc_node_unlock(n_ptr);
  1494. continue;
  1495. }
  1496. tipc_node_unlock(n_ptr);
  1497. cont:
  1498. kfree_skb(buf);
  1499. }
  1500. read_unlock_bh(&tipc_net_lock);
  1501. }
  1502. /**
  1503. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1504. *
  1505. * Returns increase in queue length (i.e. 0 or 1)
  1506. */
  1507. u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
  1508. struct sk_buff *buf)
  1509. {
  1510. struct sk_buff *queue_buf;
  1511. struct sk_buff **prev;
  1512. u32 seq_no = buf_seqno(buf);
  1513. buf->next = NULL;
  1514. /* Empty queue ? */
  1515. if (*head == NULL) {
  1516. *head = *tail = buf;
  1517. return 1;
  1518. }
  1519. /* Last ? */
  1520. if (less(buf_seqno(*tail), seq_no)) {
  1521. (*tail)->next = buf;
  1522. *tail = buf;
  1523. return 1;
  1524. }
  1525. /* Locate insertion point in queue, then insert; discard if duplicate */
  1526. prev = head;
  1527. queue_buf = *head;
  1528. for (;;) {
  1529. u32 curr_seqno = buf_seqno(queue_buf);
  1530. if (seq_no == curr_seqno) {
  1531. kfree_skb(buf);
  1532. return 0;
  1533. }
  1534. if (less(seq_no, curr_seqno))
  1535. break;
  1536. prev = &queue_buf->next;
  1537. queue_buf = queue_buf->next;
  1538. }
  1539. buf->next = queue_buf;
  1540. *prev = buf;
  1541. return 1;
  1542. }
  1543. /*
  1544. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1545. */
  1546. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1547. struct sk_buff *buf)
  1548. {
  1549. u32 seq_no = buf_seqno(buf);
  1550. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1551. link_recv_proto_msg(l_ptr, buf);
  1552. return;
  1553. }
  1554. /* Record OOS packet arrival (force mismatch on next timeout) */
  1555. l_ptr->checkpoint--;
  1556. /*
  1557. * Discard packet if a duplicate; otherwise add it to deferred queue
  1558. * and notify peer of gap as per protocol specification
  1559. */
  1560. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1561. l_ptr->stats.duplicates++;
  1562. kfree_skb(buf);
  1563. return;
  1564. }
  1565. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1566. &l_ptr->newest_deferred_in, buf)) {
  1567. l_ptr->deferred_inqueue_sz++;
  1568. l_ptr->stats.deferred_recv++;
  1569. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1570. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1571. } else
  1572. l_ptr->stats.duplicates++;
  1573. }
  1574. /*
  1575. * Send protocol message to the other endpoint.
  1576. */
  1577. void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
  1578. int probe_msg, u32 gap, u32 tolerance,
  1579. u32 priority, u32 ack_mtu)
  1580. {
  1581. struct sk_buff *buf = NULL;
  1582. struct tipc_msg *msg = l_ptr->pmsg;
  1583. u32 msg_size = sizeof(l_ptr->proto_msg);
  1584. int r_flag;
  1585. /* Discard any previous message that was deferred due to congestion */
  1586. if (l_ptr->proto_msg_queue) {
  1587. kfree_skb(l_ptr->proto_msg_queue);
  1588. l_ptr->proto_msg_queue = NULL;
  1589. }
  1590. if (link_blocked(l_ptr))
  1591. return;
  1592. /* Abort non-RESET send if communication with node is prohibited */
  1593. if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
  1594. return;
  1595. /* Create protocol message with "out-of-sequence" sequence number */
  1596. msg_set_type(msg, msg_typ);
  1597. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1598. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1599. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1600. if (msg_typ == STATE_MSG) {
  1601. u32 next_sent = mod(l_ptr->next_out_no);
  1602. if (!tipc_link_is_up(l_ptr))
  1603. return;
  1604. if (l_ptr->next_out)
  1605. next_sent = buf_seqno(l_ptr->next_out);
  1606. msg_set_next_sent(msg, next_sent);
  1607. if (l_ptr->oldest_deferred_in) {
  1608. u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
  1609. gap = mod(rec - mod(l_ptr->next_in_no));
  1610. }
  1611. msg_set_seq_gap(msg, gap);
  1612. if (gap)
  1613. l_ptr->stats.sent_nacks++;
  1614. msg_set_link_tolerance(msg, tolerance);
  1615. msg_set_linkprio(msg, priority);
  1616. msg_set_max_pkt(msg, ack_mtu);
  1617. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1618. msg_set_probe(msg, probe_msg != 0);
  1619. if (probe_msg) {
  1620. u32 mtu = l_ptr->max_pkt;
  1621. if ((mtu < l_ptr->max_pkt_target) &&
  1622. link_working_working(l_ptr) &&
  1623. l_ptr->fsm_msg_cnt) {
  1624. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1625. if (l_ptr->max_pkt_probes == 10) {
  1626. l_ptr->max_pkt_target = (msg_size - 4);
  1627. l_ptr->max_pkt_probes = 0;
  1628. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1629. }
  1630. l_ptr->max_pkt_probes++;
  1631. }
  1632. l_ptr->stats.sent_probes++;
  1633. }
  1634. l_ptr->stats.sent_states++;
  1635. } else { /* RESET_MSG or ACTIVATE_MSG */
  1636. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1637. msg_set_seq_gap(msg, 0);
  1638. msg_set_next_sent(msg, 1);
  1639. msg_set_probe(msg, 0);
  1640. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1641. msg_set_linkprio(msg, l_ptr->priority);
  1642. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1643. }
  1644. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1645. msg_set_redundant_link(msg, r_flag);
  1646. msg_set_linkprio(msg, l_ptr->priority);
  1647. msg_set_size(msg, msg_size);
  1648. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1649. buf = tipc_buf_acquire(msg_size);
  1650. if (!buf)
  1651. return;
  1652. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1653. buf->priority = TC_PRIO_CONTROL;
  1654. /* Defer message if bearer is already blocked */
  1655. if (tipc_bearer_blocked(l_ptr->b_ptr)) {
  1656. l_ptr->proto_msg_queue = buf;
  1657. return;
  1658. }
  1659. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1660. l_ptr->unacked_window = 0;
  1661. kfree_skb(buf);
  1662. }
  1663. /*
  1664. * Receive protocol message :
  1665. * Note that network plane id propagates through the network, and may
  1666. * change at any time. The node with lowest address rules
  1667. */
  1668. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
  1669. {
  1670. u32 rec_gap = 0;
  1671. u32 max_pkt_info;
  1672. u32 max_pkt_ack;
  1673. u32 msg_tol;
  1674. struct tipc_msg *msg = buf_msg(buf);
  1675. if (link_blocked(l_ptr))
  1676. goto exit;
  1677. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1678. l_ptr->checkpoint--;
  1679. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1680. if (tipc_own_addr > msg_prevnode(msg))
  1681. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1682. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1683. switch (msg_type(msg)) {
  1684. case RESET_MSG:
  1685. if (!link_working_unknown(l_ptr) &&
  1686. (l_ptr->peer_session != INVALID_SESSION)) {
  1687. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1688. break; /* duplicate or old reset: ignore */
  1689. }
  1690. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1691. link_working_unknown(l_ptr))) {
  1692. /*
  1693. * peer has lost contact -- don't allow peer's links
  1694. * to reactivate before we recognize loss & clean up
  1695. */
  1696. l_ptr->owner->block_setup = WAIT_NODE_DOWN;
  1697. }
  1698. link_state_event(l_ptr, RESET_MSG);
  1699. /* fall thru' */
  1700. case ACTIVATE_MSG:
  1701. /* Update link settings according other endpoint's values */
  1702. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1703. msg_tol = msg_link_tolerance(msg);
  1704. if (msg_tol > l_ptr->tolerance)
  1705. link_set_supervision_props(l_ptr, msg_tol);
  1706. if (msg_linkprio(msg) > l_ptr->priority)
  1707. l_ptr->priority = msg_linkprio(msg);
  1708. max_pkt_info = msg_max_pkt(msg);
  1709. if (max_pkt_info) {
  1710. if (max_pkt_info < l_ptr->max_pkt_target)
  1711. l_ptr->max_pkt_target = max_pkt_info;
  1712. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1713. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1714. } else {
  1715. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1716. }
  1717. /* Synchronize broadcast link info, if not done previously */
  1718. if (!tipc_node_is_up(l_ptr->owner)) {
  1719. l_ptr->owner->bclink.last_sent =
  1720. l_ptr->owner->bclink.last_in =
  1721. msg_last_bcast(msg);
  1722. l_ptr->owner->bclink.oos_state = 0;
  1723. }
  1724. l_ptr->peer_session = msg_session(msg);
  1725. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1726. if (msg_type(msg) == ACTIVATE_MSG)
  1727. link_state_event(l_ptr, ACTIVATE_MSG);
  1728. break;
  1729. case STATE_MSG:
  1730. msg_tol = msg_link_tolerance(msg);
  1731. if (msg_tol)
  1732. link_set_supervision_props(l_ptr, msg_tol);
  1733. if (msg_linkprio(msg) &&
  1734. (msg_linkprio(msg) != l_ptr->priority)) {
  1735. pr_warn("%s<%s>, priority change %u->%u\n",
  1736. link_rst_msg, l_ptr->name, l_ptr->priority,
  1737. msg_linkprio(msg));
  1738. l_ptr->priority = msg_linkprio(msg);
  1739. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1740. break;
  1741. }
  1742. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1743. l_ptr->stats.recv_states++;
  1744. if (link_reset_unknown(l_ptr))
  1745. break;
  1746. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1747. rec_gap = mod(msg_next_sent(msg) -
  1748. mod(l_ptr->next_in_no));
  1749. }
  1750. max_pkt_ack = msg_max_pkt(msg);
  1751. if (max_pkt_ack > l_ptr->max_pkt) {
  1752. l_ptr->max_pkt = max_pkt_ack;
  1753. l_ptr->max_pkt_probes = 0;
  1754. }
  1755. max_pkt_ack = 0;
  1756. if (msg_probe(msg)) {
  1757. l_ptr->stats.recv_probes++;
  1758. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1759. max_pkt_ack = msg_size(msg);
  1760. }
  1761. /* Protocol message before retransmits, reduce loss risk */
  1762. if (l_ptr->owner->bclink.recv_permitted)
  1763. tipc_bclink_update_link_state(l_ptr->owner,
  1764. msg_last_bcast(msg));
  1765. if (rec_gap || (msg_probe(msg))) {
  1766. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1767. 0, rec_gap, 0, 0, max_pkt_ack);
  1768. }
  1769. if (msg_seq_gap(msg)) {
  1770. l_ptr->stats.recv_nacks++;
  1771. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1772. msg_seq_gap(msg));
  1773. }
  1774. break;
  1775. }
  1776. exit:
  1777. kfree_skb(buf);
  1778. }
  1779. /*
  1780. * tipc_link_tunnel(): Send one message via a link belonging to
  1781. * another bearer. Owner node is locked.
  1782. */
  1783. static void tipc_link_tunnel(struct tipc_link *l_ptr,
  1784. struct tipc_msg *tunnel_hdr, struct tipc_msg *msg,
  1785. u32 selector)
  1786. {
  1787. struct tipc_link *tunnel;
  1788. struct sk_buff *buf;
  1789. u32 length = msg_size(msg);
  1790. tunnel = l_ptr->owner->active_links[selector & 1];
  1791. if (!tipc_link_is_up(tunnel)) {
  1792. pr_warn("%stunnel link no longer available\n", link_co_err);
  1793. return;
  1794. }
  1795. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1796. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1797. if (!buf) {
  1798. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1799. return;
  1800. }
  1801. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1802. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1803. tipc_link_send_buf(tunnel, buf);
  1804. }
  1805. /*
  1806. * changeover(): Send whole message queue via the remaining link
  1807. * Owner node is locked.
  1808. */
  1809. void tipc_link_changeover(struct tipc_link *l_ptr)
  1810. {
  1811. u32 msgcount = l_ptr->out_queue_size;
  1812. struct sk_buff *crs = l_ptr->first_out;
  1813. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1814. struct tipc_msg tunnel_hdr;
  1815. int split_bundles;
  1816. if (!tunnel)
  1817. return;
  1818. if (!l_ptr->owner->permit_changeover) {
  1819. pr_warn("%speer did not permit changeover\n", link_co_err);
  1820. return;
  1821. }
  1822. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1823. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1824. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1825. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1826. if (!l_ptr->first_out) {
  1827. struct sk_buff *buf;
  1828. buf = tipc_buf_acquire(INT_H_SIZE);
  1829. if (buf) {
  1830. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1831. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1832. tipc_link_send_buf(tunnel, buf);
  1833. } else {
  1834. pr_warn("%sunable to send changeover msg\n",
  1835. link_co_err);
  1836. }
  1837. return;
  1838. }
  1839. split_bundles = (l_ptr->owner->active_links[0] !=
  1840. l_ptr->owner->active_links[1]);
  1841. while (crs) {
  1842. struct tipc_msg *msg = buf_msg(crs);
  1843. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1844. struct tipc_msg *m = msg_get_wrapped(msg);
  1845. unchar *pos = (unchar *)m;
  1846. msgcount = msg_msgcnt(msg);
  1847. while (msgcount--) {
  1848. msg_set_seqno(m, msg_seqno(msg));
  1849. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  1850. msg_link_selector(m));
  1851. pos += align(msg_size(m));
  1852. m = (struct tipc_msg *)pos;
  1853. }
  1854. } else {
  1855. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  1856. msg_link_selector(msg));
  1857. }
  1858. crs = crs->next;
  1859. }
  1860. }
  1861. void tipc_link_send_duplicate(struct tipc_link *l_ptr, struct tipc_link *tunnel)
  1862. {
  1863. struct sk_buff *iter;
  1864. struct tipc_msg tunnel_hdr;
  1865. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1866. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1867. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1868. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1869. iter = l_ptr->first_out;
  1870. while (iter) {
  1871. struct sk_buff *outbuf;
  1872. struct tipc_msg *msg = buf_msg(iter);
  1873. u32 length = msg_size(msg);
  1874. if (msg_user(msg) == MSG_BUNDLER)
  1875. msg_set_type(msg, CLOSED_MSG);
  1876. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1877. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1878. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1879. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1880. if (outbuf == NULL) {
  1881. pr_warn("%sunable to send duplicate msg\n",
  1882. link_co_err);
  1883. return;
  1884. }
  1885. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1886. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1887. length);
  1888. tipc_link_send_buf(tunnel, outbuf);
  1889. if (!tipc_link_is_up(l_ptr))
  1890. return;
  1891. iter = iter->next;
  1892. }
  1893. }
  1894. /**
  1895. * buf_extract - extracts embedded TIPC message from another message
  1896. * @skb: encapsulating message buffer
  1897. * @from_pos: offset to extract from
  1898. *
  1899. * Returns a new message buffer containing an embedded message. The
  1900. * encapsulating message itself is left unchanged.
  1901. */
  1902. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1903. {
  1904. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1905. u32 size = msg_size(msg);
  1906. struct sk_buff *eb;
  1907. eb = tipc_buf_acquire(size);
  1908. if (eb)
  1909. skb_copy_to_linear_data(eb, msg, size);
  1910. return eb;
  1911. }
  1912. /*
  1913. * link_recv_changeover_msg(): Receive tunneled packet sent
  1914. * via other link. Node is locked. Return extracted buffer.
  1915. */
  1916. static int link_recv_changeover_msg(struct tipc_link **l_ptr,
  1917. struct sk_buff **buf)
  1918. {
  1919. struct sk_buff *tunnel_buf = *buf;
  1920. struct tipc_link *dest_link;
  1921. struct tipc_msg *msg;
  1922. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  1923. u32 msg_typ = msg_type(tunnel_msg);
  1924. u32 msg_count = msg_msgcnt(tunnel_msg);
  1925. u32 bearer_id = msg_bearer_id(tunnel_msg);
  1926. if (bearer_id >= MAX_BEARERS)
  1927. goto exit;
  1928. dest_link = (*l_ptr)->owner->links[bearer_id];
  1929. if (!dest_link)
  1930. goto exit;
  1931. if (dest_link == *l_ptr) {
  1932. pr_err("Unexpected changeover message on link <%s>\n",
  1933. (*l_ptr)->name);
  1934. goto exit;
  1935. }
  1936. *l_ptr = dest_link;
  1937. msg = msg_get_wrapped(tunnel_msg);
  1938. if (msg_typ == DUPLICATE_MSG) {
  1939. if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
  1940. goto exit;
  1941. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1942. if (*buf == NULL) {
  1943. pr_warn("%sduplicate msg dropped\n", link_co_err);
  1944. goto exit;
  1945. }
  1946. kfree_skb(tunnel_buf);
  1947. return 1;
  1948. }
  1949. /* First original message ?: */
  1950. if (tipc_link_is_up(dest_link)) {
  1951. pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
  1952. dest_link->name);
  1953. tipc_link_reset(dest_link);
  1954. dest_link->exp_msg_count = msg_count;
  1955. if (!msg_count)
  1956. goto exit;
  1957. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  1958. dest_link->exp_msg_count = msg_count;
  1959. if (!msg_count)
  1960. goto exit;
  1961. }
  1962. /* Receive original message */
  1963. if (dest_link->exp_msg_count == 0) {
  1964. pr_warn("%sgot too many tunnelled messages\n", link_co_err);
  1965. goto exit;
  1966. }
  1967. dest_link->exp_msg_count--;
  1968. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  1969. goto exit;
  1970. } else {
  1971. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1972. if (*buf != NULL) {
  1973. kfree_skb(tunnel_buf);
  1974. return 1;
  1975. } else {
  1976. pr_warn("%soriginal msg dropped\n", link_co_err);
  1977. }
  1978. }
  1979. exit:
  1980. *buf = NULL;
  1981. kfree_skb(tunnel_buf);
  1982. return 0;
  1983. }
  1984. /*
  1985. * Bundler functionality:
  1986. */
  1987. void tipc_link_recv_bundle(struct sk_buff *buf)
  1988. {
  1989. u32 msgcount = msg_msgcnt(buf_msg(buf));
  1990. u32 pos = INT_H_SIZE;
  1991. struct sk_buff *obuf;
  1992. while (msgcount--) {
  1993. obuf = buf_extract(buf, pos);
  1994. if (obuf == NULL) {
  1995. pr_warn("Link unable to unbundle message(s)\n");
  1996. break;
  1997. }
  1998. pos += align(msg_size(buf_msg(obuf)));
  1999. tipc_net_route_msg(obuf);
  2000. }
  2001. kfree_skb(buf);
  2002. }
  2003. /*
  2004. * Fragmentation/defragmentation:
  2005. */
  2006. /*
  2007. * link_send_long_buf: Entry for buffers needing fragmentation.
  2008. * The buffer is complete, inclusive total message length.
  2009. * Returns user data length.
  2010. */
  2011. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  2012. {
  2013. struct sk_buff *buf_chain = NULL;
  2014. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  2015. struct tipc_msg *inmsg = buf_msg(buf);
  2016. struct tipc_msg fragm_hdr;
  2017. u32 insize = msg_size(inmsg);
  2018. u32 dsz = msg_data_sz(inmsg);
  2019. unchar *crs = buf->data;
  2020. u32 rest = insize;
  2021. u32 pack_sz = l_ptr->max_pkt;
  2022. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2023. u32 fragm_no = 0;
  2024. u32 destaddr;
  2025. if (msg_short(inmsg))
  2026. destaddr = l_ptr->addr;
  2027. else
  2028. destaddr = msg_destnode(inmsg);
  2029. /* Prepare reusable fragment header: */
  2030. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2031. INT_H_SIZE, destaddr);
  2032. /* Chop up message: */
  2033. while (rest > 0) {
  2034. struct sk_buff *fragm;
  2035. if (rest <= fragm_sz) {
  2036. fragm_sz = rest;
  2037. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2038. }
  2039. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  2040. if (fragm == NULL) {
  2041. kfree_skb(buf);
  2042. while (buf_chain) {
  2043. buf = buf_chain;
  2044. buf_chain = buf_chain->next;
  2045. kfree_skb(buf);
  2046. }
  2047. return -ENOMEM;
  2048. }
  2049. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2050. fragm_no++;
  2051. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2052. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2053. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2054. fragm_sz);
  2055. buf_chain_tail->next = fragm;
  2056. buf_chain_tail = fragm;
  2057. rest -= fragm_sz;
  2058. crs += fragm_sz;
  2059. msg_set_type(&fragm_hdr, FRAGMENT);
  2060. }
  2061. kfree_skb(buf);
  2062. /* Append chain of fragments to send queue & send them */
  2063. l_ptr->long_msg_seq_no++;
  2064. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  2065. l_ptr->stats.sent_fragments += fragm_no;
  2066. l_ptr->stats.sent_fragmented++;
  2067. tipc_link_push_queue(l_ptr);
  2068. return dsz;
  2069. }
  2070. /*
  2071. * A pending message being re-assembled must store certain values
  2072. * to handle subsequent fragments correctly. The following functions
  2073. * help storing these values in unused, available fields in the
  2074. * pending message. This makes dynamic memory allocation unnecessary.
  2075. */
  2076. static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
  2077. {
  2078. msg_set_seqno(buf_msg(buf), seqno);
  2079. }
  2080. static u32 get_fragm_size(struct sk_buff *buf)
  2081. {
  2082. return msg_ack(buf_msg(buf));
  2083. }
  2084. static void set_fragm_size(struct sk_buff *buf, u32 sz)
  2085. {
  2086. msg_set_ack(buf_msg(buf), sz);
  2087. }
  2088. static u32 get_expected_frags(struct sk_buff *buf)
  2089. {
  2090. return msg_bcast_ack(buf_msg(buf));
  2091. }
  2092. static void set_expected_frags(struct sk_buff *buf, u32 exp)
  2093. {
  2094. msg_set_bcast_ack(buf_msg(buf), exp);
  2095. }
  2096. /*
  2097. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2098. * the reassembled buffer if message is complete.
  2099. */
  2100. int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
  2101. struct tipc_msg **m)
  2102. {
  2103. struct sk_buff *prev = NULL;
  2104. struct sk_buff *fbuf = *fb;
  2105. struct tipc_msg *fragm = buf_msg(fbuf);
  2106. struct sk_buff *pbuf = *pending;
  2107. u32 long_msg_seq_no = msg_long_msgno(fragm);
  2108. *fb = NULL;
  2109. /* Is there an incomplete message waiting for this fragment? */
  2110. while (pbuf && ((buf_seqno(pbuf) != long_msg_seq_no) ||
  2111. (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
  2112. prev = pbuf;
  2113. pbuf = pbuf->next;
  2114. }
  2115. if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
  2116. struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
  2117. u32 msg_sz = msg_size(imsg);
  2118. u32 fragm_sz = msg_data_sz(fragm);
  2119. u32 exp_fragm_cnt;
  2120. u32 max = TIPC_MAX_USER_MSG_SIZE + NAMED_H_SIZE;
  2121. if (msg_type(imsg) == TIPC_MCAST_MSG)
  2122. max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
  2123. if (fragm_sz == 0 || msg_size(imsg) > max) {
  2124. kfree_skb(fbuf);
  2125. return 0;
  2126. }
  2127. exp_fragm_cnt = msg_sz / fragm_sz + !!(msg_sz % fragm_sz);
  2128. pbuf = tipc_buf_acquire(msg_size(imsg));
  2129. if (pbuf != NULL) {
  2130. pbuf->next = *pending;
  2131. *pending = pbuf;
  2132. skb_copy_to_linear_data(pbuf, imsg,
  2133. msg_data_sz(fragm));
  2134. /* Prepare buffer for subsequent fragments. */
  2135. set_long_msg_seqno(pbuf, long_msg_seq_no);
  2136. set_fragm_size(pbuf, fragm_sz);
  2137. set_expected_frags(pbuf, exp_fragm_cnt - 1);
  2138. } else {
  2139. pr_debug("Link unable to reassemble fragmented message\n");
  2140. kfree_skb(fbuf);
  2141. return -1;
  2142. }
  2143. kfree_skb(fbuf);
  2144. return 0;
  2145. } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
  2146. u32 dsz = msg_data_sz(fragm);
  2147. u32 fsz = get_fragm_size(pbuf);
  2148. u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
  2149. u32 exp_frags = get_expected_frags(pbuf) - 1;
  2150. skb_copy_to_linear_data_offset(pbuf, crs,
  2151. msg_data(fragm), dsz);
  2152. kfree_skb(fbuf);
  2153. /* Is message complete? */
  2154. if (exp_frags == 0) {
  2155. if (prev)
  2156. prev->next = pbuf->next;
  2157. else
  2158. *pending = pbuf->next;
  2159. msg_reset_reroute_cnt(buf_msg(pbuf));
  2160. *fb = pbuf;
  2161. *m = buf_msg(pbuf);
  2162. return 1;
  2163. }
  2164. set_expected_frags(pbuf, exp_frags);
  2165. return 0;
  2166. }
  2167. kfree_skb(fbuf);
  2168. return 0;
  2169. }
  2170. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
  2171. {
  2172. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  2173. return;
  2174. l_ptr->tolerance = tolerance;
  2175. l_ptr->continuity_interval =
  2176. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2177. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2178. }
  2179. void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
  2180. {
  2181. /* Data messages from this node, inclusive FIRST_FRAGM */
  2182. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2183. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2184. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2185. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2186. /* Transiting data messages,inclusive FIRST_FRAGM */
  2187. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2188. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2189. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2190. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2191. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2192. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2193. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2194. /* FRAGMENT and LAST_FRAGMENT packets */
  2195. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2196. }
  2197. /**
  2198. * link_find_link - locate link by name
  2199. * @name: ptr to link name string
  2200. * @node: ptr to area to be filled with ptr to associated node
  2201. *
  2202. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2203. * this also prevents link deletion.
  2204. *
  2205. * Returns pointer to link (or 0 if invalid link name).
  2206. */
  2207. static struct tipc_link *link_find_link(const char *name,
  2208. struct tipc_node **node)
  2209. {
  2210. struct tipc_link *l_ptr;
  2211. struct tipc_node *n_ptr;
  2212. int i;
  2213. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  2214. for (i = 0; i < MAX_BEARERS; i++) {
  2215. l_ptr = n_ptr->links[i];
  2216. if (l_ptr && !strcmp(l_ptr->name, name))
  2217. goto found;
  2218. }
  2219. }
  2220. l_ptr = NULL;
  2221. n_ptr = NULL;
  2222. found:
  2223. *node = n_ptr;
  2224. return l_ptr;
  2225. }
  2226. /**
  2227. * link_value_is_valid -- validate proposed link tolerance/priority/window
  2228. *
  2229. * @cmd: value type (TIPC_CMD_SET_LINK_*)
  2230. * @new_value: the new value
  2231. *
  2232. * Returns 1 if value is within range, 0 if not.
  2233. */
  2234. static int link_value_is_valid(u16 cmd, u32 new_value)
  2235. {
  2236. switch (cmd) {
  2237. case TIPC_CMD_SET_LINK_TOL:
  2238. return (new_value >= TIPC_MIN_LINK_TOL) &&
  2239. (new_value <= TIPC_MAX_LINK_TOL);
  2240. case TIPC_CMD_SET_LINK_PRI:
  2241. return (new_value <= TIPC_MAX_LINK_PRI);
  2242. case TIPC_CMD_SET_LINK_WINDOW:
  2243. return (new_value >= TIPC_MIN_LINK_WIN) &&
  2244. (new_value <= TIPC_MAX_LINK_WIN);
  2245. }
  2246. return 0;
  2247. }
  2248. /**
  2249. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  2250. * @name: ptr to link, bearer, or media name
  2251. * @new_value: new value of link, bearer, or media setting
  2252. * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  2253. *
  2254. * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
  2255. *
  2256. * Returns 0 if value updated and negative value on error.
  2257. */
  2258. static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
  2259. {
  2260. struct tipc_node *node;
  2261. struct tipc_link *l_ptr;
  2262. struct tipc_bearer *b_ptr;
  2263. struct tipc_media *m_ptr;
  2264. int res = 0;
  2265. l_ptr = link_find_link(name, &node);
  2266. if (l_ptr) {
  2267. /*
  2268. * acquire node lock for tipc_link_send_proto_msg().
  2269. * see "TIPC locking policy" in net.c.
  2270. */
  2271. tipc_node_lock(node);
  2272. switch (cmd) {
  2273. case TIPC_CMD_SET_LINK_TOL:
  2274. link_set_supervision_props(l_ptr, new_value);
  2275. tipc_link_send_proto_msg(l_ptr,
  2276. STATE_MSG, 0, 0, new_value, 0, 0);
  2277. break;
  2278. case TIPC_CMD_SET_LINK_PRI:
  2279. l_ptr->priority = new_value;
  2280. tipc_link_send_proto_msg(l_ptr,
  2281. STATE_MSG, 0, 0, 0, new_value, 0);
  2282. break;
  2283. case TIPC_CMD_SET_LINK_WINDOW:
  2284. tipc_link_set_queue_limits(l_ptr, new_value);
  2285. break;
  2286. default:
  2287. res = -EINVAL;
  2288. break;
  2289. }
  2290. tipc_node_unlock(node);
  2291. return res;
  2292. }
  2293. b_ptr = tipc_bearer_find(name);
  2294. if (b_ptr) {
  2295. switch (cmd) {
  2296. case TIPC_CMD_SET_LINK_TOL:
  2297. b_ptr->tolerance = new_value;
  2298. break;
  2299. case TIPC_CMD_SET_LINK_PRI:
  2300. b_ptr->priority = new_value;
  2301. break;
  2302. case TIPC_CMD_SET_LINK_WINDOW:
  2303. b_ptr->window = new_value;
  2304. break;
  2305. default:
  2306. res = -EINVAL;
  2307. break;
  2308. }
  2309. return res;
  2310. }
  2311. m_ptr = tipc_media_find(name);
  2312. if (!m_ptr)
  2313. return -ENODEV;
  2314. switch (cmd) {
  2315. case TIPC_CMD_SET_LINK_TOL:
  2316. m_ptr->tolerance = new_value;
  2317. break;
  2318. case TIPC_CMD_SET_LINK_PRI:
  2319. m_ptr->priority = new_value;
  2320. break;
  2321. case TIPC_CMD_SET_LINK_WINDOW:
  2322. m_ptr->window = new_value;
  2323. break;
  2324. default:
  2325. res = -EINVAL;
  2326. break;
  2327. }
  2328. return res;
  2329. }
  2330. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2331. u16 cmd)
  2332. {
  2333. struct tipc_link_config *args;
  2334. u32 new_value;
  2335. int res;
  2336. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2337. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2338. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2339. new_value = ntohl(args->value);
  2340. if (!link_value_is_valid(cmd, new_value))
  2341. return tipc_cfg_reply_error_string(
  2342. "cannot change, value invalid");
  2343. if (!strcmp(args->name, tipc_bclink_name)) {
  2344. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2345. (tipc_bclink_set_queue_limits(new_value) == 0))
  2346. return tipc_cfg_reply_none();
  2347. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2348. " (cannot change setting on broadcast link)");
  2349. }
  2350. read_lock_bh(&tipc_net_lock);
  2351. res = link_cmd_set_value(args->name, new_value, cmd);
  2352. read_unlock_bh(&tipc_net_lock);
  2353. if (res)
  2354. return tipc_cfg_reply_error_string("cannot change link setting");
  2355. return tipc_cfg_reply_none();
  2356. }
  2357. /**
  2358. * link_reset_statistics - reset link statistics
  2359. * @l_ptr: pointer to link
  2360. */
  2361. static void link_reset_statistics(struct tipc_link *l_ptr)
  2362. {
  2363. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2364. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2365. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2366. }
  2367. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2368. {
  2369. char *link_name;
  2370. struct tipc_link *l_ptr;
  2371. struct tipc_node *node;
  2372. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2373. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2374. link_name = (char *)TLV_DATA(req_tlv_area);
  2375. if (!strcmp(link_name, tipc_bclink_name)) {
  2376. if (tipc_bclink_reset_stats())
  2377. return tipc_cfg_reply_error_string("link not found");
  2378. return tipc_cfg_reply_none();
  2379. }
  2380. read_lock_bh(&tipc_net_lock);
  2381. l_ptr = link_find_link(link_name, &node);
  2382. if (!l_ptr) {
  2383. read_unlock_bh(&tipc_net_lock);
  2384. return tipc_cfg_reply_error_string("link not found");
  2385. }
  2386. tipc_node_lock(node);
  2387. link_reset_statistics(l_ptr);
  2388. tipc_node_unlock(node);
  2389. read_unlock_bh(&tipc_net_lock);
  2390. return tipc_cfg_reply_none();
  2391. }
  2392. /**
  2393. * percent - convert count to a percentage of total (rounding up or down)
  2394. */
  2395. static u32 percent(u32 count, u32 total)
  2396. {
  2397. return (count * 100 + (total / 2)) / total;
  2398. }
  2399. /**
  2400. * tipc_link_stats - print link statistics
  2401. * @name: link name
  2402. * @buf: print buffer area
  2403. * @buf_size: size of print buffer area
  2404. *
  2405. * Returns length of print buffer data string (or 0 if error)
  2406. */
  2407. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2408. {
  2409. struct tipc_link *l;
  2410. struct tipc_stats *s;
  2411. struct tipc_node *node;
  2412. char *status;
  2413. u32 profile_total = 0;
  2414. int ret;
  2415. if (!strcmp(name, tipc_bclink_name))
  2416. return tipc_bclink_stats(buf, buf_size);
  2417. read_lock_bh(&tipc_net_lock);
  2418. l = link_find_link(name, &node);
  2419. if (!l) {
  2420. read_unlock_bh(&tipc_net_lock);
  2421. return 0;
  2422. }
  2423. tipc_node_lock(node);
  2424. s = &l->stats;
  2425. if (tipc_link_is_active(l))
  2426. status = "ACTIVE";
  2427. else if (tipc_link_is_up(l))
  2428. status = "STANDBY";
  2429. else
  2430. status = "DEFUNCT";
  2431. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  2432. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2433. " Window:%u packets\n",
  2434. l->name, status, l->max_pkt, l->priority,
  2435. l->tolerance, l->queue_limit[0]);
  2436. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2437. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2438. l->next_in_no - s->recv_info, s->recv_fragments,
  2439. s->recv_fragmented, s->recv_bundles,
  2440. s->recv_bundled);
  2441. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2442. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2443. l->next_out_no - s->sent_info, s->sent_fragments,
  2444. s->sent_fragmented, s->sent_bundles,
  2445. s->sent_bundled);
  2446. profile_total = s->msg_length_counts;
  2447. if (!profile_total)
  2448. profile_total = 1;
  2449. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2450. " TX profile sample:%u packets average:%u octets\n"
  2451. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2452. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2453. s->msg_length_counts,
  2454. s->msg_lengths_total / profile_total,
  2455. percent(s->msg_length_profile[0], profile_total),
  2456. percent(s->msg_length_profile[1], profile_total),
  2457. percent(s->msg_length_profile[2], profile_total),
  2458. percent(s->msg_length_profile[3], profile_total),
  2459. percent(s->msg_length_profile[4], profile_total),
  2460. percent(s->msg_length_profile[5], profile_total),
  2461. percent(s->msg_length_profile[6], profile_total));
  2462. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2463. " RX states:%u probes:%u naks:%u defs:%u"
  2464. " dups:%u\n", s->recv_states, s->recv_probes,
  2465. s->recv_nacks, s->deferred_recv, s->duplicates);
  2466. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2467. " TX states:%u probes:%u naks:%u acks:%u"
  2468. " dups:%u\n", s->sent_states, s->sent_probes,
  2469. s->sent_nacks, s->sent_acks, s->retransmitted);
  2470. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2471. " Congestion link:%u Send queue"
  2472. " max:%u avg:%u\n", s->link_congs,
  2473. s->max_queue_sz, s->queue_sz_counts ?
  2474. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  2475. tipc_node_unlock(node);
  2476. read_unlock_bh(&tipc_net_lock);
  2477. return ret;
  2478. }
  2479. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2480. {
  2481. struct sk_buff *buf;
  2482. struct tlv_desc *rep_tlv;
  2483. int str_len;
  2484. int pb_len;
  2485. char *pb;
  2486. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2487. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2488. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  2489. if (!buf)
  2490. return NULL;
  2491. rep_tlv = (struct tlv_desc *)buf->data;
  2492. pb = TLV_DATA(rep_tlv);
  2493. pb_len = ULTRA_STRING_MAX_LEN;
  2494. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2495. pb, pb_len);
  2496. if (!str_len) {
  2497. kfree_skb(buf);
  2498. return tipc_cfg_reply_error_string("link not found");
  2499. }
  2500. str_len += 1; /* for "\0" */
  2501. skb_put(buf, TLV_SPACE(str_len));
  2502. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2503. return buf;
  2504. }
  2505. /**
  2506. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2507. * @dest: network address of destination node
  2508. * @selector: used to select from set of active links
  2509. *
  2510. * If no active link can be found, uses default maximum packet size.
  2511. */
  2512. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2513. {
  2514. struct tipc_node *n_ptr;
  2515. struct tipc_link *l_ptr;
  2516. u32 res = MAX_PKT_DEFAULT;
  2517. if (dest == tipc_own_addr)
  2518. return MAX_MSG_SIZE;
  2519. read_lock_bh(&tipc_net_lock);
  2520. n_ptr = tipc_node_find(dest);
  2521. if (n_ptr) {
  2522. tipc_node_lock(n_ptr);
  2523. l_ptr = n_ptr->active_links[selector & 1];
  2524. if (l_ptr)
  2525. res = l_ptr->max_pkt;
  2526. tipc_node_unlock(n_ptr);
  2527. }
  2528. read_unlock_bh(&tipc_net_lock);
  2529. return res;
  2530. }
  2531. static void link_print(struct tipc_link *l_ptr, const char *str)
  2532. {
  2533. pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
  2534. if (link_working_unknown(l_ptr))
  2535. pr_cont(":WU\n");
  2536. else if (link_reset_reset(l_ptr))
  2537. pr_cont(":RR\n");
  2538. else if (link_reset_unknown(l_ptr))
  2539. pr_cont(":RU\n");
  2540. else if (link_working_working(l_ptr))
  2541. pr_cont(":WW\n");
  2542. else
  2543. pr_cont("\n");
  2544. }