2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* NETLINK Kernel-user communication protocol.
|
|
|
|
*
|
2008-10-14 02:01:08 +00:00
|
|
|
* Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
|
2005-04-16 22:20:36 +00:00
|
|
|
* Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
|
2013-04-17 06:47:05 +00:00
|
|
|
* Patrick McHardy <kaber@trash.net>
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
2007-02-09 14:25:07 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
|
|
|
|
* added netlink_proto_exit
|
|
|
|
* Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
|
|
|
|
* use nlk_sk, as sk->protinfo is on a diet 8)
|
2005-08-10 02:40:55 +00:00
|
|
|
* Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
|
|
|
|
* - inc module use count of module that owns
|
|
|
|
* the kernel socket in case userspace opens
|
|
|
|
* socket of same protocol
|
|
|
|
* - remove all module support, since netlink is
|
|
|
|
* mandatory if CONFIG_NET=y these days
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2006-01-11 20:17:47 +00:00
|
|
|
#include <linux/capability.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/un.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/termios.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/types.h>
|
2005-04-30 06:07:04 +00:00
|
|
|
#include <linux/audit.h>
|
2007-04-20 21:14:21 +00:00
|
|
|
#include <linux/mutex.h>
|
2013-04-17 06:47:01 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2013-06-21 17:38:07 +00:00
|
|
|
#include <linux/if_arp.h>
|
2013-04-17 06:47:02 +00:00
|
|
|
#include <asm/cacheflush.h>
|
2005-04-30 06:07:04 +00:00
|
|
|
|
2007-09-12 10:01:34 +00:00
|
|
|
#include <net/net_namespace.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/scm.h>
|
2005-11-10 01:25:53 +00:00
|
|
|
#include <net/netlink.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-03-21 16:33:47 +00:00
|
|
|
#include "af_netlink.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-10-24 04:27:10 +00:00
|
|
|
struct listeners {
|
|
|
|
struct rcu_head rcu;
|
|
|
|
unsigned long masks[0];
|
2009-07-10 09:51:32 +00:00
|
|
|
};
|
|
|
|
|
2013-04-17 06:46:56 +00:00
|
|
|
/* state bits */
|
|
|
|
#define NETLINK_CONGESTED 0x0
|
|
|
|
|
|
|
|
/* flags */
|
2005-08-15 02:27:13 +00:00
|
|
|
#define NETLINK_KERNEL_SOCKET 0x1
|
2005-08-15 19:32:15 +00:00
|
|
|
#define NETLINK_RECV_PKTINFO 0x2
|
2009-02-18 01:40:43 +00:00
|
|
|
#define NETLINK_BROADCAST_SEND_ERROR 0x4
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
#define NETLINK_RECV_NO_ENOBUFS 0x8
|
2005-08-15 02:27:13 +00:00
|
|
|
|
2011-12-23 22:33:03 +00:00
|
|
|
static inline int netlink_is_kernel(struct sock *sk)
|
2007-10-11 04:14:32 +00:00
|
|
|
{
|
|
|
|
return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
|
|
|
|
}
|
|
|
|
|
2013-03-21 16:33:47 +00:00
|
|
|
struct netlink_table *nl_table;
|
|
|
|
EXPORT_SYMBOL_GPL(nl_table);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
|
|
|
|
|
|
|
|
static int netlink_dump(struct sock *sk);
|
2013-04-17 06:47:02 +00:00
|
|
|
static void netlink_skb_destructor(struct sk_buff *skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-03-21 16:33:47 +00:00
|
|
|
DEFINE_RWLOCK(nl_table_lock);
|
|
|
|
EXPORT_SYMBOL_GPL(nl_table_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
static atomic_t nl_table_users = ATOMIC_INIT(0);
|
|
|
|
|
2012-10-18 03:21:55 +00:00
|
|
|
#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
|
|
|
|
|
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no
protection against entries being added to or removed from a chain while the
chain is in use. The issues were discussed in this thread:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2
We noticed that notifier chains in the kernel fall into two basic usage
classes:
"Blocking" chains are always called from a process context
and the callout routines are allowed to sleep;
"Atomic" chains can be called from an atomic context and
the callout routines are not allowed to sleep.
We decided to codify this distinction and make it part of the API. Therefore
this set of patches introduces three new, parallel APIs: one for blocking
notifiers, one for atomic notifiers, and one for "raw" notifiers (which is
really just the old API under a new name). New kinds of data structures are
used for the heads of the chains, and new routines are defined for
registration, unregistration, and calling a chain. The three APIs are
explained in include/linux/notifier.h and their implementation is in
kernel/sys.c.
With atomic and blocking chains, the implementation guarantees that the chain
links will not be corrupted and that chain callers will not get messed up by
entries being added or removed. For raw chains the implementation provides no
guarantees at all; users of this API must provide their own protections. (The
idea was that situations may come up where the assumptions of the atomic and
blocking APIs are not appropriate, so it should be possible for users to
handle these things in their own way.)
There are some limitations, which should not be too hard to live with. For
atomic/blocking chains, registration and unregistration must always be done in
a process context since the chain is protected by a mutex/rwsem. Also, a
callout routine for a non-raw chain must not try to register or unregister
entries on its own chain. (This did happen in a couple of places and the code
had to be changed to avoid it.)
Since atomic chains may be called from within an NMI handler, they cannot use
spinlocks for synchronization. Instead we use RCU. The overhead falls almost
entirely in the unregister routine, which is okay since unregistration is much
less frequent that calling a chain.
Here is the list of chains that we adjusted and their classifications. None
of them use the raw API, so for the moment it is only a placeholder.
ATOMIC CHAINS
-------------
arch/i386/kernel/traps.c: i386die_chain
arch/ia64/kernel/traps.c: ia64die_chain
arch/powerpc/kernel/traps.c: powerpc_die_chain
arch/sparc64/kernel/traps.c: sparc64die_chain
arch/x86_64/kernel/traps.c: die_chain
drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list
kernel/panic.c: panic_notifier_list
kernel/profile.c: task_free_notifier
net/bluetooth/hci_core.c: hci_notifier
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain
net/ipv6/addrconf.c: inet6addr_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain
net/netlink/af_netlink.c: netlink_chain
BLOCKING CHAINS
---------------
arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain
arch/s390/kernel/process.c: idle_chain
arch/x86_64/kernel/process.c idle_notifier
drivers/base/memory.c: memory_chain
drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list
drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list
drivers/macintosh/adb.c: adb_client_list
drivers/macintosh/via-pmu.c sleep_notifier_list
drivers/macintosh/via-pmu68k.c sleep_notifier_list
drivers/macintosh/windfarm_core.c wf_client_list
drivers/usb/core/notify.c usb_notifier_list
drivers/video/fbmem.c fb_notifier_list
kernel/cpu.c cpu_chain
kernel/module.c module_notify_list
kernel/profile.c munmap_notifier
kernel/profile.c task_exit_notifier
kernel/sys.c reboot_notifier_list
net/core/dev.c netdev_chain
net/decnet/dn_dev.c: dnaddr_chain
net/ipv4/devinet.c: inetaddr_chain
It's possible that some of these classifications are wrong. If they are,
please let us know or submit a patch to fix them. Note that any chain that
gets called very frequently should be atomic, because the rwsem read-locking
used for blocking chains is very likely to incur cache misses on SMP systems.
(However, if the chain's callout routines may sleep then the chain cannot be
atomic.)
The patch set was written by Alan Stern and Chandra Seetharaman, incorporating
material written by Keith Owens and suggestions from Paul McKenney and Andrew
Morton.
[jes@sgi.com: restructure the notifier chain initialization macros]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-03-27 09:16:30 +00:00
|
|
|
static ATOMIC_NOTIFIER_HEAD(netlink_chain);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
static DEFINE_SPINLOCK(netlink_tap_lock);
|
|
|
|
static struct list_head netlink_tap_all __read_mostly;
|
|
|
|
|
2011-12-22 08:52:02 +00:00
|
|
|
static inline u32 netlink_group_mask(u32 group)
|
2005-08-15 02:27:50 +00:00
|
|
|
{
|
|
|
|
return group ? 1 << (group - 1) : 0;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2012-09-07 20:12:54 +00:00
|
|
|
return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
int netlink_add_tap(struct netlink_tap *nt)
|
|
|
|
{
|
|
|
|
if (unlikely(nt->dev->type != ARPHRD_NETLINK))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
spin_lock(&netlink_tap_lock);
|
|
|
|
list_add_rcu(&nt->list, &netlink_tap_all);
|
|
|
|
spin_unlock(&netlink_tap_lock);
|
|
|
|
|
|
|
|
if (nt->module)
|
|
|
|
__module_get(nt->module);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(netlink_add_tap);
|
|
|
|
|
2013-12-30 18:49:22 +00:00
|
|
|
static int __netlink_remove_tap(struct netlink_tap *nt)
|
2013-06-21 17:38:07 +00:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
struct netlink_tap *tmp;
|
|
|
|
|
|
|
|
spin_lock(&netlink_tap_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(tmp, &netlink_tap_all, list) {
|
|
|
|
if (nt == tmp) {
|
|
|
|
list_del_rcu(&nt->list);
|
|
|
|
found = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_warn("__netlink_remove_tap: %p not found\n", nt);
|
|
|
|
out:
|
|
|
|
spin_unlock(&netlink_tap_lock);
|
|
|
|
|
|
|
|
if (found && nt->module)
|
|
|
|
module_put(nt->module);
|
|
|
|
|
|
|
|
return found ? 0 : -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
int netlink_remove_tap(struct netlink_tap *nt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = __netlink_remove_tap(nt);
|
|
|
|
synchronize_net();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(netlink_remove_tap);
|
|
|
|
|
2013-09-05 15:48:47 +00:00
|
|
|
static bool netlink_filter_tap(const struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
bool pass = false;
|
|
|
|
|
|
|
|
/* We take the more conservative approach and
|
|
|
|
* whitelist socket protocols that may pass.
|
|
|
|
*/
|
|
|
|
switch (sk->sk_protocol) {
|
|
|
|
case NETLINK_ROUTE:
|
|
|
|
case NETLINK_USERSOCK:
|
|
|
|
case NETLINK_SOCK_DIAG:
|
|
|
|
case NETLINK_NFLOG:
|
|
|
|
case NETLINK_XFRM:
|
|
|
|
case NETLINK_FIB_LOOKUP:
|
|
|
|
case NETLINK_NETFILTER:
|
|
|
|
case NETLINK_GENERIC:
|
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
static int __netlink_deliver_tap_skb(struct sk_buff *skb,
|
|
|
|
struct net_device *dev)
|
|
|
|
{
|
|
|
|
struct sk_buff *nskb;
|
2013-09-05 15:48:47 +00:00
|
|
|
struct sock *sk = skb->sk;
|
2013-06-21 17:38:07 +00:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
dev_hold(dev);
|
|
|
|
nskb = skb_clone(skb, GFP_ATOMIC);
|
|
|
|
if (nskb) {
|
|
|
|
nskb->dev = dev;
|
2013-09-05 15:48:47 +00:00
|
|
|
nskb->protocol = htons((u16) sk->sk_protocol);
|
netlink: specify netlink packet direction for nlmon
In order to facilitate development for netlink protocol dissector,
fill the unused field skb->pkt_type of the cloned skb with a hint
of the address space of the new owner (receiver) socket in the
notion of "to kernel" resp. "to user".
At the time we invoke __netlink_deliver_tap_skb(), we already have
set the new skb owner via netlink_skb_set_owner_r(), so we can use
that for netlink_is_kernel() probing.
In normal PF_PACKET network traffic, this field denotes if the
packet is destined for us (PACKET_HOST), if it's broadcast
(PACKET_BROADCAST), etc.
As we only have 3 bit reserved, we can use the value (= 6) of
PACKET_FASTROUTE as it's _not used_ anywhere in the whole kernel
and not supported anywhere, and packets of such type were never
exposed to user space, so there are no overlapping users of such
kind. Thus, as wished, that seems the only way to make both
PACKET_* values non-overlapping and therefore device agnostic.
By using those two flags for netlink skbs on nlmon devices, they
can be made available and picked up via sll_pkttype (previously
unused in netlink context) in struct sockaddr_ll. We now have
these two directions:
- PACKET_USER (= 6) -> to user space
- PACKET_KERNEL (= 7) -> to kernel space
Partial `ip a` example strace for sa_family=AF_NETLINK with
detected nl msg direction:
syscall: direction:
sendto(3, ...) = 40 /* to kernel */
recvmsg(3, ...) = 3404 /* to user */
recvmsg(3, ...) = 1120 /* to user */
recvmsg(3, ...) = 20 /* to user */
sendto(3, ...) = 40 /* to kernel */
recvmsg(3, ...) = 168 /* to user */
recvmsg(3, ...) = 144 /* to user */
recvmsg(3, ...) = 20 /* to user */
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: Jakub Zawadzki <darkjames-ws@darkjames.pl>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-12-23 13:35:56 +00:00
|
|
|
nskb->pkt_type = netlink_is_kernel(sk) ?
|
|
|
|
PACKET_KERNEL : PACKET_USER;
|
2013-09-05 15:48:47 +00:00
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
ret = dev_queue_xmit(nskb);
|
|
|
|
if (unlikely(ret > 0))
|
|
|
|
ret = net_xmit_errno(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_put(dev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __netlink_deliver_tap(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct netlink_tap *tmp;
|
|
|
|
|
2013-09-05 15:48:47 +00:00
|
|
|
if (!netlink_filter_tap(skb))
|
|
|
|
return;
|
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
|
|
|
|
ret = __netlink_deliver_tap_skb(skb, tmp->dev);
|
|
|
|
if (unlikely(ret))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_deliver_tap(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
|
|
|
|
if (unlikely(!list_empty(&netlink_tap_all)))
|
|
|
|
__netlink_deliver_tap(skb);
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2013-12-23 13:35:55 +00:00
|
|
|
static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
|
|
|
|
netlink_deliver_tap(skb);
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:05 +00:00
|
|
|
static void netlink_overrun(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
|
|
|
if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
|
|
|
|
if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
|
|
|
|
sk->sk_err = ENOBUFS;
|
|
|
|
sk->sk_error_report(sk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic_inc(&sk->sk_drops);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_rcv_wake(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
|
|
|
if (skb_queue_empty(&sk->sk_receive_queue))
|
|
|
|
clear_bit(NETLINK_CONGESTED, &nlk->state);
|
|
|
|
if (!test_bit(NETLINK_CONGESTED, &nlk->state))
|
|
|
|
wake_up_interruptible(&nlk->wait);
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:01 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
2013-04-17 06:47:02 +00:00
|
|
|
static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
static bool netlink_rx_is_mmaped(struct sock *sk)
|
|
|
|
{
|
|
|
|
return nlk_sk(sk)->rx_ring.pg_vec != NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:03 +00:00
|
|
|
static bool netlink_tx_is_mmaped(struct sock *sk)
|
|
|
|
{
|
|
|
|
return nlk_sk(sk)->tx_ring.pg_vec != NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:01 +00:00
|
|
|
static __pure struct page *pgvec_to_page(const void *addr)
|
|
|
|
{
|
|
|
|
if (is_vmalloc_addr(addr))
|
|
|
|
return vmalloc_to_page(addr);
|
|
|
|
else
|
|
|
|
return virt_to_page(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (pg_vec[i] != NULL) {
|
|
|
|
if (is_vmalloc_addr(pg_vec[i]))
|
|
|
|
vfree(pg_vec[i]);
|
|
|
|
else
|
|
|
|
free_pages((unsigned long)pg_vec[i], order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kfree(pg_vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *alloc_one_pg_vec_page(unsigned long order)
|
|
|
|
{
|
|
|
|
void *buffer;
|
|
|
|
gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
|
|
|
|
__GFP_NOWARN | __GFP_NORETRY;
|
|
|
|
|
|
|
|
buffer = (void *)__get_free_pages(gfp_flags, order);
|
|
|
|
if (buffer != NULL)
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
buffer = vzalloc((1 << order) * PAGE_SIZE);
|
|
|
|
if (buffer != NULL)
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
gfp_flags &= ~__GFP_NORETRY;
|
|
|
|
return (void *)__get_free_pages(gfp_flags, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void **alloc_pg_vec(struct netlink_sock *nlk,
|
|
|
|
struct nl_mmap_req *req, unsigned int order)
|
|
|
|
{
|
|
|
|
unsigned int block_nr = req->nm_block_nr;
|
|
|
|
unsigned int i;
|
2013-08-02 15:32:39 +00:00
|
|
|
void **pg_vec;
|
2013-04-17 06:47:01 +00:00
|
|
|
|
|
|
|
pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
|
|
|
|
if (pg_vec == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < block_nr; i++) {
|
2013-08-02 15:32:39 +00:00
|
|
|
pg_vec[i] = alloc_one_pg_vec_page(order);
|
2013-04-17 06:47:01 +00:00
|
|
|
if (pg_vec[i] == NULL)
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pg_vec;
|
|
|
|
err1:
|
|
|
|
free_pg_vec(pg_vec, order, block_nr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
|
|
|
|
bool closing, bool tx_ring)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct netlink_ring *ring;
|
|
|
|
struct sk_buff_head *queue;
|
|
|
|
void **pg_vec = NULL;
|
|
|
|
unsigned int order = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
|
|
|
|
queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
|
|
|
|
|
|
|
|
if (!closing) {
|
|
|
|
if (atomic_read(&nlk->mapped))
|
|
|
|
return -EBUSY;
|
|
|
|
if (atomic_read(&ring->pending))
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->nm_block_nr) {
|
|
|
|
if (ring->pg_vec != NULL)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if ((int)req->nm_block_size <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
|
|
|
|
return -EINVAL;
|
|
|
|
if (req->nm_frame_size < NL_MMAP_HDRLEN)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ring->frames_per_block = req->nm_block_size /
|
|
|
|
req->nm_frame_size;
|
|
|
|
if (ring->frames_per_block == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ring->frames_per_block * req->nm_block_nr !=
|
|
|
|
req->nm_frame_nr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
order = get_order(req->nm_block_size);
|
|
|
|
pg_vec = alloc_pg_vec(nlk, req, order);
|
|
|
|
if (pg_vec == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
|
|
|
if (req->nm_frame_nr)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = -EBUSY;
|
|
|
|
mutex_lock(&nlk->pg_vec_lock);
|
|
|
|
if (closing || atomic_read(&nlk->mapped) == 0) {
|
|
|
|
err = 0;
|
|
|
|
spin_lock_bh(&queue->lock);
|
|
|
|
|
|
|
|
ring->frame_max = req->nm_frame_nr - 1;
|
|
|
|
ring->head = 0;
|
|
|
|
ring->frame_size = req->nm_frame_size;
|
|
|
|
ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
|
|
|
|
|
|
|
|
swap(ring->pg_vec_len, req->nm_block_nr);
|
|
|
|
swap(ring->pg_vec_order, order);
|
|
|
|
swap(ring->pg_vec, pg_vec);
|
|
|
|
|
|
|
|
__skb_queue_purge(queue);
|
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
WARN_ON(atomic_read(&nlk->mapped));
|
|
|
|
}
|
|
|
|
mutex_unlock(&nlk->pg_vec_lock);
|
|
|
|
|
|
|
|
if (pg_vec)
|
|
|
|
free_pg_vec(pg_vec, order, req->nm_block_nr);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_mm_open(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
if (sk)
|
|
|
|
atomic_inc(&nlk_sk(sk)->mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_mm_close(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct file *file = vma->vm_file;
|
|
|
|
struct socket *sock = file->private_data;
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
if (sk)
|
|
|
|
atomic_dec(&nlk_sk(sk)->mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct vm_operations_struct netlink_mmap_ops = {
|
|
|
|
.open = netlink_mm_open,
|
|
|
|
.close = netlink_mm_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int netlink_mmap(struct file *file, struct socket *sock,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct netlink_ring *ring;
|
|
|
|
unsigned long start, size, expected;
|
|
|
|
unsigned int i;
|
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
if (vma->vm_pgoff)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&nlk->pg_vec_lock);
|
|
|
|
|
|
|
|
expected = 0;
|
|
|
|
for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
|
|
|
|
if (ring->pg_vec == NULL)
|
|
|
|
continue;
|
|
|
|
expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
size = vma->vm_end - vma->vm_start;
|
|
|
|
if (size != expected)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
start = vma->vm_start;
|
|
|
|
for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
|
|
|
|
if (ring->pg_vec == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < ring->pg_vec_len; i++) {
|
|
|
|
struct page *page;
|
|
|
|
void *kaddr = ring->pg_vec[i];
|
|
|
|
unsigned int pg_num;
|
|
|
|
|
|
|
|
for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
|
|
|
|
page = pgvec_to_page(kaddr);
|
|
|
|
err = vm_insert_page(vma, start, page);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
kaddr += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_inc(&nlk->mapped);
|
|
|
|
vma->vm_ops = &netlink_mmap_ops;
|
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
mutex_unlock(&nlk->pg_vec_lock);
|
2013-06-11 09:52:47 +00:00
|
|
|
return err;
|
2013-04-17 06:47:01 +00:00
|
|
|
}
|
2013-04-17 06:47:02 +00:00
|
|
|
|
|
|
|
static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
|
|
|
|
{
|
|
|
|
#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
|
|
|
|
struct page *p_start, *p_end;
|
|
|
|
|
|
|
|
/* First page is flushed through netlink_{get,set}_status */
|
|
|
|
p_start = pgvec_to_page(hdr + PAGE_SIZE);
|
2013-04-23 07:40:35 +00:00
|
|
|
p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
|
2013-04-17 06:47:02 +00:00
|
|
|
while (p_start <= p_end) {
|
|
|
|
flush_dcache_page(p_start);
|
|
|
|
p_start++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
|
|
|
|
{
|
|
|
|
smp_rmb();
|
|
|
|
flush_dcache_page(pgvec_to_page(hdr));
|
|
|
|
return hdr->nm_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_set_status(struct nl_mmap_hdr *hdr,
|
|
|
|
enum nl_mmap_status status)
|
|
|
|
{
|
|
|
|
hdr->nm_status = status;
|
|
|
|
flush_dcache_page(pgvec_to_page(hdr));
|
|
|
|
smp_wmb();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nl_mmap_hdr *
|
|
|
|
__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
|
|
|
|
{
|
|
|
|
unsigned int pg_vec_pos, frame_off;
|
|
|
|
|
|
|
|
pg_vec_pos = pos / ring->frames_per_block;
|
|
|
|
frame_off = pos % ring->frames_per_block;
|
|
|
|
|
|
|
|
return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nl_mmap_hdr *
|
|
|
|
netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
|
|
|
|
enum nl_mmap_status status)
|
|
|
|
{
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
|
|
|
|
hdr = __netlink_lookup_frame(ring, pos);
|
|
|
|
if (netlink_get_status(hdr) != status)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return hdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nl_mmap_hdr *
|
|
|
|
netlink_current_frame(const struct netlink_ring *ring,
|
|
|
|
enum nl_mmap_status status)
|
|
|
|
{
|
|
|
|
return netlink_lookup_frame(ring, ring->head, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nl_mmap_hdr *
|
|
|
|
netlink_previous_frame(const struct netlink_ring *ring,
|
|
|
|
enum nl_mmap_status status)
|
|
|
|
{
|
|
|
|
unsigned int prev;
|
|
|
|
|
|
|
|
prev = ring->head ? ring->head - 1 : ring->frame_max;
|
|
|
|
return netlink_lookup_frame(ring, prev, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_increment_head(struct netlink_ring *ring)
|
|
|
|
{
|
|
|
|
ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_forward_ring(struct netlink_ring *ring)
|
|
|
|
{
|
|
|
|
unsigned int head = ring->head, pos = head;
|
|
|
|
const struct nl_mmap_hdr *hdr;
|
|
|
|
|
|
|
|
do {
|
|
|
|
hdr = __netlink_lookup_frame(ring, pos);
|
|
|
|
if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
|
|
|
|
break;
|
|
|
|
if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
|
|
|
|
break;
|
|
|
|
netlink_increment_head(ring);
|
|
|
|
} while (ring->head != head);
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:05 +00:00
|
|
|
static bool netlink_dump_space(struct netlink_sock *nlk)
|
|
|
|
{
|
|
|
|
struct netlink_ring *ring = &nlk->rx_ring;
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
|
|
|
|
if (hdr == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
n = ring->head + ring->frame_max / 2;
|
|
|
|
if (n > ring->frame_max)
|
|
|
|
n -= ring->frame_max;
|
|
|
|
|
|
|
|
hdr = __netlink_lookup_frame(ring, n);
|
|
|
|
|
|
|
|
return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:02 +00:00
|
|
|
static unsigned int netlink_poll(struct file *file, struct socket *sock,
|
|
|
|
poll_table *wait)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
unsigned int mask;
|
2013-04-17 06:47:05 +00:00
|
|
|
int err;
|
2013-04-17 06:47:02 +00:00
|
|
|
|
2013-04-17 06:47:05 +00:00
|
|
|
if (nlk->rx_ring.pg_vec != NULL) {
|
|
|
|
/* Memory mapped sockets don't call recvmsg(), so flow control
|
|
|
|
* for dumps is performed here. A dump is allowed to continue
|
|
|
|
* if at least half the ring is unused.
|
|
|
|
*/
|
2013-08-15 22:31:06 +00:00
|
|
|
while (nlk->cb_running && netlink_dump_space(nlk)) {
|
2013-04-17 06:47:05 +00:00
|
|
|
err = netlink_dump(sk);
|
|
|
|
if (err < 0) {
|
|
|
|
sk->sk_err = err;
|
|
|
|
sk->sk_error_report(sk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
netlink_rcv_wake(sk);
|
|
|
|
}
|
2013-04-17 06:47:03 +00:00
|
|
|
|
2013-04-17 06:47:02 +00:00
|
|
|
mask = datagram_poll(file, sock, wait);
|
|
|
|
|
|
|
|
spin_lock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
if (nlk->rx_ring.pg_vec) {
|
|
|
|
netlink_forward_ring(&nlk->rx_ring);
|
|
|
|
if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
|
|
|
|
mask |= POLLIN | POLLRDNORM;
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
|
|
|
|
spin_lock_bh(&sk->sk_write_queue.lock);
|
|
|
|
if (nlk->tx_ring.pg_vec) {
|
|
|
|
if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
|
|
|
|
mask |= POLLOUT | POLLWRNORM;
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&sk->sk_write_queue.lock);
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
|
|
|
|
struct netlink_ring *ring,
|
|
|
|
struct nl_mmap_hdr *hdr)
|
|
|
|
{
|
|
|
|
unsigned int size;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
size = ring->frame_size - NL_MMAP_HDRLEN;
|
|
|
|
data = (void *)hdr + NL_MMAP_HDRLEN;
|
|
|
|
|
|
|
|
skb->head = data;
|
|
|
|
skb->data = data;
|
|
|
|
skb_reset_tail_pointer(skb);
|
|
|
|
skb->end = skb->tail + size;
|
|
|
|
skb->len = 0;
|
|
|
|
|
|
|
|
skb->destructor = netlink_skb_destructor;
|
|
|
|
NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
|
|
|
|
NETLINK_CB(skb).sk = sk;
|
|
|
|
}
|
2013-04-17 06:47:03 +00:00
|
|
|
|
|
|
|
static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
|
|
|
|
u32 dst_portid, u32 dst_group,
|
|
|
|
struct sock_iocb *siocb)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct netlink_ring *ring;
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
unsigned int maxlen;
|
|
|
|
bool excl = true;
|
|
|
|
int err = 0, len = 0;
|
|
|
|
|
|
|
|
/* Netlink messages are validated by the receiver before processing.
|
|
|
|
* In order to avoid userspace changing the contents of the message
|
|
|
|
* after validation, the socket and the ring may only be used by a
|
|
|
|
* single process, otherwise we fall back to copying.
|
|
|
|
*/
|
|
|
|
if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
|
|
|
|
atomic_read(&nlk->mapped) > 1)
|
|
|
|
excl = false;
|
|
|
|
|
|
|
|
mutex_lock(&nlk->pg_vec_lock);
|
|
|
|
|
|
|
|
ring = &nlk->tx_ring;
|
|
|
|
maxlen = ring->frame_size - NL_MMAP_HDRLEN;
|
|
|
|
|
|
|
|
do {
|
|
|
|
hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
|
|
|
|
if (hdr == NULL) {
|
|
|
|
if (!(msg->msg_flags & MSG_DONTWAIT) &&
|
|
|
|
atomic_read(&nlk->tx_ring.pending))
|
|
|
|
schedule();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (hdr->nm_len > maxlen) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
netlink_frame_flush_dcache(hdr);
|
|
|
|
|
|
|
|
if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
|
|
|
|
skb = alloc_skb_head(GFP_KERNEL);
|
|
|
|
if (skb == NULL) {
|
|
|
|
err = -ENOBUFS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sock_hold(sk);
|
|
|
|
netlink_ring_setup_skb(skb, sk, ring, hdr);
|
|
|
|
NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
|
|
|
|
__skb_put(skb, hdr->nm_len);
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
|
|
|
|
atomic_inc(&ring->pending);
|
|
|
|
} else {
|
|
|
|
skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
|
|
|
|
if (skb == NULL) {
|
|
|
|
err = -ENOBUFS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
__skb_put(skb, hdr->nm_len);
|
|
|
|
memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
netlink_increment_head(ring);
|
|
|
|
|
|
|
|
NETLINK_CB(skb).portid = nlk->portid;
|
|
|
|
NETLINK_CB(skb).dst_group = dst_group;
|
|
|
|
NETLINK_CB(skb).creds = siocb->scm->creds;
|
|
|
|
|
|
|
|
err = security_netlink_send(sk, skb);
|
|
|
|
if (err) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(dst_group)) {
|
|
|
|
atomic_inc(&skb->users);
|
|
|
|
netlink_broadcast(sk, skb, dst_portid, dst_group,
|
|
|
|
GFP_KERNEL);
|
|
|
|
}
|
|
|
|
err = netlink_unicast(sk, skb, dst_portid,
|
|
|
|
msg->msg_flags & MSG_DONTWAIT);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
len += err;
|
|
|
|
|
|
|
|
} while (hdr != NULL ||
|
|
|
|
(!(msg->msg_flags & MSG_DONTWAIT) &&
|
|
|
|
atomic_read(&nlk->tx_ring.pending)));
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
err = len;
|
|
|
|
out:
|
|
|
|
mutex_unlock(&nlk->pg_vec_lock);
|
|
|
|
return err;
|
|
|
|
}
|
2013-04-17 06:47:04 +00:00
|
|
|
|
|
|
|
static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
|
|
|
|
hdr = netlink_mmap_hdr(skb);
|
|
|
|
hdr->nm_len = skb->len;
|
|
|
|
hdr->nm_group = NETLINK_CB(skb).dst_group;
|
|
|
|
hdr->nm_pid = NETLINK_CB(skb).creds.pid;
|
2013-04-24 08:36:23 +00:00
|
|
|
hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
|
|
|
|
hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
|
2013-04-17 06:47:04 +00:00
|
|
|
netlink_frame_flush_dcache(hdr);
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
|
|
|
|
|
|
|
|
NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct netlink_ring *ring = &nlk->rx_ring;
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
|
|
|
|
spin_lock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
|
|
|
|
if (hdr == NULL) {
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
kfree_skb(skb);
|
2013-04-17 06:47:05 +00:00
|
|
|
netlink_overrun(sk);
|
2013-04-17 06:47:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
netlink_increment_head(ring);
|
|
|
|
__skb_queue_tail(&sk->sk_receive_queue, skb);
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
|
|
|
|
hdr->nm_len = skb->len;
|
|
|
|
hdr->nm_group = NETLINK_CB(skb).dst_group;
|
|
|
|
hdr->nm_pid = NETLINK_CB(skb).creds.pid;
|
2013-04-24 08:36:23 +00:00
|
|
|
hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
|
|
|
|
hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
|
2013-04-17 06:47:04 +00:00
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:01 +00:00
|
|
|
#else /* CONFIG_NETLINK_MMAP */
|
2013-04-17 06:47:02 +00:00
|
|
|
#define netlink_skb_is_mmaped(skb) false
|
2013-04-17 06:47:04 +00:00
|
|
|
#define netlink_rx_is_mmaped(sk) false
|
2013-04-17 06:47:03 +00:00
|
|
|
#define netlink_tx_is_mmaped(sk) false
|
2013-04-17 06:47:01 +00:00
|
|
|
#define netlink_mmap sock_no_mmap
|
2013-04-17 06:47:02 +00:00
|
|
|
#define netlink_poll datagram_poll
|
2013-04-17 06:47:03 +00:00
|
|
|
#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
|
2013-04-17 06:47:01 +00:00
|
|
|
#endif /* CONFIG_NETLINK_MMAP */
|
|
|
|
|
2013-04-17 06:47:00 +00:00
|
|
|
static void netlink_skb_destructor(struct sk_buff *skb)
|
|
|
|
{
|
2013-04-17 06:47:02 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
struct netlink_ring *ring;
|
|
|
|
struct sock *sk;
|
|
|
|
|
|
|
|
/* If a packet from the kernel to userspace was freed because of an
|
|
|
|
* error without being delivered to userspace, the kernel must reset
|
|
|
|
* the status. In the direction userspace to kernel, the status is
|
|
|
|
* always reset here after the packet was processed and freed.
|
|
|
|
*/
|
|
|
|
if (netlink_skb_is_mmaped(skb)) {
|
|
|
|
hdr = netlink_mmap_hdr(skb);
|
|
|
|
sk = NETLINK_CB(skb).sk;
|
|
|
|
|
2013-04-17 06:47:03 +00:00
|
|
|
if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
|
|
|
|
ring = &nlk_sk(sk)->tx_ring;
|
|
|
|
} else {
|
|
|
|
if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
|
|
|
|
hdr->nm_len = 0;
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
|
|
|
|
}
|
|
|
|
ring = &nlk_sk(sk)->rx_ring;
|
2013-04-17 06:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(atomic_read(&ring->pending) == 0);
|
|
|
|
atomic_dec(&ring->pending);
|
|
|
|
sock_put(sk);
|
|
|
|
|
2013-06-03 09:28:43 +00:00
|
|
|
skb->head = NULL;
|
2013-04-17 06:47:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-06-03 09:46:28 +00:00
|
|
|
if (is_vmalloc_addr(skb->head)) {
|
2013-06-28 01:04:23 +00:00
|
|
|
if (!skb->cloned ||
|
|
|
|
!atomic_dec_return(&(skb_shinfo(skb)->dataref)))
|
|
|
|
vfree(skb->head);
|
|
|
|
|
2013-06-03 09:46:28 +00:00
|
|
|
skb->head = NULL;
|
|
|
|
}
|
2013-04-17 06:47:02 +00:00
|
|
|
if (skb->sk != NULL)
|
|
|
|
sock_rfree(skb);
|
2013-04-17 06:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
|
|
|
|
{
|
|
|
|
WARN_ON(skb->sk != NULL);
|
|
|
|
skb->sk = sk;
|
|
|
|
skb->destructor = netlink_skb_destructor;
|
|
|
|
atomic_add(skb->truesize, &sk->sk_rmem_alloc);
|
|
|
|
sk_mem_charge(sk, skb->truesize);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void netlink_sock_destruct(struct sock *sk)
|
|
|
|
{
|
2007-05-03 10:17:14 +00:00
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
if (nlk->cb_running) {
|
|
|
|
if (nlk->cb.done)
|
|
|
|
nlk->cb.done(&nlk->cb);
|
2012-10-04 20:15:48 +00:00
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
module_put(nlk->cb.module);
|
|
|
|
kfree_skb(nlk->cb.skb);
|
2007-05-03 10:17:14 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
skb_queue_purge(&sk->sk_receive_queue);
|
2013-04-17 06:47:01 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
if (1) {
|
|
|
|
struct nl_mmap_req req;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if (nlk->rx_ring.pg_vec)
|
|
|
|
netlink_set_ring(sk, &req, true, false);
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if (nlk->tx_ring.pg_vec)
|
|
|
|
netlink_set_ring(sk, &req, true, true);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETLINK_MMAP */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD)) {
|
2007-12-04 08:19:38 +00:00
|
|
|
printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-07-26 04:43:18 +00:00
|
|
|
|
|
|
|
WARN_ON(atomic_read(&sk->sk_rmem_alloc));
|
|
|
|
WARN_ON(atomic_read(&sk->sk_wmem_alloc));
|
|
|
|
WARN_ON(nlk_sk(sk)->groups);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
|
|
|
|
* SMP. Look, when several writers sleep and reader wakes them up, all but one
|
2005-04-16 22:20:36 +00:00
|
|
|
* immediately hit write lock and grab all the cpus. Exclusive sleep solves
|
|
|
|
* this, _but_ remember, it adds useless work on UP machines.
|
|
|
|
*/
|
|
|
|
|
2009-09-12 03:03:15 +00:00
|
|
|
void netlink_table_grab(void)
|
2008-01-02 05:58:02 +00:00
|
|
|
__acquires(nl_table_lock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-09-12 03:03:15 +00:00
|
|
|
might_sleep();
|
|
|
|
|
2006-07-03 07:24:07 +00:00
|
|
|
write_lock_irq(&nl_table_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (atomic_read(&nl_table_users)) {
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
|
|
|
|
add_wait_queue_exclusive(&nl_table_wait, &wait);
|
2007-12-04 08:19:38 +00:00
|
|
|
for (;;) {
|
2005-04-16 22:20:36 +00:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
if (atomic_read(&nl_table_users) == 0)
|
|
|
|
break;
|
2006-07-03 07:24:07 +00:00
|
|
|
write_unlock_irq(&nl_table_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
schedule();
|
2006-07-03 07:24:07 +00:00
|
|
|
write_lock_irq(&nl_table_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&nl_table_wait, &wait);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-12 03:03:15 +00:00
|
|
|
void netlink_table_ungrab(void)
|
2008-01-02 05:58:02 +00:00
|
|
|
__releases(nl_table_lock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-07-03 07:24:07 +00:00
|
|
|
write_unlock_irq(&nl_table_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
wake_up(&nl_table_wait);
|
|
|
|
}
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
static inline void
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_lock_table(void)
|
|
|
|
{
|
|
|
|
/* read_lock() synchronizes us to netlink_table_grab */
|
|
|
|
|
|
|
|
read_lock(&nl_table_lock);
|
|
|
|
atomic_inc(&nl_table_users);
|
|
|
|
read_unlock(&nl_table_lock);
|
|
|
|
}
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
static inline void
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_unlock_table(void)
|
|
|
|
{
|
|
|
|
if (atomic_dec_and_test(&nl_table_users))
|
|
|
|
wake_up(&nl_table_wait);
|
|
|
|
}
|
|
|
|
|
2013-06-06 06:49:11 +00:00
|
|
|
static bool netlink_compare(struct net *net, struct sock *sk)
|
|
|
|
{
|
|
|
|
return net_eq(sock_net(sk), net);
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-06-06 06:49:11 +00:00
|
|
|
struct netlink_table *table = &nl_table[protocol];
|
|
|
|
struct nl_portid_hash *hash = &table->hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct hlist_head *head;
|
|
|
|
struct sock *sk;
|
|
|
|
|
|
|
|
read_lock(&nl_table_lock);
|
2012-09-07 20:12:54 +00:00
|
|
|
head = nl_portid_hashfn(hash, portid);
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each(sk, head) {
|
2013-06-06 06:49:11 +00:00
|
|
|
if (table->compare(net, sk) &&
|
|
|
|
(nlk_sk(sk)->portid == portid)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_hold(sk);
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sk = NULL;
|
|
|
|
found:
|
|
|
|
read_unlock(&nl_table_lock);
|
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static struct hlist_head *nl_portid_hash_zalloc(size_t size)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
if (size <= PAGE_SIZE)
|
2007-12-11 10:09:47 +00:00
|
|
|
return kzalloc(size, GFP_ATOMIC);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
|
|
|
return (struct hlist_head *)
|
2007-12-11 10:09:47 +00:00
|
|
|
__get_free_pages(GFP_ATOMIC | __GFP_ZERO,
|
|
|
|
get_order(size));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static void nl_portid_hash_free(struct hlist_head *table, size_t size)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
if (size <= PAGE_SIZE)
|
|
|
|
kfree(table);
|
|
|
|
else
|
|
|
|
free_pages((unsigned long)table, get_order(size));
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
unsigned int omask, mask, shift;
|
|
|
|
size_t osize, size;
|
|
|
|
struct hlist_head *otable, *table;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
omask = mask = hash->mask;
|
|
|
|
osize = size = (mask + 1) * sizeof(*table);
|
|
|
|
shift = hash->shift;
|
|
|
|
|
|
|
|
if (grow) {
|
|
|
|
if (++shift > hash->max_shift)
|
|
|
|
return 0;
|
|
|
|
mask = mask * 2 + 1;
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
table = nl_portid_hash_zalloc(size);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!table)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
otable = hash->table;
|
|
|
|
hash->table = table;
|
|
|
|
hash->mask = mask;
|
|
|
|
hash->shift = shift;
|
|
|
|
get_random_bytes(&hash->rnd, sizeof(hash->rnd));
|
|
|
|
|
|
|
|
for (i = 0; i <= omask; i++) {
|
|
|
|
struct sock *sk;
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
struct hlist_node *tmp;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each_safe(sk, tmp, &otable[i])
|
2012-09-07 20:12:54 +00:00
|
|
|
__sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
nl_portid_hash_free(otable, osize);
|
2005-04-16 22:20:36 +00:00
|
|
|
hash->rehash_time = jiffies + 10 * 60 * HZ;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int avg = hash->entries >> hash->shift;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
|
2005-04-16 22:20:36 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
|
2012-09-07 20:12:54 +00:00
|
|
|
nl_portid_hash_rehash(hash, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-22 20:49:22 +00:00
|
|
|
static const struct proto_ops netlink_ops;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-03-21 02:52:01 +00:00
|
|
|
static void
|
|
|
|
netlink_update_listeners(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct netlink_table *tbl = &nl_table[sk->sk_protocol];
|
|
|
|
unsigned long mask;
|
|
|
|
unsigned int i;
|
2012-10-18 03:21:55 +00:00
|
|
|
struct listeners *listeners;
|
|
|
|
|
|
|
|
listeners = nl_deref_protected(tbl->listeners);
|
|
|
|
if (!listeners)
|
|
|
|
return;
|
2006-03-21 02:52:01 +00:00
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
|
2006-03-21 02:52:01 +00:00
|
|
|
mask = 0;
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each_bound(sk, &tbl->mc_list) {
|
2007-07-18 22:46:06 +00:00
|
|
|
if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
|
|
|
|
mask |= nlk_sk(sk)->groups[i];
|
|
|
|
}
|
2012-10-18 03:21:55 +00:00
|
|
|
listeners->masks[i] = mask;
|
2006-03-21 02:52:01 +00:00
|
|
|
}
|
|
|
|
/* this function is only called with the netlink table "grabbed", which
|
|
|
|
* makes sure updates are visible before bind or setsockopt return. */
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-06-06 06:49:11 +00:00
|
|
|
struct netlink_table *table = &nl_table[sk->sk_protocol];
|
|
|
|
struct nl_portid_hash *hash = &table->hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct hlist_head *head;
|
|
|
|
int err = -EADDRINUSE;
|
|
|
|
struct sock *osk;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
netlink_table_grab();
|
2012-09-07 20:12:54 +00:00
|
|
|
head = nl_portid_hashfn(hash, portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
len = 0;
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each(osk, head) {
|
2013-06-06 06:49:11 +00:00
|
|
|
if (table->compare(net, osk) &&
|
|
|
|
(nlk_sk(osk)->portid == portid))
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
len++;
|
|
|
|
}
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
if (osk)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = -EBUSY;
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nlk_sk(sk)->portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
|
|
|
|
goto err;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (len && nl_portid_hash_dilute(hash, len))
|
|
|
|
head = nl_portid_hashfn(hash, portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
hash->entries++;
|
2012-09-07 20:12:54 +00:00
|
|
|
nlk_sk(sk)->portid = portid;
|
2005-04-16 22:20:36 +00:00
|
|
|
sk_add_node(sk, head);
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
netlink_table_ungrab();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_remove(struct sock *sk)
|
|
|
|
{
|
|
|
|
netlink_table_grab();
|
[NETLINK]: Fix two socket hashing bugs.
1) netlink_release() should only decrement the hash entry
count if the socket was actually hashed.
This was causing hash->entries to underflow, which
resulting in all kinds of troubles.
On 64-bit systems, this would cause the following
conditional to erroneously trigger:
err = -ENOMEM;
if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
goto err;
2) netlink_autobind() needs to propagate the error return from
netlink_insert(). Otherwise, callers will not see the error
as they should and thus try to operate on a socket with a zero pid,
which is very bad.
However, it should not propagate -EBUSY. If two threads race
to autobind the socket, that is fine. This is consistent with the
autobind behavior in other protocols.
So bug #1 above, combined with this one, resulted in hangs
on netlink_sendmsg() calls to the rtnetlink socket. We'd try
to do the user sendmsg() with the socket's pid set to zero,
later we do a socket lookup using that pid (via the value we
stashed away in NETLINK_CB(skb).pid), but that won't give us the
user socket, it will give us the rtnetlink socket. So when we
try to wake up the receive queue, we dive back into rtnetlink_rcv()
which tries to recursively take the rtnetlink semaphore.
Thanks to Jakub Jelink for providing backtraces. Also, thanks to
Herbert Xu for supplying debugging patches to help track this down,
and also finding a mistake in an earlier version of this fix.
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-06-26 22:31:51 +00:00
|
|
|
if (sk_del_node_init(sk))
|
|
|
|
nl_table[sk->sk_protocol].hash.entries--;
|
2005-08-15 19:29:13 +00:00
|
|
|
if (nlk_sk(sk)->subscriptions)
|
2005-04-16 22:20:36 +00:00
|
|
|
__sk_del_bind_node(sk);
|
|
|
|
netlink_table_ungrab();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto netlink_proto = {
|
|
|
|
.name = "NETLINK",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.obj_size = sizeof(struct netlink_sock),
|
|
|
|
};
|
|
|
|
|
2007-10-09 06:24:22 +00:00
|
|
|
static int __netlink_create(struct net *net, struct socket *sock,
|
|
|
|
struct mutex *cb_mutex, int protocol)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
struct netlink_sock *nlk;
|
2005-08-15 02:31:36 +00:00
|
|
|
|
|
|
|
sock->ops = &netlink_ops;
|
|
|
|
|
2007-11-01 07:39:31 +00:00
|
|
|
sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
|
2005-08-15 02:31:36 +00:00
|
|
|
if (!sk)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
sock_init_data(sock, sk);
|
|
|
|
|
|
|
|
nlk = nlk_sk(sk);
|
2012-04-22 21:30:21 +00:00
|
|
|
if (cb_mutex) {
|
2007-04-25 21:01:17 +00:00
|
|
|
nlk->cb_mutex = cb_mutex;
|
2012-04-22 21:30:21 +00:00
|
|
|
} else {
|
2007-04-25 21:01:17 +00:00
|
|
|
nlk->cb_mutex = &nlk->cb_def_mutex;
|
|
|
|
mutex_init(nlk->cb_mutex);
|
|
|
|
}
|
2005-08-15 02:31:36 +00:00
|
|
|
init_waitqueue_head(&nlk->wait);
|
2013-04-17 06:47:01 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
mutex_init(&nlk->pg_vec_lock);
|
|
|
|
#endif
|
2005-08-15 02:31:36 +00:00
|
|
|
|
|
|
|
sk->sk_destruct = netlink_sock_destruct;
|
|
|
|
sk->sk_protocol = protocol;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-06 06:18:14 +00:00
|
|
|
static int netlink_create(struct net *net, struct socket *sock, int protocol,
|
|
|
|
int kern)
|
2005-08-15 02:31:36 +00:00
|
|
|
{
|
|
|
|
struct module *module = NULL;
|
2007-04-20 21:14:21 +00:00
|
|
|
struct mutex *cb_mutex;
|
2005-08-15 19:29:13 +00:00
|
|
|
struct netlink_sock *nlk;
|
2014-04-23 01:31:54 +00:00
|
|
|
int (*bind)(int group);
|
|
|
|
void (*unbind)(int group);
|
2005-08-15 02:31:36 +00:00
|
|
|
int err = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
sock->state = SS_UNCONNECTED;
|
|
|
|
|
|
|
|
if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
|
|
|
|
return -ESOCKTNOSUPPORT;
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
if (protocol < 0 || protocol >= MAX_LINKS)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
|
2005-08-15 02:27:13 +00:00
|
|
|
netlink_lock_table();
|
2008-10-16 22:24:51 +00:00
|
|
|
#ifdef CONFIG_MODULES
|
2005-08-15 02:31:36 +00:00
|
|
|
if (!nl_table[protocol].registered) {
|
2005-08-15 02:27:13 +00:00
|
|
|
netlink_unlock_table();
|
2005-08-10 02:40:55 +00:00
|
|
|
request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
|
2005-08-15 02:27:13 +00:00
|
|
|
netlink_lock_table();
|
2005-08-10 02:40:55 +00:00
|
|
|
}
|
2005-08-15 02:31:36 +00:00
|
|
|
#endif
|
|
|
|
if (nl_table[protocol].registered &&
|
|
|
|
try_module_get(nl_table[protocol].module))
|
|
|
|
module = nl_table[protocol].module;
|
2010-01-30 10:05:05 +00:00
|
|
|
else
|
|
|
|
err = -EPROTONOSUPPORT;
|
2007-04-20 21:14:21 +00:00
|
|
|
cb_mutex = nl_table[protocol].cb_mutex;
|
2012-06-29 06:15:22 +00:00
|
|
|
bind = nl_table[protocol].bind;
|
2014-04-23 01:31:54 +00:00
|
|
|
unbind = nl_table[protocol].unbind;
|
2005-08-15 02:27:13 +00:00
|
|
|
netlink_unlock_table();
|
2005-08-10 02:40:55 +00:00
|
|
|
|
2010-01-30 10:05:05 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
err = __netlink_create(net, sock, cb_mutex, protocol);
|
|
|
|
if (err < 0)
|
2005-08-15 19:29:13 +00:00
|
|
|
goto out_module;
|
|
|
|
|
2008-11-24 01:34:03 +00:00
|
|
|
local_bh_disable();
|
2008-11-23 23:48:22 +00:00
|
|
|
sock_prot_inuse_add(net, &netlink_proto, 1);
|
2008-11-24 01:34:03 +00:00
|
|
|
local_bh_enable();
|
|
|
|
|
2005-08-15 19:29:13 +00:00
|
|
|
nlk = nlk_sk(sock->sk);
|
|
|
|
nlk->module = module;
|
2012-06-29 06:15:22 +00:00
|
|
|
nlk->netlink_bind = bind;
|
2014-04-23 01:31:54 +00:00
|
|
|
nlk->netlink_unbind = unbind;
|
2005-08-15 02:31:36 +00:00
|
|
|
out:
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-08-15 02:31:36 +00:00
|
|
|
out_module:
|
|
|
|
module_put(module);
|
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_release(struct socket *sock)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk;
|
|
|
|
|
|
|
|
if (!sk)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
netlink_remove(sk);
|
2007-04-19 00:05:58 +00:00
|
|
|
sock_orphan(sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
nlk = nlk_sk(sk);
|
|
|
|
|
2007-05-03 10:17:14 +00:00
|
|
|
/*
|
|
|
|
* OK. Socket is unlinked, any packets that arrive now
|
|
|
|
* will be purged.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
sock->sk = NULL;
|
|
|
|
wake_up_interruptible_all(&nlk->wait);
|
|
|
|
|
|
|
|
skb_queue_purge(&sk->sk_write_queue);
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nlk->portid) {
|
2005-04-16 22:20:36 +00:00
|
|
|
struct netlink_notify n = {
|
2008-03-25 17:26:21 +00:00
|
|
|
.net = sock_net(sk),
|
2005-04-16 22:20:36 +00:00
|
|
|
.protocol = sk->sk_protocol,
|
2012-09-07 20:12:54 +00:00
|
|
|
.portid = nlk->portid,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no
protection against entries being added to or removed from a chain while the
chain is in use. The issues were discussed in this thread:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2
We noticed that notifier chains in the kernel fall into two basic usage
classes:
"Blocking" chains are always called from a process context
and the callout routines are allowed to sleep;
"Atomic" chains can be called from an atomic context and
the callout routines are not allowed to sleep.
We decided to codify this distinction and make it part of the API. Therefore
this set of patches introduces three new, parallel APIs: one for blocking
notifiers, one for atomic notifiers, and one for "raw" notifiers (which is
really just the old API under a new name). New kinds of data structures are
used for the heads of the chains, and new routines are defined for
registration, unregistration, and calling a chain. The three APIs are
explained in include/linux/notifier.h and their implementation is in
kernel/sys.c.
With atomic and blocking chains, the implementation guarantees that the chain
links will not be corrupted and that chain callers will not get messed up by
entries being added or removed. For raw chains the implementation provides no
guarantees at all; users of this API must provide their own protections. (The
idea was that situations may come up where the assumptions of the atomic and
blocking APIs are not appropriate, so it should be possible for users to
handle these things in their own way.)
There are some limitations, which should not be too hard to live with. For
atomic/blocking chains, registration and unregistration must always be done in
a process context since the chain is protected by a mutex/rwsem. Also, a
callout routine for a non-raw chain must not try to register or unregister
entries on its own chain. (This did happen in a couple of places and the code
had to be changed to avoid it.)
Since atomic chains may be called from within an NMI handler, they cannot use
spinlocks for synchronization. Instead we use RCU. The overhead falls almost
entirely in the unregister routine, which is okay since unregistration is much
less frequent that calling a chain.
Here is the list of chains that we adjusted and their classifications. None
of them use the raw API, so for the moment it is only a placeholder.
ATOMIC CHAINS
-------------
arch/i386/kernel/traps.c: i386die_chain
arch/ia64/kernel/traps.c: ia64die_chain
arch/powerpc/kernel/traps.c: powerpc_die_chain
arch/sparc64/kernel/traps.c: sparc64die_chain
arch/x86_64/kernel/traps.c: die_chain
drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list
kernel/panic.c: panic_notifier_list
kernel/profile.c: task_free_notifier
net/bluetooth/hci_core.c: hci_notifier
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain
net/ipv6/addrconf.c: inet6addr_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain
net/netlink/af_netlink.c: netlink_chain
BLOCKING CHAINS
---------------
arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain
arch/s390/kernel/process.c: idle_chain
arch/x86_64/kernel/process.c idle_notifier
drivers/base/memory.c: memory_chain
drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list
drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list
drivers/macintosh/adb.c: adb_client_list
drivers/macintosh/via-pmu.c sleep_notifier_list
drivers/macintosh/via-pmu68k.c sleep_notifier_list
drivers/macintosh/windfarm_core.c wf_client_list
drivers/usb/core/notify.c usb_notifier_list
drivers/video/fbmem.c fb_notifier_list
kernel/cpu.c cpu_chain
kernel/module.c module_notify_list
kernel/profile.c munmap_notifier
kernel/profile.c task_exit_notifier
kernel/sys.c reboot_notifier_list
net/core/dev.c netdev_chain
net/decnet/dn_dev.c: dnaddr_chain
net/ipv4/devinet.c: inetaddr_chain
It's possible that some of these classifications are wrong. If they are,
please let us know or submit a patch to fix them. Note that any chain that
gets called very frequently should be atomic, because the rwsem read-locking
used for blocking chains is very likely to incur cache misses on SMP systems.
(However, if the chain's callout routines may sleep then the chain cannot be
atomic.)
The patch set was written by Alan Stern and Chandra Seetharaman, incorporating
material written by Keith Owens and suggestions from Paul McKenney and Andrew
Morton.
[jes@sgi.com: restructure the notifier chain initialization macros]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-03-27 09:16:30 +00:00
|
|
|
atomic_notifier_call_chain(&netlink_chain,
|
|
|
|
NETLINK_URELEASE, &n);
|
2007-02-09 14:25:07 +00:00
|
|
|
}
|
2005-08-10 02:40:55 +00:00
|
|
|
|
2007-01-02 23:24:30 +00:00
|
|
|
module_put(nlk->module);
|
2005-08-10 02:40:55 +00:00
|
|
|
|
2006-03-21 02:52:01 +00:00
|
|
|
netlink_table_grab();
|
2007-10-11 04:14:32 +00:00
|
|
|
if (netlink_is_kernel(sk)) {
|
2008-01-19 07:53:31 +00:00
|
|
|
BUG_ON(nl_table[sk->sk_protocol].registered == 0);
|
|
|
|
if (--nl_table[sk->sk_protocol].registered == 0) {
|
2012-10-18 03:21:55 +00:00
|
|
|
struct listeners *old;
|
|
|
|
|
|
|
|
old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
|
|
|
|
RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
|
|
|
|
kfree_rcu(old, rcu);
|
2008-01-19 07:53:31 +00:00
|
|
|
nl_table[sk->sk_protocol].module = NULL;
|
2012-09-08 02:53:53 +00:00
|
|
|
nl_table[sk->sk_protocol].bind = NULL;
|
2014-04-23 01:31:54 +00:00
|
|
|
nl_table[sk->sk_protocol].unbind = NULL;
|
2012-09-08 02:53:53 +00:00
|
|
|
nl_table[sk->sk_protocol].flags = 0;
|
2008-01-19 07:53:31 +00:00
|
|
|
nl_table[sk->sk_protocol].registered = 0;
|
|
|
|
}
|
2012-04-22 21:30:21 +00:00
|
|
|
} else if (nlk->subscriptions) {
|
2006-03-21 02:52:01 +00:00
|
|
|
netlink_update_listeners(sk);
|
2012-04-22 21:30:21 +00:00
|
|
|
}
|
2006-03-21 02:52:01 +00:00
|
|
|
netlink_table_ungrab();
|
2005-08-15 02:27:13 +00:00
|
|
|
|
2005-08-15 19:29:13 +00:00
|
|
|
kfree(nlk->groups);
|
|
|
|
nlk->groups = NULL;
|
|
|
|
|
2008-11-24 22:05:22 +00:00
|
|
|
local_bh_disable();
|
2008-11-23 23:48:22 +00:00
|
|
|
sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
|
2008-11-24 22:05:22 +00:00
|
|
|
local_bh_enable();
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_put(sk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_autobind(struct socket *sock)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
2008-03-25 17:26:21 +00:00
|
|
|
struct net *net = sock_net(sk);
|
2013-06-06 06:49:11 +00:00
|
|
|
struct netlink_table *table = &nl_table[sk->sk_protocol];
|
|
|
|
struct nl_portid_hash *hash = &table->hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct hlist_head *head;
|
|
|
|
struct sock *osk;
|
2012-09-07 20:12:54 +00:00
|
|
|
s32 portid = task_tgid_vnr(current);
|
2005-04-16 22:20:36 +00:00
|
|
|
int err;
|
|
|
|
static s32 rover = -4097;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
cond_resched();
|
|
|
|
netlink_table_grab();
|
2012-09-07 20:12:54 +00:00
|
|
|
head = nl_portid_hashfn(hash, portid);
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each(osk, head) {
|
2013-06-06 06:49:11 +00:00
|
|
|
if (!table->compare(net, osk))
|
2007-09-12 11:05:38 +00:00
|
|
|
continue;
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nlk_sk(osk)->portid == portid) {
|
|
|
|
/* Bind collision, search negative portid values. */
|
|
|
|
portid = rover--;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (rover > -4097)
|
|
|
|
rover = -4097;
|
|
|
|
netlink_table_ungrab();
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
netlink_table_ungrab();
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
err = netlink_insert(sk, net, portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err == -EADDRINUSE)
|
|
|
|
goto retry;
|
[NETLINK]: Fix two socket hashing bugs.
1) netlink_release() should only decrement the hash entry
count if the socket was actually hashed.
This was causing hash->entries to underflow, which
resulting in all kinds of troubles.
On 64-bit systems, this would cause the following
conditional to erroneously trigger:
err = -ENOMEM;
if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
goto err;
2) netlink_autobind() needs to propagate the error return from
netlink_insert(). Otherwise, callers will not see the error
as they should and thus try to operate on a socket with a zero pid,
which is very bad.
However, it should not propagate -EBUSY. If two threads race
to autobind the socket, that is fine. This is consistent with the
autobind behavior in other protocols.
So bug #1 above, combined with this one, resulted in hangs
on netlink_sendmsg() calls to the rtnetlink socket. We'd try
to do the user sendmsg() with the socket's pid set to zero,
later we do a socket lookup using that pid (via the value we
stashed away in NETLINK_CB(skb).pid), but that won't give us the
user socket, it will give us the rtnetlink socket. So when we
try to wake up the receive queue, we dive back into rtnetlink_rcv()
which tries to recursively take the rtnetlink semaphore.
Thanks to Jakub Jelink for providing backtraces. Also, thanks to
Herbert Xu for supplying debugging patches to help track this down,
and also finding a mistake in an earlier version of this fix.
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-06-26 22:31:51 +00:00
|
|
|
|
|
|
|
/* If 2 threads race to autobind, that is fine. */
|
|
|
|
if (err == -EBUSY)
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 21:28:03 +00:00
|
|
|
/**
|
|
|
|
* __netlink_ns_capable - General netlink message capability test
|
|
|
|
* @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
|
|
|
|
* @user_ns: The user namespace of the capability to use
|
|
|
|
* @cap: The capability to use
|
|
|
|
*
|
|
|
|
* Test to see if the opener of the socket we received the message
|
|
|
|
* from had when the netlink socket was created and the sender of the
|
|
|
|
* message has has the capability @cap in the user namespace @user_ns.
|
|
|
|
*/
|
|
|
|
bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
|
|
|
|
struct user_namespace *user_ns, int cap)
|
|
|
|
{
|
2014-05-30 18:04:00 +00:00
|
|
|
return ((nsp->flags & NETLINK_SKB_DST) ||
|
|
|
|
file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
|
|
|
|
ns_capable(user_ns, cap);
|
2014-04-23 21:28:03 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__netlink_ns_capable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netlink_ns_capable - General netlink message capability test
|
|
|
|
* @skb: socket buffer holding a netlink command from userspace
|
|
|
|
* @user_ns: The user namespace of the capability to use
|
|
|
|
* @cap: The capability to use
|
|
|
|
*
|
|
|
|
* Test to see if the opener of the socket we received the message
|
|
|
|
* from had when the netlink socket was created and the sender of the
|
|
|
|
* message has has the capability @cap in the user namespace @user_ns.
|
|
|
|
*/
|
|
|
|
bool netlink_ns_capable(const struct sk_buff *skb,
|
|
|
|
struct user_namespace *user_ns, int cap)
|
|
|
|
{
|
|
|
|
return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(netlink_ns_capable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netlink_capable - Netlink global message capability test
|
|
|
|
* @skb: socket buffer holding a netlink command from userspace
|
|
|
|
* @cap: The capability to use
|
|
|
|
*
|
|
|
|
* Test to see if the opener of the socket we received the message
|
|
|
|
* from had when the netlink socket was created and the sender of the
|
|
|
|
* message has has the capability @cap in all user namespaces.
|
|
|
|
*/
|
|
|
|
bool netlink_capable(const struct sk_buff *skb, int cap)
|
|
|
|
{
|
|
|
|
return netlink_ns_capable(skb, &init_user_ns, cap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(netlink_capable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netlink_net_capable - Netlink network namespace message capability test
|
|
|
|
* @skb: socket buffer holding a netlink command from userspace
|
|
|
|
* @cap: The capability to use
|
|
|
|
*
|
|
|
|
* Test to see if the opener of the socket we received the message
|
|
|
|
* from had when the netlink socket was created and the sender of the
|
|
|
|
* message has has the capability @cap over the network namespace of
|
|
|
|
* the socket we received the message from.
|
|
|
|
*/
|
|
|
|
bool netlink_net_capable(const struct sk_buff *skb, int cap)
|
|
|
|
{
|
|
|
|
return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(netlink_net_capable);
|
|
|
|
|
2014-04-23 21:25:48 +00:00
|
|
|
static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
|
2007-02-09 14:25:07 +00:00
|
|
|
{
|
2012-09-08 02:53:53 +00:00
|
|
|
return (nl_table[sock->sk->sk_protocol].flags & flag) ||
|
2012-11-16 03:03:07 +00:00
|
|
|
ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
|
2007-02-09 14:25:07 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-08-15 19:29:13 +00:00
|
|
|
static void
|
|
|
|
netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
|
|
|
if (nlk->subscriptions && !subscriptions)
|
|
|
|
__sk_del_bind_node(sk);
|
|
|
|
else if (!nlk->subscriptions && subscriptions)
|
|
|
|
sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
|
|
|
|
nlk->subscriptions = subscriptions;
|
|
|
|
}
|
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
static int netlink_realloc_groups(struct sock *sk)
|
2005-09-06 22:43:59 +00:00
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
unsigned int groups;
|
2007-07-18 22:46:06 +00:00
|
|
|
unsigned long *new_groups;
|
2005-09-06 22:43:59 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
netlink_table_grab();
|
|
|
|
|
2005-09-06 22:43:59 +00:00
|
|
|
groups = nl_table[sk->sk_protocol].groups;
|
2007-07-18 22:46:06 +00:00
|
|
|
if (!nl_table[sk->sk_protocol].registered) {
|
2005-09-06 22:43:59 +00:00
|
|
|
err = -ENOENT;
|
2007-07-18 22:46:06 +00:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2005-09-06 22:43:59 +00:00
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
if (nlk->ngroups >= groups)
|
|
|
|
goto out_unlock;
|
2005-09-06 22:43:59 +00:00
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
|
|
|
|
if (new_groups == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
|
2007-07-18 22:46:06 +00:00
|
|
|
NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
|
|
|
|
|
|
|
|
nlk->groups = new_groups;
|
2005-09-06 22:43:59 +00:00
|
|
|
nlk->ngroups = groups;
|
2007-07-18 22:46:06 +00:00
|
|
|
out_unlock:
|
|
|
|
netlink_table_ungrab();
|
|
|
|
return err;
|
2005-09-06 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 01:31:54 +00:00
|
|
|
static void netlink_unbind(int group, long unsigned int groups,
|
|
|
|
struct netlink_sock *nlk)
|
|
|
|
{
|
|
|
|
int undo;
|
|
|
|
|
|
|
|
if (!nlk->netlink_unbind)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (undo = 0; undo < group; undo++)
|
|
|
|
if (test_bit(group, &groups))
|
|
|
|
nlk->netlink_unbind(undo);
|
|
|
|
}
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
static int netlink_bind(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int addr_len)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
2008-03-25 17:26:21 +00:00
|
|
|
struct net *net = sock_net(sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
|
|
|
|
int err;
|
2014-04-23 01:31:54 +00:00
|
|
|
long unsigned int groups = nladdr->nl_groups;
|
2007-02-09 14:25:07 +00:00
|
|
|
|
2012-12-15 15:42:19 +00:00
|
|
|
if (addr_len < sizeof(struct sockaddr_nl))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (nladdr->nl_family != AF_NETLINK)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Only superuser is allowed to listen multicasts */
|
2014-04-23 01:31:54 +00:00
|
|
|
if (groups) {
|
2014-04-23 21:25:48 +00:00
|
|
|
if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
|
2005-09-06 22:43:59 +00:00
|
|
|
return -EPERM;
|
2007-07-18 22:46:06 +00:00
|
|
|
err = netlink_realloc_groups(sk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-09-06 22:43:59 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-04-23 01:31:54 +00:00
|
|
|
if (nlk->portid)
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nladdr->nl_pid != nlk->portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
2014-04-23 01:31:54 +00:00
|
|
|
|
|
|
|
if (nlk->netlink_bind && groups) {
|
|
|
|
int group;
|
|
|
|
|
|
|
|
for (group = 0; group < nlk->ngroups; group++) {
|
|
|
|
if (!test_bit(group, &groups))
|
|
|
|
continue;
|
|
|
|
err = nlk->netlink_bind(group);
|
|
|
|
if (!err)
|
|
|
|
continue;
|
|
|
|
netlink_unbind(group, groups, nlk);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nlk->portid) {
|
2005-04-16 22:20:36 +00:00
|
|
|
err = nladdr->nl_pid ?
|
2007-09-12 11:05:38 +00:00
|
|
|
netlink_insert(sk, net, nladdr->nl_pid) :
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_autobind(sock);
|
2014-04-23 01:31:54 +00:00
|
|
|
if (err) {
|
|
|
|
netlink_unbind(nlk->ngroups - 1, groups, nlk);
|
2005-04-16 22:20:36 +00:00
|
|
|
return err;
|
2014-04-23 01:31:54 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 01:31:54 +00:00
|
|
|
if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
netlink_table_grab();
|
2005-08-15 19:29:13 +00:00
|
|
|
netlink_update_subscriptions(sk, nlk->subscriptions +
|
2014-04-23 01:31:54 +00:00
|
|
|
hweight32(groups) -
|
2007-02-09 14:25:07 +00:00
|
|
|
hweight32(nlk->groups[0]));
|
2014-04-23 01:31:54 +00:00
|
|
|
nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
|
2006-03-21 02:52:01 +00:00
|
|
|
netlink_update_listeners(sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_table_ungrab();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_connect(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int alen, int flags)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
2007-12-04 08:19:38 +00:00
|
|
|
struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-03-31 22:58:26 +00:00
|
|
|
if (alen < sizeof(addr->sa_family))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (addr->sa_family == AF_UNSPEC) {
|
|
|
|
sk->sk_state = NETLINK_UNCONNECTED;
|
2012-09-07 20:12:54 +00:00
|
|
|
nlk->dst_portid = 0;
|
2005-08-15 02:27:50 +00:00
|
|
|
nlk->dst_group = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (addr->sa_family != AF_NETLINK)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-02-24 20:11:16 +00:00
|
|
|
if ((nladdr->nl_groups || nladdr->nl_pid) &&
|
2014-04-23 21:25:48 +00:00
|
|
|
!netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EPERM;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (!nlk->portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
err = netlink_autobind(sock);
|
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
sk->sk_state = NETLINK_CONNECTED;
|
2012-09-07 20:12:54 +00:00
|
|
|
nlk->dst_portid = nladdr->nl_pid;
|
2005-08-15 02:27:50 +00:00
|
|
|
nlk->dst_group = ffs(nladdr->nl_groups);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
static int netlink_getname(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int *addr_len, int peer)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
2009-11-08 05:51:19 +00:00
|
|
|
DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
|
2007-02-09 14:25:07 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
nladdr->nl_family = AF_NETLINK;
|
|
|
|
nladdr->nl_pad = 0;
|
|
|
|
*addr_len = sizeof(*nladdr);
|
|
|
|
|
|
|
|
if (peer) {
|
2012-09-07 20:12:54 +00:00
|
|
|
nladdr->nl_pid = nlk->dst_portid;
|
2005-08-15 02:27:50 +00:00
|
|
|
nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2012-09-07 20:12:54 +00:00
|
|
|
nladdr->nl_pid = nlk->portid;
|
2005-09-06 22:43:59 +00:00
|
|
|
nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sock;
|
|
|
|
struct netlink_sock *nlk;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!sock)
|
|
|
|
return ERR_PTR(-ECONNREFUSED);
|
|
|
|
|
|
|
|
/* Don't bother queuing skb if kernel socket has no input function */
|
|
|
|
nlk = nlk_sk(sock);
|
2007-10-11 04:15:29 +00:00
|
|
|
if (sock->sk_state == NETLINK_CONNECTED &&
|
2012-09-07 20:12:54 +00:00
|
|
|
nlk->dst_portid != nlk_sk(ssk)->portid) {
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_put(sock);
|
|
|
|
return ERR_PTR(-ECONNREFUSED);
|
|
|
|
}
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sock *netlink_getsockbyfilp(struct file *filp)
|
|
|
|
{
|
2013-01-23 22:07:38 +00:00
|
|
|
struct inode *inode = file_inode(filp);
|
2005-04-16 22:20:36 +00:00
|
|
|
struct sock *sock;
|
|
|
|
|
|
|
|
if (!S_ISSOCK(inode->i_mode))
|
|
|
|
return ERR_PTR(-ENOTSOCK);
|
|
|
|
|
|
|
|
sock = SOCKET_I(inode)->sk;
|
|
|
|
if (sock->sk_family != AF_NETLINK)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
sock_hold(sock);
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2013-06-28 01:04:23 +00:00
|
|
|
static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
|
|
|
|
int broadcast)
|
2013-06-03 09:46:28 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
void *data;
|
|
|
|
|
2013-06-28 01:04:23 +00:00
|
|
|
if (size <= NLMSG_GOODSIZE || broadcast)
|
2013-06-03 09:46:28 +00:00
|
|
|
return alloc_skb(size, GFP_KERNEL);
|
|
|
|
|
2013-06-28 01:04:23 +00:00
|
|
|
size = SKB_DATA_ALIGN(size) +
|
|
|
|
SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
2013-06-03 09:46:28 +00:00
|
|
|
|
|
|
|
data = vmalloc(size);
|
|
|
|
if (data == NULL)
|
2013-06-28 01:04:23 +00:00
|
|
|
return NULL;
|
2013-06-03 09:46:28 +00:00
|
|
|
|
2013-06-28 01:04:23 +00:00
|
|
|
skb = build_skb(data, size);
|
|
|
|
if (skb == NULL)
|
|
|
|
vfree(data);
|
|
|
|
else {
|
|
|
|
skb->head_frag = 0;
|
|
|
|
skb->destructor = netlink_skb_destructor;
|
|
|
|
}
|
2013-06-03 09:46:28 +00:00
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Attach a skb to a netlink socket.
|
|
|
|
* The caller must hold a reference to the destination socket. On error, the
|
|
|
|
* reference is dropped. The skb is not send to the destination, just all
|
|
|
|
* all error checks are performed and memory in the queue is reserved.
|
|
|
|
* Return values:
|
|
|
|
* < 0: error. skb freed, reference to sock dropped.
|
|
|
|
* 0: continue
|
|
|
|
* 1: repeat lookup - reference dropped while waiting for socket memory.
|
|
|
|
*/
|
2008-06-05 18:23:39 +00:00
|
|
|
int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
|
2007-11-07 10:42:09 +00:00
|
|
|
long *timeo, struct sock *ssk)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct netlink_sock *nlk;
|
|
|
|
|
|
|
|
nlk = nlk_sk(sk);
|
|
|
|
|
2013-04-17 06:47:03 +00:00
|
|
|
if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
|
|
|
|
test_bit(NETLINK_CONGESTED, &nlk->state)) &&
|
|
|
|
!netlink_skb_is_mmaped(skb)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
2007-11-07 10:42:09 +00:00
|
|
|
if (!*timeo) {
|
2007-10-11 04:14:32 +00:00
|
|
|
if (!ssk || netlink_is_kernel(ssk))
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_overrun(sk);
|
|
|
|
sock_put(sk);
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
__set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
add_wait_queue(&nlk->wait, &wait);
|
|
|
|
|
|
|
|
if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
|
2013-04-17 06:46:56 +00:00
|
|
|
test_bit(NETLINK_CONGESTED, &nlk->state)) &&
|
2005-04-16 22:20:36 +00:00
|
|
|
!sock_flag(sk, SOCK_DEAD))
|
2007-11-07 10:42:09 +00:00
|
|
|
*timeo = schedule_timeout(*timeo);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&nlk->wait, &wait);
|
|
|
|
sock_put(sk);
|
|
|
|
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
kfree_skb(skb);
|
2007-11-07 10:42:09 +00:00
|
|
|
return sock_intr_errno(*timeo);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2013-04-17 06:47:00 +00:00
|
|
|
netlink_skb_set_owner_r(skb, sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-05 22:17:46 +00:00
|
|
|
static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int len = skb->len;
|
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
netlink_deliver_tap(skb);
|
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
if (netlink_skb_is_mmaped(skb))
|
|
|
|
netlink_queue_mmaped_skb(sk, skb);
|
|
|
|
else if (netlink_rx_is_mmaped(sk))
|
|
|
|
netlink_ring_set_copied(sk, skb);
|
|
|
|
else
|
|
|
|
#endif /* CONFIG_NETLINK_MMAP */
|
|
|
|
skb_queue_tail(&sk->sk_receive_queue, skb);
|
2014-04-11 20:15:36 +00:00
|
|
|
sk->sk_data_ready(sk);
|
2012-04-05 22:17:46 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
int len = __netlink_sendskb(sk, skb);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_put(sk);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
kfree_skb(skb);
|
|
|
|
sock_put(sk);
|
|
|
|
}
|
|
|
|
|
2011-12-22 08:52:02 +00:00
|
|
|
static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int delta;
|
|
|
|
|
2013-04-17 06:46:59 +00:00
|
|
|
WARN_ON(skb->sk != NULL);
|
2013-04-17 06:47:03 +00:00
|
|
|
if (netlink_skb_is_mmaped(skb))
|
|
|
|
return skb;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-04-20 03:43:29 +00:00
|
|
|
delta = skb->end - skb->tail;
|
2013-06-03 09:46:28 +00:00
|
|
|
if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
|
2005-04-16 22:20:36 +00:00
|
|
|
return skb;
|
|
|
|
|
|
|
|
if (skb_shared(skb)) {
|
|
|
|
struct sk_buff *nskb = skb_clone(skb, allocation);
|
|
|
|
if (!nskb)
|
|
|
|
return skb;
|
2012-04-19 02:24:28 +00:00
|
|
|
consume_skb(skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
skb = nskb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pskb_expand_head(skb, 0, -delta, allocation))
|
|
|
|
skb->truesize -= delta;
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2012-05-24 23:21:27 +00:00
|
|
|
static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
|
|
|
|
struct sock *ssk)
|
2007-10-11 04:15:29 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
|
|
|
ret = -ECONNREFUSED;
|
|
|
|
if (nlk->netlink_rcv != NULL) {
|
|
|
|
ret = skb->len;
|
2013-04-17 06:47:00 +00:00
|
|
|
netlink_skb_set_owner_r(skb, sk);
|
2013-04-17 06:46:57 +00:00
|
|
|
NETLINK_CB(skb).sk = ssk;
|
2013-12-23 13:35:55 +00:00
|
|
|
netlink_deliver_tap_kernel(sk, ssk, skb);
|
2007-10-11 04:15:29 +00:00
|
|
|
nlk->netlink_rcv(skb);
|
2012-04-22 21:30:29 +00:00
|
|
|
consume_skb(skb);
|
|
|
|
} else {
|
|
|
|
kfree_skb(skb);
|
2007-10-11 04:15:29 +00:00
|
|
|
}
|
|
|
|
sock_put(sk);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
|
2012-09-07 20:12:54 +00:00
|
|
|
u32 portid, int nonblock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
int err;
|
|
|
|
long timeo;
|
|
|
|
|
|
|
|
skb = netlink_trim(skb, gfp_any());
|
|
|
|
|
|
|
|
timeo = sock_sndtimeo(ssk, nonblock);
|
|
|
|
retry:
|
2012-09-07 20:12:54 +00:00
|
|
|
sk = netlink_getsockbyportid(ssk, portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (IS_ERR(sk)) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return PTR_ERR(sk);
|
|
|
|
}
|
2007-10-11 04:15:29 +00:00
|
|
|
if (netlink_is_kernel(sk))
|
2012-05-24 23:21:27 +00:00
|
|
|
return netlink_unicast_kernel(sk, skb, ssk);
|
2007-10-11 04:15:29 +00:00
|
|
|
|
2008-03-21 22:46:12 +00:00
|
|
|
if (sk_filter(sk, skb)) {
|
2008-07-02 02:55:09 +00:00
|
|
|
err = skb->len;
|
2008-03-21 22:46:12 +00:00
|
|
|
kfree_skb(skb);
|
|
|
|
sock_put(sk);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-06-05 18:23:39 +00:00
|
|
|
err = netlink_attachskb(sk, skb, &timeo, ssk);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err == 1)
|
|
|
|
goto retry;
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2007-10-11 04:14:03 +00:00
|
|
|
return netlink_sendskb(sk, skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_unicast);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
|
|
|
|
u32 dst_portid, gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
struct sock *sk = NULL;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct netlink_ring *ring;
|
|
|
|
struct nl_mmap_hdr *hdr;
|
|
|
|
unsigned int maxlen;
|
|
|
|
|
|
|
|
sk = netlink_getsockbyportid(ssk, dst_portid);
|
|
|
|
if (IS_ERR(sk))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ring = &nlk_sk(sk)->rx_ring;
|
|
|
|
/* fast-path without atomic ops for common case: non-mmaped receiver */
|
|
|
|
if (ring->pg_vec == NULL)
|
|
|
|
goto out_put;
|
|
|
|
|
2013-11-30 12:21:31 +00:00
|
|
|
if (ring->frame_size - NL_MMAP_HDRLEN < size)
|
|
|
|
goto out_put;
|
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
skb = alloc_skb_head(gfp_mask);
|
|
|
|
if (skb == NULL)
|
|
|
|
goto err1;
|
|
|
|
|
|
|
|
spin_lock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
/* check again under lock */
|
|
|
|
if (ring->pg_vec == NULL)
|
|
|
|
goto out_free;
|
|
|
|
|
2013-11-30 12:21:31 +00:00
|
|
|
/* check again under lock */
|
2013-04-17 06:47:04 +00:00
|
|
|
maxlen = ring->frame_size - NL_MMAP_HDRLEN;
|
|
|
|
if (maxlen < size)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
netlink_forward_ring(ring);
|
|
|
|
hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
|
|
|
|
if (hdr == NULL)
|
|
|
|
goto err2;
|
|
|
|
netlink_ring_setup_skb(skb, sk, ring, hdr);
|
|
|
|
netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
|
|
|
|
atomic_inc(&ring->pending);
|
|
|
|
netlink_increment_head(ring);
|
|
|
|
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
return skb;
|
|
|
|
|
|
|
|
err2:
|
|
|
|
kfree_skb(skb);
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
2013-04-17 06:47:05 +00:00
|
|
|
netlink_overrun(sk);
|
2013-04-17 06:47:04 +00:00
|
|
|
err1:
|
|
|
|
sock_put(sk);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree_skb(skb);
|
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
out_put:
|
|
|
|
sock_put(sk);
|
|
|
|
out:
|
|
|
|
#endif
|
|
|
|
return alloc_skb(size, gfp_mask);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(netlink_alloc_skb);
|
|
|
|
|
2006-03-21 02:52:01 +00:00
|
|
|
int netlink_has_listeners(struct sock *sk, unsigned int group)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2010-10-24 04:27:10 +00:00
|
|
|
struct listeners *listeners;
|
2006-03-21 02:52:01 +00:00
|
|
|
|
2007-10-11 04:14:32 +00:00
|
|
|
BUG_ON(!netlink_is_kernel(sk));
|
2007-07-18 22:46:06 +00:00
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
|
|
|
|
|
2012-10-18 03:21:55 +00:00
|
|
|
if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
|
2010-10-24 04:27:10 +00:00
|
|
|
res = test_bit(group - 1, listeners->masks);
|
2007-07-18 22:46:06 +00:00
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2006-03-21 02:52:01 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(netlink_has_listeners);
|
|
|
|
|
2011-12-22 08:52:02 +00:00
|
|
|
static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
|
|
|
|
if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
|
2013-04-17 06:46:56 +00:00
|
|
|
!test_bit(NETLINK_CONGESTED, &nlk->state)) {
|
2013-04-17 06:47:00 +00:00
|
|
|
netlink_skb_set_owner_r(skb, sk);
|
2012-04-05 22:17:46 +00:00
|
|
|
__netlink_sendskb(sk, skb);
|
2011-12-22 08:52:03 +00:00
|
|
|
return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct netlink_broadcast_data {
|
|
|
|
struct sock *exclude_sk;
|
2007-09-12 11:05:38 +00:00
|
|
|
struct net *net;
|
2012-09-07 20:12:54 +00:00
|
|
|
u32 portid;
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 group;
|
|
|
|
int failure;
|
netlink: change return-value logic of netlink_broadcast()
Currently, netlink_broadcast() reports errors to the caller if no
messages at all were delivered:
1) If, at least, one message has been delivered correctly, returns 0.
2) Otherwise, if no messages at all were delivered due to skb_clone()
failure, return -ENOBUFS.
3) Otherwise, if there are no listeners, return -ESRCH.
With this patch, the caller knows if the delivery of any of the
messages to the listeners have failed:
1) If it fails to deliver any message (for whatever reason), return
-ENOBUFS.
2) Otherwise, if all messages were delivered OK, returns 0.
3) Otherwise, if no listeners, return -ESRCH.
In the current ctnetlink code and in Netfilter in general, we can add
reliable logging and connection tracking event delivery by dropping the
packets whose events were not successfully delivered over Netlink. Of
course, this option would be settable via /proc as this approach reduces
performance (in terms of filtered connections per seconds by a stateful
firewall) but providing reliable logging and event delivery (for
conntrackd) in return.
This patch also changes some clients of netlink_broadcast() that
may report ENOBUFS errors via printk. This error handling is not
of any help. Instead, the userspace daemons that are listening to
those netlink messages should resync themselves with the kernel-side
if they hit ENOBUFS.
BTW, netlink_broadcast() clients include those that call
cn_netlink_send(), nlmsg_multicast() and genlmsg_multicast() since they
internally call netlink_broadcast() and return its error value.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-02-06 07:56:36 +00:00
|
|
|
int delivery_failure;
|
2005-04-16 22:20:36 +00:00
|
|
|
int congested;
|
|
|
|
int delivered;
|
2005-10-21 07:20:43 +00:00
|
|
|
gfp_t allocation;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct sk_buff *skb, *skb2;
|
2010-05-05 00:36:46 +00:00
|
|
|
int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
|
|
|
|
void *tx_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2011-12-22 08:52:02 +00:00
|
|
|
static int do_one_broadcast(struct sock *sk,
|
2005-04-16 22:20:36 +00:00
|
|
|
struct netlink_broadcast_data *p)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if (p->exclude_sk == sk)
|
|
|
|
goto out;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
|
2005-08-15 19:29:13 +00:00
|
|
|
!test_bit(p->group - 1, nlk->groups))
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
2008-03-25 18:57:35 +00:00
|
|
|
if (!net_eq(sock_net(sk), p->net))
|
2007-09-12 11:05:38 +00:00
|
|
|
goto out;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (p->failure) {
|
|
|
|
netlink_overrun(sk);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_hold(sk);
|
|
|
|
if (p->skb2 == NULL) {
|
2005-05-19 20:06:35 +00:00
|
|
|
if (skb_shared(p->skb)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
p->skb2 = skb_clone(p->skb, p->allocation);
|
|
|
|
} else {
|
2005-05-19 20:06:35 +00:00
|
|
|
p->skb2 = skb_get(p->skb);
|
|
|
|
/*
|
|
|
|
* skb ownership may have been set when
|
|
|
|
* delivered to a previous socket.
|
|
|
|
*/
|
|
|
|
skb_orphan(p->skb2);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->skb2 == NULL) {
|
|
|
|
netlink_overrun(sk);
|
|
|
|
/* Clone failed. Notify ALL listeners. */
|
|
|
|
p->failure = 1;
|
2009-02-18 01:40:43 +00:00
|
|
|
if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
|
|
|
|
p->delivery_failure = 1;
|
2010-05-05 00:36:46 +00:00
|
|
|
} else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
|
|
|
|
kfree_skb(p->skb2);
|
|
|
|
p->skb2 = NULL;
|
2008-03-21 22:46:12 +00:00
|
|
|
} else if (sk_filter(sk, p->skb2)) {
|
|
|
|
kfree_skb(p->skb2);
|
|
|
|
p->skb2 = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
|
|
|
|
netlink_overrun(sk);
|
2009-02-18 01:40:43 +00:00
|
|
|
if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
|
|
|
|
p->delivery_failure = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
p->congested |= val;
|
|
|
|
p->delivered = 1;
|
|
|
|
p->skb2 = NULL;
|
|
|
|
}
|
|
|
|
sock_put(sk);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
|
2010-05-05 00:36:46 +00:00
|
|
|
u32 group, gfp_t allocation,
|
|
|
|
int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
|
|
|
|
void *filter_data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-03-25 17:26:21 +00:00
|
|
|
struct net *net = sock_net(ssk);
|
2005-04-16 22:20:36 +00:00
|
|
|
struct netlink_broadcast_data info;
|
|
|
|
struct sock *sk;
|
|
|
|
|
|
|
|
skb = netlink_trim(skb, allocation);
|
|
|
|
|
|
|
|
info.exclude_sk = ssk;
|
2007-09-12 11:05:38 +00:00
|
|
|
info.net = net;
|
2012-09-07 20:12:54 +00:00
|
|
|
info.portid = portid;
|
2005-04-16 22:20:36 +00:00
|
|
|
info.group = group;
|
|
|
|
info.failure = 0;
|
netlink: change return-value logic of netlink_broadcast()
Currently, netlink_broadcast() reports errors to the caller if no
messages at all were delivered:
1) If, at least, one message has been delivered correctly, returns 0.
2) Otherwise, if no messages at all were delivered due to skb_clone()
failure, return -ENOBUFS.
3) Otherwise, if there are no listeners, return -ESRCH.
With this patch, the caller knows if the delivery of any of the
messages to the listeners have failed:
1) If it fails to deliver any message (for whatever reason), return
-ENOBUFS.
2) Otherwise, if all messages were delivered OK, returns 0.
3) Otherwise, if no listeners, return -ESRCH.
In the current ctnetlink code and in Netfilter in general, we can add
reliable logging and connection tracking event delivery by dropping the
packets whose events were not successfully delivered over Netlink. Of
course, this option would be settable via /proc as this approach reduces
performance (in terms of filtered connections per seconds by a stateful
firewall) but providing reliable logging and event delivery (for
conntrackd) in return.
This patch also changes some clients of netlink_broadcast() that
may report ENOBUFS errors via printk. This error handling is not
of any help. Instead, the userspace daemons that are listening to
those netlink messages should resync themselves with the kernel-side
if they hit ENOBUFS.
BTW, netlink_broadcast() clients include those that call
cn_netlink_send(), nlmsg_multicast() and genlmsg_multicast() since they
internally call netlink_broadcast() and return its error value.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-02-06 07:56:36 +00:00
|
|
|
info.delivery_failure = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
info.congested = 0;
|
|
|
|
info.delivered = 0;
|
|
|
|
info.allocation = allocation;
|
|
|
|
info.skb = skb;
|
|
|
|
info.skb2 = NULL;
|
2010-05-05 00:36:46 +00:00
|
|
|
info.tx_filter = filter;
|
|
|
|
info.tx_data = filter_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* While we sleep in clone, do not allow to change socket list */
|
|
|
|
|
|
|
|
netlink_lock_table();
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
|
2005-04-16 22:20:36 +00:00
|
|
|
do_one_broadcast(sk, &info);
|
|
|
|
|
2010-07-20 06:45:56 +00:00
|
|
|
consume_skb(skb);
|
2005-05-19 20:07:32 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
netlink_unlock_table();
|
|
|
|
|
2010-07-20 06:45:56 +00:00
|
|
|
if (info.delivery_failure) {
|
|
|
|
kfree_skb(info.skb2);
|
netlink: change return-value logic of netlink_broadcast()
Currently, netlink_broadcast() reports errors to the caller if no
messages at all were delivered:
1) If, at least, one message has been delivered correctly, returns 0.
2) Otherwise, if no messages at all were delivered due to skb_clone()
failure, return -ENOBUFS.
3) Otherwise, if there are no listeners, return -ESRCH.
With this patch, the caller knows if the delivery of any of the
messages to the listeners have failed:
1) If it fails to deliver any message (for whatever reason), return
-ENOBUFS.
2) Otherwise, if all messages were delivered OK, returns 0.
3) Otherwise, if no listeners, return -ESRCH.
In the current ctnetlink code and in Netfilter in general, we can add
reliable logging and connection tracking event delivery by dropping the
packets whose events were not successfully delivered over Netlink. Of
course, this option would be settable via /proc as this approach reduces
performance (in terms of filtered connections per seconds by a stateful
firewall) but providing reliable logging and event delivery (for
conntrackd) in return.
This patch also changes some clients of netlink_broadcast() that
may report ENOBUFS errors via printk. This error handling is not
of any help. Instead, the userspace daemons that are listening to
those netlink messages should resync themselves with the kernel-side
if they hit ENOBUFS.
BTW, netlink_broadcast() clients include those that call
cn_netlink_send(), nlmsg_multicast() and genlmsg_multicast() since they
internally call netlink_broadcast() and return its error value.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-02-06 07:56:36 +00:00
|
|
|
return -ENOBUFS;
|
2012-04-22 21:30:21 +00:00
|
|
|
}
|
|
|
|
consume_skb(info.skb2);
|
netlink: change return-value logic of netlink_broadcast()
Currently, netlink_broadcast() reports errors to the caller if no
messages at all were delivered:
1) If, at least, one message has been delivered correctly, returns 0.
2) Otherwise, if no messages at all were delivered due to skb_clone()
failure, return -ENOBUFS.
3) Otherwise, if there are no listeners, return -ESRCH.
With this patch, the caller knows if the delivery of any of the
messages to the listeners have failed:
1) If it fails to deliver any message (for whatever reason), return
-ENOBUFS.
2) Otherwise, if all messages were delivered OK, returns 0.
3) Otherwise, if no listeners, return -ESRCH.
In the current ctnetlink code and in Netfilter in general, we can add
reliable logging and connection tracking event delivery by dropping the
packets whose events were not successfully delivered over Netlink. Of
course, this option would be settable via /proc as this approach reduces
performance (in terms of filtered connections per seconds by a stateful
firewall) but providing reliable logging and event delivery (for
conntrackd) in return.
This patch also changes some clients of netlink_broadcast() that
may report ENOBUFS errors via printk. This error handling is not
of any help. Instead, the userspace daemons that are listening to
those netlink messages should resync themselves with the kernel-side
if they hit ENOBUFS.
BTW, netlink_broadcast() clients include those that call
cn_netlink_send(), nlmsg_multicast() and genlmsg_multicast() since they
internally call netlink_broadcast() and return its error value.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-02-06 07:56:36 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info.delivered) {
|
|
|
|
if (info.congested && (allocation & __GFP_WAIT))
|
|
|
|
yield();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
2010-05-05 00:36:46 +00:00
|
|
|
EXPORT_SYMBOL(netlink_broadcast_filtered);
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
|
2010-05-05 00:36:46 +00:00
|
|
|
u32 group, gfp_t allocation)
|
|
|
|
{
|
2012-09-07 20:12:54 +00:00
|
|
|
return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
|
2010-05-05 00:36:46 +00:00
|
|
|
NULL, NULL);
|
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_broadcast);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct netlink_set_err_data {
|
|
|
|
struct sock *exclude_sk;
|
2012-09-07 20:12:54 +00:00
|
|
|
u32 portid;
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 group;
|
|
|
|
int code;
|
|
|
|
};
|
|
|
|
|
2011-12-22 08:52:02 +00:00
|
|
|
static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
2010-03-18 14:24:42 +00:00
|
|
|
int ret = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (sk == p->exclude_sk)
|
|
|
|
goto out;
|
|
|
|
|
2009-11-25 23:14:13 +00:00
|
|
|
if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
|
2007-09-12 11:05:38 +00:00
|
|
|
goto out;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
|
2005-08-15 19:29:13 +00:00
|
|
|
!test_bit(p->group - 1, nlk->groups))
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
2010-03-18 14:24:42 +00:00
|
|
|
if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
sk->sk_err = p->code;
|
|
|
|
sk->sk_error_report(sk);
|
|
|
|
out:
|
2010-03-18 14:24:42 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 07:37:30 +00:00
|
|
|
/**
|
|
|
|
* netlink_set_err - report error to broadcast listeners
|
|
|
|
* @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
|
2012-09-07 20:12:54 +00:00
|
|
|
* @portid: the PORTID of a process that we want to skip (if any)
|
2013-11-19 09:35:40 +00:00
|
|
|
* @group: the broadcast group that will notice the error
|
2009-03-04 07:37:30 +00:00
|
|
|
* @code: error code, must be negative (as usual in kernelspace)
|
2010-03-18 14:24:42 +00:00
|
|
|
*
|
|
|
|
* This function returns the number of broadcast listeners that have set the
|
|
|
|
* NETLINK_RECV_NO_ENOBUFS socket option.
|
2009-03-04 07:37:30 +00:00
|
|
|
*/
|
2012-09-07 20:12:54 +00:00
|
|
|
int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct netlink_set_err_data info;
|
|
|
|
struct sock *sk;
|
2010-03-18 14:24:42 +00:00
|
|
|
int ret = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
info.exclude_sk = ssk;
|
2012-09-07 20:12:54 +00:00
|
|
|
info.portid = portid;
|
2005-04-16 22:20:36 +00:00
|
|
|
info.group = group;
|
2009-03-04 07:37:30 +00:00
|
|
|
/* sk->sk_err wants a positive error value */
|
|
|
|
info.code = -code;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
read_lock(&nl_table_lock);
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
|
2010-03-18 14:24:42 +00:00
|
|
|
ret += do_one_set_err(sk, &info);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
read_unlock(&nl_table_lock);
|
2010-03-18 14:24:42 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-03-23 12:21:06 +00:00
|
|
|
EXPORT_SYMBOL(netlink_set_err);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-18 22:47:05 +00:00
|
|
|
/* must be called with netlink table grabbed */
|
|
|
|
static void netlink_update_socket_mc(struct netlink_sock *nlk,
|
|
|
|
unsigned int group,
|
|
|
|
int is_new)
|
|
|
|
{
|
|
|
|
int old, new = !!is_new, subscriptions;
|
|
|
|
|
|
|
|
old = test_bit(group - 1, nlk->groups);
|
|
|
|
subscriptions = nlk->subscriptions - old + new;
|
|
|
|
if (new)
|
|
|
|
__set_bit(group - 1, nlk->groups);
|
|
|
|
else
|
|
|
|
__clear_bit(group - 1, nlk->groups);
|
|
|
|
netlink_update_subscriptions(&nlk->sk, subscriptions);
|
|
|
|
netlink_update_listeners(&nlk->sk);
|
|
|
|
}
|
|
|
|
|
2005-08-15 19:32:15 +00:00
|
|
|
static int netlink_setsockopt(struct socket *sock, int level, int optname,
|
2009-09-30 23:12:20 +00:00
|
|
|
char __user *optval, unsigned int optlen)
|
2005-08-15 19:32:15 +00:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
2007-07-18 09:07:51 +00:00
|
|
|
unsigned int val = 0;
|
|
|
|
int err;
|
2005-08-15 19:32:15 +00:00
|
|
|
|
|
|
|
if (level != SOL_NETLINK)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
|
2013-04-17 06:47:01 +00:00
|
|
|
if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
|
|
|
|
optlen >= sizeof(int) &&
|
2007-07-18 09:07:51 +00:00
|
|
|
get_user(val, (unsigned int __user *)optval))
|
2005-08-15 19:32:15 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case NETLINK_PKTINFO:
|
|
|
|
if (val)
|
|
|
|
nlk->flags |= NETLINK_RECV_PKTINFO;
|
|
|
|
else
|
|
|
|
nlk->flags &= ~NETLINK_RECV_PKTINFO;
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
case NETLINK_ADD_MEMBERSHIP:
|
|
|
|
case NETLINK_DROP_MEMBERSHIP: {
|
2014-04-23 21:25:48 +00:00
|
|
|
if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
|
2005-08-15 19:32:15 +00:00
|
|
|
return -EPERM;
|
2007-07-18 22:46:06 +00:00
|
|
|
err = netlink_realloc_groups(sk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-08-15 19:32:15 +00:00
|
|
|
if (!val || val - 1 >= nlk->ngroups)
|
|
|
|
return -EINVAL;
|
2014-04-23 01:31:55 +00:00
|
|
|
if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
|
2014-04-23 01:31:54 +00:00
|
|
|
err = nlk->netlink_bind(val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2005-08-15 19:32:15 +00:00
|
|
|
netlink_table_grab();
|
2007-07-18 22:47:05 +00:00
|
|
|
netlink_update_socket_mc(nlk, val,
|
|
|
|
optname == NETLINK_ADD_MEMBERSHIP);
|
2005-08-15 19:32:15 +00:00
|
|
|
netlink_table_ungrab();
|
2014-04-23 01:31:55 +00:00
|
|
|
if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
|
|
|
|
nlk->netlink_unbind(val);
|
2012-06-29 06:15:22 +00:00
|
|
|
|
2005-08-15 19:32:15 +00:00
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-18 01:40:43 +00:00
|
|
|
case NETLINK_BROADCAST_ERROR:
|
|
|
|
if (val)
|
|
|
|
nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
|
|
|
|
else
|
|
|
|
nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
|
|
|
|
err = 0;
|
|
|
|
break;
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
case NETLINK_NO_ENOBUFS:
|
|
|
|
if (val) {
|
|
|
|
nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
|
2013-04-17 06:46:56 +00:00
|
|
|
clear_bit(NETLINK_CONGESTED, &nlk->state);
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
wake_up_interruptible(&nlk->wait);
|
2012-04-22 21:30:21 +00:00
|
|
|
} else {
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
|
2012-04-22 21:30:21 +00:00
|
|
|
}
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
err = 0;
|
|
|
|
break;
|
2013-04-17 06:47:01 +00:00
|
|
|
#ifdef CONFIG_NETLINK_MMAP
|
|
|
|
case NETLINK_RX_RING:
|
|
|
|
case NETLINK_TX_RING: {
|
|
|
|
struct nl_mmap_req req;
|
|
|
|
|
|
|
|
/* Rings might consume more memory than queue limits, require
|
|
|
|
* CAP_NET_ADMIN.
|
|
|
|
*/
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
if (optlen < sizeof(req))
|
|
|
|
return -EINVAL;
|
|
|
|
if (copy_from_user(&req, optval, sizeof(req)))
|
|
|
|
return -EFAULT;
|
|
|
|
err = netlink_set_ring(sk, &req, false,
|
|
|
|
optname == NETLINK_TX_RING);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETLINK_MMAP */
|
2005-08-15 19:32:15 +00:00
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_getsockopt(struct socket *sock, int level, int optname,
|
2007-02-09 14:25:07 +00:00
|
|
|
char __user *optval, int __user *optlen)
|
2005-08-15 19:32:15 +00:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
int len, val, err;
|
|
|
|
|
|
|
|
if (level != SOL_NETLINK)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
|
|
|
|
if (get_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
if (len < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case NETLINK_PKTINFO:
|
|
|
|
if (len < sizeof(int))
|
|
|
|
return -EINVAL;
|
|
|
|
len = sizeof(int);
|
|
|
|
val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
|
2006-10-30 23:06:12 +00:00
|
|
|
if (put_user(len, optlen) ||
|
|
|
|
put_user(val, optval))
|
|
|
|
return -EFAULT;
|
2005-08-15 19:32:15 +00:00
|
|
|
err = 0;
|
|
|
|
break;
|
2009-02-18 01:40:43 +00:00
|
|
|
case NETLINK_BROADCAST_ERROR:
|
|
|
|
if (len < sizeof(int))
|
|
|
|
return -EINVAL;
|
|
|
|
len = sizeof(int);
|
|
|
|
val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
|
|
|
|
if (put_user(len, optlen) ||
|
|
|
|
put_user(val, optval))
|
|
|
|
return -EFAULT;
|
|
|
|
err = 0;
|
|
|
|
break;
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
case NETLINK_NO_ENOBUFS:
|
|
|
|
if (len < sizeof(int))
|
|
|
|
return -EINVAL;
|
|
|
|
len = sizeof(int);
|
|
|
|
val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
|
|
|
|
if (put_user(len, optlen) ||
|
|
|
|
put_user(val, optval))
|
|
|
|
return -EFAULT;
|
|
|
|
err = 0;
|
|
|
|
break;
|
2005-08-15 19:32:15 +00:00
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nl_pktinfo info;
|
|
|
|
|
|
|
|
info.group = NETLINK_CB(skb).dst_group;
|
|
|
|
put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
|
|
|
|
struct msghdr *msg, size_t len)
|
|
|
|
{
|
|
|
|
struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
2014-01-17 21:53:15 +00:00
|
|
|
DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
|
2012-09-07 20:12:54 +00:00
|
|
|
u32 dst_portid;
|
2005-08-15 02:27:50 +00:00
|
|
|
u32 dst_group;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct sk_buff *skb;
|
|
|
|
int err;
|
|
|
|
struct scm_cookie scm;
|
2014-05-30 18:04:00 +00:00
|
|
|
u32 netlink_skb_flags = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (msg->msg_flags&MSG_OOB)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2011-09-19 05:52:27 +00:00
|
|
|
if (NULL == siocb->scm)
|
2005-04-16 22:20:36 +00:00
|
|
|
siocb->scm = &scm;
|
2011-09-19 05:52:27 +00:00
|
|
|
|
2012-08-21 06:21:17 +00:00
|
|
|
err = scm_send(sock, msg, siocb->scm, true);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (msg->msg_namelen) {
|
2010-06-13 03:31:06 +00:00
|
|
|
err = -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (addr->nl_family != AF_NETLINK)
|
2010-06-13 03:31:06 +00:00
|
|
|
goto out;
|
2012-09-07 20:12:54 +00:00
|
|
|
dst_portid = addr->nl_pid;
|
2005-08-15 02:27:50 +00:00
|
|
|
dst_group = ffs(addr->nl_groups);
|
2010-06-13 03:31:06 +00:00
|
|
|
err = -EPERM;
|
2012-09-07 20:12:54 +00:00
|
|
|
if ((dst_group || dst_portid) &&
|
2014-04-23 21:25:48 +00:00
|
|
|
!netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
|
2010-06-13 03:31:06 +00:00
|
|
|
goto out;
|
2014-05-30 18:04:00 +00:00
|
|
|
netlink_skb_flags |= NETLINK_SKB_DST;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2012-09-07 20:12:54 +00:00
|
|
|
dst_portid = nlk->dst_portid;
|
2005-08-15 02:27:50 +00:00
|
|
|
dst_group = nlk->dst_group;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
if (!nlk->portid) {
|
2005-04-16 22:20:36 +00:00
|
|
|
err = netlink_autobind(sock);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-17 06:47:03 +00:00
|
|
|
if (netlink_tx_is_mmaped(sk) &&
|
|
|
|
msg->msg_iov->iov_base == NULL) {
|
|
|
|
err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
|
|
|
|
siocb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
err = -EMSGSIZE;
|
|
|
|
if (len > sk->sk_sndbuf - 32)
|
|
|
|
goto out;
|
|
|
|
err = -ENOBUFS;
|
2013-06-28 01:04:23 +00:00
|
|
|
skb = netlink_alloc_large_skb(len, dst_group);
|
2007-12-04 08:19:38 +00:00
|
|
|
if (skb == NULL)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
NETLINK_CB(skb).portid = nlk->portid;
|
2005-08-15 02:27:50 +00:00
|
|
|
NETLINK_CB(skb).dst_group = dst_group;
|
2012-09-06 18:20:01 +00:00
|
|
|
NETLINK_CB(skb).creds = siocb->scm->creds;
|
2014-05-30 18:04:00 +00:00
|
|
|
NETLINK_CB(skb).flags = netlink_skb_flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err = -EFAULT;
|
2007-12-04 08:19:38 +00:00
|
|
|
if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree_skb(skb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = security_netlink_send(sk, skb);
|
|
|
|
if (err) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-08-15 02:27:50 +00:00
|
|
|
if (dst_group) {
|
2005-04-16 22:20:36 +00:00
|
|
|
atomic_inc(&skb->users);
|
2012-09-07 20:12:54 +00:00
|
|
|
netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2012-09-07 20:12:54 +00:00
|
|
|
err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
out:
|
2010-06-13 03:31:06 +00:00
|
|
|
scm_destroy(siocb->scm);
|
2005-04-16 22:20:36 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
|
|
|
|
struct msghdr *msg, size_t len,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
|
|
|
|
struct scm_cookie scm;
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
int noblock = flags&MSG_DONTWAIT;
|
|
|
|
size_t copied;
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
struct sk_buff *skb, *data_skb;
|
2011-02-21 02:40:47 +00:00
|
|
|
int err, ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (flags&MSG_OOB)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
copied = 0;
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
skb = skb_recv_datagram(sk, flags, noblock, &err);
|
|
|
|
if (skb == NULL)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
data_skb = skb;
|
|
|
|
|
net/compat/wext: send different messages to compat tasks
Wireless extensions have the unfortunate problem that events
are multicast netlink messages, and are not independent of
pointer size. Thus, currently 32-bit tasks on 64-bit platforms
cannot properly receive events and fail with all kinds of
strange problems, for instance wpa_supplicant never notices
disassociations, due to the way the 64-bit event looks (to a
32-bit process), the fact that the address is all zeroes is
lost, it thinks instead it is 00:00:00:00:01:00.
The same problem existed with the ioctls, until David Miller
fixed those some time ago in an heroic effort.
A different problem caused by this is that we cannot send the
ASSOCREQIE/ASSOCRESPIE events because sending them causes a
32-bit wpa_supplicant on a 64-bit system to overwrite its
internal information, which is worse than it not getting the
information at all -- so we currently resort to sending a
custom string event that it then parses. This, however, has a
severe size limitation we are frequently hitting with modern
access points; this limitation would can be lifted after this
patch by sending the correct binary, not custom, event.
A similar problem apparently happens for some other netlink
users on x86_64 with 32-bit tasks due to the alignment for
64-bit quantities.
In order to fix these problems, I have implemented a way to
send compat messages to tasks. When sending an event, we send
the non-compat event data together with a compat event data in
skb_shinfo(main_skb)->frag_list. Then, when the event is read
from the socket, the netlink code makes sure to pass out only
the skb that is compatible with the task. This approach was
suggested by David Miller, my original approach required
always sending two skbs but that had various small problems.
To determine whether compat is needed or not, I have used the
MSG_CMSG_COMPAT flag, and adjusted the call path for recv and
recvfrom to include it, even if those calls do not have a cmsg
parameter.
I have not solved one small part of the problem, and I don't
think it is necessary to: if a 32-bit application uses read()
rather than any form of recvmsg() it will still get the wrong
(64-bit) event. However, neither do applications actually do
this, nor would it be a regression.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-07-01 11:26:02 +00:00
|
|
|
#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
|
|
|
|
if (unlikely(skb_shinfo(skb)->frag_list)) {
|
|
|
|
/*
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
* If this skb has a frag_list, then here that means that we
|
|
|
|
* will have to use the frag_list skb's data for compat tasks
|
|
|
|
* and the regular skb's data for normal (non-compat) tasks.
|
net/compat/wext: send different messages to compat tasks
Wireless extensions have the unfortunate problem that events
are multicast netlink messages, and are not independent of
pointer size. Thus, currently 32-bit tasks on 64-bit platforms
cannot properly receive events and fail with all kinds of
strange problems, for instance wpa_supplicant never notices
disassociations, due to the way the 64-bit event looks (to a
32-bit process), the fact that the address is all zeroes is
lost, it thinks instead it is 00:00:00:00:01:00.
The same problem existed with the ioctls, until David Miller
fixed those some time ago in an heroic effort.
A different problem caused by this is that we cannot send the
ASSOCREQIE/ASSOCRESPIE events because sending them causes a
32-bit wpa_supplicant on a 64-bit system to overwrite its
internal information, which is worse than it not getting the
information at all -- so we currently resort to sending a
custom string event that it then parses. This, however, has a
severe size limitation we are frequently hitting with modern
access points; this limitation would can be lifted after this
patch by sending the correct binary, not custom, event.
A similar problem apparently happens for some other netlink
users on x86_64 with 32-bit tasks due to the alignment for
64-bit quantities.
In order to fix these problems, I have implemented a way to
send compat messages to tasks. When sending an event, we send
the non-compat event data together with a compat event data in
skb_shinfo(main_skb)->frag_list. Then, when the event is read
from the socket, the netlink code makes sure to pass out only
the skb that is compatible with the task. This approach was
suggested by David Miller, my original approach required
always sending two skbs but that had various small problems.
To determine whether compat is needed or not, I have used the
MSG_CMSG_COMPAT flag, and adjusted the call path for recv and
recvfrom to include it, even if those calls do not have a cmsg
parameter.
I have not solved one small part of the problem, and I don't
think it is necessary to: if a 32-bit application uses read()
rather than any form of recvmsg() it will still get the wrong
(64-bit) event. However, neither do applications actually do
this, nor would it be a regression.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-07-01 11:26:02 +00:00
|
|
|
*
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
* If we need to send the compat skb, assign it to the
|
|
|
|
* 'data_skb' variable so that it will be used below for data
|
|
|
|
* copying. We keep 'skb' for everything else, including
|
|
|
|
* freeing both later.
|
net/compat/wext: send different messages to compat tasks
Wireless extensions have the unfortunate problem that events
are multicast netlink messages, and are not independent of
pointer size. Thus, currently 32-bit tasks on 64-bit platforms
cannot properly receive events and fail with all kinds of
strange problems, for instance wpa_supplicant never notices
disassociations, due to the way the 64-bit event looks (to a
32-bit process), the fact that the address is all zeroes is
lost, it thinks instead it is 00:00:00:00:01:00.
The same problem existed with the ioctls, until David Miller
fixed those some time ago in an heroic effort.
A different problem caused by this is that we cannot send the
ASSOCREQIE/ASSOCRESPIE events because sending them causes a
32-bit wpa_supplicant on a 64-bit system to overwrite its
internal information, which is worse than it not getting the
information at all -- so we currently resort to sending a
custom string event that it then parses. This, however, has a
severe size limitation we are frequently hitting with modern
access points; this limitation would can be lifted after this
patch by sending the correct binary, not custom, event.
A similar problem apparently happens for some other netlink
users on x86_64 with 32-bit tasks due to the alignment for
64-bit quantities.
In order to fix these problems, I have implemented a way to
send compat messages to tasks. When sending an event, we send
the non-compat event data together with a compat event data in
skb_shinfo(main_skb)->frag_list. Then, when the event is read
from the socket, the netlink code makes sure to pass out only
the skb that is compatible with the task. This approach was
suggested by David Miller, my original approach required
always sending two skbs but that had various small problems.
To determine whether compat is needed or not, I have used the
MSG_CMSG_COMPAT flag, and adjusted the call path for recv and
recvfrom to include it, even if those calls do not have a cmsg
parameter.
I have not solved one small part of the problem, and I don't
think it is necessary to: if a 32-bit application uses read()
rather than any form of recvmsg() it will still get the wrong
(64-bit) event. However, neither do applications actually do
this, nor would it be a regression.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-07-01 11:26:02 +00:00
|
|
|
*/
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
if (flags & MSG_CMSG_COMPAT)
|
|
|
|
data_skb = skb_shinfo(skb)->frag_list;
|
net/compat/wext: send different messages to compat tasks
Wireless extensions have the unfortunate problem that events
are multicast netlink messages, and are not independent of
pointer size. Thus, currently 32-bit tasks on 64-bit platforms
cannot properly receive events and fail with all kinds of
strange problems, for instance wpa_supplicant never notices
disassociations, due to the way the 64-bit event looks (to a
32-bit process), the fact that the address is all zeroes is
lost, it thinks instead it is 00:00:00:00:01:00.
The same problem existed with the ioctls, until David Miller
fixed those some time ago in an heroic effort.
A different problem caused by this is that we cannot send the
ASSOCREQIE/ASSOCRESPIE events because sending them causes a
32-bit wpa_supplicant on a 64-bit system to overwrite its
internal information, which is worse than it not getting the
information at all -- so we currently resort to sending a
custom string event that it then parses. This, however, has a
severe size limitation we are frequently hitting with modern
access points; this limitation would can be lifted after this
patch by sending the correct binary, not custom, event.
A similar problem apparently happens for some other netlink
users on x86_64 with 32-bit tasks due to the alignment for
64-bit quantities.
In order to fix these problems, I have implemented a way to
send compat messages to tasks. When sending an event, we send
the non-compat event data together with a compat event data in
skb_shinfo(main_skb)->frag_list. Then, when the event is read
from the socket, the netlink code makes sure to pass out only
the skb that is compatible with the task. This approach was
suggested by David Miller, my original approach required
always sending two skbs but that had various small problems.
To determine whether compat is needed or not, I have used the
MSG_CMSG_COMPAT flag, and adjusted the call path for recv and
recvfrom to include it, even if those calls do not have a cmsg
parameter.
I have not solved one small part of the problem, and I don't
think it is necessary to: if a 32-bit application uses read()
rather than any form of recvmsg() it will still get the wrong
(64-bit) event. However, neither do applications actually do
this, nor would it be a regression.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-07-01 11:26:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-07 20:02:33 +00:00
|
|
|
/* Record the max length of recvmsg() calls for future allocations */
|
|
|
|
nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
|
|
|
|
nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
|
|
|
|
16384);
|
|
|
|
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
copied = data_skb->len;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (len < copied) {
|
|
|
|
msg->msg_flags |= MSG_TRUNC;
|
|
|
|
copied = len;
|
|
|
|
}
|
|
|
|
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
skb_reset_transport_header(data_skb);
|
|
|
|
err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (msg->msg_name) {
|
2014-01-17 21:53:15 +00:00
|
|
|
DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
|
2005-04-16 22:20:36 +00:00
|
|
|
addr->nl_family = AF_NETLINK;
|
|
|
|
addr->nl_pad = 0;
|
2012-09-07 20:12:54 +00:00
|
|
|
addr->nl_pid = NETLINK_CB(skb).portid;
|
2005-08-15 02:27:50 +00:00
|
|
|
addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
|
2005-04-16 22:20:36 +00:00
|
|
|
msg->msg_namelen = sizeof(*addr);
|
|
|
|
}
|
|
|
|
|
2006-03-13 04:34:27 +00:00
|
|
|
if (nlk->flags & NETLINK_RECV_PKTINFO)
|
|
|
|
netlink_cmsg_recv_pktinfo(msg, skb);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (NULL == siocb->scm) {
|
|
|
|
memset(&scm, 0, sizeof(scm));
|
|
|
|
siocb->scm = &scm;
|
|
|
|
}
|
|
|
|
siocb->scm->creds = *NETLINK_CREDS(skb);
|
2007-05-03 10:27:01 +00:00
|
|
|
if (flags & MSG_TRUNC)
|
netlink: fix compat recvmsg
Since
commit 1dacc76d0014a034b8aca14237c127d7c19d7726
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Wed Jul 1 11:26:02 2009 +0000
net/compat/wext: send different messages to compat tasks
we had a race condition when setting and then
restoring frag_list. Eric attempted to fix it,
but the fix created even worse problems.
However, the original motivation I had when I
added the code that turned out to be racy is
no longer clear to me, since we only copy up
to skb->len to userspace, which doesn't include
the frag_list length. As a result, not doing
any frag_list clearing and restoring avoids
the race condition, while not introducing any
other problems.
Additionally, while preparing this patch I found
that since none of the remaining netlink code is
really aware of the frag_list, we need to use the
original skb's information for packet information
and credentials. This fixes, for example, the
group information received by compat tasks.
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: stable@kernel.org [2.6.31+, for 2.6.35 revert 1235f504aa]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-08-15 21:20:44 +00:00
|
|
|
copied = data_skb->len;
|
2010-08-16 06:21:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
skb_free_datagram(sk, skb);
|
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
if (nlk->cb_running &&
|
|
|
|
atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
|
2011-02-21 02:40:47 +00:00
|
|
|
ret = netlink_dump(sk);
|
|
|
|
if (ret) {
|
|
|
|
sk->sk_err = ret;
|
|
|
|
sk->sk_error_report(sk);
|
|
|
|
}
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
scm_recv(sock, msg, siocb->scm, flags);
|
|
|
|
out:
|
|
|
|
netlink_rcv_wake(sk);
|
|
|
|
return err ? : copied;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:15:36 +00:00
|
|
|
static void netlink_data_ready(struct sock *sk)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-11 04:15:29 +00:00
|
|
|
BUG();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-09 14:25:07 +00:00
|
|
|
* We export these functions to other modules. They provide a
|
2005-04-16 22:20:36 +00:00
|
|
|
* complete set of kernel non-blocking support for message
|
|
|
|
* queueing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sock *
|
2012-09-08 02:53:54 +00:00
|
|
|
__netlink_kernel_create(struct net *net, int unit, struct module *module,
|
|
|
|
struct netlink_kernel_cfg *cfg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct socket *sock;
|
|
|
|
struct sock *sk;
|
2005-08-15 02:27:13 +00:00
|
|
|
struct netlink_sock *nlk;
|
2010-10-24 04:27:10 +00:00
|
|
|
struct listeners *listeners = NULL;
|
2012-06-29 06:15:21 +00:00
|
|
|
struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
|
|
|
|
unsigned int groups;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-08-29 09:15:24 +00:00
|
|
|
BUG_ON(!nl_table);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
if (unit < 0 || unit >= MAX_LINKS)
|
2005-04-16 22:20:36 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
|
|
|
|
return NULL;
|
|
|
|
|
[NETNS]: Fix race between put_net() and netlink_kernel_create().
The comment about "race free view of the set of network
namespaces" was a bit hasty. Look (there even can be only
one CPU, as discovered by Alexey Dobriyan and Denis Lunev):
put_net()
if (atomic_dec_and_test(&net->refcnt))
/* true */
__put_net(net);
queue_work(...);
/*
* note: the net now has refcnt 0, but still in
* the global list of net namespaces
*/
== re-schedule ==
register_pernet_subsys(&some_ops);
register_pernet_operations(&some_ops);
(*some_ops)->init(net);
/*
* we call netlink_kernel_create() here
* in some places
*/
netlink_kernel_create();
sk_alloc();
get_net(net); /* refcnt = 1 */
/*
* now we drop the net refcount not to
* block the net namespace exit in the
* future (or this can be done on the
* error path)
*/
put_net(sk->sk_net);
if (atomic_dec_and_test(&...))
/*
* true. BOOOM! The net is
* scheduled for release twice
*/
When thinking on this problem, I decided, that getting and
putting the net in init callback is wrong. If some init
callback needs to have a refcount-less reference on the struct
net, _it_ has to be careful himself, rather than relying on
the infrastructure to handle this correctly.
In case of netlink_kernel_create(), the problem is that the
sk_alloc() gets the given namespace, but passing the info
that we don't want to get it inside this call is too heavy.
Instead, I propose to crate the socket inside an init_net
namespace and then re-attach it to the desired one right
after the socket is created.
After doing this, we also have to be careful on error paths
not to drop the reference on the namespace, we didn't get
the one on.
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: Denis Lunev <den@openvz.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-01-31 03:31:06 +00:00
|
|
|
/*
|
|
|
|
* We have to just have a reference on the net from sk, but don't
|
|
|
|
* get_net it. Besides, we cannot get and then put the net here.
|
|
|
|
* So we create one inside init_net and the move it to net.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
|
|
|
|
goto out_sock_release_nosk;
|
|
|
|
|
|
|
|
sk = sock->sk;
|
2008-02-29 19:18:32 +00:00
|
|
|
sk_change_net(sk, net);
|
2005-08-10 02:40:55 +00:00
|
|
|
|
2012-06-29 06:15:21 +00:00
|
|
|
if (!cfg || cfg->groups < 32)
|
2006-03-21 02:52:01 +00:00
|
|
|
groups = 32;
|
2012-06-29 06:15:21 +00:00
|
|
|
else
|
|
|
|
groups = cfg->groups;
|
2006-03-21 02:52:01 +00:00
|
|
|
|
2010-10-24 04:27:10 +00:00
|
|
|
listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
|
2006-03-21 02:52:01 +00:00
|
|
|
if (!listeners)
|
|
|
|
goto out_sock_release;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
sk->sk_data_ready = netlink_data_ready;
|
2012-06-29 06:15:21 +00:00
|
|
|
if (cfg && cfg->input)
|
|
|
|
nlk_sk(sk)->netlink_rcv = cfg->input;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-09-12 11:05:38 +00:00
|
|
|
if (netlink_insert(sk, net, 0))
|
2005-08-15 02:27:13 +00:00
|
|
|
goto out_sock_release;
|
2005-08-10 02:40:55 +00:00
|
|
|
|
2005-08-15 02:27:13 +00:00
|
|
|
nlk = nlk_sk(sk);
|
|
|
|
nlk->flags |= NETLINK_KERNEL_SOCKET;
|
2005-08-10 02:40:55 +00:00
|
|
|
|
|
|
|
netlink_table_grab();
|
2007-09-12 11:05:38 +00:00
|
|
|
if (!nl_table[unit].registered) {
|
|
|
|
nl_table[unit].groups = groups;
|
2010-10-24 04:27:10 +00:00
|
|
|
rcu_assign_pointer(nl_table[unit].listeners, listeners);
|
2007-09-12 11:05:38 +00:00
|
|
|
nl_table[unit].cb_mutex = cb_mutex;
|
|
|
|
nl_table[unit].module = module;
|
2012-09-08 02:53:53 +00:00
|
|
|
if (cfg) {
|
|
|
|
nl_table[unit].bind = cfg->bind;
|
|
|
|
nl_table[unit].flags = cfg->flags;
|
2013-06-06 06:49:11 +00:00
|
|
|
if (cfg->compare)
|
|
|
|
nl_table[unit].compare = cfg->compare;
|
2012-09-08 02:53:53 +00:00
|
|
|
}
|
2007-09-12 11:05:38 +00:00
|
|
|
nl_table[unit].registered = 1;
|
2007-10-15 08:39:12 +00:00
|
|
|
} else {
|
|
|
|
kfree(listeners);
|
2008-01-19 07:53:31 +00:00
|
|
|
nl_table[unit].registered++;
|
2007-09-12 11:05:38 +00:00
|
|
|
}
|
2005-08-10 02:40:55 +00:00
|
|
|
netlink_table_ungrab();
|
2005-08-15 02:27:13 +00:00
|
|
|
return sk;
|
|
|
|
|
2005-08-10 02:40:55 +00:00
|
|
|
out_sock_release:
|
2006-03-21 02:52:01 +00:00
|
|
|
kfree(listeners);
|
2008-02-29 19:17:56 +00:00
|
|
|
netlink_kernel_release(sk);
|
[NETNS]: Fix race between put_net() and netlink_kernel_create().
The comment about "race free view of the set of network
namespaces" was a bit hasty. Look (there even can be only
one CPU, as discovered by Alexey Dobriyan and Denis Lunev):
put_net()
if (atomic_dec_and_test(&net->refcnt))
/* true */
__put_net(net);
queue_work(...);
/*
* note: the net now has refcnt 0, but still in
* the global list of net namespaces
*/
== re-schedule ==
register_pernet_subsys(&some_ops);
register_pernet_operations(&some_ops);
(*some_ops)->init(net);
/*
* we call netlink_kernel_create() here
* in some places
*/
netlink_kernel_create();
sk_alloc();
get_net(net); /* refcnt = 1 */
/*
* now we drop the net refcount not to
* block the net namespace exit in the
* future (or this can be done on the
* error path)
*/
put_net(sk->sk_net);
if (atomic_dec_and_test(&...))
/*
* true. BOOOM! The net is
* scheduled for release twice
*/
When thinking on this problem, I decided, that getting and
putting the net in init callback is wrong. If some init
callback needs to have a refcount-less reference on the struct
net, _it_ has to be careful himself, rather than relying on
the infrastructure to handle this correctly.
In case of netlink_kernel_create(), the problem is that the
sk_alloc() gets the given namespace, but passing the info
that we don't want to get it inside this call is too heavy.
Instead, I propose to crate the socket inside an init_net
namespace and then re-attach it to the desired one right
after the socket is created.
After doing this, we also have to be careful on error paths
not to drop the reference on the namespace, we didn't get
the one on.
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: Denis Lunev <den@openvz.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-01-31 03:31:06 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out_sock_release_nosk:
|
2005-08-10 02:40:55 +00:00
|
|
|
sock_release(sock);
|
2005-08-15 02:27:13 +00:00
|
|
|
return NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2012-09-08 02:53:54 +00:00
|
|
|
EXPORT_SYMBOL(__netlink_kernel_create);
|
2008-01-28 22:41:19 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
netlink_kernel_release(struct sock *sk)
|
|
|
|
{
|
2008-02-29 19:18:32 +00:00
|
|
|
sk_release_kernel(sk);
|
2008-01-28 22:41:19 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(netlink_kernel_release);
|
|
|
|
|
2009-09-12 03:03:15 +00:00
|
|
|
int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
|
2007-07-18 22:46:06 +00:00
|
|
|
{
|
2010-10-24 04:27:10 +00:00
|
|
|
struct listeners *new, *old;
|
2007-07-18 22:46:06 +00:00
|
|
|
struct netlink_table *tbl = &nl_table[sk->sk_protocol];
|
|
|
|
|
|
|
|
if (groups < 32)
|
|
|
|
groups = 32;
|
|
|
|
|
|
|
|
if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
|
2010-10-24 04:27:10 +00:00
|
|
|
new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
|
|
|
|
if (!new)
|
2009-09-12 03:03:15 +00:00
|
|
|
return -ENOMEM;
|
2012-10-18 03:21:55 +00:00
|
|
|
old = nl_deref_protected(tbl->listeners);
|
2010-10-24 04:27:10 +00:00
|
|
|
memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
|
|
|
|
rcu_assign_pointer(tbl->listeners, new);
|
|
|
|
|
2011-03-15 10:01:42 +00:00
|
|
|
kfree_rcu(old, rcu);
|
2007-07-18 22:46:06 +00:00
|
|
|
}
|
|
|
|
tbl->groups = groups;
|
|
|
|
|
2009-09-12 03:03:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netlink_change_ngroups - change number of multicast groups
|
|
|
|
*
|
|
|
|
* This changes the number of multicast groups that are available
|
|
|
|
* on a certain netlink family. Note that it is not possible to
|
|
|
|
* change the number of groups to below 32. Also note that it does
|
|
|
|
* not implicitly call netlink_clear_multicast_users() when the
|
|
|
|
* number of groups is reduced.
|
|
|
|
*
|
|
|
|
* @sk: The kernel netlink socket, as returned by netlink_kernel_create().
|
|
|
|
* @groups: The new number of groups.
|
|
|
|
*/
|
|
|
|
int netlink_change_ngroups(struct sock *sk, unsigned int groups)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
netlink_table_grab();
|
|
|
|
err = __netlink_change_ngroups(sk, groups);
|
2007-07-18 22:46:06 +00:00
|
|
|
netlink_table_ungrab();
|
2009-09-12 03:03:15 +00:00
|
|
|
|
2007-07-18 22:46:06 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:44:05 +00:00
|
|
|
void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
|
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each_bound(sk, &tbl->mc_list)
|
2009-09-24 22:44:05 +00:00
|
|
|
netlink_update_socket_mc(nlk_sk(sk), group, 0);
|
|
|
|
}
|
|
|
|
|
2012-01-30 20:22:06 +00:00
|
|
|
struct nlmsghdr *
|
2012-09-07 20:12:54 +00:00
|
|
|
__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
|
2012-01-30 20:22:06 +00:00
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
2013-03-27 06:47:04 +00:00
|
|
|
int size = nlmsg_msg_size(len);
|
2012-01-30 20:22:06 +00:00
|
|
|
|
2014-02-17 08:53:32 +00:00
|
|
|
nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
|
2012-01-30 20:22:06 +00:00
|
|
|
nlh->nlmsg_type = type;
|
|
|
|
nlh->nlmsg_len = size;
|
|
|
|
nlh->nlmsg_flags = flags;
|
2012-09-07 20:12:54 +00:00
|
|
|
nlh->nlmsg_pid = portid;
|
2012-01-30 20:22:06 +00:00
|
|
|
nlh->nlmsg_seq = seq;
|
|
|
|
if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
|
2013-03-27 06:47:04 +00:00
|
|
|
memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
|
2012-01-30 20:22:06 +00:00
|
|
|
return nlh;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__nlmsg_put);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* It looks a bit ugly.
|
|
|
|
* It would be better to create kernel thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int netlink_dump(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
struct netlink_callback *cb;
|
2011-06-10 01:27:09 +00:00
|
|
|
struct sk_buff *skb = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct nlmsghdr *nlh;
|
2006-08-05 06:03:29 +00:00
|
|
|
int len, err = -ENOBUFS;
|
2011-06-10 01:27:09 +00:00
|
|
|
int alloc_size;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-04-20 21:14:21 +00:00
|
|
|
mutex_lock(nlk->cb_mutex);
|
2013-08-15 22:31:06 +00:00
|
|
|
if (!nlk->cb_running) {
|
2006-08-05 06:03:29 +00:00
|
|
|
err = -EINVAL;
|
|
|
|
goto errout_skb;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
cb = &nlk->cb;
|
2011-06-10 01:27:09 +00:00
|
|
|
alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
|
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
if (!netlink_rx_is_mmaped(sk) &&
|
|
|
|
atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
|
|
|
|
goto errout_skb;
|
2014-03-07 20:02:33 +00:00
|
|
|
|
|
|
|
/* NLMSG_GOODSIZE is small to avoid high order allocations being
|
|
|
|
* required, but it makes sense to _attempt_ a 16K bytes allocation
|
|
|
|
* to reduce number of system calls on dump operations, if user
|
|
|
|
* ever provided a big enough buffer.
|
|
|
|
*/
|
|
|
|
if (alloc_size < nlk->max_recvmsg_len) {
|
|
|
|
skb = netlink_alloc_skb(sk,
|
|
|
|
nlk->max_recvmsg_len,
|
|
|
|
nlk->portid,
|
|
|
|
GFP_KERNEL |
|
|
|
|
__GFP_NOWARN |
|
|
|
|
__GFP_NORETRY);
|
|
|
|
/* available room should be exact amount to avoid MSG_TRUNC */
|
|
|
|
if (skb)
|
|
|
|
skb_reserve(skb, skb_tailroom(skb) -
|
|
|
|
nlk->max_recvmsg_len);
|
|
|
|
}
|
|
|
|
if (!skb)
|
|
|
|
skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
|
|
|
|
GFP_KERNEL);
|
2011-06-10 01:27:09 +00:00
|
|
|
if (!skb)
|
2011-06-15 03:11:42 +00:00
|
|
|
goto errout_skb;
|
2013-04-17 06:47:04 +00:00
|
|
|
netlink_skb_set_owner_r(skb, sk);
|
2011-06-10 01:27:09 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
len = cb->dump(skb, cb);
|
|
|
|
|
|
|
|
if (len > 0) {
|
2007-04-20 21:14:21 +00:00
|
|
|
mutex_unlock(nlk->cb_mutex);
|
2008-03-21 22:46:12 +00:00
|
|
|
|
|
|
|
if (sk_filter(sk, skb))
|
|
|
|
kfree_skb(skb);
|
2012-04-05 22:17:46 +00:00
|
|
|
else
|
|
|
|
__netlink_sendskb(sk, skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-05 06:03:29 +00:00
|
|
|
nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
|
|
|
|
if (!nlh)
|
|
|
|
goto errout_skb;
|
|
|
|
|
netlink: advertise incomplete dumps
Consider the following situation:
* a dump that would show 8 entries, four in the first
round, and four in the second
* between the first and second rounds, 6 entries are
removed
* now the second round will not show any entry, and
even if there is a sequence/generation counter the
application will not know
To solve this problem, add a new flag NLM_F_DUMP_INTR
to the netlink header that indicates the dump wasn't
consistent, this flag can also be set on the MSG_DONE
message that terminates the dump, and as such above
situation can be detected.
To achieve this, add a sequence counter to the netlink
callback struct. Of course, netlink code still needs
to use this new functionality. The correct way to do
that is to always set cb->seq when a dumpit callback
is invoked and call nl_dump_check_consistent() for
each new message. The core code will also call this
function for the final MSG_DONE message.
To make it usable with generic netlink, a new function
genlmsg_nlhdr() is needed to obtain the netlink header
from the genetlink user header.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: John W. Linville <linville@tuxdriver.com>
2011-06-20 11:40:46 +00:00
|
|
|
nl_dump_check_consistent(cb, nlh);
|
|
|
|
|
2006-08-05 06:03:29 +00:00
|
|
|
memcpy(nlmsg_data(nlh), &len, sizeof(len));
|
|
|
|
|
2008-03-21 22:46:12 +00:00
|
|
|
if (sk_filter(sk, skb))
|
|
|
|
kfree_skb(skb);
|
2012-04-05 22:17:46 +00:00
|
|
|
else
|
|
|
|
__netlink_sendskb(sk, skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-11-10 01:25:52 +00:00
|
|
|
if (cb->done)
|
|
|
|
cb->done(cb);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
nlk->cb_running = false;
|
|
|
|
mutex_unlock(nlk->cb_mutex);
|
2012-10-04 20:15:48 +00:00
|
|
|
module_put(cb->module);
|
2013-08-15 22:31:06 +00:00
|
|
|
consume_skb(cb->skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
2005-06-19 05:53:48 +00:00
|
|
|
|
2006-08-05 06:03:29 +00:00
|
|
|
errout_skb:
|
2007-04-20 21:14:21 +00:00
|
|
|
mutex_unlock(nlk->cb_mutex);
|
2006-08-05 06:03:29 +00:00
|
|
|
kfree_skb(skb);
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 20:15:48 +00:00
|
|
|
int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
|
|
|
|
const struct nlmsghdr *nlh,
|
|
|
|
struct netlink_dump_control *control)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct netlink_callback *cb;
|
|
|
|
struct sock *sk;
|
|
|
|
struct netlink_sock *nlk;
|
2011-02-21 02:40:47 +00:00
|
|
|
int ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
/* Memory mapped dump requests need to be copied to avoid looping
|
|
|
|
* on the pending state in netlink_mmap_sendmsg() while the CB hold
|
|
|
|
* a reference to the skb.
|
|
|
|
*/
|
|
|
|
if (netlink_skb_is_mmaped(skb)) {
|
|
|
|
skb = skb_copy(skb, GFP_KERNEL);
|
2013-08-15 22:31:06 +00:00
|
|
|
if (skb == NULL)
|
2013-04-17 06:47:04 +00:00
|
|
|
return -ENOBUFS;
|
|
|
|
} else
|
|
|
|
atomic_inc(&skb->users);
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (sk == NULL) {
|
2013-08-15 22:31:06 +00:00
|
|
|
ret = -ECONNREFUSED;
|
|
|
|
goto error_free;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2012-10-04 20:15:48 +00:00
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
nlk = nlk_sk(sk);
|
2007-04-20 21:14:21 +00:00
|
|
|
mutex_lock(nlk->cb_mutex);
|
2012-10-04 20:15:48 +00:00
|
|
|
/* A dump is in progress... */
|
2013-08-15 22:31:06 +00:00
|
|
|
if (nlk->cb_running) {
|
2012-10-04 20:15:48 +00:00
|
|
|
ret = -EBUSY;
|
2013-08-15 22:31:06 +00:00
|
|
|
goto error_unlock;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2012-10-04 20:15:48 +00:00
|
|
|
/* add reference of module which cb->dump belongs to */
|
2013-08-15 22:31:06 +00:00
|
|
|
if (!try_module_get(control->module)) {
|
2012-10-04 20:15:48 +00:00
|
|
|
ret = -EPROTONOSUPPORT;
|
2013-08-15 22:31:06 +00:00
|
|
|
goto error_unlock;
|
2012-10-04 20:15:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
cb = &nlk->cb;
|
|
|
|
memset(cb, 0, sizeof(*cb));
|
|
|
|
cb->dump = control->dump;
|
|
|
|
cb->done = control->done;
|
|
|
|
cb->nlh = nlh;
|
|
|
|
cb->data = control->data;
|
|
|
|
cb->module = control->module;
|
|
|
|
cb->min_dump_alloc = control->min_dump_alloc;
|
|
|
|
cb->skb = skb;
|
|
|
|
|
|
|
|
nlk->cb_running = true;
|
|
|
|
|
2007-04-20 21:14:21 +00:00
|
|
|
mutex_unlock(nlk->cb_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-02-21 02:40:47 +00:00
|
|
|
ret = netlink_dump(sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_put(sk);
|
2007-10-24 03:29:25 +00:00
|
|
|
|
2011-02-21 02:40:47 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2007-10-24 03:29:25 +00:00
|
|
|
/* We successfully started a dump, by returning -EINTR we
|
|
|
|
* signal not to send ACK even if it was requested.
|
|
|
|
*/
|
|
|
|
return -EINTR;
|
2013-08-15 22:31:06 +00:00
|
|
|
|
|
|
|
error_unlock:
|
|
|
|
sock_put(sk);
|
|
|
|
mutex_unlock(nlk->cb_mutex);
|
|
|
|
error_free:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2012-10-04 20:15:48 +00:00
|
|
|
EXPORT_SYMBOL(__netlink_dump_start);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct nlmsghdr *rep;
|
|
|
|
struct nlmsgerr *errmsg;
|
2006-11-10 22:10:15 +00:00
|
|
|
size_t payload = sizeof(*errmsg);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-10 22:10:15 +00:00
|
|
|
/* error messages get the original request appened */
|
|
|
|
if (err)
|
|
|
|
payload += nlmsg_len(nlh);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-17 06:47:04 +00:00
|
|
|
skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
|
|
|
|
NETLINK_CB(in_skb).portid, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!skb) {
|
|
|
|
struct sock *sk;
|
|
|
|
|
2008-03-25 17:26:21 +00:00
|
|
|
sk = netlink_lookup(sock_net(in_skb->sk),
|
2007-09-12 11:05:38 +00:00
|
|
|
in_skb->sk->sk_protocol,
|
2012-09-07 20:12:54 +00:00
|
|
|
NETLINK_CB(in_skb).portid);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (sk) {
|
|
|
|
sk->sk_err = ENOBUFS;
|
|
|
|
sk->sk_error_report(sk);
|
|
|
|
sock_put(sk);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
|
2009-09-25 13:11:44 +00:00
|
|
|
NLMSG_ERROR, payload, 0);
|
2006-08-05 06:03:29 +00:00
|
|
|
errmsg = nlmsg_data(rep);
|
2005-04-16 22:20:36 +00:00
|
|
|
errmsg->error = err;
|
2006-08-05 06:03:29 +00:00
|
|
|
memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
|
2012-09-07 20:12:54 +00:00
|
|
|
netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_ack);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-11 04:15:29 +00:00
|
|
|
int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
|
2007-03-23 06:30:12 +00:00
|
|
|
struct nlmsghdr *))
|
2005-11-10 01:25:53 +00:00
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
while (skb->len >= nlmsg_total_size(0)) {
|
2007-10-11 04:15:29 +00:00
|
|
|
int msglen;
|
|
|
|
|
2007-04-26 02:08:35 +00:00
|
|
|
nlh = nlmsg_hdr(skb);
|
2007-03-23 06:28:46 +00:00
|
|
|
err = 0;
|
2005-11-10 01:25:53 +00:00
|
|
|
|
2006-01-10 21:02:29 +00:00
|
|
|
if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
|
2005-11-10 01:25:53 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-03-23 06:28:46 +00:00
|
|
|
/* Only requests are handled by the kernel */
|
|
|
|
if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
|
2007-10-24 03:29:25 +00:00
|
|
|
goto ack;
|
2007-03-23 06:29:10 +00:00
|
|
|
|
|
|
|
/* Skip control messages */
|
|
|
|
if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
|
2007-10-24 03:29:25 +00:00
|
|
|
goto ack;
|
2007-03-23 06:28:46 +00:00
|
|
|
|
2007-03-23 06:30:12 +00:00
|
|
|
err = cb(skb, nlh);
|
2007-10-24 03:29:25 +00:00
|
|
|
if (err == -EINTR)
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
ack:
|
2007-03-23 06:28:46 +00:00
|
|
|
if (nlh->nlmsg_flags & NLM_F_ACK || err)
|
2005-11-10 01:25:53 +00:00
|
|
|
netlink_ack(skb, nlh, err);
|
|
|
|
|
2007-10-24 03:29:25 +00:00
|
|
|
skip:
|
2007-12-04 08:19:38 +00:00
|
|
|
msglen = NLMSG_ALIGN(nlh->nlmsg_len);
|
2007-10-11 04:15:29 +00:00
|
|
|
if (msglen > skb->len)
|
|
|
|
msglen = skb->len;
|
|
|
|
skb_pull(skb, msglen);
|
2005-11-10 01:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_rcv_skb);
|
2005-11-10 01:25:53 +00:00
|
|
|
|
2006-08-15 07:31:06 +00:00
|
|
|
/**
|
|
|
|
* nlmsg_notify - send a notification netlink message
|
|
|
|
* @sk: netlink socket to use
|
|
|
|
* @skb: notification message
|
2012-09-07 20:12:54 +00:00
|
|
|
* @portid: destination netlink portid for reports or 0
|
2006-08-15 07:31:06 +00:00
|
|
|
* @group: destination multicast group or 0
|
|
|
|
* @report: 1 to report back, 0 to disable
|
|
|
|
* @flags: allocation flags
|
|
|
|
*/
|
2012-09-07 20:12:54 +00:00
|
|
|
int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
|
2006-08-15 07:31:06 +00:00
|
|
|
unsigned int group, int report, gfp_t flags)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (group) {
|
2012-09-07 20:12:54 +00:00
|
|
|
int exclude_portid = 0;
|
2006-08-15 07:31:06 +00:00
|
|
|
|
|
|
|
if (report) {
|
|
|
|
atomic_inc(&skb->users);
|
2012-09-07 20:12:54 +00:00
|
|
|
exclude_portid = portid;
|
2006-08-15 07:31:06 +00:00
|
|
|
}
|
|
|
|
|
2009-02-25 07:18:28 +00:00
|
|
|
/* errors reported via destination sk->sk_err, but propagate
|
|
|
|
* delivery errors if NETLINK_BROADCAST_ERROR flag is set */
|
2012-09-07 20:12:54 +00:00
|
|
|
err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
|
2006-08-15 07:31:06 +00:00
|
|
|
}
|
|
|
|
|
2009-02-25 07:18:28 +00:00
|
|
|
if (report) {
|
|
|
|
int err2;
|
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
err2 = nlmsg_unicast(sk, skb, portid);
|
2009-02-25 07:18:28 +00:00
|
|
|
if (!err || err == -ESRCH)
|
|
|
|
err = err2;
|
|
|
|
}
|
2006-08-15 07:31:06 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(nlmsg_notify);
|
2006-08-15 07:31:06 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct nl_seq_iter {
|
2007-11-20 06:31:54 +00:00
|
|
|
struct seq_net_private p;
|
2005-04-16 22:20:36 +00:00
|
|
|
int link;
|
|
|
|
int hash_idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct nl_seq_iter *iter = seq->private;
|
|
|
|
int i, j;
|
|
|
|
struct sock *s;
|
|
|
|
loff_t off = 0;
|
|
|
|
|
2007-12-04 08:19:38 +00:00
|
|
|
for (i = 0; i < MAX_LINKS; i++) {
|
2012-09-07 20:12:54 +00:00
|
|
|
struct nl_portid_hash *hash = &nl_table[i].hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (j = 0; j <= hash->mask; j++) {
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
sk_for_each(s, &hash->table[j]) {
|
2008-03-25 17:36:06 +00:00
|
|
|
if (sock_net(s) != seq_file_net(seq))
|
2007-09-12 11:05:38 +00:00
|
|
|
continue;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (off == pos) {
|
|
|
|
iter->link = i;
|
|
|
|
iter->hash_idx = j;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
++off;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
|
2008-01-02 05:58:02 +00:00
|
|
|
__acquires(nl_table_lock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
read_lock(&nl_table_lock);
|
|
|
|
return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct sock *s;
|
|
|
|
struct nl_seq_iter *iter;
|
2013-06-06 06:49:11 +00:00
|
|
|
struct net *net;
|
2005-04-16 22:20:36 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
++*pos;
|
|
|
|
|
|
|
|
if (v == SEQ_START_TOKEN)
|
|
|
|
return netlink_seq_socket_idx(seq, 0);
|
2007-02-09 14:25:07 +00:00
|
|
|
|
2013-06-06 06:49:11 +00:00
|
|
|
net = seq_file_net(seq);
|
2007-09-12 11:05:38 +00:00
|
|
|
iter = seq->private;
|
|
|
|
s = v;
|
|
|
|
do {
|
|
|
|
s = sk_next(s);
|
2013-06-06 06:49:11 +00:00
|
|
|
} while (s && !nl_table[s->sk_protocol].compare(net, s));
|
2005-04-16 22:20:36 +00:00
|
|
|
if (s)
|
|
|
|
return s;
|
|
|
|
|
|
|
|
i = iter->link;
|
|
|
|
j = iter->hash_idx + 1;
|
|
|
|
|
|
|
|
do {
|
2012-09-07 20:12:54 +00:00
|
|
|
struct nl_portid_hash *hash = &nl_table[i].hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (; j <= hash->mask; j++) {
|
|
|
|
s = sk_head(&hash->table[j]);
|
2013-06-06 06:49:11 +00:00
|
|
|
|
|
|
|
while (s && !nl_table[s->sk_protocol].compare(net, s))
|
2007-09-12 11:05:38 +00:00
|
|
|
s = sk_next(s);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (s) {
|
|
|
|
iter->link = i;
|
|
|
|
iter->hash_idx = j;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
} while (++i < MAX_LINKS);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_seq_stop(struct seq_file *seq, void *v)
|
2008-01-02 05:58:02 +00:00
|
|
|
__releases(nl_table_lock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
read_unlock(&nl_table_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int netlink_seq_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
2012-04-22 21:30:21 +00:00
|
|
|
if (v == SEQ_START_TOKEN) {
|
2005-04-16 22:20:36 +00:00
|
|
|
seq_puts(seq,
|
|
|
|
"sk Eth Pid Groups "
|
netlink: Adding inode field to /proc/net/netlink
The Inode field in /proc/net/{tcp,udp,packet,raw,...} is useful to know the types of
file descriptors associated to a process. Actually lsof utility uses the field.
Unfortunately, unlike /proc/net/{tcp,udp,packet,raw,...}, /proc/net/netlink doesn't have the field.
This patch adds the field to /proc/net/netlink.
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-02-27 19:45:37 +00:00
|
|
|
"Rmem Wmem Dump Locks Drops Inode\n");
|
2012-04-22 21:30:21 +00:00
|
|
|
} else {
|
2005-04-16 22:20:36 +00:00
|
|
|
struct sock *s = v;
|
|
|
|
struct netlink_sock *nlk = nlk_sk(s);
|
|
|
|
|
2013-08-15 22:31:06 +00:00
|
|
|
seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
|
2005-04-16 22:20:36 +00:00
|
|
|
s,
|
|
|
|
s->sk_protocol,
|
2012-09-07 20:12:54 +00:00
|
|
|
nlk->portid,
|
2005-09-06 22:43:59 +00:00
|
|
|
nlk->groups ? (u32)nlk->groups[0] : 0,
|
2009-06-18 02:05:41 +00:00
|
|
|
sk_rmem_alloc_get(s),
|
|
|
|
sk_wmem_alloc_get(s),
|
2013-08-15 22:31:06 +00:00
|
|
|
nlk->cb_running,
|
netlink: add NETLINK_NO_ENOBUFS socket flag
This patch adds the NETLINK_NO_ENOBUFS socket flag. This flag can
be used by unicast and broadcast listeners to avoid receiving
ENOBUFS errors.
Generally speaking, ENOBUFS errors are useful to notify two things
to the listener:
a) You may increase the receiver buffer size via setsockopt().
b) You have lost messages, you may be out of sync.
In some cases, ignoring ENOBUFS errors can be useful. For example:
a) nfnetlink_queue: this subsystem does not have any sort of resync
method and you can decide to ignore ENOBUFS once you have set a
given buffer size.
b) ctnetlink: you can use this together with the socket flag
NETLINK_BROADCAST_SEND_ERROR to stop getting ENOBUFS errors as
you do not need to resync (packets whose event are not delivered
are drop to provide reliable logging and state-synchronization).
Moreover, the use of NETLINK_NO_ENOBUFS also reduces a "go up, go down"
effect in terms of performance which is due to the netlink congestion
control when the listener cannot back off. The effect is the following:
1) throughput rate goes up and netlink messages are inserted in the
receiver buffer.
2) Then, netlink buffer fills and overruns (set on nlk->state bit 0).
3) While the listener empties the receiver buffer, netlink keeps
dropping messages. Thus, throughput goes dramatically down.
4) Then, once the listener has emptied the buffer (nlk->state
bit 0 is set off), goto step 1.
This effect is easy to trigger with netlink broadcast under heavy
load, and it is more noticeable when using a big receiver buffer.
You can find some results in [1] that show this problem.
[1] http://1984.lsi.us.es/linux/netlink/
This patch also includes the use of sk_drop to account the number of
netlink messages drop due to overrun. This value is shown in
/proc/net/netlink.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2009-03-24 23:37:55 +00:00
|
|
|
atomic_read(&s->sk_refcnt),
|
netlink: Adding inode field to /proc/net/netlink
The Inode field in /proc/net/{tcp,udp,packet,raw,...} is useful to know the types of
file descriptors associated to a process. Actually lsof utility uses the field.
Unfortunately, unlike /proc/net/{tcp,udp,packet,raw,...}, /proc/net/netlink doesn't have the field.
This patch adds the field to /proc/net/netlink.
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-02-27 19:45:37 +00:00
|
|
|
atomic_read(&s->sk_drops),
|
|
|
|
sock_i_ino(s)
|
2005-04-16 22:20:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-11 06:07:31 +00:00
|
|
|
static const struct seq_operations netlink_seq_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.start = netlink_seq_start,
|
|
|
|
.next = netlink_seq_next,
|
|
|
|
.stop = netlink_seq_stop,
|
|
|
|
.show = netlink_seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int netlink_seq_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-11-20 06:31:54 +00:00
|
|
|
return seq_open_net(inode, file, &netlink_seq_ops,
|
|
|
|
sizeof(struct nl_seq_iter));
|
2007-09-12 11:05:38 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 08:55:36 +00:00
|
|
|
static const struct file_operations netlink_seq_fops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = netlink_seq_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2007-11-20 06:31:54 +00:00
|
|
|
.release = seq_release_net,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int netlink_register_notifier(struct notifier_block *nb)
|
|
|
|
{
|
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no
protection against entries being added to or removed from a chain while the
chain is in use. The issues were discussed in this thread:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2
We noticed that notifier chains in the kernel fall into two basic usage
classes:
"Blocking" chains are always called from a process context
and the callout routines are allowed to sleep;
"Atomic" chains can be called from an atomic context and
the callout routines are not allowed to sleep.
We decided to codify this distinction and make it part of the API. Therefore
this set of patches introduces three new, parallel APIs: one for blocking
notifiers, one for atomic notifiers, and one for "raw" notifiers (which is
really just the old API under a new name). New kinds of data structures are
used for the heads of the chains, and new routines are defined for
registration, unregistration, and calling a chain. The three APIs are
explained in include/linux/notifier.h and their implementation is in
kernel/sys.c.
With atomic and blocking chains, the implementation guarantees that the chain
links will not be corrupted and that chain callers will not get messed up by
entries being added or removed. For raw chains the implementation provides no
guarantees at all; users of this API must provide their own protections. (The
idea was that situations may come up where the assumptions of the atomic and
blocking APIs are not appropriate, so it should be possible for users to
handle these things in their own way.)
There are some limitations, which should not be too hard to live with. For
atomic/blocking chains, registration and unregistration must always be done in
a process context since the chain is protected by a mutex/rwsem. Also, a
callout routine for a non-raw chain must not try to register or unregister
entries on its own chain. (This did happen in a couple of places and the code
had to be changed to avoid it.)
Since atomic chains may be called from within an NMI handler, they cannot use
spinlocks for synchronization. Instead we use RCU. The overhead falls almost
entirely in the unregister routine, which is okay since unregistration is much
less frequent that calling a chain.
Here is the list of chains that we adjusted and their classifications. None
of them use the raw API, so for the moment it is only a placeholder.
ATOMIC CHAINS
-------------
arch/i386/kernel/traps.c: i386die_chain
arch/ia64/kernel/traps.c: ia64die_chain
arch/powerpc/kernel/traps.c: powerpc_die_chain
arch/sparc64/kernel/traps.c: sparc64die_chain
arch/x86_64/kernel/traps.c: die_chain
drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list
kernel/panic.c: panic_notifier_list
kernel/profile.c: task_free_notifier
net/bluetooth/hci_core.c: hci_notifier
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain
net/ipv6/addrconf.c: inet6addr_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain
net/netlink/af_netlink.c: netlink_chain
BLOCKING CHAINS
---------------
arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain
arch/s390/kernel/process.c: idle_chain
arch/x86_64/kernel/process.c idle_notifier
drivers/base/memory.c: memory_chain
drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list
drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list
drivers/macintosh/adb.c: adb_client_list
drivers/macintosh/via-pmu.c sleep_notifier_list
drivers/macintosh/via-pmu68k.c sleep_notifier_list
drivers/macintosh/windfarm_core.c wf_client_list
drivers/usb/core/notify.c usb_notifier_list
drivers/video/fbmem.c fb_notifier_list
kernel/cpu.c cpu_chain
kernel/module.c module_notify_list
kernel/profile.c munmap_notifier
kernel/profile.c task_exit_notifier
kernel/sys.c reboot_notifier_list
net/core/dev.c netdev_chain
net/decnet/dn_dev.c: dnaddr_chain
net/ipv4/devinet.c: inetaddr_chain
It's possible that some of these classifications are wrong. If they are,
please let us know or submit a patch to fix them. Note that any chain that
gets called very frequently should be atomic, because the rwsem read-locking
used for blocking chains is very likely to incur cache misses on SMP systems.
(However, if the chain's callout routines may sleep then the chain cannot be
atomic.)
The patch set was written by Alan Stern and Chandra Seetharaman, incorporating
material written by Keith Owens and suggestions from Paul McKenney and Andrew
Morton.
[jes@sgi.com: restructure the notifier chain initialization macros]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-03-27 09:16:30 +00:00
|
|
|
return atomic_notifier_chain_register(&netlink_chain, nb);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_register_notifier);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
int netlink_unregister_notifier(struct notifier_block *nb)
|
|
|
|
{
|
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no
protection against entries being added to or removed from a chain while the
chain is in use. The issues were discussed in this thread:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2
We noticed that notifier chains in the kernel fall into two basic usage
classes:
"Blocking" chains are always called from a process context
and the callout routines are allowed to sleep;
"Atomic" chains can be called from an atomic context and
the callout routines are not allowed to sleep.
We decided to codify this distinction and make it part of the API. Therefore
this set of patches introduces three new, parallel APIs: one for blocking
notifiers, one for atomic notifiers, and one for "raw" notifiers (which is
really just the old API under a new name). New kinds of data structures are
used for the heads of the chains, and new routines are defined for
registration, unregistration, and calling a chain. The three APIs are
explained in include/linux/notifier.h and their implementation is in
kernel/sys.c.
With atomic and blocking chains, the implementation guarantees that the chain
links will not be corrupted and that chain callers will not get messed up by
entries being added or removed. For raw chains the implementation provides no
guarantees at all; users of this API must provide their own protections. (The
idea was that situations may come up where the assumptions of the atomic and
blocking APIs are not appropriate, so it should be possible for users to
handle these things in their own way.)
There are some limitations, which should not be too hard to live with. For
atomic/blocking chains, registration and unregistration must always be done in
a process context since the chain is protected by a mutex/rwsem. Also, a
callout routine for a non-raw chain must not try to register or unregister
entries on its own chain. (This did happen in a couple of places and the code
had to be changed to avoid it.)
Since atomic chains may be called from within an NMI handler, they cannot use
spinlocks for synchronization. Instead we use RCU. The overhead falls almost
entirely in the unregister routine, which is okay since unregistration is much
less frequent that calling a chain.
Here is the list of chains that we adjusted and their classifications. None
of them use the raw API, so for the moment it is only a placeholder.
ATOMIC CHAINS
-------------
arch/i386/kernel/traps.c: i386die_chain
arch/ia64/kernel/traps.c: ia64die_chain
arch/powerpc/kernel/traps.c: powerpc_die_chain
arch/sparc64/kernel/traps.c: sparc64die_chain
arch/x86_64/kernel/traps.c: die_chain
drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list
kernel/panic.c: panic_notifier_list
kernel/profile.c: task_free_notifier
net/bluetooth/hci_core.c: hci_notifier
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain
net/ipv6/addrconf.c: inet6addr_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain
net/netlink/af_netlink.c: netlink_chain
BLOCKING CHAINS
---------------
arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain
arch/s390/kernel/process.c: idle_chain
arch/x86_64/kernel/process.c idle_notifier
drivers/base/memory.c: memory_chain
drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list
drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list
drivers/macintosh/adb.c: adb_client_list
drivers/macintosh/via-pmu.c sleep_notifier_list
drivers/macintosh/via-pmu68k.c sleep_notifier_list
drivers/macintosh/windfarm_core.c wf_client_list
drivers/usb/core/notify.c usb_notifier_list
drivers/video/fbmem.c fb_notifier_list
kernel/cpu.c cpu_chain
kernel/module.c module_notify_list
kernel/profile.c munmap_notifier
kernel/profile.c task_exit_notifier
kernel/sys.c reboot_notifier_list
net/core/dev.c netdev_chain
net/decnet/dn_dev.c: dnaddr_chain
net/ipv4/devinet.c: inetaddr_chain
It's possible that some of these classifications are wrong. If they are,
please let us know or submit a patch to fix them. Note that any chain that
gets called very frequently should be atomic, because the rwsem read-locking
used for blocking chains is very likely to incur cache misses on SMP systems.
(However, if the chain's callout routines may sleep then the chain cannot be
atomic.)
The patch set was written by Alan Stern and Chandra Seetharaman, incorporating
material written by Keith Owens and suggestions from Paul McKenney and Andrew
Morton.
[jes@sgi.com: restructure the notifier chain initialization macros]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-03-27 09:16:30 +00:00
|
|
|
return atomic_notifier_chain_unregister(&netlink_chain, nb);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-12-04 08:19:38 +00:00
|
|
|
EXPORT_SYMBOL(netlink_unregister_notifier);
|
2007-02-09 14:25:07 +00:00
|
|
|
|
2005-12-22 20:49:22 +00:00
|
|
|
static const struct proto_ops netlink_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.family = PF_NETLINK,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.release = netlink_release,
|
|
|
|
.bind = netlink_bind,
|
|
|
|
.connect = netlink_connect,
|
|
|
|
.socketpair = sock_no_socketpair,
|
|
|
|
.accept = sock_no_accept,
|
|
|
|
.getname = netlink_getname,
|
2013-04-17 06:47:02 +00:00
|
|
|
.poll = netlink_poll,
|
2005-04-16 22:20:36 +00:00
|
|
|
.ioctl = sock_no_ioctl,
|
|
|
|
.listen = sock_no_listen,
|
|
|
|
.shutdown = sock_no_shutdown,
|
2005-08-15 19:32:15 +00:00
|
|
|
.setsockopt = netlink_setsockopt,
|
|
|
|
.getsockopt = netlink_getsockopt,
|
2005-04-16 22:20:36 +00:00
|
|
|
.sendmsg = netlink_sendmsg,
|
|
|
|
.recvmsg = netlink_recvmsg,
|
2013-04-17 06:47:01 +00:00
|
|
|
.mmap = netlink_mmap,
|
2005-04-16 22:20:36 +00:00
|
|
|
.sendpage = sock_no_sendpage,
|
|
|
|
};
|
|
|
|
|
2009-10-05 05:58:39 +00:00
|
|
|
static const struct net_proto_family netlink_family_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.family = PF_NETLINK,
|
|
|
|
.create = netlink_create,
|
|
|
|
.owner = THIS_MODULE, /* for consistency 8) */
|
|
|
|
};
|
|
|
|
|
2007-10-09 03:38:39 +00:00
|
|
|
static int __net_init netlink_net_init(struct net *net)
|
2007-09-12 11:05:38 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2013-02-18 01:34:54 +00:00
|
|
|
if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
|
2007-09-12 11:05:38 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-09 03:38:39 +00:00
|
|
|
static void __net_exit netlink_net_exit(struct net *net)
|
2007-09-12 11:05:38 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2013-02-18 01:34:56 +00:00
|
|
|
remove_proc_entry("netlink", net->proc_net);
|
2007-09-12 11:05:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-31 02:08:01 +00:00
|
|
|
static void __init netlink_add_usersock_entry(void)
|
|
|
|
{
|
2010-10-24 04:27:10 +00:00
|
|
|
struct listeners *listeners;
|
2010-08-31 02:08:01 +00:00
|
|
|
int groups = 32;
|
|
|
|
|
2010-10-24 04:27:10 +00:00
|
|
|
listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
|
2010-08-31 02:08:01 +00:00
|
|
|
if (!listeners)
|
2010-10-24 04:27:10 +00:00
|
|
|
panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
|
2010-08-31 02:08:01 +00:00
|
|
|
|
|
|
|
netlink_table_grab();
|
|
|
|
|
|
|
|
nl_table[NETLINK_USERSOCK].groups = groups;
|
2010-10-24 04:27:10 +00:00
|
|
|
rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
|
2010-08-31 02:08:01 +00:00
|
|
|
nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
|
|
|
|
nl_table[NETLINK_USERSOCK].registered = 1;
|
2012-09-08 02:53:53 +00:00
|
|
|
nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
|
2010-08-31 02:08:01 +00:00
|
|
|
|
|
|
|
netlink_table_ungrab();
|
|
|
|
}
|
|
|
|
|
2007-11-13 11:23:50 +00:00
|
|
|
static struct pernet_operations __net_initdata netlink_net_ops = {
|
2007-09-12 11:05:38 +00:00
|
|
|
.init = netlink_net_init,
|
|
|
|
.exit = netlink_net_exit,
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int __init netlink_proto_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
2007-09-16 23:36:02 +00:00
|
|
|
unsigned long limit;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned int order;
|
|
|
|
int err = proto_register(&netlink_proto, 0);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-01-09 07:19:48 +00:00
|
|
|
BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-21 21:51:30 +00:00
|
|
|
nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
|
2006-08-29 09:15:24 +00:00
|
|
|
if (!nl_table)
|
|
|
|
goto panic;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-09-22 00:03:05 +00:00
|
|
|
if (totalram_pages >= (128 * 1024))
|
|
|
|
limit = totalram_pages >> (21 - PAGE_SHIFT);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2009-09-22 00:03:05 +00:00
|
|
|
limit = totalram_pages >> (23 - PAGE_SHIFT);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-09-16 23:36:02 +00:00
|
|
|
order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
|
|
|
|
limit = (1UL << order) / sizeof(struct hlist_head);
|
|
|
|
order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_LINKS; i++) {
|
2012-09-07 20:12:54 +00:00
|
|
|
struct nl_portid_hash *hash = &nl_table[i].hash;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-09-07 20:12:54 +00:00
|
|
|
hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!hash->table) {
|
|
|
|
while (i-- > 0)
|
2012-09-07 20:12:54 +00:00
|
|
|
nl_portid_hash_free(nl_table[i].hash.table,
|
2005-04-16 22:20:36 +00:00
|
|
|
1 * sizeof(*hash->table));
|
|
|
|
kfree(nl_table);
|
2006-08-29 09:15:24 +00:00
|
|
|
goto panic;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
hash->max_shift = order;
|
|
|
|
hash->shift = 0;
|
|
|
|
hash->mask = 0;
|
|
|
|
hash->rehash_time = jiffies;
|
2013-06-13 02:05:38 +00:00
|
|
|
|
|
|
|
nl_table[i].compare = netlink_compare;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 17:38:07 +00:00
|
|
|
INIT_LIST_HEAD(&netlink_tap_all);
|
|
|
|
|
2010-08-31 02:08:01 +00:00
|
|
|
netlink_add_usersock_entry();
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
sock_register(&netlink_family_ops);
|
2007-09-12 11:05:38 +00:00
|
|
|
register_pernet_subsys(&netlink_net_ops);
|
2007-02-09 14:25:07 +00:00
|
|
|
/* The netlink device handler may be needed early. */
|
2005-04-16 22:20:36 +00:00
|
|
|
rtnetlink_init();
|
|
|
|
out:
|
|
|
|
return err;
|
2006-08-29 09:15:24 +00:00
|
|
|
panic:
|
|
|
|
panic("netlink_init: Cannot allocate nl_table\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
core_initcall(netlink_proto_init);
|