162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/subscr.c: TIPC network topology service
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2000-2017, Ericsson AB
562306a36Sopenharmony_ci * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
662306a36Sopenharmony_ci * Copyright (c) 2020-2021, Red Hat Inc
762306a36Sopenharmony_ci * All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
1062306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1362306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1462306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1562306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1662306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
1762306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
1862306a36Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
1962306a36Sopenharmony_ci *    this software without specific prior written permission.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2262306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2362306a36Sopenharmony_ci * Software Foundation.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2662306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2762306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2862306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2962306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3062306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3162306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3262306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3362306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3462306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3562306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include "core.h"
3962306a36Sopenharmony_ci#include "name_table.h"
4062306a36Sopenharmony_ci#include "subscr.h"
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic void tipc_sub_send_event(struct tipc_subscription *sub,
4362306a36Sopenharmony_ci				struct publication *p,
4462306a36Sopenharmony_ci				u32 event)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	struct tipc_subscr *s = &sub->evt.s;
4762306a36Sopenharmony_ci	struct tipc_event *evt = &sub->evt;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	if (sub->inactive)
5062306a36Sopenharmony_ci		return;
5162306a36Sopenharmony_ci	tipc_evt_write(evt, event, event);
5262306a36Sopenharmony_ci	if (p) {
5362306a36Sopenharmony_ci		tipc_evt_write(evt, found_lower, p->sr.lower);
5462306a36Sopenharmony_ci		tipc_evt_write(evt, found_upper, p->sr.upper);
5562306a36Sopenharmony_ci		tipc_evt_write(evt, port.ref, p->sk.ref);
5662306a36Sopenharmony_ci		tipc_evt_write(evt, port.node, p->sk.node);
5762306a36Sopenharmony_ci	} else {
5862306a36Sopenharmony_ci		tipc_evt_write(evt, found_lower, s->seq.lower);
5962306a36Sopenharmony_ci		tipc_evt_write(evt, found_upper, s->seq.upper);
6062306a36Sopenharmony_ci		tipc_evt_write(evt, port.ref, 0);
6162306a36Sopenharmony_ci		tipc_evt_write(evt, port.node, 0);
6262306a36Sopenharmony_ci	}
6362306a36Sopenharmony_ci	tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * tipc_sub_check_overlap - test for subscription overlap with the given values
6862306a36Sopenharmony_ci * @subscribed: the service range subscribed for
6962306a36Sopenharmony_ci * @found: the service range we are checking for match
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * Returns true if there is overlap, otherwise false.
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_cistatic bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
7462306a36Sopenharmony_ci				   struct tipc_service_range *found)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	u32 found_lower = found->lower;
7762306a36Sopenharmony_ci	u32 found_upper = found->upper;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	if (found_lower < subscribed->lower)
8062306a36Sopenharmony_ci		found_lower = subscribed->lower;
8162306a36Sopenharmony_ci	if (found_upper > subscribed->upper)
8262306a36Sopenharmony_ci		found_upper = subscribed->upper;
8362306a36Sopenharmony_ci	return found_lower <= found_upper;
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_civoid tipc_sub_report_overlap(struct tipc_subscription *sub,
8762306a36Sopenharmony_ci			     struct publication *p,
8862306a36Sopenharmony_ci			     u32 event, bool must)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	struct tipc_service_range *sr = &sub->s.seq;
9162306a36Sopenharmony_ci	u32 filter = sub->s.filter;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	if (!tipc_sub_check_overlap(sr, &p->sr))
9462306a36Sopenharmony_ci		return;
9562306a36Sopenharmony_ci	if (!must && !(filter & TIPC_SUB_PORTS))
9662306a36Sopenharmony_ci		return;
9762306a36Sopenharmony_ci	if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
9862306a36Sopenharmony_ci		return;
9962306a36Sopenharmony_ci	if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
10062306a36Sopenharmony_ci		return;
10162306a36Sopenharmony_ci	spin_lock(&sub->lock);
10262306a36Sopenharmony_ci	tipc_sub_send_event(sub, p, event);
10362306a36Sopenharmony_ci	spin_unlock(&sub->lock);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic void tipc_sub_timeout(struct timer_list *t)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct tipc_subscription *sub = from_timer(sub, t, timer);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	spin_lock(&sub->lock);
11162306a36Sopenharmony_ci	tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
11262306a36Sopenharmony_ci	sub->inactive = true;
11362306a36Sopenharmony_ci	spin_unlock(&sub->lock);
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic void tipc_sub_kref_release(struct kref *kref)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	kfree(container_of(kref, struct tipc_subscription, kref));
11962306a36Sopenharmony_ci}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_civoid tipc_sub_put(struct tipc_subscription *subscription)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	kref_put(&subscription->kref, tipc_sub_kref_release);
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_civoid tipc_sub_get(struct tipc_subscription *subscription)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	kref_get(&subscription->kref);
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistruct tipc_subscription *tipc_sub_subscribe(struct net *net,
13262306a36Sopenharmony_ci					     struct tipc_subscr *s,
13362306a36Sopenharmony_ci					     int conid)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	u32 lower = tipc_sub_read(s, seq.lower);
13662306a36Sopenharmony_ci	u32 upper = tipc_sub_read(s, seq.upper);
13762306a36Sopenharmony_ci	u32 filter = tipc_sub_read(s, filter);
13862306a36Sopenharmony_ci	struct tipc_subscription *sub;
13962306a36Sopenharmony_ci	u32 timeout;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
14262306a36Sopenharmony_ci	    lower > upper) {
14362306a36Sopenharmony_ci		pr_warn("Subscription rejected, illegal request\n");
14462306a36Sopenharmony_ci		return NULL;
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
14762306a36Sopenharmony_ci	if (!sub) {
14862306a36Sopenharmony_ci		pr_warn("Subscription rejected, no memory\n");
14962306a36Sopenharmony_ci		return NULL;
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci	INIT_LIST_HEAD(&sub->service_list);
15262306a36Sopenharmony_ci	INIT_LIST_HEAD(&sub->sub_list);
15362306a36Sopenharmony_ci	sub->net = net;
15462306a36Sopenharmony_ci	sub->conid = conid;
15562306a36Sopenharmony_ci	sub->inactive = false;
15662306a36Sopenharmony_ci	memcpy(&sub->evt.s, s, sizeof(*s));
15762306a36Sopenharmony_ci	sub->s.seq.type = tipc_sub_read(s, seq.type);
15862306a36Sopenharmony_ci	sub->s.seq.lower = lower;
15962306a36Sopenharmony_ci	sub->s.seq.upper = upper;
16062306a36Sopenharmony_ci	sub->s.filter = filter;
16162306a36Sopenharmony_ci	sub->s.timeout = tipc_sub_read(s, timeout);
16262306a36Sopenharmony_ci	memcpy(sub->s.usr_handle, s->usr_handle, 8);
16362306a36Sopenharmony_ci	spin_lock_init(&sub->lock);
16462306a36Sopenharmony_ci	kref_init(&sub->kref);
16562306a36Sopenharmony_ci	if (!tipc_nametbl_subscribe(sub)) {
16662306a36Sopenharmony_ci		kfree(sub);
16762306a36Sopenharmony_ci		return NULL;
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci	timer_setup(&sub->timer, tipc_sub_timeout, 0);
17062306a36Sopenharmony_ci	timeout = tipc_sub_read(&sub->evt.s, timeout);
17162306a36Sopenharmony_ci	if (timeout != TIPC_WAIT_FOREVER)
17262306a36Sopenharmony_ci		mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
17362306a36Sopenharmony_ci	return sub;
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_civoid tipc_sub_unsubscribe(struct tipc_subscription *sub)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	tipc_nametbl_unsubscribe(sub);
17962306a36Sopenharmony_ci	if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
18062306a36Sopenharmony_ci		del_timer_sync(&sub->timer);
18162306a36Sopenharmony_ci	list_del(&sub->sub_list);
18262306a36Sopenharmony_ci	tipc_sub_put(sub);
18362306a36Sopenharmony_ci}
184