1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * net/tipc/net.c: TIPC network routing code
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 1995-2006, 2014, Ericsson AB
5*4882a593Smuzhiyun * Copyright (c) 2005, 2010-2011, Wind River Systems
6*4882a593Smuzhiyun * All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun * modification, are permitted provided that the following conditions are met:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
12*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
13*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
14*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
15*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
16*4882a593Smuzhiyun * 3. Neither the names of the copyright holders nor the names of its
17*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived from
18*4882a593Smuzhiyun * this software without specific prior written permission.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Alternatively, this software may be distributed under the terms of the
21*4882a593Smuzhiyun * GNU General Public License ("GPL") version 2 as published by the Free
22*4882a593Smuzhiyun * Software Foundation.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25*4882a593Smuzhiyun * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*4882a593Smuzhiyun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*4882a593Smuzhiyun * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28*4882a593Smuzhiyun * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30*4882a593Smuzhiyun * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31*4882a593Smuzhiyun * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32*4882a593Smuzhiyun * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33*4882a593Smuzhiyun * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*4882a593Smuzhiyun * POSSIBILITY OF SUCH DAMAGE.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "core.h"
38*4882a593Smuzhiyun #include "net.h"
39*4882a593Smuzhiyun #include "name_distr.h"
40*4882a593Smuzhiyun #include "subscr.h"
41*4882a593Smuzhiyun #include "socket.h"
42*4882a593Smuzhiyun #include "node.h"
43*4882a593Smuzhiyun #include "bcast.h"
44*4882a593Smuzhiyun #include "link.h"
45*4882a593Smuzhiyun #include "netlink.h"
46*4882a593Smuzhiyun #include "monitor.h"
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * The TIPC locking policy is designed to ensure a very fine locking
50*4882a593Smuzhiyun * granularity, permitting complete parallel access to individual
51*4882a593Smuzhiyun * port and node/link instances. The code consists of four major
52*4882a593Smuzhiyun * locking domains, each protected with their own disjunct set of locks.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * 1: The bearer level.
55*4882a593Smuzhiyun * RTNL lock is used to serialize the process of configuring bearer
56*4882a593Smuzhiyun * on update side, and RCU lock is applied on read side to make
57*4882a593Smuzhiyun * bearer instance valid on both paths of message transmission and
58*4882a593Smuzhiyun * reception.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * 2: The node and link level.
61*4882a593Smuzhiyun * All node instances are saved into two tipc_node_list and node_htable
62*4882a593Smuzhiyun * lists. The two lists are protected by node_list_lock on write side,
63*4882a593Smuzhiyun * and they are guarded with RCU lock on read side. Especially node
64*4882a593Smuzhiyun * instance is destroyed only when TIPC module is removed, and we can
65*4882a593Smuzhiyun * confirm that there has no any user who is accessing the node at the
66*4882a593Smuzhiyun * moment. Therefore, Except for iterating the two lists within RCU
67*4882a593Smuzhiyun * protection, it's no needed to hold RCU that we access node instance
68*4882a593Smuzhiyun * in other places.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * In addition, all members in node structure including link instances
71*4882a593Smuzhiyun * are protected by node spin lock.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * 3: The transport level of the protocol.
74*4882a593Smuzhiyun * This consists of the structures port, (and its user level
75*4882a593Smuzhiyun * representations, such as user_port and tipc_sock), reference and
76*4882a593Smuzhiyun * tipc_user (port.c, reg.c, socket.c).
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * This layer has four different locks:
79*4882a593Smuzhiyun * - The tipc_port spin_lock. This is protecting each port instance
80*4882a593Smuzhiyun * from parallel data access and removal. Since we can not place
81*4882a593Smuzhiyun * this lock in the port itself, it has been placed in the
82*4882a593Smuzhiyun * corresponding reference table entry, which has the same life
83*4882a593Smuzhiyun * cycle as the module. This entry is difficult to access from
84*4882a593Smuzhiyun * outside the TIPC core, however, so a pointer to the lock has
85*4882a593Smuzhiyun * been added in the port instance, -to be used for unlocking
86*4882a593Smuzhiyun * only.
87*4882a593Smuzhiyun * - A read/write lock to protect the reference table itself (teg.c).
88*4882a593Smuzhiyun * (Nobody is using read-only access to this, so it can just as
89*4882a593Smuzhiyun * well be changed to a spin_lock)
90*4882a593Smuzhiyun * - A spin lock to protect the registry of kernel/driver users (reg.c)
91*4882a593Smuzhiyun * - A global spin_lock (tipc_port_lock), which only task is to ensure
92*4882a593Smuzhiyun * consistency where more than one port is involved in an operation,
93*4882a593Smuzhiyun * i.e., whe a port is part of a linked list of ports.
94*4882a593Smuzhiyun * There are two such lists; 'port_list', which is used for management,
95*4882a593Smuzhiyun * and 'wait_list', which is used to queue ports during congestion.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * 4: The name table (name_table.c, name_distr.c, subscription.c)
98*4882a593Smuzhiyun * - There is one big read/write-lock (tipc_nametbl_lock) protecting the
99*4882a593Smuzhiyun * overall name table structure. Nothing must be added/removed to
100*4882a593Smuzhiyun * this structure without holding write access to it.
101*4882a593Smuzhiyun * - There is one local spin_lock per sub_sequence, which can be seen
102*4882a593Smuzhiyun * as a sub-domain to the tipc_nametbl_lock domain. It is used only
103*4882a593Smuzhiyun * for translation operations, and is needed because a translation
104*4882a593Smuzhiyun * steps the root of the 'publication' linked list between each lookup.
105*4882a593Smuzhiyun * This is always used within the scope of a tipc_nametbl_lock(read).
106*4882a593Smuzhiyun * - A local spin_lock protecting the queue of subscriber events.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun static void tipc_net_finalize(struct net *net, u32 addr);
110*4882a593Smuzhiyun
tipc_net_init(struct net * net,u8 * node_id,u32 addr)111*4882a593Smuzhiyun int tipc_net_init(struct net *net, u8 *node_id, u32 addr)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun if (tipc_own_id(net)) {
114*4882a593Smuzhiyun pr_info("Cannot configure node identity twice\n");
115*4882a593Smuzhiyun return -1;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun pr_info("Started in network mode\n");
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (node_id)
120*4882a593Smuzhiyun tipc_set_node_id(net, node_id);
121*4882a593Smuzhiyun if (addr)
122*4882a593Smuzhiyun tipc_net_finalize(net, addr);
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
tipc_net_finalize(struct net * net,u32 addr)126*4882a593Smuzhiyun static void tipc_net_finalize(struct net *net, u32 addr)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct tipc_net *tn = tipc_net(net);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (cmpxchg(&tn->node_addr, 0, addr))
131*4882a593Smuzhiyun return;
132*4882a593Smuzhiyun tipc_set_node_addr(net, addr);
133*4882a593Smuzhiyun tipc_named_reinit(net);
134*4882a593Smuzhiyun tipc_sk_reinit(net);
135*4882a593Smuzhiyun tipc_mon_reinit_self(net);
136*4882a593Smuzhiyun tipc_nametbl_publish(net, TIPC_CFG_SRV, addr, addr,
137*4882a593Smuzhiyun TIPC_CLUSTER_SCOPE, 0, addr);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
tipc_net_finalize_work(struct work_struct * work)140*4882a593Smuzhiyun void tipc_net_finalize_work(struct work_struct *work)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun struct tipc_net *tn = container_of(work, struct tipc_net, work);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun tipc_net_finalize(tipc_link_net(tn->bcl), tn->trial_addr);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
tipc_net_stop(struct net * net)147*4882a593Smuzhiyun void tipc_net_stop(struct net *net)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun if (!tipc_own_id(net))
150*4882a593Smuzhiyun return;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun rtnl_lock();
153*4882a593Smuzhiyun tipc_bearer_stop(net);
154*4882a593Smuzhiyun tipc_node_stop(net);
155*4882a593Smuzhiyun rtnl_unlock();
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun pr_info("Left network mode\n");
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
__tipc_nl_add_net(struct net * net,struct tipc_nl_msg * msg)160*4882a593Smuzhiyun static int __tipc_nl_add_net(struct net *net, struct tipc_nl_msg *msg)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct tipc_net *tn = net_generic(net, tipc_net_id);
163*4882a593Smuzhiyun u64 *w0 = (u64 *)&tn->node_id[0];
164*4882a593Smuzhiyun u64 *w1 = (u64 *)&tn->node_id[8];
165*4882a593Smuzhiyun struct nlattr *attrs;
166*4882a593Smuzhiyun void *hdr;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
169*4882a593Smuzhiyun NLM_F_MULTI, TIPC_NL_NET_GET);
170*4882a593Smuzhiyun if (!hdr)
171*4882a593Smuzhiyun return -EMSGSIZE;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NET);
174*4882a593Smuzhiyun if (!attrs)
175*4882a593Smuzhiyun goto msg_full;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (nla_put_u32(msg->skb, TIPC_NLA_NET_ID, tn->net_id))
178*4882a593Smuzhiyun goto attr_msg_full;
179*4882a593Smuzhiyun if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID, *w0, 0))
180*4882a593Smuzhiyun goto attr_msg_full;
181*4882a593Smuzhiyun if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID_W1, *w1, 0))
182*4882a593Smuzhiyun goto attr_msg_full;
183*4882a593Smuzhiyun nla_nest_end(msg->skb, attrs);
184*4882a593Smuzhiyun genlmsg_end(msg->skb, hdr);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return 0;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun attr_msg_full:
189*4882a593Smuzhiyun nla_nest_cancel(msg->skb, attrs);
190*4882a593Smuzhiyun msg_full:
191*4882a593Smuzhiyun genlmsg_cancel(msg->skb, hdr);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun return -EMSGSIZE;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
tipc_nl_net_dump(struct sk_buff * skb,struct netlink_callback * cb)196*4882a593Smuzhiyun int tipc_nl_net_dump(struct sk_buff *skb, struct netlink_callback *cb)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct net *net = sock_net(skb->sk);
199*4882a593Smuzhiyun int err;
200*4882a593Smuzhiyun int done = cb->args[0];
201*4882a593Smuzhiyun struct tipc_nl_msg msg;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (done)
204*4882a593Smuzhiyun return 0;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun msg.skb = skb;
207*4882a593Smuzhiyun msg.portid = NETLINK_CB(cb->skb).portid;
208*4882a593Smuzhiyun msg.seq = cb->nlh->nlmsg_seq;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun err = __tipc_nl_add_net(net, &msg);
211*4882a593Smuzhiyun if (err)
212*4882a593Smuzhiyun goto out;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun done = 1;
215*4882a593Smuzhiyun out:
216*4882a593Smuzhiyun cb->args[0] = done;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return skb->len;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
__tipc_nl_net_set(struct sk_buff * skb,struct genl_info * info)221*4882a593Smuzhiyun int __tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
224*4882a593Smuzhiyun struct net *net = sock_net(skb->sk);
225*4882a593Smuzhiyun struct tipc_net *tn = tipc_net(net);
226*4882a593Smuzhiyun int err;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun if (!info->attrs[TIPC_NLA_NET])
229*4882a593Smuzhiyun return -EINVAL;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
232*4882a593Smuzhiyun info->attrs[TIPC_NLA_NET],
233*4882a593Smuzhiyun tipc_nl_net_policy, info->extack);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (err)
236*4882a593Smuzhiyun return err;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* Can't change net id once TIPC has joined a network */
239*4882a593Smuzhiyun if (tipc_own_addr(net))
240*4882a593Smuzhiyun return -EPERM;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (attrs[TIPC_NLA_NET_ID]) {
243*4882a593Smuzhiyun u32 val;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun val = nla_get_u32(attrs[TIPC_NLA_NET_ID]);
246*4882a593Smuzhiyun if (val < 1 || val > 9999)
247*4882a593Smuzhiyun return -EINVAL;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun tn->net_id = val;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (attrs[TIPC_NLA_NET_ADDR]) {
253*4882a593Smuzhiyun u32 addr;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
256*4882a593Smuzhiyun if (!addr)
257*4882a593Smuzhiyun return -EINVAL;
258*4882a593Smuzhiyun tn->legacy_addr_format = true;
259*4882a593Smuzhiyun tipc_net_init(net, NULL, addr);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (attrs[TIPC_NLA_NET_NODEID]) {
263*4882a593Smuzhiyun u8 node_id[NODE_ID_LEN];
264*4882a593Smuzhiyun u64 *w0 = (u64 *)&node_id[0];
265*4882a593Smuzhiyun u64 *w1 = (u64 *)&node_id[8];
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun if (!attrs[TIPC_NLA_NET_NODEID_W1])
268*4882a593Smuzhiyun return -EINVAL;
269*4882a593Smuzhiyun *w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
270*4882a593Smuzhiyun *w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
271*4882a593Smuzhiyun tipc_net_init(net, node_id, 0);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
tipc_nl_net_set(struct sk_buff * skb,struct genl_info * info)276*4882a593Smuzhiyun int tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun int err;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun rtnl_lock();
281*4882a593Smuzhiyun err = __tipc_nl_net_set(skb, info);
282*4882a593Smuzhiyun rtnl_unlock();
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return err;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
__tipc_nl_addr_legacy_get(struct net * net,struct tipc_nl_msg * msg)287*4882a593Smuzhiyun static int __tipc_nl_addr_legacy_get(struct net *net, struct tipc_nl_msg *msg)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct tipc_net *tn = tipc_net(net);
290*4882a593Smuzhiyun struct nlattr *attrs;
291*4882a593Smuzhiyun void *hdr;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
294*4882a593Smuzhiyun 0, TIPC_NL_ADDR_LEGACY_GET);
295*4882a593Smuzhiyun if (!hdr)
296*4882a593Smuzhiyun return -EMSGSIZE;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun attrs = nla_nest_start(msg->skb, TIPC_NLA_NET);
299*4882a593Smuzhiyun if (!attrs)
300*4882a593Smuzhiyun goto msg_full;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (tn->legacy_addr_format)
303*4882a593Smuzhiyun if (nla_put_flag(msg->skb, TIPC_NLA_NET_ADDR_LEGACY))
304*4882a593Smuzhiyun goto attr_msg_full;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun nla_nest_end(msg->skb, attrs);
307*4882a593Smuzhiyun genlmsg_end(msg->skb, hdr);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun return 0;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun attr_msg_full:
312*4882a593Smuzhiyun nla_nest_cancel(msg->skb, attrs);
313*4882a593Smuzhiyun msg_full:
314*4882a593Smuzhiyun genlmsg_cancel(msg->skb, hdr);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun return -EMSGSIZE;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
tipc_nl_net_addr_legacy_get(struct sk_buff * skb,struct genl_info * info)319*4882a593Smuzhiyun int tipc_nl_net_addr_legacy_get(struct sk_buff *skb, struct genl_info *info)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun struct net *net = sock_net(skb->sk);
322*4882a593Smuzhiyun struct tipc_nl_msg msg;
323*4882a593Smuzhiyun struct sk_buff *rep;
324*4882a593Smuzhiyun int err;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
327*4882a593Smuzhiyun if (!rep)
328*4882a593Smuzhiyun return -ENOMEM;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun msg.skb = rep;
331*4882a593Smuzhiyun msg.portid = info->snd_portid;
332*4882a593Smuzhiyun msg.seq = info->snd_seq;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun err = __tipc_nl_addr_legacy_get(net, &msg);
335*4882a593Smuzhiyun if (err) {
336*4882a593Smuzhiyun nlmsg_free(msg.skb);
337*4882a593Smuzhiyun return err;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return genlmsg_reply(msg.skb, info);
341*4882a593Smuzhiyun }
342