link.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806
  1. /*
  2. * net/tipc/link.c: TIPC link code
  3. *
  4. * Copyright (c) 1996-2007, 2012, 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 "port.h"
  39. #include "name_distr.h"
  40. #include "discover.h"
  41. #include "config.h"
  42. #include <linux/pkt_sched.h>
  43. /*
  44. * Error message prefixes
  45. */
  46. static const char *link_co_err = "Link changeover error, ";
  47. static const char *link_rst_msg = "Resetting link ";
  48. static const char *link_unk_evt = "Unknown link event ";
  49. /*
  50. * Out-of-range value for link session numbers
  51. */
  52. #define INVALID_SESSION 0x10000
  53. /*
  54. * Link state events:
  55. */
  56. #define STARTING_EVT 856384768 /* link processing trigger */
  57. #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
  58. #define TIMEOUT_EVT 560817u /* link timer expired */
  59. /*
  60. * The following two 'message types' is really just implementation
  61. * data conveniently stored in the message header.
  62. * They must not be considered part of the protocol
  63. */
  64. #define OPEN_MSG 0
  65. #define CLOSED_MSG 1
  66. /*
  67. * State value stored in 'exp_msg_count'
  68. */
  69. #define START_CHANGEOVER 100000u
  70. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  71. struct sk_buff *buf);
  72. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
  73. static int link_recv_changeover_msg(struct tipc_link **l_ptr,
  74. struct sk_buff **buf);
  75. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
  76. static int link_send_sections_long(struct tipc_port *sender,
  77. struct iovec const *msg_sect,
  78. unsigned int len, u32 destnode);
  79. static void link_state_event(struct tipc_link *l_ptr, u32 event);
  80. static void link_reset_statistics(struct tipc_link *l_ptr);
  81. static void link_print(struct tipc_link *l_ptr, const char *str);
  82. static void link_start(struct tipc_link *l_ptr);
  83. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
  84. static void tipc_link_send_sync(struct tipc_link *l);
  85. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
  86. /*
  87. * Simple link routines
  88. */
  89. static unsigned int align(unsigned int i)
  90. {
  91. return (i + 3) & ~3u;
  92. }
  93. static void link_init_max_pkt(struct tipc_link *l_ptr)
  94. {
  95. u32 max_pkt;
  96. max_pkt = (l_ptr->b_ptr->mtu & ~3);
  97. if (max_pkt > MAX_MSG_SIZE)
  98. max_pkt = MAX_MSG_SIZE;
  99. l_ptr->max_pkt_target = max_pkt;
  100. if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
  101. l_ptr->max_pkt = l_ptr->max_pkt_target;
  102. else
  103. l_ptr->max_pkt = MAX_PKT_DEFAULT;
  104. l_ptr->max_pkt_probes = 0;
  105. }
  106. static u32 link_next_sent(struct tipc_link *l_ptr)
  107. {
  108. if (l_ptr->next_out)
  109. return buf_seqno(l_ptr->next_out);
  110. return mod(l_ptr->next_out_no);
  111. }
  112. static u32 link_last_sent(struct tipc_link *l_ptr)
  113. {
  114. return mod(link_next_sent(l_ptr) - 1);
  115. }
  116. /*
  117. * Simple non-static link routines (i.e. referenced outside this file)
  118. */
  119. int tipc_link_is_up(struct tipc_link *l_ptr)
  120. {
  121. if (!l_ptr)
  122. return 0;
  123. return link_working_working(l_ptr) || link_working_unknown(l_ptr);
  124. }
  125. int tipc_link_is_active(struct tipc_link *l_ptr)
  126. {
  127. return (l_ptr->owner->active_links[0] == l_ptr) ||
  128. (l_ptr->owner->active_links[1] == l_ptr);
  129. }
  130. /**
  131. * link_timeout - handle expiration of link timer
  132. * @l_ptr: pointer to link
  133. *
  134. * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
  135. * with tipc_link_delete(). (There is no risk that the node will be deleted by
  136. * another thread because tipc_link_delete() always cancels the link timer before
  137. * tipc_node_delete() is called.)
  138. */
  139. static void link_timeout(struct tipc_link *l_ptr)
  140. {
  141. tipc_node_lock(l_ptr->owner);
  142. /* update counters used in statistical profiling of send traffic */
  143. l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
  144. l_ptr->stats.queue_sz_counts++;
  145. if (l_ptr->first_out) {
  146. struct tipc_msg *msg = buf_msg(l_ptr->first_out);
  147. u32 length = msg_size(msg);
  148. if ((msg_user(msg) == MSG_FRAGMENTER) &&
  149. (msg_type(msg) == FIRST_FRAGMENT)) {
  150. length = msg_size(msg_get_wrapped(msg));
  151. }
  152. if (length) {
  153. l_ptr->stats.msg_lengths_total += length;
  154. l_ptr->stats.msg_length_counts++;
  155. if (length <= 64)
  156. l_ptr->stats.msg_length_profile[0]++;
  157. else if (length <= 256)
  158. l_ptr->stats.msg_length_profile[1]++;
  159. else if (length <= 1024)
  160. l_ptr->stats.msg_length_profile[2]++;
  161. else if (length <= 4096)
  162. l_ptr->stats.msg_length_profile[3]++;
  163. else if (length <= 16384)
  164. l_ptr->stats.msg_length_profile[4]++;
  165. else if (length <= 32768)
  166. l_ptr->stats.msg_length_profile[5]++;
  167. else
  168. l_ptr->stats.msg_length_profile[6]++;
  169. }
  170. }
  171. /* do all other link processing performed on a periodic basis */
  172. link_state_event(l_ptr, TIMEOUT_EVT);
  173. if (l_ptr->next_out)
  174. tipc_link_push_queue(l_ptr);
  175. tipc_node_unlock(l_ptr->owner);
  176. }
  177. static void link_set_timer(struct tipc_link *l_ptr, u32 time)
  178. {
  179. k_start_timer(&l_ptr->timer, time);
  180. }
  181. /**
  182. * tipc_link_create - create a new link
  183. * @n_ptr: pointer to associated node
  184. * @b_ptr: pointer to associated bearer
  185. * @media_addr: media address to use when sending messages over link
  186. *
  187. * Returns pointer to link.
  188. */
  189. struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
  190. struct tipc_bearer *b_ptr,
  191. const struct tipc_media_addr *media_addr)
  192. {
  193. struct tipc_link *l_ptr;
  194. struct tipc_msg *msg;
  195. char *if_name;
  196. char addr_string[16];
  197. u32 peer = n_ptr->addr;
  198. if (n_ptr->link_cnt >= 2) {
  199. tipc_addr_string_fill(addr_string, n_ptr->addr);
  200. pr_err("Attempt to establish third link to %s\n", addr_string);
  201. return NULL;
  202. }
  203. if (n_ptr->links[b_ptr->identity]) {
  204. tipc_addr_string_fill(addr_string, n_ptr->addr);
  205. pr_err("Attempt to establish second link on <%s> to %s\n",
  206. b_ptr->name, addr_string);
  207. return NULL;
  208. }
  209. l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
  210. if (!l_ptr) {
  211. pr_warn("Link creation failed, no memory\n");
  212. return NULL;
  213. }
  214. l_ptr->addr = peer;
  215. if_name = strchr(b_ptr->name, ':') + 1;
  216. sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
  217. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  218. tipc_node(tipc_own_addr),
  219. if_name,
  220. tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
  221. /* note: peer i/f name is updated by reset/activate message */
  222. memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
  223. l_ptr->owner = n_ptr;
  224. l_ptr->checkpoint = 1;
  225. l_ptr->peer_session = INVALID_SESSION;
  226. l_ptr->b_ptr = b_ptr;
  227. link_set_supervision_props(l_ptr, b_ptr->tolerance);
  228. l_ptr->state = RESET_UNKNOWN;
  229. l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
  230. msg = l_ptr->pmsg;
  231. tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
  232. msg_set_size(msg, sizeof(l_ptr->proto_msg));
  233. msg_set_session(msg, (tipc_random & 0xffff));
  234. msg_set_bearer_id(msg, b_ptr->identity);
  235. strcpy((char *)msg_data(msg), if_name);
  236. l_ptr->priority = b_ptr->priority;
  237. tipc_link_set_queue_limits(l_ptr, b_ptr->window);
  238. link_init_max_pkt(l_ptr);
  239. l_ptr->next_out_no = 1;
  240. INIT_LIST_HEAD(&l_ptr->waiting_ports);
  241. link_reset_statistics(l_ptr);
  242. tipc_node_attach_link(n_ptr, l_ptr);
  243. k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
  244. list_add_tail(&l_ptr->link_list, &b_ptr->links);
  245. tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
  246. return l_ptr;
  247. }
  248. /**
  249. * tipc_link_delete - delete a link
  250. * @l_ptr: pointer to link
  251. *
  252. * Note: 'tipc_net_lock' is write_locked, bearer is locked.
  253. * This routine must not grab the node lock until after link timer cancellation
  254. * to avoid a potential deadlock situation.
  255. */
  256. void tipc_link_delete(struct tipc_link *l_ptr)
  257. {
  258. if (!l_ptr) {
  259. pr_err("Attempt to delete non-existent link\n");
  260. return;
  261. }
  262. k_cancel_timer(&l_ptr->timer);
  263. tipc_node_lock(l_ptr->owner);
  264. tipc_link_reset(l_ptr);
  265. tipc_node_detach_link(l_ptr->owner, l_ptr);
  266. tipc_link_stop(l_ptr);
  267. list_del_init(&l_ptr->link_list);
  268. tipc_node_unlock(l_ptr->owner);
  269. k_term_timer(&l_ptr->timer);
  270. kfree(l_ptr);
  271. }
  272. static void link_start(struct tipc_link *l_ptr)
  273. {
  274. tipc_node_lock(l_ptr->owner);
  275. link_state_event(l_ptr, STARTING_EVT);
  276. tipc_node_unlock(l_ptr->owner);
  277. }
  278. /**
  279. * link_schedule_port - schedule port for deferred sending
  280. * @l_ptr: pointer to link
  281. * @origport: reference to sending port
  282. * @sz: amount of data to be sent
  283. *
  284. * Schedules port for renewed sending of messages after link congestion
  285. * has abated.
  286. */
  287. static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
  288. {
  289. struct tipc_port *p_ptr;
  290. spin_lock_bh(&tipc_port_list_lock);
  291. p_ptr = tipc_port_lock(origport);
  292. if (p_ptr) {
  293. if (!p_ptr->wakeup)
  294. goto exit;
  295. if (!list_empty(&p_ptr->wait_list))
  296. goto exit;
  297. p_ptr->congested = 1;
  298. p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
  299. list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
  300. l_ptr->stats.link_congs++;
  301. exit:
  302. tipc_port_unlock(p_ptr);
  303. }
  304. spin_unlock_bh(&tipc_port_list_lock);
  305. return -ELINKCONG;
  306. }
  307. void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
  308. {
  309. struct tipc_port *p_ptr;
  310. struct tipc_port *temp_p_ptr;
  311. int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
  312. if (all)
  313. win = 100000;
  314. if (win <= 0)
  315. return;
  316. if (!spin_trylock_bh(&tipc_port_list_lock))
  317. return;
  318. if (link_congested(l_ptr))
  319. goto exit;
  320. list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
  321. wait_list) {
  322. if (win <= 0)
  323. break;
  324. list_del_init(&p_ptr->wait_list);
  325. spin_lock_bh(p_ptr->lock);
  326. p_ptr->congested = 0;
  327. p_ptr->wakeup(p_ptr);
  328. win -= p_ptr->waiting_pkts;
  329. spin_unlock_bh(p_ptr->lock);
  330. }
  331. exit:
  332. spin_unlock_bh(&tipc_port_list_lock);
  333. }
  334. /**
  335. * link_release_outqueue - purge link's outbound message queue
  336. * @l_ptr: pointer to link
  337. */
  338. static void link_release_outqueue(struct tipc_link *l_ptr)
  339. {
  340. struct sk_buff *buf = l_ptr->first_out;
  341. struct sk_buff *next;
  342. while (buf) {
  343. next = buf->next;
  344. kfree_skb(buf);
  345. buf = next;
  346. }
  347. l_ptr->first_out = NULL;
  348. l_ptr->out_queue_size = 0;
  349. }
  350. /**
  351. * tipc_link_reset_fragments - purge link's inbound message fragments queue
  352. * @l_ptr: pointer to link
  353. */
  354. void tipc_link_reset_fragments(struct tipc_link *l_ptr)
  355. {
  356. kfree_skb(l_ptr->reasm_head);
  357. l_ptr->reasm_head = NULL;
  358. l_ptr->reasm_tail = NULL;
  359. }
  360. /**
  361. * tipc_link_stop - purge all inbound and outbound messages associated with link
  362. * @l_ptr: pointer to link
  363. */
  364. void tipc_link_stop(struct tipc_link *l_ptr)
  365. {
  366. struct sk_buff *buf;
  367. struct sk_buff *next;
  368. buf = l_ptr->oldest_deferred_in;
  369. while (buf) {
  370. next = buf->next;
  371. kfree_skb(buf);
  372. buf = next;
  373. }
  374. buf = l_ptr->first_out;
  375. while (buf) {
  376. next = buf->next;
  377. kfree_skb(buf);
  378. buf = next;
  379. }
  380. tipc_link_reset_fragments(l_ptr);
  381. kfree_skb(l_ptr->proto_msg_queue);
  382. l_ptr->proto_msg_queue = NULL;
  383. }
  384. void tipc_link_reset(struct tipc_link *l_ptr)
  385. {
  386. struct sk_buff *buf;
  387. u32 prev_state = l_ptr->state;
  388. u32 checkpoint = l_ptr->next_in_no;
  389. int was_active_link = tipc_link_is_active(l_ptr);
  390. msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
  391. /* Link is down, accept any session */
  392. l_ptr->peer_session = INVALID_SESSION;
  393. /* Prepare for max packet size negotiation */
  394. link_init_max_pkt(l_ptr);
  395. l_ptr->state = RESET_UNKNOWN;
  396. if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
  397. return;
  398. tipc_node_link_down(l_ptr->owner, l_ptr);
  399. tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
  400. if (was_active_link && tipc_node_active_links(l_ptr->owner) &&
  401. l_ptr->owner->permit_changeover) {
  402. l_ptr->reset_checkpoint = checkpoint;
  403. l_ptr->exp_msg_count = START_CHANGEOVER;
  404. }
  405. /* Clean up all queues: */
  406. link_release_outqueue(l_ptr);
  407. kfree_skb(l_ptr->proto_msg_queue);
  408. l_ptr->proto_msg_queue = NULL;
  409. buf = l_ptr->oldest_deferred_in;
  410. while (buf) {
  411. struct sk_buff *next = buf->next;
  412. kfree_skb(buf);
  413. buf = next;
  414. }
  415. if (!list_empty(&l_ptr->waiting_ports))
  416. tipc_link_wakeup_ports(l_ptr, 1);
  417. l_ptr->retransm_queue_head = 0;
  418. l_ptr->retransm_queue_size = 0;
  419. l_ptr->last_out = NULL;
  420. l_ptr->first_out = NULL;
  421. l_ptr->next_out = NULL;
  422. l_ptr->unacked_window = 0;
  423. l_ptr->checkpoint = 1;
  424. l_ptr->next_out_no = 1;
  425. l_ptr->deferred_inqueue_sz = 0;
  426. l_ptr->oldest_deferred_in = NULL;
  427. l_ptr->newest_deferred_in = NULL;
  428. l_ptr->fsm_msg_cnt = 0;
  429. l_ptr->stale_count = 0;
  430. link_reset_statistics(l_ptr);
  431. }
  432. static void link_activate(struct tipc_link *l_ptr)
  433. {
  434. l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
  435. tipc_node_link_up(l_ptr->owner, l_ptr);
  436. tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
  437. }
  438. /**
  439. * link_state_event - link finite state machine
  440. * @l_ptr: pointer to link
  441. * @event: state machine event to process
  442. */
  443. static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
  444. {
  445. struct tipc_link *other;
  446. u32 cont_intv = l_ptr->continuity_interval;
  447. if (!l_ptr->started && (event != STARTING_EVT))
  448. return; /* Not yet. */
  449. if (link_blocked(l_ptr)) {
  450. if (event == TIMEOUT_EVT)
  451. link_set_timer(l_ptr, cont_intv);
  452. return; /* Changeover going on */
  453. }
  454. switch (l_ptr->state) {
  455. case WORKING_WORKING:
  456. switch (event) {
  457. case TRAFFIC_MSG_EVT:
  458. case ACTIVATE_MSG:
  459. break;
  460. case TIMEOUT_EVT:
  461. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  462. l_ptr->checkpoint = l_ptr->next_in_no;
  463. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  464. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  465. 0, 0, 0, 0, 0);
  466. l_ptr->fsm_msg_cnt++;
  467. } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
  468. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  469. 1, 0, 0, 0, 0);
  470. l_ptr->fsm_msg_cnt++;
  471. }
  472. link_set_timer(l_ptr, cont_intv);
  473. break;
  474. }
  475. l_ptr->state = WORKING_UNKNOWN;
  476. l_ptr->fsm_msg_cnt = 0;
  477. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  478. l_ptr->fsm_msg_cnt++;
  479. link_set_timer(l_ptr, cont_intv / 4);
  480. break;
  481. case RESET_MSG:
  482. pr_info("%s<%s>, requested by peer\n", link_rst_msg,
  483. l_ptr->name);
  484. tipc_link_reset(l_ptr);
  485. l_ptr->state = RESET_RESET;
  486. l_ptr->fsm_msg_cnt = 0;
  487. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  488. l_ptr->fsm_msg_cnt++;
  489. link_set_timer(l_ptr, cont_intv);
  490. break;
  491. default:
  492. pr_err("%s%u in WW state\n", link_unk_evt, event);
  493. }
  494. break;
  495. case WORKING_UNKNOWN:
  496. switch (event) {
  497. case TRAFFIC_MSG_EVT:
  498. case ACTIVATE_MSG:
  499. l_ptr->state = WORKING_WORKING;
  500. l_ptr->fsm_msg_cnt = 0;
  501. link_set_timer(l_ptr, cont_intv);
  502. break;
  503. case RESET_MSG:
  504. pr_info("%s<%s>, requested by peer while probing\n",
  505. link_rst_msg, l_ptr->name);
  506. tipc_link_reset(l_ptr);
  507. l_ptr->state = RESET_RESET;
  508. l_ptr->fsm_msg_cnt = 0;
  509. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  510. l_ptr->fsm_msg_cnt++;
  511. link_set_timer(l_ptr, cont_intv);
  512. break;
  513. case TIMEOUT_EVT:
  514. if (l_ptr->next_in_no != l_ptr->checkpoint) {
  515. l_ptr->state = WORKING_WORKING;
  516. l_ptr->fsm_msg_cnt = 0;
  517. l_ptr->checkpoint = l_ptr->next_in_no;
  518. if (tipc_bclink_acks_missing(l_ptr->owner)) {
  519. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  520. 0, 0, 0, 0, 0);
  521. l_ptr->fsm_msg_cnt++;
  522. }
  523. link_set_timer(l_ptr, cont_intv);
  524. } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
  525. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  526. 1, 0, 0, 0, 0);
  527. l_ptr->fsm_msg_cnt++;
  528. link_set_timer(l_ptr, cont_intv / 4);
  529. } else { /* Link has failed */
  530. pr_warn("%s<%s>, peer not responding\n",
  531. link_rst_msg, l_ptr->name);
  532. tipc_link_reset(l_ptr);
  533. l_ptr->state = RESET_UNKNOWN;
  534. l_ptr->fsm_msg_cnt = 0;
  535. tipc_link_send_proto_msg(l_ptr, RESET_MSG,
  536. 0, 0, 0, 0, 0);
  537. l_ptr->fsm_msg_cnt++;
  538. link_set_timer(l_ptr, cont_intv);
  539. }
  540. break;
  541. default:
  542. pr_err("%s%u in WU state\n", link_unk_evt, event);
  543. }
  544. break;
  545. case RESET_UNKNOWN:
  546. switch (event) {
  547. case TRAFFIC_MSG_EVT:
  548. break;
  549. case ACTIVATE_MSG:
  550. other = l_ptr->owner->active_links[0];
  551. if (other && link_working_unknown(other))
  552. break;
  553. l_ptr->state = WORKING_WORKING;
  554. l_ptr->fsm_msg_cnt = 0;
  555. link_activate(l_ptr);
  556. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  557. l_ptr->fsm_msg_cnt++;
  558. if (l_ptr->owner->working_links == 1)
  559. tipc_link_send_sync(l_ptr);
  560. link_set_timer(l_ptr, cont_intv);
  561. break;
  562. case RESET_MSG:
  563. l_ptr->state = RESET_RESET;
  564. l_ptr->fsm_msg_cnt = 0;
  565. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
  566. l_ptr->fsm_msg_cnt++;
  567. link_set_timer(l_ptr, cont_intv);
  568. break;
  569. case STARTING_EVT:
  570. l_ptr->started = 1;
  571. /* fall through */
  572. case TIMEOUT_EVT:
  573. tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
  574. l_ptr->fsm_msg_cnt++;
  575. link_set_timer(l_ptr, cont_intv);
  576. break;
  577. default:
  578. pr_err("%s%u in RU state\n", link_unk_evt, event);
  579. }
  580. break;
  581. case RESET_RESET:
  582. switch (event) {
  583. case TRAFFIC_MSG_EVT:
  584. case ACTIVATE_MSG:
  585. other = l_ptr->owner->active_links[0];
  586. if (other && link_working_unknown(other))
  587. break;
  588. l_ptr->state = WORKING_WORKING;
  589. l_ptr->fsm_msg_cnt = 0;
  590. link_activate(l_ptr);
  591. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
  592. l_ptr->fsm_msg_cnt++;
  593. if (l_ptr->owner->working_links == 1)
  594. tipc_link_send_sync(l_ptr);
  595. link_set_timer(l_ptr, cont_intv);
  596. break;
  597. case RESET_MSG:
  598. break;
  599. case TIMEOUT_EVT:
  600. tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
  601. l_ptr->fsm_msg_cnt++;
  602. link_set_timer(l_ptr, cont_intv);
  603. break;
  604. default:
  605. pr_err("%s%u in RR state\n", link_unk_evt, event);
  606. }
  607. break;
  608. default:
  609. pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
  610. }
  611. }
  612. /*
  613. * link_bundle_buf(): Append contents of a buffer to
  614. * the tail of an existing one.
  615. */
  616. static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
  617. struct sk_buff *buf)
  618. {
  619. struct tipc_msg *bundler_msg = buf_msg(bundler);
  620. struct tipc_msg *msg = buf_msg(buf);
  621. u32 size = msg_size(msg);
  622. u32 bundle_size = msg_size(bundler_msg);
  623. u32 to_pos = align(bundle_size);
  624. u32 pad = to_pos - bundle_size;
  625. if (msg_user(bundler_msg) != MSG_BUNDLER)
  626. return 0;
  627. if (msg_type(bundler_msg) != OPEN_MSG)
  628. return 0;
  629. if (skb_tailroom(bundler) < (pad + size))
  630. return 0;
  631. if (l_ptr->max_pkt < (to_pos + size))
  632. return 0;
  633. skb_put(bundler, pad + size);
  634. skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
  635. msg_set_size(bundler_msg, to_pos + size);
  636. msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
  637. kfree_skb(buf);
  638. l_ptr->stats.sent_bundled++;
  639. return 1;
  640. }
  641. static void link_add_to_outqueue(struct tipc_link *l_ptr,
  642. struct sk_buff *buf,
  643. struct tipc_msg *msg)
  644. {
  645. u32 ack = mod(l_ptr->next_in_no - 1);
  646. u32 seqno = mod(l_ptr->next_out_no++);
  647. msg_set_word(msg, 2, ((ack << 16) | seqno));
  648. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  649. buf->next = NULL;
  650. if (l_ptr->first_out) {
  651. l_ptr->last_out->next = buf;
  652. l_ptr->last_out = buf;
  653. } else
  654. l_ptr->first_out = l_ptr->last_out = buf;
  655. l_ptr->out_queue_size++;
  656. if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
  657. l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
  658. }
  659. static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
  660. struct sk_buff *buf_chain,
  661. u32 long_msgno)
  662. {
  663. struct sk_buff *buf;
  664. struct tipc_msg *msg;
  665. if (!l_ptr->next_out)
  666. l_ptr->next_out = buf_chain;
  667. while (buf_chain) {
  668. buf = buf_chain;
  669. buf_chain = buf_chain->next;
  670. msg = buf_msg(buf);
  671. msg_set_long_msgno(msg, long_msgno);
  672. link_add_to_outqueue(l_ptr, buf, msg);
  673. }
  674. }
  675. /*
  676. * tipc_link_send_buf() is the 'full path' for messages, called from
  677. * inside TIPC when the 'fast path' in tipc_send_buf
  678. * has failed, and from link_send()
  679. */
  680. int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  681. {
  682. struct tipc_msg *msg = buf_msg(buf);
  683. u32 size = msg_size(msg);
  684. u32 dsz = msg_data_sz(msg);
  685. u32 queue_size = l_ptr->out_queue_size;
  686. u32 imp = tipc_msg_tot_importance(msg);
  687. u32 queue_limit = l_ptr->queue_limit[imp];
  688. u32 max_packet = l_ptr->max_pkt;
  689. /* Match msg importance against queue limits: */
  690. if (unlikely(queue_size >= queue_limit)) {
  691. if (imp <= TIPC_CRITICAL_IMPORTANCE) {
  692. link_schedule_port(l_ptr, msg_origport(msg), size);
  693. kfree_skb(buf);
  694. return -ELINKCONG;
  695. }
  696. kfree_skb(buf);
  697. if (imp > CONN_MANAGER) {
  698. pr_warn("%s<%s>, send queue full", link_rst_msg,
  699. l_ptr->name);
  700. tipc_link_reset(l_ptr);
  701. }
  702. return dsz;
  703. }
  704. /* Fragmentation needed ? */
  705. if (size > max_packet)
  706. return link_send_long_buf(l_ptr, buf);
  707. /* Packet can be queued or sent. */
  708. if (likely(!tipc_bearer_blocked(l_ptr->b_ptr) &&
  709. !link_congested(l_ptr))) {
  710. link_add_to_outqueue(l_ptr, buf, msg);
  711. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  712. l_ptr->unacked_window = 0;
  713. return dsz;
  714. }
  715. /* Congestion: can message be bundled ? */
  716. if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
  717. (msg_user(msg) != MSG_FRAGMENTER)) {
  718. /* Try adding message to an existing bundle */
  719. if (l_ptr->next_out &&
  720. link_bundle_buf(l_ptr, l_ptr->last_out, buf))
  721. return dsz;
  722. /* Try creating a new bundle */
  723. if (size <= max_packet * 2 / 3) {
  724. struct sk_buff *bundler = tipc_buf_acquire(max_packet);
  725. struct tipc_msg bundler_hdr;
  726. if (bundler) {
  727. tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
  728. INT_H_SIZE, l_ptr->addr);
  729. skb_copy_to_linear_data(bundler, &bundler_hdr,
  730. INT_H_SIZE);
  731. skb_trim(bundler, INT_H_SIZE);
  732. link_bundle_buf(l_ptr, bundler, buf);
  733. buf = bundler;
  734. msg = buf_msg(buf);
  735. l_ptr->stats.sent_bundles++;
  736. }
  737. }
  738. }
  739. if (!l_ptr->next_out)
  740. l_ptr->next_out = buf;
  741. link_add_to_outqueue(l_ptr, buf, msg);
  742. return dsz;
  743. }
  744. /*
  745. * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
  746. * not been selected yet, and the the owner node is not locked
  747. * Called by TIPC internal users, e.g. the name distributor
  748. */
  749. int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
  750. {
  751. struct tipc_link *l_ptr;
  752. struct tipc_node *n_ptr;
  753. int res = -ELINKCONG;
  754. read_lock_bh(&tipc_net_lock);
  755. n_ptr = tipc_node_find(dest);
  756. if (n_ptr) {
  757. tipc_node_lock(n_ptr);
  758. l_ptr = n_ptr->active_links[selector & 1];
  759. if (l_ptr)
  760. res = tipc_link_send_buf(l_ptr, buf);
  761. else
  762. kfree_skb(buf);
  763. tipc_node_unlock(n_ptr);
  764. } else {
  765. kfree_skb(buf);
  766. }
  767. read_unlock_bh(&tipc_net_lock);
  768. return res;
  769. }
  770. /*
  771. * tipc_link_send_sync - synchronize broadcast link endpoints.
  772. *
  773. * Give a newly added peer node the sequence number where it should
  774. * start receiving and acking broadcast packets.
  775. *
  776. * Called with node locked
  777. */
  778. static void tipc_link_send_sync(struct tipc_link *l)
  779. {
  780. struct sk_buff *buf;
  781. struct tipc_msg *msg;
  782. buf = tipc_buf_acquire(INT_H_SIZE);
  783. if (!buf)
  784. return;
  785. msg = buf_msg(buf);
  786. tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
  787. msg_set_last_bcast(msg, l->owner->bclink.acked);
  788. link_add_chain_to_outqueue(l, buf, 0);
  789. tipc_link_push_queue(l);
  790. }
  791. /*
  792. * tipc_link_recv_sync - synchronize broadcast link endpoints.
  793. * Receive the sequence number where we should start receiving and
  794. * acking broadcast packets from a newly added peer node, and open
  795. * up for reception of such packets.
  796. *
  797. * Called with node locked
  798. */
  799. static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
  800. {
  801. struct tipc_msg *msg = buf_msg(buf);
  802. n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
  803. n->bclink.recv_permitted = true;
  804. kfree_skb(buf);
  805. }
  806. /*
  807. * tipc_link_send_names - send name table entries to new neighbor
  808. *
  809. * Send routine for bulk delivery of name table messages when contact
  810. * with a new neighbor occurs. No link congestion checking is performed
  811. * because name table messages *must* be delivered. The messages must be
  812. * small enough not to require fragmentation.
  813. * Called without any locks held.
  814. */
  815. void tipc_link_send_names(struct list_head *message_list, u32 dest)
  816. {
  817. struct tipc_node *n_ptr;
  818. struct tipc_link *l_ptr;
  819. struct sk_buff *buf;
  820. struct sk_buff *temp_buf;
  821. if (list_empty(message_list))
  822. return;
  823. read_lock_bh(&tipc_net_lock);
  824. n_ptr = tipc_node_find(dest);
  825. if (n_ptr) {
  826. tipc_node_lock(n_ptr);
  827. l_ptr = n_ptr->active_links[0];
  828. if (l_ptr) {
  829. /* convert circular list to linear list */
  830. ((struct sk_buff *)message_list->prev)->next = NULL;
  831. link_add_chain_to_outqueue(l_ptr,
  832. (struct sk_buff *)message_list->next, 0);
  833. tipc_link_push_queue(l_ptr);
  834. INIT_LIST_HEAD(message_list);
  835. }
  836. tipc_node_unlock(n_ptr);
  837. }
  838. read_unlock_bh(&tipc_net_lock);
  839. /* discard the messages if they couldn't be sent */
  840. list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
  841. list_del((struct list_head *)buf);
  842. kfree_skb(buf);
  843. }
  844. }
  845. /*
  846. * link_send_buf_fast: Entry for data messages where the
  847. * destination link is known and the header is complete,
  848. * inclusive total message length. Very time critical.
  849. * Link is locked. Returns user data length.
  850. */
  851. static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
  852. u32 *used_max_pkt)
  853. {
  854. struct tipc_msg *msg = buf_msg(buf);
  855. int res = msg_data_sz(msg);
  856. if (likely(!link_congested(l_ptr))) {
  857. if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
  858. if (likely(!tipc_bearer_blocked(l_ptr->b_ptr))) {
  859. link_add_to_outqueue(l_ptr, buf, msg);
  860. tipc_bearer_send(l_ptr->b_ptr, buf,
  861. &l_ptr->media_addr);
  862. l_ptr->unacked_window = 0;
  863. return res;
  864. }
  865. } else
  866. *used_max_pkt = l_ptr->max_pkt;
  867. }
  868. return tipc_link_send_buf(l_ptr, buf); /* All other cases */
  869. }
  870. /*
  871. * tipc_link_send_sections_fast: Entry for messages where the
  872. * destination processor is known and the header is complete,
  873. * except for total message length.
  874. * Returns user data length or errno.
  875. */
  876. int tipc_link_send_sections_fast(struct tipc_port *sender,
  877. struct iovec const *msg_sect,
  878. unsigned int len, u32 destaddr)
  879. {
  880. struct tipc_msg *hdr = &sender->phdr;
  881. struct tipc_link *l_ptr;
  882. struct sk_buff *buf;
  883. struct tipc_node *node;
  884. int res;
  885. u32 selector = msg_origport(hdr) & 1;
  886. again:
  887. /*
  888. * Try building message using port's max_pkt hint.
  889. * (Must not hold any locks while building message.)
  890. */
  891. res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
  892. /* Exit if build request was invalid */
  893. if (unlikely(res < 0))
  894. return res;
  895. read_lock_bh(&tipc_net_lock);
  896. node = tipc_node_find(destaddr);
  897. if (likely(node)) {
  898. tipc_node_lock(node);
  899. l_ptr = node->active_links[selector];
  900. if (likely(l_ptr)) {
  901. if (likely(buf)) {
  902. res = link_send_buf_fast(l_ptr, buf,
  903. &sender->max_pkt);
  904. exit:
  905. tipc_node_unlock(node);
  906. read_unlock_bh(&tipc_net_lock);
  907. return res;
  908. }
  909. /* Exit if link (or bearer) is congested */
  910. if (link_congested(l_ptr) ||
  911. tipc_bearer_blocked(l_ptr->b_ptr)) {
  912. res = link_schedule_port(l_ptr,
  913. sender->ref, res);
  914. goto exit;
  915. }
  916. /*
  917. * Message size exceeds max_pkt hint; update hint,
  918. * then re-try fast path or fragment the message
  919. */
  920. sender->max_pkt = l_ptr->max_pkt;
  921. tipc_node_unlock(node);
  922. read_unlock_bh(&tipc_net_lock);
  923. if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
  924. goto again;
  925. return link_send_sections_long(sender, msg_sect, len,
  926. destaddr);
  927. }
  928. tipc_node_unlock(node);
  929. }
  930. read_unlock_bh(&tipc_net_lock);
  931. /* Couldn't find a link to the destination node */
  932. if (buf)
  933. return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
  934. if (res >= 0)
  935. return tipc_port_reject_sections(sender, hdr, msg_sect,
  936. len, TIPC_ERR_NO_NODE);
  937. return res;
  938. }
  939. /*
  940. * link_send_sections_long(): Entry for long messages where the
  941. * destination node is known and the header is complete,
  942. * inclusive total message length.
  943. * Link and bearer congestion status have been checked to be ok,
  944. * and are ignored if they change.
  945. *
  946. * Note that fragments do not use the full link MTU so that they won't have
  947. * to undergo refragmentation if link changeover causes them to be sent
  948. * over another link with an additional tunnel header added as prefix.
  949. * (Refragmentation will still occur if the other link has a smaller MTU.)
  950. *
  951. * Returns user data length or errno.
  952. */
  953. static int link_send_sections_long(struct tipc_port *sender,
  954. struct iovec const *msg_sect,
  955. unsigned int len, u32 destaddr)
  956. {
  957. struct tipc_link *l_ptr;
  958. struct tipc_node *node;
  959. struct tipc_msg *hdr = &sender->phdr;
  960. u32 dsz = len;
  961. u32 max_pkt, fragm_sz, rest;
  962. struct tipc_msg fragm_hdr;
  963. struct sk_buff *buf, *buf_chain, *prev;
  964. u32 fragm_crs, fragm_rest, hsz, sect_rest;
  965. const unchar __user *sect_crs;
  966. int curr_sect;
  967. u32 fragm_no;
  968. int res = 0;
  969. again:
  970. fragm_no = 1;
  971. max_pkt = sender->max_pkt - INT_H_SIZE;
  972. /* leave room for tunnel header in case of link changeover */
  973. fragm_sz = max_pkt - INT_H_SIZE;
  974. /* leave room for fragmentation header in each fragment */
  975. rest = dsz;
  976. fragm_crs = 0;
  977. fragm_rest = 0;
  978. sect_rest = 0;
  979. sect_crs = NULL;
  980. curr_sect = -1;
  981. /* Prepare reusable fragment header */
  982. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  983. INT_H_SIZE, msg_destnode(hdr));
  984. msg_set_size(&fragm_hdr, max_pkt);
  985. msg_set_fragm_no(&fragm_hdr, 1);
  986. /* Prepare header of first fragment */
  987. buf_chain = buf = tipc_buf_acquire(max_pkt);
  988. if (!buf)
  989. return -ENOMEM;
  990. buf->next = NULL;
  991. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  992. hsz = msg_hdr_sz(hdr);
  993. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
  994. /* Chop up message */
  995. fragm_crs = INT_H_SIZE + hsz;
  996. fragm_rest = fragm_sz - hsz;
  997. do { /* For all sections */
  998. u32 sz;
  999. if (!sect_rest) {
  1000. sect_rest = msg_sect[++curr_sect].iov_len;
  1001. sect_crs = msg_sect[curr_sect].iov_base;
  1002. }
  1003. if (sect_rest < fragm_rest)
  1004. sz = sect_rest;
  1005. else
  1006. sz = fragm_rest;
  1007. if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
  1008. res = -EFAULT;
  1009. error:
  1010. for (; buf_chain; buf_chain = buf) {
  1011. buf = buf_chain->next;
  1012. kfree_skb(buf_chain);
  1013. }
  1014. return res;
  1015. }
  1016. sect_crs += sz;
  1017. sect_rest -= sz;
  1018. fragm_crs += sz;
  1019. fragm_rest -= sz;
  1020. rest -= sz;
  1021. if (!fragm_rest && rest) {
  1022. /* Initiate new fragment: */
  1023. if (rest <= fragm_sz) {
  1024. fragm_sz = rest;
  1025. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  1026. } else {
  1027. msg_set_type(&fragm_hdr, FRAGMENT);
  1028. }
  1029. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  1030. msg_set_fragm_no(&fragm_hdr, ++fragm_no);
  1031. prev = buf;
  1032. buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  1033. if (!buf) {
  1034. res = -ENOMEM;
  1035. goto error;
  1036. }
  1037. buf->next = NULL;
  1038. prev->next = buf;
  1039. skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
  1040. fragm_crs = INT_H_SIZE;
  1041. fragm_rest = fragm_sz;
  1042. }
  1043. } while (rest > 0);
  1044. /*
  1045. * Now we have a buffer chain. Select a link and check
  1046. * that packet size is still OK
  1047. */
  1048. node = tipc_node_find(destaddr);
  1049. if (likely(node)) {
  1050. tipc_node_lock(node);
  1051. l_ptr = node->active_links[sender->ref & 1];
  1052. if (!l_ptr) {
  1053. tipc_node_unlock(node);
  1054. goto reject;
  1055. }
  1056. if (l_ptr->max_pkt < max_pkt) {
  1057. sender->max_pkt = l_ptr->max_pkt;
  1058. tipc_node_unlock(node);
  1059. for (; buf_chain; buf_chain = buf) {
  1060. buf = buf_chain->next;
  1061. kfree_skb(buf_chain);
  1062. }
  1063. goto again;
  1064. }
  1065. } else {
  1066. reject:
  1067. for (; buf_chain; buf_chain = buf) {
  1068. buf = buf_chain->next;
  1069. kfree_skb(buf_chain);
  1070. }
  1071. return tipc_port_reject_sections(sender, hdr, msg_sect,
  1072. len, TIPC_ERR_NO_NODE);
  1073. }
  1074. /* Append chain of fragments to send queue & send them */
  1075. l_ptr->long_msg_seq_no++;
  1076. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  1077. l_ptr->stats.sent_fragments += fragm_no;
  1078. l_ptr->stats.sent_fragmented++;
  1079. tipc_link_push_queue(l_ptr);
  1080. tipc_node_unlock(node);
  1081. return dsz;
  1082. }
  1083. /*
  1084. * tipc_link_push_packet: Push one unsent packet to the media
  1085. */
  1086. u32 tipc_link_push_packet(struct tipc_link *l_ptr)
  1087. {
  1088. struct sk_buff *buf = l_ptr->first_out;
  1089. u32 r_q_size = l_ptr->retransm_queue_size;
  1090. u32 r_q_head = l_ptr->retransm_queue_head;
  1091. /* Step to position where retransmission failed, if any, */
  1092. /* consider that buffers may have been released in meantime */
  1093. if (r_q_size && buf) {
  1094. u32 last = lesser(mod(r_q_head + r_q_size),
  1095. link_last_sent(l_ptr));
  1096. u32 first = buf_seqno(buf);
  1097. while (buf && less(first, r_q_head)) {
  1098. first = mod(first + 1);
  1099. buf = buf->next;
  1100. }
  1101. l_ptr->retransm_queue_head = r_q_head = first;
  1102. l_ptr->retransm_queue_size = r_q_size = mod(last - first);
  1103. }
  1104. /* Continue retransmission now, if there is anything: */
  1105. if (r_q_size && buf) {
  1106. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1107. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1108. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1109. l_ptr->retransm_queue_head = mod(++r_q_head);
  1110. l_ptr->retransm_queue_size = --r_q_size;
  1111. l_ptr->stats.retransmitted++;
  1112. return 0;
  1113. }
  1114. /* Send deferred protocol message, if any: */
  1115. buf = l_ptr->proto_msg_queue;
  1116. if (buf) {
  1117. msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
  1118. msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
  1119. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1120. l_ptr->unacked_window = 0;
  1121. kfree_skb(buf);
  1122. l_ptr->proto_msg_queue = NULL;
  1123. return 0;
  1124. }
  1125. /* Send one deferred data message, if send window not full: */
  1126. buf = l_ptr->next_out;
  1127. if (buf) {
  1128. struct tipc_msg *msg = buf_msg(buf);
  1129. u32 next = msg_seqno(msg);
  1130. u32 first = buf_seqno(l_ptr->first_out);
  1131. if (mod(next - first) < l_ptr->queue_limit[0]) {
  1132. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1133. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1134. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1135. if (msg_user(msg) == MSG_BUNDLER)
  1136. msg_set_type(msg, CLOSED_MSG);
  1137. l_ptr->next_out = buf->next;
  1138. return 0;
  1139. }
  1140. }
  1141. return 1;
  1142. }
  1143. /*
  1144. * push_queue(): push out the unsent messages of a link where
  1145. * congestion has abated. Node is locked
  1146. */
  1147. void tipc_link_push_queue(struct tipc_link *l_ptr)
  1148. {
  1149. u32 res;
  1150. if (tipc_bearer_blocked(l_ptr->b_ptr))
  1151. return;
  1152. do {
  1153. res = tipc_link_push_packet(l_ptr);
  1154. } while (!res);
  1155. }
  1156. static void link_reset_all(unsigned long addr)
  1157. {
  1158. struct tipc_node *n_ptr;
  1159. char addr_string[16];
  1160. u32 i;
  1161. read_lock_bh(&tipc_net_lock);
  1162. n_ptr = tipc_node_find((u32)addr);
  1163. if (!n_ptr) {
  1164. read_unlock_bh(&tipc_net_lock);
  1165. return; /* node no longer exists */
  1166. }
  1167. tipc_node_lock(n_ptr);
  1168. pr_warn("Resetting all links to %s\n",
  1169. tipc_addr_string_fill(addr_string, n_ptr->addr));
  1170. for (i = 0; i < MAX_BEARERS; i++) {
  1171. if (n_ptr->links[i]) {
  1172. link_print(n_ptr->links[i], "Resetting link\n");
  1173. tipc_link_reset(n_ptr->links[i]);
  1174. }
  1175. }
  1176. tipc_node_unlock(n_ptr);
  1177. read_unlock_bh(&tipc_net_lock);
  1178. }
  1179. static void link_retransmit_failure(struct tipc_link *l_ptr,
  1180. struct sk_buff *buf)
  1181. {
  1182. struct tipc_msg *msg = buf_msg(buf);
  1183. pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
  1184. if (l_ptr->addr) {
  1185. /* Handle failure on standard link */
  1186. link_print(l_ptr, "Resetting link\n");
  1187. tipc_link_reset(l_ptr);
  1188. } else {
  1189. /* Handle failure on broadcast link */
  1190. struct tipc_node *n_ptr;
  1191. char addr_string[16];
  1192. pr_info("Msg seq number: %u, ", msg_seqno(msg));
  1193. pr_cont("Outstanding acks: %lu\n",
  1194. (unsigned long) TIPC_SKB_CB(buf)->handle);
  1195. n_ptr = tipc_bclink_retransmit_to();
  1196. tipc_node_lock(n_ptr);
  1197. tipc_addr_string_fill(addr_string, n_ptr->addr);
  1198. pr_info("Broadcast link info for %s\n", addr_string);
  1199. pr_info("Reception permitted: %d, Acked: %u\n",
  1200. n_ptr->bclink.recv_permitted,
  1201. n_ptr->bclink.acked);
  1202. pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
  1203. n_ptr->bclink.last_in,
  1204. n_ptr->bclink.oos_state,
  1205. n_ptr->bclink.last_sent);
  1206. tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
  1207. tipc_node_unlock(n_ptr);
  1208. l_ptr->stale_count = 0;
  1209. }
  1210. }
  1211. void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
  1212. u32 retransmits)
  1213. {
  1214. struct tipc_msg *msg;
  1215. if (!buf)
  1216. return;
  1217. msg = buf_msg(buf);
  1218. if (tipc_bearer_blocked(l_ptr->b_ptr)) {
  1219. if (l_ptr->retransm_queue_size == 0) {
  1220. l_ptr->retransm_queue_head = msg_seqno(msg);
  1221. l_ptr->retransm_queue_size = retransmits;
  1222. } else {
  1223. pr_err("Unexpected retransmit on link %s (qsize=%d)\n",
  1224. l_ptr->name, l_ptr->retransm_queue_size);
  1225. }
  1226. return;
  1227. } else {
  1228. /* Detect repeated retransmit failures on unblocked bearer */
  1229. if (l_ptr->last_retransmitted == msg_seqno(msg)) {
  1230. if (++l_ptr->stale_count > 100) {
  1231. link_retransmit_failure(l_ptr, buf);
  1232. return;
  1233. }
  1234. } else {
  1235. l_ptr->last_retransmitted = msg_seqno(msg);
  1236. l_ptr->stale_count = 1;
  1237. }
  1238. }
  1239. while (retransmits && (buf != l_ptr->next_out) && buf) {
  1240. msg = buf_msg(buf);
  1241. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1242. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1243. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1244. buf = buf->next;
  1245. retransmits--;
  1246. l_ptr->stats.retransmitted++;
  1247. }
  1248. l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
  1249. }
  1250. /**
  1251. * link_insert_deferred_queue - insert deferred messages back into receive chain
  1252. */
  1253. static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
  1254. struct sk_buff *buf)
  1255. {
  1256. u32 seq_no;
  1257. if (l_ptr->oldest_deferred_in == NULL)
  1258. return buf;
  1259. seq_no = buf_seqno(l_ptr->oldest_deferred_in);
  1260. if (seq_no == mod(l_ptr->next_in_no)) {
  1261. l_ptr->newest_deferred_in->next = buf;
  1262. buf = l_ptr->oldest_deferred_in;
  1263. l_ptr->oldest_deferred_in = NULL;
  1264. l_ptr->deferred_inqueue_sz = 0;
  1265. }
  1266. return buf;
  1267. }
  1268. /**
  1269. * link_recv_buf_validate - validate basic format of received message
  1270. *
  1271. * This routine ensures a TIPC message has an acceptable header, and at least
  1272. * as much data as the header indicates it should. The routine also ensures
  1273. * that the entire message header is stored in the main fragment of the message
  1274. * buffer, to simplify future access to message header fields.
  1275. *
  1276. * Note: Having extra info present in the message header or data areas is OK.
  1277. * TIPC will ignore the excess, under the assumption that it is optional info
  1278. * introduced by a later release of the protocol.
  1279. */
  1280. static int link_recv_buf_validate(struct sk_buff *buf)
  1281. {
  1282. static u32 min_data_hdr_size[8] = {
  1283. SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
  1284. MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
  1285. };
  1286. struct tipc_msg *msg;
  1287. u32 tipc_hdr[2];
  1288. u32 size;
  1289. u32 hdr_size;
  1290. u32 min_hdr_size;
  1291. if (unlikely(buf->len < MIN_H_SIZE))
  1292. return 0;
  1293. msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
  1294. if (msg == NULL)
  1295. return 0;
  1296. if (unlikely(msg_version(msg) != TIPC_VERSION))
  1297. return 0;
  1298. size = msg_size(msg);
  1299. hdr_size = msg_hdr_sz(msg);
  1300. min_hdr_size = msg_isdata(msg) ?
  1301. min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
  1302. if (unlikely((hdr_size < min_hdr_size) ||
  1303. (size < hdr_size) ||
  1304. (buf->len < size) ||
  1305. (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
  1306. return 0;
  1307. return pskb_may_pull(buf, hdr_size);
  1308. }
  1309. /**
  1310. * tipc_recv_msg - process TIPC messages arriving from off-node
  1311. * @head: pointer to message buffer chain
  1312. * @tb_ptr: pointer to bearer message arrived on
  1313. *
  1314. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1315. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1316. */
  1317. void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
  1318. {
  1319. read_lock_bh(&tipc_net_lock);
  1320. while (head) {
  1321. struct tipc_node *n_ptr;
  1322. struct tipc_link *l_ptr;
  1323. struct sk_buff *crs;
  1324. struct sk_buff *buf = head;
  1325. struct tipc_msg *msg;
  1326. u32 seq_no;
  1327. u32 ackd;
  1328. u32 released = 0;
  1329. int type;
  1330. head = head->next;
  1331. /* Ensure bearer is still enabled */
  1332. if (unlikely(!b_ptr->active))
  1333. goto discard;
  1334. /* Ensure message is well-formed */
  1335. if (unlikely(!link_recv_buf_validate(buf)))
  1336. goto discard;
  1337. /* Ensure message data is a single contiguous unit */
  1338. if (unlikely(skb_linearize(buf)))
  1339. goto discard;
  1340. /* Handle arrival of a non-unicast link message */
  1341. msg = buf_msg(buf);
  1342. if (unlikely(msg_non_seq(msg))) {
  1343. if (msg_user(msg) == LINK_CONFIG)
  1344. tipc_disc_recv_msg(buf, b_ptr);
  1345. else
  1346. tipc_bclink_recv_pkt(buf);
  1347. continue;
  1348. }
  1349. /* Discard unicast link messages destined for another node */
  1350. if (unlikely(!msg_short(msg) &&
  1351. (msg_destnode(msg) != tipc_own_addr)))
  1352. goto discard;
  1353. /* Locate neighboring node that sent message */
  1354. n_ptr = tipc_node_find(msg_prevnode(msg));
  1355. if (unlikely(!n_ptr))
  1356. goto discard;
  1357. tipc_node_lock(n_ptr);
  1358. /* Locate unicast link endpoint that should handle message */
  1359. l_ptr = n_ptr->links[b_ptr->identity];
  1360. if (unlikely(!l_ptr))
  1361. goto unlock_discard;
  1362. /* Verify that communication with node is currently allowed */
  1363. if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
  1364. msg_user(msg) == LINK_PROTOCOL &&
  1365. (msg_type(msg) == RESET_MSG ||
  1366. msg_type(msg) == ACTIVATE_MSG) &&
  1367. !msg_redundant_link(msg))
  1368. n_ptr->block_setup &= ~WAIT_PEER_DOWN;
  1369. if (n_ptr->block_setup)
  1370. goto unlock_discard;
  1371. /* Validate message sequence number info */
  1372. seq_no = msg_seqno(msg);
  1373. ackd = msg_ack(msg);
  1374. /* Release acked messages */
  1375. if (n_ptr->bclink.recv_permitted)
  1376. tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
  1377. crs = l_ptr->first_out;
  1378. while ((crs != l_ptr->next_out) &&
  1379. less_eq(buf_seqno(crs), ackd)) {
  1380. struct sk_buff *next = crs->next;
  1381. kfree_skb(crs);
  1382. crs = next;
  1383. released++;
  1384. }
  1385. if (released) {
  1386. l_ptr->first_out = crs;
  1387. l_ptr->out_queue_size -= released;
  1388. }
  1389. /* Try sending any messages link endpoint has pending */
  1390. if (unlikely(l_ptr->next_out))
  1391. tipc_link_push_queue(l_ptr);
  1392. if (unlikely(!list_empty(&l_ptr->waiting_ports)))
  1393. tipc_link_wakeup_ports(l_ptr, 0);
  1394. if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
  1395. l_ptr->stats.sent_acks++;
  1396. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1397. }
  1398. /* Now (finally!) process the incoming message */
  1399. protocol_check:
  1400. if (unlikely(!link_working_working(l_ptr))) {
  1401. if (msg_user(msg) == LINK_PROTOCOL) {
  1402. link_recv_proto_msg(l_ptr, buf);
  1403. head = link_insert_deferred_queue(l_ptr, head);
  1404. tipc_node_unlock(n_ptr);
  1405. continue;
  1406. }
  1407. /* Traffic message. Conditionally activate link */
  1408. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1409. if (link_working_working(l_ptr)) {
  1410. /* Re-insert buffer in front of queue */
  1411. buf->next = head;
  1412. head = buf;
  1413. tipc_node_unlock(n_ptr);
  1414. continue;
  1415. }
  1416. goto unlock_discard;
  1417. }
  1418. /* Link is now in state WORKING_WORKING */
  1419. if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
  1420. link_handle_out_of_seq_msg(l_ptr, buf);
  1421. head = link_insert_deferred_queue(l_ptr, head);
  1422. tipc_node_unlock(n_ptr);
  1423. continue;
  1424. }
  1425. l_ptr->next_in_no++;
  1426. if (unlikely(l_ptr->oldest_deferred_in))
  1427. head = link_insert_deferred_queue(l_ptr, head);
  1428. deliver:
  1429. if (likely(msg_isdata(msg))) {
  1430. tipc_node_unlock(n_ptr);
  1431. tipc_port_recv_msg(buf);
  1432. continue;
  1433. }
  1434. switch (msg_user(msg)) {
  1435. int ret;
  1436. case MSG_BUNDLER:
  1437. l_ptr->stats.recv_bundles++;
  1438. l_ptr->stats.recv_bundled += msg_msgcnt(msg);
  1439. tipc_node_unlock(n_ptr);
  1440. tipc_link_recv_bundle(buf);
  1441. continue;
  1442. case NAME_DISTRIBUTOR:
  1443. n_ptr->bclink.recv_permitted = true;
  1444. tipc_node_unlock(n_ptr);
  1445. tipc_named_recv(buf);
  1446. continue;
  1447. case BCAST_PROTOCOL:
  1448. tipc_link_recv_sync(n_ptr, buf);
  1449. tipc_node_unlock(n_ptr);
  1450. continue;
  1451. case CONN_MANAGER:
  1452. tipc_node_unlock(n_ptr);
  1453. tipc_port_recv_proto_msg(buf);
  1454. continue;
  1455. case MSG_FRAGMENTER:
  1456. l_ptr->stats.recv_fragments++;
  1457. ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
  1458. &l_ptr->reasm_tail,
  1459. &buf);
  1460. if (ret == LINK_REASM_COMPLETE) {
  1461. l_ptr->stats.recv_fragmented++;
  1462. msg = buf_msg(buf);
  1463. goto deliver;
  1464. }
  1465. if (ret == LINK_REASM_ERROR)
  1466. tipc_link_reset(l_ptr);
  1467. tipc_node_unlock(n_ptr);
  1468. continue;
  1469. case CHANGEOVER_PROTOCOL:
  1470. type = msg_type(msg);
  1471. if (link_recv_changeover_msg(&l_ptr, &buf)) {
  1472. msg = buf_msg(buf);
  1473. seq_no = msg_seqno(msg);
  1474. if (type == ORIGINAL_MSG)
  1475. goto deliver;
  1476. goto protocol_check;
  1477. }
  1478. break;
  1479. default:
  1480. kfree_skb(buf);
  1481. buf = NULL;
  1482. break;
  1483. }
  1484. tipc_node_unlock(n_ptr);
  1485. tipc_net_route_msg(buf);
  1486. continue;
  1487. unlock_discard:
  1488. tipc_node_unlock(n_ptr);
  1489. discard:
  1490. kfree_skb(buf);
  1491. }
  1492. read_unlock_bh(&tipc_net_lock);
  1493. }
  1494. /**
  1495. * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
  1496. *
  1497. * Returns increase in queue length (i.e. 0 or 1)
  1498. */
  1499. u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
  1500. struct sk_buff *buf)
  1501. {
  1502. struct sk_buff *queue_buf;
  1503. struct sk_buff **prev;
  1504. u32 seq_no = buf_seqno(buf);
  1505. buf->next = NULL;
  1506. /* Empty queue ? */
  1507. if (*head == NULL) {
  1508. *head = *tail = buf;
  1509. return 1;
  1510. }
  1511. /* Last ? */
  1512. if (less(buf_seqno(*tail), seq_no)) {
  1513. (*tail)->next = buf;
  1514. *tail = buf;
  1515. return 1;
  1516. }
  1517. /* Locate insertion point in queue, then insert; discard if duplicate */
  1518. prev = head;
  1519. queue_buf = *head;
  1520. for (;;) {
  1521. u32 curr_seqno = buf_seqno(queue_buf);
  1522. if (seq_no == curr_seqno) {
  1523. kfree_skb(buf);
  1524. return 0;
  1525. }
  1526. if (less(seq_no, curr_seqno))
  1527. break;
  1528. prev = &queue_buf->next;
  1529. queue_buf = queue_buf->next;
  1530. }
  1531. buf->next = queue_buf;
  1532. *prev = buf;
  1533. return 1;
  1534. }
  1535. /*
  1536. * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
  1537. */
  1538. static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
  1539. struct sk_buff *buf)
  1540. {
  1541. u32 seq_no = buf_seqno(buf);
  1542. if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
  1543. link_recv_proto_msg(l_ptr, buf);
  1544. return;
  1545. }
  1546. /* Record OOS packet arrival (force mismatch on next timeout) */
  1547. l_ptr->checkpoint--;
  1548. /*
  1549. * Discard packet if a duplicate; otherwise add it to deferred queue
  1550. * and notify peer of gap as per protocol specification
  1551. */
  1552. if (less(seq_no, mod(l_ptr->next_in_no))) {
  1553. l_ptr->stats.duplicates++;
  1554. kfree_skb(buf);
  1555. return;
  1556. }
  1557. if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
  1558. &l_ptr->newest_deferred_in, buf)) {
  1559. l_ptr->deferred_inqueue_sz++;
  1560. l_ptr->stats.deferred_recv++;
  1561. if ((l_ptr->deferred_inqueue_sz % 16) == 1)
  1562. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  1563. } else
  1564. l_ptr->stats.duplicates++;
  1565. }
  1566. /*
  1567. * Send protocol message to the other endpoint.
  1568. */
  1569. void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
  1570. int probe_msg, u32 gap, u32 tolerance,
  1571. u32 priority, u32 ack_mtu)
  1572. {
  1573. struct sk_buff *buf = NULL;
  1574. struct tipc_msg *msg = l_ptr->pmsg;
  1575. u32 msg_size = sizeof(l_ptr->proto_msg);
  1576. int r_flag;
  1577. /* Discard any previous message that was deferred due to congestion */
  1578. if (l_ptr->proto_msg_queue) {
  1579. kfree_skb(l_ptr->proto_msg_queue);
  1580. l_ptr->proto_msg_queue = NULL;
  1581. }
  1582. if (link_blocked(l_ptr))
  1583. return;
  1584. /* Abort non-RESET send if communication with node is prohibited */
  1585. if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
  1586. return;
  1587. /* Create protocol message with "out-of-sequence" sequence number */
  1588. msg_set_type(msg, msg_typ);
  1589. msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
  1590. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1591. msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
  1592. if (msg_typ == STATE_MSG) {
  1593. u32 next_sent = mod(l_ptr->next_out_no);
  1594. if (!tipc_link_is_up(l_ptr))
  1595. return;
  1596. if (l_ptr->next_out)
  1597. next_sent = buf_seqno(l_ptr->next_out);
  1598. msg_set_next_sent(msg, next_sent);
  1599. if (l_ptr->oldest_deferred_in) {
  1600. u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
  1601. gap = mod(rec - mod(l_ptr->next_in_no));
  1602. }
  1603. msg_set_seq_gap(msg, gap);
  1604. if (gap)
  1605. l_ptr->stats.sent_nacks++;
  1606. msg_set_link_tolerance(msg, tolerance);
  1607. msg_set_linkprio(msg, priority);
  1608. msg_set_max_pkt(msg, ack_mtu);
  1609. msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
  1610. msg_set_probe(msg, probe_msg != 0);
  1611. if (probe_msg) {
  1612. u32 mtu = l_ptr->max_pkt;
  1613. if ((mtu < l_ptr->max_pkt_target) &&
  1614. link_working_working(l_ptr) &&
  1615. l_ptr->fsm_msg_cnt) {
  1616. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1617. if (l_ptr->max_pkt_probes == 10) {
  1618. l_ptr->max_pkt_target = (msg_size - 4);
  1619. l_ptr->max_pkt_probes = 0;
  1620. msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
  1621. }
  1622. l_ptr->max_pkt_probes++;
  1623. }
  1624. l_ptr->stats.sent_probes++;
  1625. }
  1626. l_ptr->stats.sent_states++;
  1627. } else { /* RESET_MSG or ACTIVATE_MSG */
  1628. msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
  1629. msg_set_seq_gap(msg, 0);
  1630. msg_set_next_sent(msg, 1);
  1631. msg_set_probe(msg, 0);
  1632. msg_set_link_tolerance(msg, l_ptr->tolerance);
  1633. msg_set_linkprio(msg, l_ptr->priority);
  1634. msg_set_max_pkt(msg, l_ptr->max_pkt_target);
  1635. }
  1636. r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
  1637. msg_set_redundant_link(msg, r_flag);
  1638. msg_set_linkprio(msg, l_ptr->priority);
  1639. msg_set_size(msg, msg_size);
  1640. msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
  1641. buf = tipc_buf_acquire(msg_size);
  1642. if (!buf)
  1643. return;
  1644. skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
  1645. buf->priority = TC_PRIO_CONTROL;
  1646. /* Defer message if bearer is already blocked */
  1647. if (tipc_bearer_blocked(l_ptr->b_ptr)) {
  1648. l_ptr->proto_msg_queue = buf;
  1649. return;
  1650. }
  1651. tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
  1652. l_ptr->unacked_window = 0;
  1653. kfree_skb(buf);
  1654. }
  1655. /*
  1656. * Receive protocol message :
  1657. * Note that network plane id propagates through the network, and may
  1658. * change at any time. The node with lowest address rules
  1659. */
  1660. static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
  1661. {
  1662. u32 rec_gap = 0;
  1663. u32 max_pkt_info;
  1664. u32 max_pkt_ack;
  1665. u32 msg_tol;
  1666. struct tipc_msg *msg = buf_msg(buf);
  1667. if (link_blocked(l_ptr))
  1668. goto exit;
  1669. /* record unnumbered packet arrival (force mismatch on next timeout) */
  1670. l_ptr->checkpoint--;
  1671. if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
  1672. if (tipc_own_addr > msg_prevnode(msg))
  1673. l_ptr->b_ptr->net_plane = msg_net_plane(msg);
  1674. l_ptr->owner->permit_changeover = msg_redundant_link(msg);
  1675. switch (msg_type(msg)) {
  1676. case RESET_MSG:
  1677. if (!link_working_unknown(l_ptr) &&
  1678. (l_ptr->peer_session != INVALID_SESSION)) {
  1679. if (less_eq(msg_session(msg), l_ptr->peer_session))
  1680. break; /* duplicate or old reset: ignore */
  1681. }
  1682. if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
  1683. link_working_unknown(l_ptr))) {
  1684. /*
  1685. * peer has lost contact -- don't allow peer's links
  1686. * to reactivate before we recognize loss & clean up
  1687. */
  1688. l_ptr->owner->block_setup = WAIT_NODE_DOWN;
  1689. }
  1690. link_state_event(l_ptr, RESET_MSG);
  1691. /* fall thru' */
  1692. case ACTIVATE_MSG:
  1693. /* Update link settings according other endpoint's values */
  1694. strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
  1695. msg_tol = msg_link_tolerance(msg);
  1696. if (msg_tol > l_ptr->tolerance)
  1697. link_set_supervision_props(l_ptr, msg_tol);
  1698. if (msg_linkprio(msg) > l_ptr->priority)
  1699. l_ptr->priority = msg_linkprio(msg);
  1700. max_pkt_info = msg_max_pkt(msg);
  1701. if (max_pkt_info) {
  1702. if (max_pkt_info < l_ptr->max_pkt_target)
  1703. l_ptr->max_pkt_target = max_pkt_info;
  1704. if (l_ptr->max_pkt > l_ptr->max_pkt_target)
  1705. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1706. } else {
  1707. l_ptr->max_pkt = l_ptr->max_pkt_target;
  1708. }
  1709. /* Synchronize broadcast link info, if not done previously */
  1710. if (!tipc_node_is_up(l_ptr->owner)) {
  1711. l_ptr->owner->bclink.last_sent =
  1712. l_ptr->owner->bclink.last_in =
  1713. msg_last_bcast(msg);
  1714. l_ptr->owner->bclink.oos_state = 0;
  1715. }
  1716. l_ptr->peer_session = msg_session(msg);
  1717. l_ptr->peer_bearer_id = msg_bearer_id(msg);
  1718. if (msg_type(msg) == ACTIVATE_MSG)
  1719. link_state_event(l_ptr, ACTIVATE_MSG);
  1720. break;
  1721. case STATE_MSG:
  1722. msg_tol = msg_link_tolerance(msg);
  1723. if (msg_tol)
  1724. link_set_supervision_props(l_ptr, msg_tol);
  1725. if (msg_linkprio(msg) &&
  1726. (msg_linkprio(msg) != l_ptr->priority)) {
  1727. pr_warn("%s<%s>, priority change %u->%u\n",
  1728. link_rst_msg, l_ptr->name, l_ptr->priority,
  1729. msg_linkprio(msg));
  1730. l_ptr->priority = msg_linkprio(msg);
  1731. tipc_link_reset(l_ptr); /* Enforce change to take effect */
  1732. break;
  1733. }
  1734. link_state_event(l_ptr, TRAFFIC_MSG_EVT);
  1735. l_ptr->stats.recv_states++;
  1736. if (link_reset_unknown(l_ptr))
  1737. break;
  1738. if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
  1739. rec_gap = mod(msg_next_sent(msg) -
  1740. mod(l_ptr->next_in_no));
  1741. }
  1742. max_pkt_ack = msg_max_pkt(msg);
  1743. if (max_pkt_ack > l_ptr->max_pkt) {
  1744. l_ptr->max_pkt = max_pkt_ack;
  1745. l_ptr->max_pkt_probes = 0;
  1746. }
  1747. max_pkt_ack = 0;
  1748. if (msg_probe(msg)) {
  1749. l_ptr->stats.recv_probes++;
  1750. if (msg_size(msg) > sizeof(l_ptr->proto_msg))
  1751. max_pkt_ack = msg_size(msg);
  1752. }
  1753. /* Protocol message before retransmits, reduce loss risk */
  1754. if (l_ptr->owner->bclink.recv_permitted)
  1755. tipc_bclink_update_link_state(l_ptr->owner,
  1756. msg_last_bcast(msg));
  1757. if (rec_gap || (msg_probe(msg))) {
  1758. tipc_link_send_proto_msg(l_ptr, STATE_MSG,
  1759. 0, rec_gap, 0, 0, max_pkt_ack);
  1760. }
  1761. if (msg_seq_gap(msg)) {
  1762. l_ptr->stats.recv_nacks++;
  1763. tipc_link_retransmit(l_ptr, l_ptr->first_out,
  1764. msg_seq_gap(msg));
  1765. }
  1766. break;
  1767. }
  1768. exit:
  1769. kfree_skb(buf);
  1770. }
  1771. /*
  1772. * tipc_link_tunnel(): Send one message via a link belonging to
  1773. * another bearer. Owner node is locked.
  1774. */
  1775. static void tipc_link_tunnel(struct tipc_link *l_ptr,
  1776. struct tipc_msg *tunnel_hdr, struct tipc_msg *msg,
  1777. u32 selector)
  1778. {
  1779. struct tipc_link *tunnel;
  1780. struct sk_buff *buf;
  1781. u32 length = msg_size(msg);
  1782. tunnel = l_ptr->owner->active_links[selector & 1];
  1783. if (!tipc_link_is_up(tunnel)) {
  1784. pr_warn("%stunnel link no longer available\n", link_co_err);
  1785. return;
  1786. }
  1787. msg_set_size(tunnel_hdr, length + INT_H_SIZE);
  1788. buf = tipc_buf_acquire(length + INT_H_SIZE);
  1789. if (!buf) {
  1790. pr_warn("%sunable to send tunnel msg\n", link_co_err);
  1791. return;
  1792. }
  1793. skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
  1794. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
  1795. tipc_link_send_buf(tunnel, buf);
  1796. }
  1797. /*
  1798. * changeover(): Send whole message queue via the remaining link
  1799. * Owner node is locked.
  1800. */
  1801. void tipc_link_changeover(struct tipc_link *l_ptr)
  1802. {
  1803. u32 msgcount = l_ptr->out_queue_size;
  1804. struct sk_buff *crs = l_ptr->first_out;
  1805. struct tipc_link *tunnel = l_ptr->owner->active_links[0];
  1806. struct tipc_msg tunnel_hdr;
  1807. int split_bundles;
  1808. if (!tunnel)
  1809. return;
  1810. if (!l_ptr->owner->permit_changeover) {
  1811. pr_warn("%speer did not permit changeover\n", link_co_err);
  1812. return;
  1813. }
  1814. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1815. ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
  1816. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1817. msg_set_msgcnt(&tunnel_hdr, msgcount);
  1818. if (!l_ptr->first_out) {
  1819. struct sk_buff *buf;
  1820. buf = tipc_buf_acquire(INT_H_SIZE);
  1821. if (buf) {
  1822. skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
  1823. msg_set_size(&tunnel_hdr, INT_H_SIZE);
  1824. tipc_link_send_buf(tunnel, buf);
  1825. } else {
  1826. pr_warn("%sunable to send changeover msg\n",
  1827. link_co_err);
  1828. }
  1829. return;
  1830. }
  1831. split_bundles = (l_ptr->owner->active_links[0] !=
  1832. l_ptr->owner->active_links[1]);
  1833. while (crs) {
  1834. struct tipc_msg *msg = buf_msg(crs);
  1835. if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
  1836. struct tipc_msg *m = msg_get_wrapped(msg);
  1837. unchar *pos = (unchar *)m;
  1838. msgcount = msg_msgcnt(msg);
  1839. while (msgcount--) {
  1840. msg_set_seqno(m, msg_seqno(msg));
  1841. tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
  1842. msg_link_selector(m));
  1843. pos += align(msg_size(m));
  1844. m = (struct tipc_msg *)pos;
  1845. }
  1846. } else {
  1847. tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
  1848. msg_link_selector(msg));
  1849. }
  1850. crs = crs->next;
  1851. }
  1852. }
  1853. void tipc_link_send_duplicate(struct tipc_link *l_ptr, struct tipc_link *tunnel)
  1854. {
  1855. struct sk_buff *iter;
  1856. struct tipc_msg tunnel_hdr;
  1857. tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
  1858. DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
  1859. msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
  1860. msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
  1861. iter = l_ptr->first_out;
  1862. while (iter) {
  1863. struct sk_buff *outbuf;
  1864. struct tipc_msg *msg = buf_msg(iter);
  1865. u32 length = msg_size(msg);
  1866. if (msg_user(msg) == MSG_BUNDLER)
  1867. msg_set_type(msg, CLOSED_MSG);
  1868. msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
  1869. msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
  1870. msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
  1871. outbuf = tipc_buf_acquire(length + INT_H_SIZE);
  1872. if (outbuf == NULL) {
  1873. pr_warn("%sunable to send duplicate msg\n",
  1874. link_co_err);
  1875. return;
  1876. }
  1877. skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
  1878. skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
  1879. length);
  1880. tipc_link_send_buf(tunnel, outbuf);
  1881. if (!tipc_link_is_up(l_ptr))
  1882. return;
  1883. iter = iter->next;
  1884. }
  1885. }
  1886. /**
  1887. * buf_extract - extracts embedded TIPC message from another message
  1888. * @skb: encapsulating message buffer
  1889. * @from_pos: offset to extract from
  1890. *
  1891. * Returns a new message buffer containing an embedded message. The
  1892. * encapsulating message itself is left unchanged.
  1893. */
  1894. static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
  1895. {
  1896. struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
  1897. u32 size = msg_size(msg);
  1898. struct sk_buff *eb;
  1899. eb = tipc_buf_acquire(size);
  1900. if (eb)
  1901. skb_copy_to_linear_data(eb, msg, size);
  1902. return eb;
  1903. }
  1904. /*
  1905. * link_recv_changeover_msg(): Receive tunneled packet sent
  1906. * via other link. Node is locked. Return extracted buffer.
  1907. */
  1908. static int link_recv_changeover_msg(struct tipc_link **l_ptr,
  1909. struct sk_buff **buf)
  1910. {
  1911. struct sk_buff *tunnel_buf = *buf;
  1912. struct tipc_link *dest_link;
  1913. struct tipc_msg *msg;
  1914. struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
  1915. u32 msg_typ = msg_type(tunnel_msg);
  1916. u32 msg_count = msg_msgcnt(tunnel_msg);
  1917. u32 bearer_id = msg_bearer_id(tunnel_msg);
  1918. if (bearer_id >= MAX_BEARERS)
  1919. goto exit;
  1920. dest_link = (*l_ptr)->owner->links[bearer_id];
  1921. if (!dest_link)
  1922. goto exit;
  1923. if (dest_link == *l_ptr) {
  1924. pr_err("Unexpected changeover message on link <%s>\n",
  1925. (*l_ptr)->name);
  1926. goto exit;
  1927. }
  1928. *l_ptr = dest_link;
  1929. msg = msg_get_wrapped(tunnel_msg);
  1930. if (msg_typ == DUPLICATE_MSG) {
  1931. if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
  1932. goto exit;
  1933. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1934. if (*buf == NULL) {
  1935. pr_warn("%sduplicate msg dropped\n", link_co_err);
  1936. goto exit;
  1937. }
  1938. kfree_skb(tunnel_buf);
  1939. return 1;
  1940. }
  1941. /* First original message ?: */
  1942. if (tipc_link_is_up(dest_link)) {
  1943. pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
  1944. dest_link->name);
  1945. tipc_link_reset(dest_link);
  1946. dest_link->exp_msg_count = msg_count;
  1947. if (!msg_count)
  1948. goto exit;
  1949. } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
  1950. dest_link->exp_msg_count = msg_count;
  1951. if (!msg_count)
  1952. goto exit;
  1953. }
  1954. /* Receive original message */
  1955. if (dest_link->exp_msg_count == 0) {
  1956. pr_warn("%sgot too many tunnelled messages\n", link_co_err);
  1957. goto exit;
  1958. }
  1959. dest_link->exp_msg_count--;
  1960. if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
  1961. goto exit;
  1962. } else {
  1963. *buf = buf_extract(tunnel_buf, INT_H_SIZE);
  1964. if (*buf != NULL) {
  1965. kfree_skb(tunnel_buf);
  1966. return 1;
  1967. } else {
  1968. pr_warn("%soriginal msg dropped\n", link_co_err);
  1969. }
  1970. }
  1971. exit:
  1972. *buf = NULL;
  1973. kfree_skb(tunnel_buf);
  1974. return 0;
  1975. }
  1976. /*
  1977. * Bundler functionality:
  1978. */
  1979. void tipc_link_recv_bundle(struct sk_buff *buf)
  1980. {
  1981. u32 msgcount = msg_msgcnt(buf_msg(buf));
  1982. u32 pos = INT_H_SIZE;
  1983. struct sk_buff *obuf;
  1984. while (msgcount--) {
  1985. obuf = buf_extract(buf, pos);
  1986. if (obuf == NULL) {
  1987. pr_warn("Link unable to unbundle message(s)\n");
  1988. break;
  1989. }
  1990. pos += align(msg_size(buf_msg(obuf)));
  1991. tipc_net_route_msg(obuf);
  1992. }
  1993. kfree_skb(buf);
  1994. }
  1995. /*
  1996. * Fragmentation/defragmentation:
  1997. */
  1998. /*
  1999. * link_send_long_buf: Entry for buffers needing fragmentation.
  2000. * The buffer is complete, inclusive total message length.
  2001. * Returns user data length.
  2002. */
  2003. static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
  2004. {
  2005. struct sk_buff *buf_chain = NULL;
  2006. struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
  2007. struct tipc_msg *inmsg = buf_msg(buf);
  2008. struct tipc_msg fragm_hdr;
  2009. u32 insize = msg_size(inmsg);
  2010. u32 dsz = msg_data_sz(inmsg);
  2011. unchar *crs = buf->data;
  2012. u32 rest = insize;
  2013. u32 pack_sz = l_ptr->max_pkt;
  2014. u32 fragm_sz = pack_sz - INT_H_SIZE;
  2015. u32 fragm_no = 0;
  2016. u32 destaddr;
  2017. if (msg_short(inmsg))
  2018. destaddr = l_ptr->addr;
  2019. else
  2020. destaddr = msg_destnode(inmsg);
  2021. /* Prepare reusable fragment header: */
  2022. tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  2023. INT_H_SIZE, destaddr);
  2024. /* Chop up message: */
  2025. while (rest > 0) {
  2026. struct sk_buff *fragm;
  2027. if (rest <= fragm_sz) {
  2028. fragm_sz = rest;
  2029. msg_set_type(&fragm_hdr, LAST_FRAGMENT);
  2030. }
  2031. fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
  2032. if (fragm == NULL) {
  2033. kfree_skb(buf);
  2034. while (buf_chain) {
  2035. buf = buf_chain;
  2036. buf_chain = buf_chain->next;
  2037. kfree_skb(buf);
  2038. }
  2039. return -ENOMEM;
  2040. }
  2041. msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
  2042. fragm_no++;
  2043. msg_set_fragm_no(&fragm_hdr, fragm_no);
  2044. skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
  2045. skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
  2046. fragm_sz);
  2047. buf_chain_tail->next = fragm;
  2048. buf_chain_tail = fragm;
  2049. rest -= fragm_sz;
  2050. crs += fragm_sz;
  2051. msg_set_type(&fragm_hdr, FRAGMENT);
  2052. }
  2053. kfree_skb(buf);
  2054. /* Append chain of fragments to send queue & send them */
  2055. l_ptr->long_msg_seq_no++;
  2056. link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
  2057. l_ptr->stats.sent_fragments += fragm_no;
  2058. l_ptr->stats.sent_fragmented++;
  2059. tipc_link_push_queue(l_ptr);
  2060. return dsz;
  2061. }
  2062. /*
  2063. * tipc_link_recv_fragment(): Called with node lock on. Returns
  2064. * the reassembled buffer if message is complete.
  2065. */
  2066. int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
  2067. struct sk_buff **fbuf)
  2068. {
  2069. struct sk_buff *frag = *fbuf;
  2070. struct tipc_msg *msg = buf_msg(frag);
  2071. u32 fragid = msg_type(msg);
  2072. bool headstolen;
  2073. int delta;
  2074. skb_pull(frag, msg_hdr_sz(msg));
  2075. if (fragid == FIRST_FRAGMENT) {
  2076. if (*head || skb_unclone(frag, GFP_ATOMIC))
  2077. goto out_free;
  2078. *head = frag;
  2079. skb_frag_list_init(*head);
  2080. return 0;
  2081. } else if (*head &&
  2082. skb_try_coalesce(*head, frag, &headstolen, &delta)) {
  2083. kfree_skb_partial(frag, headstolen);
  2084. } else {
  2085. if (!*head)
  2086. goto out_free;
  2087. if (!skb_has_frag_list(*head))
  2088. skb_shinfo(*head)->frag_list = frag;
  2089. else
  2090. (*tail)->next = frag;
  2091. *tail = frag;
  2092. (*head)->truesize += frag->truesize;
  2093. }
  2094. if (fragid == LAST_FRAGMENT) {
  2095. *fbuf = *head;
  2096. *tail = *head = NULL;
  2097. return LINK_REASM_COMPLETE;
  2098. }
  2099. return 0;
  2100. out_free:
  2101. pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
  2102. kfree_skb(*fbuf);
  2103. return LINK_REASM_ERROR;
  2104. }
  2105. static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
  2106. {
  2107. if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
  2108. return;
  2109. l_ptr->tolerance = tolerance;
  2110. l_ptr->continuity_interval =
  2111. ((tolerance / 4) > 500) ? 500 : tolerance / 4;
  2112. l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
  2113. }
  2114. void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
  2115. {
  2116. /* Data messages from this node, inclusive FIRST_FRAGM */
  2117. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
  2118. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
  2119. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
  2120. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
  2121. /* Transiting data messages,inclusive FIRST_FRAGM */
  2122. l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
  2123. l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
  2124. l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
  2125. l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
  2126. l_ptr->queue_limit[CONN_MANAGER] = 1200;
  2127. l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
  2128. l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
  2129. /* FRAGMENT and LAST_FRAGMENT packets */
  2130. l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
  2131. }
  2132. /**
  2133. * link_find_link - locate link by name
  2134. * @name: ptr to link name string
  2135. * @node: ptr to area to be filled with ptr to associated node
  2136. *
  2137. * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
  2138. * this also prevents link deletion.
  2139. *
  2140. * Returns pointer to link (or 0 if invalid link name).
  2141. */
  2142. static struct tipc_link *link_find_link(const char *name,
  2143. struct tipc_node **node)
  2144. {
  2145. struct tipc_link *l_ptr;
  2146. struct tipc_node *n_ptr;
  2147. int i;
  2148. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  2149. for (i = 0; i < MAX_BEARERS; i++) {
  2150. l_ptr = n_ptr->links[i];
  2151. if (l_ptr && !strcmp(l_ptr->name, name))
  2152. goto found;
  2153. }
  2154. }
  2155. l_ptr = NULL;
  2156. n_ptr = NULL;
  2157. found:
  2158. *node = n_ptr;
  2159. return l_ptr;
  2160. }
  2161. /**
  2162. * link_value_is_valid -- validate proposed link tolerance/priority/window
  2163. *
  2164. * @cmd: value type (TIPC_CMD_SET_LINK_*)
  2165. * @new_value: the new value
  2166. *
  2167. * Returns 1 if value is within range, 0 if not.
  2168. */
  2169. static int link_value_is_valid(u16 cmd, u32 new_value)
  2170. {
  2171. switch (cmd) {
  2172. case TIPC_CMD_SET_LINK_TOL:
  2173. return (new_value >= TIPC_MIN_LINK_TOL) &&
  2174. (new_value <= TIPC_MAX_LINK_TOL);
  2175. case TIPC_CMD_SET_LINK_PRI:
  2176. return (new_value <= TIPC_MAX_LINK_PRI);
  2177. case TIPC_CMD_SET_LINK_WINDOW:
  2178. return (new_value >= TIPC_MIN_LINK_WIN) &&
  2179. (new_value <= TIPC_MAX_LINK_WIN);
  2180. }
  2181. return 0;
  2182. }
  2183. /**
  2184. * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
  2185. * @name: ptr to link, bearer, or media name
  2186. * @new_value: new value of link, bearer, or media setting
  2187. * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
  2188. *
  2189. * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
  2190. *
  2191. * Returns 0 if value updated and negative value on error.
  2192. */
  2193. static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
  2194. {
  2195. struct tipc_node *node;
  2196. struct tipc_link *l_ptr;
  2197. struct tipc_bearer *b_ptr;
  2198. struct tipc_media *m_ptr;
  2199. int res = 0;
  2200. l_ptr = link_find_link(name, &node);
  2201. if (l_ptr) {
  2202. /*
  2203. * acquire node lock for tipc_link_send_proto_msg().
  2204. * see "TIPC locking policy" in net.c.
  2205. */
  2206. tipc_node_lock(node);
  2207. switch (cmd) {
  2208. case TIPC_CMD_SET_LINK_TOL:
  2209. link_set_supervision_props(l_ptr, new_value);
  2210. tipc_link_send_proto_msg(l_ptr,
  2211. STATE_MSG, 0, 0, new_value, 0, 0);
  2212. break;
  2213. case TIPC_CMD_SET_LINK_PRI:
  2214. l_ptr->priority = new_value;
  2215. tipc_link_send_proto_msg(l_ptr,
  2216. STATE_MSG, 0, 0, 0, new_value, 0);
  2217. break;
  2218. case TIPC_CMD_SET_LINK_WINDOW:
  2219. tipc_link_set_queue_limits(l_ptr, new_value);
  2220. break;
  2221. default:
  2222. res = -EINVAL;
  2223. break;
  2224. }
  2225. tipc_node_unlock(node);
  2226. return res;
  2227. }
  2228. b_ptr = tipc_bearer_find(name);
  2229. if (b_ptr) {
  2230. switch (cmd) {
  2231. case TIPC_CMD_SET_LINK_TOL:
  2232. b_ptr->tolerance = new_value;
  2233. break;
  2234. case TIPC_CMD_SET_LINK_PRI:
  2235. b_ptr->priority = new_value;
  2236. break;
  2237. case TIPC_CMD_SET_LINK_WINDOW:
  2238. b_ptr->window = new_value;
  2239. break;
  2240. default:
  2241. res = -EINVAL;
  2242. break;
  2243. }
  2244. return res;
  2245. }
  2246. m_ptr = tipc_media_find(name);
  2247. if (!m_ptr)
  2248. return -ENODEV;
  2249. switch (cmd) {
  2250. case TIPC_CMD_SET_LINK_TOL:
  2251. m_ptr->tolerance = new_value;
  2252. break;
  2253. case TIPC_CMD_SET_LINK_PRI:
  2254. m_ptr->priority = new_value;
  2255. break;
  2256. case TIPC_CMD_SET_LINK_WINDOW:
  2257. m_ptr->window = new_value;
  2258. break;
  2259. default:
  2260. res = -EINVAL;
  2261. break;
  2262. }
  2263. return res;
  2264. }
  2265. struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
  2266. u16 cmd)
  2267. {
  2268. struct tipc_link_config *args;
  2269. u32 new_value;
  2270. int res;
  2271. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
  2272. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2273. args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
  2274. new_value = ntohl(args->value);
  2275. if (!link_value_is_valid(cmd, new_value))
  2276. return tipc_cfg_reply_error_string(
  2277. "cannot change, value invalid");
  2278. if (!strcmp(args->name, tipc_bclink_name)) {
  2279. if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
  2280. (tipc_bclink_set_queue_limits(new_value) == 0))
  2281. return tipc_cfg_reply_none();
  2282. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  2283. " (cannot change setting on broadcast link)");
  2284. }
  2285. read_lock_bh(&tipc_net_lock);
  2286. res = link_cmd_set_value(args->name, new_value, cmd);
  2287. read_unlock_bh(&tipc_net_lock);
  2288. if (res)
  2289. return tipc_cfg_reply_error_string("cannot change link setting");
  2290. return tipc_cfg_reply_none();
  2291. }
  2292. /**
  2293. * link_reset_statistics - reset link statistics
  2294. * @l_ptr: pointer to link
  2295. */
  2296. static void link_reset_statistics(struct tipc_link *l_ptr)
  2297. {
  2298. memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
  2299. l_ptr->stats.sent_info = l_ptr->next_out_no;
  2300. l_ptr->stats.recv_info = l_ptr->next_in_no;
  2301. }
  2302. struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
  2303. {
  2304. char *link_name;
  2305. struct tipc_link *l_ptr;
  2306. struct tipc_node *node;
  2307. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2308. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2309. link_name = (char *)TLV_DATA(req_tlv_area);
  2310. if (!strcmp(link_name, tipc_bclink_name)) {
  2311. if (tipc_bclink_reset_stats())
  2312. return tipc_cfg_reply_error_string("link not found");
  2313. return tipc_cfg_reply_none();
  2314. }
  2315. read_lock_bh(&tipc_net_lock);
  2316. l_ptr = link_find_link(link_name, &node);
  2317. if (!l_ptr) {
  2318. read_unlock_bh(&tipc_net_lock);
  2319. return tipc_cfg_reply_error_string("link not found");
  2320. }
  2321. tipc_node_lock(node);
  2322. link_reset_statistics(l_ptr);
  2323. tipc_node_unlock(node);
  2324. read_unlock_bh(&tipc_net_lock);
  2325. return tipc_cfg_reply_none();
  2326. }
  2327. /**
  2328. * percent - convert count to a percentage of total (rounding up or down)
  2329. */
  2330. static u32 percent(u32 count, u32 total)
  2331. {
  2332. return (count * 100 + (total / 2)) / total;
  2333. }
  2334. /**
  2335. * tipc_link_stats - print link statistics
  2336. * @name: link name
  2337. * @buf: print buffer area
  2338. * @buf_size: size of print buffer area
  2339. *
  2340. * Returns length of print buffer data string (or 0 if error)
  2341. */
  2342. static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
  2343. {
  2344. struct tipc_link *l;
  2345. struct tipc_stats *s;
  2346. struct tipc_node *node;
  2347. char *status;
  2348. u32 profile_total = 0;
  2349. int ret;
  2350. if (!strcmp(name, tipc_bclink_name))
  2351. return tipc_bclink_stats(buf, buf_size);
  2352. read_lock_bh(&tipc_net_lock);
  2353. l = link_find_link(name, &node);
  2354. if (!l) {
  2355. read_unlock_bh(&tipc_net_lock);
  2356. return 0;
  2357. }
  2358. tipc_node_lock(node);
  2359. s = &l->stats;
  2360. if (tipc_link_is_active(l))
  2361. status = "ACTIVE";
  2362. else if (tipc_link_is_up(l))
  2363. status = "STANDBY";
  2364. else
  2365. status = "DEFUNCT";
  2366. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  2367. " %s MTU:%u Priority:%u Tolerance:%u ms"
  2368. " Window:%u packets\n",
  2369. l->name, status, l->max_pkt, l->priority,
  2370. l->tolerance, l->queue_limit[0]);
  2371. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2372. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2373. l->next_in_no - s->recv_info, s->recv_fragments,
  2374. s->recv_fragmented, s->recv_bundles,
  2375. s->recv_bundled);
  2376. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2377. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  2378. l->next_out_no - s->sent_info, s->sent_fragments,
  2379. s->sent_fragmented, s->sent_bundles,
  2380. s->sent_bundled);
  2381. profile_total = s->msg_length_counts;
  2382. if (!profile_total)
  2383. profile_total = 1;
  2384. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2385. " TX profile sample:%u packets average:%u octets\n"
  2386. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
  2387. "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  2388. s->msg_length_counts,
  2389. s->msg_lengths_total / profile_total,
  2390. percent(s->msg_length_profile[0], profile_total),
  2391. percent(s->msg_length_profile[1], profile_total),
  2392. percent(s->msg_length_profile[2], profile_total),
  2393. percent(s->msg_length_profile[3], profile_total),
  2394. percent(s->msg_length_profile[4], profile_total),
  2395. percent(s->msg_length_profile[5], profile_total),
  2396. percent(s->msg_length_profile[6], profile_total));
  2397. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2398. " RX states:%u probes:%u naks:%u defs:%u"
  2399. " dups:%u\n", s->recv_states, s->recv_probes,
  2400. s->recv_nacks, s->deferred_recv, s->duplicates);
  2401. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2402. " TX states:%u probes:%u naks:%u acks:%u"
  2403. " dups:%u\n", s->sent_states, s->sent_probes,
  2404. s->sent_nacks, s->sent_acks, s->retransmitted);
  2405. ret += tipc_snprintf(buf + ret, buf_size - ret,
  2406. " Congestion link:%u Send queue"
  2407. " max:%u avg:%u\n", s->link_congs,
  2408. s->max_queue_sz, s->queue_sz_counts ?
  2409. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  2410. tipc_node_unlock(node);
  2411. read_unlock_bh(&tipc_net_lock);
  2412. return ret;
  2413. }
  2414. struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
  2415. {
  2416. struct sk_buff *buf;
  2417. struct tlv_desc *rep_tlv;
  2418. int str_len;
  2419. int pb_len;
  2420. char *pb;
  2421. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
  2422. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  2423. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  2424. if (!buf)
  2425. return NULL;
  2426. rep_tlv = (struct tlv_desc *)buf->data;
  2427. pb = TLV_DATA(rep_tlv);
  2428. pb_len = ULTRA_STRING_MAX_LEN;
  2429. str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
  2430. pb, pb_len);
  2431. if (!str_len) {
  2432. kfree_skb(buf);
  2433. return tipc_cfg_reply_error_string("link not found");
  2434. }
  2435. str_len += 1; /* for "\0" */
  2436. skb_put(buf, TLV_SPACE(str_len));
  2437. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  2438. return buf;
  2439. }
  2440. /**
  2441. * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
  2442. * @dest: network address of destination node
  2443. * @selector: used to select from set of active links
  2444. *
  2445. * If no active link can be found, uses default maximum packet size.
  2446. */
  2447. u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
  2448. {
  2449. struct tipc_node *n_ptr;
  2450. struct tipc_link *l_ptr;
  2451. u32 res = MAX_PKT_DEFAULT;
  2452. if (dest == tipc_own_addr)
  2453. return MAX_MSG_SIZE;
  2454. read_lock_bh(&tipc_net_lock);
  2455. n_ptr = tipc_node_find(dest);
  2456. if (n_ptr) {
  2457. tipc_node_lock(n_ptr);
  2458. l_ptr = n_ptr->active_links[selector & 1];
  2459. if (l_ptr)
  2460. res = l_ptr->max_pkt;
  2461. tipc_node_unlock(n_ptr);
  2462. }
  2463. read_unlock_bh(&tipc_net_lock);
  2464. return res;
  2465. }
  2466. static void link_print(struct tipc_link *l_ptr, const char *str)
  2467. {
  2468. pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
  2469. if (link_working_unknown(l_ptr))
  2470. pr_cont(":WU\n");
  2471. else if (link_reset_reset(l_ptr))
  2472. pr_cont(":RR\n");
  2473. else if (link_reset_unknown(l_ptr))
  2474. pr_cont(":RU\n");
  2475. else if (link_working_working(l_ptr))
  2476. pr_cont(":WW\n");
  2477. else
  2478. pr_cont("\n");
  2479. }