This change extends the "arp_ip_target" parameter format to allow for a list of vlan tags to be included for each arp target. The new format for arp_ip_target is: arp_ip_target=ipv4-address[vlan-tag\...],... Examples: arp_ip_target=10.0.0.1[10] arp_ip_target=10.0.0.1[100/200] The inclusion of the list of vlan tags is optional. The new logic preserves both forward and backward compatibility with the kernel and iproute2 versions. Comparability is also persevered for: ip -d link list. Signed-off-by: David Wilder --- ip/iplink_bond.c | 127 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 11 deletions(-) diff --git a/ip/iplink_bond.c b/ip/iplink_bond.c index 62dd907c..e0f3d445 100644 --- a/ip/iplink_bond.c +++ b/ip/iplink_bond.c @@ -173,6 +173,53 @@ static void explain(void) print_explain(stderr); } +#define BOND_VLAN_PROTO_NONE htons(0xffff) +#define BOND_MAX_VLAN_TAGS 5 + +struct bond_vlan_tag { + __be16 vlan_proto; + __be16 vlan_id; +}; + +static struct bond_vlan_tag *bond_vlan_tags_parse(char *vlan_list, int level, int *size) +{ + struct bond_vlan_tag *tags = NULL; + char *vlan; + int n; + + if (level > BOND_MAX_VLAN_TAGS) { + fprintf(stderr, + "Error: Too many vlan tags specified, maximum is %d.\n", + BOND_MAX_VLAN_TAGS); + exit(1); + } + + if (!vlan_list || strlen(vlan_list) == 0) { + tags = calloc(level + 1, sizeof(*tags)); + *size = (level + 1) * (sizeof(*tags)); + if (tags) + tags[level].vlan_proto = BOND_VLAN_PROTO_NONE; + return tags; + } + + for (vlan = strsep(&vlan_list, "/"); (vlan != 0); level++) { + tags = bond_vlan_tags_parse(vlan_list, level + 1, size); + if (!tags) + continue; + + tags[level].vlan_proto = htons(ETH_P_8021Q); + n = sscanf(vlan, "%hu", &(tags[level].vlan_id)); + + if (n != 1 || tags[level].vlan_id < 1 || + tags[level].vlan_id > 4094) + return NULL; + + return tags; + } + + return NULL; +} + static int bond_parse_opt(struct link_util *lu, int argc, char **argv, struct nlmsghdr *n) { @@ -239,12 +286,29 @@ static int bond_parse_opt(struct link_util *lu, int argc, char **argv, NEXT_ARG(); char *targets = strdupa(*argv); char *target = strtok(targets, ","); - int i; + struct bond_vlan_tag *tags; + int size, i; for (i = 0; target && i < BOND_MAX_ARP_TARGETS; i++) { - __u32 addr = get_addr32(target); + struct Data { + __u32 addr; + struct bond_vlan_tag vlans[]; + } data; + char *vlan_list, *dup; + + dup = strdupa(target); + data.addr = get_addr32(strsep(&dup, "[")); + vlan_list = strsep(&dup, "]"); + + if (vlan_list) { + tags = bond_vlan_tags_parse(vlan_list, 0, &size); + memcpy(&data.vlans, tags, size); + addattr_l(n, 1024, i, &data, + sizeof(data.addr)+size); + } else { + addattr32(n, 1024, i, data.addr); + } - addattr32(n, 1024, i, addr); target = strtok(NULL, ","); } addattr_nest_end(n, nest); @@ -498,21 +562,62 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_BOND_ARP_IP_TARGET]) { struct rtattr *iptb[BOND_MAX_ARP_TARGETS + 1]; + SPRINT_BUF(pbuf); + int i; parse_rtattr_nested(iptb, BOND_MAX_ARP_TARGETS, tb[IFLA_BOND_ARP_IP_TARGET]); if (iptb[0]) { open_json_array(PRINT_JSON, "arp_ip_target"); - print_string(PRINT_FP, NULL, "arp_ip_target ", NULL); + print_color_string(PRINT_FP, COLOR_INET, NULL, + "arp_ip_target ", NULL); } - for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) { - if (iptb[i]) - print_string(PRINT_ANY, - NULL, - "%s", - rt_addr_n2a_rta(AF_INET, iptb[i])); + for (i = 0; i < BOND_MAX_ARP_TARGETS && iptb[i]; i++) { + struct Data { + __u32 addr; + struct bond_vlan_tag vlans[BOND_MAX_VLAN_TAGS + 1]; + } data; + int size = sizeof(pbuf); + int num, level; + + if (RTA_PAYLOAD(iptb[i]) < sizeof(data.addr) || + RTA_PAYLOAD(iptb[i]) > sizeof(data)) { + fprintf(stderr, "Internal Error: Bad payload for arp_ip_target.\n"); + exit(1); + } + memcpy(&data, RTA_DATA(iptb[i]), RTA_PAYLOAD(iptb[i])); + + num = snprintf(&pbuf[0], size, "%s", + rt_addr_n2a(AF_INET, sizeof(data.addr), &data.addr)); + + if (RTA_PAYLOAD(iptb[i]) > sizeof(data.addr)) { + num = num + snprintf(&pbuf[num], size - num, "["); + + for (level = 0; + data.vlans[level].vlan_proto != BOND_VLAN_PROTO_NONE; + level++) { + + if (level > BOND_MAX_VLAN_TAGS) { + fprintf(stderr, + "Internal Error: too many vlan tags.\n"); + exit(1); + } + + if (level != 0) + num = num + snprintf(&pbuf[num], size - num, "/"); + + num = num + snprintf(&pbuf[num], size - num, + "%u", data.vlans[level].vlan_id); + } + + num = num - snprintf(&pbuf[num], size - num, "]"); + + } + + print_color_string(PRINT_ANY, COLOR_INET, NULL, "%s", pbuf); + if (!is_json_context() && i < BOND_MAX_ARP_TARGETS-1 && iptb[i+1]) @@ -520,7 +625,7 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } if (iptb[0]) { - print_string(PRINT_FP, NULL, " ", NULL); + print_color_string(PRINT_FP, COLOR_INET, NULL, " ", NULL); close_json_array(PRINT_JSON, NULL); } } -- 2.43.5