port.c 32 KB

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