NetPlatform_openbsd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "exception/Er.h"
  16. #include "util/platform/netdev/NetPlatform.h"
  17. #include "util/AddrTools.h"
  18. #include "util/platform/Sockaddr.h"
  19. #include "util/CString.h"
  20. #include <errno.h>
  21. #include <ctype.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/ioctl.h>
  25. #include <unistd.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <net/if.h>
  32. #include <string.h>
  33. #include <netdb.h>
  34. #include <net/if_tun.h>
  35. #include <netinet/in.h>
  36. #include <netinet6/in6_var.h>
  37. #include <netinet6/nd6.h>
  38. static Er_DEFUN(void addIp4Address(const char* interfaceName,
  39. const uint8_t address[4],
  40. int prefixLen,
  41. struct Log* logger,
  42. struct Allocator* alloc))
  43. {
  44. Er_raise(alloc, "unimplemented");
  45. }
  46. static Er_DEFUN(void addIp6Address(const char* interfaceName,
  47. const uint8_t address[16],
  48. int prefixLen,
  49. struct Log* logger,
  50. struct Allocator* alloc))
  51. {
  52. /* stringify our IP address */
  53. char myIp[40];
  54. AddrTools_printIp((uint8_t*)myIp, address);
  55. /* set up the interface ip assignment request */
  56. struct in6_aliasreq in6_addreq;
  57. memset(&in6_addreq, 0, sizeof(in6_addreq));
  58. in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  59. in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  60. /* parse the IPv6 address and add it to the request */
  61. struct addrinfo hints, *result;
  62. bzero(&hints, sizeof(struct addrinfo));
  63. hints.ai_family = AF_INET6;
  64. int err = getaddrinfo((const char *)myIp, NULL, &hints, &result);
  65. if (err) {
  66. // Should never happen since the address is specified as binary.
  67. Er_raise(alloc, "bad IPv6 address [%s]", gai_strerror(err));
  68. }
  69. memcpy(&in6_addreq.ifra_addr, result->ai_addr, result->ai_addrlen);
  70. /* turn the prefixlen into a mask, and add it to the request */
  71. struct sockaddr_in6* mask = &in6_addreq.ifra_prefixmask;
  72. u_char *cp;
  73. int len = prefixLen;
  74. mask->sin6_len = sizeof(*mask);
  75. if ((prefixLen == 0) || (prefixLen == 128)) {
  76. memset(&mask->sin6_addr, 0xff, sizeof(struct in6_addr));
  77. } else {
  78. memset((void *)&mask->sin6_addr, 0x00, sizeof(mask->sin6_addr));
  79. for (cp = (u_char *)&mask->sin6_addr; len > 7; len -= 8) {
  80. *cp++ = 0xff;
  81. }
  82. *cp = 0xff << (8 - len);
  83. }
  84. CString_safeStrncpy(in6_addreq.ifra_name, interfaceName, sizeof(in6_addreq.ifra_name));
  85. /* do the actual assignment ioctl */
  86. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  87. if (s < 0) {
  88. Er_raise(alloc, "socket() [%s]", strerror(errno));
  89. }
  90. if (ioctl(s, SIOCAIFADDR_IN6, &in6_addreq) < 0) {
  91. int err = errno;
  92. close(s);
  93. Er_raise(alloc, "ioctl(SIOCAIFADDR) [%s] for [%s]", strerror(err), interfaceName);
  94. }
  95. Log_info(logger, "Configured IPv6 [%s/%i] for [%s]", myIp, prefixLen, interfaceName);
  96. close(s);
  97. Er_ret();
  98. }
  99. Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
  100. const uint8_t* address,
  101. int prefixLen,
  102. int addrFam,
  103. struct Log* logger,
  104. struct Allocator* tempAlloc))
  105. {
  106. if (addrFam == Sockaddr_AF_INET6) {
  107. addIp6Address(interfaceName, address, prefixLen, logger, tempAlloc);
  108. } else if (addrFam == Sockaddr_AF_INET) {
  109. addIp4Address(interfaceName, address, prefixLen, logger, tempAlloc);
  110. } else {
  111. Assert_true(0);
  112. }
  113. Er_ret();
  114. }
  115. Er_DEFUN(void NetPlatform_setMTU(const char* interfaceName,
  116. uint32_t mtu,
  117. struct Log* logger,
  118. struct Allocator* errAlloc))
  119. {
  120. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  121. if (s < 0) {
  122. Er_raise(errAlloc, "socket() [%s]", strerror(errno));
  123. }
  124. struct ifreq ifRequest;
  125. CString_safeStrncpy(ifRequest.ifr_name, interfaceName, IFNAMSIZ);
  126. ifRequest.ifr_mtu = mtu;
  127. Log_info(logger, "Setting MTU for device [%s] to [%u] bytes.", interfaceName, mtu);
  128. if (ioctl(s, SIOCSIFMTU, &ifRequest) < 0) {
  129. int err = errno;
  130. close(s);
  131. Er_raise(errAlloc, "ioctl(SIOCSIFMTU) [%s]", strerror(err));
  132. }
  133. }
  134. Er_DEFUN(void NetPlatform_setRoutes(const char* ifName,
  135. struct Sockaddr** prefixSet,
  136. int prefixCount,
  137. struct Log* logger,
  138. struct Allocator* tempAlloc))
  139. {
  140. Er_raise(tempAlloc, "NetPlatform_setRoutes is not implemented in this platform.");
  141. }