port.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, 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. DEFINE_SPINLOCK(tipc_port_list_lock);
  46. static LIST_HEAD(ports);
  47. static void port_handle_node_down(unsigned long ref);
  48. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
  49. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
  50. static void port_timeout(unsigned long ref);
  51. static u32 port_peernode(struct tipc_port *p_ptr)
  52. {
  53. return msg_destnode(&p_ptr->phdr);
  54. }
  55. static u32 port_peerport(struct tipc_port *p_ptr)
  56. {
  57. return msg_destport(&p_ptr->phdr);
  58. }
  59. /**
  60. * tipc_port_peer_msg - verify message was sent by connected port's peer
  61. *
  62. * Handles cases where the node's network address has changed from
  63. * the default of <0.0.0> to its configured setting.
  64. */
  65. int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
  66. {
  67. u32 peernode;
  68. u32 orignode;
  69. if (msg_origport(msg) != port_peerport(p_ptr))
  70. return 0;
  71. orignode = msg_orignode(msg);
  72. peernode = port_peernode(p_ptr);
  73. return (orignode == peernode) ||
  74. (!orignode && (peernode == tipc_own_addr)) ||
  75. (!peernode && (orignode == tipc_own_addr));
  76. }
  77. /**
  78. * tipc_multicast - send a multicast message to local and remote destinations
  79. */
  80. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
  81. struct iovec const *msg_sect, unsigned int len)
  82. {
  83. struct tipc_msg *hdr;
  84. struct sk_buff *buf;
  85. struct sk_buff *ibuf = NULL;
  86. struct tipc_port_list dports = {0, NULL, };
  87. struct tipc_port *oport = tipc_port_deref(ref);
  88. int ext_targets;
  89. int res;
  90. if (unlikely(!oport))
  91. return -EINVAL;
  92. /* Create multicast message */
  93. hdr = &oport->phdr;
  94. msg_set_type(hdr, TIPC_MCAST_MSG);
  95. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  96. msg_set_destport(hdr, 0);
  97. msg_set_destnode(hdr, 0);
  98. msg_set_nametype(hdr, seq->type);
  99. msg_set_namelower(hdr, seq->lower);
  100. msg_set_nameupper(hdr, seq->upper);
  101. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  102. res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  103. if (unlikely(!buf))
  104. return res;
  105. /* Figure out where to send multicast message */
  106. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  107. TIPC_NODE_SCOPE, &dports);
  108. /* Send message to destinations (duplicate it only if necessary) */
  109. if (ext_targets) {
  110. if (dports.count != 0) {
  111. ibuf = skb_copy(buf, GFP_ATOMIC);
  112. if (ibuf == NULL) {
  113. tipc_port_list_free(&dports);
  114. kfree_skb(buf);
  115. return -ENOMEM;
  116. }
  117. }
  118. res = tipc_bclink_send_msg(buf);
  119. if ((res < 0) && (dports.count != 0))
  120. kfree_skb(ibuf);
  121. } else {
  122. ibuf = buf;
  123. }
  124. if (res >= 0) {
  125. if (ibuf)
  126. tipc_port_recv_mcast(ibuf, &dports);
  127. } else {
  128. tipc_port_list_free(&dports);
  129. }
  130. return res;
  131. }
  132. /**
  133. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  134. *
  135. * If there is no port list, perform a lookup to create one
  136. */
  137. void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
  138. {
  139. struct tipc_msg *msg;
  140. struct tipc_port_list dports = {0, NULL, };
  141. struct tipc_port_list *item = dp;
  142. int cnt = 0;
  143. msg = buf_msg(buf);
  144. /* Create destination port list, if one wasn't supplied */
  145. if (dp == NULL) {
  146. tipc_nametbl_mc_translate(msg_nametype(msg),
  147. msg_namelower(msg),
  148. msg_nameupper(msg),
  149. TIPC_CLUSTER_SCOPE,
  150. &dports);
  151. item = dp = &dports;
  152. }
  153. /* Deliver a copy of message to each destination port */
  154. if (dp->count != 0) {
  155. msg_set_destnode(msg, tipc_own_addr);
  156. if (dp->count == 1) {
  157. msg_set_destport(msg, dp->ports[0]);
  158. tipc_port_recv_msg(buf);
  159. tipc_port_list_free(dp);
  160. return;
  161. }
  162. for (; cnt < dp->count; cnt++) {
  163. int index = cnt % PLSIZE;
  164. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  165. if (b == NULL) {
  166. pr_warn("Unable to deliver multicast message(s)\n");
  167. goto exit;
  168. }
  169. if ((index == 0) && (cnt != 0))
  170. item = item->next;
  171. msg_set_destport(buf_msg(b), item->ports[index]);
  172. tipc_port_recv_msg(b);
  173. }
  174. }
  175. exit:
  176. kfree_skb(buf);
  177. tipc_port_list_free(dp);
  178. }
  179. /**
  180. * tipc_createport - create a generic TIPC port
  181. *
  182. * Returns pointer to (locked) TIPC port, or NULL if unable to create it
  183. */
  184. struct tipc_port *tipc_createport(struct sock *sk,
  185. u32 (*dispatcher)(struct tipc_port *,
  186. struct sk_buff *),
  187. void (*wakeup)(struct tipc_port *),
  188. const u32 importance)
  189. {
  190. struct tipc_port *p_ptr;
  191. struct tipc_msg *msg;
  192. u32 ref;
  193. p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
  194. if (!p_ptr) {
  195. pr_warn("Port creation failed, no memory\n");
  196. return NULL;
  197. }
  198. ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
  199. if (!ref) {
  200. pr_warn("Port creation failed, ref. table exhausted\n");
  201. kfree(p_ptr);
  202. return NULL;
  203. }
  204. p_ptr->sk = sk;
  205. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  206. p_ptr->ref = ref;
  207. INIT_LIST_HEAD(&p_ptr->wait_list);
  208. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  209. p_ptr->dispatcher = dispatcher;
  210. p_ptr->wakeup = wakeup;
  211. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  212. INIT_LIST_HEAD(&p_ptr->publications);
  213. INIT_LIST_HEAD(&p_ptr->port_list);
  214. /*
  215. * Must hold port list lock while initializing message header template
  216. * to ensure a change to node's own network address doesn't result
  217. * in template containing out-dated network address information
  218. */
  219. spin_lock_bh(&tipc_port_list_lock);
  220. msg = &p_ptr->phdr;
  221. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  222. msg_set_origport(msg, ref);
  223. list_add_tail(&p_ptr->port_list, &ports);
  224. spin_unlock_bh(&tipc_port_list_lock);
  225. return p_ptr;
  226. }
  227. int tipc_deleteport(struct tipc_port *p_ptr)
  228. {
  229. struct sk_buff *buf = NULL;
  230. tipc_withdraw(p_ptr, 0, NULL);
  231. spin_lock_bh(p_ptr->lock);
  232. tipc_ref_discard(p_ptr->ref);
  233. spin_unlock_bh(p_ptr->lock);
  234. k_cancel_timer(&p_ptr->timer);
  235. if (p_ptr->connected) {
  236. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  237. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  238. }
  239. spin_lock_bh(&tipc_port_list_lock);
  240. list_del(&p_ptr->port_list);
  241. list_del(&p_ptr->wait_list);
  242. spin_unlock_bh(&tipc_port_list_lock);
  243. k_term_timer(&p_ptr->timer);
  244. kfree(p_ptr);
  245. tipc_net_route_msg(buf);
  246. return 0;
  247. }
  248. static int port_unreliable(struct tipc_port *p_ptr)
  249. {
  250. return msg_src_droppable(&p_ptr->phdr);
  251. }
  252. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  253. {
  254. struct tipc_port *p_ptr;
  255. p_ptr = tipc_port_lock(ref);
  256. if (!p_ptr)
  257. return -EINVAL;
  258. *isunreliable = port_unreliable(p_ptr);
  259. tipc_port_unlock(p_ptr);
  260. return 0;
  261. }
  262. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  263. {
  264. struct tipc_port *p_ptr;
  265. p_ptr = tipc_port_lock(ref);
  266. if (!p_ptr)
  267. return -EINVAL;
  268. msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
  269. tipc_port_unlock(p_ptr);
  270. return 0;
  271. }
  272. static int port_unreturnable(struct tipc_port *p_ptr)
  273. {
  274. return msg_dest_droppable(&p_ptr->phdr);
  275. }
  276. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  277. {
  278. struct tipc_port *p_ptr;
  279. p_ptr = tipc_port_lock(ref);
  280. if (!p_ptr)
  281. return -EINVAL;
  282. *isunrejectable = port_unreturnable(p_ptr);
  283. tipc_port_unlock(p_ptr);
  284. return 0;
  285. }
  286. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  287. {
  288. struct tipc_port *p_ptr;
  289. p_ptr = tipc_port_lock(ref);
  290. if (!p_ptr)
  291. return -EINVAL;
  292. msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
  293. tipc_port_unlock(p_ptr);
  294. return 0;
  295. }
  296. /*
  297. * port_build_proto_msg(): create connection protocol message for port
  298. *
  299. * On entry the port must be locked and connected.
  300. */
  301. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  302. u32 type, u32 ack)
  303. {
  304. struct sk_buff *buf;
  305. struct tipc_msg *msg;
  306. buf = tipc_buf_acquire(INT_H_SIZE);
  307. if (buf) {
  308. msg = buf_msg(buf);
  309. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  310. port_peernode(p_ptr));
  311. msg_set_destport(msg, port_peerport(p_ptr));
  312. msg_set_origport(msg, p_ptr->ref);
  313. msg_set_msgcnt(msg, ack);
  314. }
  315. return buf;
  316. }
  317. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  318. {
  319. struct tipc_msg *msg = buf_msg(buf);
  320. struct sk_buff *rbuf;
  321. struct tipc_msg *rmsg;
  322. int hdr_sz;
  323. u32 imp;
  324. u32 data_sz = msg_data_sz(msg);
  325. u32 src_node;
  326. u32 rmsg_sz;
  327. /* discard rejected message if it shouldn't be returned to sender */
  328. if (WARN(!msg_isdata(msg),
  329. "attempt to reject message with user=%u", msg_user(msg))) {
  330. dump_stack();
  331. goto exit;
  332. }
  333. if (msg_errcode(msg) || msg_dest_droppable(msg))
  334. goto exit;
  335. /*
  336. * construct returned message by copying rejected message header and
  337. * data (or subset), then updating header fields that need adjusting
  338. */
  339. hdr_sz = msg_hdr_sz(msg);
  340. rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
  341. rbuf = tipc_buf_acquire(rmsg_sz);
  342. if (rbuf == NULL)
  343. goto exit;
  344. rmsg = buf_msg(rbuf);
  345. skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
  346. if (msg_connected(rmsg)) {
  347. imp = msg_importance(rmsg);
  348. if (imp < TIPC_CRITICAL_IMPORTANCE)
  349. msg_set_importance(rmsg, ++imp);
  350. }
  351. msg_set_non_seq(rmsg, 0);
  352. msg_set_size(rmsg, rmsg_sz);
  353. msg_set_errcode(rmsg, err);
  354. msg_set_prevnode(rmsg, tipc_own_addr);
  355. msg_swap_words(rmsg, 4, 5);
  356. if (!msg_short(rmsg))
  357. msg_swap_words(rmsg, 6, 7);
  358. /* send self-abort message when rejecting on a connected port */
  359. if (msg_connected(msg)) {
  360. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  361. if (p_ptr) {
  362. struct sk_buff *abuf = NULL;
  363. if (p_ptr->connected)
  364. abuf = port_build_self_abort_msg(p_ptr, err);
  365. tipc_port_unlock(p_ptr);
  366. tipc_net_route_msg(abuf);
  367. }
  368. }
  369. /* send returned message & dispose of rejected message */
  370. src_node = msg_prevnode(msg);
  371. if (in_own_node(src_node))
  372. tipc_port_recv_msg(rbuf);
  373. else
  374. tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
  375. exit:
  376. kfree_skb(buf);
  377. return data_sz;
  378. }
  379. int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
  380. struct iovec const *msg_sect, unsigned int len,
  381. int err)
  382. {
  383. struct sk_buff *buf;
  384. int res;
  385. res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  386. if (!buf)
  387. return res;
  388. return tipc_reject_msg(buf, err);
  389. }
  390. static void port_timeout(unsigned long ref)
  391. {
  392. struct tipc_port *p_ptr = tipc_port_lock(ref);
  393. struct sk_buff *buf = NULL;
  394. if (!p_ptr)
  395. return;
  396. if (!p_ptr->connected) {
  397. tipc_port_unlock(p_ptr);
  398. return;
  399. }
  400. /* Last probe answered ? */
  401. if (p_ptr->probing_state == PROBING) {
  402. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  403. } else {
  404. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  405. p_ptr->probing_state = PROBING;
  406. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  407. }
  408. tipc_port_unlock(p_ptr);
  409. tipc_net_route_msg(buf);
  410. }
  411. static void port_handle_node_down(unsigned long ref)
  412. {
  413. struct tipc_port *p_ptr = tipc_port_lock(ref);
  414. struct sk_buff *buf = NULL;
  415. if (!p_ptr)
  416. return;
  417. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  418. tipc_port_unlock(p_ptr);
  419. tipc_net_route_msg(buf);
  420. }
  421. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  422. {
  423. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  424. if (buf) {
  425. struct tipc_msg *msg = buf_msg(buf);
  426. msg_swap_words(msg, 4, 5);
  427. msg_swap_words(msg, 6, 7);
  428. }
  429. return buf;
  430. }
  431. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  432. {
  433. struct sk_buff *buf;
  434. struct tipc_msg *msg;
  435. u32 imp;
  436. if (!p_ptr->connected)
  437. return NULL;
  438. buf = tipc_buf_acquire(BASIC_H_SIZE);
  439. if (buf) {
  440. msg = buf_msg(buf);
  441. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  442. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  443. msg_set_size(msg, BASIC_H_SIZE);
  444. imp = msg_importance(msg);
  445. if (imp < TIPC_CRITICAL_IMPORTANCE)
  446. msg_set_importance(msg, ++imp);
  447. msg_set_errcode(msg, err);
  448. }
  449. return buf;
  450. }
  451. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  452. {
  453. struct tipc_msg *msg = buf_msg(buf);
  454. struct tipc_port *p_ptr;
  455. struct sk_buff *r_buf = NULL;
  456. u32 destport = msg_destport(msg);
  457. int wakeable;
  458. /* Validate connection */
  459. p_ptr = tipc_port_lock(destport);
  460. if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
  461. r_buf = tipc_buf_acquire(BASIC_H_SIZE);
  462. if (r_buf) {
  463. msg = buf_msg(r_buf);
  464. tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
  465. BASIC_H_SIZE, msg_orignode(msg));
  466. msg_set_errcode(msg, TIPC_ERR_NO_PORT);
  467. msg_set_origport(msg, destport);
  468. msg_set_destport(msg, msg_origport(msg));
  469. }
  470. if (p_ptr)
  471. tipc_port_unlock(p_ptr);
  472. goto exit;
  473. }
  474. /* Process protocol message sent by peer */
  475. switch (msg_type(msg)) {
  476. case CONN_ACK:
  477. wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
  478. p_ptr->wakeup;
  479. p_ptr->acked += msg_msgcnt(msg);
  480. if (!tipc_port_congested(p_ptr)) {
  481. p_ptr->congested = 0;
  482. if (wakeable)
  483. p_ptr->wakeup(p_ptr);
  484. }
  485. break;
  486. case CONN_PROBE:
  487. r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
  488. break;
  489. default:
  490. /* CONN_PROBE_REPLY or unrecognized - no action required */
  491. break;
  492. }
  493. p_ptr->probing_state = CONFIRMED;
  494. tipc_port_unlock(p_ptr);
  495. exit:
  496. tipc_net_route_msg(r_buf);
  497. kfree_skb(buf);
  498. }
  499. static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
  500. {
  501. struct publication *publ;
  502. int ret;
  503. if (full_id)
  504. ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
  505. tipc_zone(tipc_own_addr),
  506. tipc_cluster(tipc_own_addr),
  507. tipc_node(tipc_own_addr), p_ptr->ref);
  508. else
  509. ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
  510. if (p_ptr->connected) {
  511. u32 dport = port_peerport(p_ptr);
  512. u32 destnode = port_peernode(p_ptr);
  513. ret += tipc_snprintf(buf + ret, len - ret,
  514. " connected to <%u.%u.%u:%u>",
  515. tipc_zone(destnode),
  516. tipc_cluster(destnode),
  517. tipc_node(destnode), dport);
  518. if (p_ptr->conn_type != 0)
  519. ret += tipc_snprintf(buf + ret, len - ret,
  520. " via {%u,%u}", p_ptr->conn_type,
  521. p_ptr->conn_instance);
  522. } else if (p_ptr->published) {
  523. ret += tipc_snprintf(buf + ret, len - ret, " bound to");
  524. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  525. if (publ->lower == publ->upper)
  526. ret += tipc_snprintf(buf + ret, len - ret,
  527. " {%u,%u}", publ->type,
  528. publ->lower);
  529. else
  530. ret += tipc_snprintf(buf + ret, len - ret,
  531. " {%u,%u,%u}", publ->type,
  532. publ->lower, publ->upper);
  533. }
  534. }
  535. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  536. return ret;
  537. }
  538. struct sk_buff *tipc_port_get_ports(void)
  539. {
  540. struct sk_buff *buf;
  541. struct tlv_desc *rep_tlv;
  542. char *pb;
  543. int pb_len;
  544. struct tipc_port *p_ptr;
  545. int str_len = 0;
  546. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  547. if (!buf)
  548. return NULL;
  549. rep_tlv = (struct tlv_desc *)buf->data;
  550. pb = TLV_DATA(rep_tlv);
  551. pb_len = ULTRA_STRING_MAX_LEN;
  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. str_len += port_print(p_ptr, pb, pb_len, 0);
  556. spin_unlock_bh(p_ptr->lock);
  557. }
  558. spin_unlock_bh(&tipc_port_list_lock);
  559. str_len += 1; /* for "\0" */
  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. msg_set_prevnode(msg, tipc_own_addr);
  572. msg_set_orignode(msg, tipc_own_addr);
  573. }
  574. spin_unlock_bh(&tipc_port_list_lock);
  575. }
  576. void tipc_acknowledge(u32 ref, u32 ack)
  577. {
  578. struct tipc_port *p_ptr;
  579. struct sk_buff *buf = NULL;
  580. p_ptr = tipc_port_lock(ref);
  581. if (!p_ptr)
  582. return;
  583. if (p_ptr->connected) {
  584. p_ptr->conn_unacked -= ack;
  585. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  586. }
  587. tipc_port_unlock(p_ptr);
  588. tipc_net_route_msg(buf);
  589. }
  590. int tipc_portimportance(u32 ref, unsigned int *importance)
  591. {
  592. struct tipc_port *p_ptr;
  593. p_ptr = tipc_port_lock(ref);
  594. if (!p_ptr)
  595. return -EINVAL;
  596. *importance = (unsigned int)msg_importance(&p_ptr->phdr);
  597. tipc_port_unlock(p_ptr);
  598. return 0;
  599. }
  600. int tipc_set_portimportance(u32 ref, unsigned int imp)
  601. {
  602. struct tipc_port *p_ptr;
  603. if (imp > TIPC_CRITICAL_IMPORTANCE)
  604. return -EINVAL;
  605. p_ptr = tipc_port_lock(ref);
  606. if (!p_ptr)
  607. return -EINVAL;
  608. msg_set_importance(&p_ptr->phdr, (u32)imp);
  609. tipc_port_unlock(p_ptr);
  610. return 0;
  611. }
  612. int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
  613. struct tipc_name_seq const *seq)
  614. {
  615. struct publication *publ;
  616. u32 key;
  617. if (p_ptr->connected)
  618. return -EINVAL;
  619. key = p_ptr->ref + p_ptr->pub_count + 1;
  620. if (key == p_ptr->ref)
  621. return -EADDRINUSE;
  622. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  623. scope, p_ptr->ref, key);
  624. if (publ) {
  625. list_add(&publ->pport_list, &p_ptr->publications);
  626. p_ptr->pub_count++;
  627. p_ptr->published = 1;
  628. return 0;
  629. }
  630. return -EINVAL;
  631. }
  632. int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
  633. struct tipc_name_seq const *seq)
  634. {
  635. struct publication *publ;
  636. struct publication *tpubl;
  637. int res = -EINVAL;
  638. if (!seq) {
  639. list_for_each_entry_safe(publ, tpubl,
  640. &p_ptr->publications, pport_list) {
  641. tipc_nametbl_withdraw(publ->type, publ->lower,
  642. publ->ref, publ->key);
  643. }
  644. res = 0;
  645. } else {
  646. list_for_each_entry_safe(publ, tpubl,
  647. &p_ptr->publications, pport_list) {
  648. if (publ->scope != scope)
  649. continue;
  650. if (publ->type != seq->type)
  651. continue;
  652. if (publ->lower != seq->lower)
  653. continue;
  654. if (publ->upper != seq->upper)
  655. break;
  656. tipc_nametbl_withdraw(publ->type, publ->lower,
  657. publ->ref, publ->key);
  658. res = 0;
  659. break;
  660. }
  661. }
  662. if (list_empty(&p_ptr->publications))
  663. p_ptr->published = 0;
  664. return res;
  665. }
  666. int tipc_connect(u32 ref, struct tipc_portid const *peer)
  667. {
  668. struct tipc_port *p_ptr;
  669. int res;
  670. p_ptr = tipc_port_lock(ref);
  671. if (!p_ptr)
  672. return -EINVAL;
  673. res = __tipc_connect(ref, p_ptr, peer);
  674. tipc_port_unlock(p_ptr);
  675. return res;
  676. }
  677. /*
  678. * __tipc_connect - connect to a remote peer
  679. *
  680. * Port must be locked.
  681. */
  682. int __tipc_connect(u32 ref, struct tipc_port *p_ptr,
  683. struct tipc_portid const *peer)
  684. {
  685. struct tipc_msg *msg;
  686. int res = -EINVAL;
  687. if (p_ptr->published || p_ptr->connected)
  688. goto exit;
  689. if (!peer->ref)
  690. goto exit;
  691. msg = &p_ptr->phdr;
  692. msg_set_destnode(msg, peer->node);
  693. msg_set_destport(msg, peer->ref);
  694. msg_set_type(msg, TIPC_CONN_MSG);
  695. msg_set_lookup_scope(msg, 0);
  696. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  697. p_ptr->probing_interval = PROBING_INTERVAL;
  698. p_ptr->probing_state = CONFIRMED;
  699. p_ptr->connected = 1;
  700. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  701. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  702. (void *)(unsigned long)ref,
  703. (net_ev_handler)port_handle_node_down);
  704. res = 0;
  705. exit:
  706. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  707. return res;
  708. }
  709. /*
  710. * __tipc_disconnect - disconnect port from peer
  711. *
  712. * Port must be locked.
  713. */
  714. int __tipc_disconnect(struct tipc_port *tp_ptr)
  715. {
  716. if (tp_ptr->connected) {
  717. tp_ptr->connected = 0;
  718. /* let timer expire on it's own to avoid deadlock! */
  719. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  720. return 0;
  721. }
  722. return -ENOTCONN;
  723. }
  724. /*
  725. * tipc_disconnect(): Disconnect port form peer.
  726. * This is a node local operation.
  727. */
  728. int tipc_disconnect(u32 ref)
  729. {
  730. struct tipc_port *p_ptr;
  731. int res;
  732. p_ptr = tipc_port_lock(ref);
  733. if (!p_ptr)
  734. return -EINVAL;
  735. res = __tipc_disconnect(p_ptr);
  736. tipc_port_unlock(p_ptr);
  737. return res;
  738. }
  739. /*
  740. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  741. */
  742. int tipc_shutdown(u32 ref)
  743. {
  744. struct tipc_port *p_ptr;
  745. struct sk_buff *buf = NULL;
  746. p_ptr = tipc_port_lock(ref);
  747. if (!p_ptr)
  748. return -EINVAL;
  749. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  750. tipc_port_unlock(p_ptr);
  751. tipc_net_route_msg(buf);
  752. return tipc_disconnect(ref);
  753. }
  754. /**
  755. * tipc_port_recv_msg - receive message from lower layer and deliver to port user
  756. */
  757. int tipc_port_recv_msg(struct sk_buff *buf)
  758. {
  759. struct tipc_port *p_ptr;
  760. struct tipc_msg *msg = buf_msg(buf);
  761. u32 destport = msg_destport(msg);
  762. u32 dsz = msg_data_sz(msg);
  763. u32 err;
  764. /* forward unresolved named message */
  765. if (unlikely(!destport)) {
  766. tipc_net_route_msg(buf);
  767. return dsz;
  768. }
  769. /* validate destination & pass to port, otherwise reject message */
  770. p_ptr = tipc_port_lock(destport);
  771. if (likely(p_ptr)) {
  772. err = p_ptr->dispatcher(p_ptr, buf);
  773. tipc_port_unlock(p_ptr);
  774. if (likely(!err))
  775. return dsz;
  776. } else {
  777. err = TIPC_ERR_NO_PORT;
  778. }
  779. return tipc_reject_msg(buf, err);
  780. }
  781. /*
  782. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  783. * message for this node.
  784. */
  785. static int tipc_port_recv_sections(struct tipc_port *sender,
  786. struct iovec const *msg_sect,
  787. unsigned int len)
  788. {
  789. struct sk_buff *buf;
  790. int res;
  791. res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  792. if (likely(buf))
  793. tipc_port_recv_msg(buf);
  794. return res;
  795. }
  796. /**
  797. * tipc_send - send message sections on connection
  798. */
  799. int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
  800. {
  801. struct tipc_port *p_ptr;
  802. u32 destnode;
  803. int res;
  804. p_ptr = tipc_port_deref(ref);
  805. if (!p_ptr || !p_ptr->connected)
  806. return -EINVAL;
  807. p_ptr->congested = 1;
  808. if (!tipc_port_congested(p_ptr)) {
  809. destnode = port_peernode(p_ptr);
  810. if (likely(!in_own_node(destnode)))
  811. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  812. len, destnode);
  813. else
  814. res = tipc_port_recv_sections(p_ptr, msg_sect, len);
  815. if (likely(res != -ELINKCONG)) {
  816. p_ptr->congested = 0;
  817. if (res > 0)
  818. p_ptr->sent++;
  819. return res;
  820. }
  821. }
  822. if (port_unreliable(p_ptr)) {
  823. p_ptr->congested = 0;
  824. return len;
  825. }
  826. return -ELINKCONG;
  827. }
  828. /**
  829. * tipc_send2name - send message sections to port name
  830. */
  831. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  832. struct iovec const *msg_sect, unsigned int len)
  833. {
  834. struct tipc_port *p_ptr;
  835. struct tipc_msg *msg;
  836. u32 destnode = domain;
  837. u32 destport;
  838. int res;
  839. p_ptr = tipc_port_deref(ref);
  840. if (!p_ptr || p_ptr->connected)
  841. return -EINVAL;
  842. msg = &p_ptr->phdr;
  843. msg_set_type(msg, TIPC_NAMED_MSG);
  844. msg_set_hdr_sz(msg, NAMED_H_SIZE);
  845. msg_set_nametype(msg, name->type);
  846. msg_set_nameinst(msg, name->instance);
  847. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  848. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  849. msg_set_destnode(msg, destnode);
  850. msg_set_destport(msg, destport);
  851. if (likely(destport || destnode)) {
  852. if (likely(in_own_node(destnode)))
  853. res = tipc_port_recv_sections(p_ptr, msg_sect, len);
  854. else if (tipc_own_addr)
  855. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  856. len, destnode);
  857. else
  858. res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
  859. len, TIPC_ERR_NO_NODE);
  860. if (likely(res != -ELINKCONG)) {
  861. if (res > 0)
  862. p_ptr->sent++;
  863. return res;
  864. }
  865. if (port_unreliable(p_ptr)) {
  866. return len;
  867. }
  868. return -ELINKCONG;
  869. }
  870. return tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
  871. TIPC_ERR_NO_NAME);
  872. }
  873. /**
  874. * tipc_send2port - send message sections to port identity
  875. */
  876. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  877. struct iovec const *msg_sect, unsigned int len)
  878. {
  879. struct tipc_port *p_ptr;
  880. struct tipc_msg *msg;
  881. int res;
  882. p_ptr = tipc_port_deref(ref);
  883. if (!p_ptr || p_ptr->connected)
  884. return -EINVAL;
  885. msg = &p_ptr->phdr;
  886. msg_set_type(msg, TIPC_DIRECT_MSG);
  887. msg_set_lookup_scope(msg, 0);
  888. msg_set_destnode(msg, dest->node);
  889. msg_set_destport(msg, dest->ref);
  890. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  891. if (in_own_node(dest->node))
  892. res = tipc_port_recv_sections(p_ptr, msg_sect, len);
  893. else if (tipc_own_addr)
  894. res = tipc_link_send_sections_fast(p_ptr, msg_sect, len,
  895. dest->node);
  896. else
  897. res = tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
  898. TIPC_ERR_NO_NODE);
  899. if (likely(res != -ELINKCONG)) {
  900. if (res > 0)
  901. p_ptr->sent++;
  902. return res;
  903. }
  904. if (port_unreliable(p_ptr)) {
  905. return len;
  906. }
  907. return -ELINKCONG;
  908. }