port.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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 tipc_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. kfree_skb(buf);
  102. return -ENOMEM;
  103. }
  104. }
  105. res = tipc_bclink_send_msg(buf);
  106. if ((res < 0) && (dports.count != 0))
  107. kfree_skb(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 tipc_port_list *dp)
  125. {
  126. struct tipc_msg *msg;
  127. struct tipc_port_list dports = {0, NULL, };
  128. struct tipc_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. kfree_skb(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(): create connection protocol message for port
  284. *
  285. * On entry the port must be locked and connected.
  286. */
  287. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  288. u32 type, u32 ack)
  289. {
  290. struct sk_buff *buf;
  291. struct tipc_msg *msg;
  292. buf = tipc_buf_acquire(INT_H_SIZE);
  293. if (buf) {
  294. msg = buf_msg(buf);
  295. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  296. port_peernode(p_ptr));
  297. msg_set_destport(msg, port_peerport(p_ptr));
  298. msg_set_origport(msg, p_ptr->ref);
  299. msg_set_msgcnt(msg, ack);
  300. }
  301. return buf;
  302. }
  303. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  304. {
  305. struct tipc_msg *msg = buf_msg(buf);
  306. struct sk_buff *rbuf;
  307. struct tipc_msg *rmsg;
  308. int hdr_sz;
  309. u32 imp;
  310. u32 data_sz = msg_data_sz(msg);
  311. u32 src_node;
  312. u32 rmsg_sz;
  313. /* discard rejected message if it shouldn't be returned to sender */
  314. if (WARN(!msg_isdata(msg),
  315. "attempt to reject message with user=%u", msg_user(msg))) {
  316. dump_stack();
  317. goto exit;
  318. }
  319. if (msg_errcode(msg) || msg_dest_droppable(msg))
  320. goto exit;
  321. /*
  322. * construct returned message by copying rejected message header and
  323. * data (or subset), then updating header fields that need adjusting
  324. */
  325. hdr_sz = msg_hdr_sz(msg);
  326. rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
  327. rbuf = tipc_buf_acquire(rmsg_sz);
  328. if (rbuf == NULL)
  329. goto exit;
  330. rmsg = buf_msg(rbuf);
  331. skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
  332. if (msg_connected(rmsg)) {
  333. imp = msg_importance(rmsg);
  334. if (imp < TIPC_CRITICAL_IMPORTANCE)
  335. msg_set_importance(rmsg, ++imp);
  336. }
  337. msg_set_non_seq(rmsg, 0);
  338. msg_set_size(rmsg, rmsg_sz);
  339. msg_set_errcode(rmsg, err);
  340. msg_set_prevnode(rmsg, tipc_own_addr);
  341. msg_swap_words(rmsg, 4, 5);
  342. if (!msg_short(rmsg))
  343. msg_swap_words(rmsg, 6, 7);
  344. /* send self-abort message when rejecting on a connected port */
  345. if (msg_connected(msg)) {
  346. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  347. if (p_ptr) {
  348. struct sk_buff *abuf = NULL;
  349. if (p_ptr->connected)
  350. abuf = port_build_self_abort_msg(p_ptr, err);
  351. tipc_port_unlock(p_ptr);
  352. tipc_net_route_msg(abuf);
  353. }
  354. }
  355. /* send returned message & dispose of rejected message */
  356. src_node = msg_prevnode(msg);
  357. if (src_node == tipc_own_addr)
  358. tipc_port_recv_msg(rbuf);
  359. else
  360. tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
  361. exit:
  362. kfree_skb(buf);
  363. return data_sz;
  364. }
  365. int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
  366. struct iovec const *msg_sect, u32 num_sect,
  367. unsigned int total_len, int err)
  368. {
  369. struct sk_buff *buf;
  370. int res;
  371. res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
  372. !p_ptr->user_port, &buf);
  373. if (!buf)
  374. return res;
  375. return tipc_reject_msg(buf, err);
  376. }
  377. static void port_timeout(unsigned long ref)
  378. {
  379. struct tipc_port *p_ptr = tipc_port_lock(ref);
  380. struct sk_buff *buf = NULL;
  381. if (!p_ptr)
  382. return;
  383. if (!p_ptr->connected) {
  384. tipc_port_unlock(p_ptr);
  385. return;
  386. }
  387. /* Last probe answered ? */
  388. if (p_ptr->probing_state == PROBING) {
  389. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  390. } else {
  391. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  392. p_ptr->probing_state = PROBING;
  393. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  394. }
  395. tipc_port_unlock(p_ptr);
  396. tipc_net_route_msg(buf);
  397. }
  398. static void port_handle_node_down(unsigned long ref)
  399. {
  400. struct tipc_port *p_ptr = tipc_port_lock(ref);
  401. struct sk_buff *buf = NULL;
  402. if (!p_ptr)
  403. return;
  404. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  405. tipc_port_unlock(p_ptr);
  406. tipc_net_route_msg(buf);
  407. }
  408. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  409. {
  410. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  411. if (buf) {
  412. struct tipc_msg *msg = buf_msg(buf);
  413. msg_swap_words(msg, 4, 5);
  414. msg_swap_words(msg, 6, 7);
  415. }
  416. return buf;
  417. }
  418. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  419. {
  420. struct sk_buff *buf;
  421. struct tipc_msg *msg;
  422. u32 imp;
  423. if (!p_ptr->connected)
  424. return NULL;
  425. buf = tipc_buf_acquire(BASIC_H_SIZE);
  426. if (buf) {
  427. msg = buf_msg(buf);
  428. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  429. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  430. msg_set_size(msg, BASIC_H_SIZE);
  431. imp = msg_importance(msg);
  432. if (imp < TIPC_CRITICAL_IMPORTANCE)
  433. msg_set_importance(msg, ++imp);
  434. msg_set_errcode(msg, err);
  435. }
  436. return buf;
  437. }
  438. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  439. {
  440. struct tipc_msg *msg = buf_msg(buf);
  441. struct tipc_port *p_ptr;
  442. struct sk_buff *r_buf = NULL;
  443. u32 orignode = msg_orignode(msg);
  444. u32 origport = msg_origport(msg);
  445. u32 destport = msg_destport(msg);
  446. int wakeable;
  447. /* Validate connection */
  448. p_ptr = tipc_port_lock(destport);
  449. if (!p_ptr || !p_ptr->connected ||
  450. (port_peernode(p_ptr) != orignode) ||
  451. (port_peerport(p_ptr) != origport)) {
  452. r_buf = tipc_buf_acquire(BASIC_H_SIZE);
  453. if (r_buf) {
  454. msg = buf_msg(r_buf);
  455. tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
  456. BASIC_H_SIZE, orignode);
  457. msg_set_errcode(msg, TIPC_ERR_NO_PORT);
  458. msg_set_origport(msg, destport);
  459. msg_set_destport(msg, origport);
  460. }
  461. if (p_ptr)
  462. tipc_port_unlock(p_ptr);
  463. goto exit;
  464. }
  465. /* Process protocol message sent by peer */
  466. switch (msg_type(msg)) {
  467. case CONN_ACK:
  468. wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
  469. p_ptr->wakeup;
  470. p_ptr->acked += msg_msgcnt(msg);
  471. if (!tipc_port_congested(p_ptr)) {
  472. p_ptr->congested = 0;
  473. if (wakeable)
  474. p_ptr->wakeup(p_ptr);
  475. }
  476. break;
  477. case CONN_PROBE:
  478. r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
  479. break;
  480. default:
  481. /* CONN_PROBE_REPLY or unrecognized - no action required */
  482. break;
  483. }
  484. p_ptr->probing_state = CONFIRMED;
  485. tipc_port_unlock(p_ptr);
  486. exit:
  487. tipc_net_route_msg(r_buf);
  488. kfree_skb(buf);
  489. }
  490. static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
  491. {
  492. struct publication *publ;
  493. if (full_id)
  494. tipc_printf(buf, "<%u.%u.%u:%u>:",
  495. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  496. tipc_node(tipc_own_addr), p_ptr->ref);
  497. else
  498. tipc_printf(buf, "%-10u:", p_ptr->ref);
  499. if (p_ptr->connected) {
  500. u32 dport = port_peerport(p_ptr);
  501. u32 destnode = port_peernode(p_ptr);
  502. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  503. tipc_zone(destnode), tipc_cluster(destnode),
  504. tipc_node(destnode), dport);
  505. if (p_ptr->conn_type != 0)
  506. tipc_printf(buf, " via {%u,%u}",
  507. p_ptr->conn_type,
  508. p_ptr->conn_instance);
  509. } else if (p_ptr->published) {
  510. tipc_printf(buf, " bound to");
  511. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  512. if (publ->lower == publ->upper)
  513. tipc_printf(buf, " {%u,%u}", publ->type,
  514. publ->lower);
  515. else
  516. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  517. publ->lower, publ->upper);
  518. }
  519. }
  520. tipc_printf(buf, "\n");
  521. }
  522. #define MAX_PORT_QUERY 32768
  523. struct sk_buff *tipc_port_get_ports(void)
  524. {
  525. struct sk_buff *buf;
  526. struct tlv_desc *rep_tlv;
  527. struct print_buf pb;
  528. struct tipc_port *p_ptr;
  529. int str_len;
  530. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  531. if (!buf)
  532. return NULL;
  533. rep_tlv = (struct tlv_desc *)buf->data;
  534. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  535. spin_lock_bh(&tipc_port_list_lock);
  536. list_for_each_entry(p_ptr, &ports, port_list) {
  537. spin_lock_bh(p_ptr->lock);
  538. port_print(p_ptr, &pb, 0);
  539. spin_unlock_bh(p_ptr->lock);
  540. }
  541. spin_unlock_bh(&tipc_port_list_lock);
  542. str_len = tipc_printbuf_validate(&pb);
  543. skb_put(buf, TLV_SPACE(str_len));
  544. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  545. return buf;
  546. }
  547. void tipc_port_reinit(void)
  548. {
  549. struct tipc_port *p_ptr;
  550. struct tipc_msg *msg;
  551. spin_lock_bh(&tipc_port_list_lock);
  552. list_for_each_entry(p_ptr, &ports, port_list) {
  553. msg = &p_ptr->phdr;
  554. if (msg_orignode(msg) == tipc_own_addr)
  555. break;
  556. msg_set_prevnode(msg, tipc_own_addr);
  557. msg_set_orignode(msg, tipc_own_addr);
  558. }
  559. spin_unlock_bh(&tipc_port_list_lock);
  560. }
  561. /*
  562. * port_dispatcher_sigh(): Signal handler for messages destinated
  563. * to the tipc_port interface.
  564. */
  565. static void port_dispatcher_sigh(void *dummy)
  566. {
  567. struct sk_buff *buf;
  568. spin_lock_bh(&queue_lock);
  569. buf = msg_queue_head;
  570. msg_queue_head = NULL;
  571. spin_unlock_bh(&queue_lock);
  572. while (buf) {
  573. struct tipc_port *p_ptr;
  574. struct user_port *up_ptr;
  575. struct tipc_portid orig;
  576. struct tipc_name_seq dseq;
  577. void *usr_handle;
  578. int connected;
  579. int published;
  580. u32 message_type;
  581. struct sk_buff *next = buf->next;
  582. struct tipc_msg *msg = buf_msg(buf);
  583. u32 dref = msg_destport(msg);
  584. message_type = msg_type(msg);
  585. if (message_type > TIPC_DIRECT_MSG)
  586. goto reject; /* Unsupported message type */
  587. p_ptr = tipc_port_lock(dref);
  588. if (!p_ptr)
  589. goto reject; /* Port deleted while msg in queue */
  590. orig.ref = msg_origport(msg);
  591. orig.node = msg_orignode(msg);
  592. up_ptr = p_ptr->user_port;
  593. usr_handle = up_ptr->usr_handle;
  594. connected = p_ptr->connected;
  595. published = p_ptr->published;
  596. if (unlikely(msg_errcode(msg)))
  597. goto err;
  598. switch (message_type) {
  599. case TIPC_CONN_MSG:{
  600. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  601. u32 peer_port = port_peerport(p_ptr);
  602. u32 peer_node = port_peernode(p_ptr);
  603. u32 dsz;
  604. tipc_port_unlock(p_ptr);
  605. if (unlikely(!cb))
  606. goto reject;
  607. if (unlikely(!connected)) {
  608. if (tipc_connect2port(dref, &orig))
  609. goto reject;
  610. } else if ((msg_origport(msg) != peer_port) ||
  611. (msg_orignode(msg) != peer_node))
  612. goto reject;
  613. dsz = msg_data_sz(msg);
  614. if (unlikely(dsz &&
  615. (++p_ptr->conn_unacked >=
  616. TIPC_FLOW_CONTROL_WIN)))
  617. tipc_acknowledge(dref,
  618. p_ptr->conn_unacked);
  619. skb_pull(buf, msg_hdr_sz(msg));
  620. cb(usr_handle, dref, &buf, msg_data(msg), dsz);
  621. break;
  622. }
  623. case TIPC_DIRECT_MSG:{
  624. tipc_msg_event cb = up_ptr->msg_cb;
  625. tipc_port_unlock(p_ptr);
  626. if (unlikely(!cb || connected))
  627. goto reject;
  628. skb_pull(buf, msg_hdr_sz(msg));
  629. cb(usr_handle, dref, &buf, msg_data(msg),
  630. msg_data_sz(msg), msg_importance(msg),
  631. &orig);
  632. break;
  633. }
  634. case TIPC_MCAST_MSG:
  635. case TIPC_NAMED_MSG:{
  636. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  637. tipc_port_unlock(p_ptr);
  638. if (unlikely(!cb || connected || !published))
  639. goto reject;
  640. dseq.type = msg_nametype(msg);
  641. dseq.lower = msg_nameinst(msg);
  642. dseq.upper = (message_type == TIPC_NAMED_MSG)
  643. ? dseq.lower : msg_nameupper(msg);
  644. skb_pull(buf, msg_hdr_sz(msg));
  645. cb(usr_handle, dref, &buf, msg_data(msg),
  646. msg_data_sz(msg), msg_importance(msg),
  647. &orig, &dseq);
  648. break;
  649. }
  650. }
  651. if (buf)
  652. kfree_skb(buf);
  653. buf = next;
  654. continue;
  655. err:
  656. switch (message_type) {
  657. case TIPC_CONN_MSG:{
  658. tipc_conn_shutdown_event cb =
  659. up_ptr->conn_err_cb;
  660. u32 peer_port = port_peerport(p_ptr);
  661. u32 peer_node = port_peernode(p_ptr);
  662. tipc_port_unlock(p_ptr);
  663. if (!cb || !connected)
  664. break;
  665. if ((msg_origport(msg) != peer_port) ||
  666. (msg_orignode(msg) != peer_node))
  667. break;
  668. tipc_disconnect(dref);
  669. skb_pull(buf, msg_hdr_sz(msg));
  670. cb(usr_handle, dref, &buf, msg_data(msg),
  671. msg_data_sz(msg), msg_errcode(msg));
  672. break;
  673. }
  674. case TIPC_DIRECT_MSG:{
  675. tipc_msg_err_event cb = up_ptr->err_cb;
  676. tipc_port_unlock(p_ptr);
  677. if (!cb || connected)
  678. break;
  679. skb_pull(buf, msg_hdr_sz(msg));
  680. cb(usr_handle, dref, &buf, msg_data(msg),
  681. msg_data_sz(msg), msg_errcode(msg), &orig);
  682. break;
  683. }
  684. case TIPC_MCAST_MSG:
  685. case TIPC_NAMED_MSG:{
  686. tipc_named_msg_err_event cb =
  687. up_ptr->named_err_cb;
  688. tipc_port_unlock(p_ptr);
  689. if (!cb || connected)
  690. break;
  691. dseq.type = msg_nametype(msg);
  692. dseq.lower = msg_nameinst(msg);
  693. dseq.upper = (message_type == TIPC_NAMED_MSG)
  694. ? dseq.lower : msg_nameupper(msg);
  695. skb_pull(buf, msg_hdr_sz(msg));
  696. cb(usr_handle, dref, &buf, msg_data(msg),
  697. msg_data_sz(msg), msg_errcode(msg), &dseq);
  698. break;
  699. }
  700. }
  701. if (buf)
  702. kfree_skb(buf);
  703. buf = next;
  704. continue;
  705. reject:
  706. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  707. buf = next;
  708. }
  709. }
  710. /*
  711. * port_dispatcher(): Dispatcher for messages destinated
  712. * to the tipc_port interface. Called with port locked.
  713. */
  714. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  715. {
  716. buf->next = NULL;
  717. spin_lock_bh(&queue_lock);
  718. if (msg_queue_head) {
  719. msg_queue_tail->next = buf;
  720. msg_queue_tail = buf;
  721. } else {
  722. msg_queue_tail = msg_queue_head = buf;
  723. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  724. }
  725. spin_unlock_bh(&queue_lock);
  726. return 0;
  727. }
  728. /*
  729. * Wake up port after congestion: Called with port locked,
  730. *
  731. */
  732. static void port_wakeup_sh(unsigned long ref)
  733. {
  734. struct tipc_port *p_ptr;
  735. struct user_port *up_ptr;
  736. tipc_continue_event cb = NULL;
  737. void *uh = NULL;
  738. p_ptr = tipc_port_lock(ref);
  739. if (p_ptr) {
  740. up_ptr = p_ptr->user_port;
  741. if (up_ptr) {
  742. cb = up_ptr->continue_event_cb;
  743. uh = up_ptr->usr_handle;
  744. }
  745. tipc_port_unlock(p_ptr);
  746. }
  747. if (cb)
  748. cb(uh, ref);
  749. }
  750. static void port_wakeup(struct tipc_port *p_ptr)
  751. {
  752. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  753. }
  754. void tipc_acknowledge(u32 ref, u32 ack)
  755. {
  756. struct tipc_port *p_ptr;
  757. struct sk_buff *buf = NULL;
  758. p_ptr = tipc_port_lock(ref);
  759. if (!p_ptr)
  760. return;
  761. if (p_ptr->connected) {
  762. p_ptr->conn_unacked -= ack;
  763. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  764. }
  765. tipc_port_unlock(p_ptr);
  766. tipc_net_route_msg(buf);
  767. }
  768. /*
  769. * tipc_createport(): user level call.
  770. */
  771. int tipc_createport(void *usr_handle,
  772. unsigned int importance,
  773. tipc_msg_err_event error_cb,
  774. tipc_named_msg_err_event named_error_cb,
  775. tipc_conn_shutdown_event conn_error_cb,
  776. tipc_msg_event msg_cb,
  777. tipc_named_msg_event named_msg_cb,
  778. tipc_conn_msg_event conn_msg_cb,
  779. tipc_continue_event continue_event_cb,/* May be zero */
  780. u32 *portref)
  781. {
  782. struct user_port *up_ptr;
  783. struct tipc_port *p_ptr;
  784. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  785. if (!up_ptr) {
  786. warn("Port creation failed, no memory\n");
  787. return -ENOMEM;
  788. }
  789. p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
  790. port_wakeup, importance);
  791. if (!p_ptr) {
  792. kfree(up_ptr);
  793. return -ENOMEM;
  794. }
  795. p_ptr->user_port = up_ptr;
  796. up_ptr->usr_handle = usr_handle;
  797. up_ptr->ref = p_ptr->ref;
  798. up_ptr->err_cb = error_cb;
  799. up_ptr->named_err_cb = named_error_cb;
  800. up_ptr->conn_err_cb = conn_error_cb;
  801. up_ptr->msg_cb = msg_cb;
  802. up_ptr->named_msg_cb = named_msg_cb;
  803. up_ptr->conn_msg_cb = conn_msg_cb;
  804. up_ptr->continue_event_cb = continue_event_cb;
  805. *portref = p_ptr->ref;
  806. tipc_port_unlock(p_ptr);
  807. return 0;
  808. }
  809. int tipc_portimportance(u32 ref, unsigned int *importance)
  810. {
  811. struct tipc_port *p_ptr;
  812. p_ptr = tipc_port_lock(ref);
  813. if (!p_ptr)
  814. return -EINVAL;
  815. *importance = (unsigned int)msg_importance(&p_ptr->phdr);
  816. tipc_port_unlock(p_ptr);
  817. return 0;
  818. }
  819. int tipc_set_portimportance(u32 ref, unsigned int imp)
  820. {
  821. struct tipc_port *p_ptr;
  822. if (imp > TIPC_CRITICAL_IMPORTANCE)
  823. return -EINVAL;
  824. p_ptr = tipc_port_lock(ref);
  825. if (!p_ptr)
  826. return -EINVAL;
  827. msg_set_importance(&p_ptr->phdr, (u32)imp);
  828. tipc_port_unlock(p_ptr);
  829. return 0;
  830. }
  831. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  832. {
  833. struct tipc_port *p_ptr;
  834. struct publication *publ;
  835. u32 key;
  836. int res = -EINVAL;
  837. p_ptr = tipc_port_lock(ref);
  838. if (!p_ptr)
  839. return -EINVAL;
  840. if (p_ptr->connected)
  841. goto exit;
  842. if (seq->lower > seq->upper)
  843. goto exit;
  844. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  845. goto exit;
  846. key = ref + p_ptr->pub_count + 1;
  847. if (key == ref) {
  848. res = -EADDRINUSE;
  849. goto exit;
  850. }
  851. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  852. scope, p_ptr->ref, key);
  853. if (publ) {
  854. list_add(&publ->pport_list, &p_ptr->publications);
  855. p_ptr->pub_count++;
  856. p_ptr->published = 1;
  857. res = 0;
  858. }
  859. exit:
  860. tipc_port_unlock(p_ptr);
  861. return res;
  862. }
  863. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  864. {
  865. struct tipc_port *p_ptr;
  866. struct publication *publ;
  867. struct publication *tpubl;
  868. int res = -EINVAL;
  869. p_ptr = tipc_port_lock(ref);
  870. if (!p_ptr)
  871. return -EINVAL;
  872. if (!seq) {
  873. list_for_each_entry_safe(publ, tpubl,
  874. &p_ptr->publications, pport_list) {
  875. tipc_nametbl_withdraw(publ->type, publ->lower,
  876. publ->ref, publ->key);
  877. }
  878. res = 0;
  879. } else {
  880. list_for_each_entry_safe(publ, tpubl,
  881. &p_ptr->publications, pport_list) {
  882. if (publ->scope != scope)
  883. continue;
  884. if (publ->type != seq->type)
  885. continue;
  886. if (publ->lower != seq->lower)
  887. continue;
  888. if (publ->upper != seq->upper)
  889. break;
  890. tipc_nametbl_withdraw(publ->type, publ->lower,
  891. publ->ref, publ->key);
  892. res = 0;
  893. break;
  894. }
  895. }
  896. if (list_empty(&p_ptr->publications))
  897. p_ptr->published = 0;
  898. tipc_port_unlock(p_ptr);
  899. return res;
  900. }
  901. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  902. {
  903. struct tipc_port *p_ptr;
  904. struct tipc_msg *msg;
  905. int res = -EINVAL;
  906. p_ptr = tipc_port_lock(ref);
  907. if (!p_ptr)
  908. return -EINVAL;
  909. if (p_ptr->published || p_ptr->connected)
  910. goto exit;
  911. if (!peer->ref)
  912. goto exit;
  913. msg = &p_ptr->phdr;
  914. msg_set_destnode(msg, peer->node);
  915. msg_set_destport(msg, peer->ref);
  916. msg_set_type(msg, TIPC_CONN_MSG);
  917. msg_set_lookup_scope(msg, 0);
  918. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  919. p_ptr->probing_interval = PROBING_INTERVAL;
  920. p_ptr->probing_state = CONFIRMED;
  921. p_ptr->connected = 1;
  922. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  923. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  924. (void *)(unsigned long)ref,
  925. (net_ev_handler)port_handle_node_down);
  926. res = 0;
  927. exit:
  928. tipc_port_unlock(p_ptr);
  929. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  930. return res;
  931. }
  932. /**
  933. * tipc_disconnect_port - disconnect port from peer
  934. *
  935. * Port must be locked.
  936. */
  937. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  938. {
  939. int res;
  940. if (tp_ptr->connected) {
  941. tp_ptr->connected = 0;
  942. /* let timer expire on it's own to avoid deadlock! */
  943. tipc_nodesub_unsubscribe(
  944. &((struct tipc_port *)tp_ptr)->subscription);
  945. res = 0;
  946. } else {
  947. res = -ENOTCONN;
  948. }
  949. return res;
  950. }
  951. /*
  952. * tipc_disconnect(): Disconnect port form peer.
  953. * This is a node local operation.
  954. */
  955. int tipc_disconnect(u32 ref)
  956. {
  957. struct tipc_port *p_ptr;
  958. int res;
  959. p_ptr = tipc_port_lock(ref);
  960. if (!p_ptr)
  961. return -EINVAL;
  962. res = tipc_disconnect_port((struct tipc_port *)p_ptr);
  963. tipc_port_unlock(p_ptr);
  964. return res;
  965. }
  966. /*
  967. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  968. */
  969. int tipc_shutdown(u32 ref)
  970. {
  971. struct tipc_port *p_ptr;
  972. struct sk_buff *buf = NULL;
  973. p_ptr = tipc_port_lock(ref);
  974. if (!p_ptr)
  975. return -EINVAL;
  976. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  977. tipc_port_unlock(p_ptr);
  978. tipc_net_route_msg(buf);
  979. return tipc_disconnect(ref);
  980. }
  981. /**
  982. * tipc_port_recv_msg - receive message from lower layer and deliver to port user
  983. */
  984. int tipc_port_recv_msg(struct sk_buff *buf)
  985. {
  986. struct tipc_port *p_ptr;
  987. struct tipc_msg *msg = buf_msg(buf);
  988. u32 destport = msg_destport(msg);
  989. u32 dsz = msg_data_sz(msg);
  990. u32 err;
  991. /* forward unresolved named message */
  992. if (unlikely(!destport)) {
  993. tipc_net_route_msg(buf);
  994. return dsz;
  995. }
  996. /* validate destination & pass to port, otherwise reject message */
  997. p_ptr = tipc_port_lock(destport);
  998. if (likely(p_ptr)) {
  999. if (likely(p_ptr->connected)) {
  1000. if ((unlikely(msg_origport(msg) !=
  1001. tipc_peer_port(p_ptr))) ||
  1002. (unlikely(msg_orignode(msg) !=
  1003. tipc_peer_node(p_ptr))) ||
  1004. (unlikely(!msg_connected(msg)))) {
  1005. err = TIPC_ERR_NO_PORT;
  1006. tipc_port_unlock(p_ptr);
  1007. goto reject;
  1008. }
  1009. }
  1010. err = p_ptr->dispatcher(p_ptr, buf);
  1011. tipc_port_unlock(p_ptr);
  1012. if (likely(!err))
  1013. return dsz;
  1014. } else {
  1015. err = TIPC_ERR_NO_PORT;
  1016. }
  1017. reject:
  1018. return tipc_reject_msg(buf, err);
  1019. }
  1020. /*
  1021. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1022. * message for this node.
  1023. */
  1024. static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
  1025. struct iovec const *msg_sect,
  1026. unsigned int total_len)
  1027. {
  1028. struct sk_buff *buf;
  1029. int res;
  1030. res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
  1031. MAX_MSG_SIZE, !sender->user_port, &buf);
  1032. if (likely(buf))
  1033. tipc_port_recv_msg(buf);
  1034. return res;
  1035. }
  1036. /**
  1037. * tipc_send - send message sections on connection
  1038. */
  1039. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
  1040. unsigned int total_len)
  1041. {
  1042. struct tipc_port *p_ptr;
  1043. u32 destnode;
  1044. int res;
  1045. p_ptr = tipc_port_deref(ref);
  1046. if (!p_ptr || !p_ptr->connected)
  1047. return -EINVAL;
  1048. p_ptr->congested = 1;
  1049. if (!tipc_port_congested(p_ptr)) {
  1050. destnode = port_peernode(p_ptr);
  1051. if (likely(destnode != tipc_own_addr))
  1052. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1053. total_len, destnode);
  1054. else
  1055. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1056. total_len);
  1057. if (likely(res != -ELINKCONG)) {
  1058. p_ptr->congested = 0;
  1059. if (res > 0)
  1060. p_ptr->sent++;
  1061. return res;
  1062. }
  1063. }
  1064. if (port_unreliable(p_ptr)) {
  1065. p_ptr->congested = 0;
  1066. return total_len;
  1067. }
  1068. return -ELINKCONG;
  1069. }
  1070. /**
  1071. * tipc_send2name - send message sections to port name
  1072. */
  1073. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  1074. unsigned int num_sect, struct iovec const *msg_sect,
  1075. unsigned int total_len)
  1076. {
  1077. struct tipc_port *p_ptr;
  1078. struct tipc_msg *msg;
  1079. u32 destnode = domain;
  1080. u32 destport;
  1081. int res;
  1082. p_ptr = tipc_port_deref(ref);
  1083. if (!p_ptr || p_ptr->connected)
  1084. return -EINVAL;
  1085. msg = &p_ptr->phdr;
  1086. msg_set_type(msg, TIPC_NAMED_MSG);
  1087. msg_set_hdr_sz(msg, NAMED_H_SIZE);
  1088. msg_set_nametype(msg, name->type);
  1089. msg_set_nameinst(msg, name->instance);
  1090. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  1091. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1092. msg_set_destnode(msg, destnode);
  1093. msg_set_destport(msg, destport);
  1094. if (likely(destport || destnode)) {
  1095. if (likely(destnode == tipc_own_addr))
  1096. res = tipc_port_recv_sections(p_ptr, num_sect,
  1097. msg_sect, total_len);
  1098. else
  1099. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  1100. num_sect, total_len,
  1101. destnode);
  1102. if (likely(res != -ELINKCONG)) {
  1103. if (res > 0)
  1104. p_ptr->sent++;
  1105. return res;
  1106. }
  1107. if (port_unreliable(p_ptr)) {
  1108. return total_len;
  1109. }
  1110. return -ELINKCONG;
  1111. }
  1112. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1113. total_len, TIPC_ERR_NO_NAME);
  1114. }
  1115. /**
  1116. * tipc_send2port - send message sections to port identity
  1117. */
  1118. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  1119. unsigned int num_sect, struct iovec const *msg_sect,
  1120. unsigned int total_len)
  1121. {
  1122. struct tipc_port *p_ptr;
  1123. struct tipc_msg *msg;
  1124. int res;
  1125. p_ptr = tipc_port_deref(ref);
  1126. if (!p_ptr || p_ptr->connected)
  1127. return -EINVAL;
  1128. msg = &p_ptr->phdr;
  1129. msg_set_type(msg, TIPC_DIRECT_MSG);
  1130. msg_set_lookup_scope(msg, 0);
  1131. msg_set_destnode(msg, dest->node);
  1132. msg_set_destport(msg, dest->ref);
  1133. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1134. if (dest->node == tipc_own_addr)
  1135. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1136. total_len);
  1137. else
  1138. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1139. total_len, dest->node);
  1140. if (likely(res != -ELINKCONG)) {
  1141. if (res > 0)
  1142. p_ptr->sent++;
  1143. return res;
  1144. }
  1145. if (port_unreliable(p_ptr)) {
  1146. return total_len;
  1147. }
  1148. return -ELINKCONG;
  1149. }
  1150. /**
  1151. * tipc_send_buf2port - send message buffer to port identity
  1152. */
  1153. int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
  1154. struct sk_buff *buf, unsigned int dsz)
  1155. {
  1156. struct tipc_port *p_ptr;
  1157. struct tipc_msg *msg;
  1158. int res;
  1159. p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
  1160. if (!p_ptr || p_ptr->connected)
  1161. return -EINVAL;
  1162. msg = &p_ptr->phdr;
  1163. msg_set_type(msg, TIPC_DIRECT_MSG);
  1164. msg_set_destnode(msg, dest->node);
  1165. msg_set_destport(msg, dest->ref);
  1166. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1167. msg_set_size(msg, BASIC_H_SIZE + dsz);
  1168. if (skb_cow(buf, BASIC_H_SIZE))
  1169. return -ENOMEM;
  1170. skb_push(buf, BASIC_H_SIZE);
  1171. skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
  1172. if (dest->node == tipc_own_addr)
  1173. res = tipc_port_recv_msg(buf);
  1174. else
  1175. res = tipc_send_buf_fast(buf, dest->node);
  1176. if (likely(res != -ELINKCONG)) {
  1177. if (res > 0)
  1178. p_ptr->sent++;
  1179. return res;
  1180. }
  1181. if (port_unreliable(p_ptr))
  1182. return dsz;
  1183. return -ELINKCONG;
  1184. }