port.c 26 KB

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