port.c 25 KB

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