blob: 5f8dc0e7488fa37d7e6f0992d91f483f72c0cbaf [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 *
Jon Maloydf79d042018-02-15 10:40:44 +01004 * Copyright (c) 2000-2017, Ericsson AB
Ying Xue13a2e892013-06-17 10:54:40 -04005 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
Jon Maloy09f78b82021-03-16 22:06:21 -04006 * Copyright (c) 2020-2021, Red Hat Inc
Per Lidenb97bf3f2006-01-02 19:04:38 +01007 * All rights reserved.
8 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * modification, are permitted provided that the following conditions are met:
11 *
Per Liden9ea1fd32006-01-11 13:30:43 +010012 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "name_table.h"
Allan Stephens5b06c85c2008-05-19 13:30:13 -070040#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
Jon Maloyda0a75e2018-02-15 10:40:48 +010042static void tipc_sub_send_event(struct tipc_subscription *sub,
Jon Maloy09f78b82021-03-16 22:06:21 -040043 struct publication *p,
44 u32 event)
Per Lidenb97bf3f2006-01-02 19:04:38 +010045{
Jon Maloy09f78b82021-03-16 22:06:21 -040046 struct tipc_subscr *s = &sub->evt.s;
Jon Maloy414574a2018-02-15 10:40:45 +010047 struct tipc_event *evt = &sub->evt;
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Jon Maloydf79d042018-02-15 10:40:44 +010049 if (sub->inactive)
50 return;
Jon Maloy8985ecc2018-02-15 10:40:46 +010051 tipc_evt_write(evt, event, event);
Jon Maloy09f78b82021-03-16 22:06:21 -040052 if (p) {
53 tipc_evt_write(evt, found_lower, p->sr.lower);
54 tipc_evt_write(evt, found_upper, p->sr.upper);
55 tipc_evt_write(evt, port.ref, p->sk.ref);
56 tipc_evt_write(evt, port.node, p->sk.node);
57 } else {
58 tipc_evt_write(evt, found_lower, s->seq.lower);
59 tipc_evt_write(evt, found_upper, s->seq.upper);
60 tipc_evt_write(evt, port.ref, 0);
61 tipc_evt_write(evt, port.node, 0);
62 }
Jon Maloy026321c2018-02-15 10:40:51 +010063 tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064}
65
66/**
Randy Dunlap5fcb7d42020-11-29 10:32:50 -080067 * tipc_sub_check_overlap - test for subscription overlap with the given values
68 * @seq: tipc_name_seq to check
69 * @found_lower: lower value to test
70 * @found_upper: upper value to test
Per Lidenb97bf3f2006-01-02 19:04:38 +010071 *
Jon Maloy09f78b82021-03-16 22:06:21 -040072 * Returns true if there is overlap, otherwise false.
Per Lidenb97bf3f2006-01-02 19:04:38 +010073 */
Jon Maloy09f78b82021-03-16 22:06:21 -040074bool tipc_sub_check_overlap(struct tipc_service_range *sr,
75 u32 found_lower, u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +010076{
Jon Maloy09f78b82021-03-16 22:06:21 -040077 if (found_lower < sr->lower)
78 found_lower = sr->lower;
79 if (found_upper > sr->upper)
80 found_upper = sr->upper;
Per Lidenb97bf3f2006-01-02 19:04:38 +010081 if (found_lower > found_upper)
Jon Maloy09f78b82021-03-16 22:06:21 -040082 return false;
83 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +010084}
85
Jon Maloyda0a75e2018-02-15 10:40:48 +010086void tipc_sub_report_overlap(struct tipc_subscription *sub,
Jon Maloy09f78b82021-03-16 22:06:21 -040087 struct publication *p,
88 u32 event, bool must)
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +010089{
Jon Maloy8985ecc2018-02-15 10:40:46 +010090 struct tipc_subscr *s = &sub->evt.s;
91 u32 filter = tipc_sub_read(s, filter);
Jon Maloyb6f88d92020-11-25 13:29:15 -050092 struct tipc_service_range seq;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +010093
Jon Maloy8985ecc2018-02-15 10:40:46 +010094 seq.type = tipc_sub_read(s, seq.type);
95 seq.lower = tipc_sub_read(s, seq.lower);
96 seq.upper = tipc_sub_read(s, seq.upper);
97
Jon Maloy09f78b82021-03-16 22:06:21 -040098 if (!tipc_sub_check_overlap(&seq, p->sr.lower, p->sr.upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +010099 return;
Jon Maloy232d07b2018-01-08 21:03:30 +0100100 if (!must && !(filter & TIPC_SUB_PORTS))
101 return;
Jon Maloy09f78b82021-03-16 22:06:21 -0400102 if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
Jon Maloy232d07b2018-01-08 21:03:30 +0100103 return;
Jon Maloy09f78b82021-03-16 22:06:21 -0400104 if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 return;
Jon Maloydf79d042018-02-15 10:40:44 +0100106 spin_lock(&sub->lock);
Jon Maloy09f78b82021-03-16 22:06:21 -0400107 tipc_sub_send_event(sub, p, event);
Jon Maloydf79d042018-02-15 10:40:44 +0100108 spin_unlock(&sub->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109}
110
Jon Maloyda0a75e2018-02-15 10:40:48 +0100111static void tipc_sub_timeout(struct timer_list *t)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112{
Kees Cook31b102b2017-10-30 14:06:45 -0700113 struct tipc_subscription *sub = from_timer(sub, t, timer);
Ying Xue557d054c2017-03-21 10:47:49 +0100114
Jon Maloydf79d042018-02-15 10:40:44 +0100115 spin_lock(&sub->lock);
Jon Maloy09f78b82021-03-16 22:06:21 -0400116 tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
Jon Maloydf79d042018-02-15 10:40:44 +0100117 sub->inactive = true;
118 spin_unlock(&sub->lock);
Ying Xue00bc00a92015-05-04 10:36:46 +0800119}
120
Jon Maloyda0a75e2018-02-15 10:40:48 +0100121static void tipc_sub_kref_release(struct kref *kref)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100122{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100123 kfree(container_of(kref, struct tipc_subscription, kref));
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100124}
125
Jon Maloyda0a75e2018-02-15 10:40:48 +0100126void tipc_sub_put(struct tipc_subscription *subscription)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100127{
Jon Maloyda0a75e2018-02-15 10:40:48 +0100128 kref_put(&subscription->kref, tipc_sub_kref_release);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100129}
130
Jon Maloyda0a75e2018-02-15 10:40:48 +0100131void tipc_sub_get(struct tipc_subscription *subscription)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100132{
133 kref_get(&subscription->kref);
134}
135
Jon Maloy5c45ab22018-02-15 10:40:49 +0100136struct tipc_subscription *tipc_sub_subscribe(struct net *net,
Jon Maloy242e82c2018-02-15 10:40:47 +0100137 struct tipc_subscr *s,
138 int conid)
Ying Xuea62fbcc2015-01-09 15:27:11 +0800139{
Jon Maloy8985ecc2018-02-15 10:40:46 +0100140 u32 filter = tipc_sub_read(s, filter);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100141 struct tipc_subscription *sub;
Jon Maloy242e82c2018-02-15 10:40:47 +0100142 u32 timeout;
Lijun Cheneb409462006-10-16 21:59:42 -0700143
Jon Maloy242e82c2018-02-15 10:40:47 +0100144 if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
145 (tipc_sub_read(s, seq.lower) > tipc_sub_read(s, seq.upper))) {
146 pr_warn("Subscription rejected, illegal request\n");
147 return NULL;
148 }
Allan Stephens28353e72008-05-19 13:29:47 -0700149 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700150 if (!sub) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400151 pr_warn("Subscription rejected, no memory\n");
Parthasarathy Bhuvaragan7c13c622016-02-02 10:52:11 +0100152 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 }
Jon Maloyb7142952018-04-03 19:11:19 +0200154 INIT_LIST_HEAD(&sub->service_list);
155 INIT_LIST_HEAD(&sub->sub_list);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100156 sub->net = net;
Jon Maloydf79d042018-02-15 10:40:44 +0100157 sub->conid = conid;
158 sub->inactive = false;
Parthasarathy Bhuvaragan7c13c622016-02-02 10:52:11 +0100159 memcpy(&sub->evt.s, s, sizeof(*s));
Jon Maloydf79d042018-02-15 10:40:44 +0100160 spin_lock_init(&sub->lock);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100161 kref_init(&sub->kref);
Jon Maloyc3317f42018-04-11 22:52:09 +0200162 if (!tipc_nametbl_subscribe(sub)) {
163 kfree(sub);
164 return NULL;
165 }
Jon Maloyda0a75e2018-02-15 10:40:48 +0100166 timer_setup(&sub->timer, tipc_sub_timeout, 0);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100167 timeout = tipc_sub_read(&sub->evt.s, timeout);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100168 if (timeout != TIPC_WAIT_FOREVER)
169 mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
Jon Maloydf79d042018-02-15 10:40:44 +0100170 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171}
172
Jon Maloy242e82c2018-02-15 10:40:47 +0100173void tipc_sub_unsubscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174{
Jon Maloydf79d042018-02-15 10:40:44 +0100175 tipc_nametbl_unsubscribe(sub);
176 if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
177 del_timer_sync(&sub->timer);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100178 list_del(&sub->sub_list);
179 tipc_sub_put(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180}