port.c 41 KB

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