/* Return value becomes exitcode. It's okay to not return at all */ static int do_show(char **argv) { int err; struct ip_tunnel_parm p; parse_args(argv, SIOCGETTUNNEL, &p); switch (p.iph.protocol) { case IPPROTO_IPIP: err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p); break; case IPPROTO_GRE: err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p); break; case IPPROTO_IPV6: err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p); break; default: do_tunnels_list(&p); return 0; } if (err) return -1; print_tunnel(&p); bb_putchar('\n'); return 0; }
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; switch (p.iph.protocol) { case IPPROTO_IPIP: if (p.i_flags & VTI_ISVTI) err = tnl_get_ioctl(p.name[0] ? p.name : "ip_vti0", &p); else err = tnl_get_ioctl(p.name[0] ? p.name : "tunl0", &p); break; case IPPROTO_GRE: err = tnl_get_ioctl(p.name[0] ? p.name : "gre0", &p); break; case IPPROTO_IPV6: err = tnl_get_ioctl(p.name[0] ? p.name : "sit0", &p); break; default: do_tunnels_list(&p); return 0; } if (err) return -1; print_tunnel(&p); printf("\n"); return 0; }
static int do_show(int argc, char **argv) { struct ip_tunnel_parm p, p1; const char *basedev; if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) return -1; basedev = tnl_defname(&p); if (!basedev) { struct tnl_print_nlmsg_info info = { .p1 = &p, .p2 = &p1, .init = ip_tunnel_parm_initialize, .match = ip_tunnel_parm_match, .print = print_tunnel, }; return do_tunnels_list(&info); } if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p)) return -1; print_tunnel(&p); fputc('\n', stdout); return 0; }
static void do_tunnels_list(struct ip_tunnel_parm *p) { char name[IFNAMSIZ]; unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, 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]; FILE *fp = fopen_or_warn("/proc/net/dev", "r"); if (fp == NULL) { return; } /* skip headers */ fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); while (fgets(buf, sizeof(buf), fp) != NULL) { char *ptr; /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */ ptr = strchr(buf, ':'); if (ptr == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1) ) { bb_error_msg("wrong format of /proc/net/dev"); return; } if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", &rx_bytes, &rx_packets, &rx_errs, &rx_drops, &rx_fifo, &rx_frame, &rx_multi, &tx_bytes, &tx_packets, &tx_errs, &tx_drops, &tx_fifo, &tx_colls, &tx_carrier) != 14) continue; if (p->name[0] && strcmp(p->name, name)) continue; type = do_ioctl_get_iftype(name); if (type == -1) { bb_error_msg("can't get type of [%s]", name); continue; } if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) continue; memset(&p1, 0, sizeof(p1)); if (do_get_ioctl(name, &p1)) continue; if ((p->link && p1.link != p->link) || (p->name[0] && strcmp(p1.name, p->name)) || (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || (p->i_key && p1.i_key != p->i_key) ) { continue; } print_tunnel(&p1); bb_putchar('\n'); } }
static int tunnel_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { int ret = get_response(n, arg); if (ret == 0) print_tunnel(arg); return ret; }
static int do_tunnels_list(struct ip_tunnel_parm *p) { char name[IFNAMSIZ]; unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, 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]; FILE *fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { perror("fopen"); return -1; } fgets(buf, sizeof(buf), fp); fgets(buf, sizeof(buf), fp); while (fgets(buf, sizeof(buf), fp) != NULL) { char *ptr; buf[sizeof(buf) - 1] = 0; if ((ptr = strchr(buf, ':')) == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { bb_error_msg("Wrong format of /proc/net/dev. Sorry."); return -1; } if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu", &rx_bytes, &rx_packets, &rx_errs, &rx_drops, &rx_fifo, &rx_frame, &rx_multi, &tx_bytes, &tx_packets, &tx_errs, &tx_drops, &tx_fifo, &tx_colls, &tx_carrier) != 14) continue; if (p->name[0] && strcmp(p->name, name)) continue; type = do_ioctl_get_iftype(name); if (type == -1) { bb_error_msg("Failed to get type of [%s]", name); continue; } if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) continue; memset(&p1, 0, sizeof(p1)); if (do_get_ioctl(name, &p1)) continue; if ((p->link && p1.link != p->link) || (p->name[0] && strcmp(p1.name, p->name)) || (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || (p->i_key && p1.i_key != p->i_key)) continue; print_tunnel(&p1); printf("\n"); } return 0; }
int print_mpls(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { struct rtattr *tb[MPLS_ATTR_MAX + 1]; struct genlmsghdr *ghdr = NLMSG_DATA(n); int len = n->nlmsg_len; struct rtattr *attrs; if (n->nlmsg_type != PF_MPLS) { fprintf(stderr, "Not a controller message, nlmsg_len=%d " "nlmsg_type=0x%x\n", n->nlmsg_len, n->nlmsg_type); return 0; } len -= NLMSG_LENGTH(GENL_HDRLEN); if (len < 0) { fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); return -1; } attrs = (struct rtattr *) ((char *) ghdr + GENL_HDRLEN); parse_rtattr(tb, MPLS_ATTR_MAX, attrs, len); switch (ghdr->cmd) { case MPLS_CMD_NEWILM: return print_ilm(n,arg,tb); case MPLS_CMD_NEWNHLFE: return print_nhlfe(n,arg,tb); case MPLS_CMD_NEWXC: return print_xc(n,arg,tb); case MPLS_CMD_SETLABELSPACE: return print_labelspace(n,arg,tb); default: return 0; } #if 0 if (n->nlmsg_type >= MPLS_RTM_ADDTUNNEL && n->nlmsg_type <= MPLS_RTM_DELTUNNEL) { return print_tunnel(n,arg, tb); } #endif return 0; }
static int do_show(int argc, char **argv) { struct ip_tunnel_parm p; const char *basedev; ll_init_map(&rth); if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) return -1; basedev = tnl_defname(&p); if (!basedev) return do_tunnels_list(&p); if (tnl_get_ioctl(p.name[0] ? p.name : basedev, &p)) return -1; print_tunnel(&p); printf("\n"); return 0; }
static int do_show(int argc, char **argv) { struct ip6_tnl_parm p; ip6_tnl_parm_init(&p, 0); if (parse_args(argc, argv, &p) < 0) return -1; if (!p.name[0] || show_stats) do_tunnels_list(&p); else { if (tnl_get_ioctl(p.name, &p)) return -1; print_tunnel(&p); printf("\n"); } return 0; }
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 */ if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0) return -1; if (!p.name[0] || show_stats) do_tunnels_list(&p); else { if (tnl_get_ioctl(p.name, &p)) return -1; print_tunnel(&p); printf("\n"); } return 0; }
static int do_tunnels_list(struct ip_tunnel_parm *p) { char name[IFNAMSIZ]; unsigned long rx_bytes, rx_packets, rx_errs, rx_drops, rx_fifo, rx_frame, tx_bytes, tx_packets, tx_errs, tx_drops, tx_fifo, tx_colls, tx_carrier, rx_multi; struct ip_tunnel_parm p1; char buf[512]; FILE *fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { perror("fopen"); return -1; } /* skip header lines */ if (!fgets(buf, sizeof(buf), fp) || !fgets(buf, sizeof(buf), fp)) { fprintf(stderr, "/proc/net/dev read error\n"); fclose(fp); return -1; } while (fgets(buf, sizeof(buf), fp) != NULL) { int index, type; char *ptr; buf[sizeof(buf) - 1] = 0; if ((ptr = strchr(buf, ':')) == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); fclose(fp); return -1; } if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", &rx_bytes, &rx_packets, &rx_errs, &rx_drops, &rx_fifo, &rx_frame, &rx_multi, &tx_bytes, &tx_packets, &tx_errs, &tx_drops, &tx_fifo, &tx_colls, &tx_carrier) != 14) continue; if (p->name[0] && strcmp(p->name, name)) continue; 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; } if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) continue; memset(&p1, 0, sizeof(p1)); if (tnl_get_ioctl(name, &p1)) continue; if ((p->link && p1.link != p->link) || (p->name[0] && strcmp(p1.name, p->name)) || (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || (p->i_key && p1.i_key != p->i_key)) continue; print_tunnel(&p1); if (show_stats) { printf("%s", _SL_); printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); } printf("\n"); } fclose(fp); return 0; }
static int do_tunnels_list(struct ip6_tnl_parm *p) { char buf[512]; int err = -1; FILE *fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { perror("fopen"); goto end; } /* skip two lines at the begenning of the file */ if (!fgets(buf, sizeof(buf), fp) || !fgets(buf, sizeof(buf), fp)) { fprintf(stderr, "/proc/net/dev read error\n"); return -1; } while (fgets(buf, sizeof(buf), fp) != NULL) { char name[IFNAMSIZ]; 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, tx_fifo, tx_colls, tx_carrier, rx_multi; struct ip6_tnl_parm p1; char *ptr; buf[sizeof(buf) - 1] = '\0'; if ((ptr = strchr(buf, ':')) == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { fprintf(stderr, "Wrong format of /proc/net/dev. Sorry.\n"); goto end; } if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", &rx_bytes, &rx_packets, &rx_errs, &rx_drops, &rx_fifo, &rx_frame, &rx_multi, &tx_bytes, &tx_packets, &tx_errs, &tx_drops, &tx_fifo, &tx_colls, &tx_carrier) != 14) continue; if (p->name[0] && strcmp(p->name, name)) continue; 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; } if (type != ARPHRD_TUNNEL6) continue; memset(&p1, 0, sizeof(p1)); ip6_tnl_parm_init(&p1, 0); strcpy(p1.name, name); p1.link = ll_name_to_index(p1.name); if (p1.link == 0) continue; if (tnl_get_ioctl(p1.name, &p1)) continue; if (!ip6_tnl_parm_match(p, &p1)) continue; print_tunnel(&p1); if (show_stats) { printf("%s", _SL_); printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s", rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_); printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld", tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops); } printf("\n"); } err = 0; end: if (fp) fclose(fp); return err; }
static int do_tunnels_list(struct ip6_tnl_parm2 *p) { char buf[512]; int err = -1; FILE *fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { perror("fopen"); return -1; } /* skip two lines at the begenning of the file */ if (!fgets(buf, sizeof(buf), fp) || !fgets(buf, sizeof(buf), fp)) { fprintf(stderr, "/proc/net/dev read error\n"); goto end; } while (fgets(buf, sizeof(buf), fp) != NULL) { char name[IFNAMSIZ]; int index, type; struct ip6_tnl_parm2 p1 = {}; char *ptr; buf[sizeof(buf) - 1] = '\0'; if ((ptr = strchr(buf, ':')) == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); goto end; } if (p->name[0] && strcmp(p->name, name)) continue; 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; } if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE) continue; ip6_tnl_parm_init(&p1, 0); if (type == ARPHRD_IP6GRE) p1.proto = IPPROTO_GRE; strcpy(p1.name, name); p1.link = ll_name_to_index(p1.name); if (p1.link == 0) continue; if (tnl_get_ioctl(p1.name, &p1)) continue; if (!ip6_tnl_parm_match(p, &p1)) continue; print_tunnel(&p1); if (show_stats) tnl_print_stats(ptr); printf("\n"); } err = 0; end: fclose(fp); return err; }
static int do_tunnels_list(struct ip_tunnel_parm *p) { char buf[512]; int err = -1; FILE *fp = fopen("/proc/net/dev", "r"); if (fp == NULL) { perror("fopen"); return -1; } /* skip header lines */ if (!fgets(buf, sizeof(buf), fp) || !fgets(buf, sizeof(buf), fp)) { fprintf(stderr, "/proc/net/dev read error\n"); goto end; } while (fgets(buf, sizeof(buf), fp) != NULL) { char name[IFNAMSIZ]; int index, type; struct ip_tunnel_parm p1; char *ptr; buf[sizeof(buf) - 1] = 0; ptr = strchr(buf, ':'); if (ptr == NULL || (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); goto end; } if (p->name[0] && strcmp(p->name, name)) continue; 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; } if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) continue; memset(&p1, 0, sizeof(p1)); if (tnl_get_ioctl(name, &p1)) continue; if ((p->link && p1.link != p->link) || (p->name[0] && strcmp(p1.name, p->name)) || (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || (p->i_key && p1.i_key != p->i_key)) continue; print_tunnel(&p1); if (show_stats) tnl_print_stats(ptr); printf("\n"); } err = 0; end: fclose(fp); return err; }