aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

ip.c (10444B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #include <arpa/inet.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 
      8 #include "net.h"
      9 #include "util.h"
     10 
     11 static void
     12 usage(void)
     13 {
     14   eprintf("usage: %s [addr | link | route] [args...]\n", argv0);
     15 }
     16 
     17 /* like ip(8) and busybox, accept any unambiguous prefix of an object
     18  * name (ip a, ip li, ip ro), not only the full word */
     19 static int
     20 is_object(const char *obj, const char *full)
     21 {
     22   size_t len = strlen(obj);
     23   return len > 0 && len <= strlen(full) && strncmp(obj, full, len) == 0;
     24 }
     25 
     26 static void
     27 parse_mac(const char *str, unsigned char mac[6])
     28 {
     29   unsigned int val;
     30   int          i;
     31   const char  *p = str;
     32 
     33   for (i = 0; i < 6; i++) {
     34     if (i > 0) {
     35       if (*p != ':')
     36         eprintf("invalid mac address: %s\n", str);
     37       p++;
     38     }
     39     if (sscanf(p, "%2x", &val) != 1)
     40       eprintf("invalid mac address: %s\n", str);
     41     mac[i] = val;
     42     p += 2;
     43   }
     44 }
     45 
     46 static void
     47 print_link_iface(const struct NetInterface *iface)
     48 {
     49   printf("%d: %s: mtu %d ", iface->metric, iface->name, iface->mtu);
     50   if (iface->flags & NET_IF_UP)
     51     printf("UP ");
     52   if (iface->flags & NET_IF_LOOPBACK)
     53     printf("LOOPBACK ");
     54   if (iface->flags & NET_IF_POINTOPOINT)
     55     printf("POINTOPOINT ");
     56   if (iface->flags & NET_IF_RUNNING)
     57     printf("RUNNING ");
     58   if (iface->flags & NET_IF_VIRTUAL)
     59     printf("VIRTUAL ");
     60   if (iface->flags & NET_IF_WIRELESS)
     61     printf("WIRELESS ");
     62   printf("\n");
     63   if (iface->has_mac) {
     64     printf(
     65         "    link/ether %02x:%02x:%02x:%02x:%02x:%02x\n",
     66         iface->mac[0],
     67         iface->mac[1],
     68         iface->mac[2],
     69         iface->mac[3],
     70         iface->mac[4],
     71         iface->mac[5]
     72     );
     73   }
     74 }
     75 
     76 static void
     77 print_addr_iface(const struct NetInterface *iface)
     78 {
     79   char ipv6_str[INET6_ADDRSTRLEN];
     80 
     81   print_link_iface(iface);
     82   if (iface->has_ipv4) {
     83     printf(
     84         "    inet %s netmask %s\n",
     85         inet_ntoa(iface->ipv4_addr.sin_addr),
     86         inet_ntoa(iface->ipv4_mask.sin_addr)
     87     );
     88   }
     89   if (iface->has_ipv6) {
     90     if (inet_ntop(AF_INET6, &iface->ipv6_addr.sin6_addr, ipv6_str, sizeof(ipv6_str))) {
     91       printf("    inet6 %s\n", ipv6_str);
     92     }
     93   }
     94 }
     95 
     96 static int
     97 do_addr(int argc, char *argv[])
     98 {
     99   struct NetInterface *ifaces = NULL;
    100   char                *cmd, *ip, *dev, *slash;
    101   int                  count  = 0;
    102   int                  prefix = -1;
    103   int                  i, r;
    104 
    105   if (argc == 0)
    106     cmd = "show";
    107   else
    108     cmd = argv[0];
    109 
    110   if (strcmp(cmd, "show") == 0 || strcmp(cmd, "list") == 0) {
    111     dev = NULL;
    112     if (argc > 1) {
    113       if (strcmp(argv[1], "dev") == 0 && argc > 2)
    114         dev = argv[2];
    115       else
    116         dev = argv[1];
    117     }
    118     if (net_get_interfaces(&ifaces, &count) < 0)
    119       eprintf("net_get_interfaces:");
    120     for (i = 0; i < count; i++) {
    121       if (!dev || strcmp(ifaces[i].name, dev) == 0)
    122         print_addr_iface(&ifaces[i]);
    123     }
    124     free(ifaces);
    125     return 0;
    126   }
    127 
    128   if (strcmp(cmd, "add") == 0 || strcmp(cmd, "del") == 0) {
    129     if (argc < 4)
    130       eprintf(
    131           "usage: ip addr %s <ip>/<prefix> dev "
    132           "<interface>\n",
    133           cmd
    134       );
    135     ip  = argv[1];
    136     dev = NULL;
    137     for (i = 2; i < argc; i++) {
    138       if (strcmp(argv[i], "dev") == 0 && i + 1 < argc) {
    139         dev = argv[i + 1];
    140         break;
    141       }
    142     }
    143     if (!dev)
    144       eprintf("missing dev parameter\n");
    145 
    146     slash = strchr(ip, '/');
    147     if (slash) {
    148       *slash = '\0';
    149       prefix = atoi(slash + 1);
    150     }
    151 
    152     if (strcmp(cmd, "add") == 0)
    153       r = net_add_addr(dev, ip, prefix);
    154     else
    155       r = net_del_addr(dev, ip, prefix);
    156 
    157     if (r < 0)
    158       eprintf("net_%s_addr:", cmd);
    159     return 0;
    160   }
    161 
    162 #if FEATURE_IP_ADDR_FLUSH
    163   if (strcmp(cmd, "flush") == 0) {
    164     dev = NULL;
    165     for (i = 1; i < argc; i++) {
    166       if (strcmp(argv[i], "dev") == 0 && i + 1 < argc) {
    167         dev = argv[i + 1];
    168         break;
    169       }
    170     }
    171     if (!dev)
    172       eprintf("missing dev parameter for flush\n");
    173     if (net_flush_addrs(dev) < 0)
    174       eprintf("net_flush_addrs:");
    175     return 0;
    176   }
    177 #endif
    178 
    179   usage();
    180   return 1;
    181 }
    182 
    183 static int
    184 do_link(int argc, char *argv[])
    185 {
    186   struct NetInterface *ifaces = NULL;
    187   char                *cmd, *dev, *mac_str;
    188   int                  count = 0;
    189   int                  i, mtu, qlen, set;
    190   unsigned char        mac[6];
    191 
    192   if (argc == 0)
    193     cmd = "show";
    194   else
    195     cmd = argv[0];
    196 
    197   if (strcmp(cmd, "show") == 0 || strcmp(cmd, "list") == 0) {
    198     dev = NULL;
    199     if (argc > 1) {
    200       if (strcmp(argv[1], "dev") == 0 && argc > 2)
    201         dev = argv[2];
    202       else
    203         dev = argv[1];
    204     }
    205     if (net_get_interfaces(&ifaces, &count) < 0)
    206       eprintf("net_get_interfaces:");
    207     for (i = 0; i < count; i++) {
    208       if (!dev || strcmp(ifaces[i].name, dev) == 0)
    209         print_link_iface(&ifaces[i]);
    210     }
    211     free(ifaces);
    212     return 0;
    213   }
    214 
    215   if (strcmp(cmd, "set") == 0) {
    216     if (argc < 3)
    217       eprintf(
    218           "usage: ip link set <interface> [up | down | "
    219           "mtu <mtu> | address <mac>]\n"
    220       );
    221     dev = argv[1];
    222     for (i = 2; i < argc; i++) {
    223       if (strcmp(argv[i], "up") == 0) {
    224         if (net_set_flags(dev, NET_IF_UP, 1) < 0)
    225           eprintf("net_set_flags up:");
    226       } else if (strcmp(argv[i], "down") == 0) {
    227         if (net_set_flags(dev, NET_IF_UP, 0) < 0)
    228           eprintf("net_set_flags down:");
    229       } else if (strcmp(argv[i], "mtu") == 0 && i + 1 < argc) {
    230         mtu = atoi(argv[i + 1]);
    231         if (net_set_mtu(dev, mtu) < 0)
    232           eprintf("net_set_mtu:");
    233         i++;
    234       } else if (strcmp(argv[i], "address") == 0 && i + 1 < argc) {
    235         mac_str = argv[i + 1];
    236         parse_mac(mac_str, mac);
    237         if (net_set_mac(dev, mac) < 0)
    238           eprintf("net_set_mac:");
    239         i++;
    240 #if FEATURE_IP_LINK_SET
    241       } else if (strcmp(argv[i], "arp") == 0 && i + 1 < argc) {
    242         set = (strcmp(argv[i + 1], "off") == 0);
    243         if (net_set_flags(dev, NET_IF_NOARP, set) < 0)
    244           eprintf("net_set_flags arp:");
    245         i++;
    246       } else if (strcmp(argv[i], "multicast") == 0 && i + 1 < argc) {
    247         set = (strcmp(argv[i + 1], "on") == 0);
    248         if (net_set_flags(dev, NET_IF_MULTICAST, set) < 0)
    249           eprintf("net_set_flags multicast:");
    250         i++;
    251       } else if (strcmp(argv[i], "allmulticast") == 0 && i + 1 < argc) {
    252         set = (strcmp(argv[i + 1], "on") == 0);
    253         if (net_set_flags(dev, NET_IF_ALLMULTI, set) < 0)
    254           eprintf("net_set_flags allmulticast:");
    255         i++;
    256       } else if (strcmp(argv[i], "promisc") == 0 && i + 1 < argc) {
    257         set = (strcmp(argv[i + 1], "on") == 0);
    258         if (net_set_flags(dev, NET_IF_PROMISC, set) < 0)
    259           eprintf("net_set_flags promisc:");
    260         i++;
    261       } else if (strcmp(argv[i], "name") == 0 && i + 1 < argc) {
    262         if (net_set_name(dev, argv[i + 1]) < 0)
    263           eprintf("net_set_name:");
    264         dev = argv[i + 1];
    265         i++;
    266       } else if (strcmp(argv[i], "txqueuelen") == 0 && i + 1 < argc) {
    267         qlen = atoi(argv[i + 1]);
    268         if (net_set_txqueuelen(dev, qlen) < 0)
    269           eprintf("net_set_txqueuelen:");
    270         i++;
    271 #endif
    272       }
    273     }
    274     return 0;
    275   }
    276 
    277   usage();
    278   return 1;
    279 }
    280 
    281 static int
    282 do_route(int argc, char *argv[])
    283 {
    284   char       *cmd, *dst, *slash;
    285   const char *gateway = NULL, *dev = NULL;
    286   int         prefix_len = -1, metric = -1, i;
    287 
    288   if (argc == 0)
    289     cmd = "show";
    290   else
    291     cmd = argv[0];
    292 
    293   if (strcmp(cmd, "show") == 0 || strcmp(cmd, "list") == 0) {
    294     if (net_show_routes() < 0)
    295       eprintf("net_show_routes:");
    296     return 0;
    297   }
    298 
    299 #if FEATURE_IP_ROUTE_ADD_DEL
    300   if (strcmp(cmd, "add") == 0 || strcmp(cmd, "del") == 0) {
    301     if (argc < 2)
    302       eprintf(
    303           "usage: ip route %s <prefix> [via <gateway>] "
    304           "[dev <device>] [metric <metric>]\n",
    305           cmd
    306       );
    307 
    308     dst   = argv[1];
    309     slash = strchr(dst, '/');
    310     if (slash) {
    311       *slash     = '\0';
    312       prefix_len = atoi(slash + 1);
    313     } else if (strcmp(dst, "default") == 0) {
    314       prefix_len = 0;
    315     }
    316     if (prefix_len > 32)
    317       eprintf("invalid prefix: %d\n", prefix_len);
    318 
    319     for (i = 2; i < argc; i++) {
    320       if (strcmp(argv[i], "via") == 0 && i + 1 < argc) {
    321         gateway = argv[i + 1];
    322         i++;
    323       } else if (strcmp(argv[i], "dev") == 0 && i + 1 < argc) {
    324         dev = argv[i + 1];
    325         i++;
    326       } else if (strcmp(argv[i], "metric") == 0 && i + 1 < argc) {
    327         metric = atoi(argv[i + 1]);
    328         i++;
    329       }
    330     }
    331 
    332     if (strcmp(cmd, "add") == 0) {
    333       if (net_add_route(dst, gateway, dev, prefix_len, metric) < 0)
    334         eprintf("net_add_route:");
    335     } else {
    336       if (net_del_route(dst, gateway, dev, prefix_len, metric) < 0)
    337         eprintf("net_del_route:");
    338     }
    339     return 0;
    340   }
    341 #endif
    342 
    343   usage();
    344   return 1;
    345 }
    346 
    347 // ?man ip: show or configure routing and devices
    348 // ?man arguments: [addr | link | route] [args ...]
    349 // ?man configure and view network devices, routing, and tunnels. any
    350 // ?man unambiguous prefix of an object name works too (a, addr, l,
    351 // ?man link, r, route)
    352 // ?man ## COMMANDS
    353 // ?man ### addr [show | list [dev <device>]]
    354 // ?man show interface addresses
    355 // ?man ### addr add <ip>/<prefix> dev <device>
    356 // ?man add address to interface
    357 // ?man ### addr del <ip>/<prefix> dev <device>
    358 // ?man delete address from interface
    359 // ?man ### addr flush dev <device>
    360 // ?man flush all addresses from interface
    361 // ?man ### link [show | list [dev <device>]]
    362 // ?man show interface link states
    363 // ?man ### link set <interface> [up | down | mtu <mtu> | address <mac> | arp on|off | multicast on|off | allmulticast on|off | promisc on|off | name <newname> | txqueuelen <qlen>]
    364 // ?man configure interface settings
    365 // ?man ### route [show | list]
    366 // ?man show routing table
    367 // ?man ### route add <prefix> [via <gateway>] [dev <device>] [metric <metric>]
    368 // ?man add route to routing table
    369 // ?man ### route del <prefix> [via <gateway>] [dev <device>] [metric <metric>]
    370 // ?man delete route from routing table
    371 int
    372 main(int argc, char *argv[])
    373 {
    374   char *obj;
    375 
    376   ARGBEGIN
    377   {
    378     default:
    379       usage();
    380   }
    381   ARGEND
    382 
    383   if (argc == 0)
    384     usage();
    385 
    386   obj = argv[0];
    387 
    388   if (is_object(obj, "address")) {
    389     return do_addr(argc - 1, argv + 1);
    390   } else if (is_object(obj, "link")) {
    391     return do_link(argc - 1, argv + 1);
    392   } else if (is_object(obj, "route")) {
    393     return do_route(argc - 1, argv + 1);
    394   }
    395 
    396   usage();
    397 
    398   if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
    399     return 1;
    400 
    401   return 0;
    402 }