int states;
int families;
struct ssfilter *f;
+ bool kill;
};
static const struct filter default_dbs[MAX_DB] = {
struct inet_diag_arg {
struct filter *f;
int protocol;
+ struct rtnl_handle *rth;
};
+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;
+ 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_talk(rth, &req.nlh, NULL, 0);
+}
+
static int show_one_inet_sock(const struct sockaddr_nl *addr,
struct nlmsghdr *h, void *arg)
{
if (!(diag_arg->f->families & (1 << r->idiag_family)))
return 0;
+ if (diag_arg->f->kill && kill_inet_sock(addr, h, arg) != 0) {
+ if (errno == EOPNOTSUPP || errno == ENOENT) {
+ /* Socket can't be closed, or is already closed. */
+ return 0;
+ } else {
+ perror("SOCK_DESTROY answers");
+ return -1;
+ }
+ }
if ((err = inet_show_sock(h, diag_arg->f, diag_arg->protocol)) < 0)
return err;
static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
{
int err = 0;
- struct rtnl_handle rth;
+ struct rtnl_handle rth, rth2;
int family = PF_INET;
struct inet_diag_arg arg = { .f = f, .protocol = protocol };
if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
return -1;
+
+ if (f->kill) {
+ if (rtnl_open_byproto(&rth2, 0, NETLINK_SOCK_DIAG)) {
+ rtnl_close(&rth);
+ return -1;
+ }
+ arg.rth = &rth2;
+ }
+
rth.dump = MAGIC_SEQ;
rth.dump_fp = dump_fp;
if (preferred_family == PF_INET6)
Exit:
rtnl_close(&rth);
+ if (arg.rth)
+ rtnl_close(arg.rth);
return err;
}
" -x, --unix display only Unix domain sockets\n"
" -f, --family=FAMILY display sockets of type FAMILY\n"
"\n"
+" -K, --kill forcibly close sockets, display what was closed\n"
+"\n"
" -A, --query=QUERY, --socket=QUERY\n"
" QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
"\n"
{ "context", 0, 0, 'Z' },
{ "contexts", 0, 0, 'z' },
{ "net", 1, 0, 'N' },
+ { "kill", 0, 0, 'K' },
{ 0 }
};
int ch;
int state_filter = 0;
- while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:",
+ while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:vVzZN:K",
long_opts, NULL)) != EOF) {
switch(ch) {
case 'n':
if (netns_switch(optarg))
exit(1);
break;
+ case 'K':
+ current_filter.kill = 1;
+ break;
case 'h':
help();
case '?':