blob: 1c147c869c2e68312ee02b5275589ba0e18f7727 [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
Ying Xue13a2e892013-06-17 10:54:40 -0400165static void subscr_release(struct tipc_subscriber *subscriber)
166{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500167 struct tipc_subscription *sub;
168 struct tipc_subscription *sub_temp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169
Ying Xue13a2e892013-06-17 10:54:40 -0400170 spin_lock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171
172 /* Destroy any existing subscriptions for subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
174 subscription_list) {
175 if (sub->timeout != TIPC_WAIT_FOREVER) {
Ying Xue13a2e892013-06-17 10:54:40 -0400176 spin_unlock_bh(&subscriber->lock);
Ying Xue2f55c432015-01-09 15:27:00 +0800177 del_timer_sync(&sub->timer);
Ying Xue13a2e892013-06-17 10:54:40 -0400178 spin_lock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 }
Lijun Cheneb409462006-10-16 21:59:42 -0700180 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 }
Ying Xue13a2e892013-06-17 10:54:40 -0400182 spin_unlock_bh(&subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700183
184 /* Now destroy subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 kfree(subscriber);
186}
187
188/**
Lijun Cheneb409462006-10-16 21:59:42 -0700189 * subscr_cancel - handle subscription cancellation request
190 *
Ying Xue13a2e892013-06-17 10:54:40 -0400191 * Called with subscriber lock held. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700192 * to enable the subscription timeout routine to finish without deadlocking;
193 * the lock is then reclaimed to allow caller to release it upon return.
194 *
195 * Note that fields of 's' use subscriber's endianness!
196 */
Lijun Cheneb409462006-10-16 21:59:42 -0700197static void subscr_cancel(struct tipc_subscr *s,
Paul Gortmaker11f99902011-12-29 20:49:39 -0500198 struct tipc_subscriber *subscriber)
Lijun Cheneb409462006-10-16 21:59:42 -0700199{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500200 struct tipc_subscription *sub;
201 struct tipc_subscription *sub_temp;
Lijun Cheneb409462006-10-16 21:59:42 -0700202 int found = 0;
203
204 /* Find first matching subscription, exit if not found */
Lijun Cheneb409462006-10-16 21:59:42 -0700205 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
206 subscription_list) {
Neil Hormandb5a7532010-10-21 01:06:16 +0000207 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
208 found = 1;
209 break;
210 }
Lijun Cheneb409462006-10-16 21:59:42 -0700211 }
212 if (!found)
213 return;
214
215 /* Cancel subscription timer (if used), then delete subscription */
Lijun Cheneb409462006-10-16 21:59:42 -0700216 if (sub->timeout != TIPC_WAIT_FOREVER) {
217 sub->timeout = TIPC_WAIT_FOREVER;
Ying Xue13a2e892013-06-17 10:54:40 -0400218 spin_unlock_bh(&subscriber->lock);
Ying Xue2f55c432015-01-09 15:27:00 +0800219 del_timer_sync(&sub->timer);
Ying Xue13a2e892013-06-17 10:54:40 -0400220 spin_lock_bh(&subscriber->lock);
Lijun Cheneb409462006-10-16 21:59:42 -0700221 }
Lijun Cheneb409462006-10-16 21:59:42 -0700222 subscr_del(sub);
223}
224
225/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900227 *
Ying Xue13a2e892013-06-17 10:54:40 -0400228 * Called with subscriber lock held.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800230static int subscr_subscribe(struct net *net, struct tipc_subscr *s,
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100231 struct tipc_subscriber *subscriber,
Ying Xuea62fbcc2015-01-09 15:27:11 +0800232 struct tipc_subscription **sub_p)
233{
234 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakerfead3902011-12-29 20:43:44 -0500235 struct tipc_subscription *sub;
Neil Hormandb5a7532010-10-21 01:06:16 +0000236 int swap;
237
238 /* Determine subscriber's endianness */
Neil Hormandb5a7532010-10-21 01:06:16 +0000239 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700240
241 /* Detect & process a subscription cancellation request */
Neil Hormandb5a7532010-10-21 01:06:16 +0000242 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
243 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700244 subscr_cancel(s, subscriber);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100245 return 0;
Lijun Cheneb409462006-10-16 21:59:42 -0700246 }
247
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 /* Refuse subscription if global limit exceeded */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800249 if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400250 pr_warn("Subscription rejected, limit reached (%u)\n",
Ying Xue34f256c2012-08-16 12:09:13 +0000251 TIPC_MAX_SUBSCRIPTIONS);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100252 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 }
254
255 /* Allocate subscription object */
Allan Stephens28353e72008-05-19 13:29:47 -0700256 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700257 if (!sub) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400258 pr_warn("Subscription rejected, no memory\n");
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100259 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 }
261
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 /* Initialize subscription object */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800263 sub->net = net;
Neil Hormandb5a7532010-10-21 01:06:16 +0000264 sub->seq.type = htohl(s->seq.type, swap);
265 sub->seq.lower = htohl(s->seq.lower, swap);
266 sub->seq.upper = htohl(s->seq.upper, swap);
Ying Xue2f55c432015-01-09 15:27:00 +0800267 sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
Neil Hormandb5a7532010-10-21 01:06:16 +0000268 sub->filter = htohl(s->filter, swap);
Neil Horman8c974432010-10-21 01:06:15 +0000269 if ((!(sub->filter & TIPC_SUB_PORTS) ==
270 !(sub->filter & TIPC_SUB_SERVICE)) ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800271 (sub->seq.lower > sub->seq.upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400272 pr_warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 kfree(sub);
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100274 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 list_add(&sub->subscription_list, &subscriber->subscription_list);
Ying Xue13a2e892013-06-17 10:54:40 -0400277 sub->subscriber = subscriber;
Neil Hormandb5a7532010-10-21 01:06:16 +0000278 sub->swap = swap;
Allan Stephens28353e72008-05-19 13:29:47 -0700279 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Ying Xuea62fbcc2015-01-09 15:27:11 +0800280 atomic_inc(&tn->subscription_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 if (sub->timeout != TIPC_WAIT_FOREVER) {
Ying Xue2f55c432015-01-09 15:27:00 +0800282 setup_timer(&sub->timer, subscr_timeout, (unsigned long)sub);
283 mod_timer(&sub->timer, jiffies + sub->timeout);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 }
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100285 *sub_p = sub;
286 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287}
288
Ying Xue13a2e892013-06-17 10:54:40 -0400289/* Handle one termination request for the subscriber */
290static void subscr_conn_shutdown_event(int conid, void *usr_data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291{
Ying Xue13a2e892013-06-17 10:54:40 -0400292 subscr_release((struct tipc_subscriber *)usr_data);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
Ying Xue13a2e892013-06-17 10:54:40 -0400295/* Handle one request to create a new subscription for the subscriber */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800296static void subscr_conn_msg_event(struct net *net, int conid,
297 struct sockaddr_tipc *addr, void *usr_data,
298 void *buf, size_t len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299{
Ying Xue13a2e892013-06-17 10:54:40 -0400300 struct tipc_subscriber *subscriber = usr_data;
Erik Hugnea5d0e7c2014-03-24 16:56:38 +0100301 struct tipc_subscription *sub = NULL;
Erik Hugne7fe80972015-02-27 08:56:55 +0100302 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303
Ying Xue13a2e892013-06-17 10:54:40 -0400304 spin_lock_bh(&subscriber->lock);
Erik Hugne7fe80972015-02-27 08:56:55 +0100305 subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber, &sub);
Ying Xue13a2e892013-06-17 10:54:40 -0400306 if (sub)
307 tipc_nametbl_subscribe(sub);
Erik Hugne7fe80972015-02-27 08:56:55 +0100308 else
309 tipc_conn_terminate(tn->topsrv, subscriber->conid);
Ying Xue13a2e892013-06-17 10:54:40 -0400310 spin_unlock_bh(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311}
312
Ying Xue13a2e892013-06-17 10:54:40 -0400313/* Handle one request to establish a new subscriber */
314static void *subscr_named_msg_event(int conid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500316 struct tipc_subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317
318 /* Create subscriber object */
Paul Gortmaker11f99902011-12-29 20:49:39 -0500319 subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 if (subscriber == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400321 pr_warn("Subscriber rejected, no memory\n");
Ying Xue13a2e892013-06-17 10:54:40 -0400322 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 INIT_LIST_HEAD(&subscriber->subscription_list);
Ying Xue13a2e892013-06-17 10:54:40 -0400325 subscriber->conid = conid;
326 spin_lock_init(&subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327
Ying Xue13a2e892013-06-17 10:54:40 -0400328 return (void *)subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329}
330
Ying Xuea62fbcc2015-01-09 15:27:11 +0800331int tipc_subscr_start(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800333 struct tipc_net *tn = net_generic(net, tipc_net_id);
334 const char name[] = "topology_server";
335 struct tipc_server *topsrv;
336 struct sockaddr_tipc *saddr;
337
338 saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
339 if (!saddr)
340 return -ENOMEM;
341 saddr->family = AF_TIPC;
342 saddr->addrtype = TIPC_ADDR_NAMESEQ;
343 saddr->addr.nameseq.type = TIPC_TOP_SRV;
344 saddr->addr.nameseq.lower = TIPC_TOP_SRV;
345 saddr->addr.nameseq.upper = TIPC_TOP_SRV;
346 saddr->scope = TIPC_NODE_SCOPE;
347
348 topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
349 if (!topsrv) {
350 kfree(saddr);
351 return -ENOMEM;
352 }
353 topsrv->net = net;
354 topsrv->saddr = saddr;
355 topsrv->imp = TIPC_CRITICAL_IMPORTANCE;
356 topsrv->type = SOCK_SEQPACKET;
357 topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
358 topsrv->tipc_conn_recvmsg = subscr_conn_msg_event;
359 topsrv->tipc_conn_new = subscr_named_msg_event;
360 topsrv->tipc_conn_shutdown = subscr_conn_shutdown_event;
361
362 strncpy(topsrv->name, name, strlen(name) + 1);
363 tn->topsrv = topsrv;
364 atomic_set(&tn->subscription_count, 0);
365
366 return tipc_server_start(topsrv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367}
368
Ying Xuea62fbcc2015-01-09 15:27:11 +0800369void tipc_subscr_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370{
Ying Xuea62fbcc2015-01-09 15:27:11 +0800371 struct tipc_net *tn = net_generic(net, tipc_net_id);
372 struct tipc_server *topsrv = tn->topsrv;
373
374 tipc_server_stop(topsrv);
375 kfree(topsrv->saddr);
376 kfree(topsrv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377}