Skip to content

Commit

Permalink
Merge pull request FRRouting#8629 from donaldsharp/parse_rtattr
Browse files Browse the repository at this point in the history
Parse rtattr
  • Loading branch information
idryzhov authored May 12, 2021
2 parents 31db7fc + c9d842c commit 17daea8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
39 changes: 14 additions & 25 deletions zebra/if_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ static void netlink_determine_zebra_iftype(const char *kind,
*zif_type = ZEBRA_IF_GRE;
}

#define parse_rtattr_nested(tb, max, rta) \
netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))

static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
uint32_t ns_id, const char *name)
{
Expand All @@ -312,8 +309,7 @@ static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,

ifi = NLMSG_DATA(h);

memset(linkinfo, 0, sizeof(linkinfo));
parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);

if (!linkinfo[IFLA_INFO_DATA]) {
if (IS_ZEBRA_DEBUG_KERNEL)
Expand All @@ -323,8 +319,8 @@ static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
return;
}

memset(attr, 0, sizeof(attr));
parse_rtattr_nested(attr, IFLA_VRF_MAX, linkinfo[IFLA_INFO_DATA]);
netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
linkinfo[IFLA_INFO_DATA]);
if (!attr[IFLA_VRF_TABLE]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
Expand Down Expand Up @@ -544,8 +540,7 @@ static int netlink_extract_bridge_info(struct rtattr *link_data,
struct rtattr *attr[IFLA_BR_MAX + 1];

memset(bridge_info, 0, sizeof(*bridge_info));
memset(attr, 0, sizeof(attr));
parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
if (attr[IFLA_BR_VLAN_FILTERING])
bridge_info->vlan_aware =
*(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
Expand All @@ -559,8 +554,7 @@ static int netlink_extract_vlan_info(struct rtattr *link_data,
vlanid_t vid_in_msg;

memset(vlan_info, 0, sizeof(*vlan_info));
memset(attr, 0, sizeof(attr));
parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
if (!attr[IFLA_VLAN_ID]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
Expand All @@ -579,7 +573,7 @@ static int netlink_extract_gre_info(struct rtattr *link_data,

memset(gre_info, 0, sizeof(*gre_info));
memset(attr, 0, sizeof(attr));
parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);

if (!attr[IFLA_GRE_LOCAL]) {
if (IS_ZEBRA_DEBUG_KERNEL)
Expand Down Expand Up @@ -622,8 +616,7 @@ static int netlink_extract_vxlan_info(struct rtattr *link_data,
ifindex_t ifindex_link;

memset(vxl_info, 0, sizeof(*vxl_info));
memset(attr, 0, sizeof(attr));
parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
if (!attr[IFLA_VXLAN_ID]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
Expand Down Expand Up @@ -716,8 +709,7 @@ static int netlink_bridge_vxlan_update(struct interface *ifp,
/* There is a 1-to-1 mapping of VLAN to VxLAN - hence
* only 1 access VLAN is accepted.
*/
memset(aftb, 0, sizeof(aftb));
parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
if (!aftb[IFLA_BRIDGE_VLAN_INFO])
return 0;

Expand Down Expand Up @@ -786,7 +778,6 @@ static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,

/* Fetch name and ifindex */
ifi = NLMSG_DATA(h);
memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);

if (tb[IFLA_IFNAME] == NULL)
Expand Down Expand Up @@ -854,9 +845,8 @@ static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
uint8_t bypass = 0;
struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];

memset(mbrinfo, 0, sizeof(mbrinfo));
parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
linkinfo[IFLA_INFO_SLAVE_DATA]);
netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
linkinfo[IFLA_INFO_SLAVE_DATA]);
if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
bypass = *(uint8_t *)RTA_DATA(
mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
Expand Down Expand Up @@ -910,7 +900,6 @@ static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
return netlink_bridge_interface(h, len, ns_id, startup);

/* Looking up interface name. */
memset(tb, 0, sizeof(tb));
memset(linkinfo, 0, sizeof(linkinfo));
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);

Expand All @@ -930,7 +919,8 @@ static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);

if (tb[IFLA_LINKINFO]) {
parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
tb[IFLA_LINKINFO]);

if (linkinfo[IFLA_INFO_KIND])
kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
Expand Down Expand Up @@ -1303,7 +1293,6 @@ int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
return -1;
}

memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);

ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
Expand Down Expand Up @@ -1519,7 +1508,6 @@ int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
return netlink_bridge_interface(h, len, ns_id, startup);

/* Looking up interface name. */
memset(tb, 0, sizeof(tb));
memset(linkinfo, 0, sizeof(linkinfo));
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);

Expand All @@ -1536,7 +1524,8 @@ int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
name = (char *)RTA_DATA(tb[IFLA_IFNAME]);

if (tb[IFLA_LINKINFO]) {
parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
tb[IFLA_LINKINFO]);

if (linkinfo[IFLA_INFO_KIND])
kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
Expand Down
2 changes: 2 additions & 0 deletions zebra/kernel_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ void netlink_parse_rtattr_flags(struct rtattr **tb, int max,
{
unsigned short type;

memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
while (RTA_OK(rta, len)) {
type = rta->rta_type & ~flags;
if ((type <= max) && (!tb[type]))
Expand All @@ -504,6 +505,7 @@ void netlink_parse_rtattr_flags(struct rtattr **tb, int max,
void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
int len)
{
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
while (RTA_OK(rta, len)) {
if (rta->rta_type <= max)
tb[rta->rta_type] = rta;
Expand Down
7 changes: 0 additions & 7 deletions zebra/rt_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,6 @@ static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id,
nh_vrf_id = vrf_id;

if (rtnh->rtnh_len > sizeof(*rtnh)) {
memset(rtnh_tb, 0, sizeof(rtnh_tb));

netlink_parse_rtattr(rtnh_tb, RTA_MAX, RTNH_DATA(rtnh),
rtnh->rtnh_len - sizeof(*rtnh));
if (rtnh_tb[RTA_GATEWAY])
Expand Down Expand Up @@ -628,7 +626,6 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
return -1;
}

memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);

if (rtm->rtm_flags & RTM_F_CLONED)
Expand Down Expand Up @@ -713,7 +710,6 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
if (tb[RTA_METRICS]) {
struct rtattr *mxrta[RTAX_MAX + 1];

memset(mxrta, 0, sizeof(mxrta));
netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
RTA_PAYLOAD(tb[RTA_METRICS]));

Expand Down Expand Up @@ -920,7 +916,6 @@ static int netlink_route_change_read_multicast(struct nlmsghdr *h,

len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));

memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);

if (tb[RTA_TABLE])
Expand Down Expand Up @@ -2875,7 +2870,6 @@ static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
/* Parse attributes and extract fields of interest. Do basic
* validation of the fields.
*/
memset(tb, 0, sizeof tb);
netlink_parse_rtattr_flags(tb, NDA_MAX, NDA_RTA(ndm), len,
NLA_F_NESTED);

Expand Down Expand Up @@ -3348,7 +3342,6 @@ static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
zif = (struct zebra_if *)ifp->info;

/* Parse attributes and extract fields of interest. */
memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);

if (!tb[NDA_DST]) {
Expand Down

0 comments on commit 17daea8

Please sign in to comment.