port.c 33 KB

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