port.c 32 KB

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