NetPlatform_darwin.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "exception/Except.h"
  16. #include "util/platform/netdev/NetPlatform.h"
  17. #include "util/AddrTools.h"
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <sys/ioctl.h>
  21. #include <unistd.h>
  22. #include <sys/socket.h>
  23. #include <sys/types.h>
  24. #include <stdlib.h>
  25. #include <stddef.h>
  26. #include <net/if.h>
  27. #include <string.h>
  28. #include <netdb.h>
  29. #include <net/if_var.h>
  30. #include <netinet/in_var.h>
  31. #include <netinet6/nd6.h>
  32. #include <netinet/in.h>
  33. #include <sys/kern_control.h>
  34. #include <sys/sys_domain.h>
  35. #include <sys/kern_event.h>
  36. static void addIp4Address(const char* interfaceName,
  37. const uint8_t address[4],
  38. int prefixLen,
  39. struct Log* logger,
  40. struct Except* eh)
  41. {
  42. Except_throw(eh, "unimplemented");
  43. }
  44. static void addIp6Address(const char* interfaceName,
  45. const uint8_t address[16],
  46. int prefixLen,
  47. struct Log* logger,
  48. struct Except* eh)
  49. {
  50. /* stringify our IP address */
  51. char myIp[40];
  52. AddrTools_printIp((uint8_t*)myIp, address);
  53. /* set up the interface ip assignment request */
  54. struct in6_aliasreq in6_addreq;
  55. memset(&in6_addreq, 0, sizeof(in6_addreq));
  56. in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  57. in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  58. /* parse the IPv6 address and add it to the request */
  59. struct addrinfo hints, *result;
  60. bzero(&hints, sizeof(struct addrinfo));
  61. hints.ai_family = AF_INET6;
  62. int err = getaddrinfo((const char *)myIp, NULL, &hints, &result);
  63. if (err) {
  64. // Should never happen since the address is specified as binary.
  65. Except_throw(eh, "bad IPv6 address [%s]", gai_strerror(err));
  66. }
  67. bcopy(result->ai_addr, &in6_addreq.ifra_addr, result->ai_addrlen);
  68. /* turn the prefixlen into a mask, and add it to the request */
  69. struct sockaddr_in6* mask = &in6_addreq.ifra_prefixmask;
  70. mask->sin6_len = sizeof(*mask);
  71. if (prefixLen >= 128 || prefixLen <= 0) {
  72. memset(&mask->sin6_addr, 0xff, sizeof(struct in6_addr));
  73. } else {
  74. memset((void *)&mask->sin6_addr, 0x00, sizeof(mask->sin6_addr));
  75. memset((void *)&mask->sin6_addr, 0xff, prefixLen>>3);
  76. ((uint8_t*)&mask->sin6_addr)[prefixLen>>3] = 0xff << (8 - (prefixLen%8));
  77. }
  78. strncpy(in6_addreq.ifra_name, interfaceName, sizeof(in6_addreq.ifra_name));
  79. /* do the actual assignment ioctl */
  80. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  81. if (s < 0) {
  82. Except_throw(eh, "socket() [%s]", strerror(errno));
  83. }
  84. if (ioctl(s, SIOCAIFADDR_IN6, &in6_addreq) < 0) {
  85. int err = errno;
  86. close(s);
  87. Except_throw(eh, "ioctl(SIOCAIFADDR) [%s]", strerror(err));
  88. }
  89. Log_info(logger, "Configured IPv6 [%s/%i] for [%s]", myIp, prefixLen, interfaceName);
  90. close(s);
  91. }
  92. void NetPlatform_addAddress(const char* interfaceName,
  93. const uint8_t* address,
  94. int prefixLen,
  95. int addrFam,
  96. struct Log* logger,
  97. struct Except* eh)
  98. {
  99. if (addrFam == Sockaddr_AF_INET6) {
  100. addIp6Address(interfaceName, address, prefixLen, logger, eh);
  101. } else if (addrFam == Sockaddr_AF_INET) {
  102. addIp4Address(interfaceName, address, prefixLen, logger, eh);
  103. } else {
  104. Assert_true(0);
  105. }
  106. }
  107. void NetPlatform_setMTU(const char* interfaceName,
  108. uint32_t mtu,
  109. struct Log* logger,
  110. struct Except* eh)
  111. {
  112. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  113. if (s < 0) {
  114. Except_throw(eh, "socket() [%s]", strerror(errno));
  115. }
  116. struct ifreq ifRequest;
  117. strncpy(ifRequest.ifr_name, interfaceName, IFNAMSIZ);
  118. ifRequest.ifr_mtu = mtu;
  119. Log_info(logger, "Setting MTU for device [%s] to [%u] bytes.", interfaceName, mtu);
  120. if (ioctl(s, SIOCSIFMTU, &ifRequest) < 0) {
  121. int err = errno;
  122. close(s);
  123. Except_throw(eh, "ioctl(SIOCSIFMTU) [%s]", strerror(err));
  124. }
  125. }