netlink: add socket destruction notification
Subsystems (12)
RFC 1/1 netlink: add socket destruction notification
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
--- drivers/connector/connector.c | 2 +- drivers/scsi/scsi_netlink.c | 2 +- drivers/scsi/scsi_transport_iscsi.c | 2 +- include/linux/netlink.h | 1 + include/net/genetlink.h | 3 +++ kernel/audit.c | 3 ++- lib/kobject_uevent.c | 2 +- net/bridge/netfilter/ebt_ulog.c | 2 +- net/core/rtnetlink.c | 3 ++- net/decnet/netfilter/dn_rtmsg.c | 2 +- net/ipv4/fib_frontend.c | 2 +- net/ipv4/inet_diag.c | 2 +- net/ipv4/netfilter/ip_queue.c | 2 +- net/ipv4/netfilter/ipt_ULOG.c | 6 +++--- net/ipv6/netfilter/ip6_queue.c | 2 +- net/netfilter/nfnetlink.c | 2 +- net/netlink/af_netlink.c | 6 ++++++ net/netlink/genetlink.c | 18 ++++++++++++++++-- net/xfrm/xfrm_user.c | 2 +- security/selinux/netlink.c | 3 ++- 20 files changed, 47 insertions(+), 20 deletions(-)
net/xfrm/xfrm_user.c +1 -1
@@ -2605,7 +2605,7 @@ static int __net_init xfrm_user_net_initstruct sock *nlsk; nlsk =netlink_kernel_create (net,NETLINK_XFRM ,XFRMNLGRP_MAX , - xfrm_netlink_rcv,NULL ,THIS_MODULE ); + xfrm_netlink_rcv,NULL ,NULL ,THIS_MODULE );if (nlsk ==NULL )return -ENOMEM ;rcu_assign_pointer (net->xfrm.nlsk, nlsk);
drivers/connector/connector.c +1 -1
@@ -451,7 +451,7 @@ static int __devinit cn_init(void) dev->nls =netlink_kernel_create (&init_net,NETLINK_CONNECTOR ,CN_NETLINK_USERS +0xf , - dev->input,NULL ,THIS_MODULE ); + dev->input,NULL ,NULL ,THIS_MODULE );if (!dev->nls)return -EIO ;
drivers/scsi/scsi_netlink.c +1 -1
@@ -496,7 +496,7 @@ scsi_netlink_init(void) scsi_nl_sock =netlink_kernel_create (&init_net,NETLINK_SCSITRANSPORT ,SCSI_NL_GRP_CNT , scsi_nl_rcv_msg,NULL , -THIS_MODULE ); +NULL ,THIS_MODULE );if (!scsi_nl_sock) {printk (KERN_ERR "%s: register of recieve handler failed\n" , __func__);
drivers/scsi/scsi_transport_iscsi.c +1 -1
@@ -2082,7 +2082,7 @@ static __init int iscsi_transport_init(v goto unregister_conn_class; nls =netlink_kernel_create (&init_net,NETLINK_ISCSI ,1 , iscsi_if_rx, -NULL ,THIS_MODULE ); +NULL ,NULL ,THIS_MODULE );if (!nls) { err = -ENOBUFS ; goto unregister_session_class;
kernel/audit.c +2 -1
@@ -970,7 +970,8 @@ static int __init audit_init(void)printk (KERN_INFO "audit: initializing netlink socket (%s)\n" , audit_default ?"enabled" :"disabled" ); audit_sock =netlink_kernel_create (&init_net,NETLINK_AUDIT ,0 , - audit_receive,NULL ,THIS_MODULE ); + audit_receive,NULL ,NULL , +THIS_MODULE );if (!audit_sock)audit_panic ("cannot initialize netlink socket" );else
lib/kobject_uevent.c +1 -1
@@ -322,7 +322,7 @@ EXPORT_SYMBOL_GPL(add_uevent_var);static int __init kobject_uevent_init (void ) { uevent_sock =netlink_kernel_create (&init_net,NETLINK_KOBJECT_UEVENT , -1 ,NULL ,NULL ,THIS_MODULE ); +1 ,NULL ,NULL ,NULL ,THIS_MODULE );if (!uevent_sock) {printk (KERN_ERR "kobject_uevent: unable to create netlink socket!\n" );
net/bridge/netfilter/ebt_ulog.c +1 -1
@@ -304,7 +304,7 @@ static int __init ebt_ulog_init(void) ebtulognl =netlink_kernel_create (&init_net,NETLINK_NFLOG ,EBT_ULOG_MAXNLGROUPS ,NULL ,NULL , -THIS_MODULE ); +NULL ,THIS_MODULE );if (!ebtulognl) { printk(KERN_WARNING KBUILD_MODNAME ": out of memory trying to " "call netlink_kernel_create\n");
net/core/rtnetlink.c +2 -1
@@ -1360,7 +1360,8 @@ static int rtnetlink_net_init(struct net {struct sock *sk; sk =netlink_kernel_create (net,NETLINK_ROUTE ,RTNLGRP_MAX , - rtnetlink_rcv, &rtnl_mutex,THIS_MODULE ); + rtnetlink_rcv,NULL , + &rtnl_mutex,THIS_MODULE );if (!sk)return -ENOMEM ; net->rtnl = sk;
net/decnet/netfilter/dn_rtmsg.c +1 -1
@@ -128,7 +128,7 @@ static int __init dn_rtmsg_init(void) dnrmg =netlink_kernel_create (&init_net,NETLINK_DNRTMSG ,DNRNG_NLGRP_MAX , - dnrmg_receive_user_skb, + dnrmg_receive_user_skb,NULL ,NULL ,THIS_MODULE );if (dnrmg ==NULL ) {printk (KERN_ERR "dn_rtmsg: Cannot create netlink socket" );
net/ipv4/fib_frontend.c +1 -1
@@ -879,7 +879,7 @@ static int nl_fib_lookup_init(struct net {struct sock *sk; sk =netlink_kernel_create (net,NETLINK_FIB_LOOKUP ,0 , - nl_fib_input,NULL ,THIS_MODULE ); + nl_fib_input,NULL ,NULL ,THIS_MODULE );if (sk ==NULL )return -EAFNOSUPPORT ; net->ipv4.fibnl = sk;
net/ipv4/inet_diag.c +1 -1
@@ -924,7 +924,7 @@ static int __init inet_diag_init(void) goto out; idiagnl =netlink_kernel_create (&init_net,NETLINK_INET_DIAG ,0 , - inet_diag_rcv,NULL ,THIS_MODULE ); + inet_diag_rcv,NULL ,NULL ,THIS_MODULE );if (idiagnl ==NULL ) goto out_free_table; err =0 ;
net/ipv4/netfilter/ip_queue.c +1 -1
@@ -578,7 +578,7 @@ static int __init ip_queue_init(void)netlink_register_notifier (&ipq_nl_notifier); ipqnl =netlink_kernel_create (&init_net,NETLINK_FIREWALL ,0 , - ipq_rcv_skb,NULL ,THIS_MODULE ); + ipq_rcv_skb,NULL ,NULL ,THIS_MODULE );if (ipqnl ==NULL ) {printk (KERN_ERR "ip_queue: failed to create netlink socket\n" ); goto cleanup_netlink_notifier;
net/ipv4/netfilter/ipt_ULOG.c +3 -3
@@ -400,9 +400,9 @@ static int __init ulog_tg_init(void)for (i =0 ; i <ULOG_MAXNLGROUPS ; i++)setup_timer (&ulog_buffers[i].timer, ulog_timer, i); - nflognl =netlink_kernel_create (&init_net, -NETLINK_NFLOG ,ULOG_MAXNLGROUPS ,NULL , -NULL ,THIS_MODULE ); + nflognl =netlink_kernel_create (&init_net,NETLINK_NFLOG , +ULOG_MAXNLGROUPS ,NULL , +NULL ,NULL ,THIS_MODULE );if (!nflognl)return -ENOMEM ;
net/ipv6/netfilter/ip6_queue.c +1 -1
@@ -580,7 +580,7 @@ static int __init ip6_queue_init(void)netlink_register_notifier (&ipq_nl_notifier); ipqnl =netlink_kernel_create (&init_net,NETLINK_IP6_FW ,0 , - ipq_rcv_skb,NULL ,THIS_MODULE ); + ipq_rcv_skb,NULL ,NULL ,THIS_MODULE );if (ipqnl ==NULL ) {printk (KERN_ERR "ip6_queue: failed to create netlink socket\n" ); goto cleanup_netlink_notifier;
net/netfilter/nfnetlink.c +1 -1
@@ -196,7 +196,7 @@ static int __init nfnetlink_init(void)printk ("Netfilter messages via NETLINK v%s.\n" , nfversion); nfnl =netlink_kernel_create (&init_net,NETLINK_NETFILTER ,NFNLGRP_MAX , - nfnetlink_rcv,NULL ,THIS_MODULE ); + nfnetlink_rcv,NULL ,NULL ,THIS_MODULE );if (!nfnl) {printk (KERN_ERR "cannot initialize nfnetlink!\n" );return -ENOMEM ;
net/netlink/genetlink.c +16 -2
@@ -561,6 +561,20 @@ static void genl_rcv(struct sk_buff *skbgenl_unlock (); } +static void genl_destruct (struct sock *sk) +{ +struct genl_family *f; +int idx; + +genl_lock (); + +for (idx =0 ; idx <GENL_FAM_TAB_SIZE ; idx++) +list_for_each_entry (f, &family_ht[idx], family_list) +if (f->destruct_sk) + f->destruct_sk (sk); +genl_unlock (); +} +/************************************************************************** * Controller **************************************************************************/
@@ -852,8 +866,8 @@ static int __net_init genl_pernet_init(s {/* we'll bump the group number right afterwards */ net->genl_sock =netlink_kernel_create (net,NETLINK_GENERIC ,0 , - genl_rcv, &genl_mutex, -THIS_MODULE ); + genl_rcv, genl_destruct, + &genl_mutex,THIS_MODULE );if (!net->genl_sock &&net_eq (net, &init_net))panic ("GENL: Cannot initialize generic netlink\n" );
security/selinux/netlink.c +2 -1
@@ -106,7 +106,8 @@ void selnl_notify_policyload(u32 seqno)static int __init selnl_init (void ) { selnl =netlink_kernel_create (&init_net,NETLINK_SELINUX , -SELNLGRP_MAX ,NULL ,NULL ,THIS_MODULE ); +SELNLGRP_MAX ,NULL ,NULL ,NULL , +THIS_MODULE );if (selnl ==NULL )panic ("SELinux: Cannot create netlink socket." );netlink_set_nonroot (NETLINK_SELINUX ,NL_NONROOT_RECV );
include/linux/netlink.h +1 -0
@@ -182,6 +182,7 @@ extern void netlink_table_ungrab(void);extern struct sock *netlink_kernel_create (struct net *net,int unit,unsigned int groups,void (*input)(struct sk_buff *skb), +void (*destruct)(struct sock *sk),struct mutex *cb_mutex,struct module *module);extern void netlink_kernel_release (struct sock *sk);
include/net/genetlink.h +3 -0
@@ -30,6 +30,8 @@ struct genl_multicast_group* @maxattr: maximum number of attributes supported * @netnsok: set to true if the family can handle network * namespaces and should be presented in all of them +* @destruct_sk: called when any generic netlink socket +* is destroyed (e.g. by the application closing it) * @attrbuf: buffer to store parsed attributes * @ops_list: list of all assigned operations * @family_list: family list
@@ -43,6 +45,7 @@ struct genl_familyunsigned int version;unsigned int maxattr;bool netnsok; +void (*destruct_sk)(struct sock *sk);struct nlattr ** attrbuf;/* private */ struct list_head ops_list;/* private */ struct list_head family_list;/* private */
net/netlink/af_netlink.c +6 -0
@@ -80,6 +80,7 @@ struct netlink_sock {struct mutex *cb_mutex;struct mutex cb_def_mutex;void (*netlink_rcv)(struct sk_buff *skb); +void (*destruct)(struct sock *sk);struct module *module; };
@@ -166,6 +167,9 @@ static void netlink_sock_destruct(structreturn ; } +if (nlk->destruct) + nlk->destruct (sk); +WARN_ON (atomic_read (&sk->sk_rmem_alloc));WARN_ON (atomic_read (&sk->sk_wmem_alloc));WARN_ON (nlk_sk (sk)->groups);
@@ -1464,6 +1468,7 @@ static void netlink_data_ready(struct sostruct sock *netlink_kernel_create (struct net *net,int unit,unsigned int groups,void (*input)(struct sk_buff *skb), +void (*destruct)(struct sock *sk),struct mutex *cb_mutex,struct module *module) {struct socket *sock;
@@ -1502,6 +1507,7 @@ netlink_kernel_create(struct net *net, i sk->sk_data_ready = netlink_data_ready;if (input)nlk_sk (sk)->netlink_rcv = input; +nlk_sk (sk)->destruct = destruct;if (netlink_insert (sk, net,0 )) goto out_sock_release;
David Miller
maintainerFrom: Johannes Berg <[email protected]>
Date: Fri, 02 Oct 2009 10:44:08 +0200
Quoted 8 lines
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
Signed-off-by Johannes Berg <[email protected]>
No fundamental objections.
However, as a followup, netlink_kernel_create() is becomming function_that_takes_too_many_arguments().
At this point it's better to just pass two arguments, the network namespace pointer, and a pointer to a "const struct netlink_kern_info" that holds the rest of the parameters.
Could you make that change too?
Thanks!
Quoted 8 lines · David Miller
Quoted 6 lines
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
No fundamental objections.
:)
Quoted 8 lines
However, as a followup,
netlink_kernel_create()is becommingfunction_that_takes_too_many_arguments().At this point it's better to just pass two arguments, the network namespace pointer, and a pointer to a "const struct
netlink_kern_info" that holds the rest of the parameters.Could you make that change too?
Yeah, I agree -- will change.
johannes
Quoted 9 lines · Johannes Berg
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
net/netlink/af_netlink.c static void netlink_sock_destruct(struct · line 175
Quoted 9 lines
net/netlink/af_netlink.c +3
-0@@ -166,6 +167,9 @@ static void netlink_sock_destruct(structreturn ; } +if (nlk->destruct) + nlk->destruct (sk); +WARN_ON (atomic_read (&sk->sk_rmem_alloc));WARN_ON (atomic_read (&sk->sk_wmem_alloc));WARN_ON (nlk_sk (sk)->groups);
This seems pretty similar to the NETLINK_URELEASE notifier invoked in netlink_release(). Wouldn't that one work as well?
Quoted 11 lines · Patrick McHardy
Quoted 6 lines · Johannes Berg
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
This seems pretty similar to the
NETLINK_URELEASEnotifier invoked innetlink_release(). Wouldn't that one work as well?
Hmm, it does seem similar, thanks for pointing it out. What exactly does the condition
if (nlk->pid && !nlk->subscriptions) {mean though?
johannes
Quoted 10 lines · Johannes Berg
Quoted 7 lines · Patrick McHardy
Quoted 6 lines · Johannes Berg
When we want to keep track of resources associated with applications, we need to know when an app is going away. Add a notification function to netlink that tells us that, and also hook it up to generic netlink so generic netlink can notify the families. Due to the way generic netlink works though, we need to notify all families and they have to sort out whatever resources some commands associated with the socket themselves.
Quoted 8 lines
Quoted 2 lines
This seems pretty similar to the
NETLINK_URELEASEnotifier invoked innetlink_release(). Wouldn't that one work as well?Hmm, it does seem similar, thanks for pointing it out. What exactly does the condition
if (nlk->pid && !nlk->subscriptions) {mean though?
nlk->pid is non-zero for bound sockets, which is basically any non-kernel socket which has either sent a message or explicitly called bind(). nlk->subscriptions is zero for sockets not bound to multicast groups.
So effectively it invokes the notifier for all bound unicast userspace sockets. Not sure why it doesn't invoke the notifier for sockets that are used for both unicast and multicast reception. If that is a problem I think the second condition could be removed.
Quoted 14 lines · Patrick McHardy
Quoted 8 lines
Quoted 2 lines
This seems pretty similar to the
NETLINK_URELEASEnotifier invoked innetlink_release(). Wouldn't that one work as well?Hmm, it does seem similar, thanks for pointing it out. What exactly does the condition
if (nlk->pid && !nlk->subscriptions) {mean though?
nlk->pidis non-zero for bound sockets, which is basically any non-kernel socket which has either sent a message or explicitly calledbind().nlk->subscriptionsis zero for sockets not bound to multicast groups.
Quoted 5 lines
So effectively it invokes the notifier for all bound unicast userspace sockets. Not sure why it doesn't invoke the notifier for sockets that are used for both unicast and multicast reception. If that is a problem I think the second condition could be removed.
Thanks for the explanation. I think we'd need the second condition removed, I don't see a reason to force a socket to not also have multicast RX if it's used for any of the purposes we're looking at this for. Guess we need to audit the callees to determine whether that's ok.
Can you quickly explain the difference between release and destruct?
johannes
Quoted 20 lines · Johannes Berg
Quoted 12 lines · Patrick McHardy
Quoted 7 lines
Quoted 2 lines
This seems pretty similar to the
NETLINK_URELEASEnotifier invoked innetlink_release(). Wouldn't that one work as well?Hmm, it does seem similar, thanks for pointing it out. What exactly does the condition
if (nlk->pid && !nlk->subscriptions) {mean though?
nlk->pidis non-zero for bound sockets, which is basically any non-kernel socket which has either sent a message or explicitly calledbind().nlk->subscriptionsis zero for sockets not bound to multicast groups.Quoted 5 lines
So effectively it invokes the notifier for all bound unicast userspace sockets. Not sure why it doesn't invoke the notifier for sockets that are used for both unicast and multicast reception. If that is a problem I think the second condition could be removed.
Quoted 4 lines
Thanks for the explanation. I think we'd need the second condition removed, I don't see a reason to force a socket to not also have multicast RX if it's used for any of the purposes we're looking at this for. Guess we need to audit the callees to determine whether that's ok.
I've already done that. Its currently only used by netfilter for which this change also makes sense.
Quoted 1 line
Can you quickly explain the difference between release and destruct?
release is called when the socket is closed, destruct is called once all references are gone. I think with the synchonous processing done nowadays they shouldn't make any difference, but release should be fine in either case.
Quoted 7 lines · Patrick McHardy
Quoted 4 lines
Thanks for the explanation. I think we'd need the second condition removed, I don't see a reason to force a socket to not also have multicast RX if it's used for any of the purposes we're looking at this for. Guess we need to audit the callees to determine whether that's ok.
I've already done that. Its currently only used by netfilter for which this change also makes sense.
Cool, I arrived at that conclusion too, it seemed that it would currently be somewhat strangely broken if you could add multicast groups to those sockets used there. Not sure if you can though.
Quoted 6 lines
Quoted 1 line
Can you quickly explain the difference between release and destruct?
release is called when the socket is closed, destruct is called once all references are gone. I think with the synchonous processing done nowadays they shouldn't make any difference, but release should be fine in either case.
Ok, cool, thanks. Do you want me to send the change removing the multicast check, or would you want to do that since you audited all the netlink callers?
Also, it's called URELEASE for unicast -- should we rename it to just RELEASE?
johannes
Quoted 6 lines · Johannes Berg
Quoted 4 lines · Patrick McHardy
Quoted 4 lines
Thanks for the explanation. I think we'd need the second condition removed, I don't see a reason to force a socket to not also have multicast RX if it's used for any of the purposes we're looking at this for. Guess we need to audit the callees to determine whether that's ok.
Quoted 6 lines
Quoted 2 lines
I've already done that. Its currently only used by netfilter for which this change also makes sense.
Cool, I arrived at that conclusion too, it seemed that it would currently be somewhat strangely broken if you could add multicast groups to those sockets used there. Not sure if you can though.
I don't see anything preventing it.
Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Can you quickly explain the difference between release and destruct?
release is called when the socket is closed, destruct is called once all references are gone. I think with the synchonous processing done nowadays they shouldn't make any difference, but release should be fine in either case.
Ok, cool, thanks. Do you want me to send the change removing the multicast check, or would you want to do that since you audited all the netlink callers?
Please go ahead.
Quoted 2 lines
Also, it's called URELEASE for unicast -- should we rename it to just RELEASE?
I think URELEASE is still fine since won't necessarily get called for sockets that are used for pure multicast reception when using setsockopt to bind to groups. I also have a cleanup patch removing unneccessary nlk->pid checks from netfilter which would clash with a rename :)
Quoted 5 lines · Patrick McHardy
Quoted 3 lines
Ok, cool, thanks. Do you want me to send the change removing the multicast check, or would you want to do that since you audited all the netlink callers?
Please go ahead.
Will do.
Quoted 6 lines
Quoted 2 lines
Also, it's called URELEASE for unicast -- should we rename it to just RELEASE?
I think URELEASE is still fine since won't necessarily get called for sockets that are used for pure multicast reception when using setsockopt to bind to groups.
Oh? So on which sockets can I rely on it being used? After sending at least one unicast message into the kernel? This seems to depend on pid being assigned -- when is that?
johannes
Quoted 11 lines · Johannes Berg
Quoted 4 lines · Patrick McHardy
Quoted 3 lines
Ok, cool, thanks. Do you want me to send the change removing the multicast check, or would you want to do that since you audited all the netlink callers?
Please go ahead.
Will do.
Quoted 2 lines
Quoted 2 lines
Also, it's called URELEASE for unicast -- should we rename it to just RELEASE?
Quoted 7 lines
Quoted 3 lines
I think URELEASE is still fine since won't necessarily get called for sockets that are used for pure multicast reception when using setsockopt to bind to groups.
Oh? So on which sockets can I rely on it being used? After sending at least one unicast message into the kernel? This seems to depend on pid being assigned -- when is that?
All unicast sockets that have either manually or automatically bound. Automatic binding happens when sending the first message or when calling connect().
Before that, your code can't know of the sockets existance, so I guess this should be fine.
Quoted 10 lines · Patrick McHardy
Quoted 3 lines
Oh? So on which sockets can I rely on it being used? After sending at least one unicast message into the kernel? This seems to depend on pid being assigned -- when is that?
All unicast sockets that have either manually or automatically bound. Automatic binding happens when sending the first message or when calling
connect().Before that, your code can't know of the sockets existance, so I guess this should be fine.
Ah, yes, of course... that's part of the boilerplate code to use generic netlink so I always forget about what exactly is going on there :)
Thanks!
johannes