port.c 40 KB

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