link.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391
  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. tipc_node_put(node);
  781. }
  782. if (link)
  783. return rc;
  784. if (likely(in_own_node(net, dnode))) {
  785. tipc_sk_rcv(net, list);
  786. return 0;
  787. }
  788. __skb_queue_purge(list);
  789. return rc;
  790. }
  791. /*
  792. * tipc_link_sync_xmit - synchronize broadcast link endpoints.
  793. *
  794. * Give a newly added peer node the sequence number where it should
  795. * start receiving and acking broadcast packets.
  796. *
  797. * Called with node locked
  798. */
  799. static void tipc_link_sync_xmit(struct tipc_link *link)
  800. {
  801. struct sk_buff *skb;
  802. struct tipc_msg *msg;
  803. skb = tipc_buf_acquire(INT_H_SIZE);
  804. if (!skb)
  805. return;
  806. msg = buf_msg(skb);
  807. tipc_msg_init(link_own_addr(link), msg, BCAST_PROTOCOL, STATE_MSG,
  808. INT_H_SIZE, link->addr);
  809. msg_set_last_bcast(msg, link->owner->bclink.acked);
  810. __tipc_link_xmit_skb(link, skb);
  811. }
  812. /*
  813. * tipc_link_sync_rcv - synchronize broadcast link endpoints.
  814. * Receive the sequence number where we should start receiving and
  815. * acking broadcast packets from a newly added peer node, and open
  816. * up for reception of such packets.
  817. *
  818. * Called with node locked
  819. */
  820. static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
  821. {
  822. struct tipc_msg *msg = buf_msg(buf);
  823. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  824. n->bclink.recv_permitted = true;
  825. kfree_skb(buf);
  826. }
  827. /*
  828. * tipc_link_push_packets - push unsent packets to bearer
  829. *
  830. * Push out the unsent messages of a link where congestion
  831. * has abated. Node is locked.
  832. *
  833. * Called with node locked
  834. */
  835. void tipc_link_push_packets(struct tipc_link *link)
  836. {
  837. struct sk_buff *skb;
  838. struct tipc_msg *msg;
  839. unsigned int ack = mod(link->next_in_no - 1);
  840. while (skb_queue_len(&link->transmq) < link->window) {
  841. skb = __skb_dequeue(&link->backlogq);
  842. if (!skb)
  843. break;
  844. msg = buf_msg(skb);
  845. link->backlog[msg_importance(msg)].len--;
  846. msg_set_ack(msg, ack);
  847. msg_set_bcast_ack(msg, link->owner->bclink.last_in);
  848. link->rcv_unacked = 0;
  849. __skb_queue_tail(&link->transmq, skb);
  850. tipc_bearer_send(link->owner->net, link->bearer_id,
  851. skb, &link->media_addr);
  852. }
  853. }
  854. void tipc_link_reset_all(struct tipc_node *node)
  855. {
  856. char addr_string[16];
  857. u32 i;
  858. tipc_node_lock(node);
  859. pr_warn("Resetting all links to %s\n",
  860. tipc_addr_string_fill(addr_string, node->addr));
  861. for (i = 0; i < MAX_BEARERS; i++) {
  862. if (node->links[i]) {
  863. link_print(node->links[i], "Resetting link\n");
  864. tipc_link_reset(node->links[i]);
  865. }
  866. }
  867. tipc_node_unlock(node);
  868. }
  869. static void link_retransmit_failure(struct tipc_link *l_ptr,
  870. struct sk_buff *buf)
  871. {
  872. struct tipc_msg *msg = buf_msg(buf);
  873. struct net *net = l_ptr->owner->net;
  874. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  875. if (l_ptr->addr) {
  876. /* Handle failure on standard link */
  877. link_print(l_ptr, "Resetting link\n");
  878. tipc_link_reset(l_ptr);
  879. } else {
  880. /* Handle failure on broadcast link */
  881. struct tipc_node *n_ptr;
  882. char addr_string[16];
  883. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  884. pr_cont("Outstanding acks: %lu\n",
  885. (unsigned long) TIPC_SKB_CB(buf)->handle);
  886. n_ptr = tipc_bclink_retransmit_to(net);
  887. tipc_addr_string_fill(addr_string, n_ptr->addr);
  888. pr_info("Broadcast link info for %s\n", addr_string);
  889. pr_info("Reception permitted: %d, Acked: %u\n",
  890. n_ptr->bclink.recv_permitted,
  891. n_ptr->bclink.acked);
  892. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  893. n_ptr->bclink.last_in,
  894. n_ptr->bclink.oos_state,
  895. n_ptr->bclink.last_sent);
  896. n_ptr->action_flags |= TIPC_BCAST_RESET;
  897. l_ptr->stale_count = 0;
  898. }
  899. }
  900. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
  901. u32 retransmits)
  902. {
  903. struct tipc_msg *msg;
  904. if (!skb)
  905. return;
  906. msg = buf_msg(skb);
  907. /* Detect repeated retransmit failures */
  908. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  909. if (++l_ptr->stale_count > 100) {
  910. link_retransmit_failure(l_ptr, skb);
  911. return;
  912. }
  913. } else {
  914. l_ptr->last_retransmitted = msg_seqno(msg);
  915. l_ptr->stale_count = 1;
  916. }
  917. skb_queue_walk_from(&l_ptr->transmq, skb) {
  918. if (!retransmits)
  919. break;
  920. msg = buf_msg(skb);
  921. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  922. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  923. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
  924. &l_ptr->media_addr);
  925. retransmits--;
  926. l_ptr->stats.retransmitted++;
  927. }
  928. }
  929. /* link_synch(): check if all packets arrived before the synch
  930. * point have been consumed
  931. * Returns true if the parallel links are synched, otherwise false
  932. */
  933. static bool link_synch(struct tipc_link *l)
  934. {
  935. unsigned int post_synch;
  936. struct tipc_link *pl;
  937. pl = tipc_parallel_link(l);
  938. if (pl == l)
  939. goto synched;
  940. /* Was last pre-synch packet added to input queue ? */
  941. if (less_eq(pl->next_in_no, l->synch_point))
  942. return false;
  943. /* Is it still in the input queue ? */
  944. post_synch = mod(pl->next_in_no - l->synch_point) - 1;
  945. if (skb_queue_len(&pl->inputq) > post_synch)
  946. return false;
  947. synched:
  948. l->flags &= ~LINK_SYNCHING;
  949. return true;
  950. }
  951. static void link_retrieve_defq(struct tipc_link *link,
  952. struct sk_buff_head *list)
  953. {
  954. u32 seq_no;
  955. if (skb_queue_empty(&link->deferdq))
  956. return;
  957. seq_no = buf_seqno(skb_peek(&link->deferdq));
  958. if (seq_no == mod(link->next_in_no))
  959. skb_queue_splice_tail_init(&link->deferdq, list);
  960. }
  961. /**
  962. * tipc_rcv - process TIPC packets/messages arriving from off-node
  963. * @net: the applicable net namespace
  964. * @skb: TIPC packet
  965. * @b_ptr: pointer to bearer message arrived on
  966. *
  967. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  968. * structure (i.e. cannot be NULL), but bearer can be inactive.
  969. */
  970. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
  971. {
  972. struct tipc_net *tn = net_generic(net, tipc_net_id);
  973. struct sk_buff_head head;
  974. struct tipc_node *n_ptr;
  975. struct tipc_link *l_ptr;
  976. struct sk_buff *skb1, *tmp;
  977. struct tipc_msg *msg;
  978. u32 seq_no;
  979. u32 ackd;
  980. u32 released;
  981. skb2list(skb, &head);
  982. while ((skb = __skb_dequeue(&head))) {
  983. /* Ensure message is well-formed */
  984. if (unlikely(!tipc_msg_validate(skb)))
  985. goto discard;
  986. /* Handle arrival of a non-unicast link message */
  987. msg = buf_msg(skb);
  988. if (unlikely(msg_non_seq(msg))) {
  989. if (msg_user(msg) == LINK_CONFIG)
  990. tipc_disc_rcv(net, skb, b_ptr);
  991. else
  992. tipc_bclink_rcv(net, skb);
  993. continue;
  994. }
  995. /* Discard unicast link messages destined for another node */
  996. if (unlikely(!msg_short(msg) &&
  997. (msg_destnode(msg) != tn->own_addr)))
  998. goto discard;
  999. /* Locate neighboring node that sent message */
  1000. n_ptr = tipc_node_find(net, msg_prevnode(msg));
  1001. if (unlikely(!n_ptr))
  1002. goto discard;
  1003. tipc_node_lock(n_ptr);
  1004. /* Locate unicast link endpoint that should handle message */
  1005. l_ptr = n_ptr->links[b_ptr->identity];
  1006. if (unlikely(!l_ptr))
  1007. goto unlock;
  1008. /* Verify that communication with node is currently allowed */
  1009. if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
  1010. msg_user(msg) == LINK_PROTOCOL &&
  1011. (msg_type(msg) == RESET_MSG ||
  1012. msg_type(msg) == ACTIVATE_MSG) &&
  1013. !msg_redundant_link(msg))
  1014. n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
  1015. if (tipc_node_blocked(n_ptr))
  1016. goto unlock;
  1017. /* Validate message sequence number info */
  1018. seq_no = msg_seqno(msg);
  1019. ackd = msg_ack(msg);
  1020. /* Release acked messages */
  1021. if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
  1022. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1023. released = 0;
  1024. skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
  1025. if (more(buf_seqno(skb1), ackd))
  1026. break;
  1027. __skb_unlink(skb1, &l_ptr->transmq);
  1028. kfree_skb(skb1);
  1029. released = 1;
  1030. }
  1031. /* Try sending any messages link endpoint has pending */
  1032. if (unlikely(skb_queue_len(&l_ptr->backlogq)))
  1033. tipc_link_push_packets(l_ptr);
  1034. if (released && !skb_queue_empty(&l_ptr->wakeupq))
  1035. link_prepare_wakeup(l_ptr);
  1036. /* Process the incoming packet */
  1037. if (unlikely(!link_working_working(l_ptr))) {
  1038. if (msg_user(msg) == LINK_PROTOCOL) {
  1039. tipc_link_proto_rcv(l_ptr, skb);
  1040. link_retrieve_defq(l_ptr, &head);
  1041. skb = NULL;
  1042. goto unlock;
  1043. }
  1044. /* Traffic message. Conditionally activate link */
  1045. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1046. if (link_working_working(l_ptr)) {
  1047. /* Re-insert buffer in front of queue */
  1048. __skb_queue_head(&head, skb);
  1049. skb = NULL;
  1050. goto unlock;
  1051. }
  1052. goto unlock;
  1053. }
  1054. /* Link is now in state WORKING_WORKING */
  1055. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1056. link_handle_out_of_seq_msg(l_ptr, skb);
  1057. link_retrieve_defq(l_ptr, &head);
  1058. skb = NULL;
  1059. goto unlock;
  1060. }
  1061. /* Synchronize with parallel link if applicable */
  1062. if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
  1063. link_handle_out_of_seq_msg(l_ptr, skb);
  1064. if (link_synch(l_ptr))
  1065. link_retrieve_defq(l_ptr, &head);
  1066. skb = NULL;
  1067. goto unlock;
  1068. }
  1069. l_ptr->next_in_no++;
  1070. if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
  1071. link_retrieve_defq(l_ptr, &head);
  1072. if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
  1073. l_ptr->stats.sent_acks++;
  1074. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1075. }
  1076. tipc_link_input(l_ptr, skb);
  1077. skb = NULL;
  1078. unlock:
  1079. tipc_node_unlock(n_ptr);
  1080. tipc_node_put(n_ptr);
  1081. discard:
  1082. if (unlikely(skb))
  1083. kfree_skb(skb);
  1084. }
  1085. }
  1086. /* tipc_data_input - deliver data and name distr msgs to upper layer
  1087. *
  1088. * Consumes buffer if message is of right type
  1089. * Node lock must be held
  1090. */
  1091. static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
  1092. {
  1093. struct tipc_node *node = link->owner;
  1094. struct tipc_msg *msg = buf_msg(skb);
  1095. u32 dport = msg_destport(msg);
  1096. switch (msg_user(msg)) {
  1097. case TIPC_LOW_IMPORTANCE:
  1098. case TIPC_MEDIUM_IMPORTANCE:
  1099. case TIPC_HIGH_IMPORTANCE:
  1100. case TIPC_CRITICAL_IMPORTANCE:
  1101. case CONN_MANAGER:
  1102. if (tipc_skb_queue_tail(&link->inputq, skb, dport)) {
  1103. node->inputq = &link->inputq;
  1104. node->action_flags |= TIPC_MSG_EVT;
  1105. }
  1106. return true;
  1107. case NAME_DISTRIBUTOR:
  1108. node->bclink.recv_permitted = true;
  1109. node->namedq = &link->namedq;
  1110. skb_queue_tail(&link->namedq, skb);
  1111. if (skb_queue_len(&link->namedq) == 1)
  1112. node->action_flags |= TIPC_NAMED_MSG_EVT;
  1113. return true;
  1114. case MSG_BUNDLER:
  1115. case CHANGEOVER_PROTOCOL:
  1116. case MSG_FRAGMENTER:
  1117. case BCAST_PROTOCOL:
  1118. return false;
  1119. default:
  1120. pr_warn("Dropping received illegal msg type\n");
  1121. kfree_skb(skb);
  1122. return false;
  1123. };
  1124. }
  1125. /* tipc_link_input - process packet that has passed link protocol check
  1126. *
  1127. * Consumes buffer
  1128. * Node lock must be held
  1129. */
  1130. static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
  1131. {
  1132. struct tipc_node *node = link->owner;
  1133. struct tipc_msg *msg = buf_msg(skb);
  1134. struct sk_buff *iskb;
  1135. int pos = 0;
  1136. if (likely(tipc_data_input(link, skb)))
  1137. return;
  1138. switch (msg_user(msg)) {
  1139. case CHANGEOVER_PROTOCOL:
  1140. if (msg_dup(msg)) {
  1141. link->flags |= LINK_SYNCHING;
  1142. link->synch_point = msg_seqno(msg_get_wrapped(msg));
  1143. }
  1144. if (!tipc_link_tunnel_rcv(node, &skb))
  1145. break;
  1146. if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
  1147. tipc_data_input(link, skb);
  1148. break;
  1149. }
  1150. case MSG_BUNDLER:
  1151. link->stats.recv_bundles++;
  1152. link->stats.recv_bundled += msg_msgcnt(msg);
  1153. while (tipc_msg_extract(skb, &iskb, &pos))
  1154. tipc_data_input(link, iskb);
  1155. break;
  1156. case MSG_FRAGMENTER:
  1157. link->stats.recv_fragments++;
  1158. if (tipc_buf_append(&link->reasm_buf, &skb)) {
  1159. link->stats.recv_fragmented++;
  1160. tipc_data_input(link, skb);
  1161. } else if (!link->reasm_buf) {
  1162. tipc_link_reset(link);
  1163. }
  1164. break;
  1165. case BCAST_PROTOCOL:
  1166. tipc_link_sync_rcv(node, skb);
  1167. break;
  1168. default:
  1169. break;
  1170. };
  1171. }
  1172. /**
  1173. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1174. *
  1175. * Returns increase in queue length (i.e. 0 or 1)
  1176. */
  1177. u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
  1178. {
  1179. struct sk_buff *skb1;
  1180. u32 seq_no = buf_seqno(skb);
  1181. /* Empty queue ? */
  1182. if (skb_queue_empty(list)) {
  1183. __skb_queue_tail(list, skb);
  1184. return 1;
  1185. }
  1186. /* Last ? */
  1187. if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
  1188. __skb_queue_tail(list, skb);
  1189. return 1;
  1190. }
  1191. /* Locate insertion point in queue, then insert; discard if duplicate */
  1192. skb_queue_walk(list, skb1) {
  1193. u32 curr_seqno = buf_seqno(skb1);
  1194. if (seq_no == curr_seqno) {
  1195. kfree_skb(skb);
  1196. return 0;
  1197. }
  1198. if (less(seq_no, curr_seqno))
  1199. break;
  1200. }
  1201. __skb_queue_before(list, skb1, skb);
  1202. return 1;
  1203. }
  1204. /*
  1205. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1206. */
  1207. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1208. struct sk_buff *buf)
  1209. {
  1210. u32 seq_no = buf_seqno(buf);
  1211. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1212. tipc_link_proto_rcv(l_ptr, buf);
  1213. return;
  1214. }
  1215. /* Record OOS packet arrival (force mismatch on next timeout) */
  1216. l_ptr->checkpoint--;
  1217. /*
  1218. * Discard packet if a duplicate; otherwise add it to deferred queue
  1219. * and notify peer of gap as per protocol specification
  1220. */
  1221. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1222. l_ptr->stats.duplicates++;
  1223. kfree_skb(buf);
  1224. return;
  1225. }
  1226. if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
  1227. l_ptr->stats.deferred_recv++;
  1228. if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
  1229. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1230. } else {
  1231. l_ptr->stats.duplicates++;
  1232. }
  1233. }
  1234. /*
  1235. * Send protocol message to the other endpoint.
  1236. */
  1237. void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
  1238. u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
  1239. {
  1240. struct sk_buff *buf = NULL;
  1241. struct tipc_msg *msg = l_ptr->pmsg;
  1242. u32 msg_size = sizeof(l_ptr->proto_msg);
  1243. int r_flag;
  1244. /* Don't send protocol message during link changeover */
  1245. if (l_ptr->exp_msg_count)
  1246. return;
  1247. /* Abort non-RESET send if communication with node is prohibited */
  1248. if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
  1249. return;
  1250. /* Create protocol message with "out-of-sequence" sequence number */
  1251. msg_set_type(msg, msg_typ);
  1252. msg_set_net_plane(msg, l_ptr->net_plane);
  1253. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1254. msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
  1255. if (msg_typ == STATE_MSG) {
  1256. u32 next_sent = mod(l_ptr->next_out_no);
  1257. if (!tipc_link_is_up(l_ptr))
  1258. return;
  1259. if (skb_queue_len(&l_ptr->backlogq))
  1260. next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
  1261. msg_set_next_sent(msg, next_sent);
  1262. if (!skb_queue_empty(&l_ptr->deferdq)) {
  1263. u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
  1264. gap = mod(rec - mod(l_ptr->next_in_no));
  1265. }
  1266. msg_set_seq_gap(msg, gap);
  1267. if (gap)
  1268. l_ptr->stats.sent_nacks++;
  1269. msg_set_link_tolerance(msg, tolerance);
  1270. msg_set_linkprio(msg, priority);
  1271. msg_set_max_pkt(msg, ack_mtu);
  1272. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1273. msg_set_probe(msg, probe_msg != 0);
  1274. if (probe_msg) {
  1275. u32 mtu = l_ptr->max_pkt;
  1276. if ((mtu < l_ptr->max_pkt_target) &&
  1277. link_working_working(l_ptr) &&
  1278. l_ptr->fsm_msg_cnt) {
  1279. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1280. if (l_ptr->max_pkt_probes == 10) {
  1281. l_ptr->max_pkt_target = (msg_size - 4);
  1282. l_ptr->max_pkt_probes = 0;
  1283. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1284. }
  1285. l_ptr->max_pkt_probes++;
  1286. }
  1287. l_ptr->stats.sent_probes++;
  1288. }
  1289. l_ptr->stats.sent_states++;
  1290. } else { /* RESET_MSG or ACTIVATE_MSG */
  1291. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1292. msg_set_seq_gap(msg, 0);
  1293. msg_set_next_sent(msg, 1);
  1294. msg_set_probe(msg, 0);
  1295. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1296. msg_set_linkprio(msg, l_ptr->priority);
  1297. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1298. }
  1299. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1300. msg_set_redundant_link(msg, r_flag);
  1301. msg_set_linkprio(msg, l_ptr->priority);
  1302. msg_set_size(msg, msg_size);
  1303. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1304. buf = tipc_buf_acquire(msg_size);
  1305. if (!buf)
  1306. return;
  1307. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1308. buf->priority = TC_PRIO_CONTROL;
  1309. tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
  1310. &l_ptr->media_addr);
  1311. l_ptr->rcv_unacked = 0;
  1312. kfree_skb(buf);
  1313. }
  1314. /*
  1315. * Receive protocol message :
  1316. * Note that network plane id propagates through the network, and may
  1317. * change at any time. The node with lowest address rules
  1318. */
  1319. static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
  1320. struct sk_buff *buf)
  1321. {
  1322. u32 rec_gap = 0;
  1323. u32 max_pkt_info;
  1324. u32 max_pkt_ack;
  1325. u32 msg_tol;
  1326. struct tipc_msg *msg = buf_msg(buf);
  1327. /* Discard protocol message during link changeover */
  1328. if (l_ptr->exp_msg_count)
  1329. goto exit;
  1330. if (l_ptr->net_plane != msg_net_plane(msg))
  1331. if (link_own_addr(l_ptr) > msg_prevnode(msg))
  1332. l_ptr->net_plane = msg_net_plane(msg);
  1333. switch (msg_type(msg)) {
  1334. case RESET_MSG:
  1335. if (!link_working_unknown(l_ptr) &&
  1336. (l_ptr->peer_session != INVALID_SESSION)) {
  1337. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1338. break; /* duplicate or old reset: ignore */
  1339. }
  1340. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1341. link_working_unknown(l_ptr))) {
  1342. /*
  1343. * peer has lost contact -- don't allow peer's links
  1344. * to reactivate before we recognize loss & clean up
  1345. */
  1346. l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
  1347. }
  1348. link_state_event(l_ptr, RESET_MSG);
  1349. /* fall thru' */
  1350. case ACTIVATE_MSG:
  1351. /* Update link settings according other endpoint's values */
  1352. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1353. msg_tol = msg_link_tolerance(msg);
  1354. if (msg_tol > l_ptr->tolerance)
  1355. link_set_supervision_props(l_ptr, msg_tol);
  1356. if (msg_linkprio(msg) > l_ptr->priority)
  1357. l_ptr->priority = msg_linkprio(msg);
  1358. max_pkt_info = msg_max_pkt(msg);
  1359. if (max_pkt_info) {
  1360. if (max_pkt_info < l_ptr->max_pkt_target)
  1361. l_ptr->max_pkt_target = max_pkt_info;
  1362. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1363. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1364. } else {
  1365. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1366. }
  1367. /* Synchronize broadcast link info, if not done previously */
  1368. if (!tipc_node_is_up(l_ptr->owner)) {
  1369. l_ptr->owner->bclink.last_sent =
  1370. l_ptr->owner->bclink.last_in =
  1371. msg_last_bcast(msg);
  1372. l_ptr->owner->bclink.oos_state = 0;
  1373. }
  1374. l_ptr->peer_session = msg_session(msg);
  1375. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1376. if (msg_type(msg) == ACTIVATE_MSG)
  1377. link_state_event(l_ptr, ACTIVATE_MSG);
  1378. break;
  1379. case STATE_MSG:
  1380. msg_tol = msg_link_tolerance(msg);
  1381. if (msg_tol)
  1382. link_set_supervision_props(l_ptr, msg_tol);
  1383. if (msg_linkprio(msg) &&
  1384. (msg_linkprio(msg) != l_ptr->priority)) {
  1385. pr_debug("%s<%s>, priority change %u->%u\n",
  1386. link_rst_msg, l_ptr->name,
  1387. l_ptr->priority, msg_linkprio(msg));
  1388. l_ptr->priority = msg_linkprio(msg);
  1389. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1390. break;
  1391. }
  1392. /* Record reception; force mismatch at next timeout: */
  1393. l_ptr->checkpoint--;
  1394. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1395. l_ptr->stats.recv_states++;
  1396. if (link_reset_unknown(l_ptr))
  1397. break;
  1398. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1399. rec_gap = mod(msg_next_sent(msg) -
  1400. mod(l_ptr->next_in_no));
  1401. }
  1402. max_pkt_ack = msg_max_pkt(msg);
  1403. if (max_pkt_ack > l_ptr->max_pkt) {
  1404. l_ptr->max_pkt = max_pkt_ack;
  1405. l_ptr->max_pkt_probes = 0;
  1406. }
  1407. max_pkt_ack = 0;
  1408. if (msg_probe(msg)) {
  1409. l_ptr->stats.recv_probes++;
  1410. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1411. max_pkt_ack = msg_size(msg);
  1412. }
  1413. /* Protocol message before retransmits, reduce loss risk */
  1414. if (l_ptr->owner->bclink.recv_permitted)
  1415. tipc_bclink_update_link_state(l_ptr->owner,
  1416. msg_last_bcast(msg));
  1417. if (rec_gap || (msg_probe(msg))) {
  1418. tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
  1419. 0, max_pkt_ack);
  1420. }
  1421. if (msg_seq_gap(msg)) {
  1422. l_ptr->stats.recv_nacks++;
  1423. tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
  1424. msg_seq_gap(msg));
  1425. }
  1426. break;
  1427. }
  1428. exit:
  1429. kfree_skb(buf);
  1430. }
  1431. /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
  1432. * a different bearer. Owner node is locked.
  1433. */
  1434. static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
  1435. struct tipc_msg *tunnel_hdr,
  1436. struct tipc_msg *msg,
  1437. u32 selector)
  1438. {
  1439. struct tipc_link *tunnel;
  1440. struct sk_buff *skb;
  1441. u32 length = msg_size(msg);
  1442. tunnel = l_ptr->owner->active_links[selector & 1];
  1443. if (!tipc_link_is_up(tunnel)) {
  1444. pr_warn("%stunnel link no longer available\n", link_co_err);
  1445. return;
  1446. }
  1447. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1448. skb = tipc_buf_acquire(length + INT_H_SIZE);
  1449. if (!skb) {
  1450. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1451. return;
  1452. }
  1453. skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
  1454. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
  1455. __tipc_link_xmit_skb(tunnel, skb);
  1456. }
  1457. /* tipc_link_failover_send_queue(): A link has gone down, but a second
  1458. * link is still active. We can do failover. Tunnel the failing link's
  1459. * whole send queue via the remaining link. This way, we don't lose
  1460. * any packets, and sequence order is preserved for subsequent traffic
  1461. * sent over the remaining link. Owner node is locked.
  1462. */
  1463. void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
  1464. {
  1465. int msgcount;
  1466. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1467. struct tipc_msg tunnel_hdr;
  1468. struct sk_buff *skb;
  1469. int split_bundles;
  1470. if (!tunnel)
  1471. return;
  1472. tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, CHANGEOVER_PROTOCOL,
  1473. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1474. skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
  1475. tipc_link_purge_backlog(l_ptr);
  1476. msgcount = skb_queue_len(&l_ptr->transmq);
  1477. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1478. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1479. if (skb_queue_empty(&l_ptr->transmq)) {
  1480. skb = tipc_buf_acquire(INT_H_SIZE);
  1481. if (skb) {
  1482. skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
  1483. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1484. __tipc_link_xmit_skb(tunnel, skb);
  1485. } else {
  1486. pr_warn("%sunable to send changeover msg\n",
  1487. link_co_err);
  1488. }
  1489. return;
  1490. }
  1491. split_bundles = (l_ptr->owner->active_links[0] !=
  1492. l_ptr->owner->active_links[1]);
  1493. skb_queue_walk(&l_ptr->transmq, skb) {
  1494. struct tipc_msg *msg = buf_msg(skb);
  1495. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1496. struct tipc_msg *m = msg_get_wrapped(msg);
  1497. unchar *pos = (unchar *)m;
  1498. msgcount = msg_msgcnt(msg);
  1499. while (msgcount--) {
  1500. msg_set_seqno(m, msg_seqno(msg));
  1501. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
  1502. msg_link_selector(m));
  1503. pos += align(msg_size(m));
  1504. m = (struct tipc_msg *)pos;
  1505. }
  1506. } else {
  1507. tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
  1508. msg_link_selector(msg));
  1509. }
  1510. }
  1511. }
  1512. /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
  1513. * duplicate of the first link's send queue via the new link. This way, we
  1514. * are guaranteed that currently queued packets from a socket are delivered
  1515. * before future traffic from the same socket, even if this is using the
  1516. * new link. The last arriving copy of each duplicate packet is dropped at
  1517. * the receiving end by the regular protocol check, so packet cardinality
  1518. * and sequence order is preserved per sender/receiver socket pair.
  1519. * Owner node is locked.
  1520. */
  1521. void tipc_link_dup_queue_xmit(struct tipc_link *link,
  1522. struct tipc_link *tnl)
  1523. {
  1524. struct sk_buff *skb;
  1525. struct tipc_msg tnl_hdr;
  1526. struct sk_buff_head *queue = &link->transmq;
  1527. int mcnt;
  1528. tipc_msg_init(link_own_addr(link), &tnl_hdr, CHANGEOVER_PROTOCOL,
  1529. DUPLICATE_MSG, INT_H_SIZE, link->addr);
  1530. mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
  1531. msg_set_msgcnt(&tnl_hdr, mcnt);
  1532. msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
  1533. tunnel_queue:
  1534. skb_queue_walk(queue, skb) {
  1535. struct sk_buff *outskb;
  1536. struct tipc_msg *msg = buf_msg(skb);
  1537. u32 len = msg_size(msg);
  1538. msg_set_ack(msg, mod(link->next_in_no - 1));
  1539. msg_set_bcast_ack(msg, link->owner->bclink.last_in);
  1540. msg_set_size(&tnl_hdr, len + INT_H_SIZE);
  1541. outskb = tipc_buf_acquire(len + INT_H_SIZE);
  1542. if (outskb == NULL) {
  1543. pr_warn("%sunable to send duplicate msg\n",
  1544. link_co_err);
  1545. return;
  1546. }
  1547. skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
  1548. skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
  1549. skb->data, len);
  1550. __tipc_link_xmit_skb(tnl, outskb);
  1551. if (!tipc_link_is_up(link))
  1552. return;
  1553. }
  1554. if (queue == &link->backlogq)
  1555. return;
  1556. queue = &link->backlogq;
  1557. goto tunnel_queue;
  1558. }
  1559. /* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
  1560. * Owner node is locked.
  1561. */
  1562. static void tipc_link_dup_rcv(struct tipc_link *link,
  1563. struct sk_buff *skb)
  1564. {
  1565. struct sk_buff *iskb;
  1566. int pos = 0;
  1567. if (!tipc_link_is_up(link))
  1568. return;
  1569. if (!tipc_msg_extract(skb, &iskb, &pos)) {
  1570. pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
  1571. return;
  1572. }
  1573. /* Append buffer to deferred queue, if applicable: */
  1574. link_handle_out_of_seq_msg(link, iskb);
  1575. }
  1576. /* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
  1577. * Owner node is locked.
  1578. */
  1579. static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
  1580. struct sk_buff *t_buf)
  1581. {
  1582. struct tipc_msg *t_msg = buf_msg(t_buf);
  1583. struct sk_buff *buf = NULL;
  1584. struct tipc_msg *msg;
  1585. int pos = 0;
  1586. if (tipc_link_is_up(l_ptr))
  1587. tipc_link_reset(l_ptr);
  1588. /* First failover packet? */
  1589. if (l_ptr->exp_msg_count == START_CHANGEOVER)
  1590. l_ptr->exp_msg_count = msg_msgcnt(t_msg);
  1591. /* Should there be an inner packet? */
  1592. if (l_ptr->exp_msg_count) {
  1593. l_ptr->exp_msg_count--;
  1594. if (!tipc_msg_extract(t_buf, &buf, &pos)) {
  1595. pr_warn("%sno inner failover pkt\n", link_co_err);
  1596. goto exit;
  1597. }
  1598. msg = buf_msg(buf);
  1599. if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
  1600. kfree_skb(buf);
  1601. buf = NULL;
  1602. goto exit;
  1603. }
  1604. if (msg_user(msg) == MSG_FRAGMENTER) {
  1605. l_ptr->stats.recv_fragments++;
  1606. tipc_buf_append(&l_ptr->reasm_buf, &buf);
  1607. }
  1608. }
  1609. exit:
  1610. if ((!l_ptr->exp_msg_count) && (l_ptr->flags & LINK_STOPPED))
  1611. tipc_link_delete(l_ptr);
  1612. return buf;
  1613. }
  1614. /* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
  1615. * via other link as result of a failover (ORIGINAL_MSG) or
  1616. * a new active link (DUPLICATE_MSG). Failover packets are
  1617. * returned to the active link for delivery upwards.
  1618. * Owner node is locked.
  1619. */
  1620. static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
  1621. struct sk_buff **buf)
  1622. {
  1623. struct sk_buff *t_buf = *buf;
  1624. struct tipc_link *l_ptr;
  1625. struct tipc_msg *t_msg = buf_msg(t_buf);
  1626. u32 bearer_id = msg_bearer_id(t_msg);
  1627. *buf = NULL;
  1628. if (bearer_id >= MAX_BEARERS)
  1629. goto exit;
  1630. l_ptr = n_ptr->links[bearer_id];
  1631. if (!l_ptr)
  1632. goto exit;
  1633. if (msg_type(t_msg) == DUPLICATE_MSG)
  1634. tipc_link_dup_rcv(l_ptr, t_buf);
  1635. else if (msg_type(t_msg) == ORIGINAL_MSG)
  1636. *buf = tipc_link_failover_rcv(l_ptr, t_buf);
  1637. else
  1638. pr_warn("%sunknown tunnel pkt received\n", link_co_err);
  1639. exit:
  1640. kfree_skb(t_buf);
  1641. return *buf != NULL;
  1642. }
  1643. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
  1644. {
  1645. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  1646. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1647. return;
  1648. l_ptr->tolerance = tol;
  1649. l_ptr->cont_intv = msecs_to_jiffies(intv);
  1650. l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
  1651. }
  1652. void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
  1653. {
  1654. int max_bulk = TIPC_MAX_PUBLICATIONS / (l->max_pkt / ITEM_SIZE);
  1655. l->window = win;
  1656. l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
  1657. l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
  1658. l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
  1659. l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
  1660. l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
  1661. }
  1662. /* tipc_link_find_owner - locate owner node of link by link's name
  1663. * @net: the applicable net namespace
  1664. * @name: pointer to link name string
  1665. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1666. *
  1667. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1668. */
  1669. static struct tipc_node *tipc_link_find_owner(struct net *net,
  1670. const char *link_name,
  1671. unsigned int *bearer_id)
  1672. {
  1673. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1674. struct tipc_link *l_ptr;
  1675. struct tipc_node *n_ptr;
  1676. struct tipc_node *found_node = NULL;
  1677. int i;
  1678. *bearer_id = 0;
  1679. rcu_read_lock();
  1680. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  1681. tipc_node_lock(n_ptr);
  1682. for (i = 0; i < MAX_BEARERS; i++) {
  1683. l_ptr = n_ptr->links[i];
  1684. if (l_ptr && !strcmp(l_ptr->name, link_name)) {
  1685. *bearer_id = i;
  1686. found_node = n_ptr;
  1687. break;
  1688. }
  1689. }
  1690. tipc_node_unlock(n_ptr);
  1691. if (found_node)
  1692. break;
  1693. }
  1694. rcu_read_unlock();
  1695. return found_node;
  1696. }
  1697. /**
  1698. * link_reset_statistics - reset link statistics
  1699. * @l_ptr: pointer to link
  1700. */
  1701. static void link_reset_statistics(struct tipc_link *l_ptr)
  1702. {
  1703. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  1704. l_ptr->stats.sent_info = l_ptr->next_out_no;
  1705. l_ptr->stats.recv_info = l_ptr->next_in_no;
  1706. }
  1707. static void link_print(struct tipc_link *l_ptr, const char *str)
  1708. {
  1709. struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
  1710. struct tipc_bearer *b_ptr;
  1711. rcu_read_lock();
  1712. b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
  1713. if (b_ptr)
  1714. pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
  1715. rcu_read_unlock();
  1716. if (link_working_unknown(l_ptr))
  1717. pr_cont(":WU\n");
  1718. else if (link_reset_reset(l_ptr))
  1719. pr_cont(":RR\n");
  1720. else if (link_reset_unknown(l_ptr))
  1721. pr_cont(":RU\n");
  1722. else if (link_working_working(l_ptr))
  1723. pr_cont(":WW\n");
  1724. else
  1725. pr_cont("\n");
  1726. }
  1727. /* Parse and validate nested (link) properties valid for media, bearer and link
  1728. */
  1729. int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
  1730. {
  1731. int err;
  1732. err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
  1733. tipc_nl_prop_policy);
  1734. if (err)
  1735. return err;
  1736. if (props[TIPC_NLA_PROP_PRIO]) {
  1737. u32 prio;
  1738. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1739. if (prio > TIPC_MAX_LINK_PRI)
  1740. return -EINVAL;
  1741. }
  1742. if (props[TIPC_NLA_PROP_TOL]) {
  1743. u32 tol;
  1744. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1745. if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
  1746. return -EINVAL;
  1747. }
  1748. if (props[TIPC_NLA_PROP_WIN]) {
  1749. u32 win;
  1750. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1751. if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
  1752. return -EINVAL;
  1753. }
  1754. return 0;
  1755. }
  1756. int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
  1757. {
  1758. int err;
  1759. int res = 0;
  1760. int bearer_id;
  1761. char *name;
  1762. struct tipc_link *link;
  1763. struct tipc_node *node;
  1764. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1765. struct net *net = sock_net(skb->sk);
  1766. if (!info->attrs[TIPC_NLA_LINK])
  1767. return -EINVAL;
  1768. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1769. info->attrs[TIPC_NLA_LINK],
  1770. tipc_nl_link_policy);
  1771. if (err)
  1772. return err;
  1773. if (!attrs[TIPC_NLA_LINK_NAME])
  1774. return -EINVAL;
  1775. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1776. node = tipc_link_find_owner(net, name, &bearer_id);
  1777. if (!node)
  1778. return -EINVAL;
  1779. tipc_node_lock(node);
  1780. link = node->links[bearer_id];
  1781. if (!link) {
  1782. res = -EINVAL;
  1783. goto out;
  1784. }
  1785. if (attrs[TIPC_NLA_LINK_PROP]) {
  1786. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1787. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1788. props);
  1789. if (err) {
  1790. res = err;
  1791. goto out;
  1792. }
  1793. if (props[TIPC_NLA_PROP_TOL]) {
  1794. u32 tol;
  1795. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1796. link_set_supervision_props(link, tol);
  1797. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
  1798. }
  1799. if (props[TIPC_NLA_PROP_PRIO]) {
  1800. u32 prio;
  1801. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1802. link->priority = prio;
  1803. tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
  1804. }
  1805. if (props[TIPC_NLA_PROP_WIN]) {
  1806. u32 win;
  1807. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1808. tipc_link_set_queue_limits(link, win);
  1809. }
  1810. }
  1811. out:
  1812. tipc_node_unlock(node);
  1813. return res;
  1814. }
  1815. static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
  1816. {
  1817. int i;
  1818. struct nlattr *stats;
  1819. struct nla_map {
  1820. u32 key;
  1821. u32 val;
  1822. };
  1823. struct nla_map map[] = {
  1824. {TIPC_NLA_STATS_RX_INFO, s->recv_info},
  1825. {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
  1826. {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
  1827. {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
  1828. {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
  1829. {TIPC_NLA_STATS_TX_INFO, s->sent_info},
  1830. {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
  1831. {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
  1832. {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
  1833. {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
  1834. {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
  1835. s->msg_length_counts : 1},
  1836. {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
  1837. {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
  1838. {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
  1839. {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
  1840. {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
  1841. {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
  1842. {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
  1843. {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
  1844. {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
  1845. {TIPC_NLA_STATS_RX_STATES, s->recv_states},
  1846. {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
  1847. {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
  1848. {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
  1849. {TIPC_NLA_STATS_TX_STATES, s->sent_states},
  1850. {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
  1851. {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
  1852. {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
  1853. {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
  1854. {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
  1855. {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
  1856. {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
  1857. {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
  1858. (s->accu_queue_sz / s->queue_sz_counts) : 0}
  1859. };
  1860. stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  1861. if (!stats)
  1862. return -EMSGSIZE;
  1863. for (i = 0; i < ARRAY_SIZE(map); i++)
  1864. if (nla_put_u32(skb, map[i].key, map[i].val))
  1865. goto msg_full;
  1866. nla_nest_end(skb, stats);
  1867. return 0;
  1868. msg_full:
  1869. nla_nest_cancel(skb, stats);
  1870. return -EMSGSIZE;
  1871. }
  1872. /* Caller should hold appropriate locks to protect the link */
  1873. static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
  1874. struct tipc_link *link)
  1875. {
  1876. int err;
  1877. void *hdr;
  1878. struct nlattr *attrs;
  1879. struct nlattr *prop;
  1880. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1881. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1882. NLM_F_MULTI, TIPC_NL_LINK_GET);
  1883. if (!hdr)
  1884. return -EMSGSIZE;
  1885. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  1886. if (!attrs)
  1887. goto msg_full;
  1888. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
  1889. goto attr_msg_full;
  1890. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
  1891. tipc_cluster_mask(tn->own_addr)))
  1892. goto attr_msg_full;
  1893. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
  1894. goto attr_msg_full;
  1895. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
  1896. goto attr_msg_full;
  1897. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
  1898. goto attr_msg_full;
  1899. if (tipc_link_is_up(link))
  1900. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  1901. goto attr_msg_full;
  1902. if (tipc_link_is_active(link))
  1903. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
  1904. goto attr_msg_full;
  1905. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  1906. if (!prop)
  1907. goto attr_msg_full;
  1908. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1909. goto prop_msg_full;
  1910. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
  1911. goto prop_msg_full;
  1912. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
  1913. link->window))
  1914. goto prop_msg_full;
  1915. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
  1916. goto prop_msg_full;
  1917. nla_nest_end(msg->skb, prop);
  1918. err = __tipc_nl_add_stats(msg->skb, &link->stats);
  1919. if (err)
  1920. goto attr_msg_full;
  1921. nla_nest_end(msg->skb, attrs);
  1922. genlmsg_end(msg->skb, hdr);
  1923. return 0;
  1924. prop_msg_full:
  1925. nla_nest_cancel(msg->skb, prop);
  1926. attr_msg_full:
  1927. nla_nest_cancel(msg->skb, attrs);
  1928. msg_full:
  1929. genlmsg_cancel(msg->skb, hdr);
  1930. return -EMSGSIZE;
  1931. }
  1932. /* Caller should hold node lock */
  1933. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1934. struct tipc_node *node, u32 *prev_link)
  1935. {
  1936. u32 i;
  1937. int err;
  1938. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1939. *prev_link = i;
  1940. if (!node->links[i])
  1941. continue;
  1942. err = __tipc_nl_add_link(net, msg, node->links[i]);
  1943. if (err)
  1944. return err;
  1945. }
  1946. *prev_link = 0;
  1947. return 0;
  1948. }
  1949. int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1950. {
  1951. struct net *net = sock_net(skb->sk);
  1952. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1953. struct tipc_node *node;
  1954. struct tipc_nl_msg msg;
  1955. u32 prev_node = cb->args[0];
  1956. u32 prev_link = cb->args[1];
  1957. int done = cb->args[2];
  1958. int err;
  1959. if (done)
  1960. return 0;
  1961. msg.skb = skb;
  1962. msg.portid = NETLINK_CB(cb->skb).portid;
  1963. msg.seq = cb->nlh->nlmsg_seq;
  1964. rcu_read_lock();
  1965. if (prev_node) {
  1966. node = tipc_node_find(net, prev_node);
  1967. if (!node) {
  1968. /* We never set seq or call nl_dump_check_consistent()
  1969. * this means that setting prev_seq here will cause the
  1970. * consistence check to fail in the netlink callback
  1971. * handler. Resulting in the last NLMSG_DONE message
  1972. * having the NLM_F_DUMP_INTR flag set.
  1973. */
  1974. cb->prev_seq = 1;
  1975. goto out;
  1976. }
  1977. tipc_node_put(node);
  1978. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1979. list) {
  1980. tipc_node_lock(node);
  1981. err = __tipc_nl_add_node_links(net, &msg, node,
  1982. &prev_link);
  1983. tipc_node_unlock(node);
  1984. tipc_node_put(node);
  1985. if (err)
  1986. goto out;
  1987. prev_node = node->addr;
  1988. }
  1989. } else {
  1990. err = tipc_nl_add_bc_link(net, &msg);
  1991. if (err)
  1992. goto out;
  1993. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1994. tipc_node_lock(node);
  1995. err = __tipc_nl_add_node_links(net, &msg, node,
  1996. &prev_link);
  1997. tipc_node_unlock(node);
  1998. if (err)
  1999. goto out;
  2000. prev_node = node->addr;
  2001. }
  2002. }
  2003. done = 1;
  2004. out:
  2005. rcu_read_unlock();
  2006. cb->args[0] = prev_node;
  2007. cb->args[1] = prev_link;
  2008. cb->args[2] = done;
  2009. return skb->len;
  2010. }
  2011. int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
  2012. {
  2013. struct net *net = genl_info_net(info);
  2014. struct sk_buff *ans_skb;
  2015. struct tipc_nl_msg msg;
  2016. struct tipc_link *link;
  2017. struct tipc_node *node;
  2018. char *name;
  2019. int bearer_id;
  2020. int err;
  2021. if (!info->attrs[TIPC_NLA_LINK_NAME])
  2022. return -EINVAL;
  2023. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  2024. node = tipc_link_find_owner(net, name, &bearer_id);
  2025. if (!node)
  2026. return -EINVAL;
  2027. ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  2028. if (!ans_skb)
  2029. return -ENOMEM;
  2030. msg.skb = ans_skb;
  2031. msg.portid = info->snd_portid;
  2032. msg.seq = info->snd_seq;
  2033. tipc_node_lock(node);
  2034. link = node->links[bearer_id];
  2035. if (!link) {
  2036. err = -EINVAL;
  2037. goto err_out;
  2038. }
  2039. err = __tipc_nl_add_link(net, &msg, link);
  2040. if (err)
  2041. goto err_out;
  2042. tipc_node_unlock(node);
  2043. return genlmsg_reply(ans_skb, info);
  2044. err_out:
  2045. tipc_node_unlock(node);
  2046. nlmsg_free(ans_skb);
  2047. return err;
  2048. }
  2049. int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
  2050. {
  2051. int err;
  2052. char *link_name;
  2053. unsigned int bearer_id;
  2054. struct tipc_link *link;
  2055. struct tipc_node *node;
  2056. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2057. struct net *net = sock_net(skb->sk);
  2058. if (!info->attrs[TIPC_NLA_LINK])
  2059. return -EINVAL;
  2060. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  2061. info->attrs[TIPC_NLA_LINK],
  2062. tipc_nl_link_policy);
  2063. if (err)
  2064. return err;
  2065. if (!attrs[TIPC_NLA_LINK_NAME])
  2066. return -EINVAL;
  2067. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2068. if (strcmp(link_name, tipc_bclink_name) == 0) {
  2069. err = tipc_bclink_reset_stats(net);
  2070. if (err)
  2071. return err;
  2072. return 0;
  2073. }
  2074. node = tipc_link_find_owner(net, link_name, &bearer_id);
  2075. if (!node)
  2076. return -EINVAL;
  2077. tipc_node_lock(node);
  2078. link = node->links[bearer_id];
  2079. if (!link) {
  2080. tipc_node_unlock(node);
  2081. return -EINVAL;
  2082. }
  2083. link_reset_statistics(link);
  2084. tipc_node_unlock(node);
  2085. return 0;
  2086. }