blob: 8e00d739f03a64eeea4e490dfe2ba737325ef17d [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
Jon Maloy429189a2021-03-16 22:06:22 -040068 * @subscribed: the service range subscribed for
69 * @found: the service range we are checning for match
Per Lidenb97bf3f2006-01-02 19:04:38 +010070 *
Jon Maloy09f78b82021-03-16 22:06:21 -040071 * Returns true if there is overlap, otherwise false.
Per Lidenb97bf3f2006-01-02 19:04:38 +010072 */
Jon Maloy429189a2021-03-16 22:06:22 -040073static bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
74 struct tipc_service_range *found)
Per Lidenb97bf3f2006-01-02 19:04:38 +010075{
Jon Maloy429189a2021-03-16 22:06:22 -040076 u32 found_lower = found->lower;
77 u32 found_upper = found->upper;
78
79 if (found_lower < subscribed->lower)
80 found_lower = subscribed->lower;
81 if (found_upper > subscribed->upper)
82 found_upper = subscribed->upper;
83 return found_lower <= found_upper;
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 Maloy429189a2021-03-16 22:06:22 -040090 struct tipc_service_range *sr = &sub->s.seq;
91 u32 filter = sub->s.filter;
Parthasarathy Bhuvaragana4273c72016-02-02 10:52:10 +010092
Jon Maloy429189a2021-03-16 22:06:22 -040093 if (!tipc_sub_check_overlap(sr, &p->sr))
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 return;
Jon Maloy232d07b2018-01-08 21:03:30 +010095 if (!must && !(filter & TIPC_SUB_PORTS))
96 return;
Jon Maloy09f78b82021-03-16 22:06:21 -040097 if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
Jon Maloy232d07b2018-01-08 21:03:30 +010098 return;
Jon Maloy09f78b82021-03-16 22:06:21 -040099 if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 return;
Jon Maloydf79d042018-02-15 10:40:44 +0100101 spin_lock(&sub->lock);
Jon Maloy09f78b82021-03-16 22:06:21 -0400102 tipc_sub_send_event(sub, p, event);
Jon Maloydf79d042018-02-15 10:40:44 +0100103 spin_unlock(&sub->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104}
105
Jon Maloyda0a75e2018-02-15 10:40:48 +0100106static void tipc_sub_timeout(struct timer_list *t)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107{
Kees Cook31b102b2017-10-30 14:06:45 -0700108 struct tipc_subscription *sub = from_timer(sub, t, timer);
Ying Xue557d054c2017-03-21 10:47:49 +0100109
Jon Maloydf79d042018-02-15 10:40:44 +0100110 spin_lock(&sub->lock);
Jon Maloy09f78b82021-03-16 22:06:21 -0400111 tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
Jon Maloydf79d042018-02-15 10:40:44 +0100112 sub->inactive = true;
113 spin_unlock(&sub->lock);
Ying Xue00bc00a92015-05-04 10:36:46 +0800114}
115
Jon Maloyda0a75e2018-02-15 10:40:48 +0100116static void tipc_sub_kref_release(struct kref *kref)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100117{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100118 kfree(container_of(kref, struct tipc_subscription, kref));
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100119}
120
Jon Maloyda0a75e2018-02-15 10:40:48 +0100121void tipc_sub_put(struct tipc_subscription *subscription)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100122{
Jon Maloyda0a75e2018-02-15 10:40:48 +0100123 kref_put(&subscription->kref, tipc_sub_kref_release);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100124}
125
Jon Maloyda0a75e2018-02-15 10:40:48 +0100126void tipc_sub_get(struct tipc_subscription *subscription)
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100127{
128 kref_get(&subscription->kref);
129}
130
Jon Maloy5c45ab22018-02-15 10:40:49 +0100131struct tipc_subscription *tipc_sub_subscribe(struct net *net,
Jon Maloy242e82c2018-02-15 10:40:47 +0100132 struct tipc_subscr *s,
133 int conid)
Ying Xuea62fbcc2015-01-09 15:27:11 +0800134{
Jon Maloy429189a2021-03-16 22:06:22 -0400135 u32 lower = tipc_sub_read(s, seq.lower);
136 u32 upper = tipc_sub_read(s, seq.upper);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100137 u32 filter = tipc_sub_read(s, filter);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100138 struct tipc_subscription *sub;
Jon Maloy242e82c2018-02-15 10:40:47 +0100139 u32 timeout;
Lijun Cheneb409462006-10-16 21:59:42 -0700140
Jon Maloy242e82c2018-02-15 10:40:47 +0100141 if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
Jon Maloy429189a2021-03-16 22:06:22 -0400142 lower > upper) {
Jon Maloy242e82c2018-02-15 10:40:47 +0100143 pr_warn("Subscription rejected, illegal request\n");
144 return NULL;
145 }
Allan Stephens28353e72008-05-19 13:29:47 -0700146 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700147 if (!sub) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400148 pr_warn("Subscription rejected, no memory\n");
Parthasarathy Bhuvaragan7c13c622016-02-02 10:52:11 +0100149 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 }
Jon Maloyb7142952018-04-03 19:11:19 +0200151 INIT_LIST_HEAD(&sub->service_list);
152 INIT_LIST_HEAD(&sub->sub_list);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100153 sub->net = net;
Jon Maloydf79d042018-02-15 10:40:44 +0100154 sub->conid = conid;
155 sub->inactive = false;
Parthasarathy Bhuvaragan7c13c622016-02-02 10:52:11 +0100156 memcpy(&sub->evt.s, s, sizeof(*s));
Jon Maloy429189a2021-03-16 22:06:22 -0400157 sub->s.seq.type = tipc_sub_read(s, seq.type);
158 sub->s.seq.lower = lower;
159 sub->s.seq.upper = upper;
160 sub->s.filter = filter;
161 sub->s.timeout = tipc_sub_read(s, timeout);
162 memcpy(sub->s.usr_handle, s->usr_handle, 8);
Jon Maloydf79d042018-02-15 10:40:44 +0100163 spin_lock_init(&sub->lock);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100164 kref_init(&sub->kref);
Jon Maloyc3317f42018-04-11 22:52:09 +0200165 if (!tipc_nametbl_subscribe(sub)) {
166 kfree(sub);
167 return NULL;
168 }
Jon Maloyda0a75e2018-02-15 10:40:48 +0100169 timer_setup(&sub->timer, tipc_sub_timeout, 0);
Jon Maloy8985ecc2018-02-15 10:40:46 +0100170 timeout = tipc_sub_read(&sub->evt.s, timeout);
Parthasarathy Bhuvaragand094c4d2017-01-24 13:00:44 +0100171 if (timeout != TIPC_WAIT_FOREVER)
172 mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
Jon Maloydf79d042018-02-15 10:40:44 +0100173 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174}
175
Jon Maloy242e82c2018-02-15 10:40:47 +0100176void tipc_sub_unsubscribe(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177{
Jon Maloydf79d042018-02-15 10:40:44 +0100178 tipc_nametbl_unsubscribe(sub);
179 if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
180 del_timer_sync(&sub->timer);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100181 list_del(&sub->sub_list);
182 tipc_sub_put(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183}