Skip to contentSkip to conversation

netlink: implement socket diag for netlink sockets (v2)

PATCH Applied Andrey Vagin 2 participants 7 messages Mar 21, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover netlink: implement socket diag for netlink sockets (v2)

v2: fix *_DIAG_MAX constants

Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Gao feng <[email protected]>
Cc Thomas Graf <[email protected]>

Andrey Vagin (3):
net: fix *_DIAG_MAX constants
net: prepare netlink code for netlink diag
netlink: Diag core and basic socket info dumping (v2)

 include/uapi/linux/netlink_diag.h |  42 +++++++++
 include/uapi/linux/packet_diag.h  |   4 +-
 include/uapi/linux/unix_diag.h    |   4 +-
 net/Kconfig                       |   1 +
 net/netlink/Kconfig               |  10 ++
 net/netlink/Makefile              |   3 +
 net/netlink/af_netlink.c          |  59 +-----------
 net/netlink/af_netlink.h          |  62 +++++++++++++
 net/netlink/diag.c                | 188 ++++++++++++++++++++++++++++++++++++++
 9 files changed, 317 insertions(+), 56 deletions(-)
 create mode 100644 include/uapi/linux/netlink_diag.h
 create mode 100644 net/netlink/Kconfig
 create mode 100644 net/netlink/af_netlink.h
 create mode 100644 net/netlink/diag.c
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/3 net: fix *_DIAG_MAX constants

Follow the common pattern and define *_DIAG_MAX like:

        [...]
        __XXX_DIAG_MAX,
};

