port.c 32 KB

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