162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/addr.c: TIPC address utility routines
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2000-2006, 2018, Ericsson AB
562306a36Sopenharmony_ci * Copyright (c) 2004-2005, 2010-2011, 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 "addr.h"
3962306a36Sopenharmony_ci#include "core.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cibool tipc_in_scope(bool legacy_format, u32 domain, u32 addr)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	if (!domain || (domain == addr))
4462306a36Sopenharmony_ci		return true;
4562306a36Sopenharmony_ci	if (!legacy_format)
4662306a36Sopenharmony_ci		return false;
4762306a36Sopenharmony_ci	if (domain == tipc_cluster_mask(addr)) /* domain <Z.C.0> */
4862306a36Sopenharmony_ci		return true;
4962306a36Sopenharmony_ci	if (domain == (addr & TIPC_ZONE_CLUSTER_MASK)) /* domain <Z.C.0> */
5062306a36Sopenharmony_ci		return true;
5162306a36Sopenharmony_ci	if (domain == (addr & TIPC_ZONE_MASK)) /* domain <Z.0.0> */
5262306a36Sopenharmony_ci		return true;
5362306a36Sopenharmony_ci	return false;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_civoid tipc_set_node_id(struct net *net, u8 *id)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	struct tipc_net *tn = tipc_net(net);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	memcpy(tn->node_id, id, NODE_ID_LEN);
6162306a36Sopenharmony_ci	tipc_nodeid2string(tn->node_id_string, id);
6262306a36Sopenharmony_ci	tn->trial_addr = hash128to32(id);
6362306a36Sopenharmony_ci	pr_info("Node identity %s, cluster identity %u\n",
6462306a36Sopenharmony_ci		tipc_own_id_string(net), tn->net_id);
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_civoid tipc_set_node_addr(struct net *net, u32 addr)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	struct tipc_net *tn = tipc_net(net);
7062306a36Sopenharmony_ci	u8 node_id[NODE_ID_LEN] = {0,};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	tn->node_addr = addr;
7362306a36Sopenharmony_ci	if (!tipc_own_id(net)) {
7462306a36Sopenharmony_ci		sprintf(node_id, "%x", addr);
7562306a36Sopenharmony_ci		tipc_set_node_id(net, node_id);
7662306a36Sopenharmony_ci	}
7762306a36Sopenharmony_ci	tn->trial_addr = addr;
7862306a36Sopenharmony_ci	tn->addr_trial_end = jiffies;
7962306a36Sopenharmony_ci	pr_info("Node number set to %u\n", addr);
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cichar *tipc_nodeid2string(char *str, u8 *id)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	int i;
8562306a36Sopenharmony_ci	u8 c;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* Already a string ? */
8862306a36Sopenharmony_ci	for (i = 0; i < NODE_ID_LEN; i++) {
8962306a36Sopenharmony_ci		c = id[i];
9062306a36Sopenharmony_ci		if (c >= '0' && c <= '9')
9162306a36Sopenharmony_ci			continue;
9262306a36Sopenharmony_ci		if (c >= 'A' && c <= 'Z')
9362306a36Sopenharmony_ci			continue;
9462306a36Sopenharmony_ci		if (c >= 'a' && c <= 'z')
9562306a36Sopenharmony_ci			continue;
9662306a36Sopenharmony_ci		if (c == '.')
9762306a36Sopenharmony_ci			continue;
9862306a36Sopenharmony_ci		if (c == ':')
9962306a36Sopenharmony_ci			continue;
10062306a36Sopenharmony_ci		if (c == '_')
10162306a36Sopenharmony_ci			continue;
10262306a36Sopenharmony_ci		if (c == '-')
10362306a36Sopenharmony_ci			continue;
10462306a36Sopenharmony_ci		if (c == '@')
10562306a36Sopenharmony_ci			continue;
10662306a36Sopenharmony_ci		if (c != 0)
10762306a36Sopenharmony_ci			break;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci	if (i == NODE_ID_LEN) {
11062306a36Sopenharmony_ci		memcpy(str, id, NODE_ID_LEN);
11162306a36Sopenharmony_ci		str[NODE_ID_LEN] = 0;
11262306a36Sopenharmony_ci		return str;
11362306a36Sopenharmony_ci	}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* Translate to hex string */
11662306a36Sopenharmony_ci	for (i = 0; i < NODE_ID_LEN; i++)
11762306a36Sopenharmony_ci		sprintf(&str[2 * i], "%02x", id[i]);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/* Strip off trailing zeroes */
12062306a36Sopenharmony_ci	for (i = NODE_ID_STR_LEN - 2; str[i] == '0'; i--)
12162306a36Sopenharmony_ci		str[i] = 0;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	return str;
12462306a36Sopenharmony_ci}
125