link.c 65 KB

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