port.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2008, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "config.h"
  38. #include "port.h"
  39. #include "name_table.h"
  40. /* Connection management: */
  41. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  42. #define CONFIRMED 0
  43. #define PROBING 1
  44. #define MAX_REJECT_SIZE 1024
  45. static struct sk_buff *msg_queue_head = NULL;
  46. static struct sk_buff *msg_queue_tail = NULL;
  47. DEFINE_SPINLOCK(tipc_port_list_lock);
  48. static DEFINE_SPINLOCK(queue_lock);
  49. static LIST_HEAD(ports);
  50. static void port_handle_node_down(unsigned long ref);
  51. static struct sk_buff *port_build_self_abort_msg(struct port *, u32 err);
  52. static struct sk_buff *port_build_peer_abort_msg(struct port *, u32 err);
  53. static void port_timeout(unsigned long ref);
  54. static u32 port_peernode(struct port *p_ptr)
  55. {
  56. return msg_destnode(&p_ptr->publ.phdr);
  57. }
  58. static u32 port_peerport(struct port *p_ptr)
  59. {
  60. return msg_destport(&p_ptr->publ.phdr);
  61. }
  62. static u32 port_out_seqno(struct port *p_ptr)
  63. {
  64. return msg_transp_seqno(&p_ptr->publ.phdr);
  65. }
  66. static void port_incr_out_seqno(struct port *p_ptr)
  67. {
  68. struct tipc_msg *m = &p_ptr->publ.phdr;
  69. if (likely(!msg_routed(m)))
  70. return;
  71. msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
  72. }
  73. /**
  74. * tipc_multicast - send a multicast message to local and remote destinations
  75. */
  76. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
  77. u32 num_sect, struct iovec const *msg_sect)
  78. {
  79. struct tipc_msg *hdr;
  80. struct sk_buff *buf;
  81. struct sk_buff *ibuf = NULL;
  82. struct port_list dports = {0, NULL, };
  83. struct port *oport = tipc_port_deref(ref);
  84. int ext_targets;
  85. int res;
  86. if (unlikely(!oport))
  87. return -EINVAL;
  88. /* Create multicast message */
  89. hdr = &oport->publ.phdr;
  90. msg_set_type(hdr, TIPC_MCAST_MSG);
  91. msg_set_nametype(hdr, seq->type);
  92. msg_set_namelower(hdr, seq->lower);
  93. msg_set_nameupper(hdr, seq->upper);
  94. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  95. res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  96. !oport->user_port, &buf);
  97. if (unlikely(!buf))
  98. return res;
  99. /* Figure out where to send multicast message */
  100. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  101. TIPC_NODE_SCOPE, &dports);
  102. /* Send message to destinations (duplicate it only if necessary) */
  103. if (ext_targets) {
  104. if (dports.count != 0) {
  105. ibuf = skb_copy(buf, GFP_ATOMIC);
  106. if (ibuf == NULL) {
  107. tipc_port_list_free(&dports);
  108. buf_discard(buf);
  109. return -ENOMEM;
  110. }
  111. }
  112. res = tipc_bclink_send_msg(buf);
  113. if ((res < 0) && (dports.count != 0)) {
  114. buf_discard(ibuf);
  115. }
  116. } else {
  117. ibuf = buf;
  118. }
  119. if (res >= 0) {
  120. if (ibuf)
  121. tipc_port_recv_mcast(ibuf, &dports);
  122. } else {
  123. tipc_port_list_free(&dports);
  124. }
  125. return res;
  126. }
  127. /**
  128. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  129. *
  130. * If there is no port list, perform a lookup to create one
  131. */
  132. void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
  133. {
  134. struct tipc_msg *msg;
  135. struct port_list dports = {0, NULL, };
  136. struct port_list *item = dp;
  137. int cnt = 0;
  138. msg = buf_msg(buf);
  139. /* Create destination port list, if one wasn't supplied */
  140. if (dp == NULL) {
  141. tipc_nametbl_mc_translate(msg_nametype(msg),
  142. msg_namelower(msg),
  143. msg_nameupper(msg),
  144. TIPC_CLUSTER_SCOPE,
  145. &dports);
  146. item = dp = &dports;
  147. }
  148. /* Deliver a copy of message to each destination port */
  149. if (dp->count != 0) {
  150. if (dp->count == 1) {
  151. msg_set_destport(msg, dp->ports[0]);
  152. tipc_port_recv_msg(buf);
  153. tipc_port_list_free(dp);
  154. return;
  155. }
  156. for (; cnt < dp->count; cnt++) {
  157. int index = cnt % PLSIZE;
  158. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  159. if (b == NULL) {
  160. warn("Unable to deliver multicast message(s)\n");
  161. goto exit;
  162. }
  163. if ((index == 0) && (cnt != 0)) {
  164. item = item->next;
  165. }
  166. msg_set_destport(buf_msg(b), item->ports[index]);
  167. tipc_port_recv_msg(b);
  168. }
  169. }
  170. exit:
  171. buf_discard(buf);
  172. tipc_port_list_free(dp);
  173. }
  174. /**
  175. * tipc_createport_raw - create a generic TIPC port
  176. *
  177. * Returns pointer to (locked) TIPC port, or NULL if unable to create it
  178. */
  179. struct tipc_port *tipc_createport_raw(void *usr_handle,
  180. u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
  181. void (*wakeup)(struct tipc_port *),
  182. const u32 importance)
  183. {
  184. struct port *p_ptr;
  185. struct tipc_msg *msg;
  186. u32 ref;
  187. p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
  188. if (!p_ptr) {
  189. warn("Port creation failed, no memory\n");
  190. return NULL;
  191. }
  192. ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
  193. if (!ref) {
  194. warn("Port creation failed, reference table exhausted\n");
  195. kfree(p_ptr);
  196. return NULL;
  197. }
  198. p_ptr->publ.usr_handle = usr_handle;
  199. p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
  200. p_ptr->publ.ref = ref;
  201. msg = &p_ptr->publ.phdr;
  202. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
  203. msg_set_origport(msg, ref);
  204. p_ptr->last_in_seqno = 41;
  205. p_ptr->sent = 1;
  206. INIT_LIST_HEAD(&p_ptr->wait_list);
  207. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  208. p_ptr->dispatcher = dispatcher;
  209. p_ptr->wakeup = wakeup;
  210. p_ptr->user_port = NULL;
  211. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  212. spin_lock_bh(&tipc_port_list_lock);
  213. INIT_LIST_HEAD(&p_ptr->publications);
  214. INIT_LIST_HEAD(&p_ptr->port_list);
  215. list_add_tail(&p_ptr->port_list, &ports);
  216. spin_unlock_bh(&tipc_port_list_lock);
  217. return &(p_ptr->publ);
  218. }
  219. int tipc_deleteport(u32 ref)
  220. {
  221. struct port *p_ptr;
  222. struct sk_buff *buf = NULL;
  223. tipc_withdraw(ref, 0, NULL);
  224. p_ptr = tipc_port_lock(ref);
  225. if (!p_ptr)
  226. return -EINVAL;
  227. tipc_ref_discard(ref);
  228. tipc_port_unlock(p_ptr);
  229. k_cancel_timer(&p_ptr->timer);
  230. if (p_ptr->publ.connected) {
  231. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  232. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  233. }
  234. kfree(p_ptr->user_port);
  235. spin_lock_bh(&tipc_port_list_lock);
  236. list_del(&p_ptr->port_list);
  237. list_del(&p_ptr->wait_list);
  238. spin_unlock_bh(&tipc_port_list_lock);
  239. k_term_timer(&p_ptr->timer);
  240. kfree(p_ptr);
  241. tipc_net_route_msg(buf);
  242. return 0;
  243. }
  244. static int port_unreliable(struct port *p_ptr)
  245. {
  246. return msg_src_droppable(&p_ptr->publ.phdr);
  247. }
  248. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  249. {
  250. struct port *p_ptr;
  251. p_ptr = tipc_port_lock(ref);
  252. if (!p_ptr)
  253. return -EINVAL;
  254. *isunreliable = port_unreliable(p_ptr);
  255. tipc_port_unlock(p_ptr);
  256. return 0;
  257. }
  258. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  259. {
  260. struct port *p_ptr;
  261. p_ptr = tipc_port_lock(ref);
  262. if (!p_ptr)
  263. return -EINVAL;
  264. msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
  265. tipc_port_unlock(p_ptr);
  266. return 0;
  267. }
  268. static int port_unreturnable(struct port *p_ptr)
  269. {
  270. return msg_dest_droppable(&p_ptr->publ.phdr);
  271. }
  272. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  273. {
  274. struct port *p_ptr;
  275. p_ptr = tipc_port_lock(ref);
  276. if (!p_ptr)
  277. return -EINVAL;
  278. *isunrejectable = port_unreturnable(p_ptr);
  279. tipc_port_unlock(p_ptr);
  280. return 0;
  281. }
  282. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  283. {
  284. struct port *p_ptr;
  285. p_ptr = tipc_port_lock(ref);
  286. if (!p_ptr)
  287. return -EINVAL;
  288. msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
  289. tipc_port_unlock(p_ptr);
  290. return 0;
  291. }
  292. /*
  293. * port_build_proto_msg(): build a port level protocol
  294. * or a connection abortion message. Called with
  295. * tipc_port lock on.
  296. */
  297. static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
  298. u32 origport, u32 orignode,
  299. u32 usr, u32 type, u32 err,
  300. u32 seqno, u32 ack)
  301. {
  302. struct sk_buff *buf;
  303. struct tipc_msg *msg;
  304. buf = tipc_buf_acquire(LONG_H_SIZE);
  305. if (buf) {
  306. msg = buf_msg(buf);
  307. tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
  308. msg_set_errcode(msg, err);
  309. msg_set_destport(msg, destport);
  310. msg_set_origport(msg, origport);
  311. msg_set_orignode(msg, orignode);
  312. msg_set_transp_seqno(msg, seqno);
  313. msg_set_msgcnt(msg, ack);
  314. }
  315. return buf;
  316. }
  317. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  318. {
  319. struct tipc_msg *msg = buf_msg(buf);
  320. struct sk_buff *rbuf;
  321. struct tipc_msg *rmsg;
  322. int hdr_sz;
  323. u32 imp = msg_importance(msg);
  324. u32 data_sz = msg_data_sz(msg);
  325. if (data_sz > MAX_REJECT_SIZE)
  326. data_sz = MAX_REJECT_SIZE;
  327. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  328. imp++;
  329. /* discard rejected message if it shouldn't be returned to sender */
  330. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  331. buf_discard(buf);
  332. return data_sz;
  333. }
  334. /* construct rejected message */
  335. if (msg_mcast(msg))
  336. hdr_sz = MCAST_H_SIZE;
  337. else
  338. hdr_sz = LONG_H_SIZE;
  339. rbuf = tipc_buf_acquire(data_sz + hdr_sz);
  340. if (rbuf == NULL) {
  341. buf_discard(buf);
  342. return data_sz;
  343. }
  344. rmsg = buf_msg(rbuf);
  345. tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
  346. msg_set_errcode(rmsg, err);
  347. msg_set_destport(rmsg, msg_origport(msg));
  348. msg_set_origport(rmsg, msg_destport(msg));
  349. if (msg_short(msg)) {
  350. msg_set_orignode(rmsg, tipc_own_addr);
  351. /* leave name type & instance as zeroes */
  352. } else {
  353. msg_set_orignode(rmsg, msg_destnode(msg));
  354. msg_set_nametype(rmsg, msg_nametype(msg));
  355. msg_set_nameinst(rmsg, msg_nameinst(msg));
  356. }
  357. msg_set_size(rmsg, data_sz + hdr_sz);
  358. skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
  359. /* send self-abort message when rejecting on a connected port */
  360. if (msg_connected(msg)) {
  361. struct sk_buff *abuf = NULL;
  362. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  363. if (p_ptr) {
  364. if (p_ptr->publ.connected)
  365. abuf = port_build_self_abort_msg(p_ptr, err);
  366. tipc_port_unlock(p_ptr);
  367. }
  368. tipc_net_route_msg(abuf);
  369. }
  370. /* send rejected message */
  371. buf_discard(buf);
  372. tipc_net_route_msg(rbuf);
  373. return data_sz;
  374. }
  375. int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
  376. struct iovec const *msg_sect, u32 num_sect,
  377. int err)
  378. {
  379. struct sk_buff *buf;
  380. int res;
  381. res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  382. !p_ptr->user_port, &buf);
  383. if (!buf)
  384. return res;
  385. return tipc_reject_msg(buf, err);
  386. }
  387. static void port_timeout(unsigned long ref)
  388. {
  389. struct port *p_ptr = tipc_port_lock(ref);
  390. struct sk_buff *buf = NULL;
  391. if (!p_ptr)
  392. return;
  393. if (!p_ptr->publ.connected) {
  394. tipc_port_unlock(p_ptr);
  395. return;
  396. }
  397. /* Last probe answered ? */
  398. if (p_ptr->probing_state == PROBING) {
  399. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  400. } else {
  401. buf = port_build_proto_msg(port_peerport(p_ptr),
  402. port_peernode(p_ptr),
  403. p_ptr->publ.ref,
  404. tipc_own_addr,
  405. CONN_MANAGER,
  406. CONN_PROBE,
  407. TIPC_OK,
  408. port_out_seqno(p_ptr),
  409. 0);
  410. port_incr_out_seqno(p_ptr);
  411. p_ptr->probing_state = PROBING;
  412. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  413. }
  414. tipc_port_unlock(p_ptr);
  415. tipc_net_route_msg(buf);
  416. }
  417. static void port_handle_node_down(unsigned long ref)
  418. {
  419. struct port *p_ptr = tipc_port_lock(ref);
  420. struct sk_buff *buf = NULL;
  421. if (!p_ptr)
  422. return;
  423. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  424. tipc_port_unlock(p_ptr);
  425. tipc_net_route_msg(buf);
  426. }
  427. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  428. {
  429. u32 imp = msg_importance(&p_ptr->publ.phdr);
  430. if (!p_ptr->publ.connected)
  431. return NULL;
  432. if (imp < TIPC_CRITICAL_IMPORTANCE)
  433. imp++;
  434. return port_build_proto_msg(p_ptr->publ.ref,
  435. tipc_own_addr,
  436. port_peerport(p_ptr),
  437. port_peernode(p_ptr),
  438. imp,
  439. TIPC_CONN_MSG,
  440. err,
  441. p_ptr->last_in_seqno + 1,
  442. 0);
  443. }
  444. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  445. {
  446. u32 imp = msg_importance(&p_ptr->publ.phdr);
  447. if (!p_ptr->publ.connected)
  448. return NULL;
  449. if (imp < TIPC_CRITICAL_IMPORTANCE)
  450. imp++;
  451. return port_build_proto_msg(port_peerport(p_ptr),
  452. port_peernode(p_ptr),
  453. p_ptr->publ.ref,
  454. tipc_own_addr,
  455. imp,
  456. TIPC_CONN_MSG,
  457. err,
  458. port_out_seqno(p_ptr),
  459. 0);
  460. }
  461. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  462. {
  463. struct tipc_msg *msg = buf_msg(buf);
  464. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  465. u32 err = TIPC_OK;
  466. struct sk_buff *r_buf = NULL;
  467. struct sk_buff *abort_buf = NULL;
  468. if (!p_ptr) {
  469. err = TIPC_ERR_NO_PORT;
  470. } else if (p_ptr->publ.connected) {
  471. if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
  472. (port_peerport(p_ptr) != msg_origport(msg))) {
  473. err = TIPC_ERR_NO_PORT;
  474. } else if (msg_type(msg) == CONN_ACK) {
  475. int wakeup = tipc_port_congested(p_ptr) &&
  476. p_ptr->publ.congested &&
  477. p_ptr->wakeup;
  478. p_ptr->acked += msg_msgcnt(msg);
  479. if (tipc_port_congested(p_ptr))
  480. goto exit;
  481. p_ptr->publ.congested = 0;
  482. if (!wakeup)
  483. goto exit;
  484. p_ptr->wakeup(&p_ptr->publ);
  485. goto exit;
  486. }
  487. } else if (p_ptr->publ.published) {
  488. err = TIPC_ERR_NO_PORT;
  489. }
  490. if (err) {
  491. r_buf = port_build_proto_msg(msg_origport(msg),
  492. msg_orignode(msg),
  493. msg_destport(msg),
  494. tipc_own_addr,
  495. TIPC_HIGH_IMPORTANCE,
  496. TIPC_CONN_MSG,
  497. err,
  498. 0,
  499. 0);
  500. goto exit;
  501. }
  502. /* All is fine */
  503. if (msg_type(msg) == CONN_PROBE) {
  504. r_buf = port_build_proto_msg(msg_origport(msg),
  505. msg_orignode(msg),
  506. msg_destport(msg),
  507. tipc_own_addr,
  508. CONN_MANAGER,
  509. CONN_PROBE_REPLY,
  510. TIPC_OK,
  511. port_out_seqno(p_ptr),
  512. 0);
  513. }
  514. p_ptr->probing_state = CONFIRMED;
  515. port_incr_out_seqno(p_ptr);
  516. exit:
  517. if (p_ptr)
  518. tipc_port_unlock(p_ptr);
  519. tipc_net_route_msg(r_buf);
  520. tipc_net_route_msg(abort_buf);
  521. buf_discard(buf);
  522. }
  523. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  524. {
  525. struct publication *publ;
  526. if (full_id)
  527. tipc_printf(buf, "<%u.%u.%u:%u>:",
  528. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  529. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  530. else
  531. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  532. if (p_ptr->publ.connected) {
  533. u32 dport = port_peerport(p_ptr);
  534. u32 destnode = port_peernode(p_ptr);
  535. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  536. tipc_zone(destnode), tipc_cluster(destnode),
  537. tipc_node(destnode), dport);
  538. if (p_ptr->publ.conn_type != 0)
  539. tipc_printf(buf, " via {%u,%u}",
  540. p_ptr->publ.conn_type,
  541. p_ptr->publ.conn_instance);
  542. } else if (p_ptr->publ.published) {
  543. tipc_printf(buf, " bound to");
  544. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  545. if (publ->lower == publ->upper)
  546. tipc_printf(buf, " {%u,%u}", publ->type,
  547. publ->lower);
  548. else
  549. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  550. publ->lower, publ->upper);
  551. }
  552. }
  553. tipc_printf(buf, "\n");
  554. }
  555. #define MAX_PORT_QUERY 32768
  556. struct sk_buff *tipc_port_get_ports(void)
  557. {
  558. struct sk_buff *buf;
  559. struct tlv_desc *rep_tlv;
  560. struct print_buf pb;
  561. struct port *p_ptr;
  562. int str_len;
  563. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  564. if (!buf)
  565. return NULL;
  566. rep_tlv = (struct tlv_desc *)buf->data;
  567. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  568. spin_lock_bh(&tipc_port_list_lock);
  569. list_for_each_entry(p_ptr, &ports, port_list) {
  570. spin_lock_bh(p_ptr->publ.lock);
  571. port_print(p_ptr, &pb, 0);
  572. spin_unlock_bh(p_ptr->publ.lock);
  573. }
  574. spin_unlock_bh(&tipc_port_list_lock);
  575. str_len = tipc_printbuf_validate(&pb);
  576. skb_put(buf, TLV_SPACE(str_len));
  577. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  578. return buf;
  579. }
  580. void tipc_port_reinit(void)
  581. {
  582. struct port *p_ptr;
  583. struct tipc_msg *msg;
  584. spin_lock_bh(&tipc_port_list_lock);
  585. list_for_each_entry(p_ptr, &ports, port_list) {
  586. msg = &p_ptr->publ.phdr;
  587. if (msg_orignode(msg) == tipc_own_addr)
  588. break;
  589. msg_set_prevnode(msg, tipc_own_addr);
  590. msg_set_orignode(msg, tipc_own_addr);
  591. }
  592. spin_unlock_bh(&tipc_port_list_lock);
  593. }
  594. /*
  595. * port_dispatcher_sigh(): Signal handler for messages destinated
  596. * to the tipc_port interface.
  597. */
  598. static void port_dispatcher_sigh(void *dummy)
  599. {
  600. struct sk_buff *buf;
  601. spin_lock_bh(&queue_lock);
  602. buf = msg_queue_head;
  603. msg_queue_head = NULL;
  604. spin_unlock_bh(&queue_lock);
  605. while (buf) {
  606. struct port *p_ptr;
  607. struct user_port *up_ptr;
  608. struct tipc_portid orig;
  609. struct tipc_name_seq dseq;
  610. void *usr_handle;
  611. int connected;
  612. int published;
  613. u32 message_type;
  614. struct sk_buff *next = buf->next;
  615. struct tipc_msg *msg = buf_msg(buf);
  616. u32 dref = msg_destport(msg);
  617. message_type = msg_type(msg);
  618. if (message_type > TIPC_DIRECT_MSG)
  619. goto reject; /* Unsupported message type */
  620. p_ptr = tipc_port_lock(dref);
  621. if (!p_ptr)
  622. goto reject; /* Port deleted while msg in queue */
  623. orig.ref = msg_origport(msg);
  624. orig.node = msg_orignode(msg);
  625. up_ptr = p_ptr->user_port;
  626. usr_handle = up_ptr->usr_handle;
  627. connected = p_ptr->publ.connected;
  628. published = p_ptr->publ.published;
  629. if (unlikely(msg_errcode(msg)))
  630. goto err;
  631. switch (message_type) {
  632. case TIPC_CONN_MSG:{
  633. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  634. u32 peer_port = port_peerport(p_ptr);
  635. u32 peer_node = port_peernode(p_ptr);
  636. tipc_port_unlock(p_ptr);
  637. if (unlikely(!cb))
  638. goto reject;
  639. if (unlikely(!connected)) {
  640. if (tipc_connect2port(dref, &orig))
  641. goto reject;
  642. } else if ((msg_origport(msg) != peer_port) ||
  643. (msg_orignode(msg) != peer_node))
  644. goto reject;
  645. if (unlikely(++p_ptr->publ.conn_unacked >=
  646. TIPC_FLOW_CONTROL_WIN))
  647. tipc_acknowledge(dref,
  648. p_ptr->publ.conn_unacked);
  649. skb_pull(buf, msg_hdr_sz(msg));
  650. cb(usr_handle, dref, &buf, msg_data(msg),
  651. msg_data_sz(msg));
  652. break;
  653. }
  654. case TIPC_DIRECT_MSG:{
  655. tipc_msg_event cb = up_ptr->msg_cb;
  656. tipc_port_unlock(p_ptr);
  657. if (unlikely(!cb || connected))
  658. goto reject;
  659. skb_pull(buf, msg_hdr_sz(msg));
  660. cb(usr_handle, dref, &buf, msg_data(msg),
  661. msg_data_sz(msg), msg_importance(msg),
  662. &orig);
  663. break;
  664. }
  665. case TIPC_MCAST_MSG:
  666. case TIPC_NAMED_MSG:{
  667. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  668. tipc_port_unlock(p_ptr);
  669. if (unlikely(!cb || connected || !published))
  670. goto reject;
  671. dseq.type = msg_nametype(msg);
  672. dseq.lower = msg_nameinst(msg);
  673. dseq.upper = (message_type == TIPC_NAMED_MSG)
  674. ? dseq.lower : msg_nameupper(msg);
  675. skb_pull(buf, msg_hdr_sz(msg));
  676. cb(usr_handle, dref, &buf, msg_data(msg),
  677. msg_data_sz(msg), msg_importance(msg),
  678. &orig, &dseq);
  679. break;
  680. }
  681. }
  682. if (buf)
  683. buf_discard(buf);
  684. buf = next;
  685. continue;
  686. err:
  687. switch (message_type) {
  688. case TIPC_CONN_MSG:{
  689. tipc_conn_shutdown_event cb =
  690. up_ptr->conn_err_cb;
  691. u32 peer_port = port_peerport(p_ptr);
  692. u32 peer_node = port_peernode(p_ptr);
  693. tipc_port_unlock(p_ptr);
  694. if (!cb || !connected)
  695. break;
  696. if ((msg_origport(msg) != peer_port) ||
  697. (msg_orignode(msg) != peer_node))
  698. break;
  699. tipc_disconnect(dref);
  700. skb_pull(buf, msg_hdr_sz(msg));
  701. cb(usr_handle, dref, &buf, msg_data(msg),
  702. msg_data_sz(msg), msg_errcode(msg));
  703. break;
  704. }
  705. case TIPC_DIRECT_MSG:{
  706. tipc_msg_err_event cb = up_ptr->err_cb;
  707. tipc_port_unlock(p_ptr);
  708. if (!cb || connected)
  709. break;
  710. skb_pull(buf, msg_hdr_sz(msg));
  711. cb(usr_handle, dref, &buf, msg_data(msg),
  712. msg_data_sz(msg), msg_errcode(msg), &orig);
  713. break;
  714. }
  715. case TIPC_MCAST_MSG:
  716. case TIPC_NAMED_MSG:{
  717. tipc_named_msg_err_event cb =
  718. up_ptr->named_err_cb;
  719. tipc_port_unlock(p_ptr);
  720. if (!cb || connected)
  721. break;
  722. dseq.type = msg_nametype(msg);
  723. dseq.lower = msg_nameinst(msg);
  724. dseq.upper = (message_type == TIPC_NAMED_MSG)
  725. ? dseq.lower : msg_nameupper(msg);
  726. skb_pull(buf, msg_hdr_sz(msg));
  727. cb(usr_handle, dref, &buf, msg_data(msg),
  728. msg_data_sz(msg), msg_errcode(msg), &dseq);
  729. break;
  730. }
  731. }
  732. if (buf)
  733. buf_discard(buf);
  734. buf = next;
  735. continue;
  736. reject:
  737. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  738. buf = next;
  739. }
  740. }
  741. /*
  742. * port_dispatcher(): Dispatcher for messages destinated
  743. * to the tipc_port interface. Called with port locked.
  744. */
  745. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  746. {
  747. buf->next = NULL;
  748. spin_lock_bh(&queue_lock);
  749. if (msg_queue_head) {
  750. msg_queue_tail->next = buf;
  751. msg_queue_tail = buf;
  752. } else {
  753. msg_queue_tail = msg_queue_head = buf;
  754. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  755. }
  756. spin_unlock_bh(&queue_lock);
  757. return 0;
  758. }
  759. /*
  760. * Wake up port after congestion: Called with port locked,
  761. *
  762. */
  763. static void port_wakeup_sh(unsigned long ref)
  764. {
  765. struct port *p_ptr;
  766. struct user_port *up_ptr;
  767. tipc_continue_event cb = NULL;
  768. void *uh = NULL;
  769. p_ptr = tipc_port_lock(ref);
  770. if (p_ptr) {
  771. up_ptr = p_ptr->user_port;
  772. if (up_ptr) {
  773. cb = up_ptr->continue_event_cb;
  774. uh = up_ptr->usr_handle;
  775. }
  776. tipc_port_unlock(p_ptr);
  777. }
  778. if (cb)
  779. cb(uh, ref);
  780. }
  781. static void port_wakeup(struct tipc_port *p_ptr)
  782. {
  783. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  784. }
  785. void tipc_acknowledge(u32 ref, u32 ack)
  786. {
  787. struct port *p_ptr;
  788. struct sk_buff *buf = NULL;
  789. p_ptr = tipc_port_lock(ref);
  790. if (!p_ptr)
  791. return;
  792. if (p_ptr->publ.connected) {
  793. p_ptr->publ.conn_unacked -= ack;
  794. buf = port_build_proto_msg(port_peerport(p_ptr),
  795. port_peernode(p_ptr),
  796. ref,
  797. tipc_own_addr,
  798. CONN_MANAGER,
  799. CONN_ACK,
  800. TIPC_OK,
  801. port_out_seqno(p_ptr),
  802. ack);
  803. }
  804. tipc_port_unlock(p_ptr);
  805. tipc_net_route_msg(buf);
  806. }
  807. /*
  808. * tipc_createport(): user level call.
  809. */
  810. int tipc_createport(void *usr_handle,
  811. unsigned int importance,
  812. tipc_msg_err_event error_cb,
  813. tipc_named_msg_err_event named_error_cb,
  814. tipc_conn_shutdown_event conn_error_cb,
  815. tipc_msg_event msg_cb,
  816. tipc_named_msg_event named_msg_cb,
  817. tipc_conn_msg_event conn_msg_cb,
  818. tipc_continue_event continue_event_cb,/* May be zero */
  819. u32 *portref)
  820. {
  821. struct user_port *up_ptr;
  822. struct port *p_ptr;
  823. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  824. if (!up_ptr) {
  825. warn("Port creation failed, no memory\n");
  826. return -ENOMEM;
  827. }
  828. p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
  829. port_wakeup, importance);
  830. if (!p_ptr) {
  831. kfree(up_ptr);
  832. return -ENOMEM;
  833. }
  834. p_ptr->user_port = up_ptr;
  835. up_ptr->usr_handle = usr_handle;
  836. up_ptr->ref = p_ptr->publ.ref;
  837. up_ptr->err_cb = error_cb;
  838. up_ptr->named_err_cb = named_error_cb;
  839. up_ptr->conn_err_cb = conn_error_cb;
  840. up_ptr->msg_cb = msg_cb;
  841. up_ptr->named_msg_cb = named_msg_cb;
  842. up_ptr->conn_msg_cb = conn_msg_cb;
  843. up_ptr->continue_event_cb = continue_event_cb;
  844. *portref = p_ptr->publ.ref;
  845. tipc_port_unlock(p_ptr);
  846. return 0;
  847. }
  848. int tipc_portimportance(u32 ref, unsigned int *importance)
  849. {
  850. struct port *p_ptr;
  851. p_ptr = tipc_port_lock(ref);
  852. if (!p_ptr)
  853. return -EINVAL;
  854. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  855. tipc_port_unlock(p_ptr);
  856. return 0;
  857. }
  858. int tipc_set_portimportance(u32 ref, unsigned int imp)
  859. {
  860. struct port *p_ptr;
  861. if (imp > TIPC_CRITICAL_IMPORTANCE)
  862. return -EINVAL;
  863. p_ptr = tipc_port_lock(ref);
  864. if (!p_ptr)
  865. return -EINVAL;
  866. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  867. tipc_port_unlock(p_ptr);
  868. return 0;
  869. }
  870. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  871. {
  872. struct port *p_ptr;
  873. struct publication *publ;
  874. u32 key;
  875. int res = -EINVAL;
  876. p_ptr = tipc_port_lock(ref);
  877. if (!p_ptr)
  878. return -EINVAL;
  879. if (p_ptr->publ.connected)
  880. goto exit;
  881. if (seq->lower > seq->upper)
  882. goto exit;
  883. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  884. goto exit;
  885. key = ref + p_ptr->pub_count + 1;
  886. if (key == ref) {
  887. res = -EADDRINUSE;
  888. goto exit;
  889. }
  890. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  891. scope, p_ptr->publ.ref, key);
  892. if (publ) {
  893. list_add(&publ->pport_list, &p_ptr->publications);
  894. p_ptr->pub_count++;
  895. p_ptr->publ.published = 1;
  896. res = 0;
  897. }
  898. exit:
  899. tipc_port_unlock(p_ptr);
  900. return res;
  901. }
  902. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  903. {
  904. struct port *p_ptr;
  905. struct publication *publ;
  906. struct publication *tpubl;
  907. int res = -EINVAL;
  908. p_ptr = tipc_port_lock(ref);
  909. if (!p_ptr)
  910. return -EINVAL;
  911. if (!seq) {
  912. list_for_each_entry_safe(publ, tpubl,
  913. &p_ptr->publications, pport_list) {
  914. tipc_nametbl_withdraw(publ->type, publ->lower,
  915. publ->ref, publ->key);
  916. }
  917. res = 0;
  918. } else {
  919. list_for_each_entry_safe(publ, tpubl,
  920. &p_ptr->publications, pport_list) {
  921. if (publ->scope != scope)
  922. continue;
  923. if (publ->type != seq->type)
  924. continue;
  925. if (publ->lower != seq->lower)
  926. continue;
  927. if (publ->upper != seq->upper)
  928. break;
  929. tipc_nametbl_withdraw(publ->type, publ->lower,
  930. publ->ref, publ->key);
  931. res = 0;
  932. break;
  933. }
  934. }
  935. if (list_empty(&p_ptr->publications))
  936. p_ptr->publ.published = 0;
  937. tipc_port_unlock(p_ptr);
  938. return res;
  939. }
  940. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  941. {
  942. struct port *p_ptr;
  943. struct tipc_msg *msg;
  944. int res = -EINVAL;
  945. p_ptr = tipc_port_lock(ref);
  946. if (!p_ptr)
  947. return -EINVAL;
  948. if (p_ptr->publ.published || p_ptr->publ.connected)
  949. goto exit;
  950. if (!peer->ref)
  951. goto exit;
  952. msg = &p_ptr->publ.phdr;
  953. msg_set_destnode(msg, peer->node);
  954. msg_set_destport(msg, peer->ref);
  955. msg_set_orignode(msg, tipc_own_addr);
  956. msg_set_origport(msg, p_ptr->publ.ref);
  957. msg_set_transp_seqno(msg, 42);
  958. msg_set_type(msg, TIPC_CONN_MSG);
  959. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  960. p_ptr->probing_interval = PROBING_INTERVAL;
  961. p_ptr->probing_state = CONFIRMED;
  962. p_ptr->publ.connected = 1;
  963. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  964. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  965. (void *)(unsigned long)ref,
  966. (net_ev_handler)port_handle_node_down);
  967. res = 0;
  968. exit:
  969. tipc_port_unlock(p_ptr);
  970. p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  971. return res;
  972. }
  973. /**
  974. * tipc_disconnect_port - disconnect port from peer
  975. *
  976. * Port must be locked.
  977. */
  978. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  979. {
  980. int res;
  981. if (tp_ptr->connected) {
  982. tp_ptr->connected = 0;
  983. /* let timer expire on it's own to avoid deadlock! */
  984. tipc_nodesub_unsubscribe(
  985. &((struct port *)tp_ptr)->subscription);
  986. res = 0;
  987. } else {
  988. res = -ENOTCONN;
  989. }
  990. return res;
  991. }
  992. /*
  993. * tipc_disconnect(): Disconnect port form peer.
  994. * This is a node local operation.
  995. */
  996. int tipc_disconnect(u32 ref)
  997. {
  998. struct port *p_ptr;
  999. int res;
  1000. p_ptr = tipc_port_lock(ref);
  1001. if (!p_ptr)
  1002. return -EINVAL;
  1003. res = tipc_disconnect_port((struct tipc_port *)p_ptr);
  1004. tipc_port_unlock(p_ptr);
  1005. return res;
  1006. }
  1007. /*
  1008. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1009. */
  1010. int tipc_shutdown(u32 ref)
  1011. {
  1012. struct port *p_ptr;
  1013. struct sk_buff *buf = NULL;
  1014. p_ptr = tipc_port_lock(ref);
  1015. if (!p_ptr)
  1016. return -EINVAL;
  1017. if (p_ptr->publ.connected) {
  1018. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1019. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1020. imp++;
  1021. buf = port_build_proto_msg(port_peerport(p_ptr),
  1022. port_peernode(p_ptr),
  1023. ref,
  1024. tipc_own_addr,
  1025. imp,
  1026. TIPC_CONN_MSG,
  1027. TIPC_CONN_SHUTDOWN,
  1028. port_out_seqno(p_ptr),
  1029. 0);
  1030. }
  1031. tipc_port_unlock(p_ptr);
  1032. tipc_net_route_msg(buf);
  1033. return tipc_disconnect(ref);
  1034. }
  1035. /*
  1036. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1037. * message for this node.
  1038. */
  1039. static int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1040. struct iovec const *msg_sect)
  1041. {
  1042. struct sk_buff *buf;
  1043. int res;
  1044. res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1045. MAX_MSG_SIZE, !sender->user_port, &buf);
  1046. if (likely(buf))
  1047. tipc_port_recv_msg(buf);
  1048. return res;
  1049. }
  1050. /**
  1051. * tipc_send - send message sections on connection
  1052. */
  1053. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1054. {
  1055. struct port *p_ptr;
  1056. u32 destnode;
  1057. int res;
  1058. p_ptr = tipc_port_deref(ref);
  1059. if (!p_ptr || !p_ptr->publ.connected)
  1060. return -EINVAL;
  1061. p_ptr->publ.congested = 1;
  1062. if (!tipc_port_congested(p_ptr)) {
  1063. destnode = port_peernode(p_ptr);
  1064. if (likely(destnode != tipc_own_addr))
  1065. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1066. destnode);
  1067. else
  1068. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1069. if (likely(res != -ELINKCONG)) {
  1070. port_incr_out_seqno(p_ptr);
  1071. p_ptr->publ.congested = 0;
  1072. p_ptr->sent++;
  1073. return res;
  1074. }
  1075. }
  1076. if (port_unreliable(p_ptr)) {
  1077. p_ptr->publ.congested = 0;
  1078. /* Just calculate msg length and return */
  1079. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1080. }
  1081. return -ELINKCONG;
  1082. }
  1083. /**
  1084. * tipc_send2name - send message sections to port name
  1085. */
  1086. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  1087. unsigned int num_sect, struct iovec const *msg_sect)
  1088. {
  1089. struct port *p_ptr;
  1090. struct tipc_msg *msg;
  1091. u32 destnode = domain;
  1092. u32 destport;
  1093. int res;
  1094. p_ptr = tipc_port_deref(ref);
  1095. if (!p_ptr || p_ptr->publ.connected)
  1096. return -EINVAL;
  1097. msg = &p_ptr->publ.phdr;
  1098. msg_set_type(msg, TIPC_NAMED_MSG);
  1099. msg_set_orignode(msg, tipc_own_addr);
  1100. msg_set_origport(msg, ref);
  1101. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1102. msg_set_nametype(msg, name->type);
  1103. msg_set_nameinst(msg, name->instance);
  1104. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  1105. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1106. msg_set_destnode(msg, destnode);
  1107. msg_set_destport(msg, destport);
  1108. if (likely(destport)) {
  1109. p_ptr->sent++;
  1110. if (likely(destnode == tipc_own_addr))
  1111. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1112. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1113. destnode);
  1114. if (likely(res != -ELINKCONG))
  1115. return res;
  1116. if (port_unreliable(p_ptr)) {
  1117. /* Just calculate msg length and return */
  1118. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1119. }
  1120. return -ELINKCONG;
  1121. }
  1122. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1123. TIPC_ERR_NO_NAME);
  1124. }
  1125. /**
  1126. * tipc_send2port - send message sections to port identity
  1127. */
  1128. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  1129. unsigned int num_sect, struct iovec const *msg_sect)
  1130. {
  1131. struct port *p_ptr;
  1132. struct tipc_msg *msg;
  1133. int res;
  1134. p_ptr = tipc_port_deref(ref);
  1135. if (!p_ptr || p_ptr->publ.connected)
  1136. return -EINVAL;
  1137. msg = &p_ptr->publ.phdr;
  1138. msg_set_type(msg, TIPC_DIRECT_MSG);
  1139. msg_set_orignode(msg, tipc_own_addr);
  1140. msg_set_origport(msg, ref);
  1141. msg_set_destnode(msg, dest->node);
  1142. msg_set_destport(msg, dest->ref);
  1143. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1144. p_ptr->sent++;
  1145. if (dest->node == tipc_own_addr)
  1146. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1147. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1148. if (likely(res != -ELINKCONG))
  1149. return res;
  1150. if (port_unreliable(p_ptr)) {
  1151. /* Just calculate msg length and return */
  1152. return tipc_msg_calc_data_size(msg_sect, num_sect);
  1153. }
  1154. return -ELINKCONG;
  1155. }
  1156. /**
  1157. * tipc_send_buf2port - send message buffer to port identity
  1158. */
  1159. int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
  1160. struct sk_buff *buf, unsigned int dsz)
  1161. {
  1162. struct port *p_ptr;
  1163. struct tipc_msg *msg;
  1164. int res;
  1165. p_ptr = (struct port *)tipc_ref_deref(ref);
  1166. if (!p_ptr || p_ptr->publ.connected)
  1167. return -EINVAL;
  1168. msg = &p_ptr->publ.phdr;
  1169. msg_set_type(msg, TIPC_DIRECT_MSG);
  1170. msg_set_orignode(msg, tipc_own_addr);
  1171. msg_set_origport(msg, ref);
  1172. msg_set_destnode(msg, dest->node);
  1173. msg_set_destport(msg, dest->ref);
  1174. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1175. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1176. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1177. return -ENOMEM;
  1178. skb_push(buf, DIR_MSG_H_SIZE);
  1179. skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
  1180. p_ptr->sent++;
  1181. if (dest->node == tipc_own_addr)
  1182. return tipc_port_recv_msg(buf);
  1183. res = tipc_send_buf_fast(buf, dest->node);
  1184. if (likely(res != -ELINKCONG))
  1185. return res;
  1186. if (port_unreliable(p_ptr))
  1187. return dsz;
  1188. return -ELINKCONG;
  1189. }