port.c 35 KB

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