ifconfig.c (444b9aa78c6083d249b70669480d385bd58fd7fd) ifconfig.c (d1cd0344f7b7d81beda04c3cb8cfee99351c3eb8)
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 597 unchanged lines hidden (view full) ---

606 (args.downonly && (flags & IFF_UP) != 0) ||
607 (args.uponly && (flags & IFF_UP) == 0)))
608 ifconfig(argc, argv, 0, afp);
609 }
610 goto done;
611 }
612
613 args.afp = afp;
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 597 unchanged lines hidden (view full) ---

606 (args.downonly && (flags & IFF_UP) != 0) ||
607 (args.uponly && (flags & IFF_UP) == 0)))
608 ifconfig(argc, argv, 0, afp);
609 }
610 goto done;
611 }
612
613 args.afp = afp;
614 args.allfamilies = afp == NULL;
614 args.argc = argc;
615 args.argv = argv;
616
617 list_interfaces(&args);
618
619done:
620 freeformat();
621 ifconfig_close(lifh);
622 exit(exit_code);
623}
624
615 args.argc = argc;
616 args.argv = argv;
617
618 list_interfaces(&args);
619
620done:
621 freeformat();
622 ifconfig_close(lifh);
623 exit(exit_code);
624}
625
626bool
627match_ether(const struct sockaddr_dl *sdl)
628{
629 switch (sdl->sdl_type) {
630 case IFT_ETHER:
631 case IFT_L2VLAN:
632 case IFT_BRIDGE:
633 if (sdl->sdl_alen == ETHER_ADDR_LEN)
634 return (true);
635 default:
636 return (false);
637 }
638}
639
640static bool
641match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sdl)
642{
643 if (afp == NULL)
644 return (true);
645 /* special case for "ether" address family */
646 if (!strcmp(afp->af_name, "ether")) {
647 if (sdl == NULL && !match_ether(sdl))
648 return (false);
649 return (true);
650 }
651 return (afp->af_af == sa_family);
652}
653
654static bool
655match_if_flags(struct ifconfig_args *args, int if_flags)
656{
657 if ((if_flags & IFF_CANTCONFIG) != 0)
658 return (false);
659 if (args->downonly && (if_flags & IFF_UP) != 0)
660 return (false);
661 if (args->uponly && (if_flags & IFF_UP) == 0)
662 return (false);
663 return (true);
664}
665
625static void
626list_interfaces(struct ifconfig_args *args)
627{
628 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
629 struct ifaddrs *ifap, *sifap, *ifa;
630 struct ifa_order_elt *cur, *tmp;
631 char *namecp = NULL;
632 int ifindex;

--- 34 unchanged lines hidden (view full) ---

667 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
668 if (iflen >= sizeof(name)) {
669 warnx("%s: interface name too long, skipping",
670 ifa->ifa_name);
671 continue;
672 }
673 cp = ifa->ifa_name;
674
666static void
667list_interfaces(struct ifconfig_args *args)
668{
669 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
670 struct ifaddrs *ifap, *sifap, *ifa;
671 struct ifa_order_elt *cur, *tmp;
672 char *namecp = NULL;
673 int ifindex;

--- 34 unchanged lines hidden (view full) ---

708 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
709 if (iflen >= sizeof(name)) {
710 warnx("%s: interface name too long, skipping",
711 ifa->ifa_name);
712 continue;
713 }
714 cp = ifa->ifa_name;
715
675 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
716 if (!match_if_flags(args, ifa->ifa_flags))
676 continue;
717 continue;
677 if (args->downonly && (ifa->ifa_flags & IFF_UP) != 0)
678 continue;
679 if (args->uponly && (ifa->ifa_flags & IFF_UP) == 0)
680 continue;
681 if (!group_member(ifa->ifa_name, args->matchgroup, args->nogroup))
682 continue;
683 /*
684 * Are we just listing the interfaces?
685 */
686 if (args->namesonly) {
687 if (namecp == cp)
688 continue;
718 if (!group_member(ifa->ifa_name, args->matchgroup, args->nogroup))
719 continue;
720 /*
721 * Are we just listing the interfaces?
722 */
723 if (args->namesonly) {
724 if (namecp == cp)
725 continue;
689 if (args->afp != NULL) {
690 /* special case for "ether" address family */
691 if (!strcmp(args->afp->af_name, "ether")) {
692 if (sdl == NULL ||
693 (sdl->sdl_type != IFT_ETHER &&
694 sdl->sdl_type != IFT_L2VLAN &&
695 sdl->sdl_type != IFT_BRIDGE) ||
696 sdl->sdl_alen != ETHER_ADDR_LEN)
697 continue;
698 } else {
699 if (ifa->ifa_addr->sa_family
700 != args->afp->af_af)
701 continue;
702 }
703 }
726 if (!match_afp(args->afp, ifa->ifa_addr->sa_family, sdl))
727 continue;
704 namecp = cp;
705 ifindex++;
706 if (ifindex > 1)
707 printf(" ");
708 fputs(name, stdout);
709 continue;
710 }
711 ifindex++;

--- 715 unchanged lines hidden (view full) ---

1427
1428#define IFCAPBITS \
1429"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1430"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1431"\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1432"\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1433"\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT"
1434
728 namecp = cp;
729 ifindex++;
730 if (ifindex > 1)
731 printf(" ");
732 fputs(name, stdout);
733 continue;
734 }
735 ifindex++;

--- 715 unchanged lines hidden (view full) ---

1451
1452#define IFCAPBITS \
1453"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1454"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1455"\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1456"\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1457"\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT"
1458
1435/*
1436 * Print the status of the interface. If an address family was
1437 * specified, show only it; otherwise, show them all.
1438 */
1439static void
1459static void
1440status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
1441 struct ifaddrs *ifa)
1460print_ifcap_nv(struct ifconfig_args *args, int s)
1442{
1461{
1443 struct ifaddrs *ift;
1444 struct ifstat ifs;
1445 nvlist_t *nvcap;
1446 const char *nvname;
1447 void *buf, *cookie;
1462 nvlist_t *nvcap;
1463 const char *nvname;
1464 void *buf, *cookie;
1448 int allfamilies, s, type;
1449 bool first, val;
1465 bool first, val;
1466 int type;
1450
1467
1451 if (args->afp == NULL) {
1452 allfamilies = 1;
1453 ifr.ifr_addr.sa_family = AF_LOCAL;
1454 } else {
1455 allfamilies = 0;
1456 ifr.ifr_addr.sa_family =
1457 args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
1468 buf = malloc(IFR_CAP_NV_MAXBUFSIZE);
1469 if (buf == NULL)
1470 Perror("malloc");
1471 ifr.ifr_cap_nv.buffer = buf;
1472 ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE;
1473 if (ioctl(s, SIOCGIFCAPNV, (caddr_t)&ifr) != 0)
1474 Perror("ioctl (SIOCGIFCAPNV)");
1475 nvcap = nvlist_unpack(ifr.ifr_cap_nv.buffer,
1476 ifr.ifr_cap_nv.length, 0);
1477 if (nvcap == NULL)
1478 Perror("nvlist_unpack");
1479 printf("\toptions");
1480 cookie = NULL;
1481 for (first = true;; first = false) {
1482 nvname = nvlist_next(nvcap, &type, &cookie);
1483 if (nvname == NULL) {
1484 printf("\n");
1485 break;
1486 }
1487 if (type == NV_TYPE_BOOL) {
1488 val = nvlist_get_bool(nvcap, nvname);
1489 if (val) {
1490 printf("%c%s",
1491 first ? ' ' : ',', nvname);
1492 }
1493 }
1458 }
1494 }
1459 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1495 if (args->supmedia) {
1496 printf("\tcapabilities");
1497 cookie = NULL;
1498 for (first = true;; first = false) {
1499 nvname = nvlist_next(nvcap, &type,
1500 &cookie);
1501 if (nvname == NULL) {
1502 printf("\n");
1503 break;
1504 }
1505 if (type == NV_TYPE_BOOL)
1506 printf("%c%s", first ? ' ' :
1507 ',', nvname);
1508 }
1509 }
1510 nvlist_destroy(nvcap);
1511 free(buf);
1460
1512
1461 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1462 if (s < 0)
1463 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1513 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0)
1514 Perror("ioctl (SIOCGIFCAP)");
1515}
1464
1516
1465 printf("%s: ", name);
1466 printb("flags", ifa->ifa_flags, IFFBITS);
1517static void
1518print_ifcap(struct ifconfig_args *args, int s)
1519{
1520 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0)
1521 return;
1522
1523 if ((ifr.ifr_curcap & IFCAP_NV) != 0)
1524 print_ifcap_nv(args, s);
1525 else {
1526 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1527 putchar('\n');
1528 if (args->supmedia && ifr.ifr_reqcap != 0) {
1529 printb("\tcapabilities", ifr.ifr_reqcap,
1530 IFCAPBITS);
1531 putchar('\n');
1532 }
1533 }
1534}
1535
1536static void
1537print_ifstatus(int s)
1538{
1539 struct ifstat ifs;
1540
1541 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1542 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1543 printf("%s", ifs.ascii);
1544}
1545
1546static void
1547print_metric(int s)
1548{
1467 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1468 printf(" metric %d", ifr.ifr_metric);
1549 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1550 printf(" metric %d", ifr.ifr_metric);
1551}
1552
1553static void
1554print_mtu(int s)
1555{
1469 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1470 printf(" mtu %d", ifr.ifr_mtu);
1556 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1557 printf(" mtu %d", ifr.ifr_mtu);
1471 putchar('\n');
1558}
1472
1559
1560static void
1561print_description(int s)
1562{
1473 for (;;) {
1474 if ((descr = reallocf(descr, descrlen)) != NULL) {
1475 ifr.ifr_buffer.buffer = descr;
1476 ifr.ifr_buffer.length = descrlen;
1477 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1478 if (ifr.ifr_buffer.buffer == descr) {
1479 if (strlen(descr) > 0)
1480 printf("\tdescription: %s\n",
1481 descr);
1482 } else if (ifr.ifr_buffer.length > descrlen) {
1483 descrlen = ifr.ifr_buffer.length;
1484 continue;
1485 }
1486 }
1487 } else
1488 warn("unable to allocate memory for interface"
1489 "description");
1490 break;
1491 }
1563 for (;;) {
1564 if ((descr = reallocf(descr, descrlen)) != NULL) {
1565 ifr.ifr_buffer.buffer = descr;
1566 ifr.ifr_buffer.length = descrlen;
1567 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1568 if (ifr.ifr_buffer.buffer == descr) {
1569 if (strlen(descr) > 0)
1570 printf("\tdescription: %s\n",
1571 descr);
1572 } else if (ifr.ifr_buffer.length > descrlen) {
1573 descrlen = ifr.ifr_buffer.length;
1574 continue;
1575 }
1576 }
1577 } else
1578 warn("unable to allocate memory for interface"
1579 "description");
1580 break;
1581 }
1582}
1492
1583
1493 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1494 if ((ifr.ifr_curcap & IFCAP_NV) != 0) {
1495 buf = malloc(IFR_CAP_NV_MAXBUFSIZE);
1496 if (buf == NULL)
1497 Perror("malloc");
1498 ifr.ifr_cap_nv.buffer = buf;
1499 ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE;
1500 if (ioctl(s, SIOCGIFCAPNV, (caddr_t)&ifr) != 0)
1501 Perror("ioctl (SIOCGIFCAPNV)");
1502 nvcap = nvlist_unpack(ifr.ifr_cap_nv.buffer,
1503 ifr.ifr_cap_nv.length, 0);
1504 if (nvcap == NULL)
1505 Perror("nvlist_unpack");
1506 printf("\toptions");
1507 cookie = NULL;
1508 for (first = true;; first = false) {
1509 nvname = nvlist_next(nvcap, &type, &cookie);
1510 if (nvname == NULL) {
1511 printf("\n");
1512 break;
1513 }
1514 if (type == NV_TYPE_BOOL) {
1515 val = nvlist_get_bool(nvcap, nvname);
1516 if (val) {
1517 printf("%c%s",
1518 first ? ' ' : ',', nvname);
1519 }
1520 }
1521 }
1522 if (args->supmedia) {
1523 printf("\tcapabilities");
1524 cookie = NULL;
1525 for (first = true;; first = false) {
1526 nvname = nvlist_next(nvcap, &type,
1527 &cookie);
1528 if (nvname == NULL) {
1529 printf("\n");
1530 break;
1531 }
1532 if (type == NV_TYPE_BOOL)
1533 printf("%c%s", first ? ' ' :
1534 ',', nvname);
1535 }
1536 }
1537 nvlist_destroy(nvcap);
1538 free(buf);
1584/*
1585 * Print the status of the interface. If an address family was
1586 * specified, show only it; otherwise, show them all.
1587 */
1588static void
1589status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
1590 struct ifaddrs *ifa)
1591{
1592 struct ifaddrs *ift;
1593 int s;
1594 bool allfamilies = args->afp == NULL;
1539
1595
1540 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0)
1541 Perror("ioctl (SIOCGIFCAP)");
1542 } else if (ifr.ifr_curcap != 0) {
1543 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1544 putchar('\n');
1545 if (args->supmedia && ifr.ifr_reqcap != 0) {
1546 printb("\tcapabilities", ifr.ifr_reqcap,
1547 IFCAPBITS);
1548 putchar('\n');
1549 }
1550 }
1551 }
1596 if (args->afp == NULL)
1597 ifr.ifr_addr.sa_family = AF_LOCAL;
1598 else
1599 ifr.ifr_addr.sa_family =
1600 args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
1601 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1552
1602
1603 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1604 if (s < 0)
1605 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1606
1607 printf("%s: ", name);
1608 printb("flags", ifa->ifa_flags, IFFBITS);
1609 print_metric(s);
1610 print_mtu(s);
1611 putchar('\n');
1612
1613 print_description(s);
1614
1615 print_ifcap(args, s);
1616
1553 tunnel_status(s);
1554
1555 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1556 if (ift->ifa_addr == NULL)
1557 continue;
1558 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1559 continue;
1560 if (allfamilies) {

--- 21 unchanged lines hidden (view full) ---

1582 }
1583 }
1584#endif
1585 if (allfamilies)
1586 af_other_status(s);
1587 else if (args->afp->af_other_status != NULL)
1588 args->afp->af_other_status(s);
1589
1617 tunnel_status(s);
1618
1619 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1620 if (ift->ifa_addr == NULL)
1621 continue;
1622 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1623 continue;
1624 if (allfamilies) {

--- 21 unchanged lines hidden (view full) ---

1646 }
1647 }
1648#endif
1649 if (allfamilies)
1650 af_other_status(s);
1651 else if (args->afp->af_other_status != NULL)
1652 args->afp->af_other_status(s);
1653
1590 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1591 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1592 printf("%s", ifs.ascii);
1593
1654 print_ifstatus(s);
1594 if (args->verbose > 0)
1595 sfp_status(s, &ifr, args->verbose);
1596
1597 close(s);
1598 return;
1599}
1600
1601static void

--- 249 unchanged lines hidden ---
1655 if (args->verbose > 0)
1656 sfp_status(s, &ifr, args->verbose);
1657
1658 close(s);
1659 return;
1660}
1661
1662static void

--- 249 unchanged lines hidden ---