int ip_main(int argc, char **argv);
int ip_main(int argc, char **argv)
{
- int ret = EXIT_FAILURE;
-
ip_parse_common_args(&argc, &argv);
- if (argc > 1) {
- if (ENABLE_FEATURE_IP_ADDRESS && matches(argv[1], "address") == 0) {
- ret = do_ipaddr(argc-2, argv+2);
- }
- if (ENABLE_FEATURE_IP_ROUTE && matches(argv[1], "route") == 0) {
- ret = do_iproute(argc-2, argv+2);
- }
- if (ENABLE_FEATURE_IP_LINK && matches(argv[1], "link") == 0) {
- ret = do_iplink(argc-2, argv+2);
- }
- if (ENABLE_FEATURE_IP_TUNNEL
- && (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0)
- ) {
- ret = do_iptunnel(argc-2, argv+2);
- }
- if (ENABLE_FEATURE_IP_RULE && matches(argv[1], "rule") == 0) {
- ret = do_iprule(argc-2, argv+2);
- }
- }
- if (ret) {
+ if (argc <= 1)
bb_show_usage();
+
+ if (ENABLE_FEATURE_IP_ADDRESS && matches(argv[1], "address") == 0) {
+ return do_ipaddr(argc-2, argv+2);
+ }
+ if (ENABLE_FEATURE_IP_ROUTE && matches(argv[1], "route") == 0) {
+ return do_iproute(argc-2, argv+2);
}
- return EXIT_SUCCESS;
+ if (ENABLE_FEATURE_IP_LINK && matches(argv[1], "link") == 0) {
+ return do_iplink(argc-2, argv+2);
+ }
+ if (ENABLE_FEATURE_IP_TUNNEL
+ && (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0)
+ ) {
+ return do_iptunnel(argc-2, argv+2);
+ }
+ if (ENABLE_FEATURE_IP_RULE && matches(argv[1], "rule") == 0) {
+ return do_iprule(argc-2, argv+2);
+ }
+
+ bb_show_usage();
}
#include <linux/if_link.h>
#endif
-extern int preferred_family;
-//FIXME! Appears in two .h files!
-extern const char * _SL_;
-
extern void ip_parse_common_args(int *argcp, char ***argvp);
extern int print_neigh(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);
extern int ipaddr_list_or_flush(int argc, char **argv, int flush);
extern int iproute_monitor(int argc, char **argv);
extern void iplink_usage(void) ATTRIBUTE_NORETURN;
extern void ipneigh_reset_filter(void);
+
extern int do_ipaddr(int argc, char **argv);
extern int do_iproute(int argc, char **argv);
extern int do_iprule(int argc, char **argv);
int preferred_family = AF_UNSPEC;
-int oneline = 0;
-const char * _SL_ = NULL;
+smallint oneline;
+char _SL_;
void ip_parse_common_args(int *argcp, char ***argvp)
{
argc--;
argv++;
}
- _SL_ = oneline ? "\\" : "\n" ;
+ _SL_ = oneline ? '\\' : '\n' ;
*argcp = argc;
*argvp = argv;
}
#include "ip_common.h"
-static struct
-{
+typedef struct filter_t {
int ifindex;
int family;
int oneline;
int flushp;
int flushe;
struct rtnl_handle *rth;
-} filter;
+} filter_t;
+
+#define filter (*(filter_t*)&bb_common_bufsiz1)
+
static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
{
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
- perror("SIOCGIFXQLEN");
+ bb_perror_msg("SIOCGIFXQLEN");
close(s);
return;
}
bb_error_msg("nil ifname");
return -1;
}
- if (filter.label &&
- (!filter.family || filter.family == AF_PACKET) &&
- fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
+ if (filter.label
+ && (!filter.family || filter.family == AF_PACKET)
+ && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
+ ) {
return 0;
+ }
if (n->nlmsg_type == RTM_DELLINK)
fprintf(fp, "Deleted ");
if (!filter.family || filter.family == AF_PACKET) {
SPRINT_BUF(b1);
- fprintf(fp, "%s", _SL_);
- fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
+ fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
if (tb[IFLA_ADDRESS]) {
fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
b1, sizeof(b1)));
}
}
- fprintf(fp, "\n");
+ fputc('\n', fp);
fflush(fp);
return 0;
}
static int flush_update(void)
{
if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
- perror("Failed to send flush request\n");
+ bb_perror_msg("failed to send flush request");
return -1;
}
filter.flushp = 0;
if (rta_tb[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
char buf[128];
- fprintf(fp, "%s", _SL_);
+ fputc(_SL_, fp);
if (ci->ifa_valid == 0xFFFFFFFFU)
sprintf(buf, "valid_lft forever");
else
sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
fprintf(fp, " %s", buf);
}
- fprintf(fp, "\n");
+ fputc('\n', fp);
fflush(fp);
return 0;
}
static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
{
- for ( ;ainfo ; ainfo = ainfo->next) {
+ for (; ainfo; ainfo = ainfo->next) {
struct nlmsghdr *n = &ainfo->h;
struct ifaddrmsg *ifa = NLMSG_DATA(n);
filter.oneline = _oneline;
}
+/* Return value becomes exitcode. It's okay to not return at all */
int ipaddr_list_or_flush(int argc, char **argv, int flush)
{
static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
if (flush) {
if (argc <= 0) {
- bb_error_msg(bb_msg_requires_arg, "flush");
- return -1;
+ bb_error_msg_and_die(bb_msg_requires_arg, "flush");
}
if (filter.family == AF_PACKET) {
- bb_error_msg("cannot flush link addresses");
- return -1;
+ bb_error_msg_and_die("cannot flush link addresses");
}
}
if (filter_dev) {
filter.ifindex = ll_name_to_index(filter_dev);
if (filter.ifindex <= 0) {
- bb_error_msg("device \"%s\" does not exist", filter_dev);
- return -1;
+ bb_error_msg_and_die("device \"%s\" does not exist", filter_dev);
}
}
for (;;) {
if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
- perror("Cannot send dump request");
- exit(1);
+ bb_perror_msg_and_die("cannot send dump request");
}
filter.flushed = 0;
if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
- fprintf(stderr, "Flush terminated\n");
- exit(1);
+ bb_error_msg_and_die("flush terminated");
}
if (filter.flushed == 0) {
- fflush(stdout);
return 0;
}
if (flush_update() < 0)
- exit(1);
+ return 1;
}
}
}
}
- for (l=linfo; l; l = l->next) {
+ for (l = linfo; l; l = l->next) {
if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
if (filter.family != AF_PACKET)
print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
}
- fflush(stdout);
+ fflush(stdout); /* why? */
}
- exit(0);
+ return 0;
}
static int default_scope(inet_prefix *lcl)
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int ipaddr_modify(int cmd, int argc, char **argv)
{
static const char *const option[] = {
inet_prefix brd;
int i;
if (req.ifa.ifa_family != AF_INET) {
- bb_error_msg("broadcast can be set only for IPv4 addresses");
- return -1;
+ bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
}
brd = peer;
if (brd.bitlen <= 30) {
ll_init_map(&rth);
- if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
- bb_error_msg("cannot find device \"%s\"", d);
- return -1;
+ req.ifa.ifa_index = ll_name_to_index(d);
+ if (req.ifa.ifa_index == 0) {
+ bb_error_msg_and_die("cannot find device \"%s\"", d);
}
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
- exit(2);
+ return 2;
- exit(0);
+ return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
int do_ipaddr(int argc, char **argv)
{
static const char *const commands[] = {
#include "utils.h"
#include "ip_common.h"
-/* take from linux/sockios.h */
+/* taken from linux/sockios.h */
#define SIOCSIFNAME 0x8923 /* set interface name */
-static int on_off(const char *msg)
+static void on_off(const char *msg) ATTRIBUTE_NORETURN;
+static void on_off(const char *msg)
{
- bb_error_msg("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
- return -1;
+ bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
}
+/* Exits on error */
static int get_ctl_fd(void)
{
- int s_errno;
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
return fd;
- s_errno = errno;
fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (fd >= 0)
return fd;
fd = socket(PF_INET6, SOCK_DGRAM, 0);
if (fd >= 0)
return fd;
- errno = s_errno;
- bb_perror_msg("cannot create control socket");
- return -1;
+ bb_perror_msg_and_die("cannot create control socket");
}
-static int do_chflags(char *dev, uint32_t flags, uint32_t mask)
+/* Exits on error */
+static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
{
struct ifreq ifr;
int fd;
- int err;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
fd = get_ctl_fd();
- if (fd < 0)
- return -1;
- err = ioctl(fd, SIOCGIFFLAGS, &ifr);
- if (err) {
- bb_perror_msg("SIOCGIFFLAGS");
- close(fd);
- return -1;
+ if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
+ bb_perror_msg_and_die("SIOCGIFFLAGS");
}
- if ((ifr.ifr_flags^flags)&mask) {
+ if ((ifr.ifr_flags ^ flags) & mask) {
ifr.ifr_flags &= ~mask;
- ifr.ifr_flags |= mask&flags;
- err = ioctl(fd, SIOCSIFFLAGS, &ifr);
- if (err)
- bb_perror_msg("SIOCSIFFLAGS");
+ ifr.ifr_flags |= mask & flags;
+ if (ioctl(fd, SIOCSIFFLAGS, &ifr))
+ bb_perror_msg_and_die("SIOCSIFFLAGS");
}
close(fd);
- return err;
}
-static int do_changename(char *dev, char *newdev)
+/* Exits on error */
+static void do_changename(char *dev, char *newdev)
{
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
fd = get_ctl_fd();
- if (fd < 0)
- return -1;
err = ioctl(fd, SIOCSIFNAME, &ifr);
if (err) {
- bb_perror_msg("SIOCSIFNAME");
- close(fd);
- return -1;
+ bb_perror_msg_and_die("SIOCSIFNAME");
}
close(fd);
- return err;
}
-static int set_qlen(char *dev, int qlen)
+/* Exits on error */
+static void set_qlen(char *dev, int qlen)
{
struct ifreq ifr;
int s;
s = get_ctl_fd();
- if (s < 0)
- return -1;
-
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
ifr.ifr_qlen = qlen;
if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
- bb_perror_msg("SIOCSIFXQLEN");
- close(s);
- return -1;
+ bb_perror_msg_and_die("SIOCSIFXQLEN");
}
close(s);
-
- return 0;
}
-static int set_mtu(char *dev, int mtu)
+/* Exits on error */
+static void set_mtu(char *dev, int mtu)
{
struct ifreq ifr;
int s;
s = get_ctl_fd();
- if (s < 0)
- return -1;
-
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
ifr.ifr_mtu = mtu;
if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
- bb_perror_msg("SIOCSIFMTU");
- close(s);
- return -1;
+ bb_perror_msg_and_die("SIOCSIFMTU");
}
close(s);
-
- return 0;
}
+/* Exits on error */
static int get_address(char *dev, int *htype)
{
struct ifreq ifr;
s = socket(PF_PACKET, SOCK_DGRAM, 0);
if (s < 0) {
- bb_perror_msg("socket(PF_PACKET)");
- return -1;
+ bb_perror_msg_and_die("socket(PF_PACKET)");
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
- bb_perror_msg("SIOCGIFINDEX");
- close(s);
- return -1;
+ bb_perror_msg_and_die("SIOCGIFINDEX");
}
memset(&me, 0, sizeof(me));
me.sll_ifindex = ifr.ifr_ifindex;
me.sll_protocol = htons(ETH_P_LOOP);
if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
- bb_perror_msg("bind");
- close(s);
- return -1;
+ bb_perror_msg_and_die("bind");
}
alen = sizeof(me);
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
- bb_perror_msg("getsockname");
- close(s);
- return -1;
+ bb_perror_msg_and_die("getsockname");
}
close(s);
*htype = me.sll_hatype;
return me.sll_halen;
}
-static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
+/* Exits on error */
+static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
{
int alen;
ifr->ifr_hwaddr.sa_family = hatype;
alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
if (alen < 0)
- return -1;
+ exit(1);
if (alen != halen) {
- bb_error_msg("wrong address (%s) length: expected %d bytes", lla, halen);
- return -1;
+ bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
}
- return 0;
}
-static int set_address(struct ifreq *ifr, int brd)
+/* Exits on error */
+static void set_address(struct ifreq *ifr, int brd)
{
int s;
s = get_ctl_fd();
- if (s < 0)
- return -1;
- if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
- bb_perror_msg(brd ? "SIOCSIFHWBROADCAST" : "SIOCSIFHWADDR");
- close(s);
- return -1;
+ if (ioctl(s, brd ? SIOCSIFHWBROADCAST :SIOCSIFHWADDR, ifr) < 0) {
+ bb_perror_msg_and_die(brd ? "SIOCSIFHWBROADCAST" : "SIOCSIFHWADDR");
}
close(s);
- return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int do_set(int argc, char **argv)
{
char *dev = NULL;
} else if (strcmp(*argv, "off") == 0) {
flags &= ~IFF_MULTICAST;
} else
- return on_off("multicast");
+ on_off("multicast");
} else if (strcmp(*argv, "arp") == 0) {
NEXT_ARG();
mask |= IFF_NOARP;
} else if (strcmp(*argv, "off") == 0) {
flags |= IFF_NOARP;
} else
- return on_off("noarp");
+ on_off("noarp");
} else if (strcmp(*argv, "addr") == 0) {
NEXT_ARG();
newaddr = *argv;
duparg2("dev", *argv);
dev = *argv;
}
- argc--; argv++;
+ argc--;
+ argv++;
}
if (!dev) {
- bb_error_msg(bb_msg_requires_arg, "\"dev\"");
- exit(-1);
+ bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
}
if (newaddr || newbrd) {
halen = get_address(dev, &htype);
- if (halen < 0)
- return -1;
if (newaddr) {
- if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
- return -1;
+ parse_address(dev, htype, halen, newaddr, &ifr0);
}
if (newbrd) {
- if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
- return -1;
+ parse_address(dev, htype, halen, newbrd, &ifr1);
}
}
if (newname && strcmp(dev, newname)) {
- if (do_changename(dev, newname) < 0)
- return -1;
+ do_changename(dev, newname);
dev = newname;
}
if (qlen != -1) {
- if (set_qlen(dev, qlen) < 0)
- return -1;
+ set_qlen(dev, qlen);
}
if (mtu != -1) {
- if (set_mtu(dev, mtu) < 0)
- return -1;
+ set_mtu(dev, mtu);
}
if (newaddr || newbrd) {
if (newbrd) {
- if (set_address(&ifr1, 1) < 0)
- return -1;
+ set_address(&ifr1, 1);
}
if (newaddr) {
- if (set_address(&ifr0, 0) < 0)
- return -1;
+ set_address(&ifr0, 0);
}
}
if (mask)
- return do_chflags(dev, flags, mask);
+ do_chflags(dev, flags, mask);
return 0;
}
return ipaddr_list_or_flush(argc, argv, 0);
}
+/* Return value becomes exitcode. It's okay to not return at all */
int do_iplink(int argc, char **argv)
{
- if (argc > 0) {
- if (matches(*argv, "set") == 0)
- return do_set(argc-1, argv+1);
- if (matches(*argv, "show") == 0 ||
- matches(*argv, "lst") == 0 ||
- matches(*argv, "list") == 0)
- return ipaddr_list_link(argc-1, argv+1);
- } else
+ if (argc <= 0)
return ipaddr_list_link(0, NULL);
- bb_error_msg("command \"%s\" is unknown", *argv);
- exit(-1);
+ if (matches(*argv, "set") == 0)
+ return do_set(argc-1, argv+1);
+
+ if (matches(*argv, "show") == 0 ||
+ matches(*argv, "lst") == 0 ||
+ matches(*argv, "list") == 0)
+ return ipaddr_list_link(argc-1, argv+1);
+
+ bb_error_msg_and_die("command \"%s\" is unknown", *argv);
}
#endif
-static struct
-{
+typedef struct filter_t {
int tb;
int flushed;
char *flushb;
inet_prefix mdst;
inet_prefix rsrc;
inet_prefix msrc;
-} filter;
+} filter_t;
+
+#define filter (*(filter_t*)&bb_common_bufsiz1)
static int flush_update(void)
{
if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
- perror("Failed to send flush request\n");
+ bb_perror_msg("failed to send flush request");
return -1;
}
filter.flushp = 0;
}
if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
if (r->rtm_flags & RTM_F_CLONED) {
- fprintf(fp, "%s cache ", _SL_);
+ fprintf(fp, "%c cache ", _SL_);
}
if (ci->rta_expires) {
fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
if (tb[RTA_IIF] && filter.iifmask != -1) {
fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
}
- fprintf(fp, "\n");
+ fputc('\n', fp);
fflush(fp);
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
{
struct rtnl_handle rth;
struct rtmsg r;
char buf[1024];
} req;
- char mxbuf[256];
+ char mxbuf[256];
struct rtattr * mxrta = (void*)mxbuf;
unsigned mxlock = 0;
- char *d = NULL;
+ char *d = NULL;
int gw_ok = 0;
int dst_ok = 0;
int proto_ok = 0;
if (strcmp(*argv, "to") == 0) {
NEXT_ARG();
}
- if ((**argv < '0' || **argv > '9') &&
- rtnl_rtntype_a2n(&type, *argv) == 0) {
+ if ((**argv < '0' || **argv > '9')
+ && rtnl_rtntype_a2n(&type, *argv) == 0
+ ) {
NEXT_ARG();
req.r.rtm_type = type;
type_ok = 1;
}
if (rtnl_open(&rth, 0) < 0) {
- exit(1);
+ return 1;
}
if (d) {
if (d) {
idx = ll_name_to_index(d);
if (idx == 0) {
- bb_error_msg("cannot find device \"%s\"", d);
- return -1;
+ bb_error_msg_and_die("cannot find device \"%s\"", d);
}
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
}
}
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
- exit(2);
+ return 2;
}
return 0;
return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
}
-static int iproute_flush_cache(void)
+static void iproute_flush_cache(void)
{
static const char fn[] = "/proc/sys/net/ipv4/route/flush";
int flush_fd = open(fn, O_WRONLY);
+
if (flush_fd < 0) {
bb_perror_msg("cannot open '%s'", fn);
- return -1;
+ return;
}
if (write(flush_fd, "-1", 2) < 2) {
bb_perror_msg("cannot flush routing cache");
- return -1;
+ return;
}
close(flush_fd);
- return 0;
}
static void iproute_reset_filter(void)
filter.msrc.bitlen = -1;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int iproute_list_or_flush(int argc, char **argv, int flush)
{
int do_ipv6 = preferred_family;
iproute_reset_filter();
filter.tb = RT_TABLE_MAIN;
- if (flush && argc <= 0) {
- bb_error_msg(bb_msg_requires_arg, "\"ip route flush\"");
- return -1;
- }
+ if (flush && argc <= 0)
+ bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
while (argc > 0) {
if (matches(*argv, "protocol") == 0) {
filter.rdst = filter.mdst;
}
}
- argc--; argv++;
+ argc--;
+ argv++;
}
if (do_ipv6 == AF_UNSPEC && filter.tb) {
}
if (rtnl_open(&rth, 0) < 0) {
- exit(1);
+ return 1;
}
ll_init_map(&rth);
int idx;
if (id) {
- if ((idx = ll_name_to_index(id)) == 0) {
- bb_error_msg("cannot find device \"%s\"", id);
- return -1;
+ idx = ll_name_to_index(id);
+ if (idx == 0) {
+ bb_error_msg_and_die("cannot find device \"%s\"", id);
}
filter.iif = idx;
filter.iifmask = -1;
}
if (od) {
- if ((idx = ll_name_to_index(od)) == 0) {
+ idx = ll_name_to_index(od);
+ if (idx == 0) {
bb_error_msg("cannot find device \"%s\"", od);
}
filter.oif = idx;
for (;;) {
if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
- perror("Cannot send dump request");
- return -1;
+ bb_perror_msg_and_die("cannot send dump request");
}
filter.flushed = 0;
if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
- bb_error_msg("flush terminated");
- return -1;
+ bb_error_msg_and_die("flush terminated");
}
if (filter.flushed == 0) {
- fflush(stdout);
return 0;
}
- if (flush_update() < 0)
- exit(1);
+ if (flush_update())
+ return 1;
}
}
bb_error_msg_and_die("dump terminated");
}
- exit(0);
+ return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int iproute_get(int argc, char **argv)
{
struct rtnl_handle rth;
}
req.r.rtm_dst_len = addr.bitlen;
}
- argc--; argv++;
+ argc--;
+ argv++;
}
}
}
if (rtnl_open(&rth, 0) < 0)
- exit(1);
+ return 1;
ll_init_map(&rth);
int idx;
if (idev) {
- if ((idx = ll_name_to_index(idev)) == 0) {
- bb_error_msg("cannot find device \"%s\"", idev);
- return -1;
+ idx = ll_name_to_index(idev);
+ if (idx == 0) {
+ bb_error_msg_and_die("cannot find device \"%s\"", idev);
}
addattr32(&req.n, sizeof(req), RTA_IIF, idx);
}
if (odev) {
- if ((idx = ll_name_to_index(odev)) == 0) {
- bb_error_msg("cannot find device \"%s\"", odev);
- return -1;
+ idx = ll_name_to_index(odev);
+ if (idx == 0) {
+ bb_error_msg_and_die("cannot find device \"%s\"", odev);
}
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
}
}
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
- exit(2);
+ return 2;
}
if (connected && !from_ok) {
}
if (req.n.nlmsg_type != RTM_NEWROUTE) {
- bb_error_msg("not a route?");
- return -1;
+ bb_error_msg_and_die("not a route?");
}
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0) {
- bb_error_msg("wrong len %d", len);
- return -1;
+ bb_error_msg_and_die("wrong len %d", len);
}
memset(tb, 0, sizeof(tb));
tb[RTA_PREFSRC]->rta_type = RTA_SRC;
r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
} else if (!tb[RTA_SRC]) {
- bb_error_msg("failed to connect the route");
- return -1;
+ bb_error_msg_and_die("failed to connect the route");
}
if (!odev && tb[RTA_OIF]) {
tb[RTA_OIF]->rta_type = 0;
req.n.nlmsg_type = RTM_GETROUTE;
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
- exit(2);
+ return 2;
}
}
if (print_route(NULL, &req.n, (void*)stdout) < 0) {
+// how is this useful?
bb_error_msg_and_die("an error :-)");
}
- exit(0);
+ return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
int do_iproute(int argc, char **argv)
{
static const char * const ip_route_commands[] = {
} else if (r->rtm_type != RTN_UNICAST)
fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
- fprintf(fp, "\n");
+ fputc('\n', fp);
fflush(fp);
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int iprule_list(int argc, char **argv)
{
struct rtnl_handle rth;
return 1;
if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
- bb_perror_msg("Cannot send dump request");
- return 1;
+ bb_perror_msg_and_die("cannot send dump request");
}
if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
- bb_error_msg("Dump terminated");
- return 1;
+ bb_error_msg_and_die("dump terminated");
}
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int iprule_modify(int cmd, int argc, char **argv)
{
int table_ok = 0;
if (matches(*argv, "help") == 0)
bb_show_usage();
if (rtnl_rtntype_a2n(&type, *argv))
- invarg("Failed to parse rule type", *argv);
+// bogus-looking error message "invalid argument 'cannot parse rule type' to '<*argv>'"
+ invarg("cannot parse rule type", *argv);
req.r.rtm_type = type;
}
argc--;
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
int do_iprule(int argc, char **argv)
{
static const char * const ip_rule_commands[] =
}
return iprule_modify(cmd, argc-1, argv+1);
}
-
#include <sys/socket.h>
#include <sys/ioctl.h>
-#include <string.h>
-#include <unistd.h>
-
#include <netinet/ip.h>
#include <net/if.h>
#include "ip_common.h"
+/* Dies on error */
static int do_ioctl_get_ifindex(char *dev)
{
struct ifreq ifr;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
- bb_perror_msg("ioctl");
- return 0;
+ bb_perror_msg_and_die("SIOCGIFINDEX");
}
close(fd);
return ifr.ifr_ifindex;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
- bb_perror_msg("ioctl");
+ bb_perror_msg("SIOCGIFHWADDR");
return -1;
}
close(fd);
return ifr.ifr_addr.sa_family;
}
-
static char *do_ioctl_get_ifname(int idx)
{
- static struct ifreq ifr;
+ struct ifreq ifr;
int fd;
ifr.ifr_ifindex = idx;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
if (ioctl(fd, SIOCGIFNAME, &ifr)) {
- bb_perror_msg("ioctl");
+ bb_perror_msg("SIOCGIFNAME");
return NULL;
}
close(fd);
- return ifr.ifr_name;
+ return xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
}
-
-
static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
{
struct ifreq ifr;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGETTUNNEL, &ifr);
if (err) {
- bb_perror_msg("ioctl");
+ bb_perror_msg("SIOCGETTUNNEL");
}
close(fd);
return err;
}
+/* Dies on error, otherwise returns 0 */
static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
{
struct ifreq ifr;
int fd;
- int err;
if (cmd == SIOCCHGTUNNEL && p->name[0]) {
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
- err = ioctl(fd, cmd, &ifr);
- if (err) {
- bb_perror_msg("ioctl");
+ if (ioctl(fd, cmd, &ifr)) {
+ bb_perror_msg_and_die("ioctl");
}
close(fd);
- return err;
+ return 0;
}
+/* Dies on error, otherwise returns 0 */
static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
{
struct ifreq ifr;
int fd;
- int err;
if (p->name[0]) {
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
- err = ioctl(fd, SIOCDELTUNNEL, &ifr);
- if (err) {
- bb_perror_msg("ioctl");
+ if (ioctl(fd, SIOCDELTUNNEL, &ifr)) {
+ bb_perror_msg_and_die("SIOCDELTUNNEL");
}
close(fd);
- return err;
+ return 0;
}
-static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
+/* Dies on error */
+static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
{
int count = 0;
char medium[IFNAMSIZ];
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);
+ bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
}
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);
+ bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
}
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);
+ bb_error_msg_and_die("you managed to ask for more than one tunnel mode");
}
p->iph.protocol = IPPROTO_IPV6;
} else {
- bb_error_msg("cannot guess tunnel mode");
- exit(-1);
+ bb_error_msg_and_die("cannot guess tunnel mode");
}
} else if (strcmp(*argv, "key") == 0) {
unsigned uval;
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);
+ bb_error_msg_and_die("invalid value of \"key\"");
}
p->i_key = p->o_key = htonl(uval);
}
p->o_key = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0)<0) {
- bb_error_msg("invalid value of \"ikey\"");
- exit(-1);
+ bb_error_msg_and_die("invalid value of \"ikey\"");
}
p->i_key = htonl(uval);
}
p->o_key = get_addr32(*argv);
else {
if (get_unsigned(&uval, *argv, 0)<0) {
- bb_error_msg("invalid value of \"okey\"");
- exit(-1);
+ bb_error_msg_and_die("invalid value of \"okey\"");
}
p->o_key = htonl(uval);
}
struct ip_tunnel_parm old_p;
memset(&old_p, 0, sizeof(old_p));
if (do_get_ioctl(*argv, &old_p))
- return -1;
+ exit(1);
*p = old_p;
}
}
count++;
- argc--; argv++;
+ argc--;
+ argv++;
}
-
if (p->iph.protocol == 0) {
if (memcmp(p->name, "gre", 3) == 0)
p->iph.protocol = IPPROTO_GRE;
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;
+ 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->link == 0)
- return -1;
}
if (p->i_key == 0 && IN_MULTICAST(ntohl(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;
+ bb_error_msg_and_die("broadcast tunnel requires a source address");
}
- return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int do_add(int cmd, int argc, char **argv)
{
struct ip_tunnel_parm p;
- if (parse_args(argc, argv, cmd, &p) < 0)
- return -1;
+ parse_args(argc, argv, cmd, &p);
if (p.iph.ttl && p.iph.frag_off == 0) {
- bb_error_msg("ttl != 0 and noptmudisc are incompatible");
- return -1;
+ bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
}
switch (p.iph.protocol) {
case IPPROTO_IPV6:
return do_add_ioctl(cmd, "sit0", &p);
default:
- bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
- return -1;
+ bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
}
- return -1;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int do_del(int argc, char **argv)
{
struct ip_tunnel_parm p;
- if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
- return -1;
+ parse_args(argc, argv, SIOCDELTUNNEL, &p);
switch (p.iph.protocol) {
case IPPROTO_IPIP:
default:
return do_del_ioctl(p.name, &p);
}
- return -1;
}
static void print_tunnel(struct ip_tunnel_parm *p)
p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
if (p->link) {
char *n = do_ioctl_get_ifname(p->link);
- if (n)
+ if (n) {
printf(" dev %s ", n);
+ free(n);
+ }
}
if (p->iph.ttl)
printf(" ttl %d ", p->iph.ttl);
}
if (p->i_flags & GRE_SEQ)
- printf("%s Drop packets out of sequence.\n", _SL_);
+ printf("%c Drop packets out of sequence.\n", _SL_);
if (p->i_flags & GRE_CSUM)
- printf("%s Checksum in received packet is required.", _SL_);
+ printf("%c Checksum in received packet is required.", _SL_);
if (p->o_flags & GRE_SEQ)
- printf("%s Sequence packets on output.", _SL_);
+ printf("%c Sequence packets on output.", _SL_);
if (p->o_flags & GRE_CSUM)
- printf("%s Checksum output packets.", _SL_);
+ printf("%c Checksum output packets.", _SL_);
}
-static int do_tunnels_list(struct ip_tunnel_parm *p)
+static void do_tunnels_list(struct ip_tunnel_parm *p)
{
char name[IFNAMSIZ];
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
FILE *fp = fopen("/proc/net/dev", "r");
if (fp == NULL) {
- perror("fopen");
- return -1;
+ bb_perror_msg("fopen");
+ return;
}
fgets(buf, sizeof(buf), fp);
if (ptr == NULL ||
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
bb_error_msg("wrong format of /proc/net/dev. Sorry");
- return -1;
+ 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,
continue;
type = do_ioctl_get_iftype(name);
if (type == -1) {
- bb_error_msg("failed to get type of [%s]", name);
+ bb_error_msg("cannot get type of [%s]", name);
continue;
}
if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
print_tunnel(&p1);
puts("");
}
- return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
static int do_show(int argc, char **argv)
{
int err;
struct ip_tunnel_parm p;
- if (parse_args(argc, argv, SIOCGETTUNNEL, &p) < 0)
- return -1;
+ parse_args(argc, argv, SIOCGETTUNNEL, &p);
switch (p.iph.protocol) {
case IPPROTO_IPIP:
return 0;
}
+/* Return value becomes exitcode. It's okay to not return at all */
int do_iptunnel(int argc, char **argv)
{
if (argc > 0) {
h = ifi->ifi_index&0xF;
- for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
+ for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
if (im->index == ifi->ifi_index)
break;
if (idx == 0)
return "*";
- for (im = idxmap[idx&0xF]; im; im = im->next)
+ for (im = idxmap[idx & 0xF]; im; im = im->next)
if (im->index == idx)
return im->name;
snprintf(buf, 16, "if%d", idx);
if (idx == 0)
return -1;
- for (im = idxmap[idx&0xF]; im; im = im->next)
+ for (im = idxmap[idx & 0xF]; im; im = im->next)
if (im->index == idx)
return im->type;
return -1;
if (idx == 0)
return 0;
- for (im = idxmap[idx&0xF]; im; im = im->next)
+ for (im = idxmap[idx & 0xF]; im; im = im->next)
if (im->index == idx)
return im->flags;
return 0;
}
+// TODO: caching is not warranted - no users which repeatedly call it
int ll_name_to_index(char *name)
{
static char ncache[16];
static int icache;
+
struct idxmap *im;
int sock_fd;
int i;
return 0;
if (icache && strcmp(name, ncache) == 0)
return icache;
- for (i=0; i<16; i++) {
+ for (i = 0; i < 16; i++) {
for (im = idxmap[i]; im; im = im->next) {
if (strcmp(im->name, name) == 0) {
icache = im->index;
#include "libbb.h"
-#include <string.h>
-#include <unistd.h>
-
#include "utils.h"
#include "inet_common.h"
dst->bitlen = plen;
}
}
- done:
+ done:
if (slash)
*slash = '/';
return err;
#include "rtm_map.h"
extern int preferred_family;
-extern int show_stats;
-extern int show_details;
-extern int show_raw;
-extern int resolve_hosts;
-extern int oneline;
-//FIXME! Appears in two .h files!
-extern const char * _SL_;
+extern smallint show_stats; /* UNUSED */
+extern smallint show_details; /* UNUSED */
+extern smallint show_raw; /* UNUSED */
+extern smallint resolve_hosts; /* UNUSED */
+extern smallint oneline;
+extern char _SL_;
#ifndef IPPROTO_ESP
#define IPPROTO_ESP 50
void duparg(const char *, const char *) ATTRIBUTE_NORETURN;
void duparg2(const char *, const char *) ATTRIBUTE_NORETURN;
int matches(const char *arg, const char *pattern);
-extern int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits);
+int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits);
const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
int dnet_pton(int af, const char *src, void *addr);