NetPlatform_freebsd.c 6.7 KB

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