port.c 26 KB

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