port.c 32 KB

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