port.c 32 KB

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