link.c 58 KB

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