port.c 41 KB

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