Skip to content

Commit 97c53ca

Browse files
dlunevdavem330
authored andcommitted
[NET]: Make rtnetlink infrastructure network namespace aware (v3)
After this patch none of the netlink callback support anything except the initial network namespace but the rtnetlink infrastructure now handles multiple network namespaces. Changes from v2: - IPv6 addrlabel processing Changes from v1: - no need for special rtnl_unlock handling - fixed IPv6 ndisc Signed-off-by: Denis V. Lunev <den@openvz.org> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b854272 commit 97c53ca

21 files changed

Lines changed: 102 additions & 54 deletions

‎include/linux/rtnetlink.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,11 @@ extern int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
620620
({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
621621
__rtattr_parse_nested_compat(tb, max, rta, len); })
622622

623-
extern int rtnetlink_send(struct sk_buff *skb, u32 pid, u32 group, int echo);
624-
extern int rtnl_unicast(struct sk_buff *skb, u32 pid);
625-
extern int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
623+
extern int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, u32 group, int echo);
624+
extern int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid);
625+
extern int rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
626626
struct nlmsghdr *nlh, gfp_t flags);
627-
extern void rtnl_set_sk_err(u32 group, int error);
627+
extern void rtnl_set_sk_err(struct net *net, u32 group, int error);
628628
extern int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics);
629629
extern int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst,
630630
u32 id, u32 ts, u32 tsage, long expires,

‎include/net/net_namespace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
struct proc_dir_entry;
1212
struct net_device;
13+
struct sock;
1314
struct net {
1415
atomic_t count; /* To decided when the network
1516
* namespace should be freed.
@@ -29,6 +30,8 @@ struct net {
2930
struct list_head dev_base_head;
3031
struct hlist_head *dev_name_head;
3132
struct hlist_head *dev_index_head;
33+
34+
struct sock *rtnl; /* rtnetlink socket */
3235
};
3336

3437
#ifdef CONFIG_NET

‎net/bridge/br_netlink.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ void br_ifinfo_notify(int event, struct net_bridge_port *port)
9797
kfree_skb(skb);
9898
goto errout;
9999
}
100-
err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
100+
err = rtnl_notify(skb, &init_net,0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
101101
errout:
102102
if (err < 0)
103-
rtnl_set_sk_err(RTNLGRP_LINK, err);
103+
rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
104104
}
105105

106106
/*

‎net/core/fib_rules.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,10 @@ static void notify_rule_change(int event, struct fib_rule *rule,
599599
kfree_skb(skb);
600600
goto errout;
601601
}
602-
err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
602+
err = rtnl_notify(skb, &init_net, pid, ops->nlgroup, nlh, GFP_KERNEL);
603603
errout:
604604
if (err < 0)
605-
rtnl_set_sk_err(ops->nlgroup, err);
605+
rtnl_set_sk_err(&init_net, ops->nlgroup, err);
606606
}
607607

608608
static void attach_rules(struct list_head *rules, struct net_device *dev)

‎net/core/neighbour.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,10 +2467,10 @@ static void __neigh_notify(struct neighbour *n, int type, int flags)
24672467
kfree_skb(skb);
24682468
goto errout;
24692469
}
2470-
err = rtnl_notify(skb, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2470+
err = rtnl_notify(skb, &init_net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
24712471
errout:
24722472
if (err < 0)
2473-
rtnl_set_sk_err(RTNLGRP_NEIGH, err);
2473+
rtnl_set_sk_err(&init_net, RTNLGRP_NEIGH, err);
24742474
}
24752475

24762476
#ifdef CONFIG_ARPD

‎net/core/rtnetlink.c‎

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ struct rtnl_link
6060
};
6161

6262
static DEFINE_MUTEX(rtnl_mutex);
63-
static struct sock *rtnl;
6463

6564
void rtnl_lock(void)
6665
{
@@ -458,8 +457,9 @@ size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
458457
return ret;
459458
}
460459

461-
int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
460+
int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
462461
{
462+
struct sock *rtnl = net->rtnl;
463463
int err = 0;
464464

465465
NETLINK_CB(skb).dst_group = group;
@@ -471,14 +471,17 @@ int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
471471
return err;
472472
}
473473

474-
int rtnl_unicast(struct sk_buff *skb, u32 pid)
474+
int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
475475
{
476+
struct sock *rtnl = net->rtnl;
477+
476478
return nlmsg_unicast(rtnl, skb, pid);
477479
}
478480

479-
int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
481+
int rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
480482
struct nlmsghdr *nlh, gfp_t flags)
481483
{
484+
struct sock *rtnl = net->rtnl;
482485
int report = 0;
483486

484487
if (nlh)
@@ -487,8 +490,10 @@ int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
487490
return nlmsg_notify(rtnl, skb, pid, group, report, flags);
488491
}
489492

490-
void rtnl_set_sk_err(u32 group, int error)
493+
void rtnl_set_sk_err(struct net *net, u32 group, int error)
491494
{
495+
struct sock *rtnl = net->rtnl;
496+
492497
netlink_set_err(rtnl, 0, group, error);
493498
}
494499

@@ -1201,7 +1206,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
12011206
kfree_skb(nskb);
12021207
goto errout;
12031208
}
1204-
err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
1209+
err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
12051210
errout:
12061211
dev_put(dev);
12071212

@@ -1252,10 +1257,10 @@ void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
12521257
kfree_skb(skb);
12531258
goto errout;
12541259
}
1255-
err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1260+
err = rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
12561261
errout:
12571262
if (err < 0)
1258-
rtnl_set_sk_err(RTNLGRP_LINK, err);
1263+
rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
12591264
}
12601265

12611266
/* Protected by RTNL sempahore. */
@@ -1266,6 +1271,7 @@ static int rtattr_max;
12661271

