Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * net/tipc/net.c: TIPC network routing code
0003  *
0004  * Copyright (c) 1995-2006, 2014, Ericsson AB
0005  * Copyright (c) 2005, 2010-2011, Wind River Systems
0006  * All rights reserved.
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions are met:
0010  *
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  * 3. Neither the names of the copyright holders nor the names of its
0017  *    contributors may be used to endorse or promote products derived from
0018  *    this software without specific prior written permission.
0019  *
0020  * Alternatively, this software may be distributed under the terms of the
0021  * GNU General Public License ("GPL") version 2 as published by the Free
0022  * Software Foundation.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #include "core.h"
0038 #include "net.h"
0039 #include "name_distr.h"
0040 #include "subscr.h"
0041 #include "socket.h"
0042 #include "node.h"
0043 #include "bcast.h"
0044 #include "link.h"
0045 #include "netlink.h"
0046 #include "monitor.h"
0047 
0048 /*
0049  * The TIPC locking policy is designed to ensure a very fine locking
0050  * granularity, permitting complete parallel access to individual
0051  * port and node/link instances. The code consists of four major
0052  * locking domains, each protected with their own disjunct set of locks.
0053  *
0054  * 1: The bearer level.
0055  *    RTNL lock is used to serialize the process of configuring bearer
0056  *    on update side, and RCU lock is applied on read side to make
0057  *    bearer instance valid on both paths of message transmission and
0058  *    reception.
0059  *
0060  * 2: The node and link level.
0061  *    All node instances are saved into two tipc_node_list and node_htable
0062  *    lists. The two lists are protected by node_list_lock on write side,
0063  *    and they are guarded with RCU lock on read side. Especially node
0064  *    instance is destroyed only when TIPC module is removed, and we can
0065  *    confirm that there has no any user who is accessing the node at the
0066  *    moment. Therefore, Except for iterating the two lists within RCU
0067  *    protection, it's no needed to hold RCU that we access node instance
0068  *    in other places.
0069  *
0070  *    In addition, all members in node structure including link instances
0071  *    are protected by node spin lock.
0072  *
0073  * 3: The transport level of the protocol.
0074  *    This consists of the structures port, (and its user level
0075  *    representations, such as user_port and tipc_sock), reference and
0076  *    tipc_user (port.c, reg.c, socket.c).
0077  *
0078  *    This layer has four different locks:
0079  *     - The tipc_port spin_lock. This is protecting each port instance
0080  *       from parallel data access and removal. Since we can not place
0081  *       this lock in the port itself, it has been placed in the
0082  *       corresponding reference table entry, which has the same life
0083  *       cycle as the module. This entry is difficult to access from
0084  *       outside the TIPC core, however, so a pointer to the lock has
0085  *       been added in the port instance, -to be used for unlocking
0086  *       only.
0087  *     - A read/write lock to protect the reference table itself (teg.c).
0088  *       (Nobody is using read-only access to this, so it can just as
0089  *       well be changed to a spin_lock)
0090  *     - A spin lock to protect the registry of kernel/driver users (reg.c)
0091  *     - A global spin_lock (tipc_port_lock), which only task is to ensure
0092  *       consistency where more than one port is involved in an operation,
0093  *       i.e., when a port is part of a linked list of ports.
0094  *       There are two such lists; 'port_list', which is used for management,
0095  *       and 'wait_list', which is used to queue ports during congestion.
0096  *
0097  *  4: The name table (name_table.c, name_distr.c, subscription.c)
0098  *     - There is one big read/write-lock (tipc_nametbl_lock) protecting the
0099  *       overall name table structure. Nothing must be added/removed to
0100  *       this structure without holding write access to it.
0101  *     - There is one local spin_lock per sub_sequence, which can be seen
0102  *       as a sub-domain to the tipc_nametbl_lock domain. It is used only
0103  *       for translation operations, and is needed because a translation
0104  *       steps the root of the 'publication' linked list between each lookup.
0105  *       This is always used within the scope of a tipc_nametbl_lock(read).
0106  *     - A local spin_lock protecting the queue of subscriber events.
0107 */
0108 
0109 static void tipc_net_finalize(struct net *net, u32 addr);
0110 
0111 int tipc_net_init(struct net *net, u8 *node_id, u32 addr)
0112 {
0113     if (tipc_own_id(net)) {
0114         pr_info("Cannot configure node identity twice\n");
0115         return -1;
0116     }
0117     pr_info("Started in network mode\n");
0118 
0119     if (node_id)
0120         tipc_set_node_id(net, node_id);
0121     if (addr)
0122         tipc_net_finalize(net, addr);
0123     return 0;
0124 }
0125 
0126 static void tipc_net_finalize(struct net *net, u32 addr)
0127 {
0128     struct tipc_net *tn = tipc_net(net);
0129     struct tipc_socket_addr sk = {0, addr};
0130     struct tipc_uaddr ua;
0131 
0132     tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE,
0133            TIPC_NODE_STATE, addr, addr);
0134 
0135     if (cmpxchg(&tn->node_addr, 0, addr))
0136         return;
0137     tipc_set_node_addr(net, addr);
0138     tipc_named_reinit(net);
0139     tipc_sk_reinit(net);
0140     tipc_mon_reinit_self(net);
0141     tipc_nametbl_publish(net, &ua, &sk, addr);
0142 }
0143 
0144 void tipc_net_finalize_work(struct work_struct *work)
0145 {
0146     struct tipc_net *tn = container_of(work, struct tipc_net, work);
0147 
0148     tipc_net_finalize(tipc_link_net(tn->bcl), tn->trial_addr);
0149 }
0150 
0151 void tipc_net_stop(struct net *net)
0152 {
0153     if (!tipc_own_id(net))
0154         return;
0155 
0156     rtnl_lock();
0157     tipc_bearer_stop(net);
0158     tipc_node_stop(net);
0159     rtnl_unlock();
0160 
0161     pr_info("Left network mode\n");
0162 }
0163 
0164 static int __tipc_nl_add_net(struct net *net, struct tipc_nl_msg *msg)
0165 {
0166     struct tipc_net *tn = net_generic(net, tipc_net_id);
0167     u64 *w0 = (u64 *)&tn->node_id[0];
0168     u64 *w1 = (u64 *)&tn->node_id[8];
0169     struct nlattr *attrs;
0170     void *hdr;
0171 
0172     hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
0173               NLM_F_MULTI, TIPC_NL_NET_GET);
0174     if (!hdr)
0175         return -EMSGSIZE;
0176 
0177     attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NET);
0178     if (!attrs)
0179         goto msg_full;
0180 
0181     if (nla_put_u32(msg->skb, TIPC_NLA_NET_ID, tn->net_id))
0182         goto attr_msg_full;
0183     if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID, *w0, 0))
0184         goto attr_msg_full;
0185     if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID_W1, *w1, 0))
0186         goto attr_msg_full;
0187     nla_nest_end(msg->skb, attrs);
0188     genlmsg_end(msg->skb, hdr);
0189 
0190     return 0;
0191 
0192 attr_msg_full:
0193     nla_nest_cancel(msg->skb, attrs);
0194 msg_full:
0195     genlmsg_cancel(msg->skb, hdr);
0196 
0197     return -EMSGSIZE;
0198 }
0199 
0200 int tipc_nl_net_dump(struct sk_buff *skb, struct netlink_callback *cb)
0201 {
0202     struct net *net = sock_net(skb->sk);
0203     int err;
0204     int done = cb->args[0];
0205     struct tipc_nl_msg msg;
0206 
0207     if (done)
0208         return 0;
0209 
0210     msg.skb = skb;
0211     msg.portid = NETLINK_CB(cb->skb).portid;
0212     msg.seq = cb->nlh->nlmsg_seq;
0213 
0214     err = __tipc_nl_add_net(net, &msg);
0215     if (err)
0216         goto out;
0217 
0218     done = 1;
0219 out:
0220     cb->args[0] = done;
0221 
0222     return skb->len;
0223 }
0224 
0225 int __tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
0226 {
0227     struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
0228     struct net *net = sock_net(skb->sk);
0229     struct tipc_net *tn = tipc_net(net);
0230     int err;
0231 
0232     if (!info->attrs[TIPC_NLA_NET])
0233         return -EINVAL;
0234 
0235     err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
0236                       info->attrs[TIPC_NLA_NET],
0237                       tipc_nl_net_policy, info->extack);
0238 
0239     if (err)
0240         return err;
0241 
0242     /* Can't change net id once TIPC has joined a network */
0243     if (tipc_own_addr(net))
0244         return -EPERM;
0245 
0246     if (attrs[TIPC_NLA_NET_ID]) {
0247         u32 val;
0248 
0249         val = nla_get_u32(attrs[TIPC_NLA_NET_ID]);
0250         if (val < 1 || val > 9999)
0251             return -EINVAL;
0252 
0253         tn->net_id = val;
0254     }
0255 
0256     if (attrs[TIPC_NLA_NET_ADDR]) {
0257         u32 addr;
0258 
0259         addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
0260         if (!addr)
0261             return -EINVAL;
0262         tn->legacy_addr_format = true;
0263         tipc_net_init(net, NULL, addr);
0264     }
0265 
0266     if (attrs[TIPC_NLA_NET_NODEID]) {
0267         u8 node_id[NODE_ID_LEN];
0268         u64 *w0 = (u64 *)&node_id[0];
0269         u64 *w1 = (u64 *)&node_id[8];
0270 
0271         if (!attrs[TIPC_NLA_NET_NODEID_W1])
0272             return -EINVAL;
0273         *w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
0274         *w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
0275         tipc_net_init(net, node_id, 0);
0276     }
0277     return 0;
0278 }
0279 
0280 int tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
0281 {
0282     int err;
0283 
0284     rtnl_lock();
0285     err = __tipc_nl_net_set(skb, info);
0286     rtnl_unlock();
0287 
0288     return err;
0289 }
0290 
0291 static int __tipc_nl_addr_legacy_get(struct net *net, struct tipc_nl_msg *msg)
0292 {
0293     struct tipc_net *tn = tipc_net(net);
0294     struct nlattr *attrs;
0295     void *hdr;
0296 
0297     hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
0298               0, TIPC_NL_ADDR_LEGACY_GET);
0299     if (!hdr)
0300         return -EMSGSIZE;
0301 
0302     attrs = nla_nest_start(msg->skb, TIPC_NLA_NET);
0303     if (!attrs)
0304         goto msg_full;
0305 
0306     if (tn->legacy_addr_format)
0307         if (nla_put_flag(msg->skb, TIPC_NLA_NET_ADDR_LEGACY))
0308             goto attr_msg_full;
0309 
0310     nla_nest_end(msg->skb, attrs);
0311     genlmsg_end(msg->skb, hdr);
0312 
0313     return 0;
0314 
0315 attr_msg_full:
0316     nla_nest_cancel(msg->skb, attrs);
0317 msg_full:
0318     genlmsg_cancel(msg->skb, hdr);
0319 
0320     return -EMSGSIZE;
0321 }
0322 
0323 int tipc_nl_net_addr_legacy_get(struct sk_buff *skb, struct genl_info *info)
0324 {
0325     struct net *net = sock_net(skb->sk);
0326     struct tipc_nl_msg msg;
0327     struct sk_buff *rep;
0328     int err;
0329 
0330     rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
0331     if (!rep)
0332         return -ENOMEM;
0333 
0334     msg.skb = rep;
0335     msg.portid = info->snd_portid;
0336     msg.seq = info->snd_seq;
0337 
0338     err = __tipc_nl_addr_legacy_get(net, &msg);
0339     if (err) {
0340         nlmsg_free(msg.skb);
0341         return err;
0342     }
0343 
0344     return genlmsg_reply(msg.skb, info);
0345 }