port.c 41 KB

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