vlan_ifconfig.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * hostapd / VLAN ifconfig helpers
  3. * Copyright 2003, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "utils/includes.h"
  11. #include <net/if.h>
  12. #include <sys/ioctl.h>
  13. #include "utils/common.h"
  14. #include "vlan_util.h"
  15. int ifconfig_helper(const char *if_name, int up)
  16. {
  17. int fd;
  18. struct ifreq ifr;
  19. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  20. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  21. "failed: %s", __func__, strerror(errno));
  22. return -1;
  23. }
  24. os_memset(&ifr, 0, sizeof(ifr));
  25. os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
  26. if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
  27. wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
  28. "for interface %s: %s",
  29. __func__, if_name, strerror(errno));
  30. close(fd);
  31. return -1;
  32. }
  33. if (up)
  34. ifr.ifr_flags |= IFF_UP;
  35. else
  36. ifr.ifr_flags &= ~IFF_UP;
  37. if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
  38. wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
  39. "for interface %s (up=%d): %s",
  40. __func__, if_name, up, strerror(errno));
  41. close(fd);
  42. return -1;
  43. }
  44. close(fd);
  45. return 0;
  46. }
  47. int ifconfig_up(const char *if_name)
  48. {
  49. wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
  50. return ifconfig_helper(if_name, 1);
  51. }
  52. int iface_exists(const char *ifname)
  53. {
  54. return if_nametoindex(ifname);
  55. }