port.c 40 KB

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