batctl: originators: Get outgoing ifname from netlink
PATCH 1/4 batctl: originators: Get outgoing ifname from netlink
There is no need to gather the interface name for an outgoing interface via a different syscall(s) when the kernel directly provides this information in the netlink message for this originator table entry.
batman-adv provides this functionality with Linux 5.14/batman-adv 2021.2.
--- originators.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
originators.c +13 -4
@@ -40,12 +40,14 @@ static int originators_callback(struct nl_msg *msg, void *arg)struct nlmsghdr *nlh =nlmsg_hdr (msg);int last_seen_msecs, last_seen_secs;struct print_opts *opts = arg; +char ifname_buf[IF_NAMESIZE ];struct bat_host *bat_host;struct genlmsghdr *ghdr; -char ifname[IF_NAMESIZE ]; +uint32_t ifindex;float last_seen;uint8_t *neigh;uint8_t *orig; +char *ifname;char c =' ' ;uint8_t tq;
@@ -74,9 +76,16 @@ static int originators_callback(struct nl_msg *msg, void *arg) orig =nla_data (attrs[BATADV_ATTR_ORIG_ADDRESS ]); neigh =nla_data (attrs[BATADV_ATTR_NEIGH_ADDRESS ]); -if (!if_indextoname (nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]), - ifname)) - ifname[0 ] ='\0' ; +if (attrs[BATADV_ATTR_HARD_IFNAME ]) { + ifname =nla_get_string (attrs[BATADV_ATTR_HARD_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + ifindex =nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]); +if (!if_indextoname (ifindex, ifname_buf)) + ifname_buf[0 ] ='\0' ; + + ifname = ifname_buf; + }if (attrs[BATADV_ATTR_FLAG_BEST ]) c ='*' ;
Signature · 1 line
2.30.2
PATCH 2/4 batctl: neighbors: Get outgoing ifname from netlink
There is no need to gather the interface name for an outgoing interface via a different syscall(s) when the kernel directly provides this information in the netlink message for this neighbor table entry.
batman-adv provides this functionality with Linux 5.14/batman-adv 2021.2.
--- neighbors.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
neighbors.c +13 -4
@@ -36,10 +36,12 @@ static int neighbors_callback(struct nl_msg *msg, void *arg)struct nlmsghdr *nlh =nlmsg_hdr (msg);int last_seen_msecs, last_seen_secs;struct print_opts *opts = arg; +char ifname_buf[IF_NAMESIZE ];struct bat_host *bat_host; -char ifname[IF_NAMESIZE ];struct genlmsghdr *ghdr; +uint32_t ifindex;uint8_t *neigh; +char *ifname;if (!genlmsg_valid_hdr (nlh,0 )) {fputs ("Received invalid data from kernel.\n" , stderr);
@@ -66,9 +68,16 @@ static int neighbors_callback(struct nl_msg *msg, void *arg) neigh =nla_data (attrs[BATADV_ATTR_NEIGH_ADDRESS ]); bat_host =bat_hosts_find_by_mac ((char *)neigh); -if (!if_indextoname (nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]), - ifname)) - ifname[0 ] ='\0' ; +if (attrs[BATADV_ATTR_HARD_IFNAME ]) { + ifname =nla_get_string (attrs[BATADV_ATTR_HARD_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + ifindex =nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]); +if (!if_indextoname (ifindex, ifname_buf)) + ifname_buf[0 ] ='\0' ; + + ifname = ifname_buf; + } last_seen_msecs =nla_get_u32 (attrs[BATADV_ATTR_LAST_SEEN_MSECS ]); last_seen_secs = last_seen_msecs /1000 ;
Signature · 1 line
2.30.2
PATCH 3/4 batctl: ping: Get outgoing ifname from netlink
In reply to Sven EckelmannThere is no need to gather the interface name for an outgoing interface via a different syscall(s) when the kernel directly provides this information in the netlink message for this originator table entry.
batman-adv provides this functionality with Linux 5.14/batman-adv 2021.2.
--- netlink.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
netlink.c +10 -3
@@ -728,9 +728,16 @@ static int get_nexthop_netlink_cb(struct nl_msg *msg, void *arg)/* save result */ memcpy (opts->nexthop, neigh,ETH_ALEN ); - ifname =if_indextoname (index, opts->ifname); -if (!ifname) -return NL_OK ; + +if (attrs[BATADV_ATTR_HARD_IFNAME ]) { + ifname =nla_get_string (attrs[BATADV_ATTR_HARD_IFNAME ]); +strncpy (opts->ifname, ifname,IFNAMSIZ ); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + ifname =if_indextoname (index, opts->ifname); +if (!ifname) +return NL_OK ; + } opts->found = true; opts->query_opts.err =0 ;
Signature · 1 line
2.30.2
PATCH 4/4 batctl: event: Get ifname from netlink message
In reply to Sven EckelmannThere is no need to gather the interface name for an interface via a different syscall(s) when the kernel directly provides this information in the netlink message for this event.
batman-adv provides this functionality with Linux 5.14/batman-adv 2021.2.
--- event.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-)
event.c +36 -16
@@ -174,10 +174,15 @@ static void event_parse_set_mesh(struct nlattr **attrs) ARRAY_SIZE(mesh_mandatory)))return ; - mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); - meshif_name =if_indextoname (mesh_ifindex, meshif_buf); -if (!meshif_name) -return ; +if (attrs[BATADV_ATTR_MESH_IFNAME ]) { + meshif_name =nla_get_string (attrs[BATADV_ATTR_MESH_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); + meshif_name =if_indextoname (mesh_ifindex, meshif_buf); +if (!meshif_name) +return ; + }printf ("%s: set mesh:\n" , meshif_name);
@@ -305,15 +310,25 @@ static void event_parse_set_hardif(struct nlattr **attrs) ARRAY_SIZE(hardif_mandatory)))return ; - mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); - meshif_name =if_indextoname (mesh_ifindex, meshif_buf); -if (!meshif_name) -return ; +if (attrs[BATADV_ATTR_MESH_IFNAME ]) { + meshif_name =nla_get_string (attrs[BATADV_ATTR_MESH_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); + meshif_name =if_indextoname (mesh_ifindex, meshif_buf); +if (!meshif_name) +return ; + } - hardif_ifindex =nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]); - hardif_name =if_indextoname (hardif_ifindex, hardif_buf); -if (!hardif_name) -return ; +if (attrs[BATADV_ATTR_MESH_IFNAME ]) { + hardif_name =nla_get_string (attrs[BATADV_ATTR_HARD_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + hardif_ifindex =nla_get_u32 (attrs[BATADV_ATTR_HARD_IFINDEX ]); + hardif_name =if_indextoname (hardif_ifindex, hardif_buf); +if (!hardif_name) +return ; + }printf ("%s (%s): set hardif:\n" , meshif_name, hardif_name);
@@ -350,10 +365,15 @@ static void event_parse_set_vlan(struct nlattr **attrs) ARRAY_SIZE(vlan_mandatory)))return ; - mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); - meshif_name =if_indextoname (mesh_ifindex, meshif_buf); -if (!meshif_name) -return ; +if (attrs[BATADV_ATTR_MESH_IFNAME ]) { + meshif_name =nla_get_string (attrs[BATADV_ATTR_MESH_IFNAME ]); + }else { +/* compatibility for Linux < 5.14/batman-adv < 2021.2 */ + mesh_ifindex =nla_get_u32 (attrs[BATADV_ATTR_MESH_IFINDEX ]); + meshif_name =if_indextoname (mesh_ifindex, meshif_buf); +if (!meshif_name) +return ; + } vid =nla_get_u16 (attrs[BATADV_ATTR_VLANID ]);
Signature · 1 line
2.30.2