port.c 32 KB

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