port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 MAX_REJECT_SIZE 1024
  44. DEFINE_SPINLOCK(tipc_port_list_lock);
  45. static LIST_HEAD(ports);
  46. static void port_handle_node_down(unsigned long ref);
  47. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
  48. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
  49. static void port_timeout(unsigned long ref);
  50. /**
  51. * tipc_port_peer_msg - verify message was sent by connected port's peer
  52. *
  53. * Handles cases where the node's network address has changed from
  54. * the default of <0.0.0> to its configured setting.
  55. */
  56. int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
  57. {
  58. u32 peernode;
  59. u32 orignode;
  60. if (msg_origport(msg) != tipc_port_peerport(p_ptr))
  61. return 0;
  62. orignode = msg_orignode(msg);
  63. peernode = tipc_port_peernode(p_ptr);
  64. return (orignode == peernode) ||
  65. (!orignode && (peernode == tipc_own_addr)) ||
  66. (!peernode && (orignode == tipc_own_addr));
  67. }
  68. /* tipc_port_init - intiate TIPC port and lock it
  69. *
  70. * Returns obtained reference if initialization is successful, zero otherwise
  71. */
  72. u32 tipc_port_init(struct tipc_port *p_ptr,
  73. const unsigned int importance)
  74. {
  75. struct tipc_msg *msg;
  76. u32 ref;
  77. ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
  78. if (!ref) {
  79. pr_warn("Port registration failed, ref. table exhausted\n");
  80. return 0;
  81. }
  82. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  83. p_ptr->ref = ref;
  84. INIT_LIST_HEAD(&p_ptr->wait_list);
  85. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  86. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  87. INIT_LIST_HEAD(&p_ptr->publications);
  88. INIT_LIST_HEAD(&p_ptr->port_list);
  89. /*
  90. * Must hold port list lock while initializing message header template
  91. * to ensure a change to node's own network address doesn't result
  92. * in template containing out-dated network address information
  93. */
  94. spin_lock_bh(&tipc_port_list_lock);
  95. msg = &p_ptr->phdr;
  96. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  97. msg_set_origport(msg, ref);
  98. list_add_tail(&p_ptr->port_list, &ports);
  99. spin_unlock_bh(&tipc_port_list_lock);
  100. return ref;
  101. }
  102. void tipc_port_destroy(struct tipc_port *p_ptr)
  103. {
  104. struct sk_buff *buf = NULL;
  105. struct tipc_msg *msg = NULL;
  106. u32 peer;
  107. tipc_withdraw(p_ptr, 0, NULL);
  108. spin_lock_bh(p_ptr->lock);
  109. tipc_ref_discard(p_ptr->ref);
  110. spin_unlock_bh(p_ptr->lock);
  111. k_cancel_timer(&p_ptr->timer);
  112. if (p_ptr->connected) {
  113. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  114. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  115. msg = buf_msg(buf);
  116. peer = msg_destnode(msg);
  117. tipc_link_xmit(buf, peer, msg_link_selector(msg));
  118. }
  119. spin_lock_bh(&tipc_port_list_lock);
  120. list_del(&p_ptr->port_list);
  121. list_del(&p_ptr->wait_list);
  122. spin_unlock_bh(&tipc_port_list_lock);
  123. k_term_timer(&p_ptr->timer);
  124. }
  125. /*
  126. * port_build_proto_msg(): create connection protocol message for port
  127. *
  128. * On entry the port must be locked and connected.
  129. */
  130. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  131. u32 type, u32 ack)
  132. {
  133. struct sk_buff *buf;
  134. struct tipc_msg *msg;
  135. buf = tipc_buf_acquire(INT_H_SIZE);
  136. if (buf) {
  137. msg = buf_msg(buf);
  138. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  139. tipc_port_peernode(p_ptr));
  140. msg_set_destport(msg, tipc_port_peerport(p_ptr));
  141. msg_set_origport(msg, p_ptr->ref);
  142. msg_set_msgcnt(msg, ack);
  143. buf->next = NULL;
  144. }
  145. return buf;
  146. }
  147. static void port_timeout(unsigned long ref)
  148. {
  149. struct tipc_port *p_ptr = tipc_port_lock(ref);
  150. struct sk_buff *buf = NULL;
  151. struct tipc_msg *msg = NULL;
  152. if (!p_ptr)
  153. return;
  154. if (!p_ptr->connected) {
  155. tipc_port_unlock(p_ptr);
  156. return;
  157. }
  158. /* Last probe answered ? */
  159. if (p_ptr->probing_state == TIPC_CONN_PROBING) {
  160. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  161. } else {
  162. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  163. p_ptr->probing_state = TIPC_CONN_PROBING;
  164. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  165. }
  166. tipc_port_unlock(p_ptr);
  167. msg = buf_msg(buf);
  168. tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
  169. }
  170. static void port_handle_node_down(unsigned long ref)
  171. {
  172. struct tipc_port *p_ptr = tipc_port_lock(ref);
  173. struct sk_buff *buf = NULL;
  174. struct tipc_msg *msg = NULL;
  175. if (!p_ptr)
  176. return;
  177. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  178. tipc_port_unlock(p_ptr);
  179. msg = buf_msg(buf);
  180. tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
  181. }
  182. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  183. {
  184. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  185. if (buf) {
  186. struct tipc_msg *msg = buf_msg(buf);
  187. msg_swap_words(msg, 4, 5);
  188. msg_swap_words(msg, 6, 7);
  189. buf->next = NULL;
  190. }
  191. return buf;
  192. }
  193. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  194. {
  195. struct sk_buff *buf;
  196. struct tipc_msg *msg;
  197. u32 imp;
  198. if (!p_ptr->connected)
  199. return NULL;
  200. buf = tipc_buf_acquire(BASIC_H_SIZE);
  201. if (buf) {
  202. msg = buf_msg(buf);
  203. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  204. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  205. msg_set_size(msg, BASIC_H_SIZE);
  206. imp = msg_importance(msg);
  207. if (imp < TIPC_CRITICAL_IMPORTANCE)
  208. msg_set_importance(msg, ++imp);
  209. msg_set_errcode(msg, err);
  210. buf->next = NULL;
  211. }
  212. return buf;
  213. }
  214. static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
  215. {
  216. struct publication *publ;
  217. int ret;
  218. if (full_id)
  219. ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
  220. tipc_zone(tipc_own_addr),
  221. tipc_cluster(tipc_own_addr),
  222. tipc_node(tipc_own_addr), p_ptr->ref);
  223. else
  224. ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
  225. if (p_ptr->connected) {
  226. u32 dport = tipc_port_peerport(p_ptr);
  227. u32 destnode = tipc_port_peernode(p_ptr);
  228. ret += tipc_snprintf(buf + ret, len - ret,
  229. " connected to <%u.%u.%u:%u>",
  230. tipc_zone(destnode),
  231. tipc_cluster(destnode),
  232. tipc_node(destnode), dport);
  233. if (p_ptr->conn_type != 0)
  234. ret += tipc_snprintf(buf + ret, len - ret,
  235. " via {%u,%u}", p_ptr->conn_type,
  236. p_ptr->conn_instance);
  237. } else if (p_ptr->published) {
  238. ret += tipc_snprintf(buf + ret, len - ret, " bound to");
  239. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  240. if (publ->lower == publ->upper)
  241. ret += tipc_snprintf(buf + ret, len - ret,
  242. " {%u,%u}", publ->type,
  243. publ->lower);
  244. else
  245. ret += tipc_snprintf(buf + ret, len - ret,
  246. " {%u,%u,%u}", publ->type,
  247. publ->lower, publ->upper);
  248. }
  249. }
  250. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  251. return ret;
  252. }
  253. struct sk_buff *tipc_port_get_ports(void)
  254. {
  255. struct sk_buff *buf;
  256. struct tlv_desc *rep_tlv;
  257. char *pb;
  258. int pb_len;
  259. struct tipc_port *p_ptr;
  260. int str_len = 0;
  261. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  262. if (!buf)
  263. return NULL;
  264. rep_tlv = (struct tlv_desc *)buf->data;
  265. pb = TLV_DATA(rep_tlv);
  266. pb_len = ULTRA_STRING_MAX_LEN;
  267. spin_lock_bh(&tipc_port_list_lock);
  268. list_for_each_entry(p_ptr, &ports, port_list) {
  269. spin_lock_bh(p_ptr->lock);
  270. str_len += port_print(p_ptr, pb, pb_len, 0);
  271. spin_unlock_bh(p_ptr->lock);
  272. }
  273. spin_unlock_bh(&tipc_port_list_lock);
  274. str_len += 1; /* for "\0" */
  275. skb_put(buf, TLV_SPACE(str_len));
  276. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  277. return buf;
  278. }
  279. void tipc_port_reinit(void)
  280. {
  281. struct tipc_port *p_ptr;
  282. struct tipc_msg *msg;
  283. spin_lock_bh(&tipc_port_list_lock);
  284. list_for_each_entry(p_ptr, &ports, port_list) {
  285. msg = &p_ptr->phdr;
  286. msg_set_prevnode(msg, tipc_own_addr);
  287. msg_set_orignode(msg, tipc_own_addr);
  288. }
  289. spin_unlock_bh(&tipc_port_list_lock);
  290. }
  291. void tipc_acknowledge(u32 ref, u32 ack)
  292. {
  293. struct tipc_port *p_ptr;
  294. struct sk_buff *buf = NULL;
  295. struct tipc_msg *msg;
  296. p_ptr = tipc_port_lock(ref);
  297. if (!p_ptr)
  298. return;
  299. if (p_ptr->connected)
  300. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  301. tipc_port_unlock(p_ptr);
  302. if (!buf)
  303. return;
  304. msg = buf_msg(buf);
  305. tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
  306. }
  307. int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
  308. struct tipc_name_seq const *seq)
  309. {
  310. struct publication *publ;
  311. u32 key;
  312. if (p_ptr->connected)
  313. return -EINVAL;
  314. key = p_ptr->ref + p_ptr->pub_count + 1;
  315. if (key == p_ptr->ref)
  316. return -EADDRINUSE;
  317. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  318. scope, p_ptr->ref, key);
  319. if (publ) {
  320. list_add(&publ->pport_list, &p_ptr->publications);
  321. p_ptr->pub_count++;
  322. p_ptr->published = 1;
  323. return 0;
  324. }
  325. return -EINVAL;
  326. }
  327. int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
  328. struct tipc_name_seq const *seq)
  329. {
  330. struct publication *publ;
  331. struct publication *tpubl;
  332. int res = -EINVAL;
  333. if (!seq) {
  334. list_for_each_entry_safe(publ, tpubl,
  335. &p_ptr->publications, pport_list) {
  336. tipc_nametbl_withdraw(publ->type, publ->lower,
  337. publ->ref, publ->key);
  338. }
  339. res = 0;
  340. } else {
  341. list_for_each_entry_safe(publ, tpubl,
  342. &p_ptr->publications, pport_list) {
  343. if (publ->scope != scope)
  344. continue;
  345. if (publ->type != seq->type)
  346. continue;
  347. if (publ->lower != seq->lower)
  348. continue;
  349. if (publ->upper != seq->upper)
  350. break;
  351. tipc_nametbl_withdraw(publ->type, publ->lower,
  352. publ->ref, publ->key);
  353. res = 0;
  354. break;
  355. }
  356. }
  357. if (list_empty(&p_ptr->publications))
  358. p_ptr->published = 0;
  359. return res;
  360. }
  361. int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
  362. {
  363. struct tipc_port *p_ptr;
  364. int res;
  365. p_ptr = tipc_port_lock(ref);
  366. if (!p_ptr)
  367. return -EINVAL;
  368. res = __tipc_port_connect(ref, p_ptr, peer);
  369. tipc_port_unlock(p_ptr);
  370. return res;
  371. }
  372. /*
  373. * __tipc_port_connect - connect to a remote peer
  374. *
  375. * Port must be locked.
  376. */
  377. int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
  378. struct tipc_portid const *peer)
  379. {
  380. struct tipc_msg *msg;
  381. int res = -EINVAL;
  382. if (p_ptr->published || p_ptr->connected)
  383. goto exit;
  384. if (!peer->ref)
  385. goto exit;
  386. msg = &p_ptr->phdr;
  387. msg_set_destnode(msg, peer->node);
  388. msg_set_destport(msg, peer->ref);
  389. msg_set_type(msg, TIPC_CONN_MSG);
  390. msg_set_lookup_scope(msg, 0);
  391. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  392. p_ptr->probing_interval = PROBING_INTERVAL;
  393. p_ptr->probing_state = TIPC_CONN_OK;
  394. p_ptr->connected = 1;
  395. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  396. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  397. (void *)(unsigned long)ref,
  398. (net_ev_handler)port_handle_node_down);
  399. res = 0;
  400. exit:
  401. p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
  402. return res;
  403. }
  404. /*
  405. * __tipc_disconnect - disconnect port from peer
  406. *
  407. * Port must be locked.
  408. */
  409. int __tipc_port_disconnect(struct tipc_port *tp_ptr)
  410. {
  411. if (tp_ptr->connected) {
  412. tp_ptr->connected = 0;
  413. /* let timer expire on it's own to avoid deadlock! */
  414. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  415. return 0;
  416. }
  417. return -ENOTCONN;
  418. }
  419. /*
  420. * tipc_port_disconnect(): Disconnect port form peer.
  421. * This is a node local operation.
  422. */
  423. int tipc_port_disconnect(u32 ref)
  424. {
  425. struct tipc_port *p_ptr;
  426. int res;
  427. p_ptr = tipc_port_lock(ref);
  428. if (!p_ptr)
  429. return -EINVAL;
  430. res = __tipc_port_disconnect(p_ptr);
  431. tipc_port_unlock(p_ptr);
  432. return res;
  433. }
  434. /*
  435. * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  436. */
  437. int tipc_port_shutdown(u32 ref)
  438. {
  439. struct tipc_msg *msg;
  440. struct tipc_port *p_ptr;
  441. struct sk_buff *buf = NULL;
  442. p_ptr = tipc_port_lock(ref);
  443. if (!p_ptr)
  444. return -EINVAL;
  445. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  446. tipc_port_unlock(p_ptr);
  447. msg = buf_msg(buf);
  448. tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
  449. return tipc_port_disconnect(ref);
  450. }