From: Masatake YAMATO Date: Thu, 23 Feb 2023 15:09:54 +0000 (+0900) Subject: lsfd: use extra information loaded from /proc/net/icmp X-Git-Tag: v2.39-rc1~52^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a7cba6f3c522a109e1d4fbbce51d36770f0542b4;p=thirdparty%2Futil-linux.git lsfd: use extra information loaded from /proc/net/icmp Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-sock-xinfo.c b/misc-utils/lsfd-sock-xinfo.c index 59037729b5..91c10bd7bd 100644 --- a/misc-utils/lsfd-sock-xinfo.c +++ b/misc-utils/lsfd-sock-xinfo.c @@ -39,6 +39,7 @@ #include "lsfd.h" #include "lsfd-sock.h" +static void load_xinfo_from_proc_icmp(ino_t netns_inode); static void load_xinfo_from_proc_unix(ino_t netns_inode); static void load_xinfo_from_proc_raw(ino_t netns_inode); static void load_xinfo_from_proc_tcp(ino_t netns_inode); @@ -92,6 +93,7 @@ static void load_sock_xinfo_no_nsswitch(ino_t netns) load_xinfo_from_proc_udp6(netns); load_xinfo_from_proc_udplite6(netns); load_xinfo_from_proc_raw6(netns); + load_xinfo_from_proc_icmp(netns); } static void load_sock_xinfo_with_fd(int fd, ino_t netns) @@ -889,8 +891,9 @@ struct raw_xinfo { uint16_t protocol; }; -static char *raw_get_name(struct sock_xinfo *sock_xinfo, - struct sock *sock __attribute__((__unused__))) +static char *raw_get_name_common(struct sock_xinfo *sock_xinfo, + struct sock *sock __attribute__((__unused__)), + const char *port_label) { char *str = NULL; struct l4_xinfo_class *class = (struct l4_xinfo_class *)sock_xinfo->class; @@ -906,16 +909,24 @@ static char *raw_get_name(struct sock_xinfo *sock_xinfo, xasprintf(&str, "state=%s", st_str); else if (class->is_any_addr(raddr) || !inet_ntop(class->family, raddr, remote_s, sizeof(remote_s))) - xasprintf(&str, "state=%s protocol=%"PRIu16" laddr=%s", + xasprintf(&str, "state=%s %s=%"PRIu16" laddr=%s", st_str, + port_label, raw->protocol, local_s); else - xasprintf(&str, "state=%s protocol=%"PRIu16" laddr=%s raddr=%s", + xasprintf(&str, "state=%s %s=%"PRIu16" laddr=%s raddr=%s", st_str, + port_label, raw->protocol, local_s, remote_s); return str; } +static char *raw_get_name(struct sock_xinfo *sock_xinfo, + struct sock *sock __attribute__((__unused__))) +{ + return raw_get_name_common(sock_xinfo, sock, "protocol"); +} + static char *raw_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), struct sock *sock __attribute__((__unused__))) { @@ -1001,6 +1012,64 @@ static void load_xinfo_from_proc_raw(ino_t netns_inode) &raw_xinfo_class); } +/* + * PING + */ +static char *ping_get_name(struct sock_xinfo *sock_xinfo, + struct sock *sock __attribute__((__unused__))) +{ + return raw_get_name_common(sock_xinfo, sock, "id"); +} + +static char *ping_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)), + struct sock *sock __attribute__((__unused__))) +{ + return strdup("dgram"); +} + +static bool ping_fill_column(struct proc *proc __attribute__((__unused__)), + struct sock_xinfo *sock_xinfo, + struct sock *sock __attribute__((__unused__)), + struct libscols_line *ln __attribute__((__unused__)), + int column_id, + size_t column_index __attribute__((__unused__)), + char **str) +{ + if (l3_fill_column_handler(INET, sock_xinfo, column_id, str)) + return true; + + if (column_id == COL_PING_ID) { + xasprintf(str, "%"PRIu16, + ((struct raw_xinfo *)sock_xinfo)->protocol); + return true; + } + + return false; +} + +static const struct l4_xinfo_class ping_xinfo_class = { + .sock = { + .get_name = ping_get_name, + .get_type = ping_get_type, + .get_state = tcp_get_state, + .get_listening = NULL, + .fill_column = ping_fill_column, + .free = NULL, + }, + .scan_line = raw_xinfo_scan_line, + .get_addr = tcp_xinfo_get_addr, + .is_any_addr = tcp_xinfo_is_any_addr, + .family = AF_INET, + .l3_decorator = {"", ""}, +}; + +static void load_xinfo_from_proc_icmp(ino_t netns_inode) +{ + load_xinfo_from_proc_inet_L4(netns_inode, + "/proc/net/icmp", + &ping_xinfo_class); +} + /* * TCP6 */ diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index bef4508af3..5ac4bd8a6d 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -279,6 +279,9 @@ ____ PIDFD.PID <``number``>:: PID of the process targeted by the pidfd. +PING.ID <`number`>:: +ICMP echo request id used on the PING socket. + POS <``number``>:: File position. diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 5cb1dc40be..c49505d673 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -189,6 +189,8 @@ static const struct colinfo infos[] = { N_("NSpid field in fdinfo of the pidfd") }, [COL_PIDFD_PID]={ "PIDFD.PID",5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("PID of the process targeted by the pidfd") }, + [COL_PING_ID]={ "PING.ID", 0,SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, + N_("ICMP echo request ID") }, [COL_POS] = { "POS", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("file position") }, [COL_RAW_PROTOCOL]={ "RAW.PROTOCOL",0,SCOLS_FL_RIGHT,SCOLS_JSON_NUMBER, diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 1166f9fe02..cfeae57174 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -69,6 +69,7 @@ enum { COL_PIDFD_COMM, COL_PIDFD_NSPID, COL_PIDFD_PID, + COL_PING_ID, COL_POS, COL_RAW_PROTOCOL, COL_RDEV,