ifconfig.c (ea73ff9752f77b3ed0d20f3d958fda8b51b0cf23) ifconfig.c (bbad5525fabfc8dd75032dc9aaa2cc1d33e62461)
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

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

106int exit_code = 0;
107
108static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */
109
110/* Formatter Strings */
111char *f_inet, *f_inet6, *f_ether, *f_addr;
112
113#ifdef WITHOUT_NETLINK
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

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

106int exit_code = 0;
107
108static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */
109
110/* Formatter Strings */
111char *f_inet, *f_inet6, *f_ether, *f_addr;
112
113#ifdef WITHOUT_NETLINK
114static void list_interfaces_ioctl(struct ifconfig_args *args);
115static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
114static void list_interfaces_ioctl(if_ctx *ctx);
115static void status(if_ctx *ctx, const struct sockaddr_dl *sdl,
116 struct ifaddrs *ifa);
117#endif
118static _Noreturn void usage(void);
119static void Perrorc(const char *cmd, int error);
120
121static int getifflags(const char *ifname, int us, bool err_ok);
122
123static struct afswtch *af_getbyname(const char *name);

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

417static void
418printifnamemaybe(void)
419{
420 if (ifname_to_print[0] != '\0')
421 printf("%s\n", ifname_to_print);
422}
423
424static void
116 struct ifaddrs *ifa);
117#endif
118static _Noreturn void usage(void);
119static void Perrorc(const char *cmd, int error);
120
121static int getifflags(const char *ifname, int us, bool err_ok);
122
123static struct afswtch *af_getbyname(const char *name);

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