12671272
static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
12681273
{
1274+
struct net *net = skb->sk->sk_net;
12691275
rtnl_doit_func doit;
12701276
int sz_idx, kind;
12711277
int min_len;
@@ -1294,13 +1300,15 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
12941300
return -EPERM;
12951301

12961302
if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1303+
struct sock *rtnl;
12971304
rtnl_dumpit_func dumpit;
12981305

12991306
dumpit = rtnl_get_dumpit(family, type);
13001307
if (dumpit == NULL)
13011308
return -EOPNOTSUPP;
13021309

13031310
__rtnl_unlock();
1311+
rtnl = net->rtnl;
13041312
err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
13051313
rtnl_lock();
13061314
return err;
@@ -1373,6 +1381,40 @@ static struct notifier_block rtnetlink_dev_notifier = {
13731381
.notifier_call = rtnetlink_event,
13741382
};
13751383

1384+
1385+
static int rtnetlink_net_init(struct net *net)
1386+
{
1387+
struct sock *sk;
1388+
sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1389+
rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1390+
if (!sk)
1391+
return -ENOMEM;
1392+
1393+
/* Don't hold an extra reference on the namespace */
1394+
put_net(sk->sk_net);
1395+
net->rtnl = sk;
1396+
return 0;
1397+
}
1398+
1399+
static void rtnetlink_net_exit(struct net *net)
1400+
{
1401+
struct sock *sk = net->rtnl;
1402+
if (sk) {
1403+
/* At the last minute lie and say this is a socket for the
1404+
* initial network namespace. So the socket will be safe to
1405+
* free.
1406+
*/
1407+
sk->sk_net = get_net(&init_net);
1408+
sock_put(sk);
1409+
net->rtnl = NULL;
1410+
}
1411+
}
1412+
1413+
static struct pernet_operations rtnetlink_net_ops = {
1414+
.init = rtnetlink_net_init,
1415+
.exit = rtnetlink_net_exit,
1416+
};
1417+
13761418
void __init rtnetlink_init(void)
13771419
{
13781420
int i;
@@ -1385,10 +1427,9 @@ void __init rtnetlink_init(void)
13851427
if (!rta_buf)
13861428
panic("rtnetlink_init: cannot allocate rta_buf\n");
13871429

1388-
rtnl = netlink_kernel_create(&init_net, NETLINK_ROUTE, RTNLGRP_MAX,
1389-
rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1390-
if (rtnl == NULL)
1430+
if (register_pernet_subsys(&rtnetlink_net_ops))
13911431
panic("rtnetlink_init: cannot initialize rtnetlink\n");
1432+
13921433
netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
13931434
register_netdevice_notifier(&rtnetlink_dev_notifier);
13941435

‎net/decnet/dn_dev.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,10 @@ static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
793793
kfree_skb(skb);
794794
goto errout;
795795
}
796-
err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
796+
err = rtnl_notify(skb, &init_net, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
797797
errout:
798798
if (err < 0)
799-
rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
799+
rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_IFADDR, err);
800800
}
801801

802802
static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)

‎net/decnet/dn_route.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void
15871587
goto out_free;
15881588
}
15891589

1590-
return rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
1590+
return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
15911591

15921592
out_free:
15931593
kfree_skb(skb);

‎net/decnet/dn_table.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
375375
kfree_skb(skb);
376376
goto errout;
377377
}
378-
err = rtnl_notify(skb, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
378+
err = rtnl_notify(skb, &init_net, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
379379
errout:
380380
if (err < 0)
381-
rtnl_set_sk_err(RTNLGRP_DECnet_ROUTE, err);
381+
rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_ROUTE, err);
382382
}
383383

384384
static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,

‎net/ipv4/devinet.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,10 +1240,10 @@ static void rtmsg_ifa(int event, struct in_ifaddr* ifa, struct nlmsghdr *nlh,
12401240
kfree_skb(skb);
12411241
goto errout;
12421242
}
1243-
err = rtnl_notify(skb, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
1243+
err = rtnl_notify(skb, &init_net, pid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
12441244
errout:
12451245
if (err < 0)
1246-
rtnl_set_sk_err(RTNLGRP_IPV4_IFADDR, err);
1246+
rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_IFADDR, err);
12471247
}
12481248

12491249
#ifdef CONFIG_SYSCTL

0 commit comments

Comments
 (0)