port.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2011, 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. static struct sk_buff *msg_queue_head;
  46. static struct sk_buff *msg_queue_tail;
  47. DEFINE_SPINLOCK(tipc_port_list_lock);
  48. static DEFINE_SPINLOCK(queue_lock);
  49. static LIST_HEAD(ports);
  50. static void port_handle_node_down(unsigned long ref);
  51. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
  52. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
  53. static void port_timeout(unsigned long ref);
  54. static u32 port_peernode(struct tipc_port *p_ptr)
  55. {
  56. return msg_destnode(&p_ptr->phdr);
  57. }
  58. static u32 port_peerport(struct tipc_port *p_ptr)
  59. {
  60. return msg_destport(&p_ptr->phdr);
  61. }
  62. /**
  63. * tipc_port_peer_msg - verify message was sent by connected port's peer
  64. *
  65. * Handles cases where the node's network address has changed from
  66. * the default of <0.0.0> to its configured setting.
  67. */
  68. int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
  69. {
  70. u32 peernode;
  71. u32 orignode;
  72. if (msg_origport(msg) != port_peerport(p_ptr))
  73. return 0;
  74. orignode = msg_orignode(msg);
  75. peernode = port_peernode(p_ptr);
  76. return (orignode == peernode) ||
  77. (!orignode && (peernode == tipc_own_addr)) ||
  78. (!peernode && (orignode == tipc_own_addr));
  79. }
  80. /**
  81. * tipc_multicast - send a multicast message to local and remote destinations
  82. */
  83. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
  84. u32 num_sect, struct iovec const *msg_sect,
  85. unsigned int total_len)
  86. {
  87. struct tipc_msg *hdr;
  88. struct sk_buff *buf;
  89. struct sk_buff *ibuf = NULL;
  90. struct tipc_port_list dports = {0, NULL, };
  91. struct tipc_port *oport = tipc_port_deref(ref);
  92. int ext_targets;
  93. int res;
  94. if (unlikely(!oport))
  95. return -EINVAL;
  96. /* Create multicast message */
  97. hdr = &oport->phdr;
  98. msg_set_type(hdr, TIPC_MCAST_MSG);
  99. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  100. msg_set_destport(hdr, 0);
  101. msg_set_destnode(hdr, 0);
  102. msg_set_nametype(hdr, seq->type);
  103. msg_set_namelower(hdr, seq->lower);
  104. msg_set_nameupper(hdr, seq->upper);
  105. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  106. res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
  107. !oport->user_port, &buf);
  108. if (unlikely(!buf))
  109. return res;
  110. /* Figure out where to send multicast message */
  111. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  112. TIPC_NODE_SCOPE, &dports);
  113. /* Send message to destinations (duplicate it only if necessary) */
  114. if (ext_targets) {
  115. if (dports.count != 0) {
  116. ibuf = skb_copy(buf, GFP_ATOMIC);
  117. if (ibuf == NULL) {
  118. tipc_port_list_free(&dports);
  119. kfree_skb(buf);
  120. return -ENOMEM;
  121. }
  122. }
  123. res = tipc_bclink_send_msg(buf);
  124. if ((res < 0) && (dports.count != 0))
  125. kfree_skb(ibuf);
  126. } else {
  127. ibuf = buf;
  128. }
  129. if (res >= 0) {
  130. if (ibuf)
  131. tipc_port_recv_mcast(ibuf, &dports);
  132. } else {
  133. tipc_port_list_free(&dports);
  134. }
  135. return res;
  136. }
  137. /**
  138. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  139. *
  140. * If there is no port list, perform a lookup to create one
  141. */
  142. void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
  143. {
  144. struct tipc_msg *msg;
  145. struct tipc_port_list dports = {0, NULL, };
  146. struct tipc_port_list *item = dp;
  147. int cnt = 0;
  148. msg = buf_msg(buf);
  149. /* Create destination port list, if one wasn't supplied */
  150. if (dp == NULL) {
  151. tipc_nametbl_mc_translate(msg_nametype(msg),
  152. msg_namelower(msg),
  153. msg_nameupper(msg),
  154. TIPC_CLUSTER_SCOPE,
  155. &dports);
  156. item = dp = &dports;
  157. }
  158. /* Deliver a copy of message to each destination port */
  159. if (dp->count != 0) {
  160. msg_set_destnode(msg, tipc_own_addr);
  161. if (dp->count == 1) {
  162. msg_set_destport(msg, dp->ports[0]);
  163. tipc_port_recv_msg(buf);
  164. tipc_port_list_free(dp);
  165. return;
  166. }
  167. for (; cnt < dp->count; cnt++) {
  168. int index = cnt % PLSIZE;
  169. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  170. if (b == NULL) {
  171. pr_warn("Unable to deliver multicast message(s)\n");
  172. goto exit;
  173. }
  174. if ((index == 0) && (cnt != 0))
  175. item = item->next;
  176. msg_set_destport(buf_msg(b), item->ports[index]);
  177. tipc_port_recv_msg(b);
  178. }
  179. }
  180. exit:
  181. kfree_skb(buf);
  182. tipc_port_list_free(dp);
  183. }
  184. /**
  185. * tipc_createport_raw - create a generic TIPC port
  186. *
  187. * Returns pointer to (locked) TIPC port, or NULL if unable to create it
  188. */
  189. struct tipc_port *tipc_createport_raw(void *usr_handle,
  190. u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
  191. void (*wakeup)(struct tipc_port *),
  192. const u32 importance)
  193. {
  194. struct tipc_port *p_ptr;
  195. struct tipc_msg *msg;
  196. u32 ref;
  197. p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
  198. if (!p_ptr) {
  199. pr_warn("Port creation failed, no memory\n");
  200. return NULL;
  201. }
  202. ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
  203. if (!ref) {
  204. pr_warn("Port creation failed, ref. table exhausted\n");
  205. kfree(p_ptr);
  206. return NULL;
  207. }
  208. p_ptr->usr_handle = usr_handle;
  209. p_ptr->max_pkt = MAX_PKT_DEFAULT;
  210. p_ptr->ref = ref;
  211. INIT_LIST_HEAD(&p_ptr->wait_list);
  212. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  213. p_ptr->dispatcher = dispatcher;
  214. p_ptr->wakeup = wakeup;
  215. p_ptr->user_port = NULL;
  216. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  217. INIT_LIST_HEAD(&p_ptr->publications);
  218. INIT_LIST_HEAD(&p_ptr->port_list);
  219. /*
  220. * Must hold port list lock while initializing message header template
  221. * to ensure a change to node's own network address doesn't result
  222. * in template containing out-dated network address information
  223. */
  224. spin_lock_bh(&tipc_port_list_lock);
  225. msg = &p_ptr->phdr;
  226. tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  227. msg_set_origport(msg, ref);
  228. list_add_tail(&p_ptr->port_list, &ports);
  229. spin_unlock_bh(&tipc_port_list_lock);
  230. return p_ptr;
  231. }
  232. int tipc_deleteport(u32 ref)
  233. {
  234. struct tipc_port *p_ptr;
  235. struct sk_buff *buf = NULL;
  236. tipc_withdraw(ref, 0, NULL);
  237. p_ptr = tipc_port_lock(ref);
  238. if (!p_ptr)
  239. return -EINVAL;
  240. tipc_ref_discard(ref);
  241. tipc_port_unlock(p_ptr);
  242. k_cancel_timer(&p_ptr->timer);
  243. if (p_ptr->connected) {
  244. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  245. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  246. }
  247. kfree(p_ptr->user_port);
  248. spin_lock_bh(&tipc_port_list_lock);
  249. list_del(&p_ptr->port_list);
  250. list_del(&p_ptr->wait_list);
  251. spin_unlock_bh(&tipc_port_list_lock);
  252. k_term_timer(&p_ptr->timer);
  253. kfree(p_ptr);
  254. tipc_net_route_msg(buf);
  255. return 0;
  256. }
  257. static int port_unreliable(struct tipc_port *p_ptr)
  258. {
  259. return msg_src_droppable(&p_ptr->phdr);
  260. }
  261. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  262. {
  263. struct tipc_port *p_ptr;
  264. p_ptr = tipc_port_lock(ref);
  265. if (!p_ptr)
  266. return -EINVAL;
  267. *isunreliable = port_unreliable(p_ptr);
  268. tipc_port_unlock(p_ptr);
  269. return 0;
  270. }
  271. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  272. {
  273. struct tipc_port *p_ptr;
  274. p_ptr = tipc_port_lock(ref);
  275. if (!p_ptr)
  276. return -EINVAL;
  277. msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
  278. tipc_port_unlock(p_ptr);
  279. return 0;
  280. }
  281. static int port_unreturnable(struct tipc_port *p_ptr)
  282. {
  283. return msg_dest_droppable(&p_ptr->phdr);
  284. }
  285. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  286. {
  287. struct tipc_port *p_ptr;
  288. p_ptr = tipc_port_lock(ref);
  289. if (!p_ptr)
  290. return -EINVAL;
  291. *isunrejectable = port_unreturnable(p_ptr);
  292. tipc_port_unlock(p_ptr);
  293. return 0;
  294. }
  295. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  296. {
  297. struct tipc_port *p_ptr;
  298. p_ptr = tipc_port_lock(ref);
  299. if (!p_ptr)
  300. return -EINVAL;
  301. msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
  302. tipc_port_unlock(p_ptr);
  303. return 0;
  304. }
  305. /*
  306. * port_build_proto_msg(): create connection protocol message for port
  307. *
  308. * On entry the port must be locked and connected.
  309. */
  310. static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
  311. u32 type, u32 ack)
  312. {
  313. struct sk_buff *buf;
  314. struct tipc_msg *msg;
  315. buf = tipc_buf_acquire(INT_H_SIZE);
  316. if (buf) {
  317. msg = buf_msg(buf);
  318. tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
  319. port_peernode(p_ptr));
  320. msg_set_destport(msg, port_peerport(p_ptr));
  321. msg_set_origport(msg, p_ptr->ref);
  322. msg_set_msgcnt(msg, ack);
  323. }
  324. return buf;
  325. }
  326. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  327. {
  328. struct tipc_msg *msg = buf_msg(buf);
  329. struct sk_buff *rbuf;
  330. struct tipc_msg *rmsg;
  331. int hdr_sz;
  332. u32 imp;
  333. u32 data_sz = msg_data_sz(msg);
  334. u32 src_node;
  335. u32 rmsg_sz;
  336. /* discard rejected message if it shouldn't be returned to sender */
  337. if (WARN(!msg_isdata(msg),
  338. "attempt to reject message with user=%u", msg_user(msg))) {
  339. dump_stack();
  340. goto exit;
  341. }
  342. if (msg_errcode(msg) || msg_dest_droppable(msg))
  343. goto exit;
  344. /*
  345. * construct returned message by copying rejected message header and
  346. * data (or subset), then updating header fields that need adjusting
  347. */
  348. hdr_sz = msg_hdr_sz(msg);
  349. rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
  350. rbuf = tipc_buf_acquire(rmsg_sz);
  351. if (rbuf == NULL)
  352. goto exit;
  353. rmsg = buf_msg(rbuf);
  354. skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
  355. if (msg_connected(rmsg)) {
  356. imp = msg_importance(rmsg);
  357. if (imp < TIPC_CRITICAL_IMPORTANCE)
  358. msg_set_importance(rmsg, ++imp);
  359. }
  360. msg_set_non_seq(rmsg, 0);
  361. msg_set_size(rmsg, rmsg_sz);
  362. msg_set_errcode(rmsg, err);
  363. msg_set_prevnode(rmsg, tipc_own_addr);
  364. msg_swap_words(rmsg, 4, 5);
  365. if (!msg_short(rmsg))
  366. msg_swap_words(rmsg, 6, 7);
  367. /* send self-abort message when rejecting on a connected port */
  368. if (msg_connected(msg)) {
  369. struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
  370. if (p_ptr) {
  371. struct sk_buff *abuf = NULL;
  372. if (p_ptr->connected)
  373. abuf = port_build_self_abort_msg(p_ptr, err);
  374. tipc_port_unlock(p_ptr);
  375. tipc_net_route_msg(abuf);
  376. }
  377. }
  378. /* send returned message & dispose of rejected message */
  379. src_node = msg_prevnode(msg);
  380. if (in_own_node(src_node))
  381. tipc_port_recv_msg(rbuf);
  382. else
  383. tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
  384. exit:
  385. kfree_skb(buf);
  386. return data_sz;
  387. }
  388. int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
  389. struct iovec const *msg_sect, u32 num_sect,
  390. unsigned int total_len, int err)
  391. {
  392. struct sk_buff *buf;
  393. int res;
  394. res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
  395. !p_ptr->user_port, &buf);
  396. if (!buf)
  397. return res;
  398. return tipc_reject_msg(buf, err);
  399. }
  400. static void port_timeout(unsigned long ref)
  401. {
  402. struct tipc_port *p_ptr = tipc_port_lock(ref);
  403. struct sk_buff *buf = NULL;
  404. if (!p_ptr)
  405. return;
  406. if (!p_ptr->connected) {
  407. tipc_port_unlock(p_ptr);
  408. return;
  409. }
  410. /* Last probe answered ? */
  411. if (p_ptr->probing_state == PROBING) {
  412. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  413. } else {
  414. buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
  415. p_ptr->probing_state = PROBING;
  416. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  417. }
  418. tipc_port_unlock(p_ptr);
  419. tipc_net_route_msg(buf);
  420. }
  421. static void port_handle_node_down(unsigned long ref)
  422. {
  423. struct tipc_port *p_ptr = tipc_port_lock(ref);
  424. struct sk_buff *buf = NULL;
  425. if (!p_ptr)
  426. return;
  427. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  428. tipc_port_unlock(p_ptr);
  429. tipc_net_route_msg(buf);
  430. }
  431. static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
  432. {
  433. struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
  434. if (buf) {
  435. struct tipc_msg *msg = buf_msg(buf);
  436. msg_swap_words(msg, 4, 5);
  437. msg_swap_words(msg, 6, 7);
  438. }
  439. return buf;
  440. }
  441. static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
  442. {
  443. struct sk_buff *buf;
  444. struct tipc_msg *msg;
  445. u32 imp;
  446. if (!p_ptr->connected)
  447. return NULL;
  448. buf = tipc_buf_acquire(BASIC_H_SIZE);
  449. if (buf) {
  450. msg = buf_msg(buf);
  451. memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
  452. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  453. msg_set_size(msg, BASIC_H_SIZE);
  454. imp = msg_importance(msg);
  455. if (imp < TIPC_CRITICAL_IMPORTANCE)
  456. msg_set_importance(msg, ++imp);
  457. msg_set_errcode(msg, err);
  458. }
  459. return buf;
  460. }
  461. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  462. {
  463. struct tipc_msg *msg = buf_msg(buf);
  464. struct tipc_port *p_ptr;
  465. struct sk_buff *r_buf = NULL;
  466. u32 destport = msg_destport(msg);
  467. int wakeable;
  468. /* Validate connection */
  469. p_ptr = tipc_port_lock(destport);
  470. if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
  471. r_buf = tipc_buf_acquire(BASIC_H_SIZE);
  472. if (r_buf) {
  473. msg = buf_msg(r_buf);
  474. tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
  475. BASIC_H_SIZE, msg_orignode(msg));
  476. msg_set_errcode(msg, TIPC_ERR_NO_PORT);
  477. msg_set_origport(msg, destport);
  478. msg_set_destport(msg, msg_origport(msg));
  479. }
  480. if (p_ptr)
  481. tipc_port_unlock(p_ptr);
  482. goto exit;
  483. }
  484. /* Process protocol message sent by peer */
  485. switch (msg_type(msg)) {
  486. case CONN_ACK:
  487. wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
  488. p_ptr->wakeup;
  489. p_ptr->acked += msg_msgcnt(msg);
  490. if (!tipc_port_congested(p_ptr)) {
  491. p_ptr->congested = 0;
  492. if (wakeable)
  493. p_ptr->wakeup(p_ptr);
  494. }
  495. break;
  496. case CONN_PROBE:
  497. r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
  498. break;
  499. default:
  500. /* CONN_PROBE_REPLY or unrecognized - no action required */
  501. break;
  502. }
  503. p_ptr->probing_state = CONFIRMED;
  504. tipc_port_unlock(p_ptr);
  505. exit:
  506. tipc_net_route_msg(r_buf);
  507. kfree_skb(buf);
  508. }
  509. static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
  510. {
  511. struct publication *publ;
  512. int ret;
  513. if (full_id)
  514. ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
  515. tipc_zone(tipc_own_addr),
  516. tipc_cluster(tipc_own_addr),
  517. tipc_node(tipc_own_addr), p_ptr->ref);
  518. else
  519. ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
  520. if (p_ptr->connected) {
  521. u32 dport = port_peerport(p_ptr);
  522. u32 destnode = port_peernode(p_ptr);
  523. ret += tipc_snprintf(buf + ret, len - ret,
  524. " connected to <%u.%u.%u:%u>",
  525. tipc_zone(destnode),
  526. tipc_cluster(destnode),
  527. tipc_node(destnode), dport);
  528. if (p_ptr->conn_type != 0)
  529. ret += tipc_snprintf(buf + ret, len - ret,
  530. " via {%u,%u}", p_ptr->conn_type,
  531. p_ptr->conn_instance);
  532. } else if (p_ptr->published) {
  533. ret += tipc_snprintf(buf + ret, len - ret, " bound to");
  534. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  535. if (publ->lower == publ->upper)
  536. ret += tipc_snprintf(buf + ret, len - ret,
  537. " {%u,%u}", publ->type,
  538. publ->lower);
  539. else
  540. ret += tipc_snprintf(buf + ret, len - ret,
  541. " {%u,%u,%u}", publ->type,
  542. publ->lower, publ->upper);
  543. }
  544. }
  545. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  546. return ret;
  547. }
  548. struct sk_buff *tipc_port_get_ports(void)
  549. {
  550. struct sk_buff *buf;
  551. struct tlv_desc *rep_tlv;
  552. char *pb;
  553. int pb_len;
  554. struct tipc_port *p_ptr;
  555. int str_len = 0;
  556. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  557. if (!buf)
  558. return NULL;
  559. rep_tlv = (struct tlv_desc *)buf->data;
  560. pb = TLV_DATA(rep_tlv);
  561. pb_len = ULTRA_STRING_MAX_LEN;
  562. spin_lock_bh(&tipc_port_list_lock);
  563. list_for_each_entry(p_ptr, &ports, port_list) {
  564. spin_lock_bh(p_ptr->lock);
  565. str_len += port_print(p_ptr, pb, pb_len, 0);
  566. spin_unlock_bh(p_ptr->lock);
  567. }
  568. spin_unlock_bh(&tipc_port_list_lock);
  569. str_len += 1; /* for "\0" */
  570. skb_put(buf, TLV_SPACE(str_len));
  571. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  572. return buf;
  573. }
  574. void tipc_port_reinit(void)
  575. {
  576. struct tipc_port *p_ptr;
  577. struct tipc_msg *msg;
  578. spin_lock_bh(&tipc_port_list_lock);
  579. list_for_each_entry(p_ptr, &ports, port_list) {
  580. msg = &p_ptr->phdr;
  581. msg_set_prevnode(msg, tipc_own_addr);
  582. msg_set_orignode(msg, tipc_own_addr);
  583. }
  584. spin_unlock_bh(&tipc_port_list_lock);
  585. }
  586. /*
  587. * port_dispatcher_sigh(): Signal handler for messages destinated
  588. * to the tipc_port interface.
  589. */
  590. static void port_dispatcher_sigh(void *dummy)
  591. {
  592. struct sk_buff *buf;
  593. spin_lock_bh(&queue_lock);
  594. buf = msg_queue_head;
  595. msg_queue_head = NULL;
  596. spin_unlock_bh(&queue_lock);
  597. while (buf) {
  598. struct tipc_port *p_ptr;
  599. struct user_port *up_ptr;
  600. struct tipc_portid orig;
  601. struct tipc_name_seq dseq;
  602. void *usr_handle;
  603. int connected;
  604. int peer_invalid;
  605. int published;
  606. u32 message_type;
  607. struct sk_buff *next = buf->next;
  608. struct tipc_msg *msg = buf_msg(buf);
  609. u32 dref = msg_destport(msg);
  610. message_type = msg_type(msg);
  611. if (message_type > TIPC_DIRECT_MSG)
  612. goto reject; /* Unsupported message type */
  613. p_ptr = tipc_port_lock(dref);
  614. if (!p_ptr)
  615. goto reject; /* Port deleted while msg in queue */
  616. orig.ref = msg_origport(msg);
  617. orig.node = msg_orignode(msg);
  618. up_ptr = p_ptr->user_port;
  619. usr_handle = up_ptr->usr_handle;
  620. connected = p_ptr->connected;
  621. peer_invalid = connected && !tipc_port_peer_msg(p_ptr, msg);
  622. published = p_ptr->published;
  623. if (unlikely(msg_errcode(msg)))
  624. goto err;
  625. switch (message_type) {
  626. case TIPC_CONN_MSG:{
  627. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  628. u32 dsz;
  629. tipc_port_unlock(p_ptr);
  630. if (unlikely(!cb))
  631. goto reject;
  632. if (unlikely(!connected)) {
  633. if (tipc_connect(dref, &orig))
  634. goto reject;
  635. } else if (peer_invalid)
  636. goto reject;
  637. dsz = msg_data_sz(msg);
  638. if (unlikely(dsz &&
  639. (++p_ptr->conn_unacked >=
  640. TIPC_FLOW_CONTROL_WIN)))
  641. tipc_acknowledge(dref,
  642. p_ptr->conn_unacked);
  643. skb_pull(buf, msg_hdr_sz(msg));
  644. cb(usr_handle, dref, &buf, msg_data(msg), dsz);
  645. break;
  646. }
  647. case TIPC_DIRECT_MSG:{
  648. tipc_msg_event cb = up_ptr->msg_cb;
  649. tipc_port_unlock(p_ptr);
  650. if (unlikely(!cb || connected))
  651. goto reject;
  652. skb_pull(buf, msg_hdr_sz(msg));
  653. cb(usr_handle, dref, &buf, msg_data(msg),
  654. msg_data_sz(msg), msg_importance(msg),
  655. &orig);
  656. break;
  657. }
  658. case TIPC_MCAST_MSG:
  659. case TIPC_NAMED_MSG:{
  660. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  661. tipc_port_unlock(p_ptr);
  662. if (unlikely(!cb || connected || !published))
  663. goto reject;
  664. dseq.type = msg_nametype(msg);
  665. dseq.lower = msg_nameinst(msg);
  666. dseq.upper = (message_type == TIPC_NAMED_MSG)
  667. ? dseq.lower : msg_nameupper(msg);
  668. skb_pull(buf, msg_hdr_sz(msg));
  669. cb(usr_handle, dref, &buf, msg_data(msg),
  670. msg_data_sz(msg), msg_importance(msg),
  671. &orig, &dseq);
  672. break;
  673. }
  674. }
  675. if (buf)
  676. kfree_skb(buf);
  677. buf = next;
  678. continue;
  679. err:
  680. switch (message_type) {
  681. case TIPC_CONN_MSG:{
  682. tipc_conn_shutdown_event cb =
  683. up_ptr->conn_err_cb;
  684. tipc_port_unlock(p_ptr);
  685. if (!cb || !connected || peer_invalid)
  686. break;
  687. tipc_disconnect(dref);
  688. skb_pull(buf, msg_hdr_sz(msg));
  689. cb(usr_handle, dref, &buf, msg_data(msg),
  690. msg_data_sz(msg), msg_errcode(msg));
  691. break;
  692. }
  693. case TIPC_DIRECT_MSG:{
  694. tipc_msg_err_event cb = up_ptr->err_cb;
  695. tipc_port_unlock(p_ptr);
  696. if (!cb || connected)
  697. break;
  698. skb_pull(buf, msg_hdr_sz(msg));
  699. cb(usr_handle, dref, &buf, msg_data(msg),
  700. msg_data_sz(msg), msg_errcode(msg), &orig);
  701. break;
  702. }
  703. case TIPC_MCAST_MSG:
  704. case TIPC_NAMED_MSG:{
  705. tipc_named_msg_err_event cb =
  706. up_ptr->named_err_cb;
  707. tipc_port_unlock(p_ptr);
  708. if (!cb || connected)
  709. break;
  710. dseq.type = msg_nametype(msg);
  711. dseq.lower = msg_nameinst(msg);
  712. dseq.upper = (message_type == TIPC_NAMED_MSG)
  713. ? dseq.lower : msg_nameupper(msg);
  714. skb_pull(buf, msg_hdr_sz(msg));
  715. cb(usr_handle, dref, &buf, msg_data(msg),
  716. msg_data_sz(msg), msg_errcode(msg), &dseq);
  717. break;
  718. }
  719. }
  720. if (buf)
  721. kfree_skb(buf);
  722. buf = next;
  723. continue;
  724. reject:
  725. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  726. buf = next;
  727. }
  728. }
  729. /*
  730. * port_dispatcher(): Dispatcher for messages destinated
  731. * to the tipc_port interface. Called with port locked.
  732. */
  733. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  734. {
  735. buf->next = NULL;
  736. spin_lock_bh(&queue_lock);
  737. if (msg_queue_head) {
  738. msg_queue_tail->next = buf;
  739. msg_queue_tail = buf;
  740. } else {
  741. msg_queue_tail = msg_queue_head = buf;
  742. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  743. }
  744. spin_unlock_bh(&queue_lock);
  745. return 0;
  746. }
  747. /*
  748. * Wake up port after congestion: Called with port locked
  749. */
  750. static void port_wakeup_sh(unsigned long ref)
  751. {
  752. struct tipc_port *p_ptr;
  753. struct user_port *up_ptr;
  754. tipc_continue_event cb = NULL;
  755. void *uh = NULL;
  756. p_ptr = tipc_port_lock(ref);
  757. if (p_ptr) {
  758. up_ptr = p_ptr->user_port;
  759. if (up_ptr) {
  760. cb = up_ptr->continue_event_cb;
  761. uh = up_ptr->usr_handle;
  762. }
  763. tipc_port_unlock(p_ptr);
  764. }
  765. if (cb)
  766. cb(uh, ref);
  767. }
  768. static void port_wakeup(struct tipc_port *p_ptr)
  769. {
  770. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  771. }
  772. void tipc_acknowledge(u32 ref, u32 ack)
  773. {
  774. struct tipc_port *p_ptr;
  775. struct sk_buff *buf = NULL;
  776. p_ptr = tipc_port_lock(ref);
  777. if (!p_ptr)
  778. return;
  779. if (p_ptr->connected) {
  780. p_ptr->conn_unacked -= ack;
  781. buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
  782. }
  783. tipc_port_unlock(p_ptr);
  784. tipc_net_route_msg(buf);
  785. }
  786. /*
  787. * tipc_createport(): user level call.
  788. */
  789. int tipc_createport(void *usr_handle,
  790. unsigned int importance,
  791. tipc_msg_err_event error_cb,
  792. tipc_named_msg_err_event named_error_cb,
  793. tipc_conn_shutdown_event conn_error_cb,
  794. tipc_msg_event msg_cb,
  795. tipc_named_msg_event named_msg_cb,
  796. tipc_conn_msg_event conn_msg_cb,
  797. tipc_continue_event continue_event_cb, /* May be zero */
  798. u32 *portref)
  799. {
  800. struct user_port *up_ptr;
  801. struct tipc_port *p_ptr;
  802. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  803. if (!up_ptr) {
  804. pr_warn("Port creation failed, no memory\n");
  805. return -ENOMEM;
  806. }
  807. p_ptr = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
  808. importance);
  809. if (!p_ptr) {
  810. kfree(up_ptr);
  811. return -ENOMEM;
  812. }
  813. p_ptr->user_port = up_ptr;
  814. up_ptr->usr_handle = usr_handle;
  815. up_ptr->ref = p_ptr->ref;
  816. up_ptr->err_cb = error_cb;
  817. up_ptr->named_err_cb = named_error_cb;
  818. up_ptr->conn_err_cb = conn_error_cb;
  819. up_ptr->msg_cb = msg_cb;
  820. up_ptr->named_msg_cb = named_msg_cb;
  821. up_ptr->conn_msg_cb = conn_msg_cb;
  822. up_ptr->continue_event_cb = continue_event_cb;
  823. *portref = p_ptr->ref;
  824. tipc_port_unlock(p_ptr);
  825. return 0;
  826. }
  827. int tipc_portimportance(u32 ref, unsigned int *importance)
  828. {
  829. struct tipc_port *p_ptr;
  830. p_ptr = tipc_port_lock(ref);
  831. if (!p_ptr)
  832. return -EINVAL;
  833. *importance = (unsigned int)msg_importance(&p_ptr->phdr);
  834. tipc_port_unlock(p_ptr);
  835. return 0;
  836. }
  837. int tipc_set_portimportance(u32 ref, unsigned int imp)
  838. {
  839. struct tipc_port *p_ptr;
  840. if (imp > TIPC_CRITICAL_IMPORTANCE)
  841. return -EINVAL;
  842. p_ptr = tipc_port_lock(ref);
  843. if (!p_ptr)
  844. return -EINVAL;
  845. msg_set_importance(&p_ptr->phdr, (u32)imp);
  846. tipc_port_unlock(p_ptr);
  847. return 0;
  848. }
  849. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  850. {
  851. struct tipc_port *p_ptr;
  852. struct publication *publ;
  853. u32 key;
  854. int res = -EINVAL;
  855. p_ptr = tipc_port_lock(ref);
  856. if (!p_ptr)
  857. return -EINVAL;
  858. if (p_ptr->connected)
  859. goto exit;
  860. key = ref + p_ptr->pub_count + 1;
  861. if (key == ref) {
  862. res = -EADDRINUSE;
  863. goto exit;
  864. }
  865. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  866. scope, p_ptr->ref, key);
  867. if (publ) {
  868. list_add(&publ->pport_list, &p_ptr->publications);
  869. p_ptr->pub_count++;
  870. p_ptr->published = 1;
  871. res = 0;
  872. }
  873. exit:
  874. tipc_port_unlock(p_ptr);
  875. return res;
  876. }
  877. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  878. {
  879. struct tipc_port *p_ptr;
  880. struct publication *publ;
  881. struct publication *tpubl;
  882. int res = -EINVAL;
  883. p_ptr = tipc_port_lock(ref);
  884. if (!p_ptr)
  885. return -EINVAL;
  886. if (!seq) {
  887. list_for_each_entry_safe(publ, tpubl,
  888. &p_ptr->publications, pport_list) {
  889. tipc_nametbl_withdraw(publ->type, publ->lower,
  890. publ->ref, publ->key);
  891. }
  892. res = 0;
  893. } else {
  894. list_for_each_entry_safe(publ, tpubl,
  895. &p_ptr->publications, pport_list) {
  896. if (publ->scope != scope)
  897. continue;
  898. if (publ->type != seq->type)
  899. continue;
  900. if (publ->lower != seq->lower)
  901. continue;
  902. if (publ->upper != seq->upper)
  903. break;
  904. tipc_nametbl_withdraw(publ->type, publ->lower,
  905. publ->ref, publ->key);
  906. res = 0;
  907. break;
  908. }
  909. }
  910. if (list_empty(&p_ptr->publications))
  911. p_ptr->published = 0;
  912. tipc_port_unlock(p_ptr);
  913. return res;
  914. }
  915. int tipc_connect(u32 ref, struct tipc_portid const *peer)
  916. {
  917. struct tipc_port *p_ptr;
  918. int res;
  919. p_ptr = tipc_port_lock(ref);
  920. if (!p_ptr)
  921. return -EINVAL;
  922. res = __tipc_connect(ref, p_ptr, peer);
  923. tipc_port_unlock(p_ptr);
  924. return res;
  925. }
  926. /*
  927. * __tipc_connect - connect to a remote peer
  928. *
  929. * Port must be locked.
  930. */
  931. int __tipc_connect(u32 ref, struct tipc_port *p_ptr,
  932. struct tipc_portid const *peer)
  933. {
  934. struct tipc_msg *msg;
  935. int res = -EINVAL;
  936. if (p_ptr->published || p_ptr->connected)
  937. goto exit;
  938. if (!peer->ref)
  939. goto exit;
  940. msg = &p_ptr->phdr;
  941. msg_set_destnode(msg, peer->node);
  942. msg_set_destport(msg, peer->ref);
  943. msg_set_type(msg, TIPC_CONN_MSG);
  944. msg_set_lookup_scope(msg, 0);
  945. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  946. p_ptr->probing_interval = PROBING_INTERVAL;
  947. p_ptr->probing_state = CONFIRMED;
  948. p_ptr->connected = 1;
  949. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  950. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  951. (void *)(unsigned long)ref,
  952. (net_ev_handler)port_handle_node_down);
  953. res = 0;
  954. exit:
  955. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  956. return res;
  957. }
  958. /*
  959. * __tipc_disconnect - disconnect port from peer
  960. *
  961. * Port must be locked.
  962. */
  963. int __tipc_disconnect(struct tipc_port *tp_ptr)
  964. {
  965. int res;
  966. if (tp_ptr->connected) {
  967. tp_ptr->connected = 0;
  968. /* let timer expire on it's own to avoid deadlock! */
  969. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  970. res = 0;
  971. } else {
  972. res = -ENOTCONN;
  973. }
  974. return res;
  975. }
  976. /*
  977. * tipc_disconnect(): Disconnect port form peer.
  978. * This is a node local operation.
  979. */
  980. int tipc_disconnect(u32 ref)
  981. {
  982. struct tipc_port *p_ptr;
  983. int res;
  984. p_ptr = tipc_port_lock(ref);
  985. if (!p_ptr)
  986. return -EINVAL;
  987. res = __tipc_disconnect(p_ptr);
  988. tipc_port_unlock(p_ptr);
  989. return res;
  990. }
  991. /*
  992. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  993. */
  994. int tipc_shutdown(u32 ref)
  995. {
  996. struct tipc_port *p_ptr;
  997. struct sk_buff *buf = NULL;
  998. p_ptr = tipc_port_lock(ref);
  999. if (!p_ptr)
  1000. return -EINVAL;
  1001. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  1002. tipc_port_unlock(p_ptr);
  1003. tipc_net_route_msg(buf);
  1004. return tipc_disconnect(ref);
  1005. }
  1006. /**
  1007. * tipc_port_recv_msg - receive message from lower layer and deliver to port user
  1008. */
  1009. int tipc_port_recv_msg(struct sk_buff *buf)
  1010. {
  1011. struct tipc_port *p_ptr;
  1012. struct tipc_msg *msg = buf_msg(buf);
  1013. u32 destport = msg_destport(msg);
  1014. u32 dsz = msg_data_sz(msg);
  1015. u32 err;
  1016. /* forward unresolved named message */
  1017. if (unlikely(!destport)) {
  1018. tipc_net_route_msg(buf);
  1019. return dsz;
  1020. }
  1021. /* validate destination & pass to port, otherwise reject message */
  1022. p_ptr = tipc_port_lock(destport);
  1023. if (likely(p_ptr)) {
  1024. err = p_ptr->dispatcher(p_ptr, buf);
  1025. tipc_port_unlock(p_ptr);
  1026. if (likely(!err))
  1027. return dsz;
  1028. } else {
  1029. err = TIPC_ERR_NO_PORT;
  1030. }
  1031. return tipc_reject_msg(buf, err);
  1032. }
  1033. /*
  1034. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1035. * message for this node.
  1036. */
  1037. static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
  1038. struct iovec const *msg_sect,
  1039. unsigned int total_len)
  1040. {
  1041. struct sk_buff *buf;
  1042. int res;
  1043. res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
  1044. MAX_MSG_SIZE, !sender->user_port, &buf);
  1045. if (likely(buf))
  1046. tipc_port_recv_msg(buf);
  1047. return res;
  1048. }
  1049. /**
  1050. * tipc_send - send message sections on connection
  1051. */
  1052. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
  1053. unsigned int total_len)
  1054. {
  1055. struct tipc_port *p_ptr;
  1056. u32 destnode;
  1057. int res;
  1058. p_ptr = tipc_port_deref(ref);
  1059. if (!p_ptr || !p_ptr->connected)
  1060. return -EINVAL;
  1061. p_ptr->congested = 1;
  1062. if (!tipc_port_congested(p_ptr)) {
  1063. destnode = port_peernode(p_ptr);
  1064. if (likely(!in_own_node(destnode)))
  1065. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1066. total_len, destnode);
  1067. else
  1068. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1069. total_len);
  1070. if (likely(res != -ELINKCONG)) {
  1071. p_ptr->congested = 0;
  1072. if (res > 0)
  1073. p_ptr->sent++;
  1074. return res;
  1075. }
  1076. }
  1077. if (port_unreliable(p_ptr)) {
  1078. p_ptr->congested = 0;
  1079. return total_len;
  1080. }
  1081. return -ELINKCONG;
  1082. }
  1083. /**
  1084. * tipc_send2name - send message sections to port name
  1085. */
  1086. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  1087. unsigned int num_sect, struct iovec const *msg_sect,
  1088. unsigned int total_len)
  1089. {
  1090. struct tipc_port *p_ptr;
  1091. struct tipc_msg *msg;
  1092. u32 destnode = domain;
  1093. u32 destport;
  1094. int res;
  1095. p_ptr = tipc_port_deref(ref);
  1096. if (!p_ptr || p_ptr->connected)
  1097. return -EINVAL;
  1098. msg = &p_ptr->phdr;
  1099. msg_set_type(msg, TIPC_NAMED_MSG);
  1100. msg_set_hdr_sz(msg, NAMED_H_SIZE);
  1101. msg_set_nametype(msg, name->type);
  1102. msg_set_nameinst(msg, name->instance);
  1103. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  1104. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1105. msg_set_destnode(msg, destnode);
  1106. msg_set_destport(msg, destport);
  1107. if (likely(destport || destnode)) {
  1108. if (likely(in_own_node(destnode)))
  1109. res = tipc_port_recv_sections(p_ptr, num_sect,
  1110. msg_sect, total_len);
  1111. else if (tipc_own_addr)
  1112. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  1113. num_sect, total_len,
  1114. destnode);
  1115. else
  1116. res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
  1117. num_sect, total_len,
  1118. TIPC_ERR_NO_NODE);
  1119. if (likely(res != -ELINKCONG)) {
  1120. if (res > 0)
  1121. p_ptr->sent++;
  1122. return res;
  1123. }
  1124. if (port_unreliable(p_ptr)) {
  1125. return total_len;
  1126. }
  1127. return -ELINKCONG;
  1128. }
  1129. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1130. total_len, TIPC_ERR_NO_NAME);
  1131. }
  1132. /**
  1133. * tipc_send2port - send message sections to port identity
  1134. */
  1135. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  1136. unsigned int num_sect, struct iovec const *msg_sect,
  1137. unsigned int total_len)
  1138. {
  1139. struct tipc_port *p_ptr;
  1140. struct tipc_msg *msg;
  1141. int res;
  1142. p_ptr = tipc_port_deref(ref);
  1143. if (!p_ptr || p_ptr->connected)
  1144. return -EINVAL;
  1145. msg = &p_ptr->phdr;
  1146. msg_set_type(msg, TIPC_DIRECT_MSG);
  1147. msg_set_lookup_scope(msg, 0);
  1148. msg_set_destnode(msg, dest->node);
  1149. msg_set_destport(msg, dest->ref);
  1150. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1151. if (in_own_node(dest->node))
  1152. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1153. total_len);
  1154. else if (tipc_own_addr)
  1155. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1156. total_len, dest->node);
  1157. else
  1158. res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1159. total_len, TIPC_ERR_NO_NODE);
  1160. if (likely(res != -ELINKCONG)) {
  1161. if (res > 0)
  1162. p_ptr->sent++;
  1163. return res;
  1164. }
  1165. if (port_unreliable(p_ptr)) {
  1166. return total_len;
  1167. }
  1168. return -ELINKCONG;
  1169. }
  1170. /**
  1171. * tipc_send_buf2port - send message buffer to port identity
  1172. */
  1173. int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
  1174. struct sk_buff *buf, unsigned int dsz)
  1175. {
  1176. struct tipc_port *p_ptr;
  1177. struct tipc_msg *msg;
  1178. int res;
  1179. p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
  1180. if (!p_ptr || p_ptr->connected)
  1181. return -EINVAL;
  1182. msg = &p_ptr->phdr;
  1183. msg_set_type(msg, TIPC_DIRECT_MSG);
  1184. msg_set_destnode(msg, dest->node);
  1185. msg_set_destport(msg, dest->ref);
  1186. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1187. msg_set_size(msg, BASIC_H_SIZE + dsz);
  1188. if (skb_cow(buf, BASIC_H_SIZE))
  1189. return -ENOMEM;
  1190. skb_push(buf, BASIC_H_SIZE);
  1191. skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
  1192. if (in_own_node(dest->node))
  1193. res = tipc_port_recv_msg(buf);
  1194. else
  1195. res = tipc_send_buf_fast(buf, dest->node);
  1196. if (likely(res != -ELINKCONG)) {
  1197. if (res > 0)
  1198. p_ptr->sent++;
  1199. return res;
  1200. }
  1201. if (port_unreliable(p_ptr))
  1202. return dsz;
  1203. return -ELINKCONG;
  1204. }