port.c 40 KB

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