port.c 40 KB

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