port.c 26 KB

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