Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * net/tipc/addr.c: TIPC address utility routines
0003  *
0004  * Copyright (c) 2000-2006, 2018, Ericsson AB
0005  * Copyright (c) 2004-2005, 2010-2011, Wind River Systems
0006  * Copyright (c) 2020-2021, Red Hat Inc
0007  * All rights reserved.
0008  *
0009  * Redistribution and use in source and binary forms, with or without
0010  * modification, are permitted provided that the following conditions are met:
0011  *
0012  * 1. Redistributions of source code must retain the above copyright
0013  *    notice, this list of conditions and the following disclaimer.
0014  * 2. Redistributions in binary form must reproduce the above copyright
0015  *    notice, this list of conditions and the following disclaimer in the
0016  *    documentation and/or other materials provided with the distribution.
0017  * 3. Neither the names of the copyright holders nor the names of its
0018  *    contributors may be used to endorse or promote products derived from
0019  *    this software without specific prior written permission.
0020  *
0021  * Alternatively, this software may be distributed under the terms of the
0022  * GNU General Public License ("GPL") version 2 as published by the Free
0023  * Software Foundation.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #include "addr.h"
0039 #include "core.h"
0040 
0041 bool tipc_in_scope(bool legacy_format, u32 domain, u32 addr)
0042 {
0043     if (!domain || (domain == addr))
0044         return true;
0045     if (!legacy_format)
0046         return false;
0047     if (domain == tipc_cluster_mask(addr)) /* domain <Z.C.0> */
0048         return true;
0049     if (domain == (addr & TIPC_ZONE_CLUSTER_MASK)) /* domain <Z.C.0> */
0050         return true;
0051     if (domain == (addr & TIPC_ZONE_MASK)) /* domain <Z.0.0> */
0052         return true;
0053     return false;
0054 }
0055 
0056 void tipc_set_node_id(struct net *net, u8 *id)
0057 {
0058     struct tipc_net *tn = tipc_net(net);
0059 
0060     memcpy(tn->node_id, id, NODE_ID_LEN);
0061     tipc_nodeid2string(tn->node_id_string, id);
0062     tn->trial_addr = hash128to32(id);
0063     pr_info("Node identity %s, cluster identity %u\n",
0064         tipc_own_id_string(net), tn->net_id);
0065 }
0066 
0067 void tipc_set_node_addr(struct net *net, u32 addr)
0068 {
0069     struct tipc_net *tn = tipc_net(net);
0070     u8 node_id[NODE_ID_LEN] = {0,};
0071 
0072     tn->node_addr = addr;
0073     if (!tipc_own_id(net)) {
0074         sprintf(node_id, "%x", addr);
0075         tipc_set_node_id(net, node_id);
0076     }
0077     tn->trial_addr = addr;
0078     tn->addr_trial_end = jiffies;
0079     pr_info("Node number set to %u\n", addr);
0080 }
0081 
0082 char *tipc_nodeid2string(char *str, u8 *id)
0083 {
0084     int i;
0085     u8 c;
0086 
0087     /* Already a string ? */
0088     for (i = 0; i < NODE_ID_LEN; i++) {
0089         c = id[i];
0090         if (c >= '0' && c <= '9')
0091             continue;
0092         if (c >= 'A' && c <= 'Z')
0093             continue;
0094         if (c >= 'a' && c <= 'z')
0095             continue;
0096         if (c == '.')
0097             continue;
0098         if (c == ':')
0099             continue;
0100         if (c == '_')
0101             continue;
0102         if (c == '-')
0103             continue;
0104         if (c == '@')
0105             continue;
0106         if (c != 0)
0107             break;
0108     }
0109     if (i == NODE_ID_LEN) {
0110         memcpy(str, id, NODE_ID_LEN);
0111         str[NODE_ID_LEN] = 0;
0112         return str;
0113     }
0114 
0115     /* Translate to hex string */
0116     for (i = 0; i < NODE_ID_LEN; i++)
0117         sprintf(&str[2 * i], "%02x", id[i]);
0118 
0119     /* Strip off trailing zeroes */
0120     for (i = NODE_ID_STR_LEN - 2; str[i] == '0'; i--)
0121         str[i] = 0;
0122 
0123     return str;
0124 }