port.c 33 KB

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