utils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <stdio.h>
  11. #include <netinet/in.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/ip6.h>
  14. #include <netinet/udp.h>
  15. #include <libubox/utils.h>
  16. #include "unetd.h"
  17. int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
  18. int default_port, int idx)
  19. {
  20. struct addrinfo hints = {
  21. .ai_flags = AI_ADDRCONFIG,
  22. .ai_family = af,
  23. };
  24. char *buf = strdup(str);
  25. char *host = buf, *port;
  26. struct addrinfo *ai, *ai_cur;
  27. int n_res;
  28. int ret = -1;
  29. memset(dest, 0, sizeof(*dest));
  30. if (*host == '[') {
  31. if (af == AF_INET)
  32. goto out;
  33. host++;
  34. port = strchr(host, ']');
  35. if (!port)
  36. goto out;
  37. *(port++) = 0;
  38. if (!*port)
  39. port = NULL;
  40. else if (*port == ':')
  41. port++;
  42. else
  43. goto out;
  44. hints.ai_family = AF_INET6;
  45. hints.ai_flags |= AI_NUMERICHOST;
  46. } else {
  47. host = buf;
  48. port = strchr(host, ':');
  49. if (port)
  50. *(port++) = 0;
  51. }
  52. if (getaddrinfo(host, port, &hints, &ai) || !ai)
  53. goto out;
  54. while (1) {
  55. ai_cur = ai;
  56. for (n_res = 0; ai_cur; ai_cur = ai_cur->ai_next, n_res++)
  57. if (!idx--)
  58. goto found;
  59. idx %= n_res;
  60. }
  61. found:
  62. if (ai_cur->ai_addrlen > sizeof(*dest))
  63. goto free_ai;
  64. memcpy(dest, ai_cur->ai_addr, ai_cur->ai_addrlen);
  65. if (!port)
  66. dest->in.sin_port = htons(default_port);
  67. ret = 0;
  68. free_ai:
  69. freeaddrinfo(ai);
  70. out:
  71. free(buf);
  72. return ret;
  73. }
  74. int network_get_subnet(int af, union network_addr *addr, int *mask, const char *str)
  75. {
  76. char *buf = strdup(str);
  77. char *sep, *end;
  78. int ret = -1;
  79. if (af == AF_INET6)
  80. *mask = 128;
  81. else
  82. *mask = 32;
  83. sep = strchr(buf, '/');
  84. if (sep) {
  85. unsigned long val;
  86. *(sep++) = 0;
  87. val = strtoul(sep, &end, 0);
  88. if ((end && *end) || val > *mask)
  89. goto out;
  90. *mask = val;
  91. }
  92. if (inet_pton(af, buf, addr) == 1)
  93. ret = 0;
  94. out:
  95. free(buf);
  96. return ret;
  97. }
  98. int network_get_local_addr(void *local, const union network_endpoint *target)
  99. {
  100. union network_endpoint ep = {};
  101. socklen_t len;
  102. int ret = -1;
  103. int fd;
  104. memset(local, 0, sizeof(union network_addr));
  105. if (target->sa.sa_family == AF_INET6)
  106. len = sizeof(ep.in6);
  107. else
  108. len = sizeof(ep.in);
  109. fd = socket(target->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
  110. if (fd < 0)
  111. return -1;
  112. if (connect(fd, (const struct sockaddr *)target, len))
  113. goto out;
  114. len = sizeof(ep);
  115. if (getsockname(fd, &ep.sa, &len))
  116. goto out;
  117. if (ep.sa.sa_family == AF_INET6)
  118. memcpy(local, &ep.in6.sin6_addr, sizeof(ep.in6.sin6_addr));
  119. else
  120. memcpy(local, &ep.in.sin_addr, sizeof(ep.in.sin_addr));
  121. ret = 0;
  122. out:
  123. close(fd);
  124. return ret;
  125. }
  126. void *unet_read_file(const char *name, size_t *len)
  127. {
  128. struct stat st;
  129. void *data;
  130. FILE *f;
  131. f = fopen(name, "r");
  132. if (!f)
  133. goto error;
  134. if (fstat(fileno(f), &st) < 0)
  135. goto close;
  136. if (*len && st.st_size > *len)
  137. goto close;
  138. data = malloc(st.st_size);
  139. if (!data)
  140. goto close;
  141. if (fread(data, 1, st.st_size, f) != st.st_size) {
  142. free(data);
  143. goto close;
  144. }
  145. fclose(f);
  146. *len = st.st_size;
  147. return data;
  148. close:
  149. fclose(f);
  150. error:
  151. *len = 0;
  152. return NULL;
  153. }
  154. uint64_t unet_gettime(void)
  155. {
  156. struct timespec ts;
  157. clock_gettime(CLOCK_MONOTONIC, &ts);
  158. return ts.tv_sec;
  159. }
  160. static inline uint32_t
  161. csum_tcpudp_nofold(uint32_t saddr, uint32_t daddr, uint8_t proto, uint32_t len)
  162. {
  163. uint64_t sum = 0;
  164. sum += saddr;
  165. sum += daddr;
  166. #if __BYTE_ORDER == __LITTLE_ENDIAN
  167. sum += (proto + len) << 8;
  168. #else
  169. sum += proto + len;
  170. #endif
  171. sum = (sum & 0xffffffff) + (sum >> 32);
  172. sum = (sum & 0xffffffff) + (sum >> 32);
  173. return (uint32_t)sum;
  174. }
  175. static inline uint32_t csum_add(uint32_t sum, uint32_t addend)
  176. {
  177. sum += addend;
  178. return sum + (sum < addend);
  179. }
  180. static inline uint16_t csum_fold(uint32_t sum)
  181. {
  182. sum = (sum & 0xffff) + (sum >> 16);
  183. sum = (sum & 0xffff) + (sum >> 16);
  184. return (uint16_t)~sum;
  185. }
  186. static uint32_t csum_partial(const void *buf, int len)
  187. {
  188. const uint16_t *data = buf;
  189. uint32_t sum = 0;
  190. while (len > 1) {
  191. sum += *data++;
  192. len -= 2;
  193. }
  194. if (len == 1)
  195. #if __BYTE_ORDER == __LITTLE_ENDIAN
  196. sum += *(uint8_t *)data;
  197. #else
  198. sum += *(uint8_t *)data << 8;
  199. #endif
  200. sum = (sum & 0xffff) + (sum >> 16);
  201. sum = (sum & 0xffff) + (sum >> 16);
  202. return sum;
  203. }
  204. static void fixup_udpv4(void *hdr, size_t hdrlen, const void *data, size_t len)
  205. {
  206. struct ip *ip = hdr;
  207. struct udphdr *udp = hdr + ip->ip_hl * 4;
  208. uint16_t udp_len = sizeof(*udp) + len;
  209. uint32_t sum;
  210. if ((void *)&udp[1] > hdr + hdrlen)
  211. return;
  212. udp->uh_sum = 0;
  213. udp->uh_ulen = htons(udp_len);
  214. sum = csum_tcpudp_nofold(*(uint32_t *)&ip->ip_src, *(uint32_t *)&ip->ip_dst,
  215. ip->ip_p, udp_len);
  216. sum = csum_add(sum, csum_partial(udp, sizeof(*udp)));
  217. sum = csum_add(sum, csum_partial(data, len));
  218. udp->uh_sum = csum_fold(sum);
  219. ip->ip_len = htons(hdrlen + len);
  220. ip->ip_sum = 0;
  221. ip->ip_sum = csum_fold(csum_partial(ip, sizeof(*ip)));
  222. #ifdef __APPLE__
  223. ip->ip_len = hdrlen + len;
  224. #endif
  225. }
  226. static void fixup_udpv6(void *hdr, size_t hdrlen, const void *data, size_t len)
  227. {
  228. struct ip6_hdr *ip = hdr;
  229. struct udphdr *udp = hdr + sizeof(*ip);
  230. uint16_t udp_len = htons(sizeof(*udp) + len);
  231. if ((void *)&udp[1] > hdr + hdrlen)
  232. return;
  233. ip->ip6_plen = htons(sizeof(*udp) + len);
  234. udp->uh_sum = 0;
  235. udp->uh_ulen = udp_len;
  236. udp->uh_sum = csum_fold(csum_partial(hdr, sizeof(*ip) + sizeof(*udp)));
  237. #ifdef __APPLE__
  238. ip->ip6_plen = sizeof(*udp) + len;
  239. #endif
  240. }
  241. static void fixup_ip_udp_header(void *hdr, size_t hdrlen, const void *data, size_t len)
  242. {
  243. if (hdrlen >= sizeof(struct ip6_hdr) + sizeof(struct udphdr))
  244. fixup_udpv6(hdr, hdrlen, data, len);
  245. else if (hdrlen >= sizeof(struct ip) + sizeof(struct udphdr))
  246. fixup_udpv4(hdr, hdrlen, data, len);
  247. }
  248. int sendto_rawudp(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen,
  249. const void *data, size_t len)
  250. {
  251. const struct sockaddr *sa = addr;
  252. struct iovec iov[2] = {
  253. { .iov_base = ip_hdr, .iov_len = ip_hdrlen },
  254. { .iov_base = (void *)data, .iov_len = len }
  255. };
  256. struct msghdr msg = {
  257. .msg_name = (void *)addr,
  258. .msg_iov = iov,
  259. .msg_iovlen = ARRAY_SIZE(iov),
  260. };
  261. if (sa->sa_family == AF_INET6)
  262. msg.msg_namelen = sizeof(struct sockaddr_in6);
  263. else
  264. msg.msg_namelen = sizeof(struct sockaddr_in);
  265. fixup_ip_udp_header(ip_hdr, ip_hdrlen, data, len);
  266. return sendmsg(fd, &msg, 0);
  267. }