blob: 72c339e432aa6693a30957060b022bb2bc8f8785 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Allan Stephens5b06c85c2008-05-19 13:30:13 -07002 * net/tipc/subscr.c: TIPC network topology service
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Ying Xue13a2e892013-06-17 10:54:40 -04005 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "name_table.h"
Allan Stephens5b06c85c2008-05-19 13:30:13 -070039#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040
41/**
Paul Gortmaker11f99902011-12-29 20:49:39 -050042 * struct tipc_subscriber - TIPC network topology subscriber
Ying Xue13a2e892013-06-17 10:54:40 -040043 * @conid: connection identifier to server connecting to subscriber
stephen hemminger963a18552014-01-12 12:48:00 -080044 * @lock: control access to subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010045 * @subscription_list: list of subscription objects for this subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010046 */
Paul Gortmaker11f99902011-12-29 20:49:39 -050047struct tipc_subscriber {
Ying Xue13a2e892013-06-17 10:54:40 -040048 int conid;
49 spinlock_t lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010050 struct list_head subscription_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051};
52
Per Lidenb97bf3f2006-01-02 19:04:38 +010053/**
Neil Hormandb5a7532010-10-21 01:06:16 +000054 * htohl - convert value to endianness used by destination
55 * @in: value to convert
56 * @swap: non-zero if endianness must be reversed
57 *
58 * Returns converted value
59 */
Neil Hormandb5a7532010-10-21 01:06:16 +000060static u32 htohl(u32 in, int swap)
61{
62 return swap ? swab32(in) : in;
63}
64
Ying Xue13a2e892013-06-17 10:54:40 -040065static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
66 u32 found_upper, u32 event, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +010067 u32 node)
68{
Ying Xuea62fbcc2015-01-09 15:27:11 +080069 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
Ying Xue13a2e892013-06-17 10:54:40 -040070 struct tipc_subscriber *subscriber = sub->subscriber;
71 struct kvec msg_sect;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
73 msg_sect.iov_base = (void *)&sub->evt;
74 msg_sect.iov_len = sizeof(struct tipc_event);
Neil Hormandb5a7532010-10-21 01:06:16 +000075 sub->evt.event = htohl(event, sub->swap);
76 sub->evt.found_lower = htohl(found_lower, sub->swap);
77 sub->evt.found_upper = htohl(found_upper, sub->swap);
78 sub->evt.port.ref = htohl(port_ref, sub->swap);
79 sub->evt.port.node = htohl(node, sub->swap);
Ying Xuea62fbcc2015-01-09 15:27:11 +080080 tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
81 msg_sect.iov_base, msg_sect.iov_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +010082}
83
84/**
Per Liden4323add2006-01-18 00:38:21 +010085 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 *
87 * Returns 1 if there is overlap, otherwise 0.
88 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -040089int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +010090 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +010091{
92 if (found_lower < sub->seq.lower)
93 found_lower = sub->seq.lower;
94 if (found_upper > sub->seq.upper)
95 found_upper = sub->seq.upper;
96 if (found_lower > found_upper)
97 return 0;
98 return 1;
99}
100
101/**
Per Liden4323add2006-01-18 00:38:21 +0100102 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900103 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104 * Protected by nameseq.lock in name_table.c
105 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400106void tipc_subscr_report_overlap(struct tipc_subscription *sub, u32 found_lower,
107 u32 found_upper, u32 event, u32 port_ref,
108 u32 node, int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109{
Per Liden4323add2006-01-18 00:38:21 +0100110 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700112 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700114
Ying Xue7e244772011-07-19 04:21:56 -0400115 subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116}
117
Ying Xue2f55c432015-01-09 15:27:00 +0800118static void subscr_timeout(unsigned long data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119{
Ying Xue2f55c432015-01-09 15:27:00 +0800120 struct tipc_subscription *sub = (struct tipc_subscription *)data;
Ying Xue13a2e892013-06-17 10:54:40 -0400121 struct tipc_subscriber *subscriber = sub->subscriber;
Ying Xuea62fbcc2015-01-09 15:27:11 +0800122 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
Ying Xue13a2e892013-06-17 10:54:40 -0400124 /* The spin lock per subscriber is used to protect its members */
125 spin_lock_bh(&subscriber->lock);
126
Lijun Cheneb409462006-10-16 21:59:42 -0700127 /* Validate timeout (in case subscription is being cancelled) */
Lijun Cheneb409462006-10-16 21:59:42 -0700128 if (sub->timeout == TIPC_WAIT_FOREVER) {
Ying Xue13a2e892013-06-17 10:54:40 -0400129 spin_unlock_bh(&subscriber->lock);
Lijun Cheneb409462006-10-16 21:59:42 -0700130 return;
131 }
132
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 /* Unlink subscription from name table */
Per Liden4323add2006-01-18 00:38:21 +0100134 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135
Allan Stephens28353e72008-05-19 13:29:47 -0700136 /* Unlink subscription from subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 list_del(&sub->subscription_list);
138
Ying Xue13a2e892013-06-17 10:54:40 -0400139 spin_unlock_bh(&subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700140
141 /* Notify subscriber of timeout */
Allan Stephens28353e72008-05-19 13:29:47 -0700142 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
143 TIPC_SUBSCR_TIMEOUT, 0, 0);
144
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 /* Now destroy subscription */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 kfree(sub);
Ying Xuea62fbcc2015-01-09 15:27:11 +0800147 atomic_dec(&tn->subscription_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148}
149
150/**
Lijun Cheneb409462006-10-16 21:59:42 -0700151 * subscr_del - delete a subscription within a subscription list
152 *
Ying Xue13a2e892013-06-17 10:54:40 -0400153 * Called with subscriber lock held.
Lijun Cheneb409462006-10-16 21:59:42 -0700154 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500155static void subscr_del(struct tipc_subscription *sub)
Lijun Cheneb409462006-10-16 21:59:42 -0700156{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800157 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
158
Lijun Cheneb409462006-10-16 21:59:42 -0700159 tipc_nametbl_unsubscribe(sub);
160 list_del(&sub->subscription_list);
161 kfree(sub);
Ying Xuea62fbcc2015-01-09 15:27:11 +0800162 atomic_dec(&tn->subscription_count);
Lijun Cheneb409462006-10-16 21:59:42 -0700163}
164
165/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900167 *
Ying Xue13a2e892013-06-17 10:54:40 -0400168 * Note: Must call it in process context since it might sleep.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800170static void subscr_terminate(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800172 struct tipc_subscriber *subscriber = sub->subscriber;
173 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
174
175 tipc_conn_terminate(tn->topsrv, subscriber->conid);
Ying Xue13a2e892013-06-17 10:54:40 -0400176}
177
178static void subscr_release(struct tipc_subscriber *subscriber)
179{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500180 struct tipc_subscription *sub;
181 struct tipc_subscription *sub_temp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182
Ying Xue13a2e892013-06-17 10:54:40 -0400183 spin_lock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184
185 /* Destroy any existing subscriptions for subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
187 subscription_list) {
188 if (sub->timeout != TIPC_WAIT_FOREVER) {
Ying Xue13a2e892013-06-17 10:54:40 -0400189 spin_unlock_bh(&subscriber->lock);
Ying Xue2f55c432015-01-09 15:27:00 +0800190 del_timer_sync(&sub->timer);
Ying Xue13a2e892013-06-17 10:54:40 -0400191 spin_lock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 }
Lijun Cheneb409462006-10-16 21:59:42 -0700193 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 }
Ying Xue13a2e892013-06-17 10:54:40 -0400195 spin_unlock_bh(&subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700196
197 /* Now destroy subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 kfree(subscriber);
199}
200
201/**
Lijun Cheneb409462006-10-16 21:59:42 -0700202 * subscr_cancel - handle subscription cancellation request
203 *
Ying Xue13a2e892013-06-17 10:54:40 -0400204 * Called with subscriber lock held. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700205 * to enable the subscription timeout routine to finish without deadlocking;
206 * the lock is then reclaimed to allow caller to release it upon return.
207 *
208 * Note that fields of 's' use subscriber's endianness!
209 */
Lijun Cheneb409462006-10-16 21:59:42 -0700210static void subscr_cancel(struct tipc_subscr *s,
Paul Gortmaker11f99902011-12-29 20:49:39 -0500211 struct tipc_subscriber *subscriber)
Lijun Cheneb409462006-10-16 21:59:42 -0700212{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500213 struct tipc_subscription *sub;
214 struct tipc_subscription *sub_temp;
Lijun Cheneb409462006-10-16 21:59:42 -0700215 int found = 0;
216
217 /* Find first matching subscription, exit if not found */
Lijun Cheneb409462006-10-16 21:59:42 -0700218 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
219 subscription_list) {
Neil Hormandb5a7532010-10-21 01:06:16 +0000220 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
221 found = 1;
222 break;
223 }
Lijun Cheneb409462006-10-16 21:59:42 -0700224 }
225 if (!found)
226 return;
227
228 /* Cancel subscription timer (if used), then delete subscription */
Lijun Cheneb409462006-10-16 21:59:42 -0700229 if (sub->timeout != TIPC_WAIT_FOREVER) {
230 sub->timeout = TIPC_WAIT_FOREVER;
Ying Xue13a2e892013-06-17 10:54:40 -0400231 spin_unlock_bh(&subscriber->lock);
Ying Xue2f55c432015-01-09 15:27:00 +0800232 del_timer_sync(&sub->timer);
Ying Xue13a2e892013-06-17 10:54:40 -0400233 spin_lock_bh(&subscriber->lock);
Lijun Cheneb409462006-10-16 21:59:42 -0700234 }
Lijun Cheneb409462006-10-16 21:59:42 -0700235 subscr_del(sub);
236}
237
238/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900240 *
Ying Xue13a2e892013-06-17 10:54:40 -0400241 * Called with subscriber lock held.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800243static int subscr_subscribe(struct net *net, struct tipc_subscr *s,
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100244 struct tipc_subscriber *subscriber,
Ying Xuea62fbcc2015-01-09 15:27:11 +0800245 struct tipc_subscription **sub_p)
246{
247 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakerfead3902011-12-29 20:43:44 -0500248 struct tipc_subscription *sub;
Neil Hormandb5a7532010-10-21 01:06:16 +0000249 int swap;
250
251 /* Determine subscriber's endianness */
Neil Hormandb5a7532010-10-21 01:06:16 +0000252 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700253
254 /* Detect & process a subscription cancellation request */
Neil Hormandb5a7532010-10-21 01:06:16 +0000255 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
256 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700257 subscr_cancel(s, subscriber);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100258 return 0;
Lijun Cheneb409462006-10-16 21:59:42 -0700259 }
260
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 /* Refuse subscription if global limit exceeded */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800262 if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400263 pr_warn("Subscription rejected, limit reached (%u)\n",
Ying Xue34f256c2012-08-16 12:09:13 +0000264 TIPC_MAX_SUBSCRIPTIONS);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100265 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 }
267
268 /* Allocate subscription object */
Allan Stephens28353e72008-05-19 13:29:47 -0700269 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700270 if (!sub) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400271 pr_warn("Subscription rejected, no memory\n");
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100272 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 }
274
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 /* Initialize subscription object */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800276 sub->net = net;
Neil Hormandb5a7532010-10-21 01:06:16 +0000277 sub->seq.type = htohl(s->seq.type, swap);
278 sub->seq.lower = htohl(s->seq.lower, swap);
279 sub->seq.upper = htohl(s->seq.upper, swap);
Ying Xue2f55c432015-01-09 15:27:00 +0800280 sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
Neil Hormandb5a7532010-10-21 01:06:16 +0000281 sub->filter = htohl(s->filter, swap);
Neil Horman8c974432010-10-21 01:06:15 +0000282 if ((!(sub->filter & TIPC_SUB_PORTS) ==
283 !(sub->filter & TIPC_SUB_SERVICE)) ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800284 (sub->seq.lower > sub->seq.upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400285 pr_warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 kfree(sub);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100287 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 list_add(&sub->subscription_list, &subscriber->subscription_list);
Ying Xue13a2e892013-06-17 10:54:40 -0400290 sub->subscriber = subscriber;
Neil Hormandb5a7532010-10-21 01:06:16 +0000291 sub->swap = swap;
Allan Stephens28353e72008-05-19 13:29:47 -0700292 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Ying Xuea62fbcc2015-01-09 15:27:11 +0800293 atomic_inc(&tn->subscription_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 if (sub->timeout != TIPC_WAIT_FOREVER) {
Ying Xue2f55c432015-01-09 15:27:00 +0800295 setup_timer(&sub->timer, subscr_timeout, (unsigned long)sub);
296 mod_timer(&sub->timer, jiffies + sub->timeout);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 }
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100298 *sub_p = sub;
299 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300}
301
Ying Xue13a2e892013-06-17 10:54:40 -0400302/* Handle one termination request for the subscriber */
303static void subscr_conn_shutdown_event(int conid, void *usr_data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304{
Ying Xue13a2e892013-06-17 10:54:40 -0400305 subscr_release((struct tipc_subscriber *)usr_data);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306}
307
Ying Xue13a2e892013-06-17 10:54:40 -0400308/* Handle one request to create a new subscription for the subscriber */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800309static void subscr_conn_msg_event(struct net *net, int conid,
310 struct sockaddr_tipc *addr, void *usr_data,
311 void *buf, size_t len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312{
Ying Xue13a2e892013-06-17 10:54:40 -0400313 struct tipc_subscriber *subscriber = usr_data;
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100314 struct tipc_subscription *sub = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315
Ying Xue13a2e892013-06-17 10:54:40 -0400316 spin_lock_bh(&subscriber->lock);
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800317 if (subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber,
318 &sub) < 0) {
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100319 spin_unlock_bh(&subscriber->lock);
Ying Xuea62fbcc2015-01-09 15:27:11 +0800320 subscr_terminate(sub);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100321 return;
322 }
Ying Xue13a2e892013-06-17 10:54:40 -0400323 if (sub)
324 tipc_nametbl_subscribe(sub);
325 spin_unlock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326}
327
Ying Xue13a2e892013-06-17 10:54:40 -0400328/* Handle one request to establish a new subscriber */
329static void *subscr_named_msg_event(int conid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500331 struct tipc_subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
333 /* Create subscriber object */
Paul Gortmaker11f99902011-12-29 20:49:39 -0500334 subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (subscriber == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400336 pr_warn("Subscriber rejected, no memory\n");
Ying Xue13a2e892013-06-17 10:54:40 -0400337 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 INIT_LIST_HEAD(&subscriber->subscription_list);
Ying Xue13a2e892013-06-17 10:54:40 -0400340 subscriber->conid = conid;
341 spin_lock_init(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342
Ying Xue13a2e892013-06-17 10:54:40 -0400343 return (void *)subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344}
345
Ying Xuea62fbcc2015-01-09 15:27:11 +0800346int tipc_subscr_start(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800348 struct tipc_net *tn = net_generic(net, tipc_net_id);
349 const char name[] = "topology_server";
350 struct tipc_server *topsrv;
351 struct sockaddr_tipc *saddr;
352
353 saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
354 if (!saddr)
355 return -ENOMEM;
356 saddr->family = AF_TIPC;
357 saddr->addrtype = TIPC_ADDR_NAMESEQ;
358 saddr->addr.nameseq.type = TIPC_TOP_SRV;
359 saddr->addr.nameseq.lower = TIPC_TOP_SRV;
360 saddr->addr.nameseq.upper = TIPC_TOP_SRV;
361 saddr->scope = TIPC_NODE_SCOPE;
362
363 topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
364 if (!topsrv) {
365 kfree(saddr);
366 return -ENOMEM;
367 }
368 topsrv->net = net;
369 topsrv->saddr = saddr;
370 topsrv->imp = TIPC_CRITICAL_IMPORTANCE;
371 topsrv->type = SOCK_SEQPACKET;
372 topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
373 topsrv->tipc_conn_recvmsg = subscr_conn_msg_event;
374 topsrv->tipc_conn_new = subscr_named_msg_event;
375 topsrv->tipc_conn_shutdown = subscr_conn_shutdown_event;
376
377 strncpy(topsrv->name, name, strlen(name) + 1);
378 tn->topsrv = topsrv;
379 atomic_set(&tn->subscription_count, 0);
380
381 return tipc_server_start(topsrv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382}
383
Ying Xuea62fbcc2015-01-09 15:27:11 +0800384void tipc_subscr_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800386 struct tipc_net *tn = net_generic(net, tipc_net_id);
387 struct tipc_server *topsrv = tn->topsrv;
388
389 tipc_server_stop(topsrv);
390 kfree(topsrv->saddr);
391 kfree(topsrv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392}