NetPlatform_freebsd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "util/platform/netdev/NetPlatform.h"
  16. #include "exception/Er.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_var.h>
  35. #include <net/if_tun.h>
  36. #include <netinet/in.h>
  37. #include <netinet6/in6_var.h>
  38. #include <netinet6/nd6.h>
  39. #include <arpa/inet.h>
  40. static Er_DEFUN(void addIp4Address(const char* interfaceName,
  41. const uint8_t address[4],
  42. int prefixLen,
  43. struct Log* logger,
  44. struct Allocator* alloc))
  45. {
  46. struct ifaliasreq ifaliasreq;
  47. struct sockaddr_in* in;
  48. memset(&ifaliasreq, 0, sizeof(ifaliasreq));
  49. snprintf(ifaliasreq.ifra_name,IFNAMSIZ, "%s",interfaceName);
  50. char myIp[40];
  51. snprintf(myIp, 40, "%u.%u.%u.%u", address[0], address[1], address[2], address[3]);
  52. in_addr_t nmask = ( ~((1 << (32 - prefixLen)) - 1) );
  53. char myMask[40];
  54. snprintf(myMask,40,"%u",nmask);
  55. in = (struct sockaddr_in *) &ifaliasreq.ifra_addr;
  56. in->sin_family = AF_INET;
  57. in->sin_len = sizeof(ifaliasreq.ifra_addr);
  58. int err = inet_aton(myIp, &in->sin_addr);
  59. if (err == 0){
  60. Er_raise(alloc, "inet_aton(myIp) failed");
  61. }
  62. in = (struct sockaddr_in *) &ifaliasreq.ifra_mask;
  63. in->sin_family = AF_INET;
  64. in->sin_len = sizeof(ifaliasreq.ifra_mask);
  65. err = inet_aton(myMask, &in->sin_addr);
  66. if (err == 0){
  67. Er_raise(alloc, "inet_aton(myMask) failed");
  68. }
  69. in = (struct sockaddr_in *) &ifaliasreq.ifra_broadaddr;
  70. in->sin_family = AF_INET;
  71. in->sin_len = sizeof(ifaliasreq.ifra_broadaddr);
  72. in->sin_addr.s_addr =
  73. ((struct sockaddr_in *) &ifaliasreq.ifra_addr)->sin_addr.s_addr | ~
  74. ((struct sockaddr_in *) &ifaliasreq.ifra_mask)->sin_addr.s_addr;
  75. int s = socket(PF_INET, SOCK_STREAM, 0);
  76. if (ioctl(s, SIOCAIFADDR, &ifaliasreq) == -1){
  77. int err = errno;
  78. close(s);
  79. Er_raise(alloc, "ioctl(SIOCAIFADDR) [%s] for [%s]", strerror(err), interfaceName);
  80. }
  81. Log_info(logger, "Configured IPv4 [%s/%s] for [%s]", myIp, myMask, interfaceName);
  82. close(s);
  83. Er_ret();
  84. }
  85. static Er_DEFUN(void addIp6Address(const char* interfaceName,
  86. const uint8_t address[16],
  87. int prefixLen,
  88. struct Log* logger,
  89. struct Allocator* alloc))
  90. {
  91. /* stringify our IP address */
  92. char myIp[40];
  93. AddrTools_printIp((uint8_t*)myIp, address);
  94. /* set up the interface ip assignment request */
  95. struct in6_aliasreq in6_addreq;
  96. memset(&in6_addreq, 0, sizeof(in6_addreq));
  97. in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  98. in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  99. /* parse the IPv6 address and add it to the request */
  100. struct addrinfo hints, *result;
  101. bzero(&hints, sizeof(struct addrinfo));
  102. hints.ai_family = AF_INET6;
  103. int err = getaddrinfo((const char *)myIp, NULL, &hints, &result);
  104. if (err) {
  105. // Should never happen since the address is specified as binary.
  106. Er_raise(alloc, "bad IPv6 address [%s]", gai_strerror(err));
  107. }
  108. memcpy(&in6_addreq.ifra_addr, result->ai_addr, result->ai_addrlen);
  109. /* turn the prefixlen into a mask, and add it to the request */
  110. struct sockaddr_in6* mask = &in6_addreq.ifra_prefixmask;
  111. u_char *cp;
  112. int len = prefixLen;
  113. mask->sin6_len = sizeof(*mask);
  114. if ((prefixLen == 0) || (prefixLen == 128)) {
  115. memset(&mask->sin6_addr, 0xff, sizeof(struct in6_addr));
  116. } else {
  117. memset((void *)&mask->sin6_addr, 0x00, sizeof(mask->sin6_addr));
  118. for (cp = (u_char *)&mask->sin6_addr; len > 7; len -= 8) {
  119. *cp++ = 0xff;
  120. }
  121. *cp = 0xff << (8 - len);
  122. }
  123. CString_safeStrncpy(in6_addreq.ifra_name, interfaceName, sizeof(in6_addreq.ifra_name));
  124. /* do the actual assignment ioctl */
  125. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  126. if (s < 0) {
  127. Er_raise(alloc, "socket() [%s]", strerror(errno));
  128. }
  129. if (ioctl(s, SIOCAIFADDR_IN6, &in6_addreq) < 0) {
  130. int err = errno;
  131. close(s);
  132. Er_raise(alloc, "ioctl(SIOCAIFADDR) [%s]", strerror(err));
  133. }
  134. Log_info(logger, "Configured IPv6 [%s/%i] for [%s]", myIp, prefixLen, interfaceName);
  135. close(s);
  136. Er_ret();
  137. }
  138. Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
  139. const uint8_t* address,
  140. int prefixLen,
  141. int addrFam,
  142. struct Log* logger,
  143. struct Allocator* tempAlloc))
  144. {
  145. if (addrFam == AF_INET6) {
  146. Er(addIp6Address(interfaceName, address, prefixLen, logger, tempAlloc));
  147. } else if (addrFam == AF_INET) {
  148. Er(addIp4Address(interfaceName, address, prefixLen, logger, tempAlloc));
  149. } else {
  150. Assert_true(0);
  151. }
  152. Er_ret();
  153. }
  154. Er_DEFUN(void NetPlatform_setMTU(const char* interfaceName,
  155. uint32_t mtu,
  156. struct Log* logger,
  157. struct Allocator* errAlloc))
  158. {
  159. int s = socket(AF_INET6, SOCK_DGRAM, 0);
  160. if (s < 0) {
  161. Er_raise(errAlloc, "socket() [%s]", strerror(errno));
  162. }
  163. struct ifreq ifRequest;
  164. CString_safeStrncpy(ifRequest.ifr_name, interfaceName, IFNAMSIZ);
  165. ifRequest.ifr_mtu = mtu;
  166. Log_info(logger, "Setting MTU for device [%s] to [%u] bytes.", interfaceName, mtu);
  167. if (ioctl(s, SIOCSIFMTU, &ifRequest) < 0) {
  168. int err = errno;
  169. close(s);
  170. Er_raise(errAlloc, "ioctl(SIOCSIFMTU) failed [%s]", strerror(err));
  171. }
  172. Er_ret();
  173. }
  174. Er_DEFUN(void NetPlatform_setRoutes(const char* ifName,
  175. struct Sockaddr** prefixSet,
  176. int prefixCount,
  177. struct Log* logger,
  178. struct Allocator* tempAlloc))
  179. {
  180. Er_raise(tempAlloc, "NetPlatform_setRoutes is not implemented in this platform.");
  181. }