Skip to contentSkip to conversation

Add support for operating raw sockest via diag interface

PATCH Applied Cyrill Gorcunov 3 participants 6 messages Nov 2, 2016 – Dec 1, 2016

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

v2 cover Add support for operating raw sockest via diag interface

The diag interface for raw sockets is now in linux-net-next
http://git.kernel.org/cgit/linux/kernel/git/davem/net-next.git/commit/?id=432490f9d455fb842d70219f22d9d2c812371676
so here is early patches for misc/ss

I've to update libnetlink code to keep backward compatibility and switch to parse procfs output if no raw_diag module present in the system. Note the error reporting sitting in the kernel since 2006 so it's not something new, I guess this hasn't been done for other diag modules because it been assumed that they are always here. Which is not applied to fresh raw-diag module.

Cyrill

Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/2 libnetlink: Add test for error code returned from netlink reply

In case if some diag module is not present in the system, say the kernel is not modern enough, we simply skip the error code reported. Instead we should check for data length in NLMSG_DONE and process unsupported case.

---
 lib/libnetlink.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/libnetlink.c +16 -0
index 2279935..232daee 100644
@@ -312,6 +312,22 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
 					dump_intr = 1;
 
 				if (h->nlmsg_type == NLMSG_DONE) {
+					if (rth->proto == NETLINK_SOCK_DIAG) {
+						if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) {
+							fprintf(stderr, "DONE truncated\n");
+							return -1;
+						} else {
+							int len = *(int *)NLMSG_DATA(h);
+							if (len < 0) {
+								errno = -len;
+								if (errno == ENOENT ||
+								    errno == EOPNOTSUPP)
+									return -1;
+								perror("RTNETLINK answers");
+								return len;
+							}
+						}
+					}
 					found_done = 1;
 					break; /* process next filter */
 				}
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/2 ss: Add inet raw sockets information gathering via netlink diag interface

In reply to Cyrill Gorcunov

unix, tcp, udp[lite], packet, netlink sockets already support diag interface for their collection and killing. Implement support for raw sockets.

---
 include/linux/inet_diag.h | 15 +++++++++++++++
 misc/ss.c                 | 20 ++++++++++++++++++--
 2 files changed, 33 insertions(+), 2 deletions(-)
include/linux/inet_diag.h +15 -0
index f5f5c1b..ac66148 100644
@@ -43,6 +43,21 @@ struct inet_diag_req_v2 {
 	struct inet_diag_sockid id;
 };
 
+/*
+ * An alias for struct inet_diag_req_v2,
+ * @sdiag_raw_protocol member shadows
+ * @pad explicitly, it is done this way
+ * for backward compatibility sake.
+ */
+struct inet_diag_req_raw {
+	__u8	sdiag_family;
+	__u8	sdiag_protocol;
+	__u8	idiag_ext;
+	__u8	sdiag_raw_protocol;
+	__u32	idiag_states;
+	struct inet_diag_sockid id;
+};
+
 enum {
 	INET_DIAG_REQ_NONE,
 	INET_DIAG_REQ_BYTECODE,
misc/ss.c +18 -2
index dd77b81..e8c4010 100644
@@ -724,6 +724,7 @@ struct sockstat {
 	struct sockstat	   *next;
 	unsigned int	    type;
 	uint16_t	    prot;
+	uint16_t	    raw_prot;
 	inet_prefix	    local;
 	inet_prefix	    remote;
 	int		    lport;
@@ -2190,6 +2191,10 @@ static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
 	s->mark = 0;
 	if (tb[INET_DIAG_MARK])
 		s->mark = *(__u32 *) RTA_DATA(tb[INET_DIAG_MARK]);
+	if (tb[INET_DIAG_PROTOCOL])
+		s->raw_prot = *(__u8 *)RTA_DATA(tb[INET_DIAG_PROTOCOL]);
+	else
+		s->raw_prot = 0;
 
 	if (s->local.family == AF_INET)
 		s->local.bytelen = s->remote.bytelen = 4;
@@ -2384,7 +2389,7 @@ struct inet_diag_arg {
 	struct rtnl_handle *rth;
 };
 
-static int kill_inet_sock(struct nlmsghdr *h, void *arg)
+static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s)
 {
 	struct inet_diag_msg *d = NLMSG_DATA(h);
 	struct inet_diag_arg *diag_arg = arg;
@@ -2399,6 +2404,13 @@ static int kill_inet_sock(struct nlmsghdr *h, void *arg)
 	req.r.sdiag_protocol = diag_arg->protocol;
 	req.r.id = d->id;
 
+	if (diag_arg->protocol == IPPROTO_RAW) {
+		struct inet_diag_req_raw *raw = (void *)&req.r;
+
+		BUILD_BUG_ON(sizeof(req.r) != sizeof(*raw));
+		raw->sdiag_raw_protocol = s->raw_prot;
+	}
+
 	return rtnl_talk(rth, &req.nlh, NULL, 0);
 }
 
@@ -2418,7 +2430,7 @@ static int show_one_inet_sock(const struct sockaddr_nl *addr,
 	if (diag_arg->f->f && run_ssfilter(diag_arg->f->f, &s) == 0)
 		return 0;
 
-	if (diag_arg->f->kill && kill_inet_sock(h, arg) != 0) {
+	if (diag_arg->f->kill && kill_inet_sock(h, arg, &s) != 0) {
 		if (errno == EOPNOTSUPP || errno == ENOENT) {
 			/* Socket can't be closed, or is already closed. */
 			return 0;
@@ -2715,6 +2727,10 @@ static int raw_show(struct filter *f)
 
 	dg_proto = RAW_PROTO;
 
+	if (!getenv("PROC_NET_RAW") && !getenv("PROC_ROOT") &&
+	    inet_show_netlink(f, NULL, IPPROTO_RAW) == 0)
+		return 0;
+
 	if (f->families&(1<<AF_INET)) {
 		if ((fp = net_raw_open()) == NULL)
 			goto outerr;
Signature · 1 line
2.7.4
Acked-by on v2 2/2
#3 lore raw

Re: [PATCH net-next iproute2 PATCH 2/2 v2] ss: Add inet raw sockets information gathering via netlink diag interface

Quoted 9 lines · Cyrill Gorcunov

unix, tcp, udp[lite], packet, netlink sockets already support diag interface for their collection and killing. Implement support for raw sockets.

---
 include/linux/inet_diag.h | 15 +++++++++++++++
 misc/ss.c                 | 20 ++++++++++++++++++--
 2 files changed, 33 insertions(+), 2 deletions(-)

worked for me.

#4 lore raw

Re: [PATCH net-next iproute2 PATCH 2/2 v2] ss: Add inet raw sockets information gathering via netlink diag interface

In reply to Cyrill Gorcunov
Quoted 5 lines · On Wed, 2 Nov 2016 16:14:56 +0300 Cyrill Gorcunov <[email protected]> wrote:

unix, tcp, udp[lite], packet, netlink sockets already support diag interface for their collection and killing. Implement support for raw sockets.

Applied both patches, but needed to remove inet_diag.h since already updated kernel headers.

#5 lore raw

Re: [PATCH net-next iproute2 PATCH 2/2 v2] ss: Add inet raw sockets information gathering via netlink diag interface

Quoted 3 lines · Stephen Hemminger

Applied both patches, but needed to remove inet_diag.h since already updated kernel headers.

Thank you! I think we might need to extend the matching interface for killing raw sockets in near future, because for now it is too wildcard. I put this into my todo list, once I finish with more urgent tasks will back to this one.