port.c 41 KB

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