@@ -60,7 +60,6 @@ struct rtnl_link
6060};
6161
6262static DEFINE_MUTEX (rtnl_mutex );
63- static struct sock * rtnl ;
6463
6564void 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 );
12051210errout :
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 );
12561261errout :
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
12671272static 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+
13761418void __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
0 commit comments