port.c 41 KB

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