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