port.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2006, Ericsson AB
  5. * Copyright (c) 2004-2005, 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 "config.h"
  38. #include "dbg.h"
  39. #include "port.h"
  40. #include "addr.h"
  41. #include "link.h"
  42. #include "node.h"
  43. #include "port.h"
  44. #include "name_table.h"
  45. #include "user_reg.h"
  46. #include "msg.h"
  47. #include "bcast.h"
  48. /* Connection management: */
  49. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  50. #define CONFIRMED 0
  51. #define PROBING 1
  52. #define MAX_REJECT_SIZE 1024
  53. static struct sk_buff *msg_queue_head = NULL;
  54. static struct sk_buff *msg_queue_tail = NULL;
  55. spinlock_t tipc_port_list_lock = SPIN_LOCK_UNLOCKED;
  56. static spinlock_t queue_lock = SPIN_LOCK_UNLOCKED;
  57. static LIST_HEAD(ports);
  58. static void port_handle_node_down(unsigned long ref);
  59. static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
  60. static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
  61. static void port_timeout(unsigned long ref);
  62. static u32 port_peernode(struct port *p_ptr)
  63. {
  64. return msg_destnode(&p_ptr->publ.phdr);
  65. }
  66. static u32 port_peerport(struct port *p_ptr)
  67. {
  68. return msg_destport(&p_ptr->publ.phdr);
  69. }
  70. static u32 port_out_seqno(struct port *p_ptr)
  71. {
  72. return msg_transp_seqno(&p_ptr->publ.phdr);
  73. }
  74. static void port_incr_out_seqno(struct port *p_ptr)
  75. {
  76. struct tipc_msg *m = &p_ptr->publ.phdr;
  77. if (likely(!msg_routed(m)))
  78. return;
  79. msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
  80. }
  81. /**
  82. * tipc_multicast - send a multicast message to local and remote destinations
  83. */
  84. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
  85. u32 num_sect, struct iovec const *msg_sect)
  86. {
  87. struct tipc_msg *hdr;
  88. struct sk_buff *buf;
  89. struct sk_buff *ibuf = NULL;
  90. struct port_list dports = {0, NULL, };
  91. struct port *oport = tipc_port_deref(ref);
  92. int ext_targets;
  93. int res;
  94. if (unlikely(!oport))
  95. return -EINVAL;
  96. /* Create multicast message */
  97. hdr = &oport->publ.phdr;
  98. msg_set_type(hdr, TIPC_MCAST_MSG);
  99. msg_set_nametype(hdr, seq->type);
  100. msg_set_namelower(hdr, seq->lower);
  101. msg_set_nameupper(hdr, seq->upper);
  102. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  103. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  104. !oport->user_port, &buf);
  105. if (unlikely(!buf))
  106. return res;
  107. /* Figure out where to send multicast message */
  108. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  109. TIPC_NODE_SCOPE, &dports);
  110. /* Send message to destinations (duplicate it only if necessary) */
  111. if (ext_targets) {
  112. if (dports.count != 0) {
  113. ibuf = skb_copy(buf, GFP_ATOMIC);
  114. if (ibuf == NULL) {
  115. tipc_port_list_free(&dports);
  116. buf_discard(buf);
  117. return -ENOMEM;
  118. }
  119. }
  120. res = tipc_bclink_send_msg(buf);
  121. if ((res < 0) && (dports.count != 0)) {
  122. buf_discard(ibuf);
  123. }
  124. } else {
  125. ibuf = buf;
  126. }
  127. if (res >= 0) {
  128. if (ibuf)
  129. tipc_port_recv_mcast(ibuf, &dports);
  130. } else {
  131. tipc_port_list_free(&dports);
  132. }
  133. return res;
  134. }
  135. /**
  136. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  137. *
  138. * If there is no port list, perform a lookup to create one
  139. */
  140. void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
  141. {
  142. struct tipc_msg* msg;
  143. struct port_list dports = {0, NULL, };
  144. struct port_list *item = dp;
  145. int cnt = 0;
  146. assert(buf);
  147. msg = buf_msg(buf);
  148. /* Create destination port list, if one wasn't supplied */
  149. if (dp == NULL) {
  150. tipc_nametbl_mc_translate(msg_nametype(msg),
  151. msg_namelower(msg),
  152. msg_nameupper(msg),
  153. TIPC_CLUSTER_SCOPE,
  154. &dports);
  155. item = dp = &dports;
  156. }
  157. /* Deliver a copy of message to each destination port */
  158. if (dp->count != 0) {
  159. if (dp->count == 1) {
  160. msg_set_destport(msg, dp->ports[0]);
  161. tipc_port_recv_msg(buf);
  162. tipc_port_list_free(dp);
  163. return;
  164. }
  165. for (; cnt < dp->count; cnt++) {
  166. int index = cnt % PLSIZE;
  167. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  168. if (b == NULL) {
  169. warn("Buffer allocation failure\n");
  170. msg_dbg(msg, "LOST:");
  171. goto exit;
  172. }
  173. if ((index == 0) && (cnt != 0)) {
  174. item = item->next;
  175. }
  176. msg_set_destport(buf_msg(b),item->ports[index]);
  177. tipc_port_recv_msg(b);
  178. }
  179. }
  180. exit:
  181. buf_discard(buf);
  182. tipc_port_list_free(dp);
  183. }
  184. /**
  185. * tipc_createport_raw - create a native TIPC port
  186. *
  187. * Returns local port reference
  188. */
  189. u32 tipc_createport_raw(void *usr_handle,
  190. u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
  191. void (*wakeup)(struct tipc_port *),
  192. const u32 importance)
  193. {
  194. struct port *p_ptr;
  195. struct tipc_msg *msg;
  196. u32 ref;
  197. p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
  198. if (p_ptr == NULL) {
  199. warn("Memory squeeze; failed to create port\n");
  200. return 0;
  201. }
  202. memset(p_ptr, 0, sizeof(*p_ptr));
  203. ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
  204. if (!ref) {
  205. warn("Reference Table Exhausted\n");
  206. kfree(p_ptr);
  207. return 0;
  208. }
  209. tipc_port_lock(ref);
  210. p_ptr->publ.ref = ref;
  211. msg = &p_ptr->publ.phdr;
  212. msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
  213. msg_set_orignode(msg, tipc_own_addr);
  214. msg_set_prevnode(msg, tipc_own_addr);
  215. msg_set_origport(msg, ref);
  216. msg_set_importance(msg,importance);
  217. p_ptr->last_in_seqno = 41;
  218. p_ptr->sent = 1;
  219. p_ptr->publ.usr_handle = usr_handle;
  220. INIT_LIST_HEAD(&p_ptr->wait_list);
  221. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  222. p_ptr->congested_link = NULL;
  223. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  224. p_ptr->dispatcher = dispatcher;
  225. p_ptr->wakeup = wakeup;
  226. p_ptr->user_port = NULL;
  227. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  228. spin_lock_bh(&tipc_port_list_lock);
  229. INIT_LIST_HEAD(&p_ptr->publications);
  230. INIT_LIST_HEAD(&p_ptr->port_list);
  231. list_add_tail(&p_ptr->port_list, &ports);
  232. spin_unlock_bh(&tipc_port_list_lock);
  233. tipc_port_unlock(p_ptr);
  234. return ref;
  235. }
  236. int tipc_deleteport(u32 ref)
  237. {
  238. struct port *p_ptr;
  239. struct sk_buff *buf = NULL;
  240. tipc_withdraw(ref, 0, NULL);
  241. p_ptr = tipc_port_lock(ref);
  242. if (!p_ptr)
  243. return -EINVAL;
  244. tipc_ref_discard(ref);
  245. tipc_port_unlock(p_ptr);
  246. k_cancel_timer(&p_ptr->timer);
  247. if (p_ptr->publ.connected) {
  248. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  249. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  250. }
  251. if (p_ptr->user_port) {
  252. tipc_reg_remove_port(p_ptr->user_port);
  253. kfree(p_ptr->user_port);
  254. }
  255. spin_lock_bh(&tipc_port_list_lock);
  256. list_del(&p_ptr->port_list);
  257. list_del(&p_ptr->wait_list);
  258. spin_unlock_bh(&tipc_port_list_lock);
  259. k_term_timer(&p_ptr->timer);
  260. kfree(p_ptr);
  261. dbg("Deleted port %u\n", ref);
  262. tipc_net_route_msg(buf);
  263. return TIPC_OK;
  264. }
  265. /**
  266. * tipc_get_port() - return port associated with 'ref'
  267. *
  268. * Note: Port is not locked.
  269. */
  270. struct tipc_port *tipc_get_port(const u32 ref)
  271. {
  272. return (struct tipc_port *)tipc_ref_deref(ref);
  273. }
  274. /**
  275. * tipc_get_handle - return user handle associated to port 'ref'
  276. */
  277. void *tipc_get_handle(const u32 ref)
  278. {
  279. struct port *p_ptr;
  280. void * handle;
  281. p_ptr = tipc_port_lock(ref);
  282. if (!p_ptr)
  283. return NULL;
  284. handle = p_ptr->publ.usr_handle;
  285. tipc_port_unlock(p_ptr);
  286. return handle;
  287. }
  288. static int port_unreliable(struct port *p_ptr)
  289. {
  290. return msg_src_droppable(&p_ptr->publ.phdr);
  291. }
  292. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  293. {
  294. struct port *p_ptr;
  295. p_ptr = tipc_port_lock(ref);
  296. if (!p_ptr)
  297. return -EINVAL;
  298. *isunreliable = port_unreliable(p_ptr);
  299. spin_unlock_bh(p_ptr->publ.lock);
  300. return TIPC_OK;
  301. }
  302. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  303. {
  304. struct port *p_ptr;
  305. p_ptr = tipc_port_lock(ref);
  306. if (!p_ptr)
  307. return -EINVAL;
  308. msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
  309. tipc_port_unlock(p_ptr);
  310. return TIPC_OK;
  311. }
  312. static int port_unreturnable(struct port *p_ptr)
  313. {
  314. return msg_dest_droppable(&p_ptr->publ.phdr);
  315. }
  316. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  317. {
  318. struct port *p_ptr;
  319. p_ptr = tipc_port_lock(ref);
  320. if (!p_ptr)
  321. return -EINVAL;
  322. *isunrejectable = port_unreturnable(p_ptr);
  323. spin_unlock_bh(p_ptr->publ.lock);
  324. return TIPC_OK;
  325. }
  326. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  327. {
  328. struct port *p_ptr;
  329. p_ptr = tipc_port_lock(ref);
  330. if (!p_ptr)
  331. return -EINVAL;
  332. msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
  333. tipc_port_unlock(p_ptr);
  334. return TIPC_OK;
  335. }
  336. /*
  337. * port_build_proto_msg(): build a port level protocol
  338. * or a connection abortion message. Called with
  339. * tipc_port lock on.
  340. */
  341. static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
  342. u32 origport, u32 orignode,
  343. u32 usr, u32 type, u32 err,
  344. u32 seqno, u32 ack)
  345. {
  346. struct sk_buff *buf;
  347. struct tipc_msg *msg;
  348. buf = buf_acquire(LONG_H_SIZE);
  349. if (buf) {
  350. msg = buf_msg(buf);
  351. msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
  352. msg_set_destport(msg, destport);
  353. msg_set_origport(msg, origport);
  354. msg_set_destnode(msg, destnode);
  355. msg_set_orignode(msg, orignode);
  356. msg_set_transp_seqno(msg, seqno);
  357. msg_set_msgcnt(msg, ack);
  358. msg_dbg(msg, "PORT>SEND>:");
  359. }
  360. return buf;
  361. }
  362. int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
  363. {
  364. msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
  365. msg_set_options(&tp_ptr->phdr, opt, sz);
  366. return TIPC_OK;
  367. }
  368. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  369. {
  370. struct tipc_msg *msg = buf_msg(buf);
  371. struct sk_buff *rbuf;
  372. struct tipc_msg *rmsg;
  373. int hdr_sz;
  374. u32 imp = msg_importance(msg);
  375. u32 data_sz = msg_data_sz(msg);
  376. if (data_sz > MAX_REJECT_SIZE)
  377. data_sz = MAX_REJECT_SIZE;
  378. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  379. imp++;
  380. msg_dbg(msg, "port->rej: ");
  381. /* discard rejected message if it shouldn't be returned to sender */
  382. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  383. buf_discard(buf);
  384. return data_sz;
  385. }
  386. /* construct rejected message */
  387. if (msg_mcast(msg))
  388. hdr_sz = MCAST_H_SIZE;
  389. else
  390. hdr_sz = LONG_H_SIZE;
  391. rbuf = buf_acquire(data_sz + hdr_sz);
  392. if (rbuf == NULL) {
  393. buf_discard(buf);
  394. return data_sz;
  395. }
  396. rmsg = buf_msg(rbuf);
  397. msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
  398. msg_set_destport(rmsg, msg_origport(msg));
  399. msg_set_prevnode(rmsg, tipc_own_addr);
  400. msg_set_origport(rmsg, msg_destport(msg));
  401. if (msg_short(msg))
  402. msg_set_orignode(rmsg, tipc_own_addr);
  403. else
  404. msg_set_orignode(rmsg, msg_destnode(msg));
  405. msg_set_size(rmsg, data_sz + hdr_sz);
  406. msg_set_nametype(rmsg, msg_nametype(msg));
  407. msg_set_nameinst(rmsg, msg_nameinst(msg));
  408. memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
  409. /* send self-abort message when rejecting on a connected port */
  410. if (msg_connected(msg)) {
  411. struct sk_buff *abuf = NULL;
  412. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  413. if (p_ptr) {
  414. if (p_ptr->publ.connected)
  415. abuf = port_build_self_abort_msg(p_ptr, err);
  416. tipc_port_unlock(p_ptr);
  417. }
  418. tipc_net_route_msg(abuf);
  419. }
  420. /* send rejected message */
  421. buf_discard(buf);
  422. tipc_net_route_msg(rbuf);
  423. return data_sz;
  424. }
  425. int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
  426. struct iovec const *msg_sect, u32 num_sect,
  427. int err)
  428. {
  429. struct sk_buff *buf;
  430. int res;
  431. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  432. !p_ptr->user_port, &buf);
  433. if (!buf)
  434. return res;
  435. return tipc_reject_msg(buf, err);
  436. }
  437. static void port_timeout(unsigned long ref)
  438. {
  439. struct port *p_ptr = tipc_port_lock(ref);
  440. struct sk_buff *buf = NULL;
  441. if (!p_ptr || !p_ptr->publ.connected)
  442. return;
  443. /* Last probe answered ? */
  444. if (p_ptr->probing_state == PROBING) {
  445. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  446. } else {
  447. buf = port_build_proto_msg(port_peerport(p_ptr),
  448. port_peernode(p_ptr),
  449. p_ptr->publ.ref,
  450. tipc_own_addr,
  451. CONN_MANAGER,
  452. CONN_PROBE,
  453. TIPC_OK,
  454. port_out_seqno(p_ptr),
  455. 0);
  456. port_incr_out_seqno(p_ptr);
  457. p_ptr->probing_state = PROBING;
  458. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  459. }
  460. tipc_port_unlock(p_ptr);
  461. tipc_net_route_msg(buf);
  462. }
  463. static void port_handle_node_down(unsigned long ref)
  464. {
  465. struct port *p_ptr = tipc_port_lock(ref);
  466. struct sk_buff* buf = NULL;
  467. if (!p_ptr)
  468. return;
  469. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  470. tipc_port_unlock(p_ptr);
  471. tipc_net_route_msg(buf);
  472. }
  473. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  474. {
  475. u32 imp = msg_importance(&p_ptr->publ.phdr);
  476. if (!p_ptr->publ.connected)
  477. return NULL;
  478. if (imp < TIPC_CRITICAL_IMPORTANCE)
  479. imp++;
  480. return port_build_proto_msg(p_ptr->publ.ref,
  481. tipc_own_addr,
  482. port_peerport(p_ptr),
  483. port_peernode(p_ptr),
  484. imp,
  485. TIPC_CONN_MSG,
  486. err,
  487. p_ptr->last_in_seqno + 1,
  488. 0);
  489. }
  490. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  491. {
  492. u32 imp = msg_importance(&p_ptr->publ.phdr);
  493. if (!p_ptr->publ.connected)
  494. return NULL;
  495. if (imp < TIPC_CRITICAL_IMPORTANCE)
  496. imp++;
  497. return port_build_proto_msg(port_peerport(p_ptr),
  498. port_peernode(p_ptr),
  499. p_ptr->publ.ref,
  500. tipc_own_addr,
  501. imp,
  502. TIPC_CONN_MSG,
  503. err,
  504. port_out_seqno(p_ptr),
  505. 0);
  506. }
  507. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  508. {
  509. struct tipc_msg *msg = buf_msg(buf);
  510. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  511. u32 err = TIPC_OK;
  512. struct sk_buff *r_buf = NULL;
  513. struct sk_buff *abort_buf = NULL;
  514. msg_dbg(msg, "PORT<RECV<:");
  515. if (!p_ptr) {
  516. err = TIPC_ERR_NO_PORT;
  517. } else if (p_ptr->publ.connected) {
  518. if (port_peernode(p_ptr) != msg_orignode(msg))
  519. err = TIPC_ERR_NO_PORT;
  520. if (port_peerport(p_ptr) != msg_origport(msg))
  521. err = TIPC_ERR_NO_PORT;
  522. if (!err && msg_routed(msg)) {
  523. u32 seqno = msg_transp_seqno(msg);
  524. u32 myno = ++p_ptr->last_in_seqno;
  525. if (seqno != myno) {
  526. err = TIPC_ERR_NO_PORT;
  527. abort_buf = port_build_self_abort_msg(p_ptr, err);
  528. }
  529. }
  530. if (msg_type(msg) == CONN_ACK) {
  531. int wakeup = tipc_port_congested(p_ptr) &&
  532. p_ptr->publ.congested &&
  533. p_ptr->wakeup;
  534. p_ptr->acked += msg_msgcnt(msg);
  535. if (tipc_port_congested(p_ptr))
  536. goto exit;
  537. p_ptr->publ.congested = 0;
  538. if (!wakeup)
  539. goto exit;
  540. p_ptr->wakeup(&p_ptr->publ);
  541. goto exit;
  542. }
  543. } else if (p_ptr->publ.published) {
  544. err = TIPC_ERR_NO_PORT;
  545. }
  546. if (err) {
  547. r_buf = port_build_proto_msg(msg_origport(msg),
  548. msg_orignode(msg),
  549. msg_destport(msg),
  550. tipc_own_addr,
  551. DATA_HIGH,
  552. TIPC_CONN_MSG,
  553. err,
  554. 0,
  555. 0);
  556. goto exit;
  557. }
  558. /* All is fine */
  559. if (msg_type(msg) == CONN_PROBE) {
  560. r_buf = port_build_proto_msg(msg_origport(msg),
  561. msg_orignode(msg),
  562. msg_destport(msg),
  563. tipc_own_addr,
  564. CONN_MANAGER,
  565. CONN_PROBE_REPLY,
  566. TIPC_OK,
  567. port_out_seqno(p_ptr),
  568. 0);
  569. }
  570. p_ptr->probing_state = CONFIRMED;
  571. port_incr_out_seqno(p_ptr);
  572. exit:
  573. if (p_ptr)
  574. tipc_port_unlock(p_ptr);
  575. tipc_net_route_msg(r_buf);
  576. tipc_net_route_msg(abort_buf);
  577. buf_discard(buf);
  578. }
  579. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  580. {
  581. struct publication *publ;
  582. if (full_id)
  583. tipc_printf(buf, "<%u.%u.%u:%u>:",
  584. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  585. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  586. else
  587. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  588. if (p_ptr->publ.connected) {
  589. u32 dport = port_peerport(p_ptr);
  590. u32 destnode = port_peernode(p_ptr);
  591. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  592. tipc_zone(destnode), tipc_cluster(destnode),
  593. tipc_node(destnode), dport);
  594. if (p_ptr->publ.conn_type != 0)
  595. tipc_printf(buf, " via {%u,%u}",
  596. p_ptr->publ.conn_type,
  597. p_ptr->publ.conn_instance);
  598. }
  599. else if (p_ptr->publ.published) {
  600. tipc_printf(buf, " bound to");
  601. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  602. if (publ->lower == publ->upper)
  603. tipc_printf(buf, " {%u,%u}", publ->type,
  604. publ->lower);
  605. else
  606. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  607. publ->lower, publ->upper);
  608. }
  609. }
  610. tipc_printf(buf, "\n");
  611. }
  612. #define MAX_PORT_QUERY 32768
  613. struct sk_buff *tipc_port_get_ports(void)
  614. {
  615. struct sk_buff *buf;
  616. struct tlv_desc *rep_tlv;
  617. struct print_buf pb;
  618. struct port *p_ptr;
  619. int str_len;
  620. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  621. if (!buf)
  622. return NULL;
  623. rep_tlv = (struct tlv_desc *)buf->data;
  624. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  625. spin_lock_bh(&tipc_port_list_lock);
  626. list_for_each_entry(p_ptr, &ports, port_list) {
  627. spin_lock_bh(p_ptr->publ.lock);
  628. port_print(p_ptr, &pb, 0);
  629. spin_unlock_bh(p_ptr->publ.lock);
  630. }
  631. spin_unlock_bh(&tipc_port_list_lock);
  632. str_len = tipc_printbuf_validate(&pb);
  633. skb_put(buf, TLV_SPACE(str_len));
  634. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  635. return buf;
  636. }
  637. #if 0
  638. #define MAX_PORT_STATS 2000
  639. struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
  640. {
  641. u32 ref;
  642. struct port *p_ptr;
  643. struct sk_buff *buf;
  644. struct tlv_desc *rep_tlv;
  645. struct print_buf pb;
  646. int str_len;
  647. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
  648. return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  649. ref = *(u32 *)TLV_DATA(req_tlv_area);
  650. ref = ntohl(ref);
  651. p_ptr = tipc_port_lock(ref);
  652. if (!p_ptr)
  653. return cfg_reply_error_string("port not found");
  654. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
  655. if (!buf) {
  656. tipc_port_unlock(p_ptr);
  657. return NULL;
  658. }
  659. rep_tlv = (struct tlv_desc *)buf->data;
  660. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
  661. port_print(p_ptr, &pb, 1);
  662. /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
  663. tipc_port_unlock(p_ptr);
  664. str_len = tipc_printbuf_validate(&pb);
  665. skb_put(buf, TLV_SPACE(str_len));
  666. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  667. return buf;
  668. }
  669. #endif
  670. void tipc_port_reinit(void)
  671. {
  672. struct port *p_ptr;
  673. struct tipc_msg *msg;
  674. spin_lock_bh(&tipc_port_list_lock);
  675. list_for_each_entry(p_ptr, &ports, port_list) {
  676. msg = &p_ptr->publ.phdr;
  677. if (msg_orignode(msg) == tipc_own_addr)
  678. break;
  679. msg_set_orignode(msg, tipc_own_addr);
  680. }
  681. spin_unlock_bh(&tipc_port_list_lock);
  682. }
  683. /*
  684. * port_dispatcher_sigh(): Signal handler for messages destinated
  685. * to the tipc_port interface.
  686. */
  687. static void port_dispatcher_sigh(void *dummy)
  688. {
  689. struct sk_buff *buf;
  690. spin_lock_bh(&queue_lock);
  691. buf = msg_queue_head;
  692. msg_queue_head = NULL;
  693. spin_unlock_bh(&queue_lock);
  694. while (buf) {
  695. struct port *p_ptr;
  696. struct user_port *up_ptr;
  697. struct tipc_portid orig;
  698. struct tipc_name_seq dseq;
  699. void *usr_handle;
  700. int connected;
  701. int published;
  702. struct sk_buff *next = buf->next;
  703. struct tipc_msg *msg = buf_msg(buf);
  704. u32 dref = msg_destport(msg);
  705. p_ptr = tipc_port_lock(dref);
  706. if (!p_ptr) {
  707. /* Port deleted while msg in queue */
  708. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  709. buf = next;
  710. continue;
  711. }
  712. orig.ref = msg_origport(msg);
  713. orig.node = msg_orignode(msg);
  714. up_ptr = p_ptr->user_port;
  715. usr_handle = up_ptr->usr_handle;
  716. connected = p_ptr->publ.connected;
  717. published = p_ptr->publ.published;
  718. if (unlikely(msg_errcode(msg)))
  719. goto err;
  720. switch (msg_type(msg)) {
  721. case TIPC_CONN_MSG:{
  722. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  723. u32 peer_port = port_peerport(p_ptr);
  724. u32 peer_node = port_peernode(p_ptr);
  725. spin_unlock_bh(p_ptr->publ.lock);
  726. if (unlikely(!connected)) {
  727. if (unlikely(published))
  728. goto reject;
  729. tipc_connect2port(dref,&orig);
  730. }
  731. if (unlikely(msg_origport(msg) != peer_port))
  732. goto reject;
  733. if (unlikely(msg_orignode(msg) != peer_node))
  734. goto reject;
  735. if (unlikely(!cb))
  736. goto reject;
  737. if (unlikely(++p_ptr->publ.conn_unacked >=
  738. TIPC_FLOW_CONTROL_WIN))
  739. tipc_acknowledge(dref,
  740. p_ptr->publ.conn_unacked);
  741. skb_pull(buf, msg_hdr_sz(msg));
  742. cb(usr_handle, dref, &buf, msg_data(msg),
  743. msg_data_sz(msg));
  744. break;
  745. }
  746. case TIPC_DIRECT_MSG:{
  747. tipc_msg_event cb = up_ptr->msg_cb;
  748. spin_unlock_bh(p_ptr->publ.lock);
  749. if (unlikely(connected))
  750. goto reject;
  751. if (unlikely(!cb))
  752. goto reject;
  753. skb_pull(buf, msg_hdr_sz(msg));
  754. cb(usr_handle, dref, &buf, msg_data(msg),
  755. msg_data_sz(msg), msg_importance(msg),
  756. &orig);
  757. break;
  758. }
  759. case TIPC_NAMED_MSG:{
  760. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  761. spin_unlock_bh(p_ptr->publ.lock);
  762. if (unlikely(connected))
  763. goto reject;
  764. if (unlikely(!cb))
  765. goto reject;
  766. if (unlikely(!published))
  767. goto reject;
  768. dseq.type = msg_nametype(msg);
  769. dseq.lower = msg_nameinst(msg);
  770. dseq.upper = dseq.lower;
  771. skb_pull(buf, msg_hdr_sz(msg));
  772. cb(usr_handle, dref, &buf, msg_data(msg),
  773. msg_data_sz(msg), msg_importance(msg),
  774. &orig, &dseq);
  775. break;
  776. }
  777. }
  778. if (buf)
  779. buf_discard(buf);
  780. buf = next;
  781. continue;
  782. err:
  783. switch (msg_type(msg)) {
  784. case TIPC_CONN_MSG:{
  785. tipc_conn_shutdown_event cb =
  786. up_ptr->conn_err_cb;
  787. u32 peer_port = port_peerport(p_ptr);
  788. u32 peer_node = port_peernode(p_ptr);
  789. spin_unlock_bh(p_ptr->publ.lock);
  790. if (!connected || !cb)
  791. break;
  792. if (msg_origport(msg) != peer_port)
  793. break;
  794. if (msg_orignode(msg) != peer_node)
  795. break;
  796. tipc_disconnect(dref);
  797. skb_pull(buf, msg_hdr_sz(msg));
  798. cb(usr_handle, dref, &buf, msg_data(msg),
  799. msg_data_sz(msg), msg_errcode(msg));
  800. break;
  801. }
  802. case TIPC_DIRECT_MSG:{
  803. tipc_msg_err_event cb = up_ptr->err_cb;
  804. spin_unlock_bh(p_ptr->publ.lock);
  805. if (connected || !cb)
  806. break;
  807. skb_pull(buf, msg_hdr_sz(msg));
  808. cb(usr_handle, dref, &buf, msg_data(msg),
  809. msg_data_sz(msg), msg_errcode(msg), &orig);
  810. break;
  811. }
  812. case TIPC_NAMED_MSG:{
  813. tipc_named_msg_err_event cb =
  814. up_ptr->named_err_cb;
  815. spin_unlock_bh(p_ptr->publ.lock);
  816. if (connected || !cb)
  817. break;
  818. dseq.type = msg_nametype(msg);
  819. dseq.lower = msg_nameinst(msg);
  820. dseq.upper = dseq.lower;
  821. skb_pull(buf, msg_hdr_sz(msg));
  822. cb(usr_handle, dref, &buf, msg_data(msg),
  823. msg_data_sz(msg), msg_errcode(msg), &dseq);
  824. break;
  825. }
  826. }
  827. if (buf)
  828. buf_discard(buf);
  829. buf = next;
  830. continue;
  831. reject:
  832. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  833. buf = next;
  834. }
  835. }
  836. /*
  837. * port_dispatcher(): Dispatcher for messages destinated
  838. * to the tipc_port interface. Called with port locked.
  839. */
  840. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  841. {
  842. buf->next = NULL;
  843. spin_lock_bh(&queue_lock);
  844. if (msg_queue_head) {
  845. msg_queue_tail->next = buf;
  846. msg_queue_tail = buf;
  847. } else {
  848. msg_queue_tail = msg_queue_head = buf;
  849. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  850. }
  851. spin_unlock_bh(&queue_lock);
  852. return TIPC_OK;
  853. }
  854. /*
  855. * Wake up port after congestion: Called with port locked,
  856. *
  857. */
  858. static void port_wakeup_sh(unsigned long ref)
  859. {
  860. struct port *p_ptr;
  861. struct user_port *up_ptr;
  862. tipc_continue_event cb = NULL;
  863. void *uh = NULL;
  864. p_ptr = tipc_port_lock(ref);
  865. if (p_ptr) {
  866. up_ptr = p_ptr->user_port;
  867. if (up_ptr) {
  868. cb = up_ptr->continue_event_cb;
  869. uh = up_ptr->usr_handle;
  870. }
  871. tipc_port_unlock(p_ptr);
  872. }
  873. if (cb)
  874. cb(uh, ref);
  875. }
  876. static void port_wakeup(struct tipc_port *p_ptr)
  877. {
  878. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  879. }
  880. void tipc_acknowledge(u32 ref, u32 ack)
  881. {
  882. struct port *p_ptr;
  883. struct sk_buff *buf = NULL;
  884. p_ptr = tipc_port_lock(ref);
  885. if (!p_ptr)
  886. return;
  887. if (p_ptr->publ.connected) {
  888. p_ptr->publ.conn_unacked -= ack;
  889. buf = port_build_proto_msg(port_peerport(p_ptr),
  890. port_peernode(p_ptr),
  891. ref,
  892. tipc_own_addr,
  893. CONN_MANAGER,
  894. CONN_ACK,
  895. TIPC_OK,
  896. port_out_seqno(p_ptr),
  897. ack);
  898. }
  899. tipc_port_unlock(p_ptr);
  900. tipc_net_route_msg(buf);
  901. }
  902. /*
  903. * tipc_createport(): user level call. Will add port to
  904. * registry if non-zero user_ref.
  905. */
  906. int tipc_createport(u32 user_ref,
  907. void *usr_handle,
  908. unsigned int importance,
  909. tipc_msg_err_event error_cb,
  910. tipc_named_msg_err_event named_error_cb,
  911. tipc_conn_shutdown_event conn_error_cb,
  912. tipc_msg_event msg_cb,
  913. tipc_named_msg_event named_msg_cb,
  914. tipc_conn_msg_event conn_msg_cb,
  915. tipc_continue_event continue_event_cb,/* May be zero */
  916. u32 *portref)
  917. {
  918. struct user_port *up_ptr;
  919. struct port *p_ptr;
  920. u32 ref;
  921. up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  922. if (up_ptr == NULL) {
  923. return -ENOMEM;
  924. }
  925. ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
  926. p_ptr = tipc_port_lock(ref);
  927. if (!p_ptr) {
  928. kfree(up_ptr);
  929. return -ENOMEM;
  930. }
  931. p_ptr->user_port = up_ptr;
  932. up_ptr->user_ref = user_ref;
  933. up_ptr->usr_handle = usr_handle;
  934. up_ptr->ref = p_ptr->publ.ref;
  935. up_ptr->err_cb = error_cb;
  936. up_ptr->named_err_cb = named_error_cb;
  937. up_ptr->conn_err_cb = conn_error_cb;
  938. up_ptr->msg_cb = msg_cb;
  939. up_ptr->named_msg_cb = named_msg_cb;
  940. up_ptr->conn_msg_cb = conn_msg_cb;
  941. up_ptr->continue_event_cb = continue_event_cb;
  942. INIT_LIST_HEAD(&up_ptr->uport_list);
  943. tipc_reg_add_port(up_ptr);
  944. *portref = p_ptr->publ.ref;
  945. dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
  946. tipc_port_unlock(p_ptr);
  947. return TIPC_OK;
  948. }
  949. int tipc_ownidentity(u32 ref, struct tipc_portid *id)
  950. {
  951. id->ref = ref;
  952. id->node = tipc_own_addr;
  953. return TIPC_OK;
  954. }
  955. int tipc_portimportance(u32 ref, unsigned int *importance)
  956. {
  957. struct port *p_ptr;
  958. p_ptr = tipc_port_lock(ref);
  959. if (!p_ptr)
  960. return -EINVAL;
  961. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  962. spin_unlock_bh(p_ptr->publ.lock);
  963. return TIPC_OK;
  964. }
  965. int tipc_set_portimportance(u32 ref, unsigned int imp)
  966. {
  967. struct port *p_ptr;
  968. if (imp > TIPC_CRITICAL_IMPORTANCE)
  969. return -EINVAL;
  970. p_ptr = tipc_port_lock(ref);
  971. if (!p_ptr)
  972. return -EINVAL;
  973. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  974. spin_unlock_bh(p_ptr->publ.lock);
  975. return TIPC_OK;
  976. }
  977. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  978. {
  979. struct port *p_ptr;
  980. struct publication *publ;
  981. u32 key;
  982. int res = -EINVAL;
  983. p_ptr = tipc_port_lock(ref);
  984. dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
  985. "lower = %u, upper = %u\n",
  986. ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
  987. if (!p_ptr)
  988. return -EINVAL;
  989. if (p_ptr->publ.connected)
  990. goto exit;
  991. if (seq->lower > seq->upper)
  992. goto exit;
  993. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  994. goto exit;
  995. key = ref + p_ptr->pub_count + 1;
  996. if (key == ref) {
  997. res = -EADDRINUSE;
  998. goto exit;
  999. }
  1000. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  1001. scope, p_ptr->publ.ref, key);
  1002. if (publ) {
  1003. list_add(&publ->pport_list, &p_ptr->publications);
  1004. p_ptr->pub_count++;
  1005. p_ptr->publ.published = 1;
  1006. res = TIPC_OK;
  1007. }
  1008. exit:
  1009. tipc_port_unlock(p_ptr);
  1010. return res;
  1011. }
  1012. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  1013. {
  1014. struct port *p_ptr;
  1015. struct publication *publ;
  1016. struct publication *tpubl;
  1017. int res = -EINVAL;
  1018. p_ptr = tipc_port_lock(ref);
  1019. if (!p_ptr)
  1020. return -EINVAL;
  1021. if (!p_ptr->publ.published)
  1022. goto exit;
  1023. if (!seq) {
  1024. list_for_each_entry_safe(publ, tpubl,
  1025. &p_ptr->publications, pport_list) {
  1026. tipc_nametbl_withdraw(publ->type, publ->lower,
  1027. publ->ref, publ->key);
  1028. }
  1029. res = TIPC_OK;
  1030. } else {
  1031. list_for_each_entry_safe(publ, tpubl,
  1032. &p_ptr->publications, pport_list) {
  1033. if (publ->scope != scope)
  1034. continue;
  1035. if (publ->type != seq->type)
  1036. continue;
  1037. if (publ->lower != seq->lower)
  1038. continue;
  1039. if (publ->upper != seq->upper)
  1040. break;
  1041. tipc_nametbl_withdraw(publ->type, publ->lower,
  1042. publ->ref, publ->key);
  1043. res = TIPC_OK;
  1044. break;
  1045. }
  1046. }
  1047. if (list_empty(&p_ptr->publications))
  1048. p_ptr->publ.published = 0;
  1049. exit:
  1050. tipc_port_unlock(p_ptr);
  1051. return res;
  1052. }
  1053. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  1054. {
  1055. struct port *p_ptr;
  1056. struct tipc_msg *msg;
  1057. int res = -EINVAL;
  1058. p_ptr = tipc_port_lock(ref);
  1059. if (!p_ptr)
  1060. return -EINVAL;
  1061. if (p_ptr->publ.published || p_ptr->publ.connected)
  1062. goto exit;
  1063. if (!peer->ref)
  1064. goto exit;
  1065. msg = &p_ptr->publ.phdr;
  1066. msg_set_destnode(msg, peer->node);
  1067. msg_set_destport(msg, peer->ref);
  1068. msg_set_orignode(msg, tipc_own_addr);
  1069. msg_set_origport(msg, p_ptr->publ.ref);
  1070. msg_set_transp_seqno(msg, 42);
  1071. msg_set_type(msg, TIPC_CONN_MSG);
  1072. if (!may_route(peer->node))
  1073. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1074. else
  1075. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1076. p_ptr->probing_interval = PROBING_INTERVAL;
  1077. p_ptr->probing_state = CONFIRMED;
  1078. p_ptr->publ.connected = 1;
  1079. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  1080. tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
  1081. (void *)(unsigned long)ref,
  1082. (net_ev_handler)port_handle_node_down);
  1083. res = TIPC_OK;
  1084. exit:
  1085. tipc_port_unlock(p_ptr);
  1086. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  1087. return res;
  1088. }
  1089. /*
  1090. * tipc_disconnect(): Disconnect port form peer.
  1091. * This is a node local operation.
  1092. */
  1093. int tipc_disconnect(u32 ref)
  1094. {
  1095. struct port *p_ptr;
  1096. int res = -ENOTCONN;
  1097. p_ptr = tipc_port_lock(ref);
  1098. if (!p_ptr)
  1099. return -EINVAL;
  1100. if (p_ptr->publ.connected) {
  1101. p_ptr->publ.connected = 0;
  1102. /* let timer expire on it's own to avoid deadlock! */
  1103. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  1104. res = TIPC_OK;
  1105. }
  1106. tipc_port_unlock(p_ptr);
  1107. return res;
  1108. }
  1109. /*
  1110. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1111. */
  1112. int tipc_shutdown(u32 ref)
  1113. {
  1114. struct port *p_ptr;
  1115. struct sk_buff *buf = NULL;
  1116. p_ptr = tipc_port_lock(ref);
  1117. if (!p_ptr)
  1118. return -EINVAL;
  1119. if (p_ptr->publ.connected) {
  1120. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1121. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1122. imp++;
  1123. buf = port_build_proto_msg(port_peerport(p_ptr),
  1124. port_peernode(p_ptr),
  1125. ref,
  1126. tipc_own_addr,
  1127. imp,
  1128. TIPC_CONN_MSG,
  1129. TIPC_CONN_SHUTDOWN,
  1130. port_out_seqno(p_ptr),
  1131. 0);
  1132. }
  1133. tipc_port_unlock(p_ptr);
  1134. tipc_net_route_msg(buf);
  1135. return tipc_disconnect(ref);
  1136. }
  1137. int tipc_isconnected(u32 ref, int *isconnected)
  1138. {
  1139. struct port *p_ptr;
  1140. p_ptr = tipc_port_lock(ref);
  1141. if (!p_ptr)
  1142. return -EINVAL;
  1143. *isconnected = p_ptr->publ.connected;
  1144. tipc_port_unlock(p_ptr);
  1145. return TIPC_OK;
  1146. }
  1147. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1148. {
  1149. struct port *p_ptr;
  1150. int res;
  1151. p_ptr = tipc_port_lock(ref);
  1152. if (!p_ptr)
  1153. return -EINVAL;
  1154. if (p_ptr->publ.connected) {
  1155. peer->ref = port_peerport(p_ptr);
  1156. peer->node = port_peernode(p_ptr);
  1157. res = TIPC_OK;
  1158. } else
  1159. res = -ENOTCONN;
  1160. tipc_port_unlock(p_ptr);
  1161. return res;
  1162. }
  1163. int tipc_ref_valid(u32 ref)
  1164. {
  1165. /* Works irrespective of type */
  1166. return !!tipc_ref_deref(ref);
  1167. }
  1168. /*
  1169. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1170. * message for this node.
  1171. */
  1172. int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1173. struct iovec const *msg_sect)
  1174. {
  1175. struct sk_buff *buf;
  1176. int res;
  1177. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1178. MAX_MSG_SIZE, !sender->user_port, &buf);
  1179. if (likely(buf))
  1180. tipc_port_recv_msg(buf);
  1181. return res;
  1182. }
  1183. /**
  1184. * tipc_send - send message sections on connection
  1185. */
  1186. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1187. {
  1188. struct port *p_ptr;
  1189. u32 destnode;
  1190. int res;
  1191. p_ptr = tipc_port_deref(ref);
  1192. if (!p_ptr || !p_ptr->publ.connected)
  1193. return -EINVAL;
  1194. p_ptr->publ.congested = 1;
  1195. if (!tipc_port_congested(p_ptr)) {
  1196. destnode = port_peernode(p_ptr);
  1197. if (likely(destnode != tipc_own_addr))
  1198. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1199. destnode);
  1200. else
  1201. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1202. if (likely(res != -ELINKCONG)) {
  1203. port_incr_out_seqno(p_ptr);
  1204. p_ptr->publ.congested = 0;
  1205. p_ptr->sent++;
  1206. return res;
  1207. }
  1208. }
  1209. if (port_unreliable(p_ptr)) {
  1210. p_ptr->publ.congested = 0;
  1211. /* Just calculate msg length and return */
  1212. return msg_calc_data_size(msg_sect, num_sect);
  1213. }
  1214. return -ELINKCONG;
  1215. }
  1216. /**
  1217. * tipc_send_buf - send message buffer on connection
  1218. */
  1219. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1220. {
  1221. struct port *p_ptr;
  1222. struct tipc_msg *msg;
  1223. u32 destnode;
  1224. u32 hsz;
  1225. u32 sz;
  1226. u32 res;
  1227. p_ptr = tipc_port_deref(ref);
  1228. if (!p_ptr || !p_ptr->publ.connected)
  1229. return -EINVAL;
  1230. msg = &p_ptr->publ.phdr;
  1231. hsz = msg_hdr_sz(msg);
  1232. sz = hsz + dsz;
  1233. msg_set_size(msg, sz);
  1234. if (skb_cow(buf, hsz))
  1235. return -ENOMEM;
  1236. skb_push(buf, hsz);
  1237. memcpy(buf->data, (unchar *)msg, hsz);
  1238. destnode = msg_destnode(msg);
  1239. p_ptr->publ.congested = 1;
  1240. if (!tipc_port_congested(p_ptr)) {
  1241. if (likely(destnode != tipc_own_addr))
  1242. res = tipc_send_buf_fast(buf, destnode);
  1243. else {
  1244. tipc_port_recv_msg(buf);
  1245. res = sz;
  1246. }
  1247. if (likely(res != -ELINKCONG)) {
  1248. port_incr_out_seqno(p_ptr);
  1249. p_ptr->sent++;
  1250. p_ptr->publ.congested = 0;
  1251. return res;
  1252. }
  1253. }
  1254. if (port_unreliable(p_ptr)) {
  1255. p_ptr->publ.congested = 0;
  1256. return dsz;
  1257. }
  1258. return -ELINKCONG;
  1259. }
  1260. /**
  1261. * tipc_forward2name - forward message sections to port name
  1262. */
  1263. int tipc_forward2name(u32 ref,
  1264. struct tipc_name const *name,
  1265. u32 domain,
  1266. u32 num_sect,
  1267. struct iovec const *msg_sect,
  1268. struct tipc_portid const *orig,
  1269. unsigned int importance)
  1270. {
  1271. struct port *p_ptr;
  1272. struct tipc_msg *msg;
  1273. u32 destnode = domain;
  1274. u32 destport = 0;
  1275. int res;
  1276. p_ptr = tipc_port_deref(ref);
  1277. if (!p_ptr || p_ptr->publ.connected)
  1278. return -EINVAL;
  1279. msg = &p_ptr->publ.phdr;
  1280. msg_set_type(msg, TIPC_NAMED_MSG);
  1281. msg_set_orignode(msg, orig->node);
  1282. msg_set_origport(msg, orig->ref);
  1283. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1284. msg_set_nametype(msg, name->type);
  1285. msg_set_nameinst(msg, name->instance);
  1286. msg_set_lookup_scope(msg, addr_scope(domain));
  1287. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1288. msg_set_importance(msg,importance);
  1289. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1290. msg_set_destnode(msg, destnode);
  1291. msg_set_destport(msg, destport);
  1292. if (likely(destport || destnode)) {
  1293. p_ptr->sent++;
  1294. if (likely(destnode == tipc_own_addr))
  1295. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1296. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1297. destnode);
  1298. if (likely(res != -ELINKCONG))
  1299. return res;
  1300. if (port_unreliable(p_ptr)) {
  1301. /* Just calculate msg length and return */
  1302. return msg_calc_data_size(msg_sect, num_sect);
  1303. }
  1304. return -ELINKCONG;
  1305. }
  1306. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1307. TIPC_ERR_NO_NAME);
  1308. }
  1309. /**
  1310. * tipc_send2name - send message sections to port name
  1311. */
  1312. int tipc_send2name(u32 ref,
  1313. struct tipc_name const *name,
  1314. unsigned int domain,
  1315. unsigned int num_sect,
  1316. struct iovec const *msg_sect)
  1317. {
  1318. struct tipc_portid orig;
  1319. orig.ref = ref;
  1320. orig.node = tipc_own_addr;
  1321. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1322. TIPC_PORT_IMPORTANCE);
  1323. }
  1324. /**
  1325. * tipc_forward_buf2name - forward message buffer to port name
  1326. */
  1327. int tipc_forward_buf2name(u32 ref,
  1328. struct tipc_name const *name,
  1329. u32 domain,
  1330. struct sk_buff *buf,
  1331. unsigned int dsz,
  1332. struct tipc_portid const *orig,
  1333. unsigned int importance)
  1334. {
  1335. struct port *p_ptr;
  1336. struct tipc_msg *msg;
  1337. u32 destnode = domain;
  1338. u32 destport = 0;
  1339. int res;
  1340. p_ptr = (struct port *)tipc_ref_deref(ref);
  1341. if (!p_ptr || p_ptr->publ.connected)
  1342. return -EINVAL;
  1343. msg = &p_ptr->publ.phdr;
  1344. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1345. msg_set_importance(msg, importance);
  1346. msg_set_type(msg, TIPC_NAMED_MSG);
  1347. msg_set_orignode(msg, orig->node);
  1348. msg_set_origport(msg, orig->ref);
  1349. msg_set_nametype(msg, name->type);
  1350. msg_set_nameinst(msg, name->instance);
  1351. msg_set_lookup_scope(msg, addr_scope(domain));
  1352. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1353. msg_set_size(msg, LONG_H_SIZE + dsz);
  1354. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1355. msg_set_destnode(msg, destnode);
  1356. msg_set_destport(msg, destport);
  1357. msg_dbg(msg, "forw2name ==> ");
  1358. if (skb_cow(buf, LONG_H_SIZE))
  1359. return -ENOMEM;
  1360. skb_push(buf, LONG_H_SIZE);
  1361. memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
  1362. msg_dbg(buf_msg(buf),"PREP:");
  1363. if (likely(destport || destnode)) {
  1364. p_ptr->sent++;
  1365. if (destnode == tipc_own_addr)
  1366. return tipc_port_recv_msg(buf);
  1367. res = tipc_send_buf_fast(buf, destnode);
  1368. if (likely(res != -ELINKCONG))
  1369. return res;
  1370. if (port_unreliable(p_ptr))
  1371. return dsz;
  1372. return -ELINKCONG;
  1373. }
  1374. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1375. }
  1376. /**
  1377. * tipc_send_buf2name - send message buffer to port name
  1378. */
  1379. int tipc_send_buf2name(u32 ref,
  1380. struct tipc_name const *dest,
  1381. u32 domain,
  1382. struct sk_buff *buf,
  1383. unsigned int dsz)
  1384. {
  1385. struct tipc_portid orig;
  1386. orig.ref = ref;
  1387. orig.node = tipc_own_addr;
  1388. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1389. TIPC_PORT_IMPORTANCE);
  1390. }
  1391. /**
  1392. * tipc_forward2port - forward message sections to port identity
  1393. */
  1394. int tipc_forward2port(u32 ref,
  1395. struct tipc_portid const *dest,
  1396. unsigned int num_sect,
  1397. struct iovec const *msg_sect,
  1398. struct tipc_portid const *orig,
  1399. unsigned int importance)
  1400. {
  1401. struct port *p_ptr;
  1402. struct tipc_msg *msg;
  1403. int res;
  1404. p_ptr = tipc_port_deref(ref);
  1405. if (!p_ptr || p_ptr->publ.connected)
  1406. return -EINVAL;
  1407. msg = &p_ptr->publ.phdr;
  1408. msg_set_type(msg, TIPC_DIRECT_MSG);
  1409. msg_set_orignode(msg, orig->node);
  1410. msg_set_origport(msg, orig->ref);
  1411. msg_set_destnode(msg, dest->node);
  1412. msg_set_destport(msg, dest->ref);
  1413. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1414. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1415. msg_set_importance(msg, importance);
  1416. p_ptr->sent++;
  1417. if (dest->node == tipc_own_addr)
  1418. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1419. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1420. if (likely(res != -ELINKCONG))
  1421. return res;
  1422. if (port_unreliable(p_ptr)) {
  1423. /* Just calculate msg length and return */
  1424. return msg_calc_data_size(msg_sect, num_sect);
  1425. }
  1426. return -ELINKCONG;
  1427. }
  1428. /**
  1429. * tipc_send2port - send message sections to port identity
  1430. */
  1431. int tipc_send2port(u32 ref,
  1432. struct tipc_portid const *dest,
  1433. unsigned int num_sect,
  1434. struct iovec const *msg_sect)
  1435. {
  1436. struct tipc_portid orig;
  1437. orig.ref = ref;
  1438. orig.node = tipc_own_addr;
  1439. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1440. TIPC_PORT_IMPORTANCE);
  1441. }
  1442. /**
  1443. * tipc_forward_buf2port - forward message buffer to port identity
  1444. */
  1445. int tipc_forward_buf2port(u32 ref,
  1446. struct tipc_portid const *dest,
  1447. struct sk_buff *buf,
  1448. unsigned int dsz,
  1449. struct tipc_portid const *orig,
  1450. unsigned int importance)
  1451. {
  1452. struct port *p_ptr;
  1453. struct tipc_msg *msg;
  1454. int res;
  1455. p_ptr = (struct port *)tipc_ref_deref(ref);
  1456. if (!p_ptr || p_ptr->publ.connected)
  1457. return -EINVAL;
  1458. msg = &p_ptr->publ.phdr;
  1459. msg_set_type(msg, TIPC_DIRECT_MSG);
  1460. msg_set_orignode(msg, orig->node);
  1461. msg_set_origport(msg, orig->ref);
  1462. msg_set_destnode(msg, dest->node);
  1463. msg_set_destport(msg, dest->ref);
  1464. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1465. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1466. msg_set_importance(msg, importance);
  1467. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1468. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1469. return -ENOMEM;
  1470. skb_push(buf, DIR_MSG_H_SIZE);
  1471. memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
  1472. msg_dbg(msg, "buf2port: ");
  1473. p_ptr->sent++;
  1474. if (dest->node == tipc_own_addr)
  1475. return tipc_port_recv_msg(buf);
  1476. res = tipc_send_buf_fast(buf, dest->node);
  1477. if (likely(res != -ELINKCONG))
  1478. return res;
  1479. if (port_unreliable(p_ptr))
  1480. return dsz;
  1481. return -ELINKCONG;
  1482. }
  1483. /**
  1484. * tipc_send_buf2port - send message buffer to port identity
  1485. */
  1486. int tipc_send_buf2port(u32 ref,
  1487. struct tipc_portid const *dest,
  1488. struct sk_buff *buf,
  1489. unsigned int dsz)
  1490. {
  1491. struct tipc_portid orig;
  1492. orig.ref = ref;
  1493. orig.node = tipc_own_addr;
  1494. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1495. TIPC_PORT_IMPORTANCE);
  1496. }