port.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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_prevnode(msg, tipc_own_addr);
  678. msg_set_orignode(msg, tipc_own_addr);
  679. }
  680. spin_unlock_bh(&tipc_port_list_lock);
  681. }
  682. /*
  683. * port_dispatcher_sigh(): Signal handler for messages destinated
  684. * to the tipc_port interface.
  685. */
  686. static void port_dispatcher_sigh(void *dummy)
  687. {
  688. struct sk_buff *buf;
  689. spin_lock_bh(&queue_lock);
  690. buf = msg_queue_head;
  691. msg_queue_head = NULL;
  692. spin_unlock_bh(&queue_lock);
  693. while (buf) {
  694. struct port *p_ptr;
  695. struct user_port *up_ptr;
  696. struct tipc_portid orig;
  697. struct tipc_name_seq dseq;
  698. void *usr_handle;
  699. int connected;
  700. int published;
  701. u32 message_type;
  702. struct sk_buff *next = buf->next;
  703. struct tipc_msg *msg = buf_msg(buf);
  704. u32 dref = msg_destport(msg);
  705. message_type = msg_type(msg);
  706. if (message_type > TIPC_DIRECT_MSG)
  707. goto reject; /* Unsupported message type */
  708. p_ptr = tipc_port_lock(dref);
  709. if (!p_ptr)
  710. goto reject; /* Port deleted while msg in queue */
  711. orig.ref = msg_origport(msg);
  712. orig.node = msg_orignode(msg);
  713. up_ptr = p_ptr->user_port;
  714. usr_handle = up_ptr->usr_handle;
  715. connected = p_ptr->publ.connected;
  716. published = p_ptr->publ.published;
  717. if (unlikely(msg_errcode(msg)))
  718. goto err;
  719. switch (message_type) {
  720. case TIPC_CONN_MSG:{
  721. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  722. u32 peer_port = port_peerport(p_ptr);
  723. u32 peer_node = port_peernode(p_ptr);
  724. tipc_port_unlock(p_ptr);
  725. if (unlikely(!cb))
  726. goto reject;
  727. if (unlikely(!connected)) {
  728. if (tipc_connect2port(dref, &orig))
  729. goto reject;
  730. } else if ((msg_origport(msg) != peer_port) ||
  731. (msg_orignode(msg) != peer_node))
  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(!cb || connected))
  746. goto reject;
  747. skb_pull(buf, msg_hdr_sz(msg));
  748. cb(usr_handle, dref, &buf, msg_data(msg),
  749. msg_data_sz(msg), msg_importance(msg),
  750. &orig);
  751. break;
  752. }
  753. case TIPC_MCAST_MSG:
  754. case TIPC_NAMED_MSG:{
  755. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  756. tipc_port_unlock(p_ptr);
  757. if (unlikely(!cb || connected || !published))
  758. goto reject;
  759. dseq.type = msg_nametype(msg);
  760. dseq.lower = msg_nameinst(msg);
  761. dseq.upper = (message_type == TIPC_NAMED_MSG)
  762. ? dseq.lower : msg_nameupper(msg);
  763. skb_pull(buf, msg_hdr_sz(msg));
  764. cb(usr_handle, dref, &buf, msg_data(msg),
  765. msg_data_sz(msg), msg_importance(msg),
  766. &orig, &dseq);
  767. break;
  768. }
  769. }
  770. if (buf)
  771. buf_discard(buf);
  772. buf = next;
  773. continue;
  774. err:
  775. switch (message_type) {
  776. case TIPC_CONN_MSG:{
  777. tipc_conn_shutdown_event cb =
  778. up_ptr->conn_err_cb;
  779. u32 peer_port = port_peerport(p_ptr);
  780. u32 peer_node = port_peernode(p_ptr);
  781. tipc_port_unlock(p_ptr);
  782. if (!cb || !connected)
  783. break;
  784. if ((msg_origport(msg) != peer_port) ||
  785. (msg_orignode(msg) != peer_node))
  786. break;
  787. tipc_disconnect(dref);
  788. skb_pull(buf, msg_hdr_sz(msg));
  789. cb(usr_handle, dref, &buf, msg_data(msg),
  790. msg_data_sz(msg), msg_errcode(msg));
  791. break;
  792. }
  793. case TIPC_DIRECT_MSG:{
  794. tipc_msg_err_event cb = up_ptr->err_cb;
  795. tipc_port_unlock(p_ptr);
  796. if (!cb || connected)
  797. break;
  798. skb_pull(buf, msg_hdr_sz(msg));
  799. cb(usr_handle, dref, &buf, msg_data(msg),
  800. msg_data_sz(msg), msg_errcode(msg), &orig);
  801. break;
  802. }
  803. case TIPC_MCAST_MSG:
  804. case TIPC_NAMED_MSG:{
  805. tipc_named_msg_err_event cb =
  806. up_ptr->named_err_cb;
  807. tipc_port_unlock(p_ptr);
  808. if (!cb || connected)
  809. break;
  810. dseq.type = msg_nametype(msg);
  811. dseq.lower = msg_nameinst(msg);
  812. dseq.upper = (message_type == TIPC_NAMED_MSG)
  813. ? dseq.lower : msg_nameupper(msg);
  814. skb_pull(buf, msg_hdr_sz(msg));
  815. cb(usr_handle, dref, &buf, msg_data(msg),
  816. msg_data_sz(msg), msg_errcode(msg), &dseq);
  817. break;
  818. }
  819. }
  820. if (buf)
  821. buf_discard(buf);
  822. buf = next;
  823. continue;
  824. reject:
  825. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  826. buf = next;
  827. }
  828. }
  829. /*
  830. * port_dispatcher(): Dispatcher for messages destinated
  831. * to the tipc_port interface. Called with port locked.
  832. */
  833. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  834. {
  835. buf->next = NULL;
  836. spin_lock_bh(&queue_lock);
  837. if (msg_queue_head) {
  838. msg_queue_tail->next = buf;
  839. msg_queue_tail = buf;
  840. } else {
  841. msg_queue_tail = msg_queue_head = buf;
  842. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  843. }
  844. spin_unlock_bh(&queue_lock);
  845. return TIPC_OK;
  846. }
  847. /*
  848. * Wake up port after congestion: Called with port locked,
  849. *
  850. */
  851. static void port_wakeup_sh(unsigned long ref)
  852. {
  853. struct port *p_ptr;
  854. struct user_port *up_ptr;
  855. tipc_continue_event cb = NULL;
  856. void *uh = NULL;
  857. p_ptr = tipc_port_lock(ref);
  858. if (p_ptr) {
  859. up_ptr = p_ptr->user_port;
  860. if (up_ptr) {
  861. cb = up_ptr->continue_event_cb;
  862. uh = up_ptr->usr_handle;
  863. }
  864. tipc_port_unlock(p_ptr);
  865. }
  866. if (cb)
  867. cb(uh, ref);
  868. }
  869. static void port_wakeup(struct tipc_port *p_ptr)
  870. {
  871. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  872. }
  873. void tipc_acknowledge(u32 ref, u32 ack)
  874. {
  875. struct port *p_ptr;
  876. struct sk_buff *buf = NULL;
  877. p_ptr = tipc_port_lock(ref);
  878. if (!p_ptr)
  879. return;
  880. if (p_ptr->publ.connected) {
  881. p_ptr->publ.conn_unacked -= ack;
  882. buf = port_build_proto_msg(port_peerport(p_ptr),
  883. port_peernode(p_ptr),
  884. ref,
  885. tipc_own_addr,
  886. CONN_MANAGER,
  887. CONN_ACK,
  888. TIPC_OK,
  889. port_out_seqno(p_ptr),
  890. ack);
  891. }
  892. tipc_port_unlock(p_ptr);
  893. tipc_net_route_msg(buf);
  894. }
  895. /*
  896. * tipc_createport(): user level call. Will add port to
  897. * registry if non-zero user_ref.
  898. */
  899. int tipc_createport(u32 user_ref,
  900. void *usr_handle,
  901. unsigned int importance,
  902. tipc_msg_err_event error_cb,
  903. tipc_named_msg_err_event named_error_cb,
  904. tipc_conn_shutdown_event conn_error_cb,
  905. tipc_msg_event msg_cb,
  906. tipc_named_msg_event named_msg_cb,
  907. tipc_conn_msg_event conn_msg_cb,
  908. tipc_continue_event continue_event_cb,/* May be zero */
  909. u32 *portref)
  910. {
  911. struct user_port *up_ptr;
  912. struct port *p_ptr;
  913. struct tipc_port *tp_ptr;
  914. u32 ref;
  915. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  916. if (!up_ptr) {
  917. warn("Port creation failed, no memory\n");
  918. return -ENOMEM;
  919. }
  920. ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
  921. importance, &tp_ptr);
  922. if (ref == 0) {
  923. kfree(up_ptr);
  924. return -ENOMEM;
  925. }
  926. p_ptr = (struct port *)tp_ptr;
  927. p_ptr->user_port = up_ptr;
  928. up_ptr->user_ref = user_ref;
  929. up_ptr->usr_handle = usr_handle;
  930. up_ptr->ref = p_ptr->publ.ref;
  931. up_ptr->err_cb = error_cb;
  932. up_ptr->named_err_cb = named_error_cb;
  933. up_ptr->conn_err_cb = conn_error_cb;
  934. up_ptr->msg_cb = msg_cb;
  935. up_ptr->named_msg_cb = named_msg_cb;
  936. up_ptr->conn_msg_cb = conn_msg_cb;
  937. up_ptr->continue_event_cb = continue_event_cb;
  938. INIT_LIST_HEAD(&up_ptr->uport_list);
  939. tipc_reg_add_port(up_ptr);
  940. *portref = p_ptr->publ.ref;
  941. dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
  942. tipc_port_unlock(p_ptr);
  943. return TIPC_OK;
  944. }
  945. int tipc_ownidentity(u32 ref, struct tipc_portid *id)
  946. {
  947. id->ref = ref;
  948. id->node = tipc_own_addr;
  949. return TIPC_OK;
  950. }
  951. int tipc_portimportance(u32 ref, unsigned int *importance)
  952. {
  953. struct port *p_ptr;
  954. p_ptr = tipc_port_lock(ref);
  955. if (!p_ptr)
  956. return -EINVAL;
  957. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  958. tipc_port_unlock(p_ptr);
  959. return TIPC_OK;
  960. }
  961. int tipc_set_portimportance(u32 ref, unsigned int imp)
  962. {
  963. struct port *p_ptr;
  964. if (imp > TIPC_CRITICAL_IMPORTANCE)
  965. return -EINVAL;
  966. p_ptr = tipc_port_lock(ref);
  967. if (!p_ptr)
  968. return -EINVAL;
  969. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  970. tipc_port_unlock(p_ptr);
  971. return TIPC_OK;
  972. }
  973. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  974. {
  975. struct port *p_ptr;
  976. struct publication *publ;
  977. u32 key;
  978. int res = -EINVAL;
  979. p_ptr = tipc_port_lock(ref);
  980. if (!p_ptr)
  981. return -EINVAL;
  982. dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
  983. "lower = %u, upper = %u\n",
  984. ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
  985. if (p_ptr->publ.connected)
  986. goto exit;
  987. if (seq->lower > seq->upper)
  988. goto exit;
  989. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  990. goto exit;
  991. key = ref + p_ptr->pub_count + 1;
  992. if (key == ref) {
  993. res = -EADDRINUSE;
  994. goto exit;
  995. }
  996. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  997. scope, p_ptr->publ.ref, key);
  998. if (publ) {
  999. list_add(&publ->pport_list, &p_ptr->publications);
  1000. p_ptr->pub_count++;
  1001. p_ptr->publ.published = 1;
  1002. res = TIPC_OK;
  1003. }
  1004. exit:
  1005. tipc_port_unlock(p_ptr);
  1006. return res;
  1007. }
  1008. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  1009. {
  1010. struct port *p_ptr;
  1011. struct publication *publ;
  1012. struct publication *tpubl;
  1013. int res = -EINVAL;
  1014. p_ptr = tipc_port_lock(ref);
  1015. if (!p_ptr)
  1016. return -EINVAL;
  1017. if (!seq) {
  1018. list_for_each_entry_safe(publ, tpubl,
  1019. &p_ptr->publications, pport_list) {
  1020. tipc_nametbl_withdraw(publ->type, publ->lower,
  1021. publ->ref, publ->key);
  1022. }
  1023. res = TIPC_OK;
  1024. } else {
  1025. list_for_each_entry_safe(publ, tpubl,
  1026. &p_ptr->publications, pport_list) {
  1027. if (publ->scope != scope)
  1028. continue;
  1029. if (publ->type != seq->type)
  1030. continue;
  1031. if (publ->lower != seq->lower)
  1032. continue;
  1033. if (publ->upper != seq->upper)
  1034. break;
  1035. tipc_nametbl_withdraw(publ->type, publ->lower,
  1036. publ->ref, publ->key);
  1037. res = TIPC_OK;
  1038. break;
  1039. }
  1040. }
  1041. if (list_empty(&p_ptr->publications))
  1042. p_ptr->publ.published = 0;
  1043. tipc_port_unlock(p_ptr);
  1044. return res;
  1045. }
  1046. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  1047. {
  1048. struct port *p_ptr;
  1049. struct tipc_msg *msg;
  1050. int res = -EINVAL;
  1051. p_ptr = tipc_port_lock(ref);
  1052. if (!p_ptr)
  1053. return -EINVAL;
  1054. if (p_ptr->publ.published || p_ptr->publ.connected)
  1055. goto exit;
  1056. if (!peer->ref)
  1057. goto exit;
  1058. msg = &p_ptr->publ.phdr;
  1059. msg_set_destnode(msg, peer->node);
  1060. msg_set_destport(msg, peer->ref);
  1061. msg_set_orignode(msg, tipc_own_addr);
  1062. msg_set_origport(msg, p_ptr->publ.ref);
  1063. msg_set_transp_seqno(msg, 42);
  1064. msg_set_type(msg, TIPC_CONN_MSG);
  1065. if (!may_route(peer->node))
  1066. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1067. else
  1068. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1069. p_ptr->probing_interval = PROBING_INTERVAL;
  1070. p_ptr->probing_state = CONFIRMED;
  1071. p_ptr->publ.connected = 1;
  1072. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  1073. tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
  1074. (void *)(unsigned long)ref,
  1075. (net_ev_handler)port_handle_node_down);
  1076. res = TIPC_OK;
  1077. exit:
  1078. tipc_port_unlock(p_ptr);
  1079. p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  1080. return res;
  1081. }
  1082. /**
  1083. * tipc_disconnect_port - disconnect port from peer
  1084. *
  1085. * Port must be locked.
  1086. */
  1087. int tipc_disconnect_port(struct tipc_port *tp_ptr)
  1088. {
  1089. int res;
  1090. if (tp_ptr->connected) {
  1091. tp_ptr->connected = 0;
  1092. /* let timer expire on it's own to avoid deadlock! */
  1093. tipc_nodesub_unsubscribe(
  1094. &((struct port *)tp_ptr)->subscription);
  1095. res = TIPC_OK;
  1096. } else {
  1097. res = -ENOTCONN;
  1098. }
  1099. return res;
  1100. }
  1101. /*
  1102. * tipc_disconnect(): Disconnect port form peer.
  1103. * This is a node local operation.
  1104. */
  1105. int tipc_disconnect(u32 ref)
  1106. {
  1107. struct port *p_ptr;
  1108. int res;
  1109. p_ptr = tipc_port_lock(ref);
  1110. if (!p_ptr)
  1111. return -EINVAL;
  1112. res = tipc_disconnect_port((struct tipc_port *)p_ptr);
  1113. tipc_port_unlock(p_ptr);
  1114. return res;
  1115. }
  1116. /*
  1117. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1118. */
  1119. int tipc_shutdown(u32 ref)
  1120. {
  1121. struct port *p_ptr;
  1122. struct sk_buff *buf = NULL;
  1123. p_ptr = tipc_port_lock(ref);
  1124. if (!p_ptr)
  1125. return -EINVAL;
  1126. if (p_ptr->publ.connected) {
  1127. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1128. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1129. imp++;
  1130. buf = port_build_proto_msg(port_peerport(p_ptr),
  1131. port_peernode(p_ptr),
  1132. ref,
  1133. tipc_own_addr,
  1134. imp,
  1135. TIPC_CONN_MSG,
  1136. TIPC_CONN_SHUTDOWN,
  1137. port_out_seqno(p_ptr),
  1138. 0);
  1139. }
  1140. tipc_port_unlock(p_ptr);
  1141. tipc_net_route_msg(buf);
  1142. return tipc_disconnect(ref);
  1143. }
  1144. int tipc_isconnected(u32 ref, int *isconnected)
  1145. {
  1146. struct port *p_ptr;
  1147. p_ptr = tipc_port_lock(ref);
  1148. if (!p_ptr)
  1149. return -EINVAL;
  1150. *isconnected = p_ptr->publ.connected;
  1151. tipc_port_unlock(p_ptr);
  1152. return TIPC_OK;
  1153. }
  1154. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1155. {
  1156. struct port *p_ptr;
  1157. int res;
  1158. p_ptr = tipc_port_lock(ref);
  1159. if (!p_ptr)
  1160. return -EINVAL;
  1161. if (p_ptr->publ.connected) {
  1162. peer->ref = port_peerport(p_ptr);
  1163. peer->node = port_peernode(p_ptr);
  1164. res = TIPC_OK;
  1165. } else
  1166. res = -ENOTCONN;
  1167. tipc_port_unlock(p_ptr);
  1168. return res;
  1169. }
  1170. int tipc_ref_valid(u32 ref)
  1171. {
  1172. /* Works irrespective of type */
  1173. return !!tipc_ref_deref(ref);
  1174. }
  1175. /*
  1176. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1177. * message for this node.
  1178. */
  1179. int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1180. struct iovec const *msg_sect)
  1181. {
  1182. struct sk_buff *buf;
  1183. int res;
  1184. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1185. MAX_MSG_SIZE, !sender->user_port, &buf);
  1186. if (likely(buf))
  1187. tipc_port_recv_msg(buf);
  1188. return res;
  1189. }
  1190. /**
  1191. * tipc_send - send message sections on connection
  1192. */
  1193. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1194. {
  1195. struct port *p_ptr;
  1196. u32 destnode;
  1197. int res;
  1198. p_ptr = tipc_port_deref(ref);
  1199. if (!p_ptr || !p_ptr->publ.connected)
  1200. return -EINVAL;
  1201. p_ptr->publ.congested = 1;
  1202. if (!tipc_port_congested(p_ptr)) {
  1203. destnode = port_peernode(p_ptr);
  1204. if (likely(destnode != tipc_own_addr))
  1205. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1206. destnode);
  1207. else
  1208. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1209. if (likely(res != -ELINKCONG)) {
  1210. port_incr_out_seqno(p_ptr);
  1211. p_ptr->publ.congested = 0;
  1212. p_ptr->sent++;
  1213. return res;
  1214. }
  1215. }
  1216. if (port_unreliable(p_ptr)) {
  1217. p_ptr->publ.congested = 0;
  1218. /* Just calculate msg length and return */
  1219. return msg_calc_data_size(msg_sect, num_sect);
  1220. }
  1221. return -ELINKCONG;
  1222. }
  1223. /**
  1224. * tipc_send_buf - send message buffer on connection
  1225. */
  1226. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1227. {
  1228. struct port *p_ptr;
  1229. struct tipc_msg *msg;
  1230. u32 destnode;
  1231. u32 hsz;
  1232. u32 sz;
  1233. u32 res;
  1234. p_ptr = tipc_port_deref(ref);
  1235. if (!p_ptr || !p_ptr->publ.connected)
  1236. return -EINVAL;
  1237. msg = &p_ptr->publ.phdr;
  1238. hsz = msg_hdr_sz(msg);
  1239. sz = hsz + dsz;
  1240. msg_set_size(msg, sz);
  1241. if (skb_cow(buf, hsz))
  1242. return -ENOMEM;
  1243. skb_push(buf, hsz);
  1244. skb_copy_to_linear_data(buf, msg, hsz);
  1245. destnode = msg_destnode(msg);
  1246. p_ptr->publ.congested = 1;
  1247. if (!tipc_port_congested(p_ptr)) {
  1248. if (likely(destnode != tipc_own_addr))
  1249. res = tipc_send_buf_fast(buf, destnode);
  1250. else {
  1251. tipc_port_recv_msg(buf);
  1252. res = sz;
  1253. }
  1254. if (likely(res != -ELINKCONG)) {
  1255. port_incr_out_seqno(p_ptr);
  1256. p_ptr->sent++;
  1257. p_ptr->publ.congested = 0;
  1258. return res;
  1259. }
  1260. }
  1261. if (port_unreliable(p_ptr)) {
  1262. p_ptr->publ.congested = 0;
  1263. return dsz;
  1264. }
  1265. return -ELINKCONG;
  1266. }
  1267. /**
  1268. * tipc_forward2name - forward message sections to port name
  1269. */
  1270. int tipc_forward2name(u32 ref,
  1271. struct tipc_name const *name,
  1272. u32 domain,
  1273. u32 num_sect,
  1274. struct iovec const *msg_sect,
  1275. struct tipc_portid const *orig,
  1276. unsigned int importance)
  1277. {
  1278. struct port *p_ptr;
  1279. struct tipc_msg *msg;
  1280. u32 destnode = domain;
  1281. u32 destport = 0;
  1282. int res;
  1283. p_ptr = tipc_port_deref(ref);
  1284. if (!p_ptr || p_ptr->publ.connected)
  1285. return -EINVAL;
  1286. msg = &p_ptr->publ.phdr;
  1287. msg_set_type(msg, TIPC_NAMED_MSG);
  1288. msg_set_orignode(msg, orig->node);
  1289. msg_set_origport(msg, orig->ref);
  1290. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1291. msg_set_nametype(msg, name->type);
  1292. msg_set_nameinst(msg, name->instance);
  1293. msg_set_lookup_scope(msg, addr_scope(domain));
  1294. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1295. msg_set_importance(msg,importance);
  1296. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1297. msg_set_destnode(msg, destnode);
  1298. msg_set_destport(msg, destport);
  1299. if (likely(destport || destnode)) {
  1300. p_ptr->sent++;
  1301. if (likely(destnode == tipc_own_addr))
  1302. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1303. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1304. destnode);
  1305. if (likely(res != -ELINKCONG))
  1306. return res;
  1307. if (port_unreliable(p_ptr)) {
  1308. /* Just calculate msg length and return */
  1309. return msg_calc_data_size(msg_sect, num_sect);
  1310. }
  1311. return -ELINKCONG;
  1312. }
  1313. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1314. TIPC_ERR_NO_NAME);
  1315. }
  1316. /**
  1317. * tipc_send2name - send message sections to port name
  1318. */
  1319. int tipc_send2name(u32 ref,
  1320. struct tipc_name const *name,
  1321. unsigned int domain,
  1322. unsigned int num_sect,
  1323. struct iovec const *msg_sect)
  1324. {
  1325. struct tipc_portid orig;
  1326. orig.ref = ref;
  1327. orig.node = tipc_own_addr;
  1328. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1329. TIPC_PORT_IMPORTANCE);
  1330. }
  1331. /**
  1332. * tipc_forward_buf2name - forward message buffer to port name
  1333. */
  1334. int tipc_forward_buf2name(u32 ref,
  1335. struct tipc_name const *name,
  1336. u32 domain,
  1337. struct sk_buff *buf,
  1338. unsigned int dsz,
  1339. struct tipc_portid const *orig,
  1340. unsigned int importance)
  1341. {
  1342. struct port *p_ptr;
  1343. struct tipc_msg *msg;
  1344. u32 destnode = domain;
  1345. u32 destport = 0;
  1346. int res;
  1347. p_ptr = (struct port *)tipc_ref_deref(ref);
  1348. if (!p_ptr || p_ptr->publ.connected)
  1349. return -EINVAL;
  1350. msg = &p_ptr->publ.phdr;
  1351. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1352. msg_set_importance(msg, importance);
  1353. msg_set_type(msg, TIPC_NAMED_MSG);
  1354. msg_set_orignode(msg, orig->node);
  1355. msg_set_origport(msg, orig->ref);
  1356. msg_set_nametype(msg, name->type);
  1357. msg_set_nameinst(msg, name->instance);
  1358. msg_set_lookup_scope(msg, addr_scope(domain));
  1359. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1360. msg_set_size(msg, LONG_H_SIZE + dsz);
  1361. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1362. msg_set_destnode(msg, destnode);
  1363. msg_set_destport(msg, destport);
  1364. msg_dbg(msg, "forw2name ==> ");
  1365. if (skb_cow(buf, LONG_H_SIZE))
  1366. return -ENOMEM;
  1367. skb_push(buf, LONG_H_SIZE);
  1368. skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
  1369. msg_dbg(buf_msg(buf),"PREP:");
  1370. if (likely(destport || destnode)) {
  1371. p_ptr->sent++;
  1372. if (destnode == tipc_own_addr)
  1373. return tipc_port_recv_msg(buf);
  1374. res = tipc_send_buf_fast(buf, destnode);
  1375. if (likely(res != -ELINKCONG))
  1376. return res;
  1377. if (port_unreliable(p_ptr))
  1378. return dsz;
  1379. return -ELINKCONG;
  1380. }
  1381. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1382. }
  1383. /**
  1384. * tipc_send_buf2name - send message buffer to port name
  1385. */
  1386. int tipc_send_buf2name(u32 ref,
  1387. struct tipc_name const *dest,
  1388. u32 domain,
  1389. struct sk_buff *buf,
  1390. unsigned int dsz)
  1391. {
  1392. struct tipc_portid orig;
  1393. orig.ref = ref;
  1394. orig.node = tipc_own_addr;
  1395. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1396. TIPC_PORT_IMPORTANCE);
  1397. }
  1398. /**
  1399. * tipc_forward2port - forward message sections to port identity
  1400. */
  1401. int tipc_forward2port(u32 ref,
  1402. struct tipc_portid const *dest,
  1403. unsigned int num_sect,
  1404. struct iovec const *msg_sect,
  1405. struct tipc_portid const *orig,
  1406. unsigned int importance)
  1407. {
  1408. struct port *p_ptr;
  1409. struct tipc_msg *msg;
  1410. int res;
  1411. p_ptr = tipc_port_deref(ref);
  1412. if (!p_ptr || p_ptr->publ.connected)
  1413. return -EINVAL;
  1414. msg = &p_ptr->publ.phdr;
  1415. msg_set_type(msg, TIPC_DIRECT_MSG);
  1416. msg_set_orignode(msg, orig->node);
  1417. msg_set_origport(msg, orig->ref);
  1418. msg_set_destnode(msg, dest->node);
  1419. msg_set_destport(msg, dest->ref);
  1420. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1421. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1422. msg_set_importance(msg, importance);
  1423. p_ptr->sent++;
  1424. if (dest->node == tipc_own_addr)
  1425. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1426. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1427. if (likely(res != -ELINKCONG))
  1428. return res;
  1429. if (port_unreliable(p_ptr)) {
  1430. /* Just calculate msg length and return */
  1431. return msg_calc_data_size(msg_sect, num_sect);
  1432. }
  1433. return -ELINKCONG;
  1434. }
  1435. /**
  1436. * tipc_send2port - send message sections to port identity
  1437. */
  1438. int tipc_send2port(u32 ref,
  1439. struct tipc_portid const *dest,
  1440. unsigned int num_sect,
  1441. struct iovec const *msg_sect)
  1442. {
  1443. struct tipc_portid orig;
  1444. orig.ref = ref;
  1445. orig.node = tipc_own_addr;
  1446. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1447. TIPC_PORT_IMPORTANCE);
  1448. }
  1449. /**
  1450. * tipc_forward_buf2port - forward message buffer to port identity
  1451. */
  1452. int tipc_forward_buf2port(u32 ref,
  1453. struct tipc_portid const *dest,
  1454. struct sk_buff *buf,
  1455. unsigned int dsz,
  1456. struct tipc_portid const *orig,
  1457. unsigned int importance)
  1458. {
  1459. struct port *p_ptr;
  1460. struct tipc_msg *msg;
  1461. int res;
  1462. p_ptr = (struct port *)tipc_ref_deref(ref);
  1463. if (!p_ptr || p_ptr->publ.connected)
  1464. return -EINVAL;
  1465. msg = &p_ptr->publ.phdr;
  1466. msg_set_type(msg, TIPC_DIRECT_MSG);
  1467. msg_set_orignode(msg, orig->node);
  1468. msg_set_origport(msg, orig->ref);
  1469. msg_set_destnode(msg, dest->node);
  1470. msg_set_destport(msg, dest->ref);
  1471. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1472. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1473. msg_set_importance(msg, importance);
  1474. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1475. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1476. return -ENOMEM;
  1477. skb_push(buf, DIR_MSG_H_SIZE);
  1478. skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
  1479. msg_dbg(msg, "buf2port: ");
  1480. p_ptr->sent++;
  1481. if (dest->node == tipc_own_addr)
  1482. return tipc_port_recv_msg(buf);
  1483. res = tipc_send_buf_fast(buf, dest->node);
  1484. if (likely(res != -ELINKCONG))
  1485. return res;
  1486. if (port_unreliable(p_ptr))
  1487. return dsz;
  1488. return -ELINKCONG;
  1489. }
  1490. /**
  1491. * tipc_send_buf2port - send message buffer to port identity
  1492. */
  1493. int tipc_send_buf2port(u32 ref,
  1494. struct tipc_portid const *dest,
  1495. struct sk_buff *buf,
  1496. unsigned int dsz)
  1497. {
  1498. struct tipc_portid orig;
  1499. orig.ref = ref;
  1500. orig.node = tipc_own_addr;
  1501. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1502. TIPC_PORT_IMPORTANCE);
  1503. }