port.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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_port_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_port_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->ref = ref;
  192. INIT_LIST_HEAD(&p_ptr->wait_list);
  193. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  194. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  195. INIT_LIST_HEAD(&p_ptr->publications);
  196. INIT_LIST_HEAD(&p_ptr->port_list);
  197. /*
  198. * Must hold port list lock while initializing message header template
  199. * to ensure a change to node's own network address doesn't result
  200. * in template containing out-dated network address information
  201. */
  202. spin_lock_bh(&tipc_port_list_lock);
  203. msg = &p_ptr->phdr;
  204. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  205. msg_set_origport(msg, ref);
  206. list_add_tail(&p_ptr->port_list, &ports);
  207. spin_unlock_bh(&tipc_port_list_lock);
  208. return ref;
  209. }
  210. void tipc_port_destroy(struct tipc_port *p_ptr)
  211. {
  212. struct sk_buff *buf = NULL;
  213. tipc_withdraw(p_ptr, 0, NULL);
  214. spin_lock_bh(p_ptr->lock);
  215. tipc_ref_discard(p_ptr->ref);
  216. spin_unlock_bh(p_ptr->lock);
  217. k_cancel_timer(&p_ptr->timer);
  218. if (p_ptr->connected) {
  219. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  220. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  221. }
  222. spin_lock_bh(&tipc_port_list_lock);
  223. list_del(&p_ptr->port_list);
  224. list_del(&p_ptr->wait_list);
  225. spin_unlock_bh(&tipc_port_list_lock);
  226. k_term_timer(&p_ptr->timer);
  227. tipc_net_route_msg(buf);
  228. }
  229. /*
  230. * port_build_proto_msg(): create connection protocol message for port
  231. *
  232. * On entry the port must be locked and connected.
  233. */
  234. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  235. u32 type, u32 ack)
  236. {
  237. struct sk_buff *buf;
  238. struct tipc_msg *msg;
  239. buf = tipc_buf_acquire(INT_H_SIZE);
  240. if (buf) {
  241. msg = buf_msg(buf);
  242. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  243. tipc_port_peernode(p_ptr));
  244. msg_set_destport(msg, tipc_port_peerport(p_ptr));
  245. msg_set_origport(msg, p_ptr->ref);
  246. msg_set_msgcnt(msg, ack);
  247. }
  248. return buf;
  249. }
  250. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  251. {
  252. struct tipc_msg *msg = buf_msg(buf);
  253. struct sk_buff *rbuf;
  254. struct tipc_msg *rmsg;
  255. int hdr_sz;
  256. u32 imp;
  257. u32 data_sz = msg_data_sz(msg);
  258. u32 src_node;
  259. u32 rmsg_sz;
  260. /* discard rejected message if it shouldn't be returned to sender */
  261. if (WARN(!msg_isdata(msg),
  262. "attempt to reject message with user=%u", msg_user(msg))) {
  263. dump_stack();
  264. goto exit;
  265. }
  266. if (msg_errcode(msg) || msg_dest_droppable(msg))
  267. goto exit;
  268. /*
  269. * construct returned message by copying rejected message header and
  270. * data (or subset), then updating header fields that need adjusting
  271. */
  272. hdr_sz = msg_hdr_sz(msg);
  273. rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
  274. rbuf = tipc_buf_acquire(rmsg_sz);
  275. if (rbuf == NULL)
  276. goto exit;
  277. rmsg = buf_msg(rbuf);
  278. skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
  279. if (msg_connected(rmsg)) {
  280. imp = msg_importance(rmsg);
  281. if (imp < TIPC_CRITICAL_IMPORTANCE)
  282. msg_set_importance(rmsg, ++imp);
  283. }
  284. msg_set_non_seq(rmsg, 0);
  285. msg_set_size(rmsg, rmsg_sz);
  286. msg_set_errcode(rmsg, err);
  287. msg_set_prevnode(rmsg, tipc_own_addr);
  288. msg_swap_words(rmsg, 4, 5);
  289. if (!msg_short(rmsg))
  290. msg_swap_words(rmsg, 6, 7);
  291. /* send self-abort message when rejecting on a connected port */
  292. if (msg_connected(msg)) {
  293. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  294. if (p_ptr) {
  295. struct sk_buff *abuf = NULL;
  296. if (p_ptr->connected)
  297. abuf = port_build_self_abort_msg(p_ptr, err);
  298. tipc_port_unlock(p_ptr);
  299. tipc_net_route_msg(abuf);
  300. }
  301. }
  302. /* send returned message & dispose of rejected message */
  303. src_node = msg_prevnode(msg);
  304. if (in_own_node(src_node))
  305. tipc_port_rcv(rbuf);
  306. else
  307. tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
  308. exit:
  309. kfree_skb(buf);
  310. return data_sz;
  311. }
  312. int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
  313. struct iovec const *msg_sect, unsigned int len,
  314. int err)
  315. {
  316. struct sk_buff *buf;
  317. int res;
  318. res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  319. if (!buf)
  320. return res;
  321. return tipc_reject_msg(buf, err);
  322. }
  323. static void port_timeout(unsigned long ref)
  324. {
  325. struct tipc_port *p_ptr = tipc_port_lock(ref);
  326. struct sk_buff *buf = NULL;
  327. if (!p_ptr)
  328. return;
  329. if (!p_ptr->connected) {
  330. tipc_port_unlock(p_ptr);
  331. return;
  332. }
  333. /* Last probe answered ? */
  334. if (p_ptr->probing_state == PROBING) {
  335. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  336. } else {
  337. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  338. p_ptr->probing_state = PROBING;
  339. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  340. }
  341. tipc_port_unlock(p_ptr);
  342. tipc_net_route_msg(buf);
  343. }
  344. static void port_handle_node_down(unsigned long ref)
  345. {
  346. struct tipc_port *p_ptr = tipc_port_lock(ref);
  347. struct sk_buff *buf = NULL;
  348. if (!p_ptr)
  349. return;
  350. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  351. tipc_port_unlock(p_ptr);
  352. tipc_net_route_msg(buf);
  353. }
  354. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  355. {
  356. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  357. if (buf) {
  358. struct tipc_msg *msg = buf_msg(buf);
  359. msg_swap_words(msg, 4, 5);
  360. msg_swap_words(msg, 6, 7);
  361. }
  362. return buf;
  363. }
  364. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  365. {
  366. struct sk_buff *buf;
  367. struct tipc_msg *msg;
  368. u32 imp;
  369. if (!p_ptr->connected)
  370. return NULL;
  371. buf = tipc_buf_acquire(BASIC_H_SIZE);
  372. if (buf) {
  373. msg = buf_msg(buf);
  374. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  375. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  376. msg_set_size(msg, BASIC_H_SIZE);
  377. imp = msg_importance(msg);
  378. if (imp < TIPC_CRITICAL_IMPORTANCE)
  379. msg_set_importance(msg, ++imp);
  380. msg_set_errcode(msg, err);
  381. }
  382. return buf;
  383. }
  384. void tipc_port_proto_rcv(struct sk_buff *buf)
  385. {
  386. struct tipc_msg *msg = buf_msg(buf);
  387. struct tipc_port *p_ptr;
  388. struct sk_buff *r_buf = NULL;
  389. u32 destport = msg_destport(msg);
  390. int wakeable;
  391. /* Validate connection */
  392. p_ptr = tipc_port_lock(destport);
  393. if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
  394. r_buf = tipc_buf_acquire(BASIC_H_SIZE);
  395. if (r_buf) {
  396. msg = buf_msg(r_buf);
  397. tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
  398. BASIC_H_SIZE, msg_orignode(msg));
  399. msg_set_errcode(msg, TIPC_ERR_NO_PORT);
  400. msg_set_origport(msg, destport);
  401. msg_set_destport(msg, msg_origport(msg));
  402. }
  403. if (p_ptr)
  404. tipc_port_unlock(p_ptr);
  405. goto exit;
  406. }
  407. /* Process protocol message sent by peer */
  408. switch (msg_type(msg)) {
  409. case CONN_ACK:
  410. wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
  411. p_ptr->acked += msg_msgcnt(msg);
  412. if (!tipc_port_congested(p_ptr)) {
  413. p_ptr->congested = 0;
  414. if (wakeable)
  415. tipc_port_wakeup(p_ptr);
  416. }
  417. break;
  418. case CONN_PROBE:
  419. r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
  420. break;
  421. default:
  422. /* CONN_PROBE_REPLY or unrecognized - no action required */
  423. break;
  424. }
  425. p_ptr->probing_state = CONFIRMED;
  426. tipc_port_unlock(p_ptr);
  427. exit:
  428. tipc_net_route_msg(r_buf);
  429. kfree_skb(buf);
  430. }
  431. static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
  432. {
  433. struct publication *publ;
  434. int ret;
  435. if (full_id)
  436. ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
  437. tipc_zone(tipc_own_addr),
  438. tipc_cluster(tipc_own_addr),
  439. tipc_node(tipc_own_addr), p_ptr->ref);
  440. else
  441. ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
  442. if (p_ptr->connected) {
  443. u32 dport = tipc_port_peerport(p_ptr);
  444. u32 destnode = tipc_port_peernode(p_ptr);
  445. ret += tipc_snprintf(buf + ret, len - ret,
  446. " connected to <%u.%u.%u:%u>",
  447. tipc_zone(destnode),
  448. tipc_cluster(destnode),
  449. tipc_node(destnode), dport);
  450. if (p_ptr->conn_type != 0)
  451. ret += tipc_snprintf(buf + ret, len - ret,
  452. " via {%u,%u}", p_ptr->conn_type,
  453. p_ptr->conn_instance);
  454. } else if (p_ptr->published) {
  455. ret += tipc_snprintf(buf + ret, len - ret, " bound to");
  456. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  457. if (publ->lower == publ->upper)
  458. ret += tipc_snprintf(buf + ret, len - ret,
  459. " {%u,%u}", publ->type,
  460. publ->lower);
  461. else
  462. ret += tipc_snprintf(buf + ret, len - ret,
  463. " {%u,%u,%u}", publ->type,
  464. publ->lower, publ->upper);
  465. }
  466. }
  467. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  468. return ret;
  469. }
  470. struct sk_buff *tipc_port_get_ports(void)
  471. {
  472. struct sk_buff *buf;
  473. struct tlv_desc *rep_tlv;
  474. char *pb;
  475. int pb_len;
  476. struct tipc_port *p_ptr;
  477. int str_len = 0;
  478. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  479. if (!buf)
  480. return NULL;
  481. rep_tlv = (struct tlv_desc *)buf->data;
  482. pb = TLV_DATA(rep_tlv);
  483. pb_len = ULTRA_STRING_MAX_LEN;
  484. spin_lock_bh(&tipc_port_list_lock);
  485. list_for_each_entry(p_ptr, &ports, port_list) {
  486. spin_lock_bh(p_ptr->lock);
  487. str_len += port_print(p_ptr, pb, pb_len, 0);
  488. spin_unlock_bh(p_ptr->lock);
  489. }
  490. spin_unlock_bh(&tipc_port_list_lock);
  491. str_len += 1; /* for "\0" */
  492. skb_put(buf, TLV_SPACE(str_len));
  493. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  494. return buf;
  495. }
  496. void tipc_port_reinit(void)
  497. {
  498. struct tipc_port *p_ptr;
  499. struct tipc_msg *msg;
  500. spin_lock_bh(&tipc_port_list_lock);
  501. list_for_each_entry(p_ptr, &ports, port_list) {
  502. msg = &p_ptr->phdr;
  503. msg_set_prevnode(msg, tipc_own_addr);
  504. msg_set_orignode(msg, tipc_own_addr);
  505. }
  506. spin_unlock_bh(&tipc_port_list_lock);
  507. }
  508. void tipc_acknowledge(u32 ref, u32 ack)
  509. {
  510. struct tipc_port *p_ptr;
  511. struct sk_buff *buf = NULL;
  512. p_ptr = tipc_port_lock(ref);
  513. if (!p_ptr)
  514. return;
  515. if (p_ptr->connected) {
  516. p_ptr->conn_unacked -= ack;
  517. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  518. }
  519. tipc_port_unlock(p_ptr);
  520. tipc_net_route_msg(buf);
  521. }
  522. int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
  523. struct tipc_name_seq const *seq)
  524. {
  525. struct publication *publ;
  526. u32 key;
  527. if (p_ptr->connected)
  528. return -EINVAL;
  529. key = p_ptr->ref + p_ptr->pub_count + 1;
  530. if (key == p_ptr->ref)
  531. return -EADDRINUSE;
  532. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  533. scope, p_ptr->ref, key);
  534. if (publ) {
  535. list_add(&publ->pport_list, &p_ptr->publications);
  536. p_ptr->pub_count++;
  537. p_ptr->published = 1;
  538. return 0;
  539. }
  540. return -EINVAL;
  541. }
  542. int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
  543. struct tipc_name_seq const *seq)
  544. {
  545. struct publication *publ;
  546. struct publication *tpubl;
  547. int res = -EINVAL;
  548. if (!seq) {
  549. list_for_each_entry_safe(publ, tpubl,
  550. &p_ptr->publications, pport_list) {
  551. tipc_nametbl_withdraw(publ->type, publ->lower,
  552. publ->ref, publ->key);
  553. }
  554. res = 0;
  555. } else {
  556. list_for_each_entry_safe(publ, tpubl,
  557. &p_ptr->publications, pport_list) {
  558. if (publ->scope != scope)
  559. continue;
  560. if (publ->type != seq->type)
  561. continue;
  562. if (publ->lower != seq->lower)
  563. continue;
  564. if (publ->upper != seq->upper)
  565. break;
  566. tipc_nametbl_withdraw(publ->type, publ->lower,
  567. publ->ref, publ->key);
  568. res = 0;
  569. break;
  570. }
  571. }
  572. if (list_empty(&p_ptr->publications))
  573. p_ptr->published = 0;
  574. return res;
  575. }
  576. int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
  577. {
  578. struct tipc_port *p_ptr;
  579. int res;
  580. p_ptr = tipc_port_lock(ref);
  581. if (!p_ptr)
  582. return -EINVAL;
  583. res = __tipc_port_connect(ref, p_ptr, peer);
  584. tipc_port_unlock(p_ptr);
  585. return res;
  586. }
  587. /*
  588. * __tipc_port_connect - connect to a remote peer
  589. *
  590. * Port must be locked.
  591. */
  592. int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
  593. struct tipc_portid const *peer)
  594. {
  595. struct tipc_msg *msg;
  596. int res = -EINVAL;
  597. if (p_ptr->published || p_ptr->connected)
  598. goto exit;
  599. if (!peer->ref)
  600. goto exit;
  601. msg = &p_ptr->phdr;
  602. msg_set_destnode(msg, peer->node);
  603. msg_set_destport(msg, peer->ref);
  604. msg_set_type(msg, TIPC_CONN_MSG);
  605. msg_set_lookup_scope(msg, 0);
  606. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  607. p_ptr->probing_interval = PROBING_INTERVAL;
  608. p_ptr->probing_state = CONFIRMED;
  609. p_ptr->connected = 1;
  610. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  611. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  612. (void *)(unsigned long)ref,
  613. (net_ev_handler)port_handle_node_down);
  614. res = 0;
  615. exit:
  616. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  617. return res;
  618. }
  619. /*
  620. * __tipc_disconnect - disconnect port from peer
  621. *
  622. * Port must be locked.
  623. */
  624. int __tipc_port_disconnect(struct tipc_port *tp_ptr)
  625. {
  626. if (tp_ptr->connected) {
  627. tp_ptr->connected = 0;
  628. /* let timer expire on it's own to avoid deadlock! */
  629. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  630. return 0;
  631. }
  632. return -ENOTCONN;
  633. }
  634. /*
  635. * tipc_port_disconnect(): Disconnect port form peer.
  636. * This is a node local operation.
  637. */
  638. int tipc_port_disconnect(u32 ref)
  639. {
  640. struct tipc_port *p_ptr;
  641. int res;
  642. p_ptr = tipc_port_lock(ref);
  643. if (!p_ptr)
  644. return -EINVAL;
  645. res = __tipc_port_disconnect(p_ptr);
  646. tipc_port_unlock(p_ptr);
  647. return res;
  648. }
  649. /*
  650. * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  651. */
  652. int tipc_port_shutdown(u32 ref)
  653. {
  654. struct tipc_port *p_ptr;
  655. struct sk_buff *buf = NULL;
  656. p_ptr = tipc_port_lock(ref);
  657. if (!p_ptr)
  658. return -EINVAL;
  659. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  660. tipc_port_unlock(p_ptr);
  661. tipc_net_route_msg(buf);
  662. return tipc_port_disconnect(ref);
  663. }
  664. /**
  665. * tipc_port_rcv - receive message from lower layer and deliver to port user
  666. */
  667. int tipc_port_rcv(struct sk_buff *buf)
  668. {
  669. struct tipc_port *p_ptr;
  670. struct tipc_msg *msg = buf_msg(buf);
  671. u32 destport = msg_destport(msg);
  672. u32 dsz = msg_data_sz(msg);
  673. u32 err;
  674. /* forward unresolved named message */
  675. if (unlikely(!destport)) {
  676. tipc_net_route_msg(buf);
  677. return dsz;
  678. }
  679. /* validate destination & pass to port, otherwise reject message */
  680. p_ptr = tipc_port_lock(destport);
  681. if (likely(p_ptr)) {
  682. err = tipc_sk_rcv(&tipc_port_to_sock(p_ptr)->sk, buf);
  683. tipc_port_unlock(p_ptr);
  684. if (likely(!err))
  685. return dsz;
  686. } else {
  687. err = TIPC_ERR_NO_PORT;
  688. }
  689. return tipc_reject_msg(buf, err);
  690. }
  691. /*
  692. * tipc_port_iovec_rcv: Concatenate and deliver sectioned
  693. * message for this node.
  694. */
  695. static int tipc_port_iovec_rcv(struct tipc_port *sender,
  696. struct iovec const *msg_sect,
  697. unsigned int len)
  698. {
  699. struct sk_buff *buf;
  700. int res;
  701. res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
  702. if (likely(buf))
  703. tipc_port_rcv(buf);
  704. return res;
  705. }
  706. /**
  707. * tipc_send - send message sections on connection
  708. */
  709. int tipc_send(struct tipc_port *p_ptr,
  710. struct iovec const *msg_sect,
  711. unsigned int len)
  712. {
  713. u32 destnode;
  714. int res;
  715. if (!p_ptr->connected)
  716. return -EINVAL;
  717. p_ptr->congested = 1;
  718. if (!tipc_port_congested(p_ptr)) {
  719. destnode = tipc_port_peernode(p_ptr);
  720. if (likely(!in_own_node(destnode)))
  721. res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
  722. destnode);
  723. else
  724. res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
  725. if (likely(res != -ELINKCONG)) {
  726. p_ptr->congested = 0;
  727. if (res > 0)
  728. p_ptr->sent++;
  729. return res;
  730. }
  731. }
  732. if (tipc_port_unreliable(p_ptr)) {
  733. p_ptr->congested = 0;
  734. return len;
  735. }
  736. return -ELINKCONG;
  737. }
  738. /**
  739. * tipc_send2name - send message sections to port name
  740. */
  741. int tipc_send2name(struct tipc_port *p_ptr,
  742. struct tipc_name const *name,
  743. unsigned int domain,
  744. struct iovec const *msg_sect,
  745. unsigned int len)
  746. {
  747. struct tipc_msg *msg;
  748. u32 destnode = domain;
  749. u32 destport;
  750. int res;
  751. if (p_ptr->connected)
  752. return -EINVAL;
  753. msg = &p_ptr->phdr;
  754. msg_set_type(msg, TIPC_NAMED_MSG);
  755. msg_set_hdr_sz(msg, NAMED_H_SIZE);
  756. msg_set_nametype(msg, name->type);
  757. msg_set_nameinst(msg, name->instance);
  758. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  759. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  760. msg_set_destnode(msg, destnode);
  761. msg_set_destport(msg, destport);
  762. if (likely(destport || destnode)) {
  763. if (likely(in_own_node(destnode)))
  764. res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
  765. else if (tipc_own_addr)
  766. res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
  767. destnode);
  768. else
  769. res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
  770. len, TIPC_ERR_NO_NODE);
  771. if (likely(res != -ELINKCONG)) {
  772. if (res > 0)
  773. p_ptr->sent++;
  774. return res;
  775. }
  776. if (tipc_port_unreliable(p_ptr))
  777. return len;
  778. return -ELINKCONG;
  779. }
  780. return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
  781. TIPC_ERR_NO_NAME);
  782. }
  783. /**
  784. * tipc_send2port - send message sections to port identity
  785. */
  786. int tipc_send2port(struct tipc_port *p_ptr,
  787. struct tipc_portid const *dest,
  788. struct iovec const *msg_sect,
  789. unsigned int len)
  790. {
  791. struct tipc_msg *msg;
  792. int res;
  793. if (p_ptr->connected)
  794. return -EINVAL;
  795. msg = &p_ptr->phdr;
  796. msg_set_type(msg, TIPC_DIRECT_MSG);
  797. msg_set_lookup_scope(msg, 0);
  798. msg_set_destnode(msg, dest->node);
  799. msg_set_destport(msg, dest->ref);
  800. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  801. if (in_own_node(dest->node))
  802. res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
  803. else if (tipc_own_addr)
  804. res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
  805. dest->node);
  806. else
  807. res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
  808. TIPC_ERR_NO_NODE);
  809. if (likely(res != -ELINKCONG)) {
  810. if (res > 0)
  811. p_ptr->sent++;
  812. return res;
  813. }
  814. if (tipc_port_unreliable(p_ptr))
  815. return len;
  816. return -ELINKCONG;
  817. }