Skip to contentSkip to conversation

libnetlink: add a variant of rtnl_send_check that consumes ACKs

PATCH Superseded Lorenzo Colitti 3 participants 16 messages Dec 17, 2015 – Jan 18, 2016

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

PATCH v2 1/2 libnetlink: add a variant of rtnl_send_check that consumes ACKs

The new variant is identical to rtnl_send_check, except it also consumes the kernel response instead of using MSG_PEEK. This is useful for callers that send simple commands that never cause a response but only ACKs, and that expect to receive and deal with errors without printing them to stderr like rtnl_talk does.

---
 include/libnetlink.h |  2 ++
 lib/libnetlink.c     | 14 +++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)
include/libnetlink.h +2 -0
index 431189e..a88cb4d 100644
@@ -75,6 +75,8 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
 	__attribute__((warn_unused_result));
 int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
 	__attribute__((warn_unused_result));
+int rtnl_send_check_ack(struct rtnl_handle *rth, const void *buf, int, int)
+	__attribute__((warn_unused_result));
 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
 	__attribute__((warn_unused_result));
 
lib/libnetlink.c +11 -3
index 1658214..a3ad83a 100644
@@ -134,18 +134,21 @@ int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
 	return send(rth->fd, buf, len, 0);
 }
 
-int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
+int rtnl_send_check_ack(struct rtnl_handle *rth, const void *buf, int len,
+		int ack)
 {
 	struct nlmsghdr *h;
-	int status;
+	int status, flags;
 	char resp[1024];
 
 	status = send(rth->fd, buf, len, 0);
 	if (status < 0)
 		return status;
 
+	flags = MSG_DONTWAIT | (ack ? 0 : MSG_PEEK);
+
 	/* Check for immediate errors */
-	status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
+	status = recv(rth->fd, resp, sizeof(resp), flags);
 	if (status < 0) {
 		if (errno == EAGAIN)
 			return 0;
@@ -167,6 +170,11 @@ int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
 	return 0;
 }
 
+inline int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
+{
+	return rtnl_send_check_ack(rth, buf, len, 0);
+}
+
 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
 {
 	struct nlmsghdr nlh;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#1 lore raw

PATCH v2 2/2 ss: support closing inet sockets via SOCK_DESTROY.

This patch adds a -K / --kill option to ss that attempts to forcibly close matching sockets using SOCK_DESTROY.

This is implemented by adding a new "struct action" to struct filter. If necessary, this can be extended later on to support further actions on sockets.

Because ss typically prints sockets instead of acting on them, and because the kernel only suppors forcibly closing some types of sockets, the output of -K is as follows:

- If closing the socket succeeds, the socket is printed.
- If the kernel does not support forcibly closing this type of socket (e.g., if it's a UDP socket, or a TIME_WAIT socket), the socket is silently skipped.
- If an error occurs (e.g., permission denied), the error is reported and ss exits.

---
 include/linux/sock_diag.h |  1 +
 misc/ss.c                 | 52 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)
include/linux/sock_diag.h +1 -0
index 024e1f4..dafcb89 100644
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 
 #define SOCK_DIAG_BY_FAMILY 20
+#define SOCK_DESTROY 21
 
 struct sock_diag_req {
 	__u8	sdiag_family;
misc/ss.c +50 -2

113 lines not on this page · raw message

Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#2 lore raw

Re: [iproute PATCH v2 2/2] ss: support closing inet sockets via SOCK_DESTROY.

Quoted 6 lines · Lorenzo Colitti

This patch adds a -K / --kill option to ss that attempts to forcibly close matching sockets using SOCK_DESTROY.

This is implemented by adding a new "struct action" to struct filter. If necessary, this can be extended later on to support further actions on sockets.

Does not work for me :

lpaa23:~# ./ss dst lpaa24|wc -l
401
lpaa23:~# ./ss -K dst lpaa24|wc -l
1
lpaa23:~# ./ss dst lpaa24|wc -l
401
lpaa23:~# id
uid=0(root) gid=0(root)
groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),26(tape)

Kernel is latest David Miller net-next

#15 lore raw
In reply to Lorenzo Colitti

lib/libnetlink.c int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) · line 176

Quoted 10 lines · Lorenzo Colitti

The new variant is identical to rtnl_send_check, except it also consumes the kernel response instead of using MSG_PEEK. This is useful for callers that send simple commands that never cause a response but only ACKs, and that expect to receive and deal with errors without printing them to stderr like rtnl_talk does.

lib/libnetlink.c +4 -0
+inline int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
+{
+	return rtnl_send_check_ack(rth, buf, len, 0);
+}

Please remove this inline attribute.

#3 lore raw

Re: [iproute PATCH v2 2/2] ss: support closing inet sockets via SOCK_DESTROY.

In reply to Lorenzo Colitti
Quoted 5 lines · On Thu, 17 Dec 2015 22:22:18 +0900 Lorenzo Colitti <[email protected]> wrote:
misc/ss.c +0 -0
index 0dab32c..be70c41 100644

misc/ss.c struct filter · line 168

Quoted 9 lines
misc/ss.c +3 -0
@@ -160,6 +160,9 @@ struct filter
 	int states;
 	int families;
 	struct ssfilter *f;
+	struct {
+		__u8 kill:1;
+	} action;
 };
 
 stati

