port.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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_connect2port(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_connect2port(u32 ref, struct tipc_portid const *peer)
  916. {
  917. struct tipc_port *p_ptr;
  918. struct tipc_msg *msg;
  919. int res = -EINVAL;
  920. p_ptr = tipc_port_lock(ref);
  921. if (!p_ptr)
  922. return -EINVAL;
  923. if (p_ptr->published || p_ptr->connected)
  924. goto exit;
  925. if (!peer->ref)
  926. goto exit;
  927. msg = &p_ptr->phdr;
  928. msg_set_destnode(msg, peer->node);
  929. msg_set_destport(msg, peer->ref);
  930. msg_set_type(msg, TIPC_CONN_MSG);
  931. msg_set_lookup_scope(msg, 0);
  932. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  933. p_ptr->probing_interval = PROBING_INTERVAL;
  934. p_ptr->probing_state = CONFIRMED;
  935. p_ptr->connected = 1;
  936. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  937. tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
  938. (void *)(unsigned long)ref,
  939. (net_ev_handler)port_handle_node_down);
  940. res = 0;
  941. exit:
  942. tipc_port_unlock(p_ptr);
  943. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  944. return res;
  945. }
  946. /**
  947. * tipc_disconnect_port - disconnect port from peer
  948. *
  949. * Port must be locked.
  950. */
  951. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  952. {
  953. int res;
  954. if (tp_ptr->connected) {
  955. tp_ptr->connected = 0;
  956. /* let timer expire on it's own to avoid deadlock! */
  957. tipc_nodesub_unsubscribe(&tp_ptr->subscription);
  958. res = 0;
  959. } else {
  960. res = -ENOTCONN;
  961. }
  962. return res;
  963. }
  964. /*
  965. * tipc_disconnect(): Disconnect port form peer.
  966. * This is a node local operation.
  967. */
  968. int tipc_disconnect(u32 ref)
  969. {
  970. struct tipc_port *p_ptr;
  971. int res;
  972. p_ptr = tipc_port_lock(ref);
  973. if (!p_ptr)
  974. return -EINVAL;
  975. res = tipc_disconnect_port(p_ptr);
  976. tipc_port_unlock(p_ptr);
  977. return res;
  978. }
  979. /*
  980. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  981. */
  982. int tipc_shutdown(u32 ref)
  983. {
  984. struct tipc_port *p_ptr;
  985. struct sk_buff *buf = NULL;
  986. p_ptr = tipc_port_lock(ref);
  987. if (!p_ptr)
  988. return -EINVAL;
  989. buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
  990. tipc_port_unlock(p_ptr);
  991. tipc_net_route_msg(buf);
  992. return tipc_disconnect(ref);
  993. }
  994. /**
  995. * tipc_port_recv_msg - receive message from lower layer and deliver to port user
  996. */
  997. int tipc_port_recv_msg(struct sk_buff *buf)
  998. {
  999. struct tipc_port *p_ptr;
  1000. struct tipc_msg *msg = buf_msg(buf);
  1001. u32 destport = msg_destport(msg);
  1002. u32 dsz = msg_data_sz(msg);
  1003. u32 err;
  1004. /* forward unresolved named message */
  1005. if (unlikely(!destport)) {
  1006. tipc_net_route_msg(buf);
  1007. return dsz;
  1008. }
  1009. /* validate destination & pass to port, otherwise reject message */
  1010. p_ptr = tipc_port_lock(destport);
  1011. if (likely(p_ptr)) {
  1012. err = p_ptr->dispatcher(p_ptr, buf);
  1013. tipc_port_unlock(p_ptr);
  1014. if (likely(!err))
  1015. return dsz;
  1016. } else {
  1017. err = TIPC_ERR_NO_PORT;
  1018. }
  1019. return tipc_reject_msg(buf, err);
  1020. }
  1021. /*
  1022. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1023. * message for this node.
  1024. */
  1025. static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
  1026. struct iovec const *msg_sect,
  1027. unsigned int total_len)
  1028. {
  1029. struct sk_buff *buf;
  1030. int res;
  1031. res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
  1032. MAX_MSG_SIZE, !sender->user_port, &buf);
  1033. if (likely(buf))
  1034. tipc_port_recv_msg(buf);
  1035. return res;
  1036. }
  1037. /**
  1038. * tipc_send - send message sections on connection
  1039. */
  1040. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
  1041. unsigned int total_len)
  1042. {
  1043. struct tipc_port *p_ptr;
  1044. u32 destnode;
  1045. int res;
  1046. p_ptr = tipc_port_deref(ref);
  1047. if (!p_ptr || !p_ptr->connected)
  1048. return -EINVAL;
  1049. p_ptr->congested = 1;
  1050. if (!tipc_port_congested(p_ptr)) {
  1051. destnode = port_peernode(p_ptr);
  1052. if (likely(!in_own_node(destnode)))
  1053. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1054. total_len, destnode);
  1055. else
  1056. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1057. total_len);
  1058. if (likely(res != -ELINKCONG)) {
  1059. p_ptr->congested = 0;
  1060. if (res > 0)
  1061. p_ptr->sent++;
  1062. return res;
  1063. }
  1064. }
  1065. if (port_unreliable(p_ptr)) {
  1066. p_ptr->congested = 0;
  1067. return total_len;
  1068. }
  1069. return -ELINKCONG;
  1070. }
  1071. /**
  1072. * tipc_send2name - send message sections to port name
  1073. */
  1074. int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
  1075. unsigned int num_sect, struct iovec const *msg_sect,
  1076. unsigned int total_len)
  1077. {
  1078. struct tipc_port *p_ptr;
  1079. struct tipc_msg *msg;
  1080. u32 destnode = domain;
  1081. u32 destport;
  1082. int res;
  1083. p_ptr = tipc_port_deref(ref);
  1084. if (!p_ptr || p_ptr->connected)
  1085. return -EINVAL;
  1086. msg = &p_ptr->phdr;
  1087. msg_set_type(msg, TIPC_NAMED_MSG);
  1088. msg_set_hdr_sz(msg, NAMED_H_SIZE);
  1089. msg_set_nametype(msg, name->type);
  1090. msg_set_nameinst(msg, name->instance);
  1091. msg_set_lookup_scope(msg, tipc_addr_scope(domain));
  1092. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1093. msg_set_destnode(msg, destnode);
  1094. msg_set_destport(msg, destport);
  1095. if (likely(destport || destnode)) {
  1096. if (likely(in_own_node(destnode)))
  1097. res = tipc_port_recv_sections(p_ptr, num_sect,
  1098. msg_sect, total_len);
  1099. else if (tipc_own_addr)
  1100. res = tipc_link_send_sections_fast(p_ptr, msg_sect,
  1101. num_sect, total_len,
  1102. destnode);
  1103. else
  1104. res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
  1105. num_sect, total_len,
  1106. TIPC_ERR_NO_NODE);
  1107. if (likely(res != -ELINKCONG)) {
  1108. if (res > 0)
  1109. p_ptr->sent++;
  1110. return res;
  1111. }
  1112. if (port_unreliable(p_ptr)) {
  1113. return total_len;
  1114. }
  1115. return -ELINKCONG;
  1116. }
  1117. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1118. total_len, TIPC_ERR_NO_NAME);
  1119. }
  1120. /**
  1121. * tipc_send2port - send message sections to port identity
  1122. */
  1123. int tipc_send2port(u32 ref, struct tipc_portid const *dest,
  1124. unsigned int num_sect, struct iovec const *msg_sect,
  1125. unsigned int total_len)
  1126. {
  1127. struct tipc_port *p_ptr;
  1128. struct tipc_msg *msg;
  1129. int res;
  1130. p_ptr = tipc_port_deref(ref);
  1131. if (!p_ptr || p_ptr->connected)
  1132. return -EINVAL;
  1133. msg = &p_ptr->phdr;
  1134. msg_set_type(msg, TIPC_DIRECT_MSG);
  1135. msg_set_lookup_scope(msg, 0);
  1136. msg_set_destnode(msg, dest->node);
  1137. msg_set_destport(msg, dest->ref);
  1138. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1139. if (in_own_node(dest->node))
  1140. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
  1141. total_len);
  1142. else if (tipc_own_addr)
  1143. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1144. total_len, dest->node);
  1145. else
  1146. res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1147. total_len, TIPC_ERR_NO_NODE);
  1148. if (likely(res != -ELINKCONG)) {
  1149. if (res > 0)
  1150. p_ptr->sent++;
  1151. return res;
  1152. }
  1153. if (port_unreliable(p_ptr)) {
  1154. return total_len;
  1155. }
  1156. return -ELINKCONG;
  1157. }
  1158. /**
  1159. * tipc_send_buf2port - send message buffer to port identity
  1160. */
  1161. int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
  1162. struct sk_buff *buf, unsigned int dsz)
  1163. {
  1164. struct tipc_port *p_ptr;
  1165. struct tipc_msg *msg;
  1166. int res;
  1167. p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
  1168. if (!p_ptr || p_ptr->connected)
  1169. return -EINVAL;
  1170. msg = &p_ptr->phdr;
  1171. msg_set_type(msg, TIPC_DIRECT_MSG);
  1172. msg_set_destnode(msg, dest->node);
  1173. msg_set_destport(msg, dest->ref);
  1174. msg_set_hdr_sz(msg, BASIC_H_SIZE);
  1175. msg_set_size(msg, BASIC_H_SIZE + dsz);
  1176. if (skb_cow(buf, BASIC_H_SIZE))
  1177. return -ENOMEM;
  1178. skb_push(buf, BASIC_H_SIZE);
  1179. skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
  1180. if (in_own_node(dest->node))
  1181. res = tipc_port_recv_msg(buf);
  1182. else
  1183. res = tipc_send_buf_fast(buf, dest->node);
  1184. if (likely(res != -ELINKCONG)) {
  1185. if (res > 0)
  1186. p_ptr->sent++;
  1187. return res;
  1188. }
  1189. if (port_unreliable(p_ptr))
  1190. return dsz;
  1191. return -ELINKCONG;
  1192. }