M7350/kernel/net/tipc/subscr.c

375 lines
11 KiB
C
Raw Normal View History

2024-09-09 08:52:07 +00:00
/*
* net/tipc/subscr.c: TIPC network topology service
*
* Copyright (c) 2000-2006, Ericsson AB
2024-09-09 08:57:42 +00:00
* Copyright (c) 2005-2007, 2010-2013, Wind River Systems
2024-09-09 08:52:07 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "core.h"
#include "name_table.h"
#include "subscr.h"
/**
* struct tipc_subscriber - TIPC network topology subscriber
2024-09-09 08:57:42 +00:00
* @conid: connection identifier to server connecting to subscriber
* @lock: control access to subscriber
2024-09-09 08:52:07 +00:00
* @subscription_list: list of subscription objects for this subscriber
*/
struct tipc_subscriber {
2024-09-09 08:57:42 +00:00
int conid;
spinlock_t lock;
2024-09-09 08:52:07 +00:00
struct list_head subscription_list;
};
2024-09-09 08:57:42 +00:00
static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
void *usr_data, void *buf, size_t len);
static void *subscr_named_msg_event(int conid);
static void subscr_conn_shutdown_event(int conid, void *usr_data);
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
static atomic_t subscription_count = ATOMIC_INIT(0);
static struct sockaddr_tipc topsrv_addr __read_mostly = {
.family = AF_TIPC,
.addrtype = TIPC_ADDR_NAMESEQ,
.addr.nameseq.type = TIPC_TOP_SRV,
.addr.nameseq.lower = TIPC_TOP_SRV,
.addr.nameseq.upper = TIPC_TOP_SRV,
.scope = TIPC_NODE_SCOPE
2024-09-09 08:52:07 +00:00
};
2024-09-09 08:57:42 +00:00
static struct tipc_server topsrv __read_mostly = {
.saddr = &topsrv_addr,
.imp = TIPC_CRITICAL_IMPORTANCE,
.type = SOCK_SEQPACKET,
.max_rcvbuf_size = sizeof(struct tipc_subscr),
.name = "topology_server",
.tipc_conn_recvmsg = subscr_conn_msg_event,
.tipc_conn_new = subscr_named_msg_event,
.tipc_conn_shutdown = subscr_conn_shutdown_event,
};
2024-09-09 08:52:07 +00:00
/**
* htohl - convert value to endianness used by destination
* @in: value to convert
* @swap: non-zero if endianness must be reversed
*
* Returns converted value
*/
static u32 htohl(u32 in, int swap)
{
return swap ? swab32(in) : in;
}
2024-09-09 08:57:42 +00:00
static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
u32 found_upper, u32 event, u32 port_ref,
2024-09-09 08:52:07 +00:00
u32 node)
{
2024-09-09 08:57:42 +00:00
struct tipc_subscriber *subscriber = sub->subscriber;
struct kvec msg_sect;
2024-09-09 08:52:07 +00:00
msg_sect.iov_base = (void *)&sub->evt;
msg_sect.iov_len = sizeof(struct tipc_event);
sub->evt.event = htohl(event, sub->swap);
sub->evt.found_lower = htohl(found_lower, sub->swap);
sub->evt.found_upper = htohl(found_upper, sub->swap);
sub->evt.port.ref = htohl(port_ref, sub->swap);
sub->evt.port.node = htohl(node, sub->swap);
2024-09-09 08:57:42 +00:00
tipc_conn_sendmsg(&topsrv, subscriber->conid, NULL, msg_sect.iov_base,
msg_sect.iov_len);
2024-09-09 08:52:07 +00:00
}
/**
* tipc_subscr_overlap - test for subscription overlap with the given values
*
* Returns 1 if there is overlap, otherwise 0.
*/
2024-09-09 08:57:42 +00:00
int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
2024-09-09 08:52:07 +00:00
u32 found_upper)
{
if (found_lower < sub->seq.lower)
found_lower = sub->seq.lower;
if (found_upper > sub->seq.upper)
found_upper = sub->seq.upper;
if (found_lower > found_upper)
return 0;
return 1;
}
/**
* tipc_subscr_report_overlap - issue event if there is subscription overlap
*
* Protected by nameseq.lock in name_table.c
*/
2024-09-09 08:57:42 +00:00
void tipc_subscr_report_overlap(struct tipc_subscription *sub, u32 found_lower,
u32 found_upper, u32 event, u32 port_ref,
u32 node, int must)
2024-09-09 08:52:07 +00:00
{
if (!tipc_subscr_overlap(sub, found_lower, found_upper))
return;
if (!must && !(sub->filter & TIPC_SUB_PORTS))
return;
subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
}
static void subscr_timeout(struct tipc_subscription *sub)
{
2024-09-09 08:57:42 +00:00
struct tipc_subscriber *subscriber = sub->subscriber;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
/* The spin lock per subscriber is used to protect its members */
spin_lock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
/* Validate timeout (in case subscription is being cancelled) */
if (sub->timeout == TIPC_WAIT_FOREVER) {
2024-09-09 08:57:42 +00:00
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
return;
}
/* Unlink subscription from name table */
tipc_nametbl_unsubscribe(sub);
/* Unlink subscription from subscriber */
list_del(&sub->subscription_list);
2024-09-09 08:57:42 +00:00
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
/* Notify subscriber of timeout */
subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
TIPC_SUBSCR_TIMEOUT, 0, 0);
/* Now destroy subscription */
k_term_timer(&sub->timer);
kfree(sub);
2024-09-09 08:57:42 +00:00
atomic_dec(&subscription_count);
2024-09-09 08:52:07 +00:00
}
/**
* subscr_del - delete a subscription within a subscription list
*
2024-09-09 08:57:42 +00:00
* Called with subscriber lock held.
2024-09-09 08:52:07 +00:00
*/
static void subscr_del(struct tipc_subscription *sub)
{
tipc_nametbl_unsubscribe(sub);
list_del(&sub->subscription_list);
kfree(sub);
2024-09-09 08:57:42 +00:00
atomic_dec(&subscription_count);
2024-09-09 08:52:07 +00:00
}
/**
* subscr_terminate - terminate communication with a subscriber
*
2024-09-09 08:57:42 +00:00
* Note: Must call it in process context since it might sleep.
2024-09-09 08:52:07 +00:00
*/
static void subscr_terminate(struct tipc_subscriber *subscriber)
{
2024-09-09 08:57:42 +00:00
tipc_conn_terminate(&topsrv, subscriber->conid);
}
static void subscr_release(struct tipc_subscriber *subscriber)
{
2024-09-09 08:52:07 +00:00
struct tipc_subscription *sub;
struct tipc_subscription *sub_temp;
2024-09-09 08:57:42 +00:00
spin_lock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
/* Destroy any existing subscriptions for subscriber */
list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
subscription_list) {
if (sub->timeout != TIPC_WAIT_FOREVER) {
2024-09-09 08:57:42 +00:00
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
k_cancel_timer(&sub->timer);
k_term_timer(&sub->timer);
2024-09-09 08:57:42 +00:00
spin_lock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
}
subscr_del(sub);
}
2024-09-09 08:57:42 +00:00
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
/* Now destroy subscriber */
kfree(subscriber);
}
/**
* subscr_cancel - handle subscription cancellation request
*
2024-09-09 08:57:42 +00:00
* Called with subscriber lock held. Routine must temporarily release lock
2024-09-09 08:52:07 +00:00
* to enable the subscription timeout routine to finish without deadlocking;
* the lock is then reclaimed to allow caller to release it upon return.
*
* Note that fields of 's' use subscriber's endianness!
*/
static void subscr_cancel(struct tipc_subscr *s,
struct tipc_subscriber *subscriber)
{
struct tipc_subscription *sub;
struct tipc_subscription *sub_temp;
int found = 0;
/* Find first matching subscription, exit if not found */
list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
subscription_list) {
if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
found = 1;
break;
}
}
if (!found)
return;
/* Cancel subscription timer (if used), then delete subscription */
if (sub->timeout != TIPC_WAIT_FOREVER) {
sub->timeout = TIPC_WAIT_FOREVER;
2024-09-09 08:57:42 +00:00
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
k_cancel_timer(&sub->timer);
k_term_timer(&sub->timer);
2024-09-09 08:57:42 +00:00
spin_lock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
}
subscr_del(sub);
}
/**
* subscr_subscribe - create subscription for subscriber
*
2024-09-09 08:57:42 +00:00
* Called with subscriber lock held.
2024-09-09 08:52:07 +00:00
*/
2024-09-09 08:57:42 +00:00
static int subscr_subscribe(struct tipc_subscr *s,
struct tipc_subscriber *subscriber,
struct tipc_subscription **sub_p) {
2024-09-09 08:52:07 +00:00
struct tipc_subscription *sub;
int swap;
/* Determine subscriber's endianness */
swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
/* Detect & process a subscription cancellation request */
if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
subscr_cancel(s, subscriber);
2024-09-09 08:57:42 +00:00
return 0;
2024-09-09 08:52:07 +00:00
}
/* Refuse subscription if global limit exceeded */
2024-09-09 08:57:42 +00:00
if (atomic_read(&subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
pr_warn("Subscription rejected, limit reached (%u)\n",
TIPC_MAX_SUBSCRIPTIONS);
return -EINVAL;
2024-09-09 08:52:07 +00:00
}
/* Allocate subscription object */
sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
if (!sub) {
2024-09-09 08:57:42 +00:00
pr_warn("Subscription rejected, no memory\n");
return -ENOMEM;
2024-09-09 08:52:07 +00:00
}
/* Initialize subscription object */
sub->seq.type = htohl(s->seq.type, swap);
sub->seq.lower = htohl(s->seq.lower, swap);
sub->seq.upper = htohl(s->seq.upper, swap);
sub->timeout = htohl(s->timeout, swap);
sub->filter = htohl(s->filter, swap);
if ((!(sub->filter & TIPC_SUB_PORTS) ==
!(sub->filter & TIPC_SUB_SERVICE)) ||
(sub->seq.lower > sub->seq.upper)) {
2024-09-09 08:57:42 +00:00
pr_warn("Subscription rejected, illegal request\n");
2024-09-09 08:52:07 +00:00
kfree(sub);
2024-09-09 08:57:42 +00:00
return -EINVAL;
2024-09-09 08:52:07 +00:00
}
INIT_LIST_HEAD(&sub->nameseq_list);
list_add(&sub->subscription_list, &subscriber->subscription_list);
2024-09-09 08:57:42 +00:00
sub->subscriber = subscriber;
2024-09-09 08:52:07 +00:00
sub->swap = swap;
memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
2024-09-09 08:57:42 +00:00
atomic_inc(&subscription_count);
2024-09-09 08:52:07 +00:00
if (sub->timeout != TIPC_WAIT_FOREVER) {
k_init_timer(&sub->timer,
(Handler)subscr_timeout, (unsigned long)sub);
k_start_timer(&sub->timer, sub->timeout);
}
2024-09-09 08:57:42 +00:00
*sub_p = sub;
return 0;
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
/* Handle one termination request for the subscriber */
static void subscr_conn_shutdown_event(int conid, void *usr_data)
2024-09-09 08:52:07 +00:00
{
2024-09-09 08:57:42 +00:00
subscr_release((struct tipc_subscriber *)usr_data);
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
/* Handle one request to create a new subscription for the subscriber */
static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
void *usr_data, void *buf, size_t len)
2024-09-09 08:52:07 +00:00
{
2024-09-09 08:57:42 +00:00
struct tipc_subscriber *subscriber = usr_data;
struct tipc_subscription *sub = NULL;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
spin_lock_bh(&subscriber->lock);
if (subscr_subscribe((struct tipc_subscr *)buf, subscriber, &sub) < 0) {
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
subscr_terminate(subscriber);
2024-09-09 08:57:42 +00:00
return;
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
if (sub)
tipc_nametbl_subscribe(sub);
spin_unlock_bh(&subscriber->lock);
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
/* Handle one request to establish a new subscriber */
static void *subscr_named_msg_event(int conid)
2024-09-09 08:52:07 +00:00
{
struct tipc_subscriber *subscriber;
/* Create subscriber object */
subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
if (subscriber == NULL) {
2024-09-09 08:57:42 +00:00
pr_warn("Subscriber rejected, no memory\n");
return NULL;
2024-09-09 08:52:07 +00:00
}
INIT_LIST_HEAD(&subscriber->subscription_list);
2024-09-09 08:57:42 +00:00
subscriber->conid = conid;
spin_lock_init(&subscriber->lock);
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
return (void *)subscriber;
2024-09-09 08:52:07 +00:00
}
int tipc_subscr_start(void)
{
2024-09-09 08:57:42 +00:00
return tipc_server_start(&topsrv);
2024-09-09 08:52:07 +00:00
}
void tipc_subscr_stop(void)
{
2024-09-09 08:57:42 +00:00
tipc_server_stop(&topsrv);
2024-09-09 08:52:07 +00:00
}