Because everyone is used to do:

        struct nlattr *attrs[XXX_DIAG_MAX+1];

        nla_parse([...], XXX_DIAG_MAX, [...]
Cc "David S. Miller" <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc David Howells <[email protected]>
---
 include/uapi/linux/packet_diag.h | 4 +++-
 include/uapi/linux/unix_diag.h   | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/uapi/linux/packet_diag.h +3 -1
index 93f5fa9..afafd70 100644
@@ -33,9 +33,11 @@ enum {
 	PACKET_DIAG_TX_RING,
 	PACKET_DIAG_FANOUT,
 
-	PACKET_DIAG_MAX,
+	__PACKET_DIAG_MAX,
 };
 
+#define PACKET_DIAG_MAX (__PACKET_DIAG_MAX - 1)
+
 struct packet_diag_info {
 	__u32	pdi_index;
 	__u32	pdi_version;
include/uapi/linux/unix_diag.h +3 -1
index b8a2494..b9e2a6a 100644
@@ -39,9 +39,11 @@ enum {
 	UNIX_DIAG_MEMINFO,
 	UNIX_DIAG_SHUTDOWN,
 
-	UNIX_DIAG_MAX,
+	__UNIX_DIAG_MAX,
 };
 
+#define UNIX_DIAG_MAX (__UNIX_DIAG_MAX - 1)
+
 struct unix_diag_vfs {
 	__u32	udiag_vfs_ino;
 	__u32	udiag_vfs_dev;
Signature · 1 line
1.8.1.4
maintainer Applied
#2 lore raw

Re: [PATCH 1/3] net: fix *_DIAG_MAX constants

From: Andrey Vagin <[email protected]>
Date: Thu, 21 Mar 2013 20:33:46 +0400

Quoted 12 lines

Follow the common pattern and define *_DIAG_MAX like:

        [...]
        __XXX_DIAG_MAX,
};

Because everyone is used to do:

        struct nlattr *attrs[XXX_DIAG_MAX+1];

        nla_parse([...], XXX_DIAG_MAX, [...]
Quoted 7 lines
Cc "David S. Miller" <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc David Howells <[email protected]>

Applied to 'net' and queued up for -stable.

Thanks.

#3 lore raw

PATCH 2/3 net: prepare netlink code for netlink diag

In reply to Andrey Vagin

Move a few declarations in a header.

Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Gao feng <[email protected]>
Cc Thomas Graf <[email protected]>
---
 net/netlink/af_netlink.c | 59 ++++-----------------------------------------
 net/netlink/af_netlink.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 54 deletions(-)
 create mode 100644 net/netlink/af_netlink.h
net/netlink/af_netlink.c +5 -54
index 1e3fd5b..a500ce2 100644
@@ -61,28 +61,7 @@
 #include <net/scm.h>
 #include <net/netlink.h>
 
-#define NLGRPSZ(x)	(ALIGN(x, sizeof(unsigned long) * 8) / 8)
-#define NLGRPLONGS(x)	(NLGRPSZ(x)/sizeof(unsigned long))
-
-struct netlink_sock {
-	/* struct sock has to be the first member of netlink_sock */
-	struct sock		sk;
-	u32			portid;
-	u32			dst_portid;
-	u32			dst_group;
-	u32			flags;
-	u32			subscriptions;
-	u32			ngroups;
-	unsigned long		*groups;
-	unsigned long		state;
-	wait_queue_head_t	wait;
-	struct netlink_callback	*cb;
-	struct mutex		*cb_mutex;
-	struct mutex		cb_def_mutex;
-	void			(*netlink_rcv)(struct sk_buff *skb);
-	void			(*netlink_bind)(int group);
-	struct module		*module;
-};
+#include "af_netlink.h"
 
 struct listeners {
 	struct rcu_head		rcu;
@@ -94,48 +73,20 @@ struct listeners {
 #define NETLINK_BROADCAST_SEND_ERROR	0x4
 #define NETLINK_RECV_NO_ENOBUFS	0x8
 
-static inline struct netlink_sock *nlk_sk(struct sock *sk)
-{
-	return container_of(sk, struct netlink_sock, sk);
-}
-
 static inline int netlink_is_kernel(struct sock *sk)
 {
 	return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
 }
 
-struct nl_portid_hash {
-	struct hlist_head	*table;
-	unsigned long		rehash_time;
-
-	unsigned int		mask;
-	unsigned int		shift;
-
-	unsigned int		entries;
-	unsigned int		max_shift;
-
-	u32			rnd;
-};
-
-struct netlink_table {
-	struct nl_portid_hash	hash;
-	struct hlist_head	mc_list;
-	struct listeners __rcu	*listeners;
-	unsigned int		flags;
-	unsigned int		groups;
-	struct mutex		*cb_mutex;
-	struct module		*module;
-	void			(*bind)(int group);
-	int			registered;
-};
-
-static struct netlink_table *nl_table;
+struct netlink_table *nl_table;
+EXPORT_SYMBOL_GPL(nl_table);
 
 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
 
 static int netlink_dump(struct sock *sk);
 
-static DEFINE_RWLOCK(nl_table_lock);
+DEFINE_RWLOCK(nl_table_lock);
+EXPORT_SYMBOL_GPL(nl_table_lock);
 static atomic_t nl_table_users = ATOMIC_INIT(0);
 
 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
net/netlink/af_netlink.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.8.1.4
maintainer Applied
#4 lore raw

Re: [PATCH 2/3] net: prepare netlink code for netlink diag

From: Andrey Vagin <[email protected]>
Date: Thu, 21 Mar 2013 20:33:47 +0400

Quoted 10 lines

Move a few declarations in a header.

Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Gao feng <[email protected]>
Cc Thomas Graf <[email protected]>

Applied to net-next.

#5 lore raw

PATCH 3/3 netlink: Diag core and basic socket info dumping (v2)

In reply to Andrey Vagin

The netlink_diag can be built as a module, just like it's done in unix sockets.

The core dumping message carries the basic info about netlink sockets:
family, type and protocol, portis, dst_group, dst_portid, state.

Groups can be received as an optional parameter NETLINK_DIAG_GROUPS.

Netlink sockets cab be filtered by protocols.

The socket inode number and cookie is reserved for future per-socket info retrieving. The per-protocol filtering is also reserved for future by requiring the sdiag_protocol to be zero.

The file /proc/net/netlink doesn't provide enough information for dumping netlink sockets. It doesn't provide dst_group, dst_portid, groups above 32.

v2: fix NETLINK_DIAG_MAX. Now it's equal to the last constant.

Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Gao feng <[email protected]>
Cc Thomas Graf <[email protected]>
---
 include/uapi/linux/netlink_diag.h |  42 +++++++++
 net/Kconfig                       |   1 +
 net/netlink/Kconfig               |  10 ++
 net/netlink/Makefile              |   3 +
 net/netlink/diag.c                | 188 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 244 insertions(+)
 create mode 100644 include/uapi/linux/netlink_diag.h
 create mode 100644 net/netlink/Kconfig
 create mode 100644 net/netlink/diag.c
include/uapi/linux/netlink_diag.h new file +42 -0
new file mode 100644
index 0000000..88009a3
@@ -0,0 +1,42 @@
+#ifndef __NETLINK_DIAG_H__
+#define __NETLINK_DIAG_H__
+
+#include <linux/types.h>
+
+struct netlink_diag_req {
+	__u8	sdiag_family;
+	__u8	sdiag_protocol;
+	__u16	pad;
+	__u32	ndiag_ino;
+	__u32	ndiag_show;
+	__u32	ndiag_cookie[2];
+};
+
+struct netlink_diag_msg {
+	__u8	ndiag_family;
+	__u8	ndiag_type;
+	__u8	ndiag_protocol;
+	__u8	ndiag_state;
+
+	__u32	ndiag_portid;
+	__u32	ndiag_dst_portid;
+	__u32	ndiag_dst_group;
+	__u32	ndiag_ino;
+	__u32	ndiag_cookie[2];
+};
+
+enum {
+	NETLINK_DIAG_MEMINFO,
+	NETLINK_DIAG_GROUPS,
+
+	__NETLINK_DIAG_MAX,
+};
+
+#define NETLINK_DIAG_MAX (__NETLINK_DIAG_MAX - 1)
+
+#define NDIAG_PROTO_ALL		((__u8) ~0)
+
+#define NDIAG_SHOW_MEMINFO	0x00000001 /* show memory info of a socket */
+#define NDIAG_SHOW_GROUPS	0x00000002 /* show groups of a netlink socket */
+
+#endif
net/Kconfig +1 -0
index 6f676ab..2ddc904 100644
@@ -217,6 +217,7 @@ source "net/dns_resolver/Kconfig"
 source "net/batman-adv/Kconfig"
 source "net/openvswitch/Kconfig"
 source "net/vmw_vsock/Kconfig"
+source "net/netlink/Kconfig"
 
 config RPS
 	boolean
net/netlink/Kconfig new file +10 -0
new file mode 100644
index 0000000..5d6e8c0
@@ -0,0 +1,10 @@
+#
+# Netlink Sockets
+#
+
+config NETLINK_DIAG
+	tristate "NETLINK: socket monitoring interface"
+	default n
+	---help---
+	  Support for NETLINK socket monitoring interface used by the ss tool.
+	  If unsure, say Y.
net/netlink/Makefile +3 -0
index bdd6ddf..e837917 100644
@@ -3,3 +3,6 @@
 #
 
 obj-y  				:= af_netlink.o genetlink.o
+
+obj-$(CONFIG_NETLINK_DIAG)	+= netlink_diag.o
+netlink_diag-y			:= diag.o
net/netlink/diag.c new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
1.8.1.4
maintainer Applied
#6 lore raw

Re: [PATCH 3/3] netlink: Diag core and basic socket info dumping (v2)

From: Andrey Vagin <[email protected]>
Date: Thu, 21 Mar 2013 20:33:48 +0400

Quoted 20 lines

The netlink_diag can be built as a module, just like it's done in unix sockets.

The core dumping message carries the basic info about netlink sockets:
family, type and protocol, portis, dst_group, dst_portid, state.

Groups can be received as an optional parameter NETLINK_DIAG_GROUPS.

Netlink sockets cab be filtered by protocols.

The socket inode number and cookie is reserved for future per-socket info retrieving. The per-protocol filtering is also reserved for future by requiring the sdiag_protocol to be zero.

The file /proc/net/netlink doesn't provide enough information for dumping netlink sockets. It doesn't provide dst_group, dst_portid, groups above 32.

v2: fix NETLINK_DIAG_MAX. Now it's equal to the last constant.

Quoted 8 lines
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Gao feng <[email protected]>
Cc Thomas Graf <[email protected]>

Applied to net-next