1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden * net/tipc/net.c: TIPC network routing code
3b97bf3fdSPer Liden *
45a379074SJon Paul Maloy * Copyright (c) 1995-2006, 2014, Ericsson AB
59df3b7ebSAllan Stephens * Copyright (c) 2005, 2010-2011, Wind River Systems
6b97bf3fdSPer Liden * All rights reserved.
7b97bf3fdSPer Liden *
8b97bf3fdSPer Liden * Redistribution and use in source and binary forms, with or without
9b97bf3fdSPer Liden * modification, are permitted provided that the following conditions are met:
10b97bf3fdSPer Liden *
119ea1fd3cSPer Liden * 1. Redistributions of source code must retain the above copyright
129ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer.
139ea1fd3cSPer Liden * 2. Redistributions in binary form must reproduce the above copyright
149ea1fd3cSPer Liden * notice, this list of conditions and the following disclaimer in the
159ea1fd3cSPer Liden * documentation and/or other materials provided with the distribution.
169ea1fd3cSPer Liden * 3. Neither the names of the copyright holders nor the names of its
179ea1fd3cSPer Liden * contributors may be used to endorse or promote products derived from
189ea1fd3cSPer Liden * this software without specific prior written permission.
199ea1fd3cSPer Liden *
209ea1fd3cSPer Liden * Alternatively, this software may be distributed under the terms of the
219ea1fd3cSPer Liden * GNU General Public License ("GPL") version 2 as published by the Free
229ea1fd3cSPer Liden * Software Foundation.
23b97bf3fdSPer Liden *
24b97bf3fdSPer Liden * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25b97bf3fdSPer Liden * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b97bf3fdSPer Liden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b97bf3fdSPer Liden * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28b97bf3fdSPer Liden * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b97bf3fdSPer Liden * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b97bf3fdSPer Liden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b97bf3fdSPer Liden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b97bf3fdSPer Liden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b97bf3fdSPer Liden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b97bf3fdSPer Liden * POSSIBILITY OF SUCH DAMAGE.
35b97bf3fdSPer Liden */
36b97bf3fdSPer Liden
37b97bf3fdSPer Liden #include "core.h"
38b97bf3fdSPer Liden #include "net.h"
39b97bf3fdSPer Liden #include "name_distr.h"
40b97bf3fdSPer Liden #include "subscr.h"
419816f061SJon Paul Maloy #include "socket.h"
42672d99e1SAllan Stephens #include "node.h"
43a6bf70f7SJon Paul Maloy #include "bcast.h"
44*be07f056SXin Long #include "link.h"
4549cc66eaSRichard Alpe #include "netlink.h"
4646cb01eeSHoang Le #include "monitor.h"
47fd3cf2adSRichard Alpe
48b97bf3fdSPer Liden /*
49b97bf3fdSPer Liden * The TIPC locking policy is designed to ensure a very fine locking
50b97bf3fdSPer Liden * granularity, permitting complete parallel access to individual
517216cd94SYing Xue * port and node/link instances. The code consists of four major
52b97bf3fdSPer Liden * locking domains, each protected with their own disjunct set of locks.
53b97bf3fdSPer Liden *
547216cd94SYing Xue * 1: The bearer level.
557216cd94SYing Xue * RTNL lock is used to serialize the process of configuring bearer
567216cd94SYing Xue * on update side, and RCU lock is applied on read side to make
577216cd94SYing Xue * bearer instance valid on both paths of message transmission and
587216cd94SYing Xue * reception.
59b97bf3fdSPer Liden *
607216cd94SYing Xue * 2: The node and link level.
617216cd94SYing Xue * All node instances are saved into two tipc_node_list and node_htable
627216cd94SYing Xue * lists. The two lists are protected by node_list_lock on write side,
637216cd94SYing Xue * and they are guarded with RCU lock on read side. Especially node
647216cd94SYing Xue * instance is destroyed only when TIPC module is removed, and we can
657216cd94SYing Xue * confirm that there has no any user who is accessing the node at the
667216cd94SYing Xue * moment. Therefore, Except for iterating the two lists within RCU
677216cd94SYing Xue * protection, it's no needed to hold RCU that we access node instance
687216cd94SYing Xue * in other places.
69b97bf3fdSPer Liden *
707216cd94SYing Xue * In addition, all members in node structure including link instances
717216cd94SYing Xue * are protected by node spin lock.
72b97bf3fdSPer Liden *
737216cd94SYing Xue * 3: The transport level of the protocol.
74b97bf3fdSPer Liden * This consists of the structures port, (and its user level
75b97bf3fdSPer Liden * representations, such as user_port and tipc_sock), reference and
76b97bf3fdSPer Liden * tipc_user (port.c, reg.c, socket.c).
77b97bf3fdSPer Liden *
78b97bf3fdSPer Liden * This layer has four different locks:
79b97bf3fdSPer Liden * - The tipc_port spin_lock. This is protecting each port instance
80b97bf3fdSPer Liden * from parallel data access and removal. Since we can not place
81b97bf3fdSPer Liden * this lock in the port itself, it has been placed in the
82b97bf3fdSPer Liden * corresponding reference table entry, which has the same life
83b97bf3fdSPer Liden * cycle as the module. This entry is difficult to access from
84b97bf3fdSPer Liden * outside the TIPC core, however, so a pointer to the lock has
85b97bf3fdSPer Liden * been added in the port instance, -to be used for unlocking
86b97bf3fdSPer Liden * only.
87b97bf3fdSPer Liden * - A read/write lock to protect the reference table itself (teg.c).
88b97bf3fdSPer Liden * (Nobody is using read-only access to this, so it can just as
89b97bf3fdSPer Liden * well be changed to a spin_lock)
90b97bf3fdSPer Liden * - A spin lock to protect the registry of kernel/driver users (reg.c)
914323add6SPer Liden * - A global spin_lock (tipc_port_lock), which only task is to ensure
92b97bf3fdSPer Liden * consistency where more than one port is involved in an operation,
93a79ace4bSZheng Yongjun * i.e., when a port is part of a linked list of ports.
94b97bf3fdSPer Liden * There are two such lists; 'port_list', which is used for management,
95b97bf3fdSPer Liden * and 'wait_list', which is used to queue ports during congestion.
96b97bf3fdSPer Liden *
977216cd94SYing Xue * 4: The name table (name_table.c, name_distr.c, subscription.c)
984323add6SPer Liden * - There is one big read/write-lock (tipc_nametbl_lock) protecting the
99b97bf3fdSPer Liden * overall name table structure. Nothing must be added/removed to
100b97bf3fdSPer Liden * this structure without holding write access to it.
101b97bf3fdSPer Liden * - There is one local spin_lock per sub_sequence, which can be seen
1024323add6SPer Liden * as a sub-domain to the tipc_nametbl_lock domain. It is used only
103b97bf3fdSPer Liden * for translation operations, and is needed because a translation
104b97bf3fdSPer Liden * steps the root of the 'publication' linked list between each lookup.
1054323add6SPer Liden * This is always used within the scope of a tipc_nametbl_lock(read).
106b97bf3fdSPer Liden * - A local spin_lock protecting the queue of subscriber events.
107b97bf3fdSPer Liden */
108b97bf3fdSPer Liden
109adba75beSJon Maloy static void tipc_net_finalize(struct net *net, u32 addr);
110adba75beSJon Maloy
tipc_net_init(struct net * net,u8 * node_id,u32 addr)111d50ccc2dSJon Maloy int tipc_net_init(struct net *net, u8 *node_id, u32 addr)
112b97bf3fdSPer Liden {
113d50ccc2dSJon Maloy if (tipc_own_id(net)) {
114d50ccc2dSJon Maloy pr_info("Cannot configure node identity twice\n");
115d50ccc2dSJon Maloy return -1;
116d50ccc2dSJon Maloy }
117d50ccc2dSJon Maloy pr_info("Started in network mode\n");
118b97bf3fdSPer Liden
11925b0b9c4SJon Maloy if (node_id)
120d50ccc2dSJon Maloy tipc_set_node_id(net, node_id);
121d50ccc2dSJon Maloy if (addr)
122d50ccc2dSJon Maloy tipc_net_finalize(net, addr);
123d50ccc2dSJon Maloy return 0;
124d50ccc2dSJon Maloy }
12540f9f439SHerbert Xu
tipc_net_finalize(struct net * net,u32 addr)126adba75beSJon Maloy static void tipc_net_finalize(struct net *net, u32 addr)
127d50ccc2dSJon Maloy {
1289faa89d4SJon Maloy struct tipc_net *tn = tipc_net(net);
12950a3499aSJon Maloy struct tipc_socket_addr sk = {0, addr};
13050a3499aSJon Maloy struct tipc_uaddr ua;
13150a3499aSJon Maloy
13250a3499aSJon Maloy tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_CLUSTER_SCOPE,
13350a3499aSJon Maloy TIPC_NODE_STATE, addr, addr);
1349faa89d4SJon Maloy
135adba75beSJon Maloy if (cmpxchg(&tn->node_addr, 0, addr))
136adba75beSJon Maloy return;
137d50ccc2dSJon Maloy tipc_set_node_addr(net, addr);
1384ac1c8d0SYing Xue tipc_named_reinit(net);
139e05b31f4SYing Xue tipc_sk_reinit(net);
14046cb01eeSHoang Le tipc_mon_reinit_self(net);
14150a3499aSJon Maloy tipc_nametbl_publish(net, &ua, &sk, addr);
142b97bf3fdSPer Liden }
143adba75beSJon Maloy
tipc_net_finalize_work(struct work_struct * work)144d966ddccSHoang Huu Le void tipc_net_finalize_work(struct work_struct *work)
145adba75beSJon Maloy {
146*be07f056SXin Long struct tipc_net *tn = container_of(work, struct tipc_net, work);
147adba75beSJon Maloy
148*be07f056SXin Long tipc_net_finalize(tipc_link_net(tn->bcl), tn->trial_addr);
1499faa89d4SJon Maloy }
150b97bf3fdSPer Liden
tipc_net_stop(struct net * net)151f2f9800dSYing Xue void tipc_net_stop(struct net *net)
152b97bf3fdSPer Liden {
1539926cb5fSXin Long if (!tipc_own_id(net))
154b97bf3fdSPer Liden return;
15546651c59SYing Xue
156f97e455aSYing Xue rtnl_lock();
157f2f9800dSYing Xue tipc_bearer_stop(net);
158f2f9800dSYing Xue tipc_node_stop(net);
159f97e455aSYing Xue rtnl_unlock();
16046651c59SYing Xue
1612cf8aa19SErik Hugne pr_info("Left network mode\n");
162b97bf3fdSPer Liden }
163fd3cf2adSRichard Alpe
__tipc_nl_add_net(struct net * net,struct tipc_nl_msg * msg)164c93d3baaSYing Xue static int __tipc_nl_add_net(struct net *net, struct tipc_nl_msg *msg)
165fd3cf2adSRichard Alpe {
166c93d3baaSYing Xue struct tipc_net *tn = net_generic(net, tipc_net_id);
167d50ccc2dSJon Maloy u64 *w0 = (u64 *)&tn->node_id[0];
168d50ccc2dSJon Maloy u64 *w1 = (u64 *)&tn->node_id[8];
169fd3cf2adSRichard Alpe struct nlattr *attrs;
170d50ccc2dSJon Maloy void *hdr;
171fd3cf2adSRichard Alpe
172bfb3e5ddSRichard Alpe hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
173fd3cf2adSRichard Alpe NLM_F_MULTI, TIPC_NL_NET_GET);
174fd3cf2adSRichard Alpe if (!hdr)
175fd3cf2adSRichard Alpe return -EMSGSIZE;
176fd3cf2adSRichard Alpe
177ae0be8deSMichal Kubecek attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NET);
178fd3cf2adSRichard Alpe if (!attrs)
179fd3cf2adSRichard Alpe goto msg_full;
180fd3cf2adSRichard Alpe
181c93d3baaSYing Xue if (nla_put_u32(msg->skb, TIPC_NLA_NET_ID, tn->net_id))
182fd3cf2adSRichard Alpe goto attr_msg_full;
183d50ccc2dSJon Maloy if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID, *w0, 0))
184d50ccc2dSJon Maloy goto attr_msg_full;
185d50ccc2dSJon Maloy if (nla_put_u64_64bit(msg->skb, TIPC_NLA_NET_NODEID_W1, *w1, 0))
186d50ccc2dSJon Maloy goto attr_msg_full;
187fd3cf2adSRichard Alpe nla_nest_end(msg->skb, attrs);
188fd3cf2adSRichard Alpe genlmsg_end(msg->skb, hdr);
189fd3cf2adSRichard Alpe
190fd3cf2adSRichard Alpe return 0;
191fd3cf2adSRichard Alpe
192fd3cf2adSRichard Alpe attr_msg_full:
193fd3cf2adSRichard Alpe nla_nest_cancel(msg->skb, attrs);
194fd3cf2adSRichard Alpe msg_full:
195fd3cf2adSRichard Alpe genlmsg_cancel(msg->skb, hdr);
196fd3cf2adSRichard Alpe
197fd3cf2adSRichard Alpe return -EMSGSIZE;
198fd3cf2adSRichard Alpe }
199fd3cf2adSRichard Alpe
tipc_nl_net_dump(struct sk_buff * skb,struct netlink_callback * cb)200fd3cf2adSRichard Alpe int tipc_nl_net_dump(struct sk_buff *skb, struct netlink_callback *cb)
201fd3cf2adSRichard Alpe {
202c93d3baaSYing Xue struct net *net = sock_net(skb->sk);
203fd3cf2adSRichard Alpe int err;
204fd3cf2adSRichard Alpe int done = cb->args[0];
205fd3cf2adSRichard Alpe struct tipc_nl_msg msg;
206fd3cf2adSRichard Alpe
207fd3cf2adSRichard Alpe if (done)
208fd3cf2adSRichard Alpe return 0;
209fd3cf2adSRichard Alpe
210fd3cf2adSRichard Alpe msg.skb = skb;
211fd3cf2adSRichard Alpe msg.portid = NETLINK_CB(cb->skb).portid;
212fd3cf2adSRichard Alpe msg.seq = cb->nlh->nlmsg_seq;
213fd3cf2adSRichard Alpe
214c93d3baaSYing Xue err = __tipc_nl_add_net(net, &msg);
215fd3cf2adSRichard Alpe if (err)
216fd3cf2adSRichard Alpe goto out;
217fd3cf2adSRichard Alpe
218fd3cf2adSRichard Alpe done = 1;
219fd3cf2adSRichard Alpe out:
220fd3cf2adSRichard Alpe cb->args[0] = done;
221fd3cf2adSRichard Alpe
222fd3cf2adSRichard Alpe return skb->len;
223fd3cf2adSRichard Alpe }
22427c21416SRichard Alpe
__tipc_nl_net_set(struct sk_buff * skb,struct genl_info * info)2255631f65dSYing Xue int __tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
22627c21416SRichard Alpe {
22727c21416SRichard Alpe struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
22823fd3eacSJon Maloy struct net *net = sock_net(skb->sk);
22923fd3eacSJon Maloy struct tipc_net *tn = tipc_net(net);
230c93d3baaSYing Xue int err;
23127c21416SRichard Alpe
23227c21416SRichard Alpe if (!info->attrs[TIPC_NLA_NET])
23327c21416SRichard Alpe return -EINVAL;
23427c21416SRichard Alpe
2358cb08174SJohannes Berg err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
2368cb08174SJohannes Berg info->attrs[TIPC_NLA_NET],
2378cb08174SJohannes Berg tipc_nl_net_policy, info->extack);
238d50ccc2dSJon Maloy
23927c21416SRichard Alpe if (err)
24027c21416SRichard Alpe return err;
24127c21416SRichard Alpe
24223fd3eacSJon Maloy /* Can't change net id once TIPC has joined a network */
24323fd3eacSJon Maloy if (tipc_own_addr(net))
24423fd3eacSJon Maloy return -EPERM;
24523fd3eacSJon Maloy
24627c21416SRichard Alpe if (attrs[TIPC_NLA_NET_ID]) {
24727c21416SRichard Alpe u32 val;
24827c21416SRichard Alpe
24927c21416SRichard Alpe val = nla_get_u32(attrs[TIPC_NLA_NET_ID]);
25027c21416SRichard Alpe if (val < 1 || val > 9999)
25127c21416SRichard Alpe return -EINVAL;
25227c21416SRichard Alpe
253c93d3baaSYing Xue tn->net_id = val;
25427c21416SRichard Alpe }
25527c21416SRichard Alpe
25627c21416SRichard Alpe if (attrs[TIPC_NLA_NET_ADDR]) {
25727c21416SRichard Alpe u32 addr;
25827c21416SRichard Alpe
25927c21416SRichard Alpe addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
26020263641SJon Maloy if (!addr)
26127c21416SRichard Alpe return -EINVAL;
262b89afb11SJon Maloy tn->legacy_addr_format = true;
263d50ccc2dSJon Maloy tipc_net_init(net, NULL, addr);
26427c21416SRichard Alpe }
26527c21416SRichard Alpe
266d50ccc2dSJon Maloy if (attrs[TIPC_NLA_NET_NODEID]) {
267d50ccc2dSJon Maloy u8 node_id[NODE_ID_LEN];
268d50ccc2dSJon Maloy u64 *w0 = (u64 *)&node_id[0];
269d50ccc2dSJon Maloy u64 *w1 = (u64 *)&node_id[8];
270d50ccc2dSJon Maloy
271c6404122SEric Dumazet if (!attrs[TIPC_NLA_NET_NODEID_W1])
272c6404122SEric Dumazet return -EINVAL;
273d50ccc2dSJon Maloy *w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
274d50ccc2dSJon Maloy *w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
275d50ccc2dSJon Maloy tipc_net_init(net, node_id, 0);
276d50ccc2dSJon Maloy }
27727c21416SRichard Alpe return 0;
27827c21416SRichard Alpe }
2795631f65dSYing Xue
tipc_nl_net_set(struct sk_buff * skb,struct genl_info * info)2805631f65dSYing Xue int tipc_nl_net_set(struct sk_buff *skb, struct genl_info *info)
2815631f65dSYing Xue {
2825631f65dSYing Xue int err;
2835631f65dSYing Xue
2845631f65dSYing Xue rtnl_lock();
2855631f65dSYing Xue err = __tipc_nl_net_set(skb, info);
2865631f65dSYing Xue rtnl_unlock();
2875631f65dSYing Xue
2885631f65dSYing Xue return err;
2895631f65dSYing Xue }
290e1b5e598SJohn Rutherford
__tipc_nl_addr_legacy_get(struct net * net,struct tipc_nl_msg * msg)291e1b5e598SJohn Rutherford static int __tipc_nl_addr_legacy_get(struct net *net, struct tipc_nl_msg *msg)
292e1b5e598SJohn Rutherford {
293e1b5e598SJohn Rutherford struct tipc_net *tn = tipc_net(net);
294e1b5e598SJohn Rutherford struct nlattr *attrs;
295e1b5e598SJohn Rutherford void *hdr;
296e1b5e598SJohn Rutherford
297e1b5e598SJohn Rutherford hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
298e1b5e598SJohn Rutherford 0, TIPC_NL_ADDR_LEGACY_GET);
299e1b5e598SJohn Rutherford if (!hdr)
300e1b5e598SJohn Rutherford return -EMSGSIZE;
301e1b5e598SJohn Rutherford
302e1b5e598SJohn Rutherford attrs = nla_nest_start(msg->skb, TIPC_NLA_NET);
303e1b5e598SJohn Rutherford if (!attrs)
304e1b5e598SJohn Rutherford goto msg_full;
305e1b5e598SJohn Rutherford
306e1b5e598SJohn Rutherford if (tn->legacy_addr_format)
307e1b5e598SJohn Rutherford if (nla_put_flag(msg->skb, TIPC_NLA_NET_ADDR_LEGACY))
308e1b5e598SJohn Rutherford goto attr_msg_full;
309e1b5e598SJohn Rutherford
310e1b5e598SJohn Rutherford nla_nest_end(msg->skb, attrs);
311e1b5e598SJohn Rutherford genlmsg_end(msg->skb, hdr);
312e1b5e598SJohn Rutherford
313e1b5e598SJohn Rutherford return 0;
314e1b5e598SJohn Rutherford
315e1b5e598SJohn Rutherford attr_msg_full:
316e1b5e598SJohn Rutherford nla_nest_cancel(msg->skb, attrs);
317e1b5e598SJohn Rutherford msg_full:
318e1b5e598SJohn Rutherford genlmsg_cancel(msg->skb, hdr);
319e1b5e598SJohn Rutherford
320e1b5e598SJohn Rutherford return -EMSGSIZE;
321e1b5e598SJohn Rutherford }
322e1b5e598SJohn Rutherford
tipc_nl_net_addr_legacy_get(struct sk_buff * skb,struct genl_info * info)323e1b5e598SJohn Rutherford int tipc_nl_net_addr_legacy_get(struct sk_buff *skb, struct genl_info *info)
324e1b5e598SJohn Rutherford {
325e1b5e598SJohn Rutherford struct net *net = sock_net(skb->sk);
326e1b5e598SJohn Rutherford struct tipc_nl_msg msg;
327e1b5e598SJohn Rutherford struct sk_buff *rep;
328e1b5e598SJohn Rutherford int err;
329e1b5e598SJohn Rutherford
330e1b5e598SJohn Rutherford rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
331e1b5e598SJohn Rutherford if (!rep)
332e1b5e598SJohn Rutherford return -ENOMEM;
333e1b5e598SJohn Rutherford
334e1b5e598SJohn Rutherford msg.skb = rep;
335e1b5e598SJohn Rutherford msg.portid = info->snd_portid;
336e1b5e598SJohn Rutherford msg.seq = info->snd_seq;
337e1b5e598SJohn Rutherford
338e1b5e598SJohn Rutherford err = __tipc_nl_addr_legacy_get(net, &msg);
339e1b5e598SJohn Rutherford if (err) {
340e1b5e598SJohn Rutherford nlmsg_free(msg.skb);
341e1b5e598SJohn Rutherford return err;
342e1b5e598SJohn Rutherford }
343e1b5e598SJohn Rutherford
344e1b5e598SJohn Rutherford return genlmsg_reply(msg.skb, info);
345e1b5e598SJohn Rutherford }
346