topsrv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * net/tipc/server.c: TIPC server infrastructure
  3. *
  4. * Copyright (c) 2012-2013, Wind River Systems
  5. * Copyright (c) 2017-2018, Ericsson AB
  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 "subscr.h"
  37. #include "topsrv.h"
  38. #include "core.h"
  39. #include "socket.h"
  40. #include "addr.h"
  41. #include "msg.h"
  42. #include "bearer.h"
  43. #include <net/sock.h>
  44. #include <linux/module.h>
  45. /* Number of messages to send before rescheduling */
  46. #define MAX_SEND_MSG_COUNT 25
  47. #define MAX_RECV_MSG_COUNT 25
  48. #define CF_CONNECTED 1
  49. #define TIPC_SERVER_NAME_LEN 32
  50. /**
  51. * struct tipc_topsrv - TIPC server structure
  52. * @conn_idr: identifier set of connection
  53. * @idr_lock: protect the connection identifier set
  54. * @idr_in_use: amount of allocated identifier entry
  55. * @net: network namspace instance
  56. * @awork: accept work item
  57. * @rcv_wq: receive workqueue
  58. * @send_wq: send workqueue
  59. * @listener: topsrv listener socket
  60. * @name: server name
  61. */
  62. struct tipc_topsrv {
  63. struct idr conn_idr;
  64. spinlock_t idr_lock; /* for idr list */
  65. int idr_in_use;
  66. struct net *net;
  67. struct work_struct awork;
  68. struct workqueue_struct *rcv_wq;
  69. struct workqueue_struct *send_wq;
  70. struct socket *listener;
  71. char name[TIPC_SERVER_NAME_LEN];
  72. };
  73. /**
  74. * struct tipc_conn - TIPC connection structure
  75. * @kref: reference counter to connection object
  76. * @conid: connection identifier
  77. * @sock: socket handler associated with connection
  78. * @flags: indicates connection state
  79. * @server: pointer to connected server
  80. * @sub_list: lsit to all pertaing subscriptions
  81. * @sub_lock: lock protecting the subscription list
  82. * @rwork: receive work item
  83. * @outqueue: pointer to first outbound message in queue
  84. * @outqueue_lock: control access to the outqueue
  85. * @swork: send work item
  86. */
  87. struct tipc_conn {
  88. struct kref kref;
  89. int conid;
  90. struct socket *sock;
  91. unsigned long flags;
  92. struct tipc_topsrv *server;
  93. struct list_head sub_list;
  94. spinlock_t sub_lock; /* for subscription list */
  95. struct work_struct rwork;
  96. struct list_head outqueue;
  97. spinlock_t outqueue_lock; /* for outqueue */
  98. struct work_struct swork;
  99. };
  100. /* An entry waiting to be sent */
  101. struct outqueue_entry {
  102. bool inactive;
  103. struct tipc_event evt;
  104. struct list_head list;
  105. };
  106. static void tipc_conn_recv_work(struct work_struct *work);
  107. static void tipc_conn_send_work(struct work_struct *work);
  108. static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt);
  109. static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s);
  110. static bool connected(struct tipc_conn *con)
  111. {
  112. return con && test_bit(CF_CONNECTED, &con->flags);
  113. }
  114. static void tipc_conn_kref_release(struct kref *kref)
  115. {
  116. struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
  117. struct tipc_topsrv *s = con->server;
  118. struct outqueue_entry *e, *safe;
  119. spin_lock_bh(&s->idr_lock);
  120. idr_remove(&s->conn_idr, con->conid);
  121. s->idr_in_use--;
  122. spin_unlock_bh(&s->idr_lock);
  123. if (con->sock)
  124. sock_release(con->sock);
  125. spin_lock_bh(&con->outqueue_lock);
  126. list_for_each_entry_safe(e, safe, &con->outqueue, list) {
  127. list_del(&e->list);
  128. kfree(e);
  129. }
  130. spin_unlock_bh(&con->outqueue_lock);
  131. kfree(con);
  132. }
  133. static void conn_put(struct tipc_conn *con)
  134. {
  135. kref_put(&con->kref, tipc_conn_kref_release);
  136. }
  137. static void conn_get(struct tipc_conn *con)
  138. {
  139. kref_get(&con->kref);
  140. }
  141. static void tipc_conn_close(struct tipc_conn *con)
  142. {
  143. struct sock *sk = con->sock->sk;
  144. bool disconnect = false;
  145. write_lock_bh(&sk->sk_callback_lock);
  146. disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
  147. if (disconnect) {
  148. sk->sk_user_data = NULL;
  149. tipc_conn_delete_sub(con, NULL);
  150. }
  151. write_unlock_bh(&sk->sk_callback_lock);
  152. /* Handle concurrent calls from sending and receiving threads */
  153. if (!disconnect)
  154. return;
  155. /* Don't flush pending works, -just let them expire */
  156. kernel_sock_shutdown(con->sock, SHUT_RDWR);
  157. conn_put(con);
  158. }
  159. static struct tipc_conn *tipc_conn_alloc(struct tipc_topsrv *s, struct socket *sock)
  160. {
  161. struct tipc_conn *con;
  162. int ret;
  163. con = kzalloc(sizeof(*con), GFP_ATOMIC);
  164. if (!con)
  165. return ERR_PTR(-ENOMEM);
  166. kref_init(&con->kref);
  167. INIT_LIST_HEAD(&con->outqueue);
  168. INIT_LIST_HEAD(&con->sub_list);
  169. spin_lock_init(&con->outqueue_lock);
  170. spin_lock_init(&con->sub_lock);
  171. INIT_WORK(&con->swork, tipc_conn_send_work);
  172. INIT_WORK(&con->rwork, tipc_conn_recv_work);
  173. spin_lock_bh(&s->idr_lock);
  174. ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
  175. if (ret < 0) {
  176. kfree(con);
  177. spin_unlock_bh(&s->idr_lock);
  178. return ERR_PTR(-ENOMEM);
  179. }
  180. con->conid = ret;
  181. s->idr_in_use++;
  182. set_bit(CF_CONNECTED, &con->flags);
  183. con->server = s;
  184. con->sock = sock;
  185. conn_get(con);
  186. spin_unlock_bh(&s->idr_lock);
  187. return con;
  188. }
  189. static struct tipc_conn *tipc_conn_lookup(struct tipc_topsrv *s, int conid)
  190. {
  191. struct tipc_conn *con;
  192. spin_lock_bh(&s->idr_lock);
  193. con = idr_find(&s->conn_idr, conid);
  194. if (!connected(con) || !kref_get_unless_zero(&con->kref))
  195. con = NULL;
  196. spin_unlock_bh(&s->idr_lock);
  197. return con;
  198. }
  199. /* tipc_conn_delete_sub - delete a specific or all subscriptions
  200. * for a given subscriber
  201. */
  202. static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
  203. {
  204. struct tipc_net *tn = tipc_net(con->server->net);
  205. struct list_head *sub_list = &con->sub_list;
  206. struct tipc_subscription *sub, *tmp;
  207. spin_lock_bh(&con->sub_lock);
  208. list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
  209. if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
  210. tipc_sub_unsubscribe(sub);
  211. atomic_dec(&tn->subscription_count);
  212. if (s)
  213. break;
  214. }
  215. }
  216. spin_unlock_bh(&con->sub_lock);
  217. }
  218. static void tipc_conn_send_to_sock(struct tipc_conn *con)
  219. {
  220. struct list_head *queue = &con->outqueue;
  221. struct tipc_topsrv *srv = con->server;
  222. struct outqueue_entry *e;
  223. struct tipc_event *evt;
  224. struct msghdr msg;
  225. struct kvec iov;
  226. int count = 0;
  227. int ret;
  228. spin_lock_bh(&con->outqueue_lock);
  229. while (!list_empty(queue)) {
  230. e = list_first_entry(queue, struct outqueue_entry, list);
  231. evt = &e->evt;
  232. spin_unlock_bh(&con->outqueue_lock);
  233. if (e->inactive)
  234. tipc_conn_delete_sub(con, &evt->s);
  235. memset(&msg, 0, sizeof(msg));
  236. msg.msg_flags = MSG_DONTWAIT;
  237. iov.iov_base = evt;
  238. iov.iov_len = sizeof(*evt);
  239. msg.msg_name = NULL;
  240. if (con->sock) {
  241. ret = kernel_sendmsg(con->sock, &msg, &iov,
  242. 1, sizeof(*evt));
  243. if (ret == -EWOULDBLOCK || ret == 0) {
  244. cond_resched();
  245. return;
  246. } else if (ret < 0) {
  247. return tipc_conn_close(con);
  248. }
  249. } else {
  250. tipc_topsrv_kern_evt(srv->net, evt);
  251. }
  252. /* Don't starve users filling buffers */
  253. if (++count >= MAX_SEND_MSG_COUNT) {
  254. cond_resched();
  255. count = 0;
  256. }
  257. spin_lock_bh(&con->outqueue_lock);
  258. list_del(&e->list);
  259. kfree(e);
  260. }
  261. spin_unlock_bh(&con->outqueue_lock);
  262. }
  263. static void tipc_conn_send_work(struct work_struct *work)
  264. {
  265. struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
  266. if (connected(con))
  267. tipc_conn_send_to_sock(con);
  268. conn_put(con);
  269. }
  270. /* tipc_topsrv_queue_evt() - interrupt level call from a subscription instance
  271. * The queued work is launched into tipc_conn_send_work()->tipc_conn_send_to_sock()
  272. */
  273. void tipc_topsrv_queue_evt(struct net *net, int conid,
  274. u32 event, struct tipc_event *evt)
  275. {
  276. struct tipc_topsrv *srv = tipc_topsrv(net);
  277. struct outqueue_entry *e;
  278. struct tipc_conn *con;
  279. con = tipc_conn_lookup(srv, conid);
  280. if (!con)
  281. return;
  282. if (!connected(con))
  283. goto err;
  284. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  285. if (!e)
  286. goto err;
  287. e->inactive = (event == TIPC_SUBSCR_TIMEOUT);
  288. memcpy(&e->evt, evt, sizeof(*evt));
  289. spin_lock_bh(&con->outqueue_lock);
  290. list_add_tail(&e->list, &con->outqueue);
  291. spin_unlock_bh(&con->outqueue_lock);
  292. if (queue_work(srv->send_wq, &con->swork))
  293. return;
  294. err:
  295. conn_put(con);
  296. }
  297. /* tipc_conn_write_space - interrupt callback after a sendmsg EAGAIN
  298. * Indicates that there now is more space in the send buffer
  299. * The queued work is launched into tipc_send_work()->tipc_conn_send_to_sock()
  300. */
  301. static void tipc_conn_write_space(struct sock *sk)
  302. {
  303. struct tipc_conn *con;
  304. read_lock_bh(&sk->sk_callback_lock);
  305. con = sk->sk_user_data;
  306. if (connected(con)) {
  307. conn_get(con);
  308. if (!queue_work(con->server->send_wq, &con->swork))
  309. conn_put(con);
  310. }
  311. read_unlock_bh(&sk->sk_callback_lock);
  312. }
  313. static int tipc_conn_rcv_sub(struct tipc_topsrv *srv,
  314. struct tipc_conn *con,
  315. struct tipc_subscr *s)
  316. {
  317. struct tipc_net *tn = tipc_net(srv->net);
  318. struct tipc_subscription *sub;
  319. u32 s_filter = tipc_sub_read(s, filter);
  320. if (s_filter & TIPC_SUB_CANCEL) {
  321. tipc_sub_write(s, filter, s_filter & ~TIPC_SUB_CANCEL);
  322. tipc_conn_delete_sub(con, s);
  323. return 0;
  324. }
  325. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) {
  326. pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR);
  327. return -1;
  328. }
  329. sub = tipc_sub_subscribe(srv->net, s, con->conid);
  330. if (!sub)
  331. return -1;
  332. atomic_inc(&tn->subscription_count);
  333. spin_lock_bh(&con->sub_lock);
  334. list_add(&sub->sub_list, &con->sub_list);
  335. spin_unlock_bh(&con->sub_lock);
  336. return 0;
  337. }
  338. static int tipc_conn_rcv_from_sock(struct tipc_conn *con)
  339. {
  340. struct tipc_topsrv *srv = con->server;
  341. struct sock *sk = con->sock->sk;
  342. struct msghdr msg = {};
  343. struct tipc_subscr s;
  344. struct kvec iov;
  345. int ret;
  346. iov.iov_base = &s;
  347. iov.iov_len = sizeof(s);
  348. msg.msg_name = NULL;
  349. iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, iov.iov_len);
  350. ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT);
  351. if (ret == -EWOULDBLOCK)
  352. return -EWOULDBLOCK;
  353. if (ret == sizeof(s)) {
  354. read_lock_bh(&sk->sk_callback_lock);
  355. /* RACE: the connection can be closed in the meantime */
  356. if (likely(connected(con)))
  357. ret = tipc_conn_rcv_sub(srv, con, &s);
  358. read_unlock_bh(&sk->sk_callback_lock);
  359. if (!ret)
  360. return 0;
  361. }
  362. tipc_conn_close(con);
  363. return ret;
  364. }
  365. static void tipc_conn_recv_work(struct work_struct *work)
  366. {
  367. struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
  368. int count = 0;
  369. while (connected(con)) {
  370. if (tipc_conn_rcv_from_sock(con))
  371. break;
  372. /* Don't flood Rx machine */
  373. if (++count >= MAX_RECV_MSG_COUNT) {
  374. cond_resched();
  375. count = 0;
  376. }
  377. }
  378. conn_put(con);
  379. }
  380. /* tipc_conn_data_ready - interrupt callback indicating the socket has data
  381. * The queued work is launched into tipc_recv_work()->tipc_conn_rcv_from_sock()
  382. */
  383. static void tipc_conn_data_ready(struct sock *sk)
  384. {
  385. struct tipc_conn *con;
  386. read_lock_bh(&sk->sk_callback_lock);
  387. con = sk->sk_user_data;
  388. if (connected(con)) {
  389. conn_get(con);
  390. if (!queue_work(con->server->rcv_wq, &con->rwork))
  391. conn_put(con);
  392. }
  393. read_unlock_bh(&sk->sk_callback_lock);
  394. }
  395. static void tipc_topsrv_accept(struct work_struct *work)
  396. {
  397. struct tipc_topsrv *srv = container_of(work, struct tipc_topsrv, awork);
  398. struct socket *newsock, *lsock;
  399. struct tipc_conn *con;
  400. struct sock *newsk;
  401. int ret;
  402. spin_lock_bh(&srv->idr_lock);
  403. if (!srv->listener) {
  404. spin_unlock_bh(&srv->idr_lock);
  405. return;
  406. }
  407. lsock = srv->listener;
  408. spin_unlock_bh(&srv->idr_lock);
  409. while (1) {
  410. ret = kernel_accept(lsock, &newsock, O_NONBLOCK);
  411. if (ret < 0)
  412. return;
  413. con = tipc_conn_alloc(srv, newsock);
  414. if (IS_ERR(con)) {
  415. ret = PTR_ERR(con);
  416. sock_release(newsock);
  417. return;
  418. }
  419. /* Register callbacks */
  420. newsk = newsock->sk;
  421. write_lock_bh(&newsk->sk_callback_lock);
  422. newsk->sk_data_ready = tipc_conn_data_ready;
  423. newsk->sk_write_space = tipc_conn_write_space;
  424. newsk->sk_user_data = con;
  425. write_unlock_bh(&newsk->sk_callback_lock);
  426. /* Wake up receive process in case of 'SYN+' message */
  427. newsk->sk_data_ready(newsk);
  428. conn_put(con);
  429. }
  430. }
  431. /* tipc_topsrv_listener_data_ready - interrupt callback with connection request
  432. * The queued job is launched into tipc_topsrv_accept()
  433. */
  434. static void tipc_topsrv_listener_data_ready(struct sock *sk)
  435. {
  436. struct tipc_topsrv *srv;
  437. read_lock_bh(&sk->sk_callback_lock);
  438. srv = sk->sk_user_data;
  439. if (srv)
  440. queue_work(srv->rcv_wq, &srv->awork);
  441. read_unlock_bh(&sk->sk_callback_lock);
  442. }
  443. static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)
  444. {
  445. struct socket *lsock = NULL;
  446. struct sockaddr_tipc saddr;
  447. struct sock *sk;
  448. int rc;
  449. rc = sock_create_kern(srv->net, AF_TIPC, SOCK_SEQPACKET, 0, &lsock);
  450. if (rc < 0)
  451. return rc;
  452. srv->listener = lsock;
  453. sk = lsock->sk;
  454. write_lock_bh(&sk->sk_callback_lock);
  455. sk->sk_data_ready = tipc_topsrv_listener_data_ready;
  456. sk->sk_user_data = srv;
  457. write_unlock_bh(&sk->sk_callback_lock);
  458. lock_sock(sk);
  459. rc = tsk_set_importance(sk, TIPC_CRITICAL_IMPORTANCE);
  460. release_sock(sk);
  461. if (rc < 0)
  462. goto err;
  463. saddr.family = AF_TIPC;
  464. saddr.addrtype = TIPC_SERVICE_RANGE;
  465. saddr.addr.nameseq.type = TIPC_TOP_SRV;
  466. saddr.addr.nameseq.lower = TIPC_TOP_SRV;
  467. saddr.addr.nameseq.upper = TIPC_TOP_SRV;
  468. saddr.scope = TIPC_NODE_SCOPE;
  469. rc = tipc_sk_bind(lsock, (struct sockaddr *)&saddr, sizeof(saddr));
  470. if (rc < 0)
  471. goto err;
  472. rc = kernel_listen(lsock, 0);
  473. if (rc < 0)
  474. goto err;
  475. /* As server's listening socket owner and creator is the same module,
  476. * we have to decrease TIPC module reference count to guarantee that
  477. * it remains zero after the server socket is created, otherwise,
  478. * executing "rmmod" command is unable to make TIPC module deleted
  479. * after TIPC module is inserted successfully.
  480. *
  481. * However, the reference count is ever increased twice in
  482. * sock_create_kern(): one is to increase the reference count of owner
  483. * of TIPC socket's proto_ops struct; another is to increment the
  484. * reference count of owner of TIPC proto struct. Therefore, we must
  485. * decrement the module reference count twice to ensure that it keeps
  486. * zero after server's listening socket is created. Of course, we
  487. * must bump the module reference count twice as well before the socket
  488. * is closed.
  489. */
  490. module_put(lsock->ops->owner);
  491. module_put(sk->sk_prot_creator->owner);
  492. return 0;
  493. err:
  494. sock_release(lsock);
  495. return -EINVAL;
  496. }
  497. bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
  498. u32 upper, u32 filter, int *conid)
  499. {
  500. struct tipc_subscr sub;
  501. struct tipc_conn *con;
  502. int rc;
  503. sub.seq.type = type;
  504. sub.seq.lower = lower;
  505. sub.seq.upper = upper;
  506. sub.timeout = TIPC_WAIT_FOREVER;
  507. sub.filter = filter;
  508. *(u64 *)&sub.usr_handle = (u64)port;
  509. con = tipc_conn_alloc(tipc_topsrv(net), NULL);
  510. if (IS_ERR(con))
  511. return false;
  512. *conid = con->conid;
  513. rc = tipc_conn_rcv_sub(tipc_topsrv(net), con, &sub);
  514. if (rc)
  515. conn_put(con);
  516. conn_put(con);
  517. return !rc;
  518. }
  519. void tipc_topsrv_kern_unsubscr(struct net *net, int conid)
  520. {
  521. struct tipc_conn *con;
  522. con = tipc_conn_lookup(tipc_topsrv(net), conid);
  523. if (!con)
  524. return;
  525. test_and_clear_bit(CF_CONNECTED, &con->flags);
  526. tipc_conn_delete_sub(con, NULL);
  527. conn_put(con);
  528. conn_put(con);
  529. }
  530. static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt)
  531. {
  532. u32 port = *(u32 *)&evt->s.usr_handle;
  533. u32 self = tipc_own_addr(net);
  534. struct sk_buff_head evtq;
  535. struct sk_buff *skb;
  536. skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt),
  537. self, self, port, port, 0);
  538. if (!skb)
  539. return;
  540. msg_set_dest_droppable(buf_msg(skb), true);
  541. memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt));
  542. skb_queue_head_init(&evtq);
  543. __skb_queue_tail(&evtq, skb);
  544. tipc_loopback_trace(net, &evtq);
  545. tipc_sk_rcv(net, &evtq);
  546. }
  547. static int tipc_topsrv_work_start(struct tipc_topsrv *s)
  548. {
  549. s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
  550. if (!s->rcv_wq) {
  551. pr_err("can't start tipc receive workqueue\n");
  552. return -ENOMEM;
  553. }
  554. s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
  555. if (!s->send_wq) {
  556. pr_err("can't start tipc send workqueue\n");
  557. destroy_workqueue(s->rcv_wq);
  558. return -ENOMEM;
  559. }
  560. return 0;
  561. }
  562. static void tipc_topsrv_work_stop(struct tipc_topsrv *s)
  563. {
  564. destroy_workqueue(s->rcv_wq);
  565. destroy_workqueue(s->send_wq);
  566. }
  567. static int tipc_topsrv_start(struct net *net)
  568. {
  569. struct tipc_net *tn = tipc_net(net);
  570. const char name[] = "topology_server";
  571. struct tipc_topsrv *srv;
  572. int ret;
  573. srv = kzalloc(sizeof(*srv), GFP_ATOMIC);
  574. if (!srv)
  575. return -ENOMEM;
  576. srv->net = net;
  577. INIT_WORK(&srv->awork, tipc_topsrv_accept);
  578. strscpy(srv->name, name, sizeof(srv->name));
  579. tn->topsrv = srv;
  580. atomic_set(&tn->subscription_count, 0);
  581. spin_lock_init(&srv->idr_lock);
  582. idr_init(&srv->conn_idr);
  583. srv->idr_in_use = 0;
  584. ret = tipc_topsrv_work_start(srv);
  585. if (ret < 0)
  586. goto err_start;
  587. ret = tipc_topsrv_create_listener(srv);
  588. if (ret < 0)
  589. goto err_create;
  590. return 0;
  591. err_create:
  592. tipc_topsrv_work_stop(srv);
  593. err_start:
  594. kfree(srv);
  595. return ret;
  596. }
  597. static void tipc_topsrv_stop(struct net *net)
  598. {
  599. struct tipc_topsrv *srv = tipc_topsrv(net);
  600. struct socket *lsock = srv->listener;
  601. struct tipc_conn *con;
  602. int id;
  603. spin_lock_bh(&srv->idr_lock);
  604. for (id = 0; srv->idr_in_use; id++) {
  605. con = idr_find(&srv->conn_idr, id);
  606. if (con) {
  607. spin_unlock_bh(&srv->idr_lock);
  608. tipc_conn_close(con);
  609. spin_lock_bh(&srv->idr_lock);
  610. }
  611. }
  612. __module_get(lsock->ops->owner);
  613. __module_get(lsock->sk->sk_prot_creator->owner);
  614. srv->listener = NULL;
  615. spin_unlock_bh(&srv->idr_lock);
  616. tipc_topsrv_work_stop(srv);
  617. sock_release(lsock);
  618. idr_destroy(&srv->conn_idr);
  619. kfree(srv);
  620. }
  621. int __net_init tipc_topsrv_init_net(struct net *net)
  622. {
  623. return tipc_topsrv_start(net);
  624. }
  625. void __net_exit tipc_topsrv_exit_net(struct net *net)
  626. {
  627. tipc_topsrv_stop(net);
  628. }