port.c 32 KB

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