162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * net/tipc/net.c: TIPC network routing code 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright (c) 1995-2006, 2014, Ericsson AB 562306a36Sopenharmony_ci * Copyright (c) 2005, 2010-2011, Wind River Systems 662306a36Sopenharmony_ci * All rights reserved. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 962306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met: 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 1262306a36Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 1362306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 1462306a36Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 1562306a36Sopenharmony_ci * documentation and/or other materials provided with the distribution. 1662306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its 1762306a36Sopenharmony_ci * contributors may be used to endorse or promote products derived from 1862306a36Sopenharmony_ci * this software without specific prior written permission. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the 2162306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free 2262306a36Sopenharmony_ci * Software Foundation. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2562306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2662306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2762306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2862306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2962306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3062306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3162306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3262306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3362306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3462306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#include "core.h" 3862306a36Sopenharmony_ci#include "net.h" 3962306a36Sopenharmony_ci#include "name_distr.h" 4062306a36Sopenharmony_ci#include "subscr.h" 4162306a36Sopenharmony_ci#include "socket.h" 4262306a36Sopenharmony_ci#include "node.h" 4362306a36Sopenharmony_ci#include "bcast.h" 4462306a36Sopenharmony_ci#include "link.h" 4562306a36Sopenharmony_ci#include "netlink.h" 4662306a36Sopenharmony_ci#include "monitor.h" 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci/* 4962306a36Sopenharmony_ci * The TIPC locking policy is designed to ensure a very fine locking 5062306a36Sopenharmony_ci * granularity, permitting complete parallel access to individual 5162306a36Sopenharmony_ci * port and node/link instances. The code consists of four major 5262306a36Sopenharmony_ci * locking domains, each protected with their own disjunct set of locks. 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * 1: The bearer level. 5562306a36Sopenharmony_ci * RTNL lock is used to serialize the process of configuring bearer 5662306a36Sopenharmony_ci * on update side, and RCU lock is applied on read side to make 5762306a36Sopenharmony_ci * bearer instance valid on both paths of message transmission and 5862306a36Sopenharmony_ci * reception. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * 2: The node and link level. 6162306a36Sopenharmony_ci * All node instances are saved into two tipc_node_list and node_htable 6262306a36Sopenharmony_ci * lists. The two lists are protected by node_list_lock on write side, 6362306a36Sopenharmony_ci * and they are guarded with RCU lock on read side. Especially node 6462306a36Sopenharmony_ci * instance is destroyed only when TIPC module is removed, and we can 6562306a36Sopenharmony_ci * confirm that there has no any user who is accessing the node at the 6662306a36Sopenharmony_ci * moment. Therefore, Except for iterating the two lists within RCU 6762306a36Sopenharmony_ci * protection, it's no needed to hold RCU that we access node instance 6862306a36Sopenharmony_ci * in other places. 6962306a36Sopenharmony_ci * 7062306a36Sopenharmony_ci * In addition, all members in node structure including link instances 7162306a36Sopenharmony_ci * are protected by node spin lock. 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * 3: The transport level of the protocol. 7462306a36Sopenharmony_ci * This consists of the structures port, (and its user level 7562306a36Sopenharmony_ci * representations, such as user_port and tipc_sock), reference and 7662306a36Sopenharmony_ci * tipc_user (port.c, reg.c, socket.c). 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * This layer has four different locks: 7962306a36Sopenharmony_ci * - The tipc_port spin_lock. This is protecting each port instance 8062306a36Sopenharmony_ci * from parallel data access and removal. Since we can not place 8162306a36Sopenharmony_ci * this lock in the port itself, it has been placed in the 8262306a36Sopenharmony_ci * corresponding reference table entry, which has the same life 8362306a36Sopenharmony_ci * cycle as the module. This entry is difficult to access from 8462306a36Sopenharmony_ci * outside the TIPC core, however, so a pointer to the lock has 8562306a36Sopenharmony_ci * been added in the port instance, -to be used for unlocking 8662306a36Sopenharmony_ci * only. 8762306a36Sopenharmony_ci * - A read/write lock to protect the reference table itself (teg.c). 8862306a36Sopenharmony_ci * (Nobody is using read-only access to this, so it can just as 8962306a36Sopenharmony_ci * well be changed to a spin_lock) 9062306a36Sopenharmony_ci * - A spin lock to protect the registry of kernel/driver users (reg.c) 9162306a36Sopenharmony_ci * - A global spin_lock (tipc_port_lock), which only task is to ensure 9262306a36Sopenharmony_ci * consistency where more than one port is involved in an operation, 9362306a36Sopenharmony_ci * i.e., when a port is part of a linked list of ports. 9462306a36Sopenharmony_ci * There are two such lists; 'port_list', which is used for management, 9562306a36Sopenharmony_ci * and 'wait_list', which is used to queue ports during congestion. 9662306a36Sopenharmony_ci * 9762306a36Sopenharmony_ci * 4: The name table (name_table.c, name_distr.c, subscription.c) 9862306a36Sopenharmony_ci * - There is one big read/write-lock (tipc_nametbl_lock) protecting the 9962306a36Sopenharmony_ci * overall name table structure. Nothing must be added/removed to 10062306a36Sopenharmony_ci * this structure without holding write access to it. 10162306a36Sopenharmony_ci * - There is one local spin_lock per sub_sequence, which can be seen 10262306a36Sopenharmony_ci * as a sub-domain to the tipc_nametbl_lock domain. It is used only 10362306a36Sopenharmony_ci * for translation operations, and is needed because a translation 10462306a36Sopenharmony_ci * steps the root of the 'publication' linked list between each lookup. 10562306a36Sopenharmony_ci * This is always used within the scope of a tipc_nametbl_lock(read). 10662306a36Sopenharmony_ci * - A local spin_lock protecting the queue of subscriber events. 10762306a36Sopenharmony_ci*/ 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_cistatic void tipc_net_finalize(struct net *net, u32 addr); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciint tipc_net_init(struct net *net, u8 *node_id, u32 addr) 11262306a36Sopenharmony_ci{ 11362306a36Sopenharmony_ci if (tipc_own_id(net)) { 11462306a36Sopenharmony_ci pr_info("Cannot configure node identity twice\n"); 11562306a36Sopenharmony_ci return -1; 11662306a36Sopenharmony_ci } 11762306a36Sopenharmony_ci pr_info("Started in network mode\n"); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci if (node_id) 12062306a36Sopenharmony_ci tipc_set_node_id(net, node_id); 12162306a36Sopenharmony_ci if (addr) 12262306a36Sopenharmony_ci tipc_net_finalize(net, addr); 12362306a36Sopenharmony_ci return 0; 12462306a36Sopenharmony_ci} 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic void tipc_net_finalize(struct net *net, u32 addr) 12762306a36Sopenharmony_ci{ 12862306a36Sopenharmony_ci struct tipc_net *tn = tipc_net(net); 12962306a36Sopenharmony_ci struct tipc_socket_addr sk = {0, addr}; 13062306a36Sopenharmony_ci struct tipc_uaddr ua; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE, 13362306a36Sopenharmony_ci TIPC_NODE_STATE, addr, addr); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci if (cmpxchg(&tn->node_addr, 0, addr)) 13662306a36Sopenharmony_ci return; 13762306a36Sopenharmony_ci tipc_set_node_addr(net, addr); 13862306a36Sopenharmony_ci tipc_named_reinit(net); 13962306a36Sopenharmony_ci tipc_sk_reinit(net); 14062306a36Sopenharmony_ci tipc_mon_reinit_self(net); 14162306a36Sopenharmony_ci tipc_nametbl_publish(net, &ua, &sk, addr); 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_civoid tipc_net_finalize_work(struct work_struct *work) 14562306a36Sopenharmony_ci{ 14662306a36Sopenharmony_ci struct tipc_net *tn = container_of(work, struct tipc_net, work); 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci tipc_net_finalize(tipc_link_net(tn->bcl), tn->trial_addr); 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_civoid tipc_net_stop(struct net *net) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci if (!tipc_own_id(net)) 15462306a36Sopenharmony_ci return; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci rtnl_lock(); 15762306a36Sopenharmony_ci tipc_bearer_stop(net); 15862306a36Sopenharmony_ci tipc_node_stop(net); 15962306a36Sopenharmony_ci rtnl_unlock(); 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci pr_info("Left network mode\n"); 16262306a36Sopenharmony_ci} 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_cistatic int __tipc_nl_add_net(struct net *net, struct tipc_nl_msg *msg) 16562306a36Sopenharmony_ci{ 16662306a36Sopenharmony_ci struct tipc_net *tn = net_generic(net, tipc_net_id); 16762306a36Sopenharmony_ci u64 *w0 = (u64 *)&tn->node_id[0]; 16862306a36Sopenharmony_ci u64 *w1 = (u64 *)&tn->node_id[8]; 16962306a36Sopenharmony_ci struct nlattr *attrs; 17062306a36Sopenharmony_ci void *hdr; 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, 17362306a36Sopenharmony_ci NLM_F_MULTI, TIPC_NL_NET_GET); 17462306a36Sopenharmony_ci if (!hdr) 17562306a36Sopenharmony_ci return -EMSGSIZE; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NET); 17862306a36Sopenharmony_ci if (!attrs) 17962306a36Sopenharmony_ci goto msg_full; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci if (nla_put_u32(msg->skb, TIPC_NLA_NET_ID, tn->net_id)) 18262306a36Sopenharmony_ci goto attr_msg_full; 18362306a36Sopenharmony_ci if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID, *w0, 0)) 18462306a36Sopenharmony_ci goto attr_msg_full; 18562306a36Sopenharmony_ci if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID_W1, *w1, 0)) 18662306a36Sopenharmony_ci goto attr_msg_full; 18762306a36Sopenharmony_ci nla_nest_end(msg->skb, attrs); 18862306a36Sopenharmony_ci genlmsg_end(msg->skb, hdr); 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci return 0; 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciattr_msg_full: 19362306a36Sopenharmony_ci nla_nest_cancel(msg->skb, attrs); 19462306a36Sopenharmony_cimsg_full: 19562306a36Sopenharmony_ci genlmsg_cancel(msg->skb, hdr); 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci return -EMSGSIZE; 19862306a36Sopenharmony_ci} 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ciint tipc_nl_net_dump(struct sk_buff *skb, struct netlink_callback *cb) 20162306a36Sopenharmony_ci{ 20262306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 20362306a36Sopenharmony_ci int err; 20462306a36Sopenharmony_ci int done = cb->args[0]; 20562306a36Sopenharmony_ci struct tipc_nl_msg msg; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci if (done) 20862306a36Sopenharmony_ci return 0; 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci msg.skb = skb; 21162306a36Sopenharmony_ci msg.portid = NETLINK_CB(cb->skb).portid; 21262306a36Sopenharmony_ci msg.seq = cb->nlh->nlmsg_seq; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci err = __tipc_nl_add_net(net, &msg); 21562306a36Sopenharmony_ci if (err) 21662306a36Sopenharmony_ci goto out; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci done = 1; 21962306a36Sopenharmony_ciout: 22062306a36Sopenharmony_ci cb->args[0] = done; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci return skb->len; 22362306a36Sopenharmony_ci} 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ciint __tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info) 22662306a36Sopenharmony_ci{ 22762306a36Sopenharmony_ci struct nlattr *attrs[TIPC_NLA_NET_MAX + 1]; 22862306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 22962306a36Sopenharmony_ci struct tipc_net *tn = tipc_net(net); 23062306a36Sopenharmony_ci int err; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci if (!info->attrs[TIPC_NLA_NET]) 23362306a36Sopenharmony_ci return -EINVAL; 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX, 23662306a36Sopenharmony_ci info->attrs[TIPC_NLA_NET], 23762306a36Sopenharmony_ci tipc_nl_net_policy, info->extack); 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci if (err) 24062306a36Sopenharmony_ci return err; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci /* Can't change net id once TIPC has joined a network */ 24362306a36Sopenharmony_ci if (tipc_own_addr(net)) 24462306a36Sopenharmony_ci return -EPERM; 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci if (attrs[TIPC_NLA_NET_ID]) { 24762306a36Sopenharmony_ci u32 val; 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci val = nla_get_u32(attrs[TIPC_NLA_NET_ID]); 25062306a36Sopenharmony_ci if (val < 1 || val > 9999) 25162306a36Sopenharmony_ci return -EINVAL; 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci tn->net_id = val; 25462306a36Sopenharmony_ci } 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci if (attrs[TIPC_NLA_NET_ADDR]) { 25762306a36Sopenharmony_ci u32 addr; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]); 26062306a36Sopenharmony_ci if (!addr) 26162306a36Sopenharmony_ci return -EINVAL; 26262306a36Sopenharmony_ci tn->legacy_addr_format = true; 26362306a36Sopenharmony_ci tipc_net_init(net, NULL, addr); 26462306a36Sopenharmony_ci } 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci if (attrs[TIPC_NLA_NET_NODEID]) { 26762306a36Sopenharmony_ci u8 node_id[NODE_ID_LEN]; 26862306a36Sopenharmony_ci u64 *w0 = (u64 *)&node_id[0]; 26962306a36Sopenharmony_ci u64 *w1 = (u64 *)&node_id[8]; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci if (!attrs[TIPC_NLA_NET_NODEID_W1]) 27262306a36Sopenharmony_ci return -EINVAL; 27362306a36Sopenharmony_ci *w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]); 27462306a36Sopenharmony_ci *w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]); 27562306a36Sopenharmony_ci tipc_net_init(net, node_id, 0); 27662306a36Sopenharmony_ci } 27762306a36Sopenharmony_ci return 0; 27862306a36Sopenharmony_ci} 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ciint tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci int err; 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci rtnl_lock(); 28562306a36Sopenharmony_ci err = __tipc_nl_net_set(skb, info); 28662306a36Sopenharmony_ci rtnl_unlock(); 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci return err; 28962306a36Sopenharmony_ci} 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_cistatic int __tipc_nl_addr_legacy_get(struct net *net, struct tipc_nl_msg *msg) 29262306a36Sopenharmony_ci{ 29362306a36Sopenharmony_ci struct tipc_net *tn = tipc_net(net); 29462306a36Sopenharmony_ci struct nlattr *attrs; 29562306a36Sopenharmony_ci void *hdr; 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, 29862306a36Sopenharmony_ci 0, TIPC_NL_ADDR_LEGACY_GET); 29962306a36Sopenharmony_ci if (!hdr) 30062306a36Sopenharmony_ci return -EMSGSIZE; 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci attrs = nla_nest_start(msg->skb, TIPC_NLA_NET); 30362306a36Sopenharmony_ci if (!attrs) 30462306a36Sopenharmony_ci goto msg_full; 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci if (tn->legacy_addr_format) 30762306a36Sopenharmony_ci if (nla_put_flag(msg->skb, TIPC_NLA_NET_ADDR_LEGACY)) 30862306a36Sopenharmony_ci goto attr_msg_full; 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci nla_nest_end(msg->skb, attrs); 31162306a36Sopenharmony_ci genlmsg_end(msg->skb, hdr); 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci return 0; 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ciattr_msg_full: 31662306a36Sopenharmony_ci nla_nest_cancel(msg->skb, attrs); 31762306a36Sopenharmony_cimsg_full: 31862306a36Sopenharmony_ci genlmsg_cancel(msg->skb, hdr); 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci return -EMSGSIZE; 32162306a36Sopenharmony_ci} 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ciint tipc_nl_net_addr_legacy_get(struct sk_buff *skb, struct genl_info *info) 32462306a36Sopenharmony_ci{ 32562306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 32662306a36Sopenharmony_ci struct tipc_nl_msg msg; 32762306a36Sopenharmony_ci struct sk_buff *rep; 32862306a36Sopenharmony_ci int err; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 33162306a36Sopenharmony_ci if (!rep) 33262306a36Sopenharmony_ci return -ENOMEM; 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci msg.skb = rep; 33562306a36Sopenharmony_ci msg.portid = info->snd_portid; 33662306a36Sopenharmony_ci msg.seq = info->snd_seq; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci err = __tipc_nl_addr_legacy_get(net, &msg); 33962306a36Sopenharmony_ci if (err) { 34062306a36Sopenharmony_ci nlmsg_free(msg.skb); 34162306a36Sopenharmony_ci return err; 34262306a36Sopenharmony_ci } 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci return genlmsg_reply(msg.skb, info); 34562306a36Sopenharmony_ci} 346