port.c 32 KB

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