port.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2007, 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 = 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 native TIPC port
  184. *
  185. * Returns local port reference
  186. */
  187. u32 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 0;
  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 0;
  205. }
  206. tipc_port_lock(ref);
  207. p_ptr->publ.usr_handle = usr_handle;
  208. p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
  209. p_ptr->publ.ref = ref;
  210. msg = &p_ptr->publ.phdr;
  211. msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
  212. msg_set_orignode(msg, tipc_own_addr);
  213. msg_set_prevnode(msg, tipc_own_addr);
  214. msg_set_origport(msg, ref);
  215. msg_set_importance(msg,importance);
  216. p_ptr->last_in_seqno = 41;
  217. p_ptr->sent = 1;
  218. INIT_LIST_HEAD(&p_ptr->wait_list);
  219. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  220. p_ptr->congested_link = NULL;
  221. p_ptr->dispatcher = dispatcher;
  222. p_ptr->wakeup = wakeup;
  223. p_ptr->user_port = NULL;
  224. k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
  225. spin_lock_bh(&tipc_port_list_lock);
  226. INIT_LIST_HEAD(&p_ptr->publications);
  227. INIT_LIST_HEAD(&p_ptr->port_list);
  228. list_add_tail(&p_ptr->port_list, &ports);
  229. spin_unlock_bh(&tipc_port_list_lock);
  230. tipc_port_unlock(p_ptr);
  231. return ref;
  232. }
  233. int tipc_deleteport(u32 ref)
  234. {
  235. struct port *p_ptr;
  236. struct sk_buff *buf = NULL;
  237. tipc_withdraw(ref, 0, NULL);
  238. p_ptr = tipc_port_lock(ref);
  239. if (!p_ptr)
  240. return -EINVAL;
  241. tipc_ref_discard(ref);
  242. tipc_port_unlock(p_ptr);
  243. k_cancel_timer(&p_ptr->timer);
  244. if (p_ptr->publ.connected) {
  245. buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  246. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  247. }
  248. if (p_ptr->user_port) {
  249. tipc_reg_remove_port(p_ptr->user_port);
  250. kfree(p_ptr->user_port);
  251. }
  252. spin_lock_bh(&tipc_port_list_lock);
  253. list_del(&p_ptr->port_list);
  254. list_del(&p_ptr->wait_list);
  255. spin_unlock_bh(&tipc_port_list_lock);
  256. k_term_timer(&p_ptr->timer);
  257. kfree(p_ptr);
  258. dbg("Deleted port %u\n", ref);
  259. tipc_net_route_msg(buf);
  260. return TIPC_OK;
  261. }
  262. /**
  263. * tipc_get_port() - return port associated with 'ref'
  264. *
  265. * Note: Port is not locked.
  266. */
  267. struct tipc_port *tipc_get_port(const u32 ref)
  268. {
  269. return (struct tipc_port *)tipc_ref_deref(ref);
  270. }
  271. /**
  272. * tipc_get_handle - return user handle associated to port 'ref'
  273. */
  274. void *tipc_get_handle(const u32 ref)
  275. {
  276. struct port *p_ptr;
  277. void * handle;
  278. p_ptr = tipc_port_lock(ref);
  279. if (!p_ptr)
  280. return NULL;
  281. handle = p_ptr->publ.usr_handle;
  282. tipc_port_unlock(p_ptr);
  283. return handle;
  284. }
  285. static int port_unreliable(struct port *p_ptr)
  286. {
  287. return msg_src_droppable(&p_ptr->publ.phdr);
  288. }
  289. int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
  290. {
  291. struct port *p_ptr;
  292. p_ptr = tipc_port_lock(ref);
  293. if (!p_ptr)
  294. return -EINVAL;
  295. *isunreliable = port_unreliable(p_ptr);
  296. spin_unlock_bh(p_ptr->publ.lock);
  297. return TIPC_OK;
  298. }
  299. int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
  300. {
  301. struct port *p_ptr;
  302. p_ptr = tipc_port_lock(ref);
  303. if (!p_ptr)
  304. return -EINVAL;
  305. msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
  306. tipc_port_unlock(p_ptr);
  307. return TIPC_OK;
  308. }
  309. static int port_unreturnable(struct port *p_ptr)
  310. {
  311. return msg_dest_droppable(&p_ptr->publ.phdr);
  312. }
  313. int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
  314. {
  315. struct port *p_ptr;
  316. p_ptr = tipc_port_lock(ref);
  317. if (!p_ptr)
  318. return -EINVAL;
  319. *isunrejectable = port_unreturnable(p_ptr);
  320. spin_unlock_bh(p_ptr->publ.lock);
  321. return TIPC_OK;
  322. }
  323. int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
  324. {
  325. struct port *p_ptr;
  326. p_ptr = tipc_port_lock(ref);
  327. if (!p_ptr)
  328. return -EINVAL;
  329. msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
  330. tipc_port_unlock(p_ptr);
  331. return TIPC_OK;
  332. }
  333. /*
  334. * port_build_proto_msg(): build a port level protocol
  335. * or a connection abortion message. Called with
  336. * tipc_port lock on.
  337. */
  338. static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
  339. u32 origport, u32 orignode,
  340. u32 usr, u32 type, u32 err,
  341. u32 seqno, u32 ack)
  342. {
  343. struct sk_buff *buf;
  344. struct tipc_msg *msg;
  345. buf = buf_acquire(LONG_H_SIZE);
  346. if (buf) {
  347. msg = buf_msg(buf);
  348. msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
  349. msg_set_destport(msg, destport);
  350. msg_set_origport(msg, origport);
  351. msg_set_destnode(msg, destnode);
  352. msg_set_orignode(msg, orignode);
  353. msg_set_transp_seqno(msg, seqno);
  354. msg_set_msgcnt(msg, ack);
  355. msg_dbg(msg, "PORT>SEND>:");
  356. }
  357. return buf;
  358. }
  359. int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
  360. {
  361. msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
  362. msg_set_options(&tp_ptr->phdr, opt, sz);
  363. return TIPC_OK;
  364. }
  365. int tipc_reject_msg(struct sk_buff *buf, u32 err)
  366. {
  367. struct tipc_msg *msg = buf_msg(buf);
  368. struct sk_buff *rbuf;
  369. struct tipc_msg *rmsg;
  370. int hdr_sz;
  371. u32 imp = msg_importance(msg);
  372. u32 data_sz = msg_data_sz(msg);
  373. if (data_sz > MAX_REJECT_SIZE)
  374. data_sz = MAX_REJECT_SIZE;
  375. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  376. imp++;
  377. msg_dbg(msg, "port->rej: ");
  378. /* discard rejected message if it shouldn't be returned to sender */
  379. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  380. buf_discard(buf);
  381. return data_sz;
  382. }
  383. /* construct rejected message */
  384. if (msg_mcast(msg))
  385. hdr_sz = MCAST_H_SIZE;
  386. else
  387. hdr_sz = LONG_H_SIZE;
  388. rbuf = buf_acquire(data_sz + hdr_sz);
  389. if (rbuf == NULL) {
  390. buf_discard(buf);
  391. return data_sz;
  392. }
  393. rmsg = buf_msg(rbuf);
  394. msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
  395. msg_set_destport(rmsg, msg_origport(msg));
  396. msg_set_prevnode(rmsg, tipc_own_addr);
  397. msg_set_origport(rmsg, msg_destport(msg));
  398. if (msg_short(msg))
  399. msg_set_orignode(rmsg, tipc_own_addr);
  400. else
  401. msg_set_orignode(rmsg, msg_destnode(msg));
  402. msg_set_size(rmsg, data_sz + hdr_sz);
  403. msg_set_nametype(rmsg, msg_nametype(msg));
  404. msg_set_nameinst(rmsg, msg_nameinst(msg));
  405. skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
  406. /* send self-abort message when rejecting on a connected port */
  407. if (msg_connected(msg)) {
  408. struct sk_buff *abuf = NULL;
  409. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  410. if (p_ptr) {
  411. if (p_ptr->publ.connected)
  412. abuf = port_build_self_abort_msg(p_ptr, err);
  413. tipc_port_unlock(p_ptr);
  414. }
  415. tipc_net_route_msg(abuf);
  416. }
  417. /* send rejected message */
  418. buf_discard(buf);
  419. tipc_net_route_msg(rbuf);
  420. return data_sz;
  421. }
  422. int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
  423. struct iovec const *msg_sect, u32 num_sect,
  424. int err)
  425. {
  426. struct sk_buff *buf;
  427. int res;
  428. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  429. !p_ptr->user_port, &buf);
  430. if (!buf)
  431. return res;
  432. return tipc_reject_msg(buf, err);
  433. }
  434. static void port_timeout(unsigned long ref)
  435. {
  436. struct port *p_ptr = tipc_port_lock(ref);
  437. struct sk_buff *buf = NULL;
  438. if (!p_ptr)
  439. return;
  440. if (!p_ptr->publ.connected) {
  441. tipc_port_unlock(p_ptr);
  442. return;
  443. }
  444. /* Last probe answered ? */
  445. if (p_ptr->probing_state == PROBING) {
  446. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  447. } else {
  448. buf = port_build_proto_msg(port_peerport(p_ptr),
  449. port_peernode(p_ptr),
  450. p_ptr->publ.ref,
  451. tipc_own_addr,
  452. CONN_MANAGER,
  453. CONN_PROBE,
  454. TIPC_OK,
  455. port_out_seqno(p_ptr),
  456. 0);
  457. port_incr_out_seqno(p_ptr);
  458. p_ptr->probing_state = PROBING;
  459. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  460. }
  461. tipc_port_unlock(p_ptr);
  462. tipc_net_route_msg(buf);
  463. }
  464. static void port_handle_node_down(unsigned long ref)
  465. {
  466. struct port *p_ptr = tipc_port_lock(ref);
  467. struct sk_buff* buf = NULL;
  468. if (!p_ptr)
  469. return;
  470. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  471. tipc_port_unlock(p_ptr);
  472. tipc_net_route_msg(buf);
  473. }
  474. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  475. {
  476. u32 imp = msg_importance(&p_ptr->publ.phdr);
  477. if (!p_ptr->publ.connected)
  478. return NULL;
  479. if (imp < TIPC_CRITICAL_IMPORTANCE)
  480. imp++;
  481. return port_build_proto_msg(p_ptr->publ.ref,
  482. tipc_own_addr,
  483. port_peerport(p_ptr),
  484. port_peernode(p_ptr),
  485. imp,
  486. TIPC_CONN_MSG,
  487. err,
  488. p_ptr->last_in_seqno + 1,
  489. 0);
  490. }
  491. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  492. {
  493. u32 imp = msg_importance(&p_ptr->publ.phdr);
  494. if (!p_ptr->publ.connected)
  495. return NULL;
  496. if (imp < TIPC_CRITICAL_IMPORTANCE)
  497. imp++;
  498. return port_build_proto_msg(port_peerport(p_ptr),
  499. port_peernode(p_ptr),
  500. p_ptr->publ.ref,
  501. tipc_own_addr,
  502. imp,
  503. TIPC_CONN_MSG,
  504. err,
  505. port_out_seqno(p_ptr),
  506. 0);
  507. }
  508. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  509. {
  510. struct tipc_msg *msg = buf_msg(buf);
  511. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  512. u32 err = TIPC_OK;
  513. struct sk_buff *r_buf = NULL;
  514. struct sk_buff *abort_buf = NULL;
  515. msg_dbg(msg, "PORT<RECV<:");
  516. if (!p_ptr) {
  517. err = TIPC_ERR_NO_PORT;
  518. } else if (p_ptr->publ.connected) {
  519. if (port_peernode(p_ptr) != msg_orignode(msg))
  520. err = TIPC_ERR_NO_PORT;
  521. if (port_peerport(p_ptr) != msg_origport(msg))
  522. err = TIPC_ERR_NO_PORT;
  523. if (!err && msg_routed(msg)) {
  524. u32 seqno = msg_transp_seqno(msg);
  525. u32 myno = ++p_ptr->last_in_seqno;
  526. if (seqno != myno) {
  527. err = TIPC_ERR_NO_PORT;
  528. abort_buf = port_build_self_abort_msg(p_ptr, err);
  529. }
  530. }
  531. if (msg_type(msg) == CONN_ACK) {
  532. int wakeup = tipc_port_congested(p_ptr) &&
  533. p_ptr->publ.congested &&
  534. p_ptr->wakeup;
  535. p_ptr->acked += msg_msgcnt(msg);
  536. if (tipc_port_congested(p_ptr))
  537. goto exit;
  538. p_ptr->publ.congested = 0;
  539. if (!wakeup)
  540. goto exit;
  541. p_ptr->wakeup(&p_ptr->publ);
  542. goto exit;
  543. }
  544. } else if (p_ptr->publ.published) {
  545. err = TIPC_ERR_NO_PORT;
  546. }
  547. if (err) {
  548. r_buf = port_build_proto_msg(msg_origport(msg),
  549. msg_orignode(msg),
  550. msg_destport(msg),
  551. tipc_own_addr,
  552. DATA_HIGH,
  553. TIPC_CONN_MSG,
  554. err,
  555. 0,
  556. 0);
  557. goto exit;
  558. }
  559. /* All is fine */
  560. if (msg_type(msg) == CONN_PROBE) {
  561. r_buf = port_build_proto_msg(msg_origport(msg),
  562. msg_orignode(msg),
  563. msg_destport(msg),
  564. tipc_own_addr,
  565. CONN_MANAGER,
  566. CONN_PROBE_REPLY,
  567. TIPC_OK,
  568. port_out_seqno(p_ptr),
  569. 0);
  570. }
  571. p_ptr->probing_state = CONFIRMED;
  572. port_incr_out_seqno(p_ptr);
  573. exit:
  574. if (p_ptr)
  575. tipc_port_unlock(p_ptr);
  576. tipc_net_route_msg(r_buf);
  577. tipc_net_route_msg(abort_buf);
  578. buf_discard(buf);
  579. }
  580. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  581. {
  582. struct publication *publ;
  583. if (full_id)
  584. tipc_printf(buf, "<%u.%u.%u:%u>:",
  585. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  586. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  587. else
  588. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  589. if (p_ptr->publ.connected) {
  590. u32 dport = port_peerport(p_ptr);
  591. u32 destnode = port_peernode(p_ptr);
  592. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  593. tipc_zone(destnode), tipc_cluster(destnode),
  594. tipc_node(destnode), dport);
  595. if (p_ptr->publ.conn_type != 0)
  596. tipc_printf(buf, " via {%u,%u}",
  597. p_ptr->publ.conn_type,
  598. p_ptr->publ.conn_instance);
  599. }
  600. else if (p_ptr->publ.published) {
  601. tipc_printf(buf, " bound to");
  602. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  603. if (publ->lower == publ->upper)
  604. tipc_printf(buf, " {%u,%u}", publ->type,
  605. publ->lower);
  606. else
  607. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  608. publ->lower, publ->upper);
  609. }
  610. }
  611. tipc_printf(buf, "\n");
  612. }
  613. #define MAX_PORT_QUERY 32768
  614. struct sk_buff *tipc_port_get_ports(void)
  615. {
  616. struct sk_buff *buf;
  617. struct tlv_desc *rep_tlv;
  618. struct print_buf pb;
  619. struct port *p_ptr;
  620. int str_len;
  621. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  622. if (!buf)
  623. return NULL;
  624. rep_tlv = (struct tlv_desc *)buf->data;
  625. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  626. spin_lock_bh(&tipc_port_list_lock);
  627. list_for_each_entry(p_ptr, &ports, port_list) {
  628. spin_lock_bh(p_ptr->publ.lock);
  629. port_print(p_ptr, &pb, 0);
  630. spin_unlock_bh(p_ptr->publ.lock);
  631. }
  632. spin_unlock_bh(&tipc_port_list_lock);
  633. str_len = tipc_printbuf_validate(&pb);
  634. skb_put(buf, TLV_SPACE(str_len));
  635. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  636. return buf;
  637. }
  638. #if 0
  639. #define MAX_PORT_STATS 2000
  640. struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
  641. {
  642. u32 ref;
  643. struct port *p_ptr;
  644. struct sk_buff *buf;
  645. struct tlv_desc *rep_tlv;
  646. struct print_buf pb;
  647. int str_len;
  648. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
  649. return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  650. ref = *(u32 *)TLV_DATA(req_tlv_area);
  651. ref = ntohl(ref);
  652. p_ptr = tipc_port_lock(ref);
  653. if (!p_ptr)
  654. return cfg_reply_error_string("port not found");
  655. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
  656. if (!buf) {
  657. tipc_port_unlock(p_ptr);
  658. return NULL;
  659. }
  660. rep_tlv = (struct tlv_desc *)buf->data;
  661. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
  662. port_print(p_ptr, &pb, 1);
  663. /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
  664. tipc_port_unlock(p_ptr);
  665. str_len = tipc_printbuf_validate(&pb);
  666. skb_put(buf, TLV_SPACE(str_len));
  667. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  668. return buf;
  669. }
  670. #endif
  671. void tipc_port_reinit(void)
  672. {
  673. struct port *p_ptr;
  674. struct tipc_msg *msg;
  675. spin_lock_bh(&tipc_port_list_lock);
  676. list_for_each_entry(p_ptr, &ports, port_list) {
  677. msg = &p_ptr->publ.phdr;
  678. if (msg_orignode(msg) == tipc_own_addr)
  679. break;
  680. msg_set_orignode(msg, tipc_own_addr);
  681. }
  682. spin_unlock_bh(&tipc_port_list_lock);
  683. }
  684. /*
  685. * port_dispatcher_sigh(): Signal handler for messages destinated
  686. * to the tipc_port interface.
  687. */
  688. static void port_dispatcher_sigh(void *dummy)
  689. {
  690. struct sk_buff *buf;
  691. spin_lock_bh(&queue_lock);
  692. buf = msg_queue_head;
  693. msg_queue_head = NULL;
  694. spin_unlock_bh(&queue_lock);
  695. while (buf) {
  696. struct port *p_ptr;
  697. struct user_port *up_ptr;
  698. struct tipc_portid orig;
  699. struct tipc_name_seq dseq;
  700. void *usr_handle;
  701. int connected;
  702. int published;
  703. u32 message_type;
  704. struct sk_buff *next = buf->next;
  705. struct tipc_msg *msg = buf_msg(buf);
  706. u32 dref = msg_destport(msg);
  707. message_type = msg_type(msg);
  708. if (message_type > TIPC_DIRECT_MSG)
  709. goto reject; /* Unsupported message type */
  710. p_ptr = tipc_port_lock(dref);
  711. if (!p_ptr)
  712. goto reject; /* Port deleted while msg in queue */
  713. orig.ref = msg_origport(msg);
  714. orig.node = msg_orignode(msg);
  715. up_ptr = p_ptr->user_port;
  716. usr_handle = up_ptr->usr_handle;
  717. connected = p_ptr->publ.connected;
  718. published = p_ptr->publ.published;
  719. if (unlikely(msg_errcode(msg)))
  720. goto err;
  721. switch (message_type) {
  722. case TIPC_CONN_MSG:{
  723. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  724. u32 peer_port = port_peerport(p_ptr);
  725. u32 peer_node = port_peernode(p_ptr);
  726. spin_unlock_bh(p_ptr->publ.lock);
  727. if (unlikely(!connected)) {
  728. if (unlikely(published))
  729. goto reject;
  730. tipc_connect2port(dref,&orig);
  731. }
  732. if (unlikely(msg_origport(msg) != peer_port))
  733. goto reject;
  734. if (unlikely(msg_orignode(msg) != peer_node))
  735. goto reject;
  736. if (unlikely(!cb))
  737. goto reject;
  738. if (unlikely(++p_ptr->publ.conn_unacked >=
  739. TIPC_FLOW_CONTROL_WIN))
  740. tipc_acknowledge(dref,
  741. p_ptr->publ.conn_unacked);
  742. skb_pull(buf, msg_hdr_sz(msg));
  743. cb(usr_handle, dref, &buf, msg_data(msg),
  744. msg_data_sz(msg));
  745. break;
  746. }
  747. case TIPC_DIRECT_MSG:{
  748. tipc_msg_event cb = up_ptr->msg_cb;
  749. spin_unlock_bh(p_ptr->publ.lock);
  750. if (unlikely(connected))
  751. goto reject;
  752. if (unlikely(!cb))
  753. goto reject;
  754. skb_pull(buf, msg_hdr_sz(msg));
  755. cb(usr_handle, dref, &buf, msg_data(msg),
  756. msg_data_sz(msg), msg_importance(msg),
  757. &orig);
  758. break;
  759. }
  760. case TIPC_MCAST_MSG:
  761. case TIPC_NAMED_MSG:{
  762. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  763. spin_unlock_bh(p_ptr->publ.lock);
  764. if (unlikely(connected))
  765. goto reject;
  766. if (unlikely(!cb))
  767. goto reject;
  768. if (unlikely(!published))
  769. goto reject;
  770. dseq.type = msg_nametype(msg);
  771. dseq.lower = msg_nameinst(msg);
  772. dseq.upper = (message_type == TIPC_NAMED_MSG)
  773. ? dseq.lower : msg_nameupper(msg);
  774. skb_pull(buf, msg_hdr_sz(msg));
  775. cb(usr_handle, dref, &buf, msg_data(msg),
  776. msg_data_sz(msg), msg_importance(msg),
  777. &orig, &dseq);
  778. break;
  779. }
  780. }
  781. if (buf)
  782. buf_discard(buf);
  783. buf = next;
  784. continue;
  785. err:
  786. switch (message_type) {
  787. case TIPC_CONN_MSG:{
  788. tipc_conn_shutdown_event cb =
  789. up_ptr->conn_err_cb;
  790. u32 peer_port = port_peerport(p_ptr);
  791. u32 peer_node = port_peernode(p_ptr);
  792. spin_unlock_bh(p_ptr->publ.lock);
  793. if (!connected || !cb)
  794. break;
  795. if (msg_origport(msg) != peer_port)
  796. break;
  797. if (msg_orignode(msg) != peer_node)
  798. break;
  799. tipc_disconnect(dref);
  800. skb_pull(buf, msg_hdr_sz(msg));
  801. cb(usr_handle, dref, &buf, msg_data(msg),
  802. msg_data_sz(msg), msg_errcode(msg));
  803. break;
  804. }
  805. case TIPC_DIRECT_MSG:{
  806. tipc_msg_err_event cb = up_ptr->err_cb;
  807. spin_unlock_bh(p_ptr->publ.lock);
  808. if (connected || !cb)
  809. break;
  810. skb_pull(buf, msg_hdr_sz(msg));
  811. cb(usr_handle, dref, &buf, msg_data(msg),
  812. msg_data_sz(msg), msg_errcode(msg), &orig);
  813. break;
  814. }
  815. case TIPC_MCAST_MSG:
  816. case TIPC_NAMED_MSG:{
  817. tipc_named_msg_err_event cb =
  818. up_ptr->named_err_cb;
  819. spin_unlock_bh(p_ptr->publ.lock);
  820. if (connected || !cb)
  821. break;
  822. dseq.type = msg_nametype(msg);
  823. dseq.lower = msg_nameinst(msg);
  824. dseq.upper = (message_type == TIPC_NAMED_MSG)
  825. ? dseq.lower : msg_nameupper(msg);
  826. skb_pull(buf, msg_hdr_sz(msg));
  827. cb(usr_handle, dref, &buf, msg_data(msg),
  828. msg_data_sz(msg), msg_errcode(msg), &dseq);
  829. break;
  830. }
  831. }
  832. if (buf)
  833. buf_discard(buf);
  834. buf = next;
  835. continue;
  836. reject:
  837. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  838. buf = next;
  839. }
  840. }
  841. /*
  842. * port_dispatcher(): Dispatcher for messages destinated
  843. * to the tipc_port interface. Called with port locked.
  844. */
  845. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  846. {
  847. buf->next = NULL;
  848. spin_lock_bh(&queue_lock);
  849. if (msg_queue_head) {
  850. msg_queue_tail->next = buf;
  851. msg_queue_tail = buf;
  852. } else {
  853. msg_queue_tail = msg_queue_head = buf;
  854. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  855. }
  856. spin_unlock_bh(&queue_lock);
  857. return TIPC_OK;
  858. }
  859. /*
  860. * Wake up port after congestion: Called with port locked,
  861. *
  862. */
  863. static void port_wakeup_sh(unsigned long ref)
  864. {
  865. struct port *p_ptr;
  866. struct user_port *up_ptr;
  867. tipc_continue_event cb = NULL;
  868. void *uh = NULL;
  869. p_ptr = tipc_port_lock(ref);
  870. if (p_ptr) {
  871. up_ptr = p_ptr->user_port;
  872. if (up_ptr) {
  873. cb = up_ptr->continue_event_cb;
  874. uh = up_ptr->usr_handle;
  875. }
  876. tipc_port_unlock(p_ptr);
  877. }
  878. if (cb)
  879. cb(uh, ref);
  880. }
  881. static void port_wakeup(struct tipc_port *p_ptr)
  882. {
  883. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  884. }
  885. void tipc_acknowledge(u32 ref, u32 ack)
  886. {
  887. struct port *p_ptr;
  888. struct sk_buff *buf = NULL;
  889. p_ptr = tipc_port_lock(ref);
  890. if (!p_ptr)
  891. return;
  892. if (p_ptr->publ.connected) {
  893. p_ptr->publ.conn_unacked -= ack;
  894. buf = port_build_proto_msg(port_peerport(p_ptr),
  895. port_peernode(p_ptr),
  896. ref,
  897. tipc_own_addr,
  898. CONN_MANAGER,
  899. CONN_ACK,
  900. TIPC_OK,
  901. port_out_seqno(p_ptr),
  902. ack);
  903. }
  904. tipc_port_unlock(p_ptr);
  905. tipc_net_route_msg(buf);
  906. }
  907. /*
  908. * tipc_createport(): user level call. Will add port to
  909. * registry if non-zero user_ref.
  910. */
  911. int tipc_createport(u32 user_ref,
  912. void *usr_handle,
  913. unsigned int importance,
  914. tipc_msg_err_event error_cb,
  915. tipc_named_msg_err_event named_error_cb,
  916. tipc_conn_shutdown_event conn_error_cb,
  917. tipc_msg_event msg_cb,
  918. tipc_named_msg_event named_msg_cb,
  919. tipc_conn_msg_event conn_msg_cb,
  920. tipc_continue_event continue_event_cb,/* May be zero */
  921. u32 *portref)
  922. {
  923. struct user_port *up_ptr;
  924. struct port *p_ptr;
  925. u32 ref;
  926. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  927. if (!up_ptr) {
  928. warn("Port creation failed, no memory\n");
  929. return -ENOMEM;
  930. }
  931. ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
  932. p_ptr = tipc_port_lock(ref);
  933. if (!p_ptr) {
  934. kfree(up_ptr);
  935. return -ENOMEM;
  936. }
  937. p_ptr->user_port = up_ptr;
  938. up_ptr->user_ref = user_ref;
  939. up_ptr->usr_handle = usr_handle;
  940. up_ptr->ref = p_ptr->publ.ref;
  941. up_ptr->err_cb = error_cb;
  942. up_ptr->named_err_cb = named_error_cb;
  943. up_ptr->conn_err_cb = conn_error_cb;
  944. up_ptr->msg_cb = msg_cb;
  945. up_ptr->named_msg_cb = named_msg_cb;
  946. up_ptr->conn_msg_cb = conn_msg_cb;
  947. up_ptr->continue_event_cb = continue_event_cb;
  948. INIT_LIST_HEAD(&up_ptr->uport_list);
  949. tipc_reg_add_port(up_ptr);
  950. *portref = p_ptr->publ.ref;
  951. dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
  952. tipc_port_unlock(p_ptr);
  953. return TIPC_OK;
  954. }
  955. int tipc_ownidentity(u32 ref, struct tipc_portid *id)
  956. {
  957. id->ref = ref;
  958. id->node = tipc_own_addr;
  959. return TIPC_OK;
  960. }
  961. int tipc_portimportance(u32 ref, unsigned int *importance)
  962. {
  963. struct port *p_ptr;
  964. p_ptr = tipc_port_lock(ref);
  965. if (!p_ptr)
  966. return -EINVAL;
  967. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  968. spin_unlock_bh(p_ptr->publ.lock);
  969. return TIPC_OK;
  970. }
  971. int tipc_set_portimportance(u32 ref, unsigned int imp)
  972. {
  973. struct port *p_ptr;
  974. if (imp > TIPC_CRITICAL_IMPORTANCE)
  975. return -EINVAL;
  976. p_ptr = tipc_port_lock(ref);
  977. if (!p_ptr)
  978. return -EINVAL;
  979. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  980. spin_unlock_bh(p_ptr->publ.lock);
  981. return TIPC_OK;
  982. }
  983. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  984. {
  985. struct port *p_ptr;
  986. struct publication *publ;
  987. u32 key;
  988. int res = -EINVAL;
  989. p_ptr = tipc_port_lock(ref);
  990. if (!p_ptr)
  991. return -EINVAL;
  992. dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
  993. "lower = %u, upper = %u\n",
  994. ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
  995. if (p_ptr->publ.connected)
  996. goto exit;
  997. if (seq->lower > seq->upper)
  998. goto exit;
  999. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  1000. goto exit;
  1001. key = ref + p_ptr->pub_count + 1;
  1002. if (key == ref) {
  1003. res = -EADDRINUSE;
  1004. goto exit;
  1005. }
  1006. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  1007. scope, p_ptr->publ.ref, key);
  1008. if (publ) {
  1009. list_add(&publ->pport_list, &p_ptr->publications);
  1010. p_ptr->pub_count++;
  1011. p_ptr->publ.published = 1;
  1012. res = TIPC_OK;
  1013. }
  1014. exit:
  1015. tipc_port_unlock(p_ptr);
  1016. return res;
  1017. }
  1018. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  1019. {
  1020. struct port *p_ptr;
  1021. struct publication *publ;
  1022. struct publication *tpubl;
  1023. int res = -EINVAL;
  1024. p_ptr = tipc_port_lock(ref);
  1025. if (!p_ptr)
  1026. return -EINVAL;
  1027. if (!seq) {
  1028. list_for_each_entry_safe(publ, tpubl,
  1029. &p_ptr->publications, pport_list) {
  1030. tipc_nametbl_withdraw(publ->type, publ->lower,
  1031. publ->ref, publ->key);
  1032. }
  1033. res = TIPC_OK;
  1034. } else {
  1035. list_for_each_entry_safe(publ, tpubl,
  1036. &p_ptr->publications, pport_list) {
  1037. if (publ->scope != scope)
  1038. continue;
  1039. if (publ->type != seq->type)
  1040. continue;
  1041. if (publ->lower != seq->lower)
  1042. continue;
  1043. if (publ->upper != seq->upper)
  1044. break;
  1045. tipc_nametbl_withdraw(publ->type, publ->lower,
  1046. publ->ref, publ->key);
  1047. res = TIPC_OK;
  1048. break;
  1049. }
  1050. }
  1051. if (list_empty(&p_ptr->publications))
  1052. p_ptr->publ.published = 0;
  1053. tipc_port_unlock(p_ptr);
  1054. return res;
  1055. }
  1056. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  1057. {
  1058. struct port *p_ptr;
  1059. struct tipc_msg *msg;
  1060. int res = -EINVAL;
  1061. p_ptr = tipc_port_lock(ref);
  1062. if (!p_ptr)
  1063. return -EINVAL;
  1064. if (p_ptr->publ.published || p_ptr->publ.connected)
  1065. goto exit;
  1066. if (!peer->ref)
  1067. goto exit;
  1068. msg = &p_ptr->publ.phdr;
  1069. msg_set_destnode(msg, peer->node);
  1070. msg_set_destport(msg, peer->ref);
  1071. msg_set_orignode(msg, tipc_own_addr);
  1072. msg_set_origport(msg, p_ptr->publ.ref);
  1073. msg_set_transp_seqno(msg, 42);
  1074. msg_set_type(msg, TIPC_CONN_MSG);
  1075. if (!may_route(peer->node))
  1076. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1077. else
  1078. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1079. p_ptr->probing_interval = PROBING_INTERVAL;
  1080. p_ptr->probing_state = CONFIRMED;
  1081. p_ptr->publ.connected = 1;
  1082. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  1083. tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
  1084. (void *)(unsigned long)ref,
  1085. (net_ev_handler)port_handle_node_down);
  1086. res = TIPC_OK;
  1087. exit:
  1088. tipc_port_unlock(p_ptr);
  1089. p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  1090. return res;
  1091. }
  1092. /*
  1093. * tipc_disconnect(): Disconnect port form peer.
  1094. * This is a node local operation.
  1095. */
  1096. int tipc_disconnect(u32 ref)
  1097. {
  1098. struct port *p_ptr;
  1099. int res = -ENOTCONN;
  1100. p_ptr = tipc_port_lock(ref);
  1101. if (!p_ptr)
  1102. return -EINVAL;
  1103. if (p_ptr->publ.connected) {
  1104. p_ptr->publ.connected = 0;
  1105. /* let timer expire on it's own to avoid deadlock! */
  1106. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  1107. res = TIPC_OK;
  1108. }
  1109. tipc_port_unlock(p_ptr);
  1110. return res;
  1111. }
  1112. /*
  1113. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1114. */
  1115. int tipc_shutdown(u32 ref)
  1116. {
  1117. struct port *p_ptr;
  1118. struct sk_buff *buf = NULL;
  1119. p_ptr = tipc_port_lock(ref);
  1120. if (!p_ptr)
  1121. return -EINVAL;
  1122. if (p_ptr->publ.connected) {
  1123. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1124. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1125. imp++;
  1126. buf = port_build_proto_msg(port_peerport(p_ptr),
  1127. port_peernode(p_ptr),
  1128. ref,
  1129. tipc_own_addr,
  1130. imp,
  1131. TIPC_CONN_MSG,
  1132. TIPC_CONN_SHUTDOWN,
  1133. port_out_seqno(p_ptr),
  1134. 0);
  1135. }
  1136. tipc_port_unlock(p_ptr);
  1137. tipc_net_route_msg(buf);
  1138. return tipc_disconnect(ref);
  1139. }
  1140. int tipc_isconnected(u32 ref, int *isconnected)
  1141. {
  1142. struct port *p_ptr;
  1143. p_ptr = tipc_port_lock(ref);
  1144. if (!p_ptr)
  1145. return -EINVAL;
  1146. *isconnected = p_ptr->publ.connected;
  1147. tipc_port_unlock(p_ptr);
  1148. return TIPC_OK;
  1149. }
  1150. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1151. {
  1152. struct port *p_ptr;
  1153. int res;
  1154. p_ptr = tipc_port_lock(ref);
  1155. if (!p_ptr)
  1156. return -EINVAL;
  1157. if (p_ptr->publ.connected) {
  1158. peer->ref = port_peerport(p_ptr);
  1159. peer->node = port_peernode(p_ptr);
  1160. res = TIPC_OK;
  1161. } else
  1162. res = -ENOTCONN;
  1163. tipc_port_unlock(p_ptr);
  1164. return res;
  1165. }
  1166. int tipc_ref_valid(u32 ref)
  1167. {
  1168. /* Works irrespective of type */
  1169. return !!tipc_ref_deref(ref);
  1170. }
  1171. /*
  1172. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1173. * message for this node.
  1174. */
  1175. int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1176. struct iovec const *msg_sect)
  1177. {
  1178. struct sk_buff *buf;
  1179. int res;
  1180. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1181. MAX_MSG_SIZE, !sender->user_port, &buf);
  1182. if (likely(buf))
  1183. tipc_port_recv_msg(buf);
  1184. return res;
  1185. }
  1186. /**
  1187. * tipc_send - send message sections on connection
  1188. */
  1189. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1190. {
  1191. struct port *p_ptr;
  1192. u32 destnode;
  1193. int res;
  1194. p_ptr = tipc_port_deref(ref);
  1195. if (!p_ptr || !p_ptr->publ.connected)
  1196. return -EINVAL;
  1197. p_ptr->publ.congested = 1;
  1198. if (!tipc_port_congested(p_ptr)) {
  1199. destnode = port_peernode(p_ptr);
  1200. if (likely(destnode != tipc_own_addr))
  1201. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1202. destnode);
  1203. else
  1204. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1205. if (likely(res != -ELINKCONG)) {
  1206. port_incr_out_seqno(p_ptr);
  1207. p_ptr->publ.congested = 0;
  1208. p_ptr->sent++;
  1209. return res;
  1210. }
  1211. }
  1212. if (port_unreliable(p_ptr)) {
  1213. p_ptr->publ.congested = 0;
  1214. /* Just calculate msg length and return */
  1215. return msg_calc_data_size(msg_sect, num_sect);
  1216. }
  1217. return -ELINKCONG;
  1218. }
  1219. /**
  1220. * tipc_send_buf - send message buffer on connection
  1221. */
  1222. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1223. {
  1224. struct port *p_ptr;
  1225. struct tipc_msg *msg;
  1226. u32 destnode;
  1227. u32 hsz;
  1228. u32 sz;
  1229. u32 res;
  1230. p_ptr = tipc_port_deref(ref);
  1231. if (!p_ptr || !p_ptr->publ.connected)
  1232. return -EINVAL;
  1233. msg = &p_ptr->publ.phdr;
  1234. hsz = msg_hdr_sz(msg);
  1235. sz = hsz + dsz;
  1236. msg_set_size(msg, sz);
  1237. if (skb_cow(buf, hsz))
  1238. return -ENOMEM;
  1239. skb_push(buf, hsz);
  1240. skb_copy_to_linear_data(buf, msg, hsz);
  1241. destnode = msg_destnode(msg);
  1242. p_ptr->publ.congested = 1;
  1243. if (!tipc_port_congested(p_ptr)) {
  1244. if (likely(destnode != tipc_own_addr))
  1245. res = tipc_send_buf_fast(buf, destnode);
  1246. else {
  1247. tipc_port_recv_msg(buf);
  1248. res = sz;
  1249. }
  1250. if (likely(res != -ELINKCONG)) {
  1251. port_incr_out_seqno(p_ptr);
  1252. p_ptr->sent++;
  1253. p_ptr->publ.congested = 0;
  1254. return res;
  1255. }
  1256. }
  1257. if (port_unreliable(p_ptr)) {
  1258. p_ptr->publ.congested = 0;
  1259. return dsz;
  1260. }
  1261. return -ELINKCONG;
  1262. }
  1263. /**
  1264. * tipc_forward2name - forward message sections to port name
  1265. */
  1266. int tipc_forward2name(u32 ref,
  1267. struct tipc_name const *name,
  1268. u32 domain,
  1269. u32 num_sect,
  1270. struct iovec const *msg_sect,
  1271. struct tipc_portid const *orig,
  1272. unsigned int importance)
  1273. {
  1274. struct port *p_ptr;
  1275. struct tipc_msg *msg;
  1276. u32 destnode = domain;
  1277. u32 destport = 0;
  1278. int res;
  1279. p_ptr = tipc_port_deref(ref);
  1280. if (!p_ptr || p_ptr->publ.connected)
  1281. return -EINVAL;
  1282. msg = &p_ptr->publ.phdr;
  1283. msg_set_type(msg, TIPC_NAMED_MSG);
  1284. msg_set_orignode(msg, orig->node);
  1285. msg_set_origport(msg, orig->ref);
  1286. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1287. msg_set_nametype(msg, name->type);
  1288. msg_set_nameinst(msg, name->instance);
  1289. msg_set_lookup_scope(msg, addr_scope(domain));
  1290. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1291. msg_set_importance(msg,importance);
  1292. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1293. msg_set_destnode(msg, destnode);
  1294. msg_set_destport(msg, destport);
  1295. if (likely(destport || destnode)) {
  1296. p_ptr->sent++;
  1297. if (likely(destnode == tipc_own_addr))
  1298. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1299. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1300. destnode);
  1301. if (likely(res != -ELINKCONG))
  1302. return res;
  1303. if (port_unreliable(p_ptr)) {
  1304. /* Just calculate msg length and return */
  1305. return msg_calc_data_size(msg_sect, num_sect);
  1306. }
  1307. return -ELINKCONG;
  1308. }
  1309. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1310. TIPC_ERR_NO_NAME);
  1311. }
  1312. /**
  1313. * tipc_send2name - send message sections to port name
  1314. */
  1315. int tipc_send2name(u32 ref,
  1316. struct tipc_name const *name,
  1317. unsigned int domain,
  1318. unsigned int num_sect,
  1319. struct iovec const *msg_sect)
  1320. {
  1321. struct tipc_portid orig;
  1322. orig.ref = ref;
  1323. orig.node = tipc_own_addr;
  1324. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1325. TIPC_PORT_IMPORTANCE);
  1326. }
  1327. /**
  1328. * tipc_forward_buf2name - forward message buffer to port name
  1329. */
  1330. int tipc_forward_buf2name(u32 ref,
  1331. struct tipc_name const *name,
  1332. u32 domain,
  1333. struct sk_buff *buf,
  1334. unsigned int dsz,
  1335. struct tipc_portid const *orig,
  1336. unsigned int importance)
  1337. {
  1338. struct port *p_ptr;
  1339. struct tipc_msg *msg;
  1340. u32 destnode = domain;
  1341. u32 destport = 0;
  1342. int res;
  1343. p_ptr = (struct port *)tipc_ref_deref(ref);
  1344. if (!p_ptr || p_ptr->publ.connected)
  1345. return -EINVAL;
  1346. msg = &p_ptr->publ.phdr;
  1347. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1348. msg_set_importance(msg, importance);
  1349. msg_set_type(msg, TIPC_NAMED_MSG);
  1350. msg_set_orignode(msg, orig->node);
  1351. msg_set_origport(msg, orig->ref);
  1352. msg_set_nametype(msg, name->type);
  1353. msg_set_nameinst(msg, name->instance);
  1354. msg_set_lookup_scope(msg, addr_scope(domain));
  1355. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1356. msg_set_size(msg, LONG_H_SIZE + dsz);
  1357. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1358. msg_set_destnode(msg, destnode);
  1359. msg_set_destport(msg, destport);
  1360. msg_dbg(msg, "forw2name ==> ");
  1361. if (skb_cow(buf, LONG_H_SIZE))
  1362. return -ENOMEM;
  1363. skb_push(buf, LONG_H_SIZE);
  1364. skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
  1365. msg_dbg(buf_msg(buf),"PREP:");
  1366. if (likely(destport || destnode)) {
  1367. p_ptr->sent++;
  1368. if (destnode == tipc_own_addr)
  1369. return tipc_port_recv_msg(buf);
  1370. res = tipc_send_buf_fast(buf, destnode);
  1371. if (likely(res != -ELINKCONG))
  1372. return res;
  1373. if (port_unreliable(p_ptr))
  1374. return dsz;
  1375. return -ELINKCONG;
  1376. }
  1377. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1378. }
  1379. /**
  1380. * tipc_send_buf2name - send message buffer to port name
  1381. */
  1382. int tipc_send_buf2name(u32 ref,
  1383. struct tipc_name const *dest,
  1384. u32 domain,
  1385. struct sk_buff *buf,
  1386. unsigned int dsz)
  1387. {
  1388. struct tipc_portid orig;
  1389. orig.ref = ref;
  1390. orig.node = tipc_own_addr;
  1391. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1392. TIPC_PORT_IMPORTANCE);
  1393. }
  1394. /**
  1395. * tipc_forward2port - forward message sections to port identity
  1396. */
  1397. int tipc_forward2port(u32 ref,
  1398. struct tipc_portid const *dest,
  1399. unsigned int num_sect,
  1400. struct iovec const *msg_sect,
  1401. struct tipc_portid const *orig,
  1402. unsigned int importance)
  1403. {
  1404. struct port *p_ptr;
  1405. struct tipc_msg *msg;
  1406. int res;
  1407. p_ptr = tipc_port_deref(ref);
  1408. if (!p_ptr || p_ptr->publ.connected)
  1409. return -EINVAL;
  1410. msg = &p_ptr->publ.phdr;
  1411. msg_set_type(msg, TIPC_DIRECT_MSG);
  1412. msg_set_orignode(msg, orig->node);
  1413. msg_set_origport(msg, orig->ref);
  1414. msg_set_destnode(msg, dest->node);
  1415. msg_set_destport(msg, dest->ref);
  1416. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1417. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1418. msg_set_importance(msg, importance);
  1419. p_ptr->sent++;
  1420. if (dest->node == tipc_own_addr)
  1421. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1422. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1423. if (likely(res != -ELINKCONG))
  1424. return res;
  1425. if (port_unreliable(p_ptr)) {
  1426. /* Just calculate msg length and return */
  1427. return msg_calc_data_size(msg_sect, num_sect);
  1428. }
  1429. return -ELINKCONG;
  1430. }
  1431. /**
  1432. * tipc_send2port - send message sections to port identity
  1433. */
  1434. int tipc_send2port(u32 ref,
  1435. struct tipc_portid const *dest,
  1436. unsigned int num_sect,
  1437. struct iovec const *msg_sect)
  1438. {
  1439. struct tipc_portid orig;
  1440. orig.ref = ref;
  1441. orig.node = tipc_own_addr;
  1442. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1443. TIPC_PORT_IMPORTANCE);
  1444. }
  1445. /**
  1446. * tipc_forward_buf2port - forward message buffer to port identity
  1447. */
  1448. int tipc_forward_buf2port(u32 ref,
  1449. struct tipc_portid const *dest,
  1450. struct sk_buff *buf,
  1451. unsigned int dsz,
  1452. struct tipc_portid const *orig,
  1453. unsigned int importance)
  1454. {
  1455. struct port *p_ptr;
  1456. struct tipc_msg *msg;
  1457. int res;
  1458. p_ptr = (struct port *)tipc_ref_deref(ref);
  1459. if (!p_ptr || p_ptr->publ.connected)
  1460. return -EINVAL;
  1461. msg = &p_ptr->publ.phdr;
  1462. msg_set_type(msg, TIPC_DIRECT_MSG);
  1463. msg_set_orignode(msg, orig->node);
  1464. msg_set_origport(msg, orig->ref);
  1465. msg_set_destnode(msg, dest->node);
  1466. msg_set_destport(msg, dest->ref);
  1467. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1468. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1469. msg_set_importance(msg, importance);
  1470. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1471. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1472. return -ENOMEM;
  1473. skb_push(buf, DIR_MSG_H_SIZE);
  1474. skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
  1475. msg_dbg(msg, "buf2port: ");
  1476. p_ptr->sent++;
  1477. if (dest->node == tipc_own_addr)
  1478. return tipc_port_recv_msg(buf);
  1479. res = tipc_send_buf_fast(buf, dest->node);
  1480. if (likely(res != -ELINKCONG))
  1481. return res;
  1482. if (port_unreliable(p_ptr))
  1483. return dsz;
  1484. return -ELINKCONG;
  1485. }
  1486. /**
  1487. * tipc_send_buf2port - send message buffer to port identity
  1488. */
  1489. int tipc_send_buf2port(u32 ref,
  1490. struct tipc_portid const *dest,
  1491. struct sk_buff *buf,
  1492. unsigned int dsz)
  1493. {
  1494. struct tipc_portid orig;
  1495. orig.ref = ref;
  1496. orig.node = tipc_own_addr;
  1497. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1498. TIPC_PORT_IMPORTANCE);
  1499. }