diff --git a/Makefile b/Makefile index 6a51e0db..32587db3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,13 @@ # SPDX-License-Identifier: GPL-2.0 # Top level Makefile for iproute2 +ifeq ("$(origin V)", "command line") +VERBOSE = $(V) +endif +ifndef VERBOSE +VERBOSE = 0 +endif + ifeq ($(VERBOSE),0) MAKEFLAGS += --no-print-directory endif diff --git a/bridge/fdb.c b/bridge/fdb.c index 376713b6..4d55fb04 100644 --- a/bridge/fdb.c +++ b/bridge/fdb.c @@ -219,10 +219,9 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) unsigned int ifindex = rta_getattr_u32(tb[NDA_IFINDEX]); if (ifindex) { - char ifname[IF_NAMESIZE]; + if (!tb[NDA_LINK_NETNSID]) { + const char *ifname = ll_index_to_name(ifindex); - if (!tb[NDA_LINK_NETNSID] && - if_indextoname(ifindex, ifname)) { if (jw_global) jsonw_string_field(jw_global, "viaIf", ifname); diff --git a/bridge/link.c b/bridge/link.c index e2371d0d..870ebe05 100644 --- a/bridge/link.c +++ b/bridge/link.c @@ -26,8 +26,6 @@ static const char *port_states[] = { [BR_STATE_BLOCKING] = "blocking", }; -extern char *if_indextoname(unsigned int __ifindex, char *__ifname); - static void print_link_flags(FILE *fp, unsigned int flags) { fprintf(fp, "<"); @@ -104,7 +102,6 @@ int print_linkinfo(const struct sockaddr_nl *who, int len = n->nlmsg_len; struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr *tb[IFLA_MAX+1]; - char b1[IFNAMSIZ]; len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) { @@ -135,14 +132,10 @@ int print_linkinfo(const struct sockaddr_nl *who, print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); if (tb[IFLA_LINK]) { - SPRINT_BUF(b1); int iflink = rta_getattr_u32(tb[IFLA_LINK]); - if (iflink == 0) - fprintf(fp, "@NONE: "); - else - fprintf(fp, "@%s: ", - if_indextoname(iflink, b1)); + fprintf(fp, "@%s: ", + iflink ? ll_index_to_name(iflink) : "NONE"); } else fprintf(fp, ": "); @@ -151,9 +144,11 @@ int print_linkinfo(const struct sockaddr_nl *who, if (tb[IFLA_MTU]) fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU])); - if (tb[IFLA_MASTER]) - fprintf(fp, "master %s ", - if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1)); + if (tb[IFLA_MASTER]) { + int master = rta_getattr_u32(tb[IFLA_MASTER]); + + fprintf(fp, "master %s ", ll_index_to_name(master)); + } if (tb[IFLA_PROTINFO]) { if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) { diff --git a/devlink/devlink.c b/devlink/devlink.c index 1b15eef8..220b2bb2 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -343,7 +343,7 @@ static int attr_cb(const struct nlattr *attr, void *data) int type; if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0) - return MNL_CB_ERROR; + return MNL_CB_OK; type = mnl_attr_get_type(attr); if (mnl_attr_validate(attr, devlink_policy[type]) < 0) diff --git a/ip/ifcfg b/ip/ifcfg old mode 100644 new mode 100755 index 851b9215..5b34decd --- a/ip/ifcfg +++ b/ip/ifcfg @@ -1,4 +1,4 @@ -#! /bin/bash +#! /bin/sh # SPDX-License-Identifier: GPL-2.0 CheckForwarding () { @@ -7,7 +7,7 @@ CheckForwarding () { fwd=0 if [ -d $sbase ]; then for dir in $sbase/*/forwarding; do - fwd=$[$fwd + `cat $dir`] + fwd=$(( fwd + $(cat "$dir") )) done else fwd=2 @@ -128,12 +128,12 @@ fi arping -q -A -c 1 -I $dev $ipaddr noarp=$? ( sleep 2 ; - arping -q -U -c 1 -I $dev $ipaddr ) >& /dev/null /dev/null 2>&1 & /dev/null -ip route add unreachable 255.255.255.255 >& /dev/null +ip route add unreachable 224.0.0.0/24 >/dev/null 2>&1 +ip route add unreachable 255.255.255.255 >/dev/null 2>&1 if [ "`ip link ls $dev | grep -c MULTICAST`" -ge 1 ]; then - ip route add 224.0.0.0/4 dev $dev scope global >& /dev/null + ip route add 224.0.0.0/4 dev $dev scope global >/dev/null 2>&1 fi if [ $fwd -eq 0 ]; then diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c index b8db49c9..783e28a6 100644 --- a/ip/ip6tunnel.c +++ b/ip/ip6tunnel.c @@ -92,7 +92,10 @@ static void print_tunnel(struct ip6_tnl_parm2 *p) else printf(" encaplimit %u", p->encap_limit); - printf(" hoplimit %u", p->hop_limit); + if (p->hop_limit) + printf(" hoplimit %u", p->hop_limit); + else + printf(" hoplimit inherit"); if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS) printf(" tclass inherit"); diff --git a/ip/ipaddress.c b/ip/ipaddress.c index c35b6b3f..ba60125c 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1558,6 +1558,8 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, print_bool(PRINT_ANY, "deleted", "Deleted ", true); if (!brief) { + const char *name; + if (filter.oneline || filter.flushb) { const char *dev = ll_index_to_name(ifa->ifa_index); @@ -1570,20 +1572,13 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, } } - int family = ifa->ifa_family; - - if (ifa->ifa_family == AF_INET) - print_string(PRINT_ANY, "family", " %s ", "inet"); - else if (ifa->ifa_family == AF_INET6) - print_string(PRINT_ANY, "family", " %s ", "inet6"); - else if (ifa->ifa_family == AF_DECnet) - print_string(PRINT_ANY, "family", " %s ", "dnet"); - else if (ifa->ifa_family == AF_IPX) - print_string(PRINT_ANY, "family", " %s ", "ipx"); - else - print_int(PRINT_ANY, - "family_index", - " family %d ", family); + name = family_name(ifa->ifa_family); + if (*name != '?') { + print_string(PRINT_ANY, "family", " %s ", name); + } else { + print_int(PRINT_ANY, "family_index", " family %d ", + ifa->ifa_family); + } } if (rta_tb[IFA_LOCAL]) { @@ -2251,6 +2246,12 @@ ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max) for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { parse_rtattr_nested(vf, IFLA_VF_MAX, i); + + if (!vf[IFLA_VF_RATE]) { + fprintf(stderr, "VF min/max rate API not supported\n"); + exit(1); + } + vf_rate = RTA_DATA(vf[IFLA_VF_RATE]); if (vf_rate->vf == vfnum) { *min = vf_rate->min_tx_rate; diff --git a/ip/ipfou.c b/ip/ipfou.c index febc2c8c..1f392ade 100644 --- a/ip/ipfou.c +++ b/ip/ipfou.c @@ -52,7 +52,7 @@ static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n, __u8 ipproto, type; bool gue_set = false; int ipproto_set = 0; - unsigned short family = AF_INET; + __u8 family = AF_INET; while (argc > 0) { if (!matches(*argv, "port")) { @@ -103,7 +103,7 @@ static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n, addattr16(n, 1024, FOU_ATTR_PORT, port); addattr8(n, 1024, FOU_ATTR_TYPE, type); - addattr16(n, 1024, FOU_ATTR_AF, family); + addattr8(n, 1024, FOU_ATTR_AF, family); if (ipproto_set) addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto); diff --git a/ip/iplink.c b/ip/iplink.c index 0ea5547f..230f4c53 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -548,6 +548,14 @@ static int iplink_parse_vf(int vf, int *argcp, char ***argvp, if (tivt.max_tx_rate == -1) tivt.max_tx_rate = tmax; } + + if (tivt.max_tx_rate && tivt.min_tx_rate > tivt.max_tx_rate) { + fprintf(stderr, + "Invalid min_tx_rate %d - must be <= max_tx_rate %d\n", + tivt.min_tx_rate, tivt.max_tx_rate); + return -1; + } + addattr_l(&req->n, sizeof(*req), IFLA_VF_RATE, &tivt, sizeof(tivt)); } @@ -779,10 +787,10 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, argc--; argv++; break; } else if (matches(*argv, "alias") == 0) { + NEXT_ARG(); len = strlen(*argv); if (len >= IFALIASZ) invarg("alias too long\n", *argv); - NEXT_ARG(); addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS, *argv, len); } else if (strcmp(*argv, "group") == 0) { diff --git a/ip/iplink_bond.c b/ip/iplink_bond.c index 2b5cf4f6..f01fd8da 100644 --- a/ip/iplink_bond.c +++ b/ip/iplink_bond.c @@ -369,8 +369,6 @@ static int bond_parse_opt(struct link_util *lu, int argc, char **argv, static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { - unsigned int ifindex; - if (!tb) return; @@ -380,21 +378,16 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "mode", "mode %s ", mode); } - if (tb[IFLA_BOND_ACTIVE_SLAVE] && - (ifindex = rta_getattr_u32(tb[IFLA_BOND_ACTIVE_SLAVE]))) { - char buf[IFNAMSIZ]; - const char *n = if_indextoname(ifindex, buf); + if (tb[IFLA_BOND_ACTIVE_SLAVE]) { + unsigned int ifindex = + rta_getattr_u32(tb[IFLA_BOND_ACTIVE_SLAVE]); - if (n) + if (ifindex) { print_string(PRINT_ANY, "active_slave", "active_slave %s ", - n); - else - print_uint(PRINT_ANY, - "active_slave_index", - "active_slave %u ", - ifindex); + ll_index_to_name(ifindex)); + } } if (tb[IFLA_BOND_MIIMON]) @@ -479,18 +472,15 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) arp_all_targets); } - if (tb[IFLA_BOND_PRIMARY] && - (ifindex = rta_getattr_u32(tb[IFLA_BOND_PRIMARY]))) { - char buf[IFNAMSIZ]; - const char *n = if_indextoname(ifindex, buf); + if (tb[IFLA_BOND_PRIMARY]) { + unsigned int ifindex = rta_getattr_u32(tb[IFLA_BOND_PRIMARY]); - if (n) - print_string(PRINT_ANY, "primary", "primary %s ", n); - else - print_uint(PRINT_ANY, - "primary_index", - "primary %u ", - ifindex); + if (ifindex) { + print_string(PRINT_ANY, + "primary", + "primary %s ", + ll_index_to_name(ifindex)); + } } if (tb[IFLA_BOND_PRIMARY_RESELECT]) { diff --git a/ip/iplink_geneve.c b/ip/iplink_geneve.c index f0f1d1c7..2d0a0411 100644 --- a/ip/iplink_geneve.c +++ b/ip/iplink_geneve.c @@ -227,7 +227,8 @@ static int geneve_parse_opt(struct link_util *lu, int argc, char **argv, static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; - __u8 tos; + __u8 ttl = 0; + __u8 tos = 0; if (!tb) return; @@ -262,27 +263,20 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) } } - if (tb[IFLA_GENEVE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]); + if (tb[IFLA_GENEVE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - } - - if (tb[IFLA_GENEVE_TOS] && - (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) { - if (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - if (tos == 1) { - print_string(PRINT_FP, - "tos", - "tos %s ", - "inherit"); - } else { - fprintf(f, "tos %#x ", tos); - } - } + if (tb[IFLA_GENEVE_TOS]) + tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } if (tb[IFLA_GENEVE_LABEL]) { diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c index 661eaa79..88b56625 100644 --- a/ip/iplink_vxlan.c +++ b/ip/iplink_vxlan.c @@ -394,10 +394,9 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv, static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { __u32 vni; - unsigned int link; - __u8 tos; + __u8 ttl = 0; + __u8 tos = 0; __u32 maxaddr; - char s2[64]; if (!tb) return; @@ -467,14 +466,13 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) &addr)); } - if (tb[IFLA_VXLAN_LINK] && - (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VXLAN_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_VXLAN_PORT_RANGE]) { @@ -517,27 +515,22 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS])) print_bool(PRINT_ANY, "l3miss", "l3miss ", true); - if (tb[IFLA_VXLAN_TOS] && - (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) { - if (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - if (tos == 1) - fprintf(f, "tos %s ", "inherit"); - else - fprintf(f, "tos %#x ", tos); - } - } - - if (tb[IFLA_VXLAN_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); + if (tb[IFLA_VXLAN_TOS]) + tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); else - print_int(PRINT_JSON, "ttl", NULL, ttl); + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); } + if (tb[IFLA_VXLAN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); + if (tb[IFLA_VXLAN_LABEL]) { __u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]); diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c index 740da7c6..a1d36ba2 100644 --- a/ip/iproute_lwtunnel.c +++ b/ip/iproute_lwtunnel.c @@ -196,7 +196,6 @@ static int read_action_type(const char *name) static void print_encap_seg6local(FILE *fp, struct rtattr *encap) { struct rtattr *tb[SEG6_LOCAL_MAX + 1]; - char ifbuf[IFNAMSIZ]; int action; parse_rtattr_nested(tb, SEG6_LOCAL_MAX, encap); @@ -229,15 +228,13 @@ static void print_encap_seg6local(FILE *fp, struct rtattr *encap) if (tb[SEG6_LOCAL_IIF]) { int iif = rta_getattr_u32(tb[SEG6_LOCAL_IIF]); - fprintf(fp, "iif %s ", - if_indextoname(iif, ifbuf) ?: ""); + fprintf(fp, "iif %s ", ll_index_to_name(iif)); } if (tb[SEG6_LOCAL_OIF]) { int oif = rta_getattr_u32(tb[SEG6_LOCAL_OIF]); - fprintf(fp, "oif %s ", - if_indextoname(oif, ifbuf) ?: ""); + fprintf(fp, "oif %s ", ll_index_to_name(oif)); } } @@ -274,7 +271,7 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap) rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_DST])); if (tb[LWTUNNEL_IP_TTL]) - fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL])); + fprintf(fp, "ttl %u ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL])); if (tb[LWTUNNEL_IP_TOS]) fprintf(fp, "tos %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TOS])); @@ -329,7 +326,7 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap) rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_DST])); if (tb[LWTUNNEL_IP6_HOPLIMIT]) - fprintf(fp, "hoplimit %d ", + fprintf(fp, "hoplimit %u ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT])); if (tb[LWTUNNEL_IP6_TC]) diff --git a/ip/iptunnel.c b/ip/iptunnel.c index ce610f84..0aa3b332 100644 --- a/ip/iptunnel.c +++ b/ip/iptunnel.c @@ -326,7 +326,7 @@ static void print_tunnel(struct ip_tunnel_parm *p) } if (p->iph.ttl) - printf(" ttl %d", p->iph.ttl); + printf(" ttl %u", p->iph.ttl); else printf(" ttl inherit"); diff --git a/ip/link_gre.c b/ip/link_gre.c index 52c581d2..674603ba 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -402,6 +402,8 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) const char *remote = "any"; unsigned int iflags = 0; unsigned int oflags = 0; + __u8 ttl = 0; + __u8 tos = 0; if (tb[IFLA_GRE_REMOTE]) { unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]); @@ -421,44 +423,31 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { + if (tb[IFLA_GRE_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); - } - - if (tb[IFLA_GRE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } else { - print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - } - - if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) { - int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); - - if (is_json_context()) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", tos); - print_string(PRINT_JSON, "tos", NULL, b1); - } else { - fputs("tos ", f); - if (tos == 1) - fputs("inherit ", f); - else - fprintf(f, "0x%x ", tos); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); } } + if (tb[IFLA_GRE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); + + if (tb[IFLA_GRE_TOS]) + tos = rta_getattr_u8(tb[IFLA_GRE_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); + } + if (tb[IFLA_GRE_PMTUDISC]) { if (!rta_getattr_u8(tb[IFLA_GRE_PMTUDISC])) print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false); @@ -495,9 +484,8 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[]) __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } @@ -544,76 +532,11 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_hex(PRINT_ANY, "erspan_hwid", "erspan_hwid 0x%x ", erspan_hwid); } - if (tb[IFLA_GRE_ENCAP_TYPE] && - rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { - __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); - __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); - - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "%s ", "unknown"); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - - print_bool(PRINT_JSON, - "csum", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - - print_bool(PRINT_JSON, - "csum6", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM6); - - print_bool(PRINT_JSON, - "remcsum", - NULL, - flags & TUNNEL_ENCAP_FLAG_REMCSUM); - - close_json_object(); - } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_GRE_ENCAP_TYPE, + IFLA_GRE_ENCAP_FLAGS, + IFLA_GRE_ENCAP_SPORT, + IFLA_GRE_ENCAP_DPORT); } static void gre_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_gre6.c b/ip/link_gre6.c index bc33026c..a0eeb5a6 100644 --- a/ip/link_gre6.c +++ b/ip/link_gre6.c @@ -430,8 +430,9 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) unsigned int iflags = 0; unsigned int oflags = 0; unsigned int flags = 0; - unsigned int flowinfo = 0; + __u32 flowinfo = 0; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; + __u8 ttl = 0; if (!tb) return; @@ -469,56 +470,31 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) { + if (tb[IFLA_GRE_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } - if (tb[IFLA_GRE_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); + if (tb[IFLA_GRE_TTL]) + ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl); + else + print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit"); - if (ttl) - print_int(PRINT_ANY, "ttl", "hoplimit %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } - - if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) + if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) { print_bool(PRINT_ANY, "ip6_tnl_f_ign_encap_limit", "encaplimit none ", true); - else if (tb[IFLA_GRE_ENCAP_LIMIT]) { - int encap_limit = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]); + } else if (tb[IFLA_GRE_ENCAP_LIMIT]) { + __u8 val = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]); - print_int(PRINT_ANY, - "encap_limit", - "encaplimit %d ", - encap_limit); - } - - if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) { - print_bool(PRINT_ANY, - "ip6_tnl_f_use_orig_flowlabel", - "flowlabel inherit ", - true); - } else { - if (is_json_context()) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%05x", - ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); - print_string(PRINT_JSON, "flowlabel", NULL, b1); - - } else { - fprintf(f, "flowlabel 0x%05x ", - ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); - } + print_uint(PRINT_ANY, "encap_limit", "encaplimit %u ", val); } if (flags & IP6_TNL_F_USE_ORIG_TCLASS) { @@ -526,17 +502,23 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) "ip6_tnl_f_use_orig_tclass", "tclass inherit ", true); - } else { - if (is_json_context()) { - SPRINT_BUF(b1); + } else if (tb[IFLA_GRE_FLOWINFO]) { + __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20; - snprintf(b1, sizeof(b1), "0x%05x", - ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20); - print_string(PRINT_JSON, "tclass", NULL, b1); - } else { - fprintf(f, "tclass 0x%02x ", - ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20); - } + snprintf(s2, sizeof(s2), "0x%02x", val); + print_string(PRINT_ANY, "tclass", "tclass %s ", s2); + } + + if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) { + print_bool(PRINT_ANY, + "ip6_tnl_f_use_orig_flowlabel", + "flowlabel inherit ", + true); + } else if (tb[IFLA_GRE_FLOWINFO]) { + __u32 val = ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL); + + snprintf(s2, sizeof(s2), "0x%05x", val); + print_string(PRINT_ANY, "flowlabel", "flowlabel %s ", s2); } if (flags & IP6_TNL_F_RCV_DSCP_COPY) @@ -570,18 +552,17 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (oflags & GRE_CSUM) print_bool(PRINT_ANY, "ocsum", "ocsum ", true); - if (flags & IP6_TNL_F_USE_ORIG_FWMARK) + if (flags & IP6_TNL_F_USE_ORIG_FWMARK) { print_bool(PRINT_ANY, "ip6_tnl_f_use_orig_fwmark", "fwmark inherit ", true); - else if (tb[IFLA_GRE_FWMARK]) { + } else if (tb[IFLA_GRE_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } @@ -653,25 +634,14 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) fputs("encap-sport auto ", f); else fprintf(f, "encap-sport %u", ntohs(sport)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); } } + + tnl_print_encap(tb, + IFLA_GRE_ENCAP_TYPE, + IFLA_GRE_ENCAP_FLAGS, + IFLA_GRE_ENCAP_SPORT, + IFLA_GRE_ENCAP_DPORT); } static void gre_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c index 84205b12..8f5c9bd7 100644 --- a/ip/link_ip6tnl.c +++ b/ip/link_ip6tnl.c @@ -336,6 +336,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb char s2[64]; int flags = 0; __u32 flowinfo = 0; + __u8 ttl = 0; if (!tb) return; @@ -377,49 +378,43 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL])); } - if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { + if (tb[IFLA_IPTUN_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } - if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) + if (tb[IFLA_IPTUN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl); + else + print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit"); + + if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) { print_bool(PRINT_ANY, "ip6_tnl_f_ign_encap_limit", "encaplimit none ", true); - else if (tb[IFLA_IPTUN_ENCAP_LIMIT]) - print_uint(PRINT_ANY, - "encap_limit", - "encaplimit %u ", - rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT])); + } else if (tb[IFLA_IPTUN_ENCAP_LIMIT]) { + __u8 val = rta_getattr_u8(tb[IFLA_IPTUN_ENCAP_LIMIT]); - if (tb[IFLA_IPTUN_TTL]) - print_uint(PRINT_ANY, - "ttl", - "hoplimit %u ", - rta_getattr_u8(tb[IFLA_IPTUN_TTL])); + print_uint(PRINT_ANY, "encap_limit", "encaplimit %u ", val); + } - if (flags & IP6_TNL_F_USE_ORIG_TCLASS) + if (flags & IP6_TNL_F_USE_ORIG_TCLASS) { print_bool(PRINT_ANY, "ip6_tnl_f_use_orig_tclass", "tclass inherit ", true); - else if (tb[IFLA_IPTUN_FLOWINFO]) { - __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS); + } else if (tb[IFLA_IPTUN_FLOWINFO]) { + __u32 val = ntohl(flowinfo & IP6_FLOWINFO_TCLASS) >> 20; - if (is_json_context()) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%02x", (__u8)(val >> 20)); - print_string(PRINT_JSON, "flowinfo_tclass", NULL, b1); - } else { - printf("tclass 0x%02x ", (__u8)(val >> 20)); - } + snprintf(s2, sizeof(s2), "0x%02x", val); + print_string(PRINT_ANY, "tclass", "tclass %s ", s2); } if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) { @@ -427,27 +422,11 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb "ip6_tnl_f_use_orig_flowlabel", "flowlabel inherit ", true); - } else { - if (is_json_context()) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%05x", - ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); - print_string(PRINT_JSON, "flowlabel", NULL, b1); - } else { - printf("flowlabel 0x%05x ", - ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL)); - } - } - - if (is_json_context()) { - SPRINT_BUF(flwinfo); - - snprintf(flwinfo, sizeof(flwinfo), "0x%08x", ntohl(flowinfo)); - print_string(PRINT_JSON, "flowinfo", NULL, flwinfo); - } else { - printf("(flowinfo 0x%08x) ", ntohl(flowinfo)); + } else if (tb[IFLA_IPTUN_FLOWINFO]) { + __u32 val = ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL); + snprintf(s2, sizeof(s2), "0x%05x", val); + print_string(PRINT_ANY, "flowlabel", "flowlabel %s ", s2); } if (flags & IP6_TNL_F_RCV_DSCP_COPY) @@ -459,6 +438,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb if (flags & IP6_TNL_F_MIP6_DEV) print_bool(PRINT_ANY, "ip6_tnl_f_mip6_dev", "mip6 ", true); + if (flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) + print_bool(PRINT_ANY, + "ip6_tnl_f_allow_local_remote", + "allow-localremote ", + true); + if (flags & IP6_TNL_F_USE_ORIG_FWMARK) { print_bool(PRINT_ANY, "ip6_tnl_f_use_orig_fwmark", @@ -468,77 +453,16 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]); if (fwmark) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", fwmark); - print_string(PRINT_ANY, "fwmark", "fwmark %s ", b1); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } - if (flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) - print_bool(PRINT_ANY, - "ip6_tnl_f_allow_local_remote", - "allow-localremote ", - true); - - if (tb[IFLA_IPTUN_ENCAP_TYPE] && - rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { - __u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]); - __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "unknown ", NULL); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, "csum", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, "csum6", NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM6); - print_bool(PRINT_JSON, "remcsum", NULL, - flags & TUNNEL_ENCAP_FLAG_REMCSUM); - close_json_object(); - } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } + tnl_print_encap(tb, + IFLA_IPTUN_ENCAP_TYPE, + IFLA_IPTUN_ENCAP_FLAGS, + IFLA_IPTUN_ENCAP_SPORT, + IFLA_IPTUN_ENCAP_DPORT); } static void ip6tunnel_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c index 24a0f0cd..ce3855c1 100644 --- a/ip/link_iptnl.c +++ b/ip/link_iptnl.c @@ -360,11 +360,12 @@ get_failed: static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { - char s1[1024]; char s2[64]; const char *local = "any"; const char *remote = "any"; - __u16 prefixlen, type; + __u16 prefixlen; + __u8 ttl = 0; + __u8 tos = 0; if (!tb) return; @@ -407,43 +408,31 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) { + if (tb[IFLA_IPTUN_LINK]) { unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]); - const char *n = if_indextoname(link, s2); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_int(PRINT_ANY, "link_index", "dev %u ", link); - } - - if (tb[IFLA_IPTUN_TTL]) { - __u8 ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); - - if (ttl) - print_int(PRINT_ANY, "ttl", "ttl %d ", ttl); - else - print_int(PRINT_JSON, "ttl", NULL, ttl); - } else { - print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); - } - - if (tb[IFLA_IPTUN_TOS]) { - int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); - - if (tos) { - if (is_json_context()) { - print_0xhex(PRINT_JSON, "tos", "%#x", tos); - } else { - fputs("tos ", f); - if (tos == 1) - fputs("inherit ", f); - else - fprintf(f, "0x%x ", tos); - } + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); } } + if (tb[IFLA_IPTUN_TTL]) + ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]); + if (is_json_context() || ttl) + print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); + else + print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); + + if (tb[IFLA_IPTUN_TOS]) + tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]); + if (tos) { + if (is_json_context() || tos != 1) + print_0xhex(PRINT_ANY, "tos", "tos 0x%x ", tos); + else + print_string(PRINT_FP, NULL, "tos %s ", "inherit"); + } + if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC])) print_bool(PRINT_ANY, "pmtudisc", "pmtudisc ", true); else @@ -465,7 +454,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ const char *prefix = inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]), - s1, sizeof(s1)); + s2, sizeof(s2)); if (is_json_context()) { print_string(PRINT_JSON, "prefix", NULL, prefix); @@ -492,79 +481,20 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[ } } - if (tb[IFLA_IPTUN_ENCAP_TYPE] && - (type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) { - __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]); - __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]); - __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]); - - open_json_object("encap"); - print_string(PRINT_FP, NULL, "encap ", NULL); - switch (type) { - case TUNNEL_ENCAP_FOU: - print_string(PRINT_ANY, "type", "%s ", "fou"); - break; - case TUNNEL_ENCAP_GUE: - print_string(PRINT_ANY, "type", "%s ", "gue"); - break; - default: - print_null(PRINT_ANY, "type", "unknown ", NULL); - break; - } - - if (is_json_context()) { - print_uint(PRINT_JSON, - "sport", - NULL, - sport ? ntohs(sport) : 0); - print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); - print_bool(PRINT_JSON, - "csum", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM); - print_bool(PRINT_JSON, - "csum6", - NULL, - flags & TUNNEL_ENCAP_FLAG_CSUM6); - print_bool(PRINT_JSON, - "remcsum", - NULL, - flags & TUNNEL_ENCAP_FLAG_REMCSUM); - close_json_object(); - } else { - if (sport == 0) - fputs("encap-sport auto ", f); - else - fprintf(f, "encap-sport %u", ntohs(sport)); - - fprintf(f, "encap-dport %u ", ntohs(dport)); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM) - fputs("encap-csum ", f); - else - fputs("noencap-csum ", f); - - if (flags & TUNNEL_ENCAP_FLAG_CSUM6) - fputs("encap-csum6 ", f); - else - fputs("noencap-csum6 ", f); - - if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) - fputs("encap-remcsum ", f); - else - fputs("noencap-remcsum ", f); - } - } - if (tb[IFLA_IPTUN_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } + + tnl_print_encap(tb, + IFLA_IPTUN_ENCAP_TYPE, + IFLA_IPTUN_ENCAP_FLAGS, + IFLA_IPTUN_ENCAP_SPORT, + IFLA_IPTUN_ENCAP_DPORT); } static void iptunnel_print_help(struct link_util *lu, int argc, char **argv, diff --git a/ip/link_vti.c b/ip/link_vti.c index 2b0fab27..1439e53c 100644 --- a/ip/link_vti.c +++ b/ip/link_vti.c @@ -167,9 +167,7 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { const char *local = "any"; const char *remote = "any"; - __u32 key; - unsigned int link; - char s2[IFNAMSIZ]; + char s2[64]; if (!tb) return; @@ -192,33 +190,37 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_VTI_LINK] && - (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VTI_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } - if (tb[IFLA_VTI_IKEY] && - (key = rta_getattr_u32(tb[IFLA_VTI_IKEY]))) - print_0xhex(PRINT_ANY, "ikey", "ikey %#x ", ntohl(key)); + if (tb[IFLA_VTI_IKEY]) { + struct rtattr *rta = tb[IFLA_VTI_IKEY]; + __u32 key = rta_getattr_u32(rta); + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "ikey", "ikey %s ", s2); + } - if (tb[IFLA_VTI_OKEY] && - (key = rta_getattr_u32(tb[IFLA_VTI_OKEY]))) - print_0xhex(PRINT_ANY, "okey", "okey %#x ", ntohl(key)); + if (tb[IFLA_VTI_OKEY]) { + struct rtattr *rta = tb[IFLA_VTI_OKEY]; + __u32 key = rta_getattr_u32(rta); + + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "okey", "okey %s ", s2); + } if (tb[IFLA_VTI_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]); if (fwmark) { - SPRINT_BUF(b1); - - snprintf(b1, sizeof(b1), "0x%x", fwmark); - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } diff --git a/ip/link_vti6.c b/ip/link_vti6.c index 74c246d3..2a86d592 100644 --- a/ip/link_vti6.c +++ b/ip/link_vti6.c @@ -24,20 +24,25 @@ #include "ip_common.h" #include "tunnel.h" +static void print_usage(FILE *f) +{ + fprintf(f, + "Usage: ... vti6 [ remote ADDR ]\n" + " [ local ADDR ]\n" + " [ [i|o]key KEY ]\n" + " [ dev PHYS_DEV ]\n" + " [ fwmark MARK ]\n" + "\n" + "Where: ADDR := { IPV6_ADDRESS }\n" + " KEY := { DOTTED_QUAD | NUMBER }\n" + " MARK := { 0x0..0xffffffff }\n" + ); +} static void usage(void) __attribute__((noreturn)); static void usage(void) { - fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n"); - fprintf(stderr, " type { vti6 } [ remote ADDR ] [ local ADDR ]\n"); - fprintf(stderr, " [ [i|o]key KEY ]\n"); - fprintf(stderr, " [ dev PHYS_DEV ]\n"); - fprintf(stderr, " [ fwmark MARK ]\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Where: NAME := STRING\n"); - fprintf(stderr, " ADDR := { IPV6_ADDRESS }\n"); - fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); - fprintf(stderr, " MARK := { 0x0..0xffffffff }\n"); + print_usage(stderr); exit(-1); } @@ -168,7 +173,6 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) const char *remote = "any"; struct in6_addr saddr; struct in6_addr daddr; - unsigned int link; char s2[64]; if (!tb) @@ -190,39 +194,51 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) print_string(PRINT_ANY, "local", "local %s ", local); - if (tb[IFLA_VTI_LINK] && (link = rta_getattr_u32(tb[IFLA_VTI_LINK]))) { - const char *n = if_indextoname(link, s2); + if (tb[IFLA_VTI_LINK]) { + unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]); - if (n) - print_string(PRINT_ANY, "link", "dev %s ", n); - else - print_uint(PRINT_ANY, "link_index", "dev %u ", link); + if (link) { + print_string(PRINT_ANY, "link", "dev %s ", + ll_index_to_name(link)); + } } if (tb[IFLA_VTI_IKEY]) { - inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2)); - print_string(PRINT_ANY, "ikey", "ikey %s ", s2); + struct rtattr *rta = tb[IFLA_VTI_IKEY]; + __u32 key = rta_getattr_u32(rta); + + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "ikey", "ikey %s ", s2); } if (tb[IFLA_VTI_OKEY]) { - inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2)); - print_string(PRINT_ANY, "okey", "okey %s ", s2); + struct rtattr *rta = tb[IFLA_VTI_OKEY]; + __u32 key = rta_getattr_u32(rta); + + if (key && inet_ntop(AF_INET, RTA_DATA(rta), s2, sizeof(s2))) + print_string(PRINT_ANY, "okey", "okey %s ", s2); } if (tb[IFLA_VTI_FWMARK]) { __u32 fwmark = rta_getattr_u32(tb[IFLA_VTI_FWMARK]); if (fwmark) { - snprintf(s2, sizeof(s2), "0x%x", fwmark); - - print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2); + print_0xhex(PRINT_ANY, + "fwmark", "fwmark 0x%x ", fwmark); } } } +static void vti6_print_help(struct link_util *lu, int argc, char **argv, + FILE *f) +{ + print_usage(f); +} + struct link_util vti6_link_util = { .id = "vti6", .maxattr = IFLA_VTI_MAX, .parse_opt = vti6_parse_opt, .print_opt = vti6_print_opt, + .print_help = vti6_print_help, }; diff --git a/ip/routef b/ip/routef old mode 100644 new mode 100755 diff --git a/ip/routel b/ip/routel old mode 100644 new mode 100755 diff --git a/ip/rtpr b/ip/rtpr old mode 100644 new mode 100755 index 192a476f..7e48674b --- a/ip/rtpr +++ b/ip/rtpr @@ -1,4 +1,4 @@ -#! /bin/bash +#! /bin/sh # SPDX-License-Identifier: GPL-2.0 exec tr "[\\\\]" "[ diff --git a/ip/tunnel.c b/ip/tunnel.c index f8601038..04148046 100644 --- a/ip/tunnel.c +++ b/ip/tunnel.c @@ -36,36 +36,26 @@ #include "utils.h" #include "tunnel.h" +#include "json_print.h" const char *tnl_strproto(__u8 proto) { - static char buf[16]; - switch (proto) { case IPPROTO_IPIP: - strcpy(buf, "ip"); - break; + return "ip"; case IPPROTO_GRE: - strcpy(buf, "gre"); - break; + return "gre"; case IPPROTO_IPV6: - strcpy(buf, "ipv6"); - break; + return "ipv6"; case IPPROTO_ESP: - strcpy(buf, "esp"); - break; + return "esp"; case IPPROTO_MPLS: - strcpy(buf, "mpls"); - break; + return "mpls"; case 0: - strcpy(buf, "any"); - break; + return "any"; default: - strcpy(buf, "unknown"); - break; + return "unknown"; } - - return buf; } int tnl_get_ioctl(const char *basedev, void *p) @@ -200,6 +190,96 @@ __be32 tnl_parse_key(const char *name, const char *key) return htonl(uval); } +static const char *tnl_encap_str(const char *name, int enabled, int port) +{ + static const char ne[][sizeof("no")] = { + [0] = "no", + [1] = "", + }; + static char buf[32]; + char b1[16]; + const char *val; + + if (!port) { + val = "auto"; + } else if (port < 0) { + val = ""; + } else { + snprintf(b1, sizeof(b1), "%u", port - 1); + val = b1; + } + + snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val); + return buf; +} + +void tnl_print_encap(struct rtattr *tb[], + int encap_type, int encap_flags, + int encap_sport, int encap_dport) +{ + __u16 type, flags, sport, dport; + + if (!tb[encap_type]) + return; + + type = rta_getattr_u16(tb[encap_type]); + if (type == TUNNEL_ENCAP_NONE) + return; + + flags = rta_getattr_u16(tb[encap_flags]); + sport = rta_getattr_u16(tb[encap_sport]); + dport = rta_getattr_u16(tb[encap_dport]); + + open_json_object("encap"); + print_string(PRINT_FP, NULL, "encap ", NULL); + + switch (type) { + case TUNNEL_ENCAP_FOU: + print_string(PRINT_ANY, "type", "%s ", "fou"); + break; + case TUNNEL_ENCAP_GUE: + print_string(PRINT_ANY, "type", "%s ", "gue"); + break; + default: + print_null(PRINT_ANY, "type", "%s ", "unknown"); + break; + } + + if (is_json_context()) { + print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); + print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); + print_bool(PRINT_JSON, "csum", NULL, + flags & TUNNEL_ENCAP_FLAG_CSUM); + print_bool(PRINT_JSON, "csum6", NULL, + flags & TUNNEL_ENCAP_FLAG_CSUM6); + print_bool(PRINT_JSON, "remcsum", NULL, + flags & TUNNEL_ENCAP_FLAG_REMCSUM); + close_json_object(); + } else { + int t; + + t = sport ? ntohs(sport) + 1 : 0; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("sport", 1, t)); + + t = ntohs(dport) + 1; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("dport", 1, t)); + + t = flags & TUNNEL_ENCAP_FLAG_CSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("csum", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_CSUM6; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("csum6", t, -1)); + + t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; + print_string(PRINT_FP, NULL, "%s", + tnl_encap_str("remcsum", t, -1)); + } +} + /* tnl_print_stats - print tunnel statistics * * @buf - tunnel interface's line in /proc/net/dev, diff --git a/ip/tunnel.h b/ip/tunnel.h index 9a03c0d7..a5c537c1 100644 --- a/ip/tunnel.h +++ b/ip/tunnel.h @@ -23,6 +23,8 @@ #include +struct rtattr; + const char *tnl_strproto(__u8 proto); int tnl_get_ioctl(const char *basedev, void *p); @@ -32,6 +34,9 @@ int tnl_prl_ioctl(int cmd, const char *name, void *p); int tnl_6rd_ioctl(int cmd, const char *name, void *p); int tnl_ioctl_get_6rd(const char *name, void *p); __be32 tnl_parse_key(const char *name, const char *key); +void tnl_print_encap(struct rtattr *tb[], + int encap_type, int encap_flags, + int encap_sport, int encap_dport); void tnl_print_stats(const char *buf); #endif diff --git a/man/man8/ip-link.8.in b/man/man8/ip-link.8.in index 1d1f0363..481589ea 100644 --- a/man/man8/ip-link.8.in +++ b/man/man8/ip-link.8.in @@ -1690,6 +1690,7 @@ option instead. .sp .BI max_tx_rate " TXRATE" - change the allowed maximum transmit bandwidth, in Mbps, for the specified VF. +Setting this parameter to 0 disables rate limiting. .B vf parameter must be specified. @@ -1697,6 +1698,7 @@ parameter must be specified. .BI min_tx_rate " TXRATE" - change the allowed minimum transmit bandwidth, in Mbps, for the specified VF. Minimum TXRATE should be always <= Maximum TXRATE. +Setting this parameter to 0 disables rate limiting. .B vf parameter must be specified. diff --git a/misc/ss.c b/misc/ss.c index 1abf43d0..29a25070 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -1018,12 +1018,15 @@ static void print_right_spacing(struct column *f, int printed) /* Done with field: update buffer pointer, start new token after current one */ static void field_flush(struct column *f) { - struct buf_chunk *chunk = buffer.tail; - unsigned int pad = buffer.cur->len % 2; + struct buf_chunk *chunk; + unsigned int pad; if (f->disabled) return; + chunk = buffer.tail; + pad = buffer.cur->len % 2; + if (buffer.cur->len > f->max_len) f->max_len = buffer.cur->len; @@ -3708,7 +3711,10 @@ static int unix_show(struct filter *f) }; memcpy(st.local.data, &u->name, sizeof(u->name)); - if (strcmp(u->peer_name, "*")) + /* when parsing the old format rport is set to 0 and + * therefore peer_name remains NULL + */ + if (u->peer_name && strcmp(u->peer_name, "*")) memcpy(st.remote.data, &u->peer_name, sizeof(u->peer_name)); if (run_ssfilter(f->f, &st) == 0) { diff --git a/tc/README.last b/tc/README.last deleted file mode 100644 index 63f6f7b0..00000000 --- a/tc/README.last +++ /dev/null @@ -1,45 +0,0 @@ -Kernel code and interface. --------------------------- - -* Compile time switches - -There is only one, but very important, compile time switch. -It is not settable by "make config", but should be selected -manually and after a bit of thinking in - -PSCHED_CLOCK_SOURCE can take three values: - - PSCHED_GETTIMEOFDAY - PSCHED_JIFFIES - PSCHED_CPU - - - PSCHED_GETTIMEOFDAY - -Default setting is the most conservative PSCHED_GETTIMEOFDAY. -It is very slow both because of weird slowness of do_gettimeofday() -and because it forces code to use unnatural "timeval" format, -where microseconds and seconds fields are separate. -Besides that, it will misbehave, when delays exceed 2 seconds -(f.e. very slow links or classes bounded to small slice of bandwidth) -To resume: as only you will get it working, select correct clock -source and forget about PSCHED_GETTIMEOFDAY forever. - - - PSCHED_JIFFIES - -Clock is derived from jiffies. On architectures with HZ=100 -granularity of this clock is not enough to make reasonable -bindings to real time. However, taking into account Linux -architecture problems, which force us to use artificial -integrated clock in any case, this switch is not so bad -for schduling even on high speed networks, though policing -is not reliable. - - - PSCHED_CPU - -It is available only for alpha and pentiums with correct -CPU timestamp. It is the fastest way, use it when it is available, -but remember: not all pentiums have this facility, and -a lot of them have clock, broken by APM etc. etc. diff --git a/tc/m_action.c b/tc/m_action.c index 611f6cc2..445d0b69 100644 --- a/tc/m_action.c +++ b/tc/m_action.c @@ -194,7 +194,10 @@ int parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) } else { struct action_util *a = NULL; - strncpy(k, *argv, sizeof(k) - 1); + if (!action_a2n(*argv, NULL, false)) + strncpy(k, "gact", sizeof(k) - 1); + else + strncpy(k, *argv, sizeof(k) - 1); eap = 0; if (argc > 0) { a = get_action_kind(k); diff --git a/tc/tc_filter.c b/tc/tc_filter.c index 7dd123ab..a86785d6 100644 --- a/tc/tc_filter.c +++ b/tc/tc_filter.c @@ -298,7 +298,7 @@ int print_filter(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) if (!filter_protocol || filter_protocol != f_proto) { if (f_proto) { SPRINT_BUF(b1); - print_string(PRINT_JSON, "protocol", + print_string(PRINT_ANY, "protocol", "protocol %s ", ll_proto_n2a(f_proto, b1, sizeof(b1))); } diff --git a/tc/tc_util.c b/tc/tc_util.c index ee9a70aa..10e5aa91 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -511,7 +511,7 @@ static const char *action_n2a(int action) * * In error case, returns -1 and does not touch @result. Otherwise returns 0. */ -static int action_a2n(char *arg, int *result, bool allow_num) +int action_a2n(char *arg, int *result, bool allow_num) { int n; char dummy; @@ -535,13 +535,15 @@ static int action_a2n(char *arg, int *result, bool allow_num) for (iter = a2n; iter->a; iter++) { if (matches(arg, iter->a) != 0) continue; - *result = iter->n; - return 0; + n = iter->n; + goto out_ok; } if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1) return -1; - *result = n; +out_ok: + if (result) + *result = n; return 0; } diff --git a/tc/tc_util.h b/tc/tc_util.h index 1218610d..e354765f 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -132,4 +132,6 @@ int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt); int cls_names_init(char *path); void cls_names_uninit(void); +int action_a2n(char *arg, int *result, bool allow_num); + #endif diff --git a/testsuite/lib/generic.sh b/testsuite/lib/generic.sh index b7de7044..8cef20fa 100644 --- a/testsuite/lib/generic.sh +++ b/testsuite/lib/generic.sh @@ -87,7 +87,11 @@ ts_qdisc_available() rand_dev() { - echo "dev-$(tr -dc "[:alpha:]" < /dev/urandom | head -c 6)" + rnd="" + while [ ${#rnd} -ne 6 ]; do + rnd="$(head -c 250 /dev/urandom | tr -dc '[:alpha:]' | head -c 6)" + done + echo "dev-$rnd" } pr_failed()