417static void
418printifnamemaybe(void)
419{
420 if (ifname_to_print[0] != '\0')
421 printf("%s\n", ifname_to_print);
422}
423
424static void
425list_interfaces(struct ifconfig_args *args)
425list_interfaces(if_ctx *ctx)
426{
427#ifdef WITHOUT_NETLINK
426{
427#ifdef WITHOUT_NETLINK
428 list_interfaces_ioctl(args);
428 list_interfaces_ioctl(ctx);
429#else
429#else
430 list_interfaces_nl(args);
430 list_interfaces_nl(ctx->args);
431#endif
432}
433
434static char *
435args_peek(struct ifconfig_args *args)
436{
437 if (args->argc > 0)
438 return (args->argv[0]);

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

566 usage();
567 }
568
569 args->argc = argc;
570 args->argv = argv;
571}
572
573static int
431#endif
432}
433
434static char *
435args_peek(struct ifconfig_args *args)
436{
437 if (args->argc > 0)
438 return (args->argv[0]);

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

566 usage();
567 }
568
569 args->argc = argc;
570 args->argv = argv;
571}
572
573static int
574ifconfig_wrapper(struct ifconfig_args *args, int iscreate,
575 const struct afswtch *uafp)
574ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp)
576{
575{
577 struct ifconfig_context ctx = {
578 .args = args,
579 .io_s = -1,
580 .ifname = args->ifname,
581 };
582
583#ifdef WITHOUT_NETLINK
576#ifdef WITHOUT_NETLINK
584 return (ifconfig(&ctx, iscreate, uafp));
577 return (ifconfig_ioctl(ctx, iscreate, uafp));
585#else
578#else
586 return (ifconfig_wrapper_nl(&ctx, iscreate, uafp));
579 return (ifconfig_nl(ctx, iscreate, uafp));
587#endif
588}
589
590static bool
591isargcreate(const char *arg)
592{
593 if (arg == NULL)
594 return (false);

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

611 char *envformat;
612 int flags;
613#ifdef JAIL
614 int jid;
615#endif
616 struct ifconfig_args _args = {};
617 struct ifconfig_args *args = &_args;
618
580#endif
581}
582
583static bool
584isargcreate(const char *arg)
585{
586 if (arg == NULL)
587 return (false);

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

604 char *envformat;
605 int flags;
606#ifdef JAIL
607 int jid;
608#endif
609 struct ifconfig_args _args = {};
610 struct ifconfig_args *args = &_args;
611
612 struct ifconfig_context ctx = {
613 .args = args,
614 .io_s = -1,
615 };
616
619 f_inet = f_inet6 = f_ether = f_addr = NULL;
620
621 lifh = ifconfig_open();
622 if (lifh == NULL)
623 err(EXIT_FAILURE, "ifconfig_open");
624
625 envformat = getenv("IFCONFIG_FORMAT");
626 if (envformat != NULL)

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

642 if (jail_attach(jid) != 0)
643 Perror("cannot attach to jail");
644 }
645#endif
646
647 if (!args->all && !args->namesonly) {
648 /* not listing, need an argument */
649 args->ifname = args_pop(args);
617 f_inet = f_inet6 = f_ether = f_addr = NULL;
618
619 lifh = ifconfig_open();
620 if (lifh == NULL)
621 err(EXIT_FAILURE, "ifconfig_open");
622
623 envformat = getenv("IFCONFIG_FORMAT");
624 if (envformat != NULL)

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

640 if (jail_attach(jid) != 0)
641 Perror("cannot attach to jail");
642 }
643#endif
644
645 if (!args->all && !args->namesonly) {
646 /* not listing, need an argument */
647 args->ifname = args_pop(args);
648 ctx.ifname = args->ifname;
650
651 /* check and maybe load support for this interface */
652 ifmaybeload(args, args->ifname);
653
654 char *arg = args_peek(args);
655 if (if_nametoindex(args->ifname) == 0) {
656 /*
657 * NOTE: We must special-case the `create' command
658 * right here as we would otherwise fail when trying
659 * to find the interface.
660 */
661 if (isargcreate(arg)) {
662 if (isnametoolong(args->ifname))
663 errx(1, "%s: cloning name too long",
664 args->ifname);
649
650 /* check and maybe load support for this interface */
651 ifmaybeload(args, args->ifname);
652
653 char *arg = args_peek(args);
654 if (if_nametoindex(args->ifname) == 0) {
655 /*
656 * NOTE: We must special-case the `create' command
657 * right here as we would otherwise fail when trying
658 * to find the interface.
659 */
660 if (isargcreate(arg)) {
661 if (isnametoolong(args->ifname))
662 errx(1, "%s: cloning name too long",
663 args->ifname);
665 ifconfig_wrapper(args, 1, NULL);
664 ifconfig(&ctx, 1, NULL);
666 exit(exit_code);
667 }
668#ifdef JAIL
669 /*
670 * NOTE: We have to special-case the `-vnet' command
671 * right here as we would otherwise fail when trying
672 * to find the interface as it lives in another vnet.
673 */
674 if (arg != NULL && (strcmp(arg, "-vnet") == 0)) {
675 if (isnametoolong(args->ifname))
676 errx(1, "%s: interface name too long",
677 args->ifname);
665 exit(exit_code);
666 }
667#ifdef JAIL
668 /*
669 * NOTE: We have to special-case the `-vnet' command
670 * right here as we would otherwise fail when trying
671 * to find the interface as it lives in another vnet.
672 */
673 if (arg != NULL && (strcmp(arg, "-vnet") == 0)) {
674 if (isnametoolong(args->ifname))
675 errx(1, "%s: interface name too long",
676 args->ifname);
678 ifconfig_wrapper(args, 0, NULL);
677 ifconfig(&ctx, 0, NULL);
679 exit(exit_code);
680 }
681#endif
682 errx(1, "interface %s does not exist", args->ifname);
683 } else {
684 /*
685 * Do not allow use `create` command as hostname if
686 * address family is not specified.

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

709 if ((args->argc > 0) && (args->ifname != NULL)) {
710 if (isnametoolong(args->ifname))
711 warnx("%s: interface name too long, skipping", args->ifname);
712 else {
713 flags = getifflags(args->ifname, -1, false);
714 if (!(((flags & IFF_CANTCONFIG) != 0) ||
715 (args->downonly && (flags & IFF_UP) != 0) ||
716 (args->uponly && (flags & IFF_UP) == 0)))
678 exit(exit_code);
679 }
680#endif
681 errx(1, "interface %s does not exist", args->ifname);
682 } else {
683 /*
684 * Do not allow use `create` command as hostname if
685 * address family is not specified.

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

708 if ((args->argc > 0) && (args->ifname != NULL)) {
709 if (isnametoolong(args->ifname))
710 warnx("%s: interface name too long, skipping", args->ifname);
711 else {
712 flags = getifflags(args->ifname, -1, false);
713 if (!(((flags & IFF_CANTCONFIG) != 0) ||
714 (args->downonly && (flags & IFF_UP) != 0) ||
715 (args->uponly && (flags & IFF_UP) == 0)))
717 ifconfig_wrapper(args, 0, args->afp);
716 ifconfig(&ctx, 0, args->afp);
718 }
719 goto done;
720 }
721
722 args->allfamilies = args->afp == NULL;
723
717 }
718 goto done;
719 }
720
721 args->allfamilies = args->afp == NULL;
722
724 list_interfaces(args);
723 list_interfaces(&ctx);
725
726done:
727 freeformat();
728 ifconfig_close(lifh);
729 exit(exit_code);
730}
731
732bool

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

766 if (sdl == NULL && !match_ether(sdl))
767 return (false);
768 return (true);
769 }
770 return (afp->af_af == sa_family);
771}
772
773static void
724
725done:
726 freeformat();
727 ifconfig_close(lifh);
728 exit(exit_code);
729}
730
731bool

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

765 if (sdl == NULL && !match_ether(sdl))
766 return (false);
767 return (true);
768 }
769 return (afp->af_af == sa_family);
770}
771
772static void
774list_interfaces_ioctl(struct ifconfig_args *args)
773list_interfaces_ioctl(if_ctx *ctx)
775{
776 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
777 struct ifaddrs *ifap, *sifap, *ifa;
778 struct ifa_order_elt *cur, *tmp;
779 char *namecp = NULL;
780 int ifindex;
774{
775 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
776 struct ifaddrs *ifap, *sifap, *ifa;
777 struct ifa_order_elt *cur, *tmp;
778 char *namecp = NULL;
779 int ifindex;
780 struct ifconfig_args *args = ctx->args;
781
782 if (getifaddrs(&ifap) != 0)
783 err(EXIT_FAILURE, "getifaddrs");
784
785 char *cp = NULL;
786
787 if (calcorders(ifap, &q) != 0)
788 err(EXIT_FAILURE, "calcorders");

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

835 if (ifindex > 1)
836 printf(" ");
837 fputs(cp, stdout);
838 continue;
839 }
840 ifindex++;
841
842 if (args->argc > 0)
781
782 if (getifaddrs(&ifap) != 0)
783 err(EXIT_FAILURE, "getifaddrs");
784
785 char *cp = NULL;
786
787 if (calcorders(ifap, &q) != 0)
788 err(EXIT_FAILURE, "calcorders");

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

835 if (ifindex > 1)
836 printf(" ");
837 fputs(cp, stdout);
838 continue;
839 }
840 ifindex++;
841
842 if (args->argc > 0)
843 ifconfig_wrapper(args, 0, args->afp);
843 ifconfig(ctx, 0, args->afp);
844 else
844 else
845 status(args, sdl, ifa);
845 status(ctx, sdl, ifa);
846 }
847 if (args->namesonly)
848 printf("\n");
849 freeifaddrs(ifap);
850}
851#endif
852
853/*

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

1084 if (setaddr || setmask) {
1085 int error = afp->af_exec(ctx, afp->af_aifaddr, afp->af_addreq);
1086 if (error != 0)
1087 Perrorc("ioctl (SIOCAIFADDR)", error);
1088 }
1089}
1090
1091int
846 }
847 if (args->namesonly)
848 printf("\n");
849 freeifaddrs(ifap);
850}
851#endif
852
853/*

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

1084 if (setaddr || setmask) {
1085 int error = afp->af_exec(ctx, afp->af_aifaddr, afp->af_addreq);
1086 if (error != 0)
1087 Perrorc("ioctl (SIOCAIFADDR)", error);
1088 }
1089}
1090
1091int
1092ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
1092ifconfig_ioctl(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
1093{
1094 const struct afswtch *afp, *nafp;
1095 const struct cmd *p;
1096 struct callback *cb;
1097 int s;
1098 int argc = orig_ctx->args->argc;
1099 char *const *argv = orig_ctx->args->argv;
1100 struct ifconfig_context _ctx = {

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

1712 }
1713}
1714
1715/*
1716 * Print the status of the interface. If an address family was
1717 * specified, show only it; otherwise, show them all.
1718 */
1719static void
1093{
1094 const struct afswtch *afp, *nafp;
1095 const struct cmd *p;
1096 struct callback *cb;
1097 int s;
1098 int argc = orig_ctx->args->argc;
1099 char *const *argv = orig_ctx->args->argv;
1100 struct ifconfig_context _ctx = {

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

1712 }
1713}
1714
1715/*
1716 * Print the status of the interface. If an address family was
1717 * specified, show only it; otherwise, show them all.
1718 */
1719static void
1720status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
1720status(if_ctx *ctx, const struct sockaddr_dl *sdl,
1721 struct ifaddrs *ifa)
1722{
1723 struct ifaddrs *ift;
1721 struct ifaddrs *ifa)
1722{
1723 struct ifaddrs *ift;
1724 int s;
1724 int s, old_s;
1725 struct ifconfig_args *args = ctx->args;
1725 bool allfamilies = args->afp == NULL;
1726 char *ifname = ifa->ifa_name;
1727
1728 if (args->afp == NULL)
1729 ifr.ifr_addr.sa_family = AF_LOCAL;
1730 else
1731 ifr.ifr_addr.sa_family =
1732 args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
1733 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1734
1735 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1736 if (s < 0)
1737 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1726 bool allfamilies = args->afp == NULL;
1727 char *ifname = ifa->ifa_name;
1728
1729 if (args->afp == NULL)
1730 ifr.ifr_addr.sa_family = AF_LOCAL;
1731 else
1732 ifr.ifr_addr.sa_family =
1733 args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
1734 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1735
1736 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1737 if (s < 0)
1738 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1739 old_s = ctx->io_s;
1740 ctx->io_s = s;
1738
1741
1739 struct ifconfig_context _ctx = {
1740 .io_s = s,
1741 .ifname = ifname,
1742 };
1743 struct ifconfig_context *ctx = &_ctx;
1744
1745 printf("%s: ", ifname);
1746 printb("flags", ifa->ifa_flags, IFFBITS);
1747 print_metric(s);
1748 print_mtu(s);
1749 putchar('\n');
1750
1751 print_description(s);
1752

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

1789 else if (args->afp->af_other_status != NULL)
1790 args->afp->af_other_status(ctx);
1791
1792 print_ifstatus(ctx);
1793 if (args->verbose > 0)
1794 sfp_status(ctx);
1795
1796 close(s);
1742 printf("%s: ", ifname);
1743 printb("flags", ifa->ifa_flags, IFFBITS);
1744 print_metric(s);
1745 print_mtu(s);
1746 putchar('\n');
1747
1748 print_description(s);
1749

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

1786 else if (args->afp->af_other_status != NULL)
1787 args->afp->af_other_status(ctx);
1788
1789 print_ifstatus(ctx);
1790 if (args->verbose > 0)
1791 sfp_status(ctx);
1792
1793 close(s);
1794 ctx->io_s = old_s;
1797 return;
1798}
1799#endif
1800
1801void
1802tunnel_status(if_ctx *ctx)
1803{
1804 af_all_tunnel_status(ctx);

--- 254 unchanged lines hidden ---
1795 return;
1796}
1797#endif
1798
1799void
1800tunnel_status(if_ctx *ctx)
1801{
1802 af_all_tunnel_status(ctx);

--- 254 unchanged lines hidden ---