Please just make it a boolean or integer, not a structure wrapped around a bit field. If you need to extend in the future then change it then. Please don't write code based on speculative future additions.

#4 lore raw

Re: [iproute PATCH v2 2/2] ss: support closing inet sockets via SOCK_DESTROY.

I've just uploaded a new version. The changes from v2 are as follows:

- Removed the superfluous inline keyword.
- The code now ignores ENOENT from kill_inet_sock. This can happen if something else closed the socket during the scan, or if the user requests killing a socket that is not in the hash tables and thus cannot be found by inet_diag_find_one_icsk.
- The semantics of rtnl_send_check_ack are clearer. If the caller passes in ack=1, the function blocks until a response is received (unlike v2 which passed in MSG_PEEK). Also, an NLMSG_ERROR with an err of 0 is not treated as a failure.
- kill_inet_sock always requests an ACK when closing a socket.

This version is also tested on real hardware. The following work:

- Passing in -K as non-root immediately stops with EPERM.
- Running "ss -a -K dport = :22" closes SSH.
- Running ss -a -K dport = :5222 closes my XMPP connections, interrupts my chat client, and sends RSTs to the server.
- The above command silently skips TIME_WAIT sockets, which cannot be destroyed, without interrupting the dump.

#5 lore raw

PATCH v3 1/2 libnetlink: add a variant of rtnl_send_check that consumes ACKs

The new variant is identical to rtnl_send_check, except it also consumes the kernel response instead of using MSG_PEEK. This is useful for callers that send simple commands that never cause a response but only ACKs, and that expect to receive and deal with errors without printing them to stderr like rtnl_talk does.

---
 include/libnetlink.h |  2 ++
 lib/libnetlink.c     | 16 +++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)
include/libnetlink.h +2 -0
index 431189e..a88cb4d 100644
@@ -75,6 +75,8 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
 	__attribute__((warn_unused_result));
 int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
 	__attribute__((warn_unused_result));
+int rtnl_send_check_ack(struct rtnl_handle *rth, const void *buf, int, int)
+	__attribute__((warn_unused_result));
 int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
 	__attribute__((warn_unused_result));
 
lib/libnetlink.c +13 -3
index 1658214..43cc73b 100644
@@ -134,18 +134,21 @@ int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
 	return send(rth->fd, buf, len, 0);
 }
 
-int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
+int rtnl_send_check_ack(struct rtnl_handle *rth, const void *buf, int len,
+		int ack)
 {
 	struct nlmsghdr *h;
-	int status;
+	int status, flags;
 	char resp[1024];
 
 	status = send(rth->fd, buf, len, 0);
 	if (status < 0)
 		return status;
 
+	flags = ack ? 0 : MSG_DONTWAIT|MSG_PEEK;
+
 	/* Check for immediate errors */
-	status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
+	status = recv(rth->fd, resp, sizeof(resp), flags);
 	if (status < 0) {
 		if (errno == EAGAIN)
 			return 0;
@@ -160,6 +163,8 @@ int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
 				fprintf(stderr, "ERROR truncated\n");
 			else 
 				errno = -err->error;
+			if (ack && errno == 0)
+				return 0;
 			return -1;
 		}
 	}
@@ -167,6 +172,11 @@ int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
 	return 0;
 }
 
+int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
+{
+	return rtnl_send_check_ack(rth, buf, len, 0);
+}
+
 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
 {
 	struct nlmsghdr nlh;
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#7 lore raw

PATCH v3 2/2 ss: support closing inet sockets via SOCK_DESTROY.

In reply to Lorenzo Colitti

This patch adds a -K / --kill option to ss that attempts to forcibly close matching sockets using SOCK_DESTROY.

Because ss typically prints sockets instead of acting on them, and because the kernel only supports forcibly closing some types of sockets, the output of -K is as follows:

- If closing the socket succeeds, the socket is printed.
- If the kernel does not support forcibly closing this type of socket (e.g., if it's a UDP socket, or a TIME_WAIT socket), the socket is silently skipped.
- If an error occurs (e.g., permission denied), the error is reported and ss exits.

---
 include/linux/sock_diag.h |  1 +
 misc/ss.c                 | 50 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)
include/linux/sock_diag.h +1 -0
index 024e1f4..dafcb89 100644
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 
 #define SOCK_DIAG_BY_FAMILY 20
+#define SOCK_DESTROY 21
 
 struct sock_diag_req {
 	__u8	sdiag_family;
misc/ss.c +48 -2

111 lines not on this page · raw message

Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#14 lore raw

Re: [iproute PATCH v2 2/2] ss: support closing inet sockets via SOCK_DESTROY.

In reply to Lorenzo Colitti
Quoted 3 lines · Lorenzo Colitti

I've just uploaded a new version. The changes from v2 are as follows:
[...]

Also: as requested, kill is now a boolean instead of a bitfield inside a struct.

#6 lore raw
In reply to Lorenzo Colitti
Quoted 7 lines · On Tue, 22 Dec 2015 17:31:33 +0900 Lorenzo Colitti <[email protected]> wrote:

The new variant is identical to rtnl_send_check, except it also consumes the kernel response instead of using MSG_PEEK. This is useful for callers that send simple commands that never cause a response but only ACKs, and that expect to receive and deal with errors without printing them to stderr like rtnl_talk does.

Originally, iproute2 used netlink so that every request had an ACK and this is the API in rtnl_talk. Then as an optimization it was observed that ACK from kernel is not necessary (all error reports are handled in send), but that for some asynchronous errors a check was necessary.

Therefore, I wonder why you need this, either:
* don't ask kernel for ACK's (like most other ip commands),
* or use rtnl_talk() and expect ACK.

#8 lore raw

Re: [iproute PATCH v3 2/2] ss: support closing inet sockets via SOCK_DESTROY.

In reply to Lorenzo Colitti
Quoted 7 lines · On Tue, 22 Dec 2015 17:31:34 +0900 Lorenzo Colitti <[email protected]> wrote:
misc/ss.c +6 -0
+static int kill_inet_sock(const struct sockaddr_nl *addr,
+		struct nlmsghdr *h, void *arg)
+{
+	struct inet_diag_msg *d = NLMSG_DATA(h);
+	struct inet_diag_arg *diag_arg = arg;
+	struct rtnl_handle *rth = diag_arg->rth;

misc/ss.c static int sockdiag_send(int family, int fd, int protocol, struct filter *f) · line 2216

Quoted 10 lines
misc/ss.c +10 -0
+	DIAG_REQUEST(req, struct inet_diag_req_v2 r);
+
+	req.nlh.nlmsg_type = SOCK_DESTROY;
+	req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+	req.nlh.nlmsg_seq = ++rth->seq;
+	req.r.sdiag_family = d->idiag_family;
+	req.r.sdiag_protocol = diag_arg->protocol;
+	req.r.id = d->id;
+
+	return rtnl_send_check_ack(rth, &req.nlh, req.nlh.nlmsg_len, 1);

Just use rtnl_talk() instead, it does request/reply.

#9 lore raw

Re: [iproute PATCH v3 2/2] ss: support closing inet sockets via SOCK_DESTROY.

Quoted 10 lines · Stephen Hemminger

misc/ss.c static int sockdiag_send(int family, int fd, int protocol, struct filter *f) · line 2216

Quoted 8 lines
misc/ss.c +8 -0
+     req.nlh.nlmsg_type = SOCK_DESTROY;
+     req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+     req.nlh.nlmsg_seq = ++rth->seq;
+     req.r.sdiag_family = d->idiag_family;
+     req.r.sdiag_protocol = diag_arg->protocol;
+     req.r.id = d->id;
+
+     return rtnl_send_check_ack(rth, &req.nlh, req.nlh.nlmsg_len, 1);

Just use rtnl_talk() instead, it does request/reply.

The reason I did not use rtnl_talk is that it prints all errors to stderr. This does not
fit well with SOCK_DESTROY, for which it is expected that some operations will fail.

For example, if you type "ss -a -K dport = :443", you probably don't want to see one "RTNETLINK answers: Operation not supported" error for every TIME-WAIT socket to port 443, and you don't want to see "RTNETLINK answers: No such file or directory" if one of those sockets happens to be closed during the scan. Silently ignoring these errors seemed best.

I could also add a parameter to rtnl_talk to suppress printing errors, though the patch to do so would be roughly equivalent to the patch where I added rtnl_send_check_ack. I can also just use rtnl_talk as you suggest and not care about the errors.

Let me know what you prefer.

#10 lore raw

Re: [iproute PATCH v3 2/2] ss: support closing inet sockets via SOCK_DESTROY.

This new version uses rtnl_talk as requested. It solves the problem of spurious error messages by making rtnl_talk not print any netlink-returned errors when operating on a NETLINK_SOCK_DIAG socket.

Because there is currently no code that calls rtnl_talk on a NETLINK_SOCK_DIAG socket, this does not affect existing behaviour. The code in this patchset prints netlink errors by itself, and any future code that does this will need to be responsible for doing the same.

#11 lore raw

PATCH v4 1/2 libnetlink: don't print NETLINK_SOCK_DIAG errors in rtnl_talk

This change is a no-op, as currently no code uses rtnl_talk on NETLINK_SOCK_DIAG_BY_FAMILY sockets. It is needed to suppress spurious errors when using SOCK_DESTROY via rtnl_talk.

---
 lib/libnetlink.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/libnetlink.c +4 -2
index 1658214..d6b5fd3 100644
@@ -419,8 +419,10 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
 					return 0;
 				}
 
-				fprintf(stderr, "RTNETLINK answers: %s\n",
-					strerror(-err->error));
+				if (rtnl->proto != NETLINK_SOCK_DIAG)
+					fprintf(stderr,
+						"RTNETLINK answers: %s\n",
+						strerror(-err->error));
 				errno = -err->error;
 				return -1;
 			}
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#12 lore raw

PATCH v4 2/2 ss: support closing inet sockets via SOCK_DESTROY.

In reply to Lorenzo Colitti

This patch adds a -K / --kill option to ss that attempts to forcibly close matching sockets using SOCK_DESTROY.

Because ss typically prints sockets instead of acting on them, and because the kernel only supports forcibly closing some types of sockets, the output of -K is as follows:

- If closing the socket succeeds, the socket is printed.
- If the kernel does not support forcibly closing this type of socket (e.g., if it's a UDP socket, or a TIME_WAIT socket), the socket is silently skipped.
- If an error occurs (e.g., permission denied), the error is reported and ss exits.

---
 include/linux/sock_diag.h |  1 +
 man/man8/ss.8             |  5 +++++
 misc/ss.c                 | 50 +++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 54 insertions(+), 2 deletions(-)
include/linux/sock_diag.h +1 -0
index 024e1f4..dafcb89 100644
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 
 #define SOCK_DIAG_BY_FAMILY 20
+#define SOCK_DESTROY 21
 
 struct sock_diag_req {
 	__u8	sdiag_family;
man/man8/ss.8 +5 -0
index f4d5264..758460c 100644
@@ -48,6 +48,11 @@ Show process using socket.
 .B \-i, \-\-info
 Show internal TCP information.
 .TP
+.B \-K, \-\-kill
+Attempts to forcibly close sockets. This option displays sockets that are
+successfully closed and silently skips sockets that the kernel does not support
+closing. It supports IPv4 and IPv6 sockets only.
+.TP
 .B \-s, \-\-summary
 Print summary statistics. This option does not parse socket lists obtaining
 summary from various sources. It is useful when amount of sockets is so huge
misc/ss.c +48 -2

111 lines not on this page · raw message

Signature · 1 line
2.6.0.rc2.230.g3dd15c0
#13 lore raw

Re: [iproute PATCH v4 2/2] ss: support closing inet sockets via SOCK_DESTROY.

Quoted 7 lines · On Fri, 8 Jan 2016 17:32:37 +0900 Lorenzo Colitti <[email protected]> wrote:

This patch adds a -K / --kill option to ss that attempts to forcibly close matching sockets using SOCK_DESTROY.

Because ss typically prints sockets instead of acting on them, and because the kernel only supports forcibly closing some types of sockets, the output of -K is as follows:

Quoted 8 lines

- If closing the socket succeeds, the socket is printed.
- If the kernel does not support forcibly closing this type of socket (e.g., if it's a UDP socket, or a TIME_WAIT socket), the socket is silently skipped.
- If an error occurs (e.g., permission denied), the error is reported and ss exits.

Both applied, thanks