port.c 33 KB

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