port.c 33 KB

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