port.c 41 KB

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