busybox/networking/libiproute/ipaddress.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   4 *
   5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
   6 *
   7 * Changes:
   8 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
   9 */
  10
  11#include <fnmatch.h>
  12#include <net/if.h>
  13#include <net/if_arp.h>
  14
  15#include "ip_common.h"  /* #include "libbb.h" is inside */
  16#include "rt_names.h"
  17#include "utils.h"
  18
  19#ifndef IFF_LOWER_UP
  20/* from linux/if.h */
  21#define IFF_LOWER_UP  0x10000  /* driver signals L1 up */
  22#endif
  23
  24struct filter_t {
  25        char *label;
  26        char *flushb;
  27        struct rtnl_handle *rth;
  28        int scope, scopemask;
  29        int flags, flagmask;
  30        int flushp;
  31        int flushe;
  32        int ifindex;
  33        family_t family;
  34        smallint showqueue;
  35        smallint oneline;
  36        smallint up;
  37        smallint flushed;
  38        inet_prefix pfx;
  39} FIX_ALIASING;
  40typedef struct filter_t filter_t;
  41
  42#define G_filter (*(filter_t*)&bb_common_bufsiz1)
  43
  44
  45static void print_link_flags(unsigned flags, unsigned mdown)
  46{
  47        static const int flag_masks[] = {
  48                IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
  49                IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
  50        static const char flag_labels[] ALIGN1 =
  51                "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
  52                "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
  53
  54        bb_putchar('<');
  55        if (flags & IFF_UP && !(flags & IFF_RUNNING))
  56                printf("NO-CARRIER,");
  57        flags &= ~IFF_RUNNING;
  58#if 0
  59        _PF(ALLMULTI);
  60        _PF(PROMISC);
  61        _PF(MASTER);
  62        _PF(SLAVE);
  63        _PF(DEBUG);
  64        _PF(DYNAMIC);
  65        _PF(AUTOMEDIA);
  66        _PF(PORTSEL);
  67        _PF(NOTRAILERS);
  68#endif
  69        flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
  70        if (flags)
  71                printf("%x", flags);
  72        if (mdown)
  73                printf(",M-DOWN");
  74        printf("> ");
  75}
  76
  77static void print_queuelen(char *name)
  78{
  79        struct ifreq ifr;
  80        int s;
  81
  82        s = socket(AF_INET, SOCK_STREAM, 0);
  83        if (s < 0)
  84                return;
  85
  86        memset(&ifr, 0, sizeof(ifr));
  87        strncpy_IFNAMSIZ(ifr.ifr_name, name);
  88        if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
  89                close(s);
  90                return;
  91        }
  92        close(s);
  93
  94        if (ifr.ifr_qlen)
  95                printf("qlen %d", ifr.ifr_qlen);
  96}
  97
  98static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
  99{
 100        struct ifinfomsg *ifi = NLMSG_DATA(n);
 101        struct rtattr *tb[IFLA_MAX+1];
 102        int len = n->nlmsg_len;
 103
 104        if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
 105                return 0;
 106
 107        len -= NLMSG_LENGTH(sizeof(*ifi));
 108        if (len < 0)
 109                return -1;
 110
 111        if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex)
 112                return 0;
 113        if (G_filter.up && !(ifi->ifi_flags & IFF_UP))
 114                return 0;
 115
 116        memset(tb, 0, sizeof(tb));
 117        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
 118        if (tb[IFLA_IFNAME] == NULL) {
 119                bb_error_msg("nil ifname");
 120                return -1;
 121        }
 122        if (G_filter.label
 123         && (!G_filter.family || G_filter.family == AF_PACKET)
 124         && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
 125        ) {
 126                return 0;
 127        }
 128
 129        if (n->nlmsg_type == RTM_DELLINK)
 130                printf("Deleted ");
 131
 132        printf("%d: %s", ifi->ifi_index,
 133                /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/
 134                (char*)RTA_DATA(tb[IFLA_IFNAME])
 135        );
 136
 137        {
 138                unsigned m_flag = 0;
 139                if (tb[IFLA_LINK]) {
 140                        int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
 141                        if (iflink == 0)
 142                                printf("@NONE: ");
 143                        else {
 144                                printf("@%s: ", ll_index_to_name(iflink));
 145                                m_flag = ll_index_to_flags(iflink);
 146                                m_flag = !(m_flag & IFF_UP);
 147                        }
 148                } else {
 149                        printf(": ");
 150                }
 151                print_link_flags(ifi->ifi_flags, m_flag);
 152        }
 153
 154        if (tb[IFLA_MTU])
 155                printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
 156        if (tb[IFLA_QDISC])
 157                printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
 158#ifdef IFLA_MASTER
 159        if (tb[IFLA_MASTER]) {
 160                printf("master %s ", ll_index_to_name(*(int*)RTA_DATA(tb[IFLA_MASTER])));
 161        }
 162#endif
 163/* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
 164#ifdef IFF_DORMANT
 165        if (tb[IFLA_OPERSTATE]) {
 166                static const char operstate_labels[] ALIGN1 =
 167                        "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
 168                        "TESTING\0""DORMANT\0""UP\0";
 169                printf("state %s ", nth_string(operstate_labels,
 170                                        *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
 171        }
 172#endif
 173        if (G_filter.showqueue)
 174                print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
 175
 176        if (!G_filter.family || G_filter.family == AF_PACKET) {
 177                SPRINT_BUF(b1);
 178                printf("%c    link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
 179
 180                if (tb[IFLA_ADDRESS]) {
 181                        fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
 182                                                      RTA_PAYLOAD(tb[IFLA_ADDRESS]),
 183                                                      ifi->ifi_type,
 184                                                      b1, sizeof(b1)), stdout);
 185                }
 186                if (tb[IFLA_BROADCAST]) {
 187                        if (ifi->ifi_flags & IFF_POINTOPOINT)
 188                                printf(" peer ");
 189                        else
 190                                printf(" brd ");
 191                        fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
 192                                                      RTA_PAYLOAD(tb[IFLA_BROADCAST]),
 193                                                      ifi->ifi_type,
 194                                                      b1, sizeof(b1)), stdout);
 195                }
 196        }
 197        bb_putchar('\n');
 198        /*fflush_all();*/
 199        return 0;
 200}
 201
 202static int flush_update(void)
 203{
 204        if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
 205                bb_perror_msg("can't send flush request");
 206                return -1;
 207        }
 208        G_filter.flushp = 0;
 209        return 0;
 210}
 211
 212static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
 213                struct nlmsghdr *n, void *arg UNUSED_PARAM)
 214{
 215        struct ifaddrmsg *ifa = NLMSG_DATA(n);
 216        int len = n->nlmsg_len;
 217        struct rtattr * rta_tb[IFA_MAX+1];
 218        char abuf[256];
 219
 220        if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
 221                return 0;
 222        len -= NLMSG_LENGTH(sizeof(*ifa));
 223        if (len < 0) {
 224                bb_error_msg("wrong nlmsg len %d", len);
 225                return -1;
 226        }
 227
 228        if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
 229                return 0;
 230
 231        memset(rta_tb, 0, sizeof(rta_tb));
 232        parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
 233
 234        if (!rta_tb[IFA_LOCAL])
 235                rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
 236        if (!rta_tb[IFA_ADDRESS])
 237                rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
 238
 239        if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
 240                return 0;
 241        if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
 242                return 0;
 243        if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
 244                return 0;
 245        if (G_filter.label) {
 246                const char *label;
 247                if (rta_tb[IFA_LABEL])
 248                        label = RTA_DATA(rta_tb[IFA_LABEL]);
 249                else
 250                        label = ll_index_to_name(ifa->ifa_index);
 251                if (fnmatch(G_filter.label, label, 0) != 0)
 252                        return 0;
 253        }
 254        if (G_filter.pfx.family) {
 255                if (rta_tb[IFA_LOCAL]) {
 256                        inet_prefix dst;
 257                        memset(&dst, 0, sizeof(dst));
 258                        dst.family = ifa->ifa_family;
 259                        memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
 260                        if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
 261                                return 0;
 262                }
 263        }
 264
 265        if (G_filter.flushb) {
 266                struct nlmsghdr *fn;
 267                if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
 268                        if (flush_update())
 269                                return -1;
 270                }
 271                fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
 272                memcpy(fn, n, n->nlmsg_len);
 273                fn->nlmsg_type = RTM_DELADDR;
 274                fn->nlmsg_flags = NLM_F_REQUEST;
 275                fn->nlmsg_seq = ++G_filter.rth->seq;
 276                G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
 277                G_filter.flushed = 1;
 278                return 0;
 279        }
 280
 281        if (n->nlmsg_type == RTM_DELADDR)
 282                printf("Deleted ");
 283
 284        if (G_filter.oneline)
 285                printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
 286        if (ifa->ifa_family == AF_INET)
 287                printf("    inet ");
 288        else if (ifa->ifa_family == AF_INET6)
 289                printf("    inet6 ");
 290        else
 291                printf("    family %d ", ifa->ifa_family);
 292
 293        if (rta_tb[IFA_LOCAL]) {
 294                fputs(rt_addr_n2a(ifa->ifa_family,
 295                                              RTA_DATA(rta_tb[IFA_LOCAL]),
 296                                              abuf, sizeof(abuf)), stdout);
 297
 298                if (rta_tb[IFA_ADDRESS] == NULL
 299                 || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
 300                ) {
 301                        printf("/%d ", ifa->ifa_prefixlen);
 302                } else {
 303                        printf(" peer %s/%d ",
 304                                rt_addr_n2a(ifa->ifa_family,
 305                                            RTA_DATA(rta_tb[IFA_ADDRESS]),
 306                                            abuf, sizeof(abuf)),
 307                                ifa->ifa_prefixlen);
 308                }
 309        }
 310
 311        if (rta_tb[IFA_BROADCAST]) {
 312                printf("brd %s ",
 313                        rt_addr_n2a(ifa->ifa_family,
 314                                        RTA_DATA(rta_tb[IFA_BROADCAST]),
 315                                        abuf, sizeof(abuf))
 316                );
 317        }
 318        if (rta_tb[IFA_ANYCAST]) {
 319                printf("any %s ",
 320                        rt_addr_n2a(ifa->ifa_family,
 321                                        RTA_DATA(rta_tb[IFA_ANYCAST]),
 322                                        abuf, sizeof(abuf))
 323                );
 324        }
 325        printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
 326        if (ifa->ifa_flags & IFA_F_SECONDARY) {
 327                ifa->ifa_flags &= ~IFA_F_SECONDARY;
 328                printf("secondary ");
 329        }
 330        if (ifa->ifa_flags & IFA_F_TENTATIVE) {
 331                ifa->ifa_flags &= ~IFA_F_TENTATIVE;
 332                printf("tentative ");
 333        }
 334        if (ifa->ifa_flags & IFA_F_DEPRECATED) {
 335                ifa->ifa_flags &= ~IFA_F_DEPRECATED;
 336                printf("deprecated ");
 337        }
 338        if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
 339                printf("dynamic ");
 340        } else
 341                ifa->ifa_flags &= ~IFA_F_PERMANENT;
 342        if (ifa->ifa_flags)
 343                printf("flags %02x ", ifa->ifa_flags);
 344        if (rta_tb[IFA_LABEL])
 345                fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
 346        if (rta_tb[IFA_CACHEINFO]) {
 347                struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
 348                char buf[128];
 349                bb_putchar(_SL_);
 350                if (ci->ifa_valid == 0xFFFFFFFFU)
 351                        sprintf(buf, "valid_lft forever");
 352                else
 353                        sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
 354                if (ci->ifa_prefered == 0xFFFFFFFFU)
 355                        sprintf(buf+strlen(buf), " preferred_lft forever");
 356                else
 357                        sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
 358                printf("       %s", buf);
 359        }
 360        bb_putchar('\n');
 361        /*fflush_all();*/
 362        return 0;
 363}
 364
 365
 366struct nlmsg_list {
 367        struct nlmsg_list *next;
 368        struct nlmsghdr   h;
 369};
 370
 371static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
 372{
 373        for (; ainfo; ainfo = ainfo->next) {
 374                struct nlmsghdr *n = &ainfo->h;
 375                struct ifaddrmsg *ifa = NLMSG_DATA(n);
 376
 377                if (n->nlmsg_type != RTM_NEWADDR)
 378                        continue;
 379                if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
 380                        return -1;
 381                if (ifa->ifa_index != ifindex
 382                 || (G_filter.family && G_filter.family != ifa->ifa_family)
 383                ) {
 384                        continue;
 385                }
 386                print_addrinfo(NULL, n, NULL);
 387        }
 388        return 0;
 389}
 390
 391
 392static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 393{
 394        struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
 395        struct nlmsg_list *h;
 396        struct nlmsg_list **lp;
 397
 398        h = xzalloc(n->nlmsg_len + sizeof(void*));
 399
 400        memcpy(&h->h, n, n->nlmsg_len);
 401        /*h->next = NULL; - xzalloc did it */
 402
 403        for (lp = linfo; *lp; lp = &(*lp)->next)
 404                continue;
 405        *lp = h;
 406
 407        ll_remember_index(who, n, NULL);
 408        return 0;
 409}
 410
 411static void ipaddr_reset_filter(int _oneline)
 412{
 413        memset(&G_filter, 0, sizeof(G_filter));
 414        G_filter.oneline = _oneline;
 415}
 416
 417/* Return value becomes exitcode. It's okay to not return at all */
 418int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
 419{
 420        static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
 421
 422        struct nlmsg_list *linfo = NULL;
 423        struct nlmsg_list *ainfo = NULL;
 424        struct nlmsg_list *l;
 425        struct rtnl_handle rth;
 426        char *filter_dev = NULL;
 427        int no_link = 0;
 428
 429        ipaddr_reset_filter(oneline);
 430        G_filter.showqueue = 1;
 431
 432        if (G_filter.family == AF_UNSPEC)
 433                G_filter.family = preferred_family;
 434
 435        if (flush) {
 436                if (!*argv) {
 437                        bb_error_msg_and_die(bb_msg_requires_arg, "flush");
 438                }
 439                if (G_filter.family == AF_PACKET) {
 440                        bb_error_msg_and_die("can't flush link addresses");
 441                }
 442        }
 443
 444        while (*argv) {
 445                const smalluint key = index_in_strings(option, *argv);
 446                if (key == 0) { /* to */
 447                        NEXT_ARG();
 448                        get_prefix(&G_filter.pfx, *argv, G_filter.family);
 449                        if (G_filter.family == AF_UNSPEC) {
 450                                G_filter.family = G_filter.pfx.family;
 451                        }
 452                } else if (key == 1) { /* scope */
 453                        uint32_t scope = 0;
 454                        NEXT_ARG();
 455                        G_filter.scopemask = -1;
 456                        if (rtnl_rtscope_a2n(&scope, *argv)) {
 457                                if (strcmp(*argv, "all") != 0) {
 458                                        invarg(*argv, "scope");
 459                                }
 460                                scope = RT_SCOPE_NOWHERE;
 461                                G_filter.scopemask = 0;
 462                        }
 463                        G_filter.scope = scope;
 464                } else if (key == 2) { /* up */
 465                        G_filter.up = 1;
 466                } else if (key == 3) { /* label */
 467                        NEXT_ARG();
 468                        G_filter.label = *argv;
 469                } else {
 470                        if (key == 4) /* dev */
 471                                NEXT_ARG();
 472                        if (filter_dev)
 473                                duparg2("dev", *argv);
 474                        filter_dev = *argv;
 475                }
 476                argv++;
 477        }
 478
 479        xrtnl_open(&rth);
 480
 481        xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
 482        xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
 483
 484        if (filter_dev) {
 485                G_filter.ifindex = xll_name_to_index(filter_dev);
 486        }
 487
 488        if (flush) {
 489                char flushb[4096-512];
 490
 491                G_filter.flushb = flushb;
 492                G_filter.flushp = 0;
 493                G_filter.flushe = sizeof(flushb);
 494                G_filter.rth = &rth;
 495
 496                for (;;) {
 497                        xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
 498                        G_filter.flushed = 0;
 499                        xrtnl_dump_filter(&rth, print_addrinfo, NULL);
 500                        if (G_filter.flushed == 0) {
 501                                return 0;
 502                        }
 503                        if (flush_update() < 0) {
 504                                return 1;
 505                        }
 506                }
 507        }
 508
 509        if (G_filter.family != AF_PACKET) {
 510                xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
 511                xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
 512        }
 513
 514
 515        if (G_filter.family && G_filter.family != AF_PACKET) {
 516                struct nlmsg_list **lp;
 517                lp = &linfo;
 518
 519                if (G_filter.oneline)
 520                        no_link = 1;
 521
 522                while ((l = *lp) != NULL) {
 523                        int ok = 0;
 524                        struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
 525                        struct nlmsg_list *a;
 526
 527                        for (a = ainfo; a; a = a->next) {
 528                                struct nlmsghdr *n = &a->h;
 529                                struct ifaddrmsg *ifa = NLMSG_DATA(n);
 530
 531                                if (ifa->ifa_index != ifi->ifi_index
 532                                 || (G_filter.family && G_filter.family != ifa->ifa_family)
 533                                ) {
 534                                        continue;
 535                                }
 536                                if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
 537                                        continue;
 538                                if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
 539                                        continue;
 540                                if (G_filter.pfx.family || G_filter.label) {
 541                                        struct rtattr *tb[IFA_MAX+1];
 542                                        memset(tb, 0, sizeof(tb));
 543                                        parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
 544                                        if (!tb[IFA_LOCAL])
 545                                                tb[IFA_LOCAL] = tb[IFA_ADDRESS];
 546
 547                                        if (G_filter.pfx.family && tb[IFA_LOCAL]) {
 548                                                inet_prefix dst;
 549                                                memset(&dst, 0, sizeof(dst));
 550                                                dst.family = ifa->ifa_family;
 551                                                memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
 552                                                if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
 553                                                        continue;
 554                                        }
 555                                        if (G_filter.label) {
 556                                                const char *label;
 557                                                if (tb[IFA_LABEL])
 558                                                        label = RTA_DATA(tb[IFA_LABEL]);
 559                                                else
 560                                                        label = ll_index_to_name(ifa->ifa_index);
 561                                                if (fnmatch(G_filter.label, label, 0) != 0)
 562                                                        continue;
 563                                        }
 564                                }
 565
 566                                ok = 1;
 567                                break;
 568                        }
 569                        if (!ok)
 570                                *lp = l->next;
 571                        else
 572                                lp = &l->next;
 573                }
 574        }
 575
 576        for (l = linfo; l; l = l->next) {
 577                if (no_link || print_linkinfo(&l->h) == 0) {
 578                        struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
 579                        if (G_filter.family != AF_PACKET)
 580                                print_selected_addrinfo(ifi->ifi_index, ainfo);
 581                }
 582        }
 583
 584        return 0;
 585}
 586
 587static int default_scope(inet_prefix *lcl)
 588{
 589        if (lcl->family == AF_INET) {
 590                if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
 591                        return RT_SCOPE_HOST;
 592        }
 593        return 0;
 594}
 595
 596/* Return value becomes exitcode. It's okay to not return at all */
 597static int ipaddr_modify(int cmd, int flags, char **argv)
 598{
 599        static const char option[] ALIGN1 =
 600                "peer\0""remote\0""broadcast\0""brd\0"
 601                "anycast\0""scope\0""dev\0""label\0""local\0";
 602        struct rtnl_handle rth;
 603        struct {
 604                struct nlmsghdr  n;
 605                struct ifaddrmsg ifa;
 606                char             buf[256];
 607        } req;
 608        char *d = NULL;
 609        char *l = NULL;
 610        inet_prefix lcl;
 611        inet_prefix peer;
 612        int local_len = 0;
 613        int peer_len = 0;
 614        int brd_len = 0;
 615        int any_len = 0;
 616        bool scoped = 0;
 617
 618        memset(&req, 0, sizeof(req));
 619
 620        req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
 621        req.n.nlmsg_flags = NLM_F_REQUEST | flags;
 622        req.n.nlmsg_type = cmd;
 623        req.ifa.ifa_family = preferred_family;
 624
 625        while (*argv) {
 626                unsigned arg = index_in_strings(option, *argv);
 627                /* if search fails, "local" is assumed */
 628                if ((int)arg >= 0)
 629                        NEXT_ARG();
 630
 631                if (arg <= 1) { /* peer, remote */
 632                        if (peer_len) {
 633                                duparg("peer", *argv);
 634                        }
 635                        get_prefix(&peer, *argv, req.ifa.ifa_family);
 636                        peer_len = peer.bytelen;
 637                        if (req.ifa.ifa_family == AF_UNSPEC) {
 638                                req.ifa.ifa_family = peer.family;
 639                        }
 640                        addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
 641                        req.ifa.ifa_prefixlen = peer.bitlen;
 642                } else if (arg <= 3) { /* broadcast, brd */
 643                        inet_prefix addr;
 644                        if (brd_len) {
 645                                duparg("broadcast", *argv);
 646                        }
 647                        if (LONE_CHAR(*argv, '+')) {
 648                                brd_len = -1;
 649                        } else if (LONE_DASH(*argv)) {
 650                                brd_len = -2;
 651                        } else {
 652                                get_addr(&addr, *argv, req.ifa.ifa_family);
 653                                if (req.ifa.ifa_family == AF_UNSPEC)
 654                                        req.ifa.ifa_family = addr.family;
 655                                addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
 656                                brd_len = addr.bytelen;
 657                        }
 658                } else if (arg == 4) { /* anycast */
 659                        inet_prefix addr;
 660                        if (any_len) {
 661                                duparg("anycast", *argv);
 662                        }
 663                        get_addr(&addr, *argv, req.ifa.ifa_family);
 664                        if (req.ifa.ifa_family == AF_UNSPEC) {
 665                                req.ifa.ifa_family = addr.family;
 666                        }
 667                        addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
 668                        any_len = addr.bytelen;
 669                } else if (arg == 5) { /* scope */
 670                        uint32_t scope = 0;
 671                        if (rtnl_rtscope_a2n(&scope, *argv)) {
 672                                invarg(*argv, "scope");
 673                        }
 674                        req.ifa.ifa_scope = scope;
 675                        scoped = 1;
 676                } else if (arg == 6) { /* dev */
 677                        d = *argv;
 678                } else if (arg == 7) { /* label */
 679                        l = *argv;
 680                        addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
 681                } else {
 682                        /* local (specified or assumed) */
 683                        if (local_len) {
 684                                duparg2("local", *argv);
 685                        }
 686                        get_prefix(&lcl, *argv, req.ifa.ifa_family);
 687                        if (req.ifa.ifa_family == AF_UNSPEC) {
 688                                req.ifa.ifa_family = lcl.family;
 689                        }
 690                        addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
 691                        local_len = lcl.bytelen;
 692                }
 693                argv++;
 694        }
 695
 696        if (!d) {
 697                /* There was no "dev IFACE", but we need that */
 698                bb_error_msg_and_die("need \"dev IFACE\"");
 699        }
 700        if (l && !is_prefixed_with(l, d)) {
 701                bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
 702        }
 703
 704        if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
 705                peer = lcl;
 706                addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
 707        }
 708        if (req.ifa.ifa_prefixlen == 0)
 709                req.ifa.ifa_prefixlen = lcl.bitlen;
 710
 711        if (brd_len < 0 && cmd != RTM_DELADDR) {
 712                inet_prefix brd;
 713                int i;
 714                if (req.ifa.ifa_family != AF_INET) {
 715                        bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
 716                }
 717                brd = peer;
 718                if (brd.bitlen <= 30) {
 719                        for (i = 31; i >= brd.bitlen; i--) {
 720                                if (brd_len == -1)
 721                                        brd.data[0] |= htonl(1<<(31-i));
 722                                else
 723                                        brd.data[0] &= ~htonl(1<<(31-i));
 724                        }
 725                        addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
 726                        brd_len = brd.bytelen;
 727                }
 728        }
 729        if (!scoped && cmd != RTM_DELADDR)
 730                req.ifa.ifa_scope = default_scope(&lcl);
 731
 732        xrtnl_open(&rth);
 733
 734        ll_init_map(&rth);
 735
 736        req.ifa.ifa_index = xll_name_to_index(d);
 737
 738        if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
 739                return 2;
 740
 741        return 0;
 742}
 743
 744/* Return value becomes exitcode. It's okay to not return at all */
 745int FAST_FUNC do_ipaddr(char **argv)
 746{
 747        static const char commands[] ALIGN1 =
 748                /* 0    1         2      3          4         5       6       7      8 */
 749                "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
 750        int cmd = 2;
 751        if (*argv) {
 752                cmd = index_in_substrings(commands, *argv);
 753                if (cmd < 0)
 754                        invarg(*argv, applet_name);
 755                argv++;
 756                if (cmd <= 4) {
 757                        return ipaddr_modify(
 758                                /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
 759                                /*flags:*/
 760                                        cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
 761                                        cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
 762                                        cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
 763                                        0 /* delete */
 764                        , argv);
 765                }
 766        }
 767        return ipaddr_list_or_flush(argv, cmd == 8);
 768}
 769