link.c 63 KB

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