xref: /openbmc/linux/net/tipc/subscr.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1 b97bf3fdSPer Liden /*
2 5b06c85cSAllan Stephens  * net/tipc/subscr.c: TIPC network topology service
3 b97bf3fdSPer Liden  *
4 df79d040SJon Maloy  * Copyright (c) 2000-2017, Ericsson AB
5 13a2e898SYing Xue  * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
6 09f78b85SJon Maloy  * Copyright (c) 2020-2021, Red Hat Inc
7 b97bf3fdSPer Liden  * All rights reserved.
8 b97bf3fdSPer Liden  *
9 b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
10 b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
11 b97bf3fdSPer Liden  *
12 9ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
13 9ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
14 9ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
15 9ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
16 9ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
17 9ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
18 9ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
19 9ea1fd3cSPer Liden  *    this software without specific prior written permission.
20 9ea1fd3cSPer Liden  *
21 9ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
22 9ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
23 9ea1fd3cSPer Liden  * Software Foundation.
24 b97bf3fdSPer Liden  *
25 b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
36 b97bf3fdSPer Liden  */
37 b97bf3fdSPer Liden 
38 b97bf3fdSPer Liden #include "core.h"
39 b97bf3fdSPer Liden #include "name_table.h"
40 5b06c85cSAllan Stephens #include "subscr.h"
41 b97bf3fdSPer Liden 
tipc_sub_send_event(struct tipc_subscription * sub,struct publication * p,u32 event)42 da0a75e8SJon Maloy static void tipc_sub_send_event(struct tipc_subscription *sub,
43 09f78b85SJon Maloy 				struct publication *p,
44 09f78b85SJon Maloy 				u32 event)
45 b97bf3fdSPer Liden {
46 09f78b85SJon Maloy 	struct tipc_subscr *s = &sub->evt.s;
47 414574a0SJon Maloy 	struct tipc_event *evt = &sub->evt;
48 b97bf3fdSPer Liden 
49 df79d040SJon Maloy 	if (sub->inactive)
50 df79d040SJon Maloy 		return;
51 8985ecc7SJon Maloy 	tipc_evt_write(evt, event, event);
52 09f78b85SJon Maloy 	if (p) {
53 09f78b85SJon Maloy 		tipc_evt_write(evt, found_lower, p->sr.lower);
54 09f78b85SJon Maloy 		tipc_evt_write(evt, found_upper, p->sr.upper);
55 09f78b85SJon Maloy 		tipc_evt_write(evt, port.ref, p->sk.ref);
56 09f78b85SJon Maloy 		tipc_evt_write(evt, port.node, p->sk.node);
57 09f78b85SJon Maloy 	} else {
58 09f78b85SJon Maloy 		tipc_evt_write(evt, found_lower, s->seq.lower);
59 09f78b85SJon Maloy 		tipc_evt_write(evt, found_upper, s->seq.upper);
60 09f78b85SJon Maloy 		tipc_evt_write(evt, port.ref, 0);
61 09f78b85SJon Maloy 		tipc_evt_write(evt, port.node, 0);
62 09f78b85SJon Maloy 	}
63 026321c6SJon Maloy 	tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
64 b97bf3fdSPer Liden }
65 b97bf3fdSPer Liden 
66 b97bf3fdSPer Liden /**
67 5fcb7d47SRandy Dunlap  * tipc_sub_check_overlap - test for subscription overlap with the given values
68 429189acSJon Maloy  * @subscribed: the service range subscribed for
69 *f1dcdc07Sgushengxian  * @found: the service range we are checking for match
70 b97bf3fdSPer Liden  *
71 09f78b85SJon Maloy  * Returns true if there is overlap, otherwise false.
72 b97bf3fdSPer Liden  */
tipc_sub_check_overlap(struct tipc_service_range * subscribed,struct tipc_service_range * found)73 429189acSJon Maloy static bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
74 429189acSJon Maloy 				   struct tipc_service_range *found)
75 b97bf3fdSPer Liden {
76 429189acSJon Maloy 	u32 found_lower = found->lower;
77 429189acSJon Maloy 	u32 found_upper = found->upper;
78 429189acSJon Maloy 
79 429189acSJon Maloy 	if (found_lower < subscribed->lower)
80 429189acSJon Maloy 		found_lower = subscribed->lower;
81 429189acSJon Maloy 	if (found_upper > subscribed->upper)
82 429189acSJon Maloy 		found_upper = subscribed->upper;
83 429189acSJon Maloy 	return found_lower <= found_upper;
84 b97bf3fdSPer Liden }
85 b97bf3fdSPer Liden 
tipc_sub_report_overlap(struct tipc_subscription * sub,struct publication * p,u32 event,bool must)86 da0a75e8SJon Maloy void tipc_sub_report_overlap(struct tipc_subscription *sub,
87 09f78b85SJon Maloy 			     struct publication *p,
88 09f78b85SJon Maloy 			     u32 event, bool must)
89 a4273c73SParthasarathy Bhuvaragan {
90 429189acSJon Maloy 	struct tipc_service_range *sr = &sub->s.seq;
91 429189acSJon Maloy 	u32 filter = sub->s.filter;
92 a4273c73SParthasarathy Bhuvaragan 
93 429189acSJon Maloy 	if (!tipc_sub_check_overlap(sr, &p->sr))
94 b97bf3fdSPer Liden 		return;
95 232d07b7SJon Maloy 	if (!must && !(filter & TIPC_SUB_PORTS))
96 232d07b7SJon Maloy 		return;
97 09f78b85SJon Maloy 	if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
98 232d07b7SJon Maloy 		return;
99 09f78b85SJon Maloy 	if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
100 b97bf3fdSPer Liden 		return;
101 df79d040SJon Maloy 	spin_lock(&sub->lock);
102 09f78b85SJon Maloy 	tipc_sub_send_event(sub, p, event);
103 df79d040SJon Maloy 	spin_unlock(&sub->lock);
104 b97bf3fdSPer Liden }
105 b97bf3fdSPer Liden 
tipc_sub_timeout(struct timer_list * t)106 da0a75e8SJon Maloy static void tipc_sub_timeout(struct timer_list *t)
107 b97bf3fdSPer Liden {
108 31b102bbSKees Cook 	struct tipc_subscription *sub = from_timer(sub, t, timer);
109 557d054cSYing Xue 
110 df79d040SJon Maloy 	spin_lock(&sub->lock);
111 09f78b85SJon Maloy 	tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
112 df79d040SJon Maloy 	sub->inactive = true;
113 df79d040SJon Maloy 	spin_unlock(&sub->lock);
114 00bc00a9SYing Xue }
115 00bc00a9SYing Xue 
tipc_sub_kref_release(struct kref * kref)116 da0a75e8SJon Maloy static void tipc_sub_kref_release(struct kref *kref)
117 d094c4d5SParthasarathy Bhuvaragan {
118 5c45ab24SJon Maloy 	kfree(container_of(kref, struct tipc_subscription, kref));
119 d094c4d5SParthasarathy Bhuvaragan }
120 d094c4d5SParthasarathy Bhuvaragan 
tipc_sub_put(struct tipc_subscription * subscription)121 da0a75e8SJon Maloy void tipc_sub_put(struct tipc_subscription *subscription)
122 d094c4d5SParthasarathy Bhuvaragan {
123 da0a75e8SJon Maloy 	kref_put(&subscription->kref, tipc_sub_kref_release);
124 d094c4d5SParthasarathy Bhuvaragan }
125 d094c4d5SParthasarathy Bhuvaragan 
tipc_sub_get(struct tipc_subscription * subscription)126 da0a75e8SJon Maloy void tipc_sub_get(struct tipc_subscription *subscription)
127 d094c4d5SParthasarathy Bhuvaragan {
128 d094c4d5SParthasarathy Bhuvaragan 	kref_get(&subscription->kref);
129 d094c4d5SParthasarathy Bhuvaragan }
130 d094c4d5SParthasarathy Bhuvaragan 
tipc_sub_subscribe(struct net * net,struct tipc_subscr * s,int conid)131 5c45ab24SJon Maloy struct tipc_subscription *tipc_sub_subscribe(struct net *net,
132 7c13c622SParthasarathy Bhuvaragan 					     struct tipc_subscr *s,
133 8985ecc7SJon Maloy 					     int conid)
134 a62fbcceSYing Xue {
135 429189acSJon Maloy 	u32 lower = tipc_sub_read(s, seq.lower);
136 429189acSJon Maloy 	u32 upper = tipc_sub_read(s, seq.upper);
137 8985ecc7SJon Maloy 	u32 filter = tipc_sub_read(s, filter);
138 da0a75e8SJon Maloy 	struct tipc_subscription *sub;
139 242e82ccSJon Maloy 	u32 timeout;
140 eb409460SLijun Chen 
141 242e82ccSJon Maloy 	if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
142 429189acSJon Maloy 	    lower > upper) {
143 242e82ccSJon Maloy 		pr_warn("Subscription rejected, illegal request\n");
144 242e82ccSJon Maloy 		return NULL;
145 242e82ccSJon Maloy 	}
146 28353e7fSAllan Stephens 	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
147 a10bd924SAllan Stephens 	if (!sub) {
148 2cf8aa19SErik Hugne 		pr_warn("Subscription rejected, no memory\n");
149 7c13c622SParthasarathy Bhuvaragan 		return NULL;
150 b97bf3fdSPer Liden 	}
151 b714295aSJon Maloy 	INIT_LIST_HEAD(&sub->service_list);
152 b714295aSJon Maloy 	INIT_LIST_HEAD(&sub->sub_list);
153 5c45ab24SJon Maloy 	sub->net = net;
154 df79d040SJon Maloy 	sub->conid = conid;
155 df79d040SJon Maloy 	sub->inactive = false;
156 7c13c622SParthasarathy Bhuvaragan 	memcpy(&sub->evt.s, s, sizeof(*s));
157 429189acSJon Maloy 	sub->s.seq.type = tipc_sub_read(s, seq.type);
158 429189acSJon Maloy 	sub->s.seq.lower = lower;
159 429189acSJon Maloy 	sub->s.seq.upper = upper;
160 429189acSJon Maloy 	sub->s.filter = filter;
161 429189acSJon Maloy 	sub->s.timeout = tipc_sub_read(s, timeout);
162 429189acSJon Maloy 	memcpy(sub->s.usr_handle, s->usr_handle, 8);
163 df79d040SJon Maloy 	spin_lock_init(&sub->lock);
164 d094c4d5SParthasarathy Bhuvaragan 	kref_init(&sub->kref);
165 c3317f4dSJon Maloy 	if (!tipc_nametbl_subscribe(sub)) {
166 c3317f4dSJon Maloy 		kfree(sub);
167 c3317f4dSJon Maloy 		return NULL;
168 c3317f4dSJon Maloy 	}
169 da0a75e8SJon Maloy 	timer_setup(&sub->timer, tipc_sub_timeout, 0);
170 8985ecc7SJon Maloy 	timeout = tipc_sub_read(&sub->evt.s, timeout);
171 d094c4d5SParthasarathy Bhuvaragan 	if (timeout != TIPC_WAIT_FOREVER)
172 f3ad288cSParthasarathy Bhuvaragan 		mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
173 df79d040SJon Maloy 	return sub;
174 b97bf3fdSPer Liden }
175 b97bf3fdSPer Liden 
tipc_sub_unsubscribe(struct tipc_subscription * sub)176 242e82ccSJon Maloy void tipc_sub_unsubscribe(struct tipc_subscription *sub)
177 b97bf3fdSPer Liden {
178 df79d040SJon Maloy 	tipc_nametbl_unsubscribe(sub);
179 df79d040SJon Maloy 	if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
180 df79d040SJon Maloy 		del_timer_sync(&sub->timer);
181 da0a75e8SJon Maloy 	list_del(&sub->sub_list);
182 da0a75e8SJon Maloy 	tipc_sub_put(sub);
183 b97bf3fdSPer Liden }
184