port.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, 2014, 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. #include "socket.h"
  41. /* Connection management: */
  42. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  43. #define CONFIRMED 0
  44. #define PROBING 1
  45. #define MAX_REJECT_SIZE 1024
  46. DEFINE_SPINLOCK(tipc_port_list_lock);
  47. static LIST_HEAD(ports);
  48. static void port_handle_node_down(unsigned long ref);
  49. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
  50. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
  51. static void port_timeout(unsigned long ref);
  52. /**
  53. * tipc_port_peer_msg - verify message was sent by connected port's peer
  54. *
  55. * Handles cases where the node's network address has changed from
  56. * the default of <0.0.0> to its configured setting.
  57. */
  58. int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
  59. {
  60. u32 peernode;
  61. u32 orignode;
  62. if (msg_origport(msg) != tipc_port_peerport(p_ptr))
  63. return 0;
  64. orignode = msg_orignode(msg);
  65. peernode = tipc_port_peernode(p_ptr);
  66. return (orignode == peernode) ||
  67. (!orignode && (peernode == tipc_own_addr)) ||
  68. (!peernode && (orignode == tipc_own_addr));
  69. }
  70. /**
  71. * tipc_port_mcast_xmit - send a multicast message to local and remote
  72. * destinations
  73. */
  74. int tipc_port_mcast_xmit(struct tipc_port *oport,
  75. struct tipc_name_seq const *seq,
  76. struct iovec const *msg_sect,
  77. unsigned int len)
  78. {
  79. struct tipc_msg *hdr;
  80. struct sk_buff *buf;
  81. struct sk_buff *ibuf = NULL;
  82. struct tipc_port_list dports = {0, NULL, };
  83. int ext_targets;
  84. int res;
  85. /* Create multicast message */
  86. hdr = &oport->phdr;
  87. msg_set_type(hdr, TIPC_MCAST_MSG);
  88. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  89. msg_set_destport(hdr, 0);
  90. msg_set_destnode(hdr, 0);
  91. msg_set_nametype(hdr, seq->type);
  92. msg_set_namelower(hdr, seq->lower);
  93. msg_set_nameupper(hdr, seq->upper);
  94. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  95. res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  96. if (unlikely(!buf))
  97. return res;
  98. /* Figure out where to send multicast message */
  99. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  100. TIPC_NODE_SCOPE, &dports);
  101. /* Send message to destinations (duplicate it only if necessary) */
  102. if (ext_targets) {
  103. if (dports.count != 0) {
  104. ibuf = skb_copy(buf, GFP_ATOMIC);
  105. if (ibuf == NULL) {
  106. tipc_port_list_free(&dports);
  107. kfree_skb(buf);
  108. return -ENOMEM;
  109. }
  110. }
  111. res = tipc_bclink_xmit(buf);
  112. if ((res < 0) && (dports.count != 0))
  113. kfree_skb(ibuf);
  114. } else {
  115. ibuf = buf;
  116. }
  117. if (res >= 0) {
  118. if (ibuf)
  119. tipc_port_mcast_rcv(ibuf, &dports);
  120. } else {
  121. tipc_port_list_free(&dports);
  122. }
  123. return res;
  124. }
  125. /**
  126. * tipc_port_mcast_rcv - deliver multicast message to all destination ports
  127. *
  128. * If there is no port list, perform a lookup to create one
  129. */
  130. void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
  131. {
  132. struct tipc_msg *msg;
  133. struct tipc_port_list dports = {0, NULL, };
  134. struct tipc_port_list *item = dp;
  135. int cnt = 0;
  136. msg = buf_msg(buf);
  137. /* Create destination port list, if one wasn't supplied */
  138. if (dp == NULL) {
  139. tipc_nametbl_mc_translate(msg_nametype(msg),
  140. msg_namelower(msg),
  141. msg_nameupper(msg),
  142. TIPC_CLUSTER_SCOPE,
  143. &dports);
  144. item = dp = &dports;
  145. }
  146. /* Deliver a copy of message to each destination port */
  147. if (dp->count != 0) {
  148. msg_set_destnode(msg, tipc_own_addr);
  149. if (dp->count == 1) {
  150. msg_set_destport(msg, dp->ports[0]);
  151. tipc_sk_rcv(buf);
  152. tipc_port_list_free(dp);
  153. return;
  154. }
  155. for (; cnt < dp->count; cnt++) {
  156. int index = cnt % PLSIZE;
  157. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  158. if (b == NULL) {
  159. pr_warn("Unable to deliver multicast message(s)\n");
  160. goto exit;
  161. }
  162. if ((index == 0) && (cnt != 0))
  163. item = item->next;
  164. msg_set_destport(buf_msg(b), item->ports[index]);
  165. tipc_sk_rcv(b);
  166. }
  167. }
  168. exit:
  169. kfree_skb(buf);
  170. tipc_port_list_free(dp);
  171. }
  172. void tipc_port_wakeup(struct tipc_port *port)
  173. {
  174. tipc_sock_wakeup(tipc_port_to_sock(port));
  175. }
  176. /* tipc_port_init - intiate TIPC port and lock it
  177. *
  178. * Returns obtained reference if initialization is successful, zero otherwise
  179. */
  180. u32 tipc_port_init(struct tipc_port *p_ptr,
  181. const unsigned int importance)
  182. {
  183. struct tipc_msg *msg;
  184. u32 ref;
  185. ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
  186. if (!ref) {
  187. pr_warn("Port registration failed, ref. table exhausted\n");
  188. return 0;
  189. }
  190. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  191. p_ptr->sent = 1;
  192. p_ptr->ref = ref;
  193. INIT_LIST_HEAD(&p_ptr->wait_list);
  194. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  195. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  196. INIT_LIST_HEAD(&p_ptr->publications);
  197. INIT_LIST_HEAD(&p_ptr->port_list);
  198. /*
  199. * Must hold port list lock while initializing message header template
  200. * to ensure a change to node's own network address doesn't result
  201. * in template containing out-dated network address information
  202. */
  203. spin_lock_bh(&tipc_port_list_lock);
  204. msg = &p_ptr->phdr;
  205. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  206. msg_set_origport(msg, ref);
  207. list_add_tail(&p_ptr->port_list, &ports);
  208. spin_unlock_bh(&tipc_port_list_lock);
  209. return ref;
  210. }
  211. void tipc_port_destroy(struct tipc_port *p_ptr)
  212. {
  213. struct sk_buff *buf = NULL;
  214. tipc_withdraw(p_ptr, 0, NULL);
  215. spin_lock_bh(p_ptr->lock);
  216. tipc_ref_discard(p_ptr->ref);
  217. spin_unlock_bh(p_ptr->lock);
  218. k_cancel_timer(&p_ptr->timer);
  219. if (p_ptr->connected) {
  220. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  221. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  222. }
  223. spin_lock_bh(&tipc_port_list_lock);
  224. list_del(&p_ptr->port_list);
  225. list_del(&p_ptr->wait_list);
  226. spin_unlock_bh(&tipc_port_list_lock);
  227. k_term_timer(&p_ptr->timer);
  228. tipc_net_route_msg(buf);
  229. }
  230. /*
  231. * port_build_proto_msg(): create connection protocol message for port
  232. *
  233. * On entry the port must be locked and connected.
  234. */
  235. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  236. u32 type, u32 ack)
  237. {
  238. struct sk_buff *buf;
  239. struct tipc_msg *msg;
  240. buf = tipc_buf_acquire(INT_H_SIZE);
  241. if (buf) {
  242. msg = buf_msg(buf);
  243. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  244. tipc_port_peernode(p_ptr));
  245. msg_set_destport(msg, tipc_port_peerport(p_ptr));
  246. msg_set_origport(msg, p_ptr->ref);
  247. msg_set_msgcnt(msg, ack);
  248. }
  249. return buf;
  250. }
  251. static void port_timeout(unsigned long ref)
  252. {
  253. struct tipc_port *p_ptr = tipc_port_lock(ref);
  254. struct sk_buff *buf = NULL;
  255. if (!p_ptr)
  256. return;
  257. if (!p_ptr->connected) {
  258. tipc_port_unlock(p_ptr);
  259. return;
  260. }
  261. /* Last probe answered ? */
  262. if (p_ptr->probing_state == PROBING) {
  263. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  264. } else {
  265. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  266. p_ptr->probing_state = PROBING;
  267. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  268. }
  269. tipc_port_unlock(p_ptr);
  270. tipc_net_route_msg(buf);
  271. }
  272. static void port_handle_node_down(unsigned long ref)
  273. {
  274. struct tipc_port *p_ptr = tipc_port_lock(ref);
  275. struct sk_buff *buf = NULL;
  276. if (!p_ptr)
  277. return;
  278. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  279. tipc_port_unlock(p_ptr);
  280. tipc_net_route_msg(buf);
  281. }
  282. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  283. {
  284. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  285. if (buf) {
  286. struct tipc_msg *msg = buf_msg(buf);
  287. msg_swap_words(msg, 4, 5);
  288. msg_swap_words(msg, 6, 7);
  289. }
  290. return buf;
  291. }
  292. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  293. {
  294. struct sk_buff *buf;
  295. struct tipc_msg *msg;
  296. u32 imp;
  297. if (!p_ptr->connected)
  298. return NULL;
  299. buf = tipc_buf_acquire(BASIC_H_SIZE);
  300. if (buf) {
  301. msg = buf_msg(buf);
  302. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  303. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  304. msg_set_size(msg, BASIC_H_SIZE);
  305. imp = msg_importance(msg);
  306. if (imp < TIPC_CRITICAL_IMPORTANCE)
  307. msg_set_importance(msg, ++imp);
  308. msg_set_errcode(msg, err);
  309. }
  310. return buf;
  311. }
  312. void tipc_port_proto_rcv(struct sk_buff *buf)
  313. {
  314. struct tipc_msg *msg = buf_msg(buf);
  315. struct tipc_port *p_ptr;
  316. struct sk_buff *r_buf = NULL;
  317. u32 destport = msg_destport(msg);
  318. int wakeable;
  319. /* Validate connection */
  320. p_ptr = tipc_port_lock(destport);
  321. if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
  322. r_buf = tipc_buf_acquire(BASIC_H_SIZE);
  323. if (r_buf) {
  324. msg = buf_msg(r_buf);
  325. tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
  326. BASIC_H_SIZE, msg_orignode(msg));
  327. msg_set_errcode(msg, TIPC_ERR_NO_PORT);
  328. msg_set_origport(msg, destport);
  329. msg_set_destport(msg, msg_origport(msg));
  330. }
  331. if (p_ptr)
  332. tipc_port_unlock(p_ptr);
  333. goto exit;
  334. }
  335. /* Process protocol message sent by peer */
  336. switch (msg_type(msg)) {
  337. case CONN_ACK:
  338. wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
  339. p_ptr->acked += msg_msgcnt(msg);
  340. if (!tipc_port_congested(p_ptr)) {
  341. p_ptr->congested = 0;
  342. if (wakeable)
  343. tipc_port_wakeup(p_ptr);
  344. }
  345. break;
  346. case CONN_PROBE:
  347. r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
  348. break;
  349. default:
  350. /* CONN_PROBE_REPLY or unrecognized - no action required */
  351. break;
  352. }
  353. p_ptr->probing_state = CONFIRMED;
  354. tipc_port_unlock(p_ptr);
  355. exit:
  356. tipc_net_route_msg(r_buf);
  357. kfree_skb(buf);
  358. }
  359. static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
  360. {
  361. struct publication *publ;
  362. int ret;
  363. if (full_id)
  364. ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
  365. tipc_zone(tipc_own_addr),
  366. tipc_cluster(tipc_own_addr),
  367. tipc_node(tipc_own_addr), p_ptr->ref);
  368. else
  369. ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
  370. if (p_ptr->connected) {
  371. u32 dport = tipc_port_peerport(p_ptr);
  372. u32 destnode = tipc_port_peernode(p_ptr);
  373. ret += tipc_snprintf(buf + ret, len - ret,
  374. " connected to <%u.%u.%u:%u>",
  375. tipc_zone(destnode),
  376. tipc_cluster(destnode),
  377. tipc_node(destnode), dport);
  378. if (p_ptr->conn_type != 0)
  379. ret += tipc_snprintf(buf + ret, len - ret,
  380. " via {%u,%u}", p_ptr->conn_type,
  381. p_ptr->conn_instance);
  382. } else if (p_ptr->published) {
  383. ret += tipc_snprintf(buf + ret, len - ret, " bound to");
  384. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  385. if (publ->lower == publ->upper)
  386. ret += tipc_snprintf(buf + ret, len - ret,
  387. " {%u,%u}", publ->type,
  388. publ->lower);
  389. else
  390. ret += tipc_snprintf(buf + ret, len - ret,
  391. " {%u,%u,%u}", publ->type,
  392. publ->lower, publ->upper);
  393. }
  394. }
  395. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  396. return ret;
  397. }
  398. struct sk_buff *tipc_port_get_ports(void)
  399. {
  400. struct sk_buff *buf;
  401. struct tlv_desc *rep_tlv;
  402. char *pb;
  403. int pb_len;
  404. struct tipc_port *p_ptr;
  405. int str_len = 0;
  406. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  407. if (!buf)
  408. return NULL;
  409. rep_tlv = (struct tlv_desc *)buf->data;
  410. pb = TLV_DATA(rep_tlv);
  411. pb_len = ULTRA_STRING_MAX_LEN;
  412. spin_lock_bh(&tipc_port_list_lock);
  413. list_for_each_entry(p_ptr, &ports, port_list) {
  414. spin_lock_bh(p_ptr->lock);
  415. str_len += port_print(p_ptr, pb, pb_len, 0);
  416. spin_unlock_bh(p_ptr->lock);
  417. }
  418. spin_unlock_bh(&tipc_port_list_lock);
  419. str_len += 1; /* for "\0" */
  420. skb_put(buf, TLV_SPACE(str_len));
  421. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  422. return buf;
  423. }
  424. void tipc_port_reinit(void)
  425. {
  426. struct tipc_port *p_ptr;
  427. struct tipc_msg *msg;
  428. spin_lock_bh(&tipc_port_list_lock);
  429. list_for_each_entry(p_ptr, &ports, port_list) {
  430. msg = &p_ptr->phdr;
  431. msg_set_prevnode(msg, tipc_own_addr);
  432. msg_set_orignode(msg, tipc_own_addr);
  433. }
  434. spin_unlock_bh(&tipc_port_list_lock);
  435. }
  436. void tipc_acknowledge(u32 ref, u32 ack)
  437. {
  438. struct tipc_port *p_ptr;
  439. struct sk_buff *buf = NULL;
  440. p_ptr = tipc_port_lock(ref);
  441. if (!p_ptr)
  442. return;
  443. if (p_ptr->connected) {
  444. p_ptr->conn_unacked -= ack;
  445. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  446. }
  447. tipc_port_unlock(p_ptr);
  448. tipc_net_route_msg(buf);
  449. }
  450. int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
  451. struct tipc_name_seq const *seq)
  452. {
  453. struct publication *publ;
  454. u32 key;
  455. if (p_ptr->connected)
  456. return -EINVAL;
  457. key = p_ptr->ref + p_ptr->pub_count + 1;
  458. if (key == p_ptr->ref)
  459. return -EADDRINUSE;
  460. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  461. scope, p_ptr->ref, key);
  462. if (publ) {
  463. list_add(&publ->pport_list, &p_ptr->publications);
  464. p_ptr->pub_count++;
  465. p_ptr->published = 1;
  466. return 0;
  467. }
  468. return -EINVAL;
  469. }
  470. int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
  471. struct tipc_name_seq const *seq)
  472. {
  473. struct publication *publ;
  474. struct publication *tpubl;
  475. int res = -EINVAL;
  476. if (!seq) {
  477. list_for_each_entry_safe(publ, tpubl,
  478. &p_ptr->publications, pport_list) {
  479. tipc_nametbl_withdraw(publ->type, publ->lower,
  480. publ->ref, publ->key);
  481. }
  482. res = 0;
  483. } else {
  484. list_for_each_entry_safe(publ, tpubl,
  485. &p_ptr->publications, pport_list) {
  486. if (publ->scope != scope)
  487. continue;
  488. if (publ->type != seq->type)
  489. continue;
  490. if (publ->lower != seq->lower)
  491. continue;
  492. if (publ->upper != seq->upper)
  493. break;
  494. tipc_nametbl_withdraw(publ->type, publ->lower,
  495. publ->ref, publ->key);
  496. res = 0;
  497. break;
  498. }
  499. }
  500. if (list_empty(&p_ptr->publications))
  501. p_ptr->published = 0;
  502. return res;
  503. }
  504. int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
  505. {
  506. struct tipc_port *p_ptr;
  507. int res;
  508. p_ptr = tipc_port_lock(ref);
  509. if (!p_ptr)
  510. return -EINVAL;
  511. res = __tipc_port_connect(ref, p_ptr, peer);
  512. tipc_port_unlock(p_ptr);
  513. return res;
  514. }
  515. /*
  516. * __tipc_port_connect - connect to a remote peer
  517. *
  518. * Port must be locked.
  519. */
  520. int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
  521. struct tipc_portid const *peer)
  522. {
  523. struct tipc_msg *msg;
  524. int res = -EINVAL;
  525. if (p_ptr->published || p_ptr->connected)
  526. goto exit;
  527. if (!peer->ref)
  528. goto exit;
  529. msg = &p_ptr->phdr;
  530. msg_set_destnode(msg, peer->node);
  531. msg_set_destport(msg, peer->ref);
  532. msg_set_type(msg, TIPC_CONN_MSG);
  533. msg_set_lookup_scope(msg, 0);
  534. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  535. p_ptr->probing_interval = PROBING_INTERVAL;
  536. p_ptr->probing_state = CONFIRMED;
  537. p_ptr->connected = 1;
  538. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  539. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  540. (void *)(unsigned long)ref,
  541. (net_ev_handler)port_handle_node_down);
  542. res = 0;
  543. exit:
  544. p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
  545. return res;
  546. }
  547. /*
  548. * __tipc_disconnect - disconnect port from peer
  549. *
  550. * Port must be locked.
  551. */
  552. int __tipc_port_disconnect(struct tipc_port *tp_ptr)
  553. {
  554. if (tp_ptr->connected) {
  555. tp_ptr->connected = 0;
  556. /* let timer expire on it's own to avoid deadlock! */
  557. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  558. return 0;
  559. }
  560. return -ENOTCONN;
  561. }
  562. /*
  563. * tipc_port_disconnect(): Disconnect port form peer.
  564. * This is a node local operation.
  565. */
  566. int tipc_port_disconnect(u32 ref)
  567. {
  568. struct tipc_port *p_ptr;
  569. int res;
  570. p_ptr = tipc_port_lock(ref);
  571. if (!p_ptr)
  572. return -EINVAL;
  573. res = __tipc_port_disconnect(p_ptr);
  574. tipc_port_unlock(p_ptr);
  575. return res;
  576. }
  577. /*
  578. * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  579. */
  580. int tipc_port_shutdown(u32 ref)
  581. {
  582. struct tipc_port *p_ptr;
  583. struct sk_buff *buf = NULL;
  584. p_ptr = tipc_port_lock(ref);
  585. if (!p_ptr)
  586. return -EINVAL;
  587. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  588. tipc_port_unlock(p_ptr);
  589. tipc_net_route_msg(buf);
  590. return tipc_port_disconnect(ref);
  591. }