port.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. /*
  2. * net/tipc/port.c: TIPC port code
  3. *
  4. * Copyright (c) 1992-2007, Ericsson AB
  5. * Copyright (c) 2004-2007, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "config.h"
  38. #include "dbg.h"
  39. #include "port.h"
  40. #include "addr.h"
  41. #include "link.h"
  42. #include "node.h"
  43. #include "name_table.h"
  44. #include "user_reg.h"
  45. #include "msg.h"
  46. #include "bcast.h"
  47. /* Connection management: */
  48. #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
  49. #define CONFIRMED 0
  50. #define PROBING 1
  51. #define MAX_REJECT_SIZE 1024
  52. static struct sk_buff *msg_queue_head = NULL;
  53. static struct sk_buff *msg_queue_tail = NULL;
  54. DEFINE_SPINLOCK(tipc_port_list_lock);
  55. static DEFINE_SPINLOCK(queue_lock);
  56. static LIST_HEAD(ports);
  57. static void port_handle_node_down(unsigned long ref);
  58. static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
  59. static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
  60. static void port_timeout(unsigned long ref);
  61. static u32 port_peernode(struct port *p_ptr)
  62. {
  63. return msg_destnode(&p_ptr->publ.phdr);
  64. }
  65. static u32 port_peerport(struct port *p_ptr)
  66. {
  67. return msg_destport(&p_ptr->publ.phdr);
  68. }
  69. static u32 port_out_seqno(struct port *p_ptr)
  70. {
  71. return msg_transp_seqno(&p_ptr->publ.phdr);
  72. }
  73. static void port_incr_out_seqno(struct port *p_ptr)
  74. {
  75. struct tipc_msg *m = &p_ptr->publ.phdr;
  76. if (likely(!msg_routed(m)))
  77. return;
  78. msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
  79. }
  80. /**
  81. * tipc_multicast - send a multicast message to local and remote destinations
  82. */
  83. int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
  84. u32 num_sect, struct iovec const *msg_sect)
  85. {
  86. struct tipc_msg *hdr;
  87. struct sk_buff *buf;
  88. struct sk_buff *ibuf = NULL;
  89. struct port_list dports = {0, NULL, };
  90. struct port *oport = tipc_port_deref(ref);
  91. int ext_targets;
  92. int res;
  93. if (unlikely(!oport))
  94. return -EINVAL;
  95. /* Create multicast message */
  96. hdr = &oport->publ.phdr;
  97. msg_set_type(hdr, TIPC_MCAST_MSG);
  98. msg_set_nametype(hdr, seq->type);
  99. msg_set_namelower(hdr, seq->lower);
  100. msg_set_nameupper(hdr, seq->upper);
  101. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  102. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  103. !oport->user_port, &buf);
  104. if (unlikely(!buf))
  105. return res;
  106. /* Figure out where to send multicast message */
  107. ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
  108. TIPC_NODE_SCOPE, &dports);
  109. /* Send message to destinations (duplicate it only if necessary) */
  110. if (ext_targets) {
  111. if (dports.count != 0) {
  112. ibuf = skb_copy(buf, GFP_ATOMIC);
  113. if (ibuf == NULL) {
  114. tipc_port_list_free(&dports);
  115. buf_discard(buf);
  116. return -ENOMEM;
  117. }
  118. }
  119. res = tipc_bclink_send_msg(buf);
  120. if ((res < 0) && (dports.count != 0)) {
  121. buf_discard(ibuf);
  122. }
  123. } else {
  124. ibuf = buf;
  125. }
  126. if (res >= 0) {
  127. if (ibuf)
  128. tipc_port_recv_mcast(ibuf, &dports);
  129. } else {
  130. tipc_port_list_free(&dports);
  131. }
  132. return res;
  133. }
  134. /**
  135. * tipc_port_recv_mcast - deliver multicast message to all destination ports
  136. *
  137. * If there is no port list, perform a lookup to create one
  138. */
  139. void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
  140. {
  141. struct tipc_msg* msg;
  142. struct port_list dports = {0, NULL, };
  143. struct port_list *item = dp;
  144. int cnt = 0;
  145. msg = buf_msg(buf);
  146. /* Create destination port list, if one wasn't supplied */
  147. if (dp == NULL) {
  148. tipc_nametbl_mc_translate(msg_nametype(msg),
  149. msg_namelower(msg),
  150. msg_nameupper(msg),
  151. TIPC_CLUSTER_SCOPE,
  152. &dports);
  153. item = dp = &dports;
  154. }
  155. /* Deliver a copy of message to each destination port */
  156. if (dp->count != 0) {
  157. if (dp->count == 1) {
  158. msg_set_destport(msg, dp->ports[0]);
  159. tipc_port_recv_msg(buf);
  160. tipc_port_list_free(dp);
  161. return;
  162. }
  163. for (; cnt < dp->count; cnt++) {
  164. int index = cnt % PLSIZE;
  165. struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
  166. if (b == NULL) {
  167. warn("Unable to deliver multicast message(s)\n");
  168. msg_dbg(msg, "LOST:");
  169. goto exit;
  170. }
  171. if ((index == 0) && (cnt != 0)) {
  172. item = item->next;
  173. }
  174. msg_set_destport(buf_msg(b),item->ports[index]);
  175. tipc_port_recv_msg(b);
  176. }
  177. }
  178. exit:
  179. buf_discard(buf);
  180. tipc_port_list_free(dp);
  181. }
  182. /**
  183. * tipc_createport_raw - create a generic TIPC port
  184. *
  185. * Returns port reference, or 0 if unable to create it
  186. *
  187. * Note: The newly created port is returned in the locked state.
  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. struct tipc_port **tp_ptr)
  194. {
  195. struct port *p_ptr;
  196. struct tipc_msg *msg;
  197. u32 ref;
  198. p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
  199. if (!p_ptr) {
  200. warn("Port creation failed, no memory\n");
  201. return 0;
  202. }
  203. ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
  204. if (!ref) {
  205. warn("Port creation failed, reference table exhausted\n");
  206. kfree(p_ptr);
  207. return 0;
  208. }
  209. p_ptr->publ.usr_handle = usr_handle;
  210. p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
  211. p_ptr->publ.ref = ref;
  212. msg = &p_ptr->publ.phdr;
  213. msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE,
  214. 0);
  215. msg_set_orignode(msg, tipc_own_addr);
  216. msg_set_prevnode(msg, tipc_own_addr);
  217. msg_set_origport(msg, ref);
  218. msg_set_importance(msg,importance);
  219. p_ptr->last_in_seqno = 41;
  220. p_ptr->sent = 1;
  221. INIT_LIST_HEAD(&p_ptr->wait_list);
  222. INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
  223. p_ptr->congested_link = NULL;
  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. *tp_ptr = &p_ptr->publ;
  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. tipc_port_unlock(p_ptr);
  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. tipc_port_unlock(p_ptr);
  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_reject_msg(struct sk_buff *buf, u32 err)
  363. {
  364. struct tipc_msg *msg = buf_msg(buf);
  365. struct sk_buff *rbuf;
  366. struct tipc_msg *rmsg;
  367. int hdr_sz;
  368. u32 imp = msg_importance(msg);
  369. u32 data_sz = msg_data_sz(msg);
  370. if (data_sz > MAX_REJECT_SIZE)
  371. data_sz = MAX_REJECT_SIZE;
  372. if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
  373. imp++;
  374. msg_dbg(msg, "port->rej: ");
  375. /* discard rejected message if it shouldn't be returned to sender */
  376. if (msg_errcode(msg) || msg_dest_droppable(msg)) {
  377. buf_discard(buf);
  378. return data_sz;
  379. }
  380. /* construct rejected message */
  381. if (msg_mcast(msg))
  382. hdr_sz = MCAST_H_SIZE;
  383. else
  384. hdr_sz = LONG_H_SIZE;
  385. rbuf = buf_acquire(data_sz + hdr_sz);
  386. if (rbuf == NULL) {
  387. buf_discard(buf);
  388. return data_sz;
  389. }
  390. rmsg = buf_msg(rbuf);
  391. msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
  392. msg_set_destport(rmsg, msg_origport(msg));
  393. msg_set_prevnode(rmsg, tipc_own_addr);
  394. msg_set_origport(rmsg, msg_destport(msg));
  395. if (msg_short(msg))
  396. msg_set_orignode(rmsg, tipc_own_addr);
  397. else
  398. msg_set_orignode(rmsg, msg_destnode(msg));
  399. msg_set_size(rmsg, data_sz + hdr_sz);
  400. msg_set_nametype(rmsg, msg_nametype(msg));
  401. msg_set_nameinst(rmsg, msg_nameinst(msg));
  402. skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
  403. /* send self-abort message when rejecting on a connected port */
  404. if (msg_connected(msg)) {
  405. struct sk_buff *abuf = NULL;
  406. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  407. if (p_ptr) {
  408. if (p_ptr->publ.connected)
  409. abuf = port_build_self_abort_msg(p_ptr, err);
  410. tipc_port_unlock(p_ptr);
  411. }
  412. tipc_net_route_msg(abuf);
  413. }
  414. /* send rejected message */
  415. buf_discard(buf);
  416. tipc_net_route_msg(rbuf);
  417. return data_sz;
  418. }
  419. int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
  420. struct iovec const *msg_sect, u32 num_sect,
  421. int err)
  422. {
  423. struct sk_buff *buf;
  424. int res;
  425. res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
  426. !p_ptr->user_port, &buf);
  427. if (!buf)
  428. return res;
  429. return tipc_reject_msg(buf, err);
  430. }
  431. static void port_timeout(unsigned long ref)
  432. {
  433. struct port *p_ptr = tipc_port_lock(ref);
  434. struct sk_buff *buf = NULL;
  435. if (!p_ptr)
  436. return;
  437. if (!p_ptr->publ.connected) {
  438. tipc_port_unlock(p_ptr);
  439. return;
  440. }
  441. /* Last probe answered ? */
  442. if (p_ptr->probing_state == PROBING) {
  443. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  444. } else {
  445. buf = port_build_proto_msg(port_peerport(p_ptr),
  446. port_peernode(p_ptr),
  447. p_ptr->publ.ref,
  448. tipc_own_addr,
  449. CONN_MANAGER,
  450. CONN_PROBE,
  451. TIPC_OK,
  452. port_out_seqno(p_ptr),
  453. 0);
  454. port_incr_out_seqno(p_ptr);
  455. p_ptr->probing_state = PROBING;
  456. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  457. }
  458. tipc_port_unlock(p_ptr);
  459. tipc_net_route_msg(buf);
  460. }
  461. static void port_handle_node_down(unsigned long ref)
  462. {
  463. struct port *p_ptr = tipc_port_lock(ref);
  464. struct sk_buff* buf = NULL;
  465. if (!p_ptr)
  466. return;
  467. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  468. tipc_port_unlock(p_ptr);
  469. tipc_net_route_msg(buf);
  470. }
  471. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  472. {
  473. u32 imp = msg_importance(&p_ptr->publ.phdr);
  474. if (!p_ptr->publ.connected)
  475. return NULL;
  476. if (imp < TIPC_CRITICAL_IMPORTANCE)
  477. imp++;
  478. return port_build_proto_msg(p_ptr->publ.ref,
  479. tipc_own_addr,
  480. port_peerport(p_ptr),
  481. port_peernode(p_ptr),
  482. imp,
  483. TIPC_CONN_MSG,
  484. err,
  485. p_ptr->last_in_seqno + 1,
  486. 0);
  487. }
  488. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  489. {
  490. u32 imp = msg_importance(&p_ptr->publ.phdr);
  491. if (!p_ptr->publ.connected)
  492. return NULL;
  493. if (imp < TIPC_CRITICAL_IMPORTANCE)
  494. imp++;
  495. return port_build_proto_msg(port_peerport(p_ptr),
  496. port_peernode(p_ptr),
  497. p_ptr->publ.ref,
  498. tipc_own_addr,
  499. imp,
  500. TIPC_CONN_MSG,
  501. err,
  502. port_out_seqno(p_ptr),
  503. 0);
  504. }
  505. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  506. {
  507. struct tipc_msg *msg = buf_msg(buf);
  508. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  509. u32 err = TIPC_OK;
  510. struct sk_buff *r_buf = NULL;
  511. struct sk_buff *abort_buf = NULL;
  512. msg_dbg(msg, "PORT<RECV<:");
  513. if (!p_ptr) {
  514. err = TIPC_ERR_NO_PORT;
  515. } else if (p_ptr->publ.connected) {
  516. if (port_peernode(p_ptr) != msg_orignode(msg))
  517. err = TIPC_ERR_NO_PORT;
  518. if (port_peerport(p_ptr) != msg_origport(msg))
  519. err = TIPC_ERR_NO_PORT;
  520. if (!err && msg_routed(msg)) {
  521. u32 seqno = msg_transp_seqno(msg);
  522. u32 myno = ++p_ptr->last_in_seqno;
  523. if (seqno != myno) {
  524. err = TIPC_ERR_NO_PORT;
  525. abort_buf = port_build_self_abort_msg(p_ptr, err);
  526. }
  527. }
  528. if (msg_type(msg) == CONN_ACK) {
  529. int wakeup = tipc_port_congested(p_ptr) &&
  530. p_ptr->publ.congested &&
  531. p_ptr->wakeup;
  532. p_ptr->acked += msg_msgcnt(msg);
  533. if (tipc_port_congested(p_ptr))
  534. goto exit;
  535. p_ptr->publ.congested = 0;
  536. if (!wakeup)
  537. goto exit;
  538. p_ptr->wakeup(&p_ptr->publ);
  539. goto exit;
  540. }
  541. } else if (p_ptr->publ.published) {
  542. err = TIPC_ERR_NO_PORT;
  543. }
  544. if (err) {
  545. r_buf = port_build_proto_msg(msg_origport(msg),
  546. msg_orignode(msg),
  547. msg_destport(msg),
  548. tipc_own_addr,
  549. TIPC_HIGH_IMPORTANCE,
  550. TIPC_CONN_MSG,
  551. err,
  552. 0,
  553. 0);
  554. goto exit;
  555. }
  556. /* All is fine */
  557. if (msg_type(msg) == CONN_PROBE) {
  558. r_buf = port_build_proto_msg(msg_origport(msg),
  559. msg_orignode(msg),
  560. msg_destport(msg),
  561. tipc_own_addr,
  562. CONN_MANAGER,
  563. CONN_PROBE_REPLY,
  564. TIPC_OK,
  565. port_out_seqno(p_ptr),
  566. 0);
  567. }
  568. p_ptr->probing_state = CONFIRMED;
  569. port_incr_out_seqno(p_ptr);
  570. exit:
  571. if (p_ptr)
  572. tipc_port_unlock(p_ptr);
  573. tipc_net_route_msg(r_buf);
  574. tipc_net_route_msg(abort_buf);
  575. buf_discard(buf);
  576. }
  577. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  578. {
  579. struct publication *publ;
  580. if (full_id)
  581. tipc_printf(buf, "<%u.%u.%u:%u>:",
  582. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  583. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  584. else
  585. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  586. if (p_ptr->publ.connected) {
  587. u32 dport = port_peerport(p_ptr);
  588. u32 destnode = port_peernode(p_ptr);
  589. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  590. tipc_zone(destnode), tipc_cluster(destnode),
  591. tipc_node(destnode), dport);
  592. if (p_ptr->publ.conn_type != 0)
  593. tipc_printf(buf, " via {%u,%u}",
  594. p_ptr->publ.conn_type,
  595. p_ptr->publ.conn_instance);
  596. }
  597. else if (p_ptr->publ.published) {
  598. tipc_printf(buf, " bound to");
  599. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  600. if (publ->lower == publ->upper)
  601. tipc_printf(buf, " {%u,%u}", publ->type,
  602. publ->lower);
  603. else
  604. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  605. publ->lower, publ->upper);
  606. }
  607. }
  608. tipc_printf(buf, "\n");
  609. }
  610. #define MAX_PORT_QUERY 32768
  611. struct sk_buff *tipc_port_get_ports(void)
  612. {
  613. struct sk_buff *buf;
  614. struct tlv_desc *rep_tlv;
  615. struct print_buf pb;
  616. struct port *p_ptr;
  617. int str_len;
  618. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  619. if (!buf)
  620. return NULL;
  621. rep_tlv = (struct tlv_desc *)buf->data;
  622. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  623. spin_lock_bh(&tipc_port_list_lock);
  624. list_for_each_entry(p_ptr, &ports, port_list) {
  625. spin_lock_bh(p_ptr->publ.lock);
  626. port_print(p_ptr, &pb, 0);
  627. spin_unlock_bh(p_ptr->publ.lock);
  628. }
  629. spin_unlock_bh(&tipc_port_list_lock);
  630. str_len = tipc_printbuf_validate(&pb);
  631. skb_put(buf, TLV_SPACE(str_len));
  632. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  633. return buf;
  634. }
  635. #if 0
  636. #define MAX_PORT_STATS 2000
  637. struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
  638. {
  639. u32 ref;
  640. struct port *p_ptr;
  641. struct sk_buff *buf;
  642. struct tlv_desc *rep_tlv;
  643. struct print_buf pb;
  644. int str_len;
  645. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
  646. return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  647. ref = *(u32 *)TLV_DATA(req_tlv_area);
  648. ref = ntohl(ref);
  649. p_ptr = tipc_port_lock(ref);
  650. if (!p_ptr)
  651. return cfg_reply_error_string("port not found");
  652. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
  653. if (!buf) {
  654. tipc_port_unlock(p_ptr);
  655. return NULL;
  656. }
  657. rep_tlv = (struct tlv_desc *)buf->data;
  658. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
  659. port_print(p_ptr, &pb, 1);
  660. /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
  661. tipc_port_unlock(p_ptr);
  662. str_len = tipc_printbuf_validate(&pb);
  663. skb_put(buf, TLV_SPACE(str_len));
  664. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  665. return buf;
  666. }
  667. #endif
  668. void tipc_port_reinit(void)
  669. {
  670. struct port *p_ptr;
  671. struct tipc_msg *msg;
  672. spin_lock_bh(&tipc_port_list_lock);
  673. list_for_each_entry(p_ptr, &ports, port_list) {
  674. msg = &p_ptr->publ.phdr;
  675. if (msg_orignode(msg) == tipc_own_addr)
  676. break;
  677. msg_set_orignode(msg, tipc_own_addr);
  678. }
  679. spin_unlock_bh(&tipc_port_list_lock);
  680. }
  681. /*
  682. * port_dispatcher_sigh(): Signal handler for messages destinated
  683. * to the tipc_port interface.
  684. */
  685. static void port_dispatcher_sigh(void *dummy)
  686. {
  687. struct sk_buff *buf;
  688. spin_lock_bh(&queue_lock);
  689. buf = msg_queue_head;
  690. msg_queue_head = NULL;
  691. spin_unlock_bh(&queue_lock);
  692. while (buf) {
  693. struct port *p_ptr;
  694. struct user_port *up_ptr;
  695. struct tipc_portid orig;
  696. struct tipc_name_seq dseq;
  697. void *usr_handle;
  698. int connected;
  699. int published;
  700. u32 message_type;
  701. struct sk_buff *next = buf->next;
  702. struct tipc_msg *msg = buf_msg(buf);
  703. u32 dref = msg_destport(msg);
  704. message_type = msg_type(msg);
  705. if (message_type > TIPC_DIRECT_MSG)
  706. goto reject; /* Unsupported message type */
  707. p_ptr = tipc_port_lock(dref);
  708. if (!p_ptr)
  709. goto reject; /* Port deleted while msg in queue */
  710. orig.ref = msg_origport(msg);
  711. orig.node = msg_orignode(msg);
  712. up_ptr = p_ptr->user_port;
  713. usr_handle = up_ptr->usr_handle;
  714. connected = p_ptr->publ.connected;
  715. published = p_ptr->publ.published;
  716. if (unlikely(msg_errcode(msg)))
  717. goto err;
  718. switch (message_type) {
  719. case TIPC_CONN_MSG:{
  720. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  721. u32 peer_port = port_peerport(p_ptr);
  722. u32 peer_node = port_peernode(p_ptr);
  723. tipc_port_unlock(p_ptr);
  724. if (unlikely(!connected)) {
  725. if (unlikely(published))
  726. goto reject;
  727. tipc_connect2port(dref,&orig);
  728. }
  729. if (unlikely(msg_origport(msg) != peer_port))
  730. goto reject;
  731. if (unlikely(msg_orignode(msg) != peer_node))
  732. goto reject;
  733. if (unlikely(!cb))
  734. goto reject;
  735. if (unlikely(++p_ptr->publ.conn_unacked >=
  736. TIPC_FLOW_CONTROL_WIN))
  737. tipc_acknowledge(dref,
  738. p_ptr->publ.conn_unacked);
  739. skb_pull(buf, msg_hdr_sz(msg));
  740. cb(usr_handle, dref, &buf, msg_data(msg),
  741. msg_data_sz(msg));
  742. break;
  743. }
  744. case TIPC_DIRECT_MSG:{
  745. tipc_msg_event cb = up_ptr->msg_cb;
  746. tipc_port_unlock(p_ptr);
  747. if (unlikely(connected))
  748. goto reject;
  749. if (unlikely(!cb))
  750. goto reject;
  751. skb_pull(buf, msg_hdr_sz(msg));
  752. cb(usr_handle, dref, &buf, msg_data(msg),
  753. msg_data_sz(msg), msg_importance(msg),
  754. &orig);
  755. break;
  756. }
  757. case TIPC_MCAST_MSG:
  758. case TIPC_NAMED_MSG:{
  759. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  760. tipc_port_unlock(p_ptr);
  761. if (unlikely(connected))
  762. goto reject;
  763. if (unlikely(!cb))
  764. goto reject;
  765. if (unlikely(!published))
  766. goto reject;
  767. dseq.type = msg_nametype(msg);
  768. dseq.lower = msg_nameinst(msg);
  769. dseq.upper = (message_type == TIPC_NAMED_MSG)
  770. ? dseq.lower : msg_nameupper(msg);
  771. skb_pull(buf, msg_hdr_sz(msg));
  772. cb(usr_handle, dref, &buf, msg_data(msg),
  773. msg_data_sz(msg), msg_importance(msg),
  774. &orig, &dseq);
  775. break;
  776. }
  777. }
  778. if (buf)
  779. buf_discard(buf);
  780. buf = next;
  781. continue;
  782. err:
  783. switch (message_type) {
  784. case TIPC_CONN_MSG:{
  785. tipc_conn_shutdown_event cb =
  786. up_ptr->conn_err_cb;
  787. u32 peer_port = port_peerport(p_ptr);
  788. u32 peer_node = port_peernode(p_ptr);
  789. tipc_port_unlock(p_ptr);
  790. if (!connected || !cb)
  791. break;
  792. if (msg_origport(msg) != peer_port)
  793. break;
  794. if (msg_orignode(msg) != peer_node)
  795. break;
  796. tipc_disconnect(dref);
  797. skb_pull(buf, msg_hdr_sz(msg));
  798. cb(usr_handle, dref, &buf, msg_data(msg),
  799. msg_data_sz(msg), msg_errcode(msg));
  800. break;
  801. }
  802. case TIPC_DIRECT_MSG:{
  803. tipc_msg_err_event cb = up_ptr->err_cb;
  804. tipc_port_unlock(p_ptr);
  805. if (connected || !cb)
  806. break;
  807. skb_pull(buf, msg_hdr_sz(msg));
  808. cb(usr_handle, dref, &buf, msg_data(msg),
  809. msg_data_sz(msg), msg_errcode(msg), &orig);
  810. break;
  811. }
  812. case TIPC_MCAST_MSG:
  813. case TIPC_NAMED_MSG:{
  814. tipc_named_msg_err_event cb =
  815. up_ptr->named_err_cb;
  816. tipc_port_unlock(p_ptr);
  817. if (connected || !cb)
  818. break;
  819. dseq.type = msg_nametype(msg);
  820. dseq.lower = msg_nameinst(msg);
  821. dseq.upper = (message_type == TIPC_NAMED_MSG)
  822. ? dseq.lower : msg_nameupper(msg);
  823. skb_pull(buf, msg_hdr_sz(msg));
  824. cb(usr_handle, dref, &buf, msg_data(msg),
  825. msg_data_sz(msg), msg_errcode(msg), &dseq);
  826. break;
  827. }
  828. }
  829. if (buf)
  830. buf_discard(buf);
  831. buf = next;
  832. continue;
  833. reject:
  834. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  835. buf = next;
  836. }
  837. }
  838. /*
  839. * port_dispatcher(): Dispatcher for messages destinated
  840. * to the tipc_port interface. Called with port locked.
  841. */
  842. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  843. {
  844. buf->next = NULL;
  845. spin_lock_bh(&queue_lock);
  846. if (msg_queue_head) {
  847. msg_queue_tail->next = buf;
  848. msg_queue_tail = buf;
  849. } else {
  850. msg_queue_tail = msg_queue_head = buf;
  851. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  852. }
  853. spin_unlock_bh(&queue_lock);
  854. return TIPC_OK;
  855. }
  856. /*
  857. * Wake up port after congestion: Called with port locked,
  858. *
  859. */
  860. static void port_wakeup_sh(unsigned long ref)
  861. {
  862. struct port *p_ptr;
  863. struct user_port *up_ptr;
  864. tipc_continue_event cb = NULL;
  865. void *uh = NULL;
  866. p_ptr = tipc_port_lock(ref);
  867. if (p_ptr) {
  868. up_ptr = p_ptr->user_port;
  869. if (up_ptr) {
  870. cb = up_ptr->continue_event_cb;
  871. uh = up_ptr->usr_handle;
  872. }
  873. tipc_port_unlock(p_ptr);
  874. }
  875. if (cb)
  876. cb(uh, ref);
  877. }
  878. static void port_wakeup(struct tipc_port *p_ptr)
  879. {
  880. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  881. }
  882. void tipc_acknowledge(u32 ref, u32 ack)
  883. {
  884. struct port *p_ptr;
  885. struct sk_buff *buf = NULL;
  886. p_ptr = tipc_port_lock(ref);
  887. if (!p_ptr)
  888. return;
  889. if (p_ptr->publ.connected) {
  890. p_ptr->publ.conn_unacked -= ack;
  891. buf = port_build_proto_msg(port_peerport(p_ptr),
  892. port_peernode(p_ptr),
  893. ref,
  894. tipc_own_addr,
  895. CONN_MANAGER,
  896. CONN_ACK,
  897. TIPC_OK,
  898. port_out_seqno(p_ptr),
  899. ack);
  900. }
  901. tipc_port_unlock(p_ptr);
  902. tipc_net_route_msg(buf);
  903. }
  904. /*
  905. * tipc_createport(): user level call. Will add port to
  906. * registry if non-zero user_ref.
  907. */
  908. int tipc_createport(u32 user_ref,
  909. void *usr_handle,
  910. unsigned int importance,
  911. tipc_msg_err_event error_cb,
  912. tipc_named_msg_err_event named_error_cb,
  913. tipc_conn_shutdown_event conn_error_cb,
  914. tipc_msg_event msg_cb,
  915. tipc_named_msg_event named_msg_cb,
  916. tipc_conn_msg_event conn_msg_cb,
  917. tipc_continue_event continue_event_cb,/* May be zero */
  918. u32 *portref)
  919. {
  920. struct user_port *up_ptr;
  921. struct port *p_ptr;
  922. struct tipc_port *tp_ptr;
  923. u32 ref;
  924. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  925. if (!up_ptr) {
  926. warn("Port creation failed, no memory\n");
  927. return -ENOMEM;
  928. }
  929. ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
  930. importance, &tp_ptr);
  931. if (ref == 0) {
  932. kfree(up_ptr);
  933. return -ENOMEM;
  934. }
  935. p_ptr = (struct port *)tp_ptr;
  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. tipc_port_unlock(p_ptr);
  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. tipc_port_unlock(p_ptr);
  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. if (!p_ptr)
  990. return -EINVAL;
  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->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->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  1089. return res;
  1090. }
  1091. /**
  1092. * tipc_disconnect_port - disconnect port from peer
  1093. *
  1094. * Port must be locked.
  1095. */
  1096. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  1097. {
  1098. int res;
  1099. if (tp_ptr->connected) {
  1100. tp_ptr->connected = 0;
  1101. /* let timer expire on it's own to avoid deadlock! */
  1102. tipc_nodesub_unsubscribe(
  1103. &((struct port *)tp_ptr)->subscription);
  1104. res = TIPC_OK;
  1105. } else {
  1106. res = -ENOTCONN;
  1107. }
  1108. return res;
  1109. }
  1110. /*
  1111. * tipc_disconnect(): Disconnect port form peer.
  1112. * This is a node local operation.
  1113. */
  1114. int tipc_disconnect(u32 ref)
  1115. {
  1116. struct port *p_ptr;
  1117. int res;
  1118. p_ptr = tipc_port_lock(ref);
  1119. if (!p_ptr)
  1120. return -EINVAL;
  1121. res = tipc_disconnect_port((struct tipc_port *)p_ptr);
  1122. tipc_port_unlock(p_ptr);
  1123. return res;
  1124. }
  1125. /*
  1126. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1127. */
  1128. int tipc_shutdown(u32 ref)
  1129. {
  1130. struct port *p_ptr;
  1131. struct sk_buff *buf = NULL;
  1132. p_ptr = tipc_port_lock(ref);
  1133. if (!p_ptr)
  1134. return -EINVAL;
  1135. if (p_ptr->publ.connected) {
  1136. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1137. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1138. imp++;
  1139. buf = port_build_proto_msg(port_peerport(p_ptr),
  1140. port_peernode(p_ptr),
  1141. ref,
  1142. tipc_own_addr,
  1143. imp,
  1144. TIPC_CONN_MSG,
  1145. TIPC_CONN_SHUTDOWN,
  1146. port_out_seqno(p_ptr),
  1147. 0);
  1148. }
  1149. tipc_port_unlock(p_ptr);
  1150. tipc_net_route_msg(buf);
  1151. return tipc_disconnect(ref);
  1152. }
  1153. int tipc_isconnected(u32 ref, int *isconnected)
  1154. {
  1155. struct port *p_ptr;
  1156. p_ptr = tipc_port_lock(ref);
  1157. if (!p_ptr)
  1158. return -EINVAL;
  1159. *isconnected = p_ptr->publ.connected;
  1160. tipc_port_unlock(p_ptr);
  1161. return TIPC_OK;
  1162. }
  1163. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1164. {
  1165. struct port *p_ptr;
  1166. int res;
  1167. p_ptr = tipc_port_lock(ref);
  1168. if (!p_ptr)
  1169. return -EINVAL;
  1170. if (p_ptr->publ.connected) {
  1171. peer->ref = port_peerport(p_ptr);
  1172. peer->node = port_peernode(p_ptr);
  1173. res = TIPC_OK;
  1174. } else
  1175. res = -ENOTCONN;
  1176. tipc_port_unlock(p_ptr);
  1177. return res;
  1178. }
  1179. int tipc_ref_valid(u32 ref)
  1180. {
  1181. /* Works irrespective of type */
  1182. return !!tipc_ref_deref(ref);
  1183. }
  1184. /*
  1185. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1186. * message for this node.
  1187. */
  1188. int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1189. struct iovec const *msg_sect)
  1190. {
  1191. struct sk_buff *buf;
  1192. int res;
  1193. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1194. MAX_MSG_SIZE, !sender->user_port, &buf);
  1195. if (likely(buf))
  1196. tipc_port_recv_msg(buf);
  1197. return res;
  1198. }
  1199. /**
  1200. * tipc_send - send message sections on connection
  1201. */
  1202. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1203. {
  1204. struct port *p_ptr;
  1205. u32 destnode;
  1206. int res;
  1207. p_ptr = tipc_port_deref(ref);
  1208. if (!p_ptr || !p_ptr->publ.connected)
  1209. return -EINVAL;
  1210. p_ptr->publ.congested = 1;
  1211. if (!tipc_port_congested(p_ptr)) {
  1212. destnode = port_peernode(p_ptr);
  1213. if (likely(destnode != tipc_own_addr))
  1214. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1215. destnode);
  1216. else
  1217. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1218. if (likely(res != -ELINKCONG)) {
  1219. port_incr_out_seqno(p_ptr);
  1220. p_ptr->publ.congested = 0;
  1221. p_ptr->sent++;
  1222. return res;
  1223. }
  1224. }
  1225. if (port_unreliable(p_ptr)) {
  1226. p_ptr->publ.congested = 0;
  1227. /* Just calculate msg length and return */
  1228. return msg_calc_data_size(msg_sect, num_sect);
  1229. }
  1230. return -ELINKCONG;
  1231. }
  1232. /**
  1233. * tipc_send_buf - send message buffer on connection
  1234. */
  1235. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1236. {
  1237. struct port *p_ptr;
  1238. struct tipc_msg *msg;
  1239. u32 destnode;
  1240. u32 hsz;
  1241. u32 sz;
  1242. u32 res;
  1243. p_ptr = tipc_port_deref(ref);
  1244. if (!p_ptr || !p_ptr->publ.connected)
  1245. return -EINVAL;
  1246. msg = &p_ptr->publ.phdr;
  1247. hsz = msg_hdr_sz(msg);
  1248. sz = hsz + dsz;
  1249. msg_set_size(msg, sz);
  1250. if (skb_cow(buf, hsz))
  1251. return -ENOMEM;
  1252. skb_push(buf, hsz);
  1253. skb_copy_to_linear_data(buf, msg, hsz);
  1254. destnode = msg_destnode(msg);
  1255. p_ptr->publ.congested = 1;
  1256. if (!tipc_port_congested(p_ptr)) {
  1257. if (likely(destnode != tipc_own_addr))
  1258. res = tipc_send_buf_fast(buf, destnode);
  1259. else {
  1260. tipc_port_recv_msg(buf);
  1261. res = sz;
  1262. }
  1263. if (likely(res != -ELINKCONG)) {
  1264. port_incr_out_seqno(p_ptr);
  1265. p_ptr->sent++;
  1266. p_ptr->publ.congested = 0;
  1267. return res;
  1268. }
  1269. }
  1270. if (port_unreliable(p_ptr)) {
  1271. p_ptr->publ.congested = 0;
  1272. return dsz;
  1273. }
  1274. return -ELINKCONG;
  1275. }
  1276. /**
  1277. * tipc_forward2name - forward message sections to port name
  1278. */
  1279. int tipc_forward2name(u32 ref,
  1280. struct tipc_name const *name,
  1281. u32 domain,
  1282. u32 num_sect,
  1283. struct iovec const *msg_sect,
  1284. struct tipc_portid const *orig,
  1285. unsigned int importance)
  1286. {
  1287. struct port *p_ptr;
  1288. struct tipc_msg *msg;
  1289. u32 destnode = domain;
  1290. u32 destport = 0;
  1291. int res;
  1292. p_ptr = tipc_port_deref(ref);
  1293. if (!p_ptr || p_ptr->publ.connected)
  1294. return -EINVAL;
  1295. msg = &p_ptr->publ.phdr;
  1296. msg_set_type(msg, TIPC_NAMED_MSG);
  1297. msg_set_orignode(msg, orig->node);
  1298. msg_set_origport(msg, orig->ref);
  1299. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1300. msg_set_nametype(msg, name->type);
  1301. msg_set_nameinst(msg, name->instance);
  1302. msg_set_lookup_scope(msg, addr_scope(domain));
  1303. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1304. msg_set_importance(msg,importance);
  1305. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1306. msg_set_destnode(msg, destnode);
  1307. msg_set_destport(msg, destport);
  1308. if (likely(destport || destnode)) {
  1309. p_ptr->sent++;
  1310. if (likely(destnode == tipc_own_addr))
  1311. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1312. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1313. destnode);
  1314. if (likely(res != -ELINKCONG))
  1315. return res;
  1316. if (port_unreliable(p_ptr)) {
  1317. /* Just calculate msg length and return */
  1318. return msg_calc_data_size(msg_sect, num_sect);
  1319. }
  1320. return -ELINKCONG;
  1321. }
  1322. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1323. TIPC_ERR_NO_NAME);
  1324. }
  1325. /**
  1326. * tipc_send2name - send message sections to port name
  1327. */
  1328. int tipc_send2name(u32 ref,
  1329. struct tipc_name const *name,
  1330. unsigned int domain,
  1331. unsigned int num_sect,
  1332. struct iovec const *msg_sect)
  1333. {
  1334. struct tipc_portid orig;
  1335. orig.ref = ref;
  1336. orig.node = tipc_own_addr;
  1337. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1338. TIPC_PORT_IMPORTANCE);
  1339. }
  1340. /**
  1341. * tipc_forward_buf2name - forward message buffer to port name
  1342. */
  1343. int tipc_forward_buf2name(u32 ref,
  1344. struct tipc_name const *name,
  1345. u32 domain,
  1346. struct sk_buff *buf,
  1347. unsigned int dsz,
  1348. struct tipc_portid const *orig,
  1349. unsigned int importance)
  1350. {
  1351. struct port *p_ptr;
  1352. struct tipc_msg *msg;
  1353. u32 destnode = domain;
  1354. u32 destport = 0;
  1355. int res;
  1356. p_ptr = (struct port *)tipc_ref_deref(ref);
  1357. if (!p_ptr || p_ptr->publ.connected)
  1358. return -EINVAL;
  1359. msg = &p_ptr->publ.phdr;
  1360. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1361. msg_set_importance(msg, importance);
  1362. msg_set_type(msg, TIPC_NAMED_MSG);
  1363. msg_set_orignode(msg, orig->node);
  1364. msg_set_origport(msg, orig->ref);
  1365. msg_set_nametype(msg, name->type);
  1366. msg_set_nameinst(msg, name->instance);
  1367. msg_set_lookup_scope(msg, addr_scope(domain));
  1368. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1369. msg_set_size(msg, LONG_H_SIZE + dsz);
  1370. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1371. msg_set_destnode(msg, destnode);
  1372. msg_set_destport(msg, destport);
  1373. msg_dbg(msg, "forw2name ==> ");
  1374. if (skb_cow(buf, LONG_H_SIZE))
  1375. return -ENOMEM;
  1376. skb_push(buf, LONG_H_SIZE);
  1377. skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
  1378. msg_dbg(buf_msg(buf),"PREP:");
  1379. if (likely(destport || destnode)) {
  1380. p_ptr->sent++;
  1381. if (destnode == tipc_own_addr)
  1382. return tipc_port_recv_msg(buf);
  1383. res = tipc_send_buf_fast(buf, destnode);
  1384. if (likely(res != -ELINKCONG))
  1385. return res;
  1386. if (port_unreliable(p_ptr))
  1387. return dsz;
  1388. return -ELINKCONG;
  1389. }
  1390. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1391. }
  1392. /**
  1393. * tipc_send_buf2name - send message buffer to port name
  1394. */
  1395. int tipc_send_buf2name(u32 ref,
  1396. struct tipc_name const *dest,
  1397. u32 domain,
  1398. struct sk_buff *buf,
  1399. unsigned int dsz)
  1400. {
  1401. struct tipc_portid orig;
  1402. orig.ref = ref;
  1403. orig.node = tipc_own_addr;
  1404. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1405. TIPC_PORT_IMPORTANCE);
  1406. }
  1407. /**
  1408. * tipc_forward2port - forward message sections to port identity
  1409. */
  1410. int tipc_forward2port(u32 ref,
  1411. struct tipc_portid const *dest,
  1412. unsigned int num_sect,
  1413. struct iovec const *msg_sect,
  1414. struct tipc_portid const *orig,
  1415. unsigned int importance)
  1416. {
  1417. struct port *p_ptr;
  1418. struct tipc_msg *msg;
  1419. int res;
  1420. p_ptr = tipc_port_deref(ref);
  1421. if (!p_ptr || p_ptr->publ.connected)
  1422. return -EINVAL;
  1423. msg = &p_ptr->publ.phdr;
  1424. msg_set_type(msg, TIPC_DIRECT_MSG);
  1425. msg_set_orignode(msg, orig->node);
  1426. msg_set_origport(msg, orig->ref);
  1427. msg_set_destnode(msg, dest->node);
  1428. msg_set_destport(msg, dest->ref);
  1429. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1430. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1431. msg_set_importance(msg, importance);
  1432. p_ptr->sent++;
  1433. if (dest->node == tipc_own_addr)
  1434. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1435. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1436. if (likely(res != -ELINKCONG))
  1437. return res;
  1438. if (port_unreliable(p_ptr)) {
  1439. /* Just calculate msg length and return */
  1440. return msg_calc_data_size(msg_sect, num_sect);
  1441. }
  1442. return -ELINKCONG;
  1443. }
  1444. /**
  1445. * tipc_send2port - send message sections to port identity
  1446. */
  1447. int tipc_send2port(u32 ref,
  1448. struct tipc_portid const *dest,
  1449. unsigned int num_sect,
  1450. struct iovec const *msg_sect)
  1451. {
  1452. struct tipc_portid orig;
  1453. orig.ref = ref;
  1454. orig.node = tipc_own_addr;
  1455. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1456. TIPC_PORT_IMPORTANCE);
  1457. }
  1458. /**
  1459. * tipc_forward_buf2port - forward message buffer to port identity
  1460. */
  1461. int tipc_forward_buf2port(u32 ref,
  1462. struct tipc_portid const *dest,
  1463. struct sk_buff *buf,
  1464. unsigned int dsz,
  1465. struct tipc_portid const *orig,
  1466. unsigned int importance)
  1467. {
  1468. struct port *p_ptr;
  1469. struct tipc_msg *msg;
  1470. int res;
  1471. p_ptr = (struct port *)tipc_ref_deref(ref);
  1472. if (!p_ptr || p_ptr->publ.connected)
  1473. return -EINVAL;
  1474. msg = &p_ptr->publ.phdr;
  1475. msg_set_type(msg, TIPC_DIRECT_MSG);
  1476. msg_set_orignode(msg, orig->node);
  1477. msg_set_origport(msg, orig->ref);
  1478. msg_set_destnode(msg, dest->node);
  1479. msg_set_destport(msg, dest->ref);
  1480. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1481. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1482. msg_set_importance(msg, importance);
  1483. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1484. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1485. return -ENOMEM;
  1486. skb_push(buf, DIR_MSG_H_SIZE);
  1487. skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
  1488. msg_dbg(msg, "buf2port: ");
  1489. p_ptr->sent++;
  1490. if (dest->node == tipc_own_addr)
  1491. return tipc_port_recv_msg(buf);
  1492. res = tipc_send_buf_fast(buf, dest->node);
  1493. if (likely(res != -ELINKCONG))
  1494. return res;
  1495. if (port_unreliable(p_ptr))
  1496. return dsz;
  1497. return -ELINKCONG;
  1498. }
  1499. /**
  1500. * tipc_send_buf2port - send message buffer to port identity
  1501. */
  1502. int tipc_send_buf2port(u32 ref,
  1503. struct tipc_portid const *dest,
  1504. struct sk_buff *buf,
  1505. unsigned int dsz)
  1506. {
  1507. struct tipc_portid orig;
  1508. orig.ref = ref;
  1509. orig.node = tipc_own_addr;
  1510. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1511. TIPC_PORT_IMPORTANCE);
  1512. }