port.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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)
  440. return;
  441. if (!p_ptr->publ.connected) {
  442. tipc_port_unlock(p_ptr);
  443. return;
  444. }
  445. /* Last probe answered ? */
  446. if (p_ptr->probing_state == PROBING) {
  447. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
  448. } else {
  449. buf = port_build_proto_msg(port_peerport(p_ptr),
  450. port_peernode(p_ptr),
  451. p_ptr->publ.ref,
  452. tipc_own_addr,
  453. CONN_MANAGER,
  454. CONN_PROBE,
  455. TIPC_OK,
  456. port_out_seqno(p_ptr),
  457. 0);
  458. port_incr_out_seqno(p_ptr);
  459. p_ptr->probing_state = PROBING;
  460. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  461. }
  462. tipc_port_unlock(p_ptr);
  463. tipc_net_route_msg(buf);
  464. }
  465. static void port_handle_node_down(unsigned long ref)
  466. {
  467. struct port *p_ptr = tipc_port_lock(ref);
  468. struct sk_buff* buf = NULL;
  469. if (!p_ptr)
  470. return;
  471. buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
  472. tipc_port_unlock(p_ptr);
  473. tipc_net_route_msg(buf);
  474. }
  475. static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
  476. {
  477. u32 imp = msg_importance(&p_ptr->publ.phdr);
  478. if (!p_ptr->publ.connected)
  479. return NULL;
  480. if (imp < TIPC_CRITICAL_IMPORTANCE)
  481. imp++;
  482. return port_build_proto_msg(p_ptr->publ.ref,
  483. tipc_own_addr,
  484. port_peerport(p_ptr),
  485. port_peernode(p_ptr),
  486. imp,
  487. TIPC_CONN_MSG,
  488. err,
  489. p_ptr->last_in_seqno + 1,
  490. 0);
  491. }
  492. static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
  493. {
  494. u32 imp = msg_importance(&p_ptr->publ.phdr);
  495. if (!p_ptr->publ.connected)
  496. return NULL;
  497. if (imp < TIPC_CRITICAL_IMPORTANCE)
  498. imp++;
  499. return port_build_proto_msg(port_peerport(p_ptr),
  500. port_peernode(p_ptr),
  501. p_ptr->publ.ref,
  502. tipc_own_addr,
  503. imp,
  504. TIPC_CONN_MSG,
  505. err,
  506. port_out_seqno(p_ptr),
  507. 0);
  508. }
  509. void tipc_port_recv_proto_msg(struct sk_buff *buf)
  510. {
  511. struct tipc_msg *msg = buf_msg(buf);
  512. struct port *p_ptr = tipc_port_lock(msg_destport(msg));
  513. u32 err = TIPC_OK;
  514. struct sk_buff *r_buf = NULL;
  515. struct sk_buff *abort_buf = NULL;
  516. msg_dbg(msg, "PORT<RECV<:");
  517. if (!p_ptr) {
  518. err = TIPC_ERR_NO_PORT;
  519. } else if (p_ptr->publ.connected) {
  520. if (port_peernode(p_ptr) != msg_orignode(msg))
  521. err = TIPC_ERR_NO_PORT;
  522. if (port_peerport(p_ptr) != msg_origport(msg))
  523. err = TIPC_ERR_NO_PORT;
  524. if (!err && msg_routed(msg)) {
  525. u32 seqno = msg_transp_seqno(msg);
  526. u32 myno = ++p_ptr->last_in_seqno;
  527. if (seqno != myno) {
  528. err = TIPC_ERR_NO_PORT;
  529. abort_buf = port_build_self_abort_msg(p_ptr, err);
  530. }
  531. }
  532. if (msg_type(msg) == CONN_ACK) {
  533. int wakeup = tipc_port_congested(p_ptr) &&
  534. p_ptr->publ.congested &&
  535. p_ptr->wakeup;
  536. p_ptr->acked += msg_msgcnt(msg);
  537. if (tipc_port_congested(p_ptr))
  538. goto exit;
  539. p_ptr->publ.congested = 0;
  540. if (!wakeup)
  541. goto exit;
  542. p_ptr->wakeup(&p_ptr->publ);
  543. goto exit;
  544. }
  545. } else if (p_ptr->publ.published) {
  546. err = TIPC_ERR_NO_PORT;
  547. }
  548. if (err) {
  549. r_buf = port_build_proto_msg(msg_origport(msg),
  550. msg_orignode(msg),
  551. msg_destport(msg),
  552. tipc_own_addr,
  553. DATA_HIGH,
  554. TIPC_CONN_MSG,
  555. err,
  556. 0,
  557. 0);
  558. goto exit;
  559. }
  560. /* All is fine */
  561. if (msg_type(msg) == CONN_PROBE) {
  562. r_buf = port_build_proto_msg(msg_origport(msg),
  563. msg_orignode(msg),
  564. msg_destport(msg),
  565. tipc_own_addr,
  566. CONN_MANAGER,
  567. CONN_PROBE_REPLY,
  568. TIPC_OK,
  569. port_out_seqno(p_ptr),
  570. 0);
  571. }
  572. p_ptr->probing_state = CONFIRMED;
  573. port_incr_out_seqno(p_ptr);
  574. exit:
  575. if (p_ptr)
  576. tipc_port_unlock(p_ptr);
  577. tipc_net_route_msg(r_buf);
  578. tipc_net_route_msg(abort_buf);
  579. buf_discard(buf);
  580. }
  581. static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
  582. {
  583. struct publication *publ;
  584. if (full_id)
  585. tipc_printf(buf, "<%u.%u.%u:%u>:",
  586. tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
  587. tipc_node(tipc_own_addr), p_ptr->publ.ref);
  588. else
  589. tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
  590. if (p_ptr->publ.connected) {
  591. u32 dport = port_peerport(p_ptr);
  592. u32 destnode = port_peernode(p_ptr);
  593. tipc_printf(buf, " connected to <%u.%u.%u:%u>",
  594. tipc_zone(destnode), tipc_cluster(destnode),
  595. tipc_node(destnode), dport);
  596. if (p_ptr->publ.conn_type != 0)
  597. tipc_printf(buf, " via {%u,%u}",
  598. p_ptr->publ.conn_type,
  599. p_ptr->publ.conn_instance);
  600. }
  601. else if (p_ptr->publ.published) {
  602. tipc_printf(buf, " bound to");
  603. list_for_each_entry(publ, &p_ptr->publications, pport_list) {
  604. if (publ->lower == publ->upper)
  605. tipc_printf(buf, " {%u,%u}", publ->type,
  606. publ->lower);
  607. else
  608. tipc_printf(buf, " {%u,%u,%u}", publ->type,
  609. publ->lower, publ->upper);
  610. }
  611. }
  612. tipc_printf(buf, "\n");
  613. }
  614. #define MAX_PORT_QUERY 32768
  615. struct sk_buff *tipc_port_get_ports(void)
  616. {
  617. struct sk_buff *buf;
  618. struct tlv_desc *rep_tlv;
  619. struct print_buf pb;
  620. struct port *p_ptr;
  621. int str_len;
  622. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
  623. if (!buf)
  624. return NULL;
  625. rep_tlv = (struct tlv_desc *)buf->data;
  626. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
  627. spin_lock_bh(&tipc_port_list_lock);
  628. list_for_each_entry(p_ptr, &ports, port_list) {
  629. spin_lock_bh(p_ptr->publ.lock);
  630. port_print(p_ptr, &pb, 0);
  631. spin_unlock_bh(p_ptr->publ.lock);
  632. }
  633. spin_unlock_bh(&tipc_port_list_lock);
  634. str_len = tipc_printbuf_validate(&pb);
  635. skb_put(buf, TLV_SPACE(str_len));
  636. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  637. return buf;
  638. }
  639. #if 0
  640. #define MAX_PORT_STATS 2000
  641. struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
  642. {
  643. u32 ref;
  644. struct port *p_ptr;
  645. struct sk_buff *buf;
  646. struct tlv_desc *rep_tlv;
  647. struct print_buf pb;
  648. int str_len;
  649. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
  650. return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  651. ref = *(u32 *)TLV_DATA(req_tlv_area);
  652. ref = ntohl(ref);
  653. p_ptr = tipc_port_lock(ref);
  654. if (!p_ptr)
  655. return cfg_reply_error_string("port not found");
  656. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
  657. if (!buf) {
  658. tipc_port_unlock(p_ptr);
  659. return NULL;
  660. }
  661. rep_tlv = (struct tlv_desc *)buf->data;
  662. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
  663. port_print(p_ptr, &pb, 1);
  664. /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
  665. tipc_port_unlock(p_ptr);
  666. str_len = tipc_printbuf_validate(&pb);
  667. skb_put(buf, TLV_SPACE(str_len));
  668. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  669. return buf;
  670. }
  671. #endif
  672. void tipc_port_reinit(void)
  673. {
  674. struct port *p_ptr;
  675. struct tipc_msg *msg;
  676. spin_lock_bh(&tipc_port_list_lock);
  677. list_for_each_entry(p_ptr, &ports, port_list) {
  678. msg = &p_ptr->publ.phdr;
  679. if (msg_orignode(msg) == tipc_own_addr)
  680. break;
  681. msg_set_orignode(msg, tipc_own_addr);
  682. }
  683. spin_unlock_bh(&tipc_port_list_lock);
  684. }
  685. /*
  686. * port_dispatcher_sigh(): Signal handler for messages destinated
  687. * to the tipc_port interface.
  688. */
  689. static void port_dispatcher_sigh(void *dummy)
  690. {
  691. struct sk_buff *buf;
  692. spin_lock_bh(&queue_lock);
  693. buf = msg_queue_head;
  694. msg_queue_head = NULL;
  695. spin_unlock_bh(&queue_lock);
  696. while (buf) {
  697. struct port *p_ptr;
  698. struct user_port *up_ptr;
  699. struct tipc_portid orig;
  700. struct tipc_name_seq dseq;
  701. void *usr_handle;
  702. int connected;
  703. int published;
  704. u32 message_type;
  705. struct sk_buff *next = buf->next;
  706. struct tipc_msg *msg = buf_msg(buf);
  707. u32 dref = msg_destport(msg);
  708. message_type = msg_type(msg);
  709. if (message_type > TIPC_DIRECT_MSG)
  710. goto reject; /* Unsupported message type */
  711. p_ptr = tipc_port_lock(dref);
  712. if (!p_ptr)
  713. goto reject; /* Port deleted while msg in queue */
  714. orig.ref = msg_origport(msg);
  715. orig.node = msg_orignode(msg);
  716. up_ptr = p_ptr->user_port;
  717. usr_handle = up_ptr->usr_handle;
  718. connected = p_ptr->publ.connected;
  719. published = p_ptr->publ.published;
  720. if (unlikely(msg_errcode(msg)))
  721. goto err;
  722. switch (message_type) {
  723. case TIPC_CONN_MSG:{
  724. tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
  725. u32 peer_port = port_peerport(p_ptr);
  726. u32 peer_node = port_peernode(p_ptr);
  727. spin_unlock_bh(p_ptr->publ.lock);
  728. if (unlikely(!connected)) {
  729. if (unlikely(published))
  730. goto reject;
  731. tipc_connect2port(dref,&orig);
  732. }
  733. if (unlikely(msg_origport(msg) != peer_port))
  734. goto reject;
  735. if (unlikely(msg_orignode(msg) != peer_node))
  736. goto reject;
  737. if (unlikely(!cb))
  738. goto reject;
  739. if (unlikely(++p_ptr->publ.conn_unacked >=
  740. TIPC_FLOW_CONTROL_WIN))
  741. tipc_acknowledge(dref,
  742. p_ptr->publ.conn_unacked);
  743. skb_pull(buf, msg_hdr_sz(msg));
  744. cb(usr_handle, dref, &buf, msg_data(msg),
  745. msg_data_sz(msg));
  746. break;
  747. }
  748. case TIPC_DIRECT_MSG:{
  749. tipc_msg_event cb = up_ptr->msg_cb;
  750. spin_unlock_bh(p_ptr->publ.lock);
  751. if (unlikely(connected))
  752. goto reject;
  753. if (unlikely(!cb))
  754. goto reject;
  755. skb_pull(buf, msg_hdr_sz(msg));
  756. cb(usr_handle, dref, &buf, msg_data(msg),
  757. msg_data_sz(msg), msg_importance(msg),
  758. &orig);
  759. break;
  760. }
  761. case TIPC_MCAST_MSG:
  762. case TIPC_NAMED_MSG:{
  763. tipc_named_msg_event cb = up_ptr->named_msg_cb;
  764. spin_unlock_bh(p_ptr->publ.lock);
  765. if (unlikely(connected))
  766. goto reject;
  767. if (unlikely(!cb))
  768. goto reject;
  769. if (unlikely(!published))
  770. goto reject;
  771. dseq.type = msg_nametype(msg);
  772. dseq.lower = msg_nameinst(msg);
  773. dseq.upper = (message_type == TIPC_NAMED_MSG)
  774. ? dseq.lower : msg_nameupper(msg);
  775. skb_pull(buf, msg_hdr_sz(msg));
  776. cb(usr_handle, dref, &buf, msg_data(msg),
  777. msg_data_sz(msg), msg_importance(msg),
  778. &orig, &dseq);
  779. break;
  780. }
  781. }
  782. if (buf)
  783. buf_discard(buf);
  784. buf = next;
  785. continue;
  786. err:
  787. switch (message_type) {
  788. case TIPC_CONN_MSG:{
  789. tipc_conn_shutdown_event cb =
  790. up_ptr->conn_err_cb;
  791. u32 peer_port = port_peerport(p_ptr);
  792. u32 peer_node = port_peernode(p_ptr);
  793. spin_unlock_bh(p_ptr->publ.lock);
  794. if (!connected || !cb)
  795. break;
  796. if (msg_origport(msg) != peer_port)
  797. break;
  798. if (msg_orignode(msg) != peer_node)
  799. break;
  800. tipc_disconnect(dref);
  801. skb_pull(buf, msg_hdr_sz(msg));
  802. cb(usr_handle, dref, &buf, msg_data(msg),
  803. msg_data_sz(msg), msg_errcode(msg));
  804. break;
  805. }
  806. case TIPC_DIRECT_MSG:{
  807. tipc_msg_err_event cb = up_ptr->err_cb;
  808. spin_unlock_bh(p_ptr->publ.lock);
  809. if (connected || !cb)
  810. break;
  811. skb_pull(buf, msg_hdr_sz(msg));
  812. cb(usr_handle, dref, &buf, msg_data(msg),
  813. msg_data_sz(msg), msg_errcode(msg), &orig);
  814. break;
  815. }
  816. case TIPC_MCAST_MSG:
  817. case TIPC_NAMED_MSG:{
  818. tipc_named_msg_err_event cb =
  819. up_ptr->named_err_cb;
  820. spin_unlock_bh(p_ptr->publ.lock);
  821. if (connected || !cb)
  822. break;
  823. dseq.type = msg_nametype(msg);
  824. dseq.lower = msg_nameinst(msg);
  825. dseq.upper = (message_type == TIPC_NAMED_MSG)
  826. ? dseq.lower : msg_nameupper(msg);
  827. skb_pull(buf, msg_hdr_sz(msg));
  828. cb(usr_handle, dref, &buf, msg_data(msg),
  829. msg_data_sz(msg), msg_errcode(msg), &dseq);
  830. break;
  831. }
  832. }
  833. if (buf)
  834. buf_discard(buf);
  835. buf = next;
  836. continue;
  837. reject:
  838. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  839. buf = next;
  840. }
  841. }
  842. /*
  843. * port_dispatcher(): Dispatcher for messages destinated
  844. * to the tipc_port interface. Called with port locked.
  845. */
  846. static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
  847. {
  848. buf->next = NULL;
  849. spin_lock_bh(&queue_lock);
  850. if (msg_queue_head) {
  851. msg_queue_tail->next = buf;
  852. msg_queue_tail = buf;
  853. } else {
  854. msg_queue_tail = msg_queue_head = buf;
  855. tipc_k_signal((Handler)port_dispatcher_sigh, 0);
  856. }
  857. spin_unlock_bh(&queue_lock);
  858. return TIPC_OK;
  859. }
  860. /*
  861. * Wake up port after congestion: Called with port locked,
  862. *
  863. */
  864. static void port_wakeup_sh(unsigned long ref)
  865. {
  866. struct port *p_ptr;
  867. struct user_port *up_ptr;
  868. tipc_continue_event cb = NULL;
  869. void *uh = NULL;
  870. p_ptr = tipc_port_lock(ref);
  871. if (p_ptr) {
  872. up_ptr = p_ptr->user_port;
  873. if (up_ptr) {
  874. cb = up_ptr->continue_event_cb;
  875. uh = up_ptr->usr_handle;
  876. }
  877. tipc_port_unlock(p_ptr);
  878. }
  879. if (cb)
  880. cb(uh, ref);
  881. }
  882. static void port_wakeup(struct tipc_port *p_ptr)
  883. {
  884. tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
  885. }
  886. void tipc_acknowledge(u32 ref, u32 ack)
  887. {
  888. struct port *p_ptr;
  889. struct sk_buff *buf = NULL;
  890. p_ptr = tipc_port_lock(ref);
  891. if (!p_ptr)
  892. return;
  893. if (p_ptr->publ.connected) {
  894. p_ptr->publ.conn_unacked -= ack;
  895. buf = port_build_proto_msg(port_peerport(p_ptr),
  896. port_peernode(p_ptr),
  897. ref,
  898. tipc_own_addr,
  899. CONN_MANAGER,
  900. CONN_ACK,
  901. TIPC_OK,
  902. port_out_seqno(p_ptr),
  903. ack);
  904. }
  905. tipc_port_unlock(p_ptr);
  906. tipc_net_route_msg(buf);
  907. }
  908. /*
  909. * tipc_createport(): user level call. Will add port to
  910. * registry if non-zero user_ref.
  911. */
  912. int tipc_createport(u32 user_ref,
  913. void *usr_handle,
  914. unsigned int importance,
  915. tipc_msg_err_event error_cb,
  916. tipc_named_msg_err_event named_error_cb,
  917. tipc_conn_shutdown_event conn_error_cb,
  918. tipc_msg_event msg_cb,
  919. tipc_named_msg_event named_msg_cb,
  920. tipc_conn_msg_event conn_msg_cb,
  921. tipc_continue_event continue_event_cb,/* May be zero */
  922. u32 *portref)
  923. {
  924. struct user_port *up_ptr;
  925. struct port *p_ptr;
  926. u32 ref;
  927. up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
  928. if (!up_ptr) {
  929. warn("Port creation failed, no memory\n");
  930. return -ENOMEM;
  931. }
  932. ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
  933. p_ptr = tipc_port_lock(ref);
  934. if (!p_ptr) {
  935. kfree(up_ptr);
  936. return -ENOMEM;
  937. }
  938. p_ptr->user_port = up_ptr;
  939. up_ptr->user_ref = user_ref;
  940. up_ptr->usr_handle = usr_handle;
  941. up_ptr->ref = p_ptr->publ.ref;
  942. up_ptr->err_cb = error_cb;
  943. up_ptr->named_err_cb = named_error_cb;
  944. up_ptr->conn_err_cb = conn_error_cb;
  945. up_ptr->msg_cb = msg_cb;
  946. up_ptr->named_msg_cb = named_msg_cb;
  947. up_ptr->conn_msg_cb = conn_msg_cb;
  948. up_ptr->continue_event_cb = continue_event_cb;
  949. INIT_LIST_HEAD(&up_ptr->uport_list);
  950. tipc_reg_add_port(up_ptr);
  951. *portref = p_ptr->publ.ref;
  952. dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
  953. tipc_port_unlock(p_ptr);
  954. return TIPC_OK;
  955. }
  956. int tipc_ownidentity(u32 ref, struct tipc_portid *id)
  957. {
  958. id->ref = ref;
  959. id->node = tipc_own_addr;
  960. return TIPC_OK;
  961. }
  962. int tipc_portimportance(u32 ref, unsigned int *importance)
  963. {
  964. struct port *p_ptr;
  965. p_ptr = tipc_port_lock(ref);
  966. if (!p_ptr)
  967. return -EINVAL;
  968. *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
  969. spin_unlock_bh(p_ptr->publ.lock);
  970. return TIPC_OK;
  971. }
  972. int tipc_set_portimportance(u32 ref, unsigned int imp)
  973. {
  974. struct port *p_ptr;
  975. if (imp > TIPC_CRITICAL_IMPORTANCE)
  976. return -EINVAL;
  977. p_ptr = tipc_port_lock(ref);
  978. if (!p_ptr)
  979. return -EINVAL;
  980. msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
  981. spin_unlock_bh(p_ptr->publ.lock);
  982. return TIPC_OK;
  983. }
  984. int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  985. {
  986. struct port *p_ptr;
  987. struct publication *publ;
  988. u32 key;
  989. int res = -EINVAL;
  990. p_ptr = tipc_port_lock(ref);
  991. if (!p_ptr)
  992. return -EINVAL;
  993. dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
  994. "lower = %u, upper = %u\n",
  995. ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
  996. if (p_ptr->publ.connected)
  997. goto exit;
  998. if (seq->lower > seq->upper)
  999. goto exit;
  1000. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
  1001. goto exit;
  1002. key = ref + p_ptr->pub_count + 1;
  1003. if (key == ref) {
  1004. res = -EADDRINUSE;
  1005. goto exit;
  1006. }
  1007. publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
  1008. scope, p_ptr->publ.ref, key);
  1009. if (publ) {
  1010. list_add(&publ->pport_list, &p_ptr->publications);
  1011. p_ptr->pub_count++;
  1012. p_ptr->publ.published = 1;
  1013. res = TIPC_OK;
  1014. }
  1015. exit:
  1016. tipc_port_unlock(p_ptr);
  1017. return res;
  1018. }
  1019. int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
  1020. {
  1021. struct port *p_ptr;
  1022. struct publication *publ;
  1023. struct publication *tpubl;
  1024. int res = -EINVAL;
  1025. p_ptr = tipc_port_lock(ref);
  1026. if (!p_ptr)
  1027. return -EINVAL;
  1028. if (!seq) {
  1029. list_for_each_entry_safe(publ, tpubl,
  1030. &p_ptr->publications, pport_list) {
  1031. tipc_nametbl_withdraw(publ->type, publ->lower,
  1032. publ->ref, publ->key);
  1033. }
  1034. res = TIPC_OK;
  1035. } else {
  1036. list_for_each_entry_safe(publ, tpubl,
  1037. &p_ptr->publications, pport_list) {
  1038. if (publ->scope != scope)
  1039. continue;
  1040. if (publ->type != seq->type)
  1041. continue;
  1042. if (publ->lower != seq->lower)
  1043. continue;
  1044. if (publ->upper != seq->upper)
  1045. break;
  1046. tipc_nametbl_withdraw(publ->type, publ->lower,
  1047. publ->ref, publ->key);
  1048. res = TIPC_OK;
  1049. break;
  1050. }
  1051. }
  1052. if (list_empty(&p_ptr->publications))
  1053. p_ptr->publ.published = 0;
  1054. tipc_port_unlock(p_ptr);
  1055. return res;
  1056. }
  1057. int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
  1058. {
  1059. struct port *p_ptr;
  1060. struct tipc_msg *msg;
  1061. int res = -EINVAL;
  1062. p_ptr = tipc_port_lock(ref);
  1063. if (!p_ptr)
  1064. return -EINVAL;
  1065. if (p_ptr->publ.published || p_ptr->publ.connected)
  1066. goto exit;
  1067. if (!peer->ref)
  1068. goto exit;
  1069. msg = &p_ptr->publ.phdr;
  1070. msg_set_destnode(msg, peer->node);
  1071. msg_set_destport(msg, peer->ref);
  1072. msg_set_orignode(msg, tipc_own_addr);
  1073. msg_set_origport(msg, p_ptr->publ.ref);
  1074. msg_set_transp_seqno(msg, 42);
  1075. msg_set_type(msg, TIPC_CONN_MSG);
  1076. if (!may_route(peer->node))
  1077. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1078. else
  1079. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1080. p_ptr->probing_interval = PROBING_INTERVAL;
  1081. p_ptr->probing_state = CONFIRMED;
  1082. p_ptr->publ.connected = 1;
  1083. k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
  1084. tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
  1085. (void *)(unsigned long)ref,
  1086. (net_ev_handler)port_handle_node_down);
  1087. res = TIPC_OK;
  1088. exit:
  1089. tipc_port_unlock(p_ptr);
  1090. p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
  1091. return res;
  1092. }
  1093. /*
  1094. * tipc_disconnect(): Disconnect port form peer.
  1095. * This is a node local operation.
  1096. */
  1097. int tipc_disconnect(u32 ref)
  1098. {
  1099. struct port *p_ptr;
  1100. int res = -ENOTCONN;
  1101. p_ptr = tipc_port_lock(ref);
  1102. if (!p_ptr)
  1103. return -EINVAL;
  1104. if (p_ptr->publ.connected) {
  1105. p_ptr->publ.connected = 0;
  1106. /* let timer expire on it's own to avoid deadlock! */
  1107. tipc_nodesub_unsubscribe(&p_ptr->subscription);
  1108. res = TIPC_OK;
  1109. }
  1110. tipc_port_unlock(p_ptr);
  1111. return res;
  1112. }
  1113. /*
  1114. * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
  1115. */
  1116. int tipc_shutdown(u32 ref)
  1117. {
  1118. struct port *p_ptr;
  1119. struct sk_buff *buf = NULL;
  1120. p_ptr = tipc_port_lock(ref);
  1121. if (!p_ptr)
  1122. return -EINVAL;
  1123. if (p_ptr->publ.connected) {
  1124. u32 imp = msg_importance(&p_ptr->publ.phdr);
  1125. if (imp < TIPC_CRITICAL_IMPORTANCE)
  1126. imp++;
  1127. buf = port_build_proto_msg(port_peerport(p_ptr),
  1128. port_peernode(p_ptr),
  1129. ref,
  1130. tipc_own_addr,
  1131. imp,
  1132. TIPC_CONN_MSG,
  1133. TIPC_CONN_SHUTDOWN,
  1134. port_out_seqno(p_ptr),
  1135. 0);
  1136. }
  1137. tipc_port_unlock(p_ptr);
  1138. tipc_net_route_msg(buf);
  1139. return tipc_disconnect(ref);
  1140. }
  1141. int tipc_isconnected(u32 ref, int *isconnected)
  1142. {
  1143. struct port *p_ptr;
  1144. p_ptr = tipc_port_lock(ref);
  1145. if (!p_ptr)
  1146. return -EINVAL;
  1147. *isconnected = p_ptr->publ.connected;
  1148. tipc_port_unlock(p_ptr);
  1149. return TIPC_OK;
  1150. }
  1151. int tipc_peer(u32 ref, struct tipc_portid *peer)
  1152. {
  1153. struct port *p_ptr;
  1154. int res;
  1155. p_ptr = tipc_port_lock(ref);
  1156. if (!p_ptr)
  1157. return -EINVAL;
  1158. if (p_ptr->publ.connected) {
  1159. peer->ref = port_peerport(p_ptr);
  1160. peer->node = port_peernode(p_ptr);
  1161. res = TIPC_OK;
  1162. } else
  1163. res = -ENOTCONN;
  1164. tipc_port_unlock(p_ptr);
  1165. return res;
  1166. }
  1167. int tipc_ref_valid(u32 ref)
  1168. {
  1169. /* Works irrespective of type */
  1170. return !!tipc_ref_deref(ref);
  1171. }
  1172. /*
  1173. * tipc_port_recv_sections(): Concatenate and deliver sectioned
  1174. * message for this node.
  1175. */
  1176. int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
  1177. struct iovec const *msg_sect)
  1178. {
  1179. struct sk_buff *buf;
  1180. int res;
  1181. res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
  1182. MAX_MSG_SIZE, !sender->user_port, &buf);
  1183. if (likely(buf))
  1184. tipc_port_recv_msg(buf);
  1185. return res;
  1186. }
  1187. /**
  1188. * tipc_send - send message sections on connection
  1189. */
  1190. int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
  1191. {
  1192. struct port *p_ptr;
  1193. u32 destnode;
  1194. int res;
  1195. p_ptr = tipc_port_deref(ref);
  1196. if (!p_ptr || !p_ptr->publ.connected)
  1197. return -EINVAL;
  1198. p_ptr->publ.congested = 1;
  1199. if (!tipc_port_congested(p_ptr)) {
  1200. destnode = port_peernode(p_ptr);
  1201. if (likely(destnode != tipc_own_addr))
  1202. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1203. destnode);
  1204. else
  1205. res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1206. if (likely(res != -ELINKCONG)) {
  1207. port_incr_out_seqno(p_ptr);
  1208. p_ptr->publ.congested = 0;
  1209. p_ptr->sent++;
  1210. return res;
  1211. }
  1212. }
  1213. if (port_unreliable(p_ptr)) {
  1214. p_ptr->publ.congested = 0;
  1215. /* Just calculate msg length and return */
  1216. return msg_calc_data_size(msg_sect, num_sect);
  1217. }
  1218. return -ELINKCONG;
  1219. }
  1220. /**
  1221. * tipc_send_buf - send message buffer on connection
  1222. */
  1223. int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
  1224. {
  1225. struct port *p_ptr;
  1226. struct tipc_msg *msg;
  1227. u32 destnode;
  1228. u32 hsz;
  1229. u32 sz;
  1230. u32 res;
  1231. p_ptr = tipc_port_deref(ref);
  1232. if (!p_ptr || !p_ptr->publ.connected)
  1233. return -EINVAL;
  1234. msg = &p_ptr->publ.phdr;
  1235. hsz = msg_hdr_sz(msg);
  1236. sz = hsz + dsz;
  1237. msg_set_size(msg, sz);
  1238. if (skb_cow(buf, hsz))
  1239. return -ENOMEM;
  1240. skb_push(buf, hsz);
  1241. memcpy(buf->data, (unchar *)msg, hsz);
  1242. destnode = msg_destnode(msg);
  1243. p_ptr->publ.congested = 1;
  1244. if (!tipc_port_congested(p_ptr)) {
  1245. if (likely(destnode != tipc_own_addr))
  1246. res = tipc_send_buf_fast(buf, destnode);
  1247. else {
  1248. tipc_port_recv_msg(buf);
  1249. res = sz;
  1250. }
  1251. if (likely(res != -ELINKCONG)) {
  1252. port_incr_out_seqno(p_ptr);
  1253. p_ptr->sent++;
  1254. p_ptr->publ.congested = 0;
  1255. return res;
  1256. }
  1257. }
  1258. if (port_unreliable(p_ptr)) {
  1259. p_ptr->publ.congested = 0;
  1260. return dsz;
  1261. }
  1262. return -ELINKCONG;
  1263. }
  1264. /**
  1265. * tipc_forward2name - forward message sections to port name
  1266. */
  1267. int tipc_forward2name(u32 ref,
  1268. struct tipc_name const *name,
  1269. u32 domain,
  1270. u32 num_sect,
  1271. struct iovec const *msg_sect,
  1272. struct tipc_portid const *orig,
  1273. unsigned int importance)
  1274. {
  1275. struct port *p_ptr;
  1276. struct tipc_msg *msg;
  1277. u32 destnode = domain;
  1278. u32 destport = 0;
  1279. int res;
  1280. p_ptr = tipc_port_deref(ref);
  1281. if (!p_ptr || p_ptr->publ.connected)
  1282. return -EINVAL;
  1283. msg = &p_ptr->publ.phdr;
  1284. msg_set_type(msg, TIPC_NAMED_MSG);
  1285. msg_set_orignode(msg, orig->node);
  1286. msg_set_origport(msg, orig->ref);
  1287. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1288. msg_set_nametype(msg, name->type);
  1289. msg_set_nameinst(msg, name->instance);
  1290. msg_set_lookup_scope(msg, addr_scope(domain));
  1291. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1292. msg_set_importance(msg,importance);
  1293. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1294. msg_set_destnode(msg, destnode);
  1295. msg_set_destport(msg, destport);
  1296. if (likely(destport || destnode)) {
  1297. p_ptr->sent++;
  1298. if (likely(destnode == tipc_own_addr))
  1299. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1300. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
  1301. destnode);
  1302. if (likely(res != -ELINKCONG))
  1303. return res;
  1304. if (port_unreliable(p_ptr)) {
  1305. /* Just calculate msg length and return */
  1306. return msg_calc_data_size(msg_sect, num_sect);
  1307. }
  1308. return -ELINKCONG;
  1309. }
  1310. return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
  1311. TIPC_ERR_NO_NAME);
  1312. }
  1313. /**
  1314. * tipc_send2name - send message sections to port name
  1315. */
  1316. int tipc_send2name(u32 ref,
  1317. struct tipc_name const *name,
  1318. unsigned int domain,
  1319. unsigned int num_sect,
  1320. struct iovec const *msg_sect)
  1321. {
  1322. struct tipc_portid orig;
  1323. orig.ref = ref;
  1324. orig.node = tipc_own_addr;
  1325. return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
  1326. TIPC_PORT_IMPORTANCE);
  1327. }
  1328. /**
  1329. * tipc_forward_buf2name - forward message buffer to port name
  1330. */
  1331. int tipc_forward_buf2name(u32 ref,
  1332. struct tipc_name const *name,
  1333. u32 domain,
  1334. struct sk_buff *buf,
  1335. unsigned int dsz,
  1336. struct tipc_portid const *orig,
  1337. unsigned int importance)
  1338. {
  1339. struct port *p_ptr;
  1340. struct tipc_msg *msg;
  1341. u32 destnode = domain;
  1342. u32 destport = 0;
  1343. int res;
  1344. p_ptr = (struct port *)tipc_ref_deref(ref);
  1345. if (!p_ptr || p_ptr->publ.connected)
  1346. return -EINVAL;
  1347. msg = &p_ptr->publ.phdr;
  1348. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1349. msg_set_importance(msg, importance);
  1350. msg_set_type(msg, TIPC_NAMED_MSG);
  1351. msg_set_orignode(msg, orig->node);
  1352. msg_set_origport(msg, orig->ref);
  1353. msg_set_nametype(msg, name->type);
  1354. msg_set_nameinst(msg, name->instance);
  1355. msg_set_lookup_scope(msg, addr_scope(domain));
  1356. msg_set_hdr_sz(msg, LONG_H_SIZE);
  1357. msg_set_size(msg, LONG_H_SIZE + dsz);
  1358. destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
  1359. msg_set_destnode(msg, destnode);
  1360. msg_set_destport(msg, destport);
  1361. msg_dbg(msg, "forw2name ==> ");
  1362. if (skb_cow(buf, LONG_H_SIZE))
  1363. return -ENOMEM;
  1364. skb_push(buf, LONG_H_SIZE);
  1365. memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
  1366. msg_dbg(buf_msg(buf),"PREP:");
  1367. if (likely(destport || destnode)) {
  1368. p_ptr->sent++;
  1369. if (destnode == tipc_own_addr)
  1370. return tipc_port_recv_msg(buf);
  1371. res = tipc_send_buf_fast(buf, destnode);
  1372. if (likely(res != -ELINKCONG))
  1373. return res;
  1374. if (port_unreliable(p_ptr))
  1375. return dsz;
  1376. return -ELINKCONG;
  1377. }
  1378. return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
  1379. }
  1380. /**
  1381. * tipc_send_buf2name - send message buffer to port name
  1382. */
  1383. int tipc_send_buf2name(u32 ref,
  1384. struct tipc_name const *dest,
  1385. u32 domain,
  1386. struct sk_buff *buf,
  1387. unsigned int dsz)
  1388. {
  1389. struct tipc_portid orig;
  1390. orig.ref = ref;
  1391. orig.node = tipc_own_addr;
  1392. return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
  1393. TIPC_PORT_IMPORTANCE);
  1394. }
  1395. /**
  1396. * tipc_forward2port - forward message sections to port identity
  1397. */
  1398. int tipc_forward2port(u32 ref,
  1399. struct tipc_portid const *dest,
  1400. unsigned int num_sect,
  1401. struct iovec const *msg_sect,
  1402. struct tipc_portid const *orig,
  1403. unsigned int importance)
  1404. {
  1405. struct port *p_ptr;
  1406. struct tipc_msg *msg;
  1407. int res;
  1408. p_ptr = tipc_port_deref(ref);
  1409. if (!p_ptr || p_ptr->publ.connected)
  1410. return -EINVAL;
  1411. msg = &p_ptr->publ.phdr;
  1412. msg_set_type(msg, TIPC_DIRECT_MSG);
  1413. msg_set_orignode(msg, orig->node);
  1414. msg_set_origport(msg, orig->ref);
  1415. msg_set_destnode(msg, dest->node);
  1416. msg_set_destport(msg, dest->ref);
  1417. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1418. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1419. msg_set_importance(msg, importance);
  1420. p_ptr->sent++;
  1421. if (dest->node == tipc_own_addr)
  1422. return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
  1423. res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
  1424. if (likely(res != -ELINKCONG))
  1425. return res;
  1426. if (port_unreliable(p_ptr)) {
  1427. /* Just calculate msg length and return */
  1428. return msg_calc_data_size(msg_sect, num_sect);
  1429. }
  1430. return -ELINKCONG;
  1431. }
  1432. /**
  1433. * tipc_send2port - send message sections to port identity
  1434. */
  1435. int tipc_send2port(u32 ref,
  1436. struct tipc_portid const *dest,
  1437. unsigned int num_sect,
  1438. struct iovec const *msg_sect)
  1439. {
  1440. struct tipc_portid orig;
  1441. orig.ref = ref;
  1442. orig.node = tipc_own_addr;
  1443. return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
  1444. TIPC_PORT_IMPORTANCE);
  1445. }
  1446. /**
  1447. * tipc_forward_buf2port - forward message buffer to port identity
  1448. */
  1449. int tipc_forward_buf2port(u32 ref,
  1450. struct tipc_portid const *dest,
  1451. struct sk_buff *buf,
  1452. unsigned int dsz,
  1453. struct tipc_portid const *orig,
  1454. unsigned int importance)
  1455. {
  1456. struct port *p_ptr;
  1457. struct tipc_msg *msg;
  1458. int res;
  1459. p_ptr = (struct port *)tipc_ref_deref(ref);
  1460. if (!p_ptr || p_ptr->publ.connected)
  1461. return -EINVAL;
  1462. msg = &p_ptr->publ.phdr;
  1463. msg_set_type(msg, TIPC_DIRECT_MSG);
  1464. msg_set_orignode(msg, orig->node);
  1465. msg_set_origport(msg, orig->ref);
  1466. msg_set_destnode(msg, dest->node);
  1467. msg_set_destport(msg, dest->ref);
  1468. msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
  1469. if (importance <= TIPC_CRITICAL_IMPORTANCE)
  1470. msg_set_importance(msg, importance);
  1471. msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
  1472. if (skb_cow(buf, DIR_MSG_H_SIZE))
  1473. return -ENOMEM;
  1474. skb_push(buf, DIR_MSG_H_SIZE);
  1475. memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
  1476. msg_dbg(msg, "buf2port: ");
  1477. p_ptr->sent++;
  1478. if (dest->node == tipc_own_addr)
  1479. return tipc_port_recv_msg(buf);
  1480. res = tipc_send_buf_fast(buf, dest->node);
  1481. if (likely(res != -ELINKCONG))
  1482. return res;
  1483. if (port_unreliable(p_ptr))
  1484. return dsz;
  1485. return -ELINKCONG;
  1486. }
  1487. /**
  1488. * tipc_send_buf2port - send message buffer to port identity
  1489. */
  1490. int tipc_send_buf2port(u32 ref,
  1491. struct tipc_portid const *dest,
  1492. struct sk_buff *buf,
  1493. unsigned int dsz)
  1494. {
  1495. struct tipc_portid orig;
  1496. orig.ref = ref;
  1497. orig.node = tipc_own_addr;
  1498. return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
  1499. TIPC_PORT_IMPORTANCE);
  1500. }