link.c 65 KB

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