/* 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 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 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; }
/* Dies on error */ static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p) { static const char keywords[] ALIGN1 = "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0" "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0" "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0" "remote\0""any\0""local\0""dev\0" "ttl\0""inherit\0""tos\0""dsfield\0" "name\0"; enum { ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip, ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq, ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc, ARG_remote, ARG_any, ARG_local, ARG_dev, ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield, ARG_name }; int count = 0; char medium[IFNAMSIZ]; int key; memset(p, 0, sizeof(*p)); medium[0] = '\0'; p->iph.version = 4; p->iph.ihl = 5; #ifndef IP_DF #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ #endif p->iph.frag_off = htons(IP_DF); while (*argv) { key = index_in_strings(keywords, *argv); if (key == ARG_mode) { NEXT_ARG(); key = index_in_strings(keywords, *argv); if (key == ARG_ipip || key == ARG_ip_ip ) { if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); } p->iph.protocol = IPPROTO_IPIP; } else if (key == ARG_gre || key == ARG_gre_ip ) { if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); } p->iph.protocol = IPPROTO_GRE; } else if (key == ARG_sit || key == ARG_ip6_ip ) { if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); } p->iph.protocol = IPPROTO_IPV6; } else { bb_error_msg_and_die("%s tunnel mode", "can't guess"); } } else if (key == ARG_key) { unsigned uval; NEXT_ARG(); p->i_flags |= GRE_KEY; p->o_flags |= GRE_KEY; if (strchr(*argv, '.')) p->i_key = p->o_key = get_addr32(*argv); else { uval = get_unsigned(*argv, "key"); p->i_key = p->o_key = htonl(uval); } } else if (key == ARG_ikey) { unsigned uval; NEXT_ARG(); p->i_flags |= GRE_KEY; if (strchr(*argv, '.')) p->o_key = get_addr32(*argv); else { uval = get_unsigned(*argv, "ikey"); p->i_key = htonl(uval); } } else if (key == ARG_okey) { unsigned uval; NEXT_ARG(); p->o_flags |= GRE_KEY; if (strchr(*argv, '.')) p->o_key = get_addr32(*argv); else { uval = get_unsigned(*argv, "okey"); p->o_key = htonl(uval); } } else if (key == ARG_seq) { p->i_flags |= GRE_SEQ; p->o_flags |= GRE_SEQ; } else if (key == ARG_iseq) { p->i_flags |= GRE_SEQ; } else if (key == ARG_oseq) { p->o_flags |= GRE_SEQ; } else if (key == ARG_csum) { p->i_flags |= GRE_CSUM; p->o_flags |= GRE_CSUM; } else if (key == ARG_icsum) { p->i_flags |= GRE_CSUM; } else if (key == ARG_ocsum) { p->o_flags |= GRE_CSUM; } else if (key == ARG_nopmtudisc) { p->iph.frag_off = 0; } else if (key == ARG_pmtudisc) { p->iph.frag_off = htons(IP_DF); } else if (key == ARG_remote) { NEXT_ARG(); key = index_in_strings(keywords, *argv); if (key != ARG_any) p->iph.daddr = get_addr32(*argv); } else if (key == ARG_local) { NEXT_ARG(); key = index_in_strings(keywords, *argv); if (key != ARG_any) p->iph.saddr = get_addr32(*argv); } else if (key == ARG_dev) { NEXT_ARG(); strncpy_IFNAMSIZ(medium, *argv); } else if (key == ARG_ttl) { unsigned uval; NEXT_ARG(); key = index_in_strings(keywords, *argv); if (key != ARG_inherit) { uval = get_unsigned(*argv, "TTL"); if (uval > 255) invarg(*argv, "TTL must be <=255"); p->iph.ttl = uval; } } else if (key == ARG_tos || key == ARG_dsfield ) { uint32_t uval; NEXT_ARG(); key = index_in_strings(keywords, *argv); if (key != ARG_inherit) { if (rtnl_dsfield_a2n(&uval, *argv)) invarg(*argv, "TOS"); p->iph.tos = uval; } else p->iph.tos = 1; } else { if (key == ARG_name) { NEXT_ARG(); } if (p->name[0]) duparg2("name", *argv); strncpy_IFNAMSIZ(p->name, *argv); if (cmd == SIOCCHGTUNNEL && count == 0) { struct ip_tunnel_parm old_p; memset(&old_p, 0, sizeof(old_p)); if (do_get_ioctl(*argv, &old_p)) exit(EXIT_FAILURE); *p = old_p; } } count++; argv++; } if (p->iph.protocol == 0) { if (memcmp(p->name, "gre", 3) == 0) p->iph.protocol = IPPROTO_GRE; else if (memcmp(p->name, "ipip", 4) == 0) p->iph.protocol = IPPROTO_IPIP; else if (memcmp(p->name, "sit", 3) == 0) p->iph.protocol = IPPROTO_IPV6; } if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { bb_error_msg_and_die("keys are not allowed with ipip and sit"); } } if (medium[0]) { p->link = do_ioctl_get_ifindex(medium); } if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { p->i_key = p->iph.daddr; p->i_flags |= GRE_KEY; } if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { p->o_key = p->iph.daddr; p->o_flags |= GRE_KEY; } if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { bb_error_msg_and_die("broadcast tunnel requires a source address"); } }
static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) { int count = 0; char medium[IFNAMSIZ]; memset(p, 0, sizeof(*p)); memset(&medium, 0, sizeof(medium)); p->iph.version = 4; p->iph.ihl = 5; #ifndef IP_DF #define IP_DF 0x4000 /* Flag: "Don't Fragment" */ #endif p->iph.frag_off = htons(IP_DF); while (argc > 0) { if (strcmp(*argv, "mode") == 0) { NEXT_ARG(); if (strcmp(*argv, "ipip") == 0 || strcmp(*argv, "ip/ip") == 0) { if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { bb_error_msg("You managed to ask for more than one tunnel mode."); exit(-1); } p->iph.protocol = IPPROTO_IPIP; } else if (strcmp(*argv, "gre") == 0 || strcmp(*argv, "gre/ip") == 0) { if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { bb_error_msg("You managed to ask for more than one tunnel mode."); exit(-1); } p->iph.protocol = IPPROTO_GRE; } else if (strcmp(*argv, "sit") == 0 || strcmp(*argv, "ipv6/ip") == 0) { if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { bb_error_msg("You managed to ask for more than one tunnel mode."); exit(-1); } p->iph.protocol = IPPROTO_IPV6; } else { bb_error_msg("Cannot guess tunnel mode."); exit(-1); } } else if (strcmp(*argv, "key") == 0) { unsigned uval; NEXT_ARG(); p->i_flags |= GRE_KEY; p->o_flags |= GRE_KEY; if (strchr(*argv, '.')) p->i_key = p->o_key = get_addr32(*argv); else { if (get_unsigned(&uval, *argv, 0)<0) { bb_error_msg("invalid value of \"key\""); exit(-1); } p->i_key = p->o_key = htonl(uval); } } else if (strcmp(*argv, "ikey") == 0) { unsigned uval; NEXT_ARG(); p->i_flags |= GRE_KEY; if (strchr(*argv, '.')) p->o_key = get_addr32(*argv); else { if (get_unsigned(&uval, *argv, 0)<0) { bb_error_msg("invalid value of \"ikey\""); exit(-1); } p->i_key = htonl(uval); } } else if (strcmp(*argv, "okey") == 0) { unsigned uval; NEXT_ARG(); p->o_flags |= GRE_KEY; if (strchr(*argv, '.')) p->o_key = get_addr32(*argv); else { if (get_unsigned(&uval, *argv, 0)<0) { bb_error_msg("invalid value of \"okey\""); exit(-1); } p->o_key = htonl(uval); } } else if (strcmp(*argv, "seq") == 0) { p->i_flags |= GRE_SEQ; p->o_flags |= GRE_SEQ; } else if (strcmp(*argv, "iseq") == 0) { p->i_flags |= GRE_SEQ; } else if (strcmp(*argv, "oseq") == 0) { p->o_flags |= GRE_SEQ; } else if (strcmp(*argv, "csum") == 0) { p->i_flags |= GRE_CSUM; p->o_flags |= GRE_CSUM; } else if (strcmp(*argv, "icsum") == 0) { p->i_flags |= GRE_CSUM; } else if (strcmp(*argv, "ocsum") == 0) { p->o_flags |= GRE_CSUM; } else if (strcmp(*argv, "nopmtudisc") == 0) { p->iph.frag_off = 0; } else if (strcmp(*argv, "pmtudisc") == 0) { p->iph.frag_off = htons(IP_DF); } else if (strcmp(*argv, "remote") == 0) { NEXT_ARG(); if (strcmp(*argv, "any")) p->iph.daddr = get_addr32(*argv); } else if (strcmp(*argv, "local") == 0) { NEXT_ARG(); if (strcmp(*argv, "any")) p->iph.saddr = get_addr32(*argv); } else if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); strncpy(medium, *argv, IFNAMSIZ-1); } else if (strcmp(*argv, "ttl") == 0) { unsigned uval; NEXT_ARG(); if (strcmp(*argv, "inherit") != 0) { if (get_unsigned(&uval, *argv, 0)) invarg(*argv, "TTL"); if (uval > 255) invarg(*argv, "TTL must be <=255"); p->iph.ttl = uval; } } else if (strcmp(*argv, "tos") == 0 || matches(*argv, "dsfield") == 0) { __u32 uval; NEXT_ARG(); if (strcmp(*argv, "inherit") != 0) { if (rtnl_dsfield_a2n(&uval, *argv)) invarg(*argv, "TOS"); p->iph.tos = uval; } else p->iph.tos = 1; } else { if (strcmp(*argv, "name") == 0) { NEXT_ARG(); } if (p->name[0]) duparg2("name", *argv); strncpy(p->name, *argv, IFNAMSIZ); if (cmd == SIOCCHGTUNNEL && count == 0) { struct ip_tunnel_parm old_p; memset(&old_p, 0, sizeof(old_p)); if (do_get_ioctl(*argv, &old_p)) return -1; *p = old_p; } } count++; argc--; argv++; } if (p->iph.protocol == 0) { if (memcmp(p->name, "gre", 3) == 0) p->iph.protocol = IPPROTO_GRE; else if (memcmp(p->name, "ipip", 4) == 0) p->iph.protocol = IPPROTO_IPIP; else if (memcmp(p->name, "sit", 3) == 0) p->iph.protocol = IPPROTO_IPV6; } if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { bb_error_msg("Keys are not allowed with ipip and sit."); return -1; } } if (medium[0]) { p->link = do_ioctl_get_ifindex(medium); if (p->link == 0) return -1; } if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { p->i_key = p->iph.daddr; p->i_flags |= GRE_KEY; } if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) { p->o_key = p->iph.daddr; p->o_flags |= GRE_KEY; } if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { bb_error_msg("Broadcast tunnel requires a source address."); return -1; } return 0; }