NetPlatform_openbsd.c 5.2 KB

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