1b97bf3fdSPer Liden /*
25b06c85cSAllan Stephens * net/tipc/subscr.c: TIPC network topology service
3b97bf3fdSPer Liden *
4df79d040SJon Maloy * Copyright (c) 2000-2017, Ericsson AB
513a2e898SYing Xue * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
609f78b85SJon Maloy * Copyright (c) 2020-2021, Red Hat Inc
7b97bf3fdSPer Liden * All rights reserved.
8b97bf3fdSPer Liden *
9b97bf3fdSPer Liden * Redistribution and use in source and binary forms, with or without
10b97bf3fdSPer Liden * modification, are permitted provided that the following conditions are met:
11b97bf3fdSPer Liden *
129ea1fd3cSPer Liden * 1. Redistributions of source code must retain the above copyright
139ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer.
149ea1fd3cSPer Liden * 2. Redistributions in binary form must reproduce the above copyright
159ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer in the
169ea1fd3cSPer Liden * documentation and/or other materials provided with the distribution.
179ea1fd3cSPer Liden * 3. Neither the names of the copyright holders nor the names of its
189ea1fd3cSPer Liden * contributors may be used to endorse or promote products derived from
199ea1fd3cSPer Liden * this software without specific prior written permission.
209ea1fd3cSPer Liden *
219ea1fd3cSPer Liden * Alternatively, this software may be distributed under the terms of the
229ea1fd3cSPer Liden * GNU General Public License ("GPL") version 2 as published by the Free
239ea1fd3cSPer Liden * Software Foundation.
24b97bf3fdSPer Liden *
25b97bf3fdSPer Liden * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26b97bf3fdSPer Liden * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27b97bf3fdSPer Liden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28b97bf3fdSPer Liden * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29b97bf3fdSPer Liden * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30b97bf3fdSPer Liden * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31b97bf3fdSPer Liden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32b97bf3fdSPer Liden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33b97bf3fdSPer Liden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34b97bf3fdSPer Liden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35b97bf3fdSPer Liden * POSSIBILITY OF SUCH DAMAGE.
36b97bf3fdSPer Liden */
37b97bf3fdSPer Liden
38b97bf3fdSPer Liden #include "core.h"
39b97bf3fdSPer Liden #include "name_table.h"
405b06c85cSAllan Stephens #include "subscr.h"
41b97bf3fdSPer Liden
tipc_sub_send_event(struct tipc_subscription * sub,struct publication * p,u32 event)42da0a75e8SJon Maloy static void tipc_sub_send_event(struct tipc_subscription *sub,
4309f78b85SJon Maloy struct publication *p,
4409f78b85SJon Maloy u32 event)
45b97bf3fdSPer Liden {
4609f78b85SJon Maloy struct tipc_subscr *s = &sub->evt.s;
47414574a0SJon Maloy struct tipc_event *evt = &sub->evt;
48b97bf3fdSPer Liden
49df79d040SJon Maloy if (sub->inactive)
50df79d040SJon Maloy return;
518985ecc7SJon Maloy tipc_evt_write(evt, event, event);
5209f78b85SJon Maloy if (p) {
5309f78b85SJon Maloy tipc_evt_write(evt, found_lower, p->sr.lower);
5409f78b85SJon Maloy tipc_evt_write(evt, found_upper, p->sr.upper);
5509f78b85SJon Maloy tipc_evt_write(evt, port.ref, p->sk.ref);
5609f78b85SJon Maloy tipc_evt_write(evt, port.node, p->sk.node);
5709f78b85SJon Maloy } else {
5809f78b85SJon Maloy tipc_evt_write(evt, found_lower, s->seq.lower);
5909f78b85SJon Maloy tipc_evt_write(evt, found_upper, s->seq.upper);
6009f78b85SJon Maloy tipc_evt_write(evt, port.ref, 0);
6109f78b85SJon Maloy tipc_evt_write(evt, port.node, 0);
6209f78b85SJon Maloy }
63026321c6SJon Maloy tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
64b97bf3fdSPer Liden }
65b97bf3fdSPer Liden
66b97bf3fdSPer Liden /**
675fcb7d47SRandy Dunlap * tipc_sub_check_overlap - test for subscription overlap with the given values
68429189acSJon Maloy * @subscribed: the service range subscribed for
69*f1dcdc07Sgushengxian * @found: the service range we are checking for match
70b97bf3fdSPer Liden *
7109f78b85SJon Maloy * Returns true if there is overlap, otherwise false.
72b97bf3fdSPer Liden */
tipc_sub_check_overlap(struct tipc_service_range * subscribed,struct tipc_service_range * found)73429189acSJon Maloy static bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
74429189acSJon Maloy struct tipc_service_range *found)
75b97bf3fdSPer Liden {
76429189acSJon Maloy u32 found_lower = found->lower;
77429189acSJon Maloy u32 found_upper = found->upper;
78429189acSJon Maloy
79429189acSJon Maloy if (found_lower < subscribed->lower)
80429189acSJon Maloy found_lower = subscribed->lower;
81429189acSJon Maloy if (found_upper > subscribed->upper)
82429189acSJon Maloy found_upper = subscribed->upper;
83429189acSJon Maloy return found_lower <= found_upper;
84b97bf3fdSPer Liden }
85b97bf3fdSPer Liden
tipc_sub_report_overlap(struct tipc_subscription * sub,struct publication * p,u32 event,bool must)86da0a75e8SJon Maloy void tipc_sub_report_overlap(struct tipc_subscription *sub,
8709f78b85SJon Maloy struct publication *p,
8809f78b85SJon Maloy u32 event, bool must)
89a4273c73SParthasarathy Bhuvaragan {
90429189acSJon Maloy struct tipc_service_range *sr = &sub->s.seq;
91429189acSJon Maloy u32 filter = sub->s.filter;
92a4273c73SParthasarathy Bhuvaragan
93429189acSJon Maloy if (!tipc_sub_check_overlap(sr, &p->sr))
94b97bf3fdSPer Liden return;
95232d07b7SJon Maloy if (!must && !(filter & TIPC_SUB_PORTS))
96232d07b7SJon Maloy return;
9709f78b85SJon Maloy if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
98232d07b7SJon Maloy return;
9909f78b85SJon Maloy if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
100b97bf3fdSPer Liden return;
101df79d040SJon Maloy spin_lock(&sub->lock);
10209f78b85SJon Maloy tipc_sub_send_event(sub, p, event);
103df79d040SJon Maloy spin_unlock(&sub->lock);
104b97bf3fdSPer Liden }
105b97bf3fdSPer Liden
tipc_sub_timeout(struct timer_list * t)106da0a75e8SJon Maloy static void tipc_sub_timeout(struct timer_list *t)
107b97bf3fdSPer Liden {
10831b102bbSKees Cook struct tipc_subscription *sub = from_timer(sub, t, timer);
109557d054cSYing Xue
110df79d040SJon Maloy spin_lock(&sub->lock);
11109f78b85SJon Maloy tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
112df79d040SJon Maloy sub->inactive = true;
113df79d040SJon Maloy spin_unlock(&sub->lock);
11400bc00a9SYing Xue }
11500bc00a9SYing Xue
tipc_sub_kref_release(struct kref * kref)116da0a75e8SJon Maloy static void tipc_sub_kref_release(struct kref *kref)
117d094c4d5SParthasarathy Bhuvaragan {
1185c45ab24SJon Maloy kfree(container_of(kref, struct tipc_subscription, kref));
119d094c4d5SParthasarathy Bhuvaragan }
120d094c4d5SParthasarathy Bhuvaragan
tipc_sub_put(struct tipc_subscription * subscription)121da0a75e8SJon Maloy void tipc_sub_put(struct tipc_subscription *subscription)
122d094c4d5SParthasarathy Bhuvaragan {
123da0a75e8SJon Maloy kref_put(&subscription->kref, tipc_sub_kref_release);
124d094c4d5SParthasarathy Bhuvaragan }
125d094c4d5SParthasarathy Bhuvaragan
tipc_sub_get(struct tipc_subscription * subscription)126da0a75e8SJon Maloy void tipc_sub_get(struct tipc_subscription *subscription)
127d094c4d5SParthasarathy Bhuvaragan {
128d094c4d5SParthasarathy Bhuvaragan kref_get(&subscription->kref);
129d094c4d5SParthasarathy Bhuvaragan }
130d094c4d5SParthasarathy Bhuvaragan
tipc_sub_subscribe(struct net * net,struct tipc_subscr * s,int conid)1315c45ab24SJon Maloy struct tipc_subscription *tipc_sub_subscribe(struct net *net,
1327c13c622SParthasarathy Bhuvaragan struct tipc_subscr *s,
1338985ecc7SJon Maloy int conid)
134a62fbcceSYing Xue {
135429189acSJon Maloy u32 lower = tipc_sub_read(s, seq.lower);
136429189acSJon Maloy u32 upper = tipc_sub_read(s, seq.upper);
1378985ecc7SJon Maloy u32 filter = tipc_sub_read(s, filter);
138da0a75e8SJon Maloy struct tipc_subscription *sub;
139242e82ccSJon Maloy u32 timeout;
140eb409460SLijun Chen
141242e82ccSJon Maloy if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
142429189acSJon Maloy lower > upper) {
143242e82ccSJon Maloy pr_warn("Subscription rejected, illegal request\n");
144242e82ccSJon Maloy return NULL;
145242e82ccSJon Maloy }
14628353e7fSAllan Stephens sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
147a10bd924SAllan Stephens if (!sub) {
1482cf8aa19SErik Hugne pr_warn("Subscription rejected, no memory\n");
1497c13c622SParthasarathy Bhuvaragan return NULL;
150b97bf3fdSPer Liden }
151b714295aSJon Maloy INIT_LIST_HEAD(&sub->service_list);
152b714295aSJon Maloy INIT_LIST_HEAD(&sub->sub_list);
1535c45ab24SJon Maloy sub->net = net;
154df79d040SJon Maloy sub->conid = conid;
155df79d040SJon Maloy sub->inactive = false;
1567c13c622SParthasarathy Bhuvaragan memcpy(&sub->evt.s, s, sizeof(*s));
157429189acSJon Maloy sub->s.seq.type = tipc_sub_read(s, seq.type);
158429189acSJon Maloy sub->s.seq.lower = lower;
159429189acSJon Maloy sub->s.seq.upper = upper;
160429189acSJon Maloy sub->s.filter = filter;
161429189acSJon Maloy sub->s.timeout = tipc_sub_read(s, timeout);
162429189acSJon Maloy memcpy(sub->s.usr_handle, s->usr_handle, 8);
163df79d040SJon Maloy spin_lock_init(&sub->lock);
164d094c4d5SParthasarathy Bhuvaragan kref_init(&sub->kref);
165c3317f4dSJon Maloy if (!tipc_nametbl_subscribe(sub)) {
166c3317f4dSJon Maloy kfree(sub);
167c3317f4dSJon Maloy return NULL;
168c3317f4dSJon Maloy }
169da0a75e8SJon Maloy timer_setup(&sub->timer, tipc_sub_timeout, 0);
1708985ecc7SJon Maloy timeout = tipc_sub_read(&sub->evt.s, timeout);
171d094c4d5SParthasarathy Bhuvaragan if (timeout != TIPC_WAIT_FOREVER)
172f3ad288cSParthasarathy Bhuvaragan mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
173df79d040SJon Maloy return sub;
174b97bf3fdSPer Liden }
175b97bf3fdSPer Liden
tipc_sub_unsubscribe(struct tipc_subscription * sub)176242e82ccSJon Maloy void tipc_sub_unsubscribe(struct tipc_subscription *sub)
177b97bf3fdSPer Liden {
178df79d040SJon Maloy tipc_nametbl_unsubscribe(sub);
179df79d040SJon Maloy if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
180df79d040SJon Maloy del_timer_sync(&sub->timer);
181da0a75e8SJon Maloy list_del(&sub->sub_list);
182da0a75e8SJon Maloy tipc_sub_put(sub);
183b97bf3fdSPer Liden }
184