diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c index 203e4a3a..f0db5aa0 100644 --- a/ip/ip6tunnel.c +++ b/ip/ip6tunnel.c @@ -36,6 +36,7 @@ #include "utils.h" #include "tunnel.h" +#include "ip_common.h" #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000) #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF) @@ -75,7 +76,7 @@ static void print_tunnel(struct ip6_tnl_parm *p) printf("%s: %s/ipv6 remote %s local %s", p->name, tnl_strproto(p->proto), remote, local); if (p->link) { - char *n = tnl_ioctl_get_ifname(p->link); + const char *n = ll_index_to_name(p->link); if (n) printf(" dev %s", n); } @@ -210,7 +211,7 @@ static int parse_args(int argc, char **argv, struct ip6_tnl_parm *p) argc--; argv++; } if (medium[0]) { - p->link = tnl_ioctl_get_ifindex(medium); + p->link = ll_name_to_index(medium); if (p->link == 0) return -1; } @@ -266,7 +267,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) while (fgets(buf, sizeof(buf), fp) != NULL) { char name[IFNAMSIZ]; - int type; + int index, type; unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, rx_fifo, rx_frame, tx_bytes, tx_packets, tx_errs, tx_drops, @@ -288,7 +289,10 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) continue; if (p->name[0] && strcmp(p->name, name)) continue; - type = tnl_ioctl_get_iftype(name); + index = ll_name_to_index(name); + if (index == 0) + continue; + type = ll_index_to_type(index); if (type == -1) { fprintf(stderr, "Failed to get type of [%s]\n", name); continue; @@ -298,7 +302,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p) memset(&p1, 0, sizeof(p1)); ip6_tnl_parm_init(&p1, 0); strcpy(p1.name, name); - p1.link = tnl_ioctl_get_ifindex(p1.name); + p1.link = ll_name_to_index(p1.name); if (p1.link == 0) continue; if (tnl_get_ioctl(p1.name, &p1)) @@ -329,6 +333,7 @@ static int do_show(int argc, char **argv) { struct ip6_tnl_parm p; + ll_init_map(&rth); ip6_tnl_parm_init(&p, 0); p.proto = 0; /* default to any */ diff --git a/ip/iptunnel.c b/ip/iptunnel.c index 3525fbb2..2a5c1a19 100644 --- a/ip/iptunnel.c +++ b/ip/iptunnel.c @@ -18,8 +18,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -231,7 +231,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) } if (medium[0]) { - p->link = tnl_ioctl_get_ifindex(medium); + p->link = if_nametoindex(medium); if (p->link == 0) return -1; } @@ -342,7 +342,7 @@ static void print_tunnel(struct ip_tunnel_parm *p) } if (p->link) { - char *n = tnl_ioctl_get_ifname(p->link); + const char *n = ll_index_to_name(p->link); if (n) printf(" dev %s ", n); } @@ -402,7 +402,6 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) rx_fifo, rx_frame, tx_bytes, tx_packets, tx_errs, tx_drops, tx_fifo, tx_colls, tx_carrier, rx_multi; - int type; struct ip_tunnel_parm p1; char buf[512]; @@ -416,6 +415,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) fgets(buf, sizeof(buf), fp); while (fgets(buf, sizeof(buf), fp) != NULL) { + int index, type; char *ptr; buf[sizeof(buf) - 1] = 0; if ((ptr = strchr(buf, ':')) == NULL || @@ -431,7 +431,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) continue; if (p->name[0] && strcmp(p->name, name)) continue; - type = tnl_ioctl_get_iftype(name); + index = ll_name_to_index(name); + if (index == 0) + continue; + type = ll_index_to_type(index); if (type == -1) { fprintf(stderr, "Failed to get type of [%s]\n", name); continue; @@ -467,6 +470,7 @@ static int do_show(int argc, char **argv) int err; struct ip_tunnel_parm p; + ll_init_map(&rth); if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) return -1; diff --git a/ip/link_gre.c b/ip/link_gre.c index 9f8bde66..62baaf48 100644 --- a/ip/link_gre.c +++ b/ip/link_gre.c @@ -206,7 +206,7 @@ get_failed: saddr = get_addr32(*argv); } else if (!matches(*argv, "dev")) { NEXT_ARG(); - link = tnl_ioctl_get_ifindex(*argv); + link = if_nametoindex(*argv); if (link == 0) exit(-1); } else if (!matches(*argv, "ttl") || @@ -298,7 +298,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_GRE_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_GRE_LINK])) { unsigned link = *(__u32 *)RTA_DATA(tb[IFLA_GRE_LINK]); - char *n = tnl_ioctl_get_ifname(link); + const char *n = if_indextoname(link, s2); if (n) fprintf(f, "dev %s ", n); diff --git a/ip/tunnel.c b/ip/tunnel.c index 6efbd2df..b176d3f0 100644 --- a/ip/tunnel.c +++ b/ip/tunnel.c @@ -63,58 +63,6 @@ const char *tnl_strproto(__u8 proto) return buf; } -int tnl_ioctl_get_ifindex(const char *dev) -{ - struct ifreq ifr; - int fd; - int err; - - strncpy(ifr.ifr_name, dev, IFNAMSIZ); - fd = socket(preferred_family, SOCK_DGRAM, 0); - err = ioctl(fd, SIOCGIFINDEX, &ifr); - if (err) { - perror("ioctl"); - return 0; - } - close(fd); - return ifr.ifr_ifindex; -} - -int tnl_ioctl_get_iftype(const char *dev) -{ - struct ifreq ifr; - int fd; - int err; - - strncpy(ifr.ifr_name, dev, IFNAMSIZ); - fd = socket(preferred_family, SOCK_DGRAM, 0); - err = ioctl(fd, SIOCGIFHWADDR, &ifr); - if (err) { - perror("ioctl"); - return -1; - } - close(fd); - return ifr.ifr_addr.sa_family; -} - - -char * tnl_ioctl_get_ifname(int idx) -{ - static struct ifreq ifr; - int fd; - int err; - - ifr.ifr_ifindex = idx; - fd = socket(preferred_family, SOCK_DGRAM, 0); - err = ioctl(fd, SIOCGIFNAME, &ifr); - if (err) { - perror("ioctl"); - return NULL; - } - close(fd); - return ifr.ifr_name; -} - int tnl_get_ioctl(const char *basedev, void *p) { struct ifreq ifr; @@ -126,7 +74,9 @@ int tnl_get_ioctl(const char *basedev, void *p) fd = socket(preferred_family, SOCK_DGRAM, 0); err = ioctl(fd, SIOCGETTUNNEL, &ifr); if (err) - perror("ioctl"); + fprintf(stderr, "get tunnel %s failed: %s\n", basedev, + strerror(errno)); + close(fd); return err; } @@ -145,7 +95,8 @@ int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) fd = socket(preferred_family, SOCK_DGRAM, 0); err = ioctl(fd, cmd, &ifr); if (err) - perror("ioctl"); + fprintf(stderr, "add tunnel %s failed: %s\n", ifr.ifr_name, + strerror(errno)); close(fd); return err; } @@ -160,16 +111,19 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p) strncpy(ifr.ifr_name, name, IFNAMSIZ); else strncpy(ifr.ifr_name, basedev, IFNAMSIZ); + ifr.ifr_ifru.ifru_data = p; fd = socket(preferred_family, SOCK_DGRAM, 0); err = ioctl(fd, SIOCDELTUNNEL, &ifr); if (err) - perror("ioctl"); + fprintf(stderr, "delete tunnel %s failed: %s\n", + ifr.ifr_name, strerror(errno)); close(fd); return err; } -static int tnl_gen_ioctl(int cmd, const char *name, void *p, int skiperr) +static int tnl_gen_ioctl(int cmd, const char *name, + void *p, int skiperr) { struct ifreq ifr; int fd; @@ -180,7 +134,8 @@ static int tnl_gen_ioctl(int cmd, const char *name, void *p, int skiperr) fd = socket(preferred_family, SOCK_DGRAM, 0); err = ioctl(fd, cmd, &ifr); if (err && errno != skiperr) - perror("ioctl"); + fprintf(stderr, "%s: ioctl %x failed: %s\n", name, + cmd, strerror(errno)); close(fd); return err; } diff --git a/ip/tunnel.h b/ip/tunnel.h index ded226b4..7e7fe135 100644 --- a/ip/tunnel.h +++ b/ip/tunnel.h @@ -25,9 +25,7 @@ #include const char *tnl_strproto(__u8 proto); -int tnl_ioctl_get_ifindex(const char *dev); -int tnl_ioctl_get_iftype(const char *dev); -char * tnl_ioctl_get_ifname(int idx); + int tnl_get_ioctl(const char *basedev, void *p); int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p); int tnl_del_ioctl(const char *basedev, const char *name, void *p);