interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <sys/socket.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/utsname.h>
  20. #include <net/if.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <sys/types.h>
  24. #include <ifaddrs.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <libubox/usock.h>
  31. #include <libubox/uloop.h>
  32. #include <libubox/avl-cmp.h>
  33. #include <libubox/utils.h>
  34. #include "cache.h"
  35. #include "interface.h"
  36. #include "util.h"
  37. #include "dns.h"
  38. #include "announce.h"
  39. #include "service.h"
  40. static struct uloop_fd ufd[] = {
  41. [SOCK_UC_IPV4] = { .fd = -1 },
  42. [SOCK_UC_IPV6] = { .fd = -1 },
  43. [SOCK_MC_IPV4] = { .fd = -1 },
  44. [SOCK_MC_IPV6] = { .fd = -1 },
  45. };
  46. static int
  47. interface_send_packet4(struct interface *iface, struct sockaddr_in *to, struct iovec *iov, int iov_len)
  48. {
  49. static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
  50. static struct sockaddr_in a = {};
  51. static struct msghdr m = {
  52. .msg_name = (struct sockaddr *) &a,
  53. .msg_namelen = sizeof(a),
  54. .msg_control = cmsg_data,
  55. .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
  56. };
  57. struct in_pktinfo *pkti;
  58. struct cmsghdr *cmsg;
  59. int fd;
  60. a.sin_family = AF_INET;
  61. a.sin_port = htons(MCAST_PORT);
  62. m.msg_iov = iov;
  63. m.msg_iovlen = iov_len;
  64. memset(cmsg_data, 0, sizeof(cmsg_data));
  65. cmsg = CMSG_FIRSTHDR(&m);
  66. cmsg->cmsg_len = m.msg_controllen;
  67. cmsg->cmsg_level = IPPROTO_IP;
  68. cmsg->cmsg_type = IP_PKTINFO;
  69. pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
  70. pkti->ipi_ifindex = iface->ifindex;
  71. fd = ufd[iface->type].fd;
  72. if (interface_multicast(iface)) {
  73. a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
  74. if (to)
  75. fprintf(stderr, "Ignoring IPv4 address for multicast interface\n");
  76. } else {
  77. a.sin_addr.s_addr = to->sin_addr.s_addr;
  78. }
  79. return sendmsg(fd, &m, 0);
  80. }
  81. static int
  82. interface_send_packet6(struct interface *iface, struct sockaddr_in6 *to, struct iovec *iov, int iov_len)
  83. {
  84. static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
  85. static struct sockaddr_in6 a = {};
  86. static struct msghdr m = {
  87. .msg_name = (struct sockaddr *) &a,
  88. .msg_namelen = sizeof(a),
  89. .msg_control = cmsg_data,
  90. .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)),
  91. };
  92. struct in6_pktinfo *pkti;
  93. struct cmsghdr *cmsg;
  94. int fd;
  95. a.sin6_family = AF_INET6;
  96. a.sin6_port = htons(MCAST_PORT);
  97. a.sin6_scope_id = iface->ifindex;
  98. m.msg_iov = iov;
  99. m.msg_iovlen = iov_len;
  100. memset(cmsg_data, 0, sizeof(cmsg_data));
  101. cmsg = CMSG_FIRSTHDR(&m);
  102. cmsg->cmsg_len = m.msg_controllen;
  103. cmsg->cmsg_level = IPPROTO_IPV6;
  104. cmsg->cmsg_type = IPV6_PKTINFO;
  105. pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
  106. pkti->ipi6_ifindex = iface->ifindex;
  107. fd = ufd[iface->type].fd;
  108. if (interface_multicast(iface)) {
  109. inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
  110. if (to)
  111. fprintf(stderr, "Ignoring IPv6 address for multicast interface\n");
  112. } else {
  113. a.sin6_addr = to->sin6_addr;
  114. }
  115. return sendmsg(fd, &m, 0);
  116. }
  117. int
  118. interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len)
  119. {
  120. if (!interface_multicast(iface) && !to) {
  121. fprintf(stderr, "No IP address specified for unicast interface\n");
  122. errno = EINVAL;
  123. return -1;
  124. }
  125. if (debug > 1) {
  126. fprintf(stderr, "TX ipv%d: %s\n", interface_ipv6(iface) ? 6 : 4, iface->name);
  127. fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
  128. }
  129. if (interface_ipv6(iface))
  130. return interface_send_packet6(iface, (struct sockaddr_in6 *)to, iov, iov_len);
  131. return interface_send_packet4(iface, (struct sockaddr_in *)to, iov, iov_len);
  132. }
  133. static struct interface *interface_lookup(unsigned int ifindex, enum umdns_socket_type type)
  134. {
  135. struct interface *iface;
  136. vlist_for_each_element(&interfaces, iface, node)
  137. if (iface->ifindex == ifindex && iface->type == type)
  138. return iface;
  139. return NULL;
  140. }
  141. static void interface_free(struct interface *iface)
  142. {
  143. announce_free(iface);
  144. free(iface->addrs.v4);
  145. free(iface);
  146. }
  147. static int
  148. interface_valid_src(void *ip1, void *mask, void *ip2, int len)
  149. {
  150. uint8_t *i1 = ip1;
  151. uint8_t *i2 = ip2;
  152. uint8_t *m = mask;
  153. int i;
  154. if (cfg_no_subnet)
  155. return 0;
  156. for (i = 0; i < len; i++, i1++, i2++, m++) {
  157. if ((*i1 & *m) != (*i2 & *m))
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. static void
  163. read_socket4(struct uloop_fd *u, unsigned int events)
  164. {
  165. enum umdns_socket_type type = (enum umdns_socket_type)(u - ufd);
  166. struct interface *iface;
  167. static uint8_t buffer[8 * 1024];
  168. struct iovec iov[1];
  169. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
  170. struct cmsghdr *cmsgptr;
  171. struct msghdr msg;
  172. socklen_t len;
  173. struct sockaddr_in from;
  174. int flags = 0;
  175. uint8_t ttl = 0;
  176. struct in_pktinfo *inp = NULL;
  177. bool valid_src = false;
  178. if (u->eof) {
  179. uloop_end();
  180. return;
  181. }
  182. iov[0].iov_base = buffer;
  183. iov[0].iov_len = sizeof(buffer);
  184. memset(&msg, 0, sizeof(msg));
  185. msg.msg_name = (struct sockaddr *) &from;
  186. msg.msg_namelen = sizeof(struct sockaddr_in);
  187. msg.msg_iov = iov;
  188. msg.msg_iovlen = 1;
  189. msg.msg_control = &cmsg;
  190. msg.msg_controllen = sizeof(cmsg);
  191. len = recvmsg(u->fd, &msg, flags);
  192. if (len == -1) {
  193. perror("read failed");
  194. return;
  195. }
  196. for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  197. void *c = CMSG_DATA(cmsgptr);
  198. switch (cmsgptr->cmsg_type) {
  199. case IP_PKTINFO:
  200. inp = ((struct in_pktinfo *) c);
  201. break;
  202. case IP_TTL:
  203. ttl = (uint8_t) *((int *) c);
  204. break;
  205. default:
  206. fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
  207. return;
  208. }
  209. }
  210. if (!inp)
  211. return;
  212. iface = interface_lookup(inp->ipi_ifindex, type);
  213. if (!iface)
  214. return;
  215. if (debug > 1) {
  216. char buf[256];
  217. fprintf(stderr, "RX ipv4: %s\n", iface->name);
  218. fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
  219. inet_ntop(AF_INET, &from.sin_addr, buf, 256);
  220. fprintf(stderr, " src %s:%d\n", buf, ntohs(from.sin_port));
  221. inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
  222. fprintf(stderr, " dst %s\n", buf);
  223. inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
  224. fprintf(stderr, " real %s\n", buf);
  225. fprintf(stderr, " ttl %u\n", ttl);
  226. }
  227. for (size_t i = 0; i < iface->addrs.n_addr; i++) {
  228. if (!interface_valid_src((void *)&iface->addrs.v4[i].addr,
  229. (void *)&iface->addrs.v4[i].mask,
  230. (void *) &from.sin_addr, 4)) {
  231. valid_src = true;
  232. break;
  233. }
  234. }
  235. if (!valid_src)
  236. return;
  237. dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin_port), buffer, len);
  238. }
  239. static void
  240. read_socket6(struct uloop_fd *u, unsigned int events)
  241. {
  242. enum umdns_socket_type type = (enum umdns_socket_type)(u - ufd);
  243. struct interface *iface;
  244. static uint8_t buffer[8 * 1024];
  245. struct iovec iov[1];
  246. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
  247. struct cmsghdr *cmsgptr;
  248. struct msghdr msg;
  249. socklen_t len;
  250. struct sockaddr_in6 from;
  251. int flags = 0;
  252. int ttl = 0;
  253. struct in6_pktinfo *inp = NULL;
  254. bool valid_src = false;
  255. if (u->eof) {
  256. uloop_end();
  257. return;
  258. }
  259. iov[0].iov_base = buffer;
  260. iov[0].iov_len = sizeof(buffer);
  261. memset(&msg, 0, sizeof(msg));
  262. msg.msg_name = (struct sockaddr *) &from;
  263. msg.msg_namelen = sizeof(struct sockaddr_in6);
  264. msg.msg_iov = iov;
  265. msg.msg_iovlen = 1;
  266. msg.msg_control = &cmsg6;
  267. msg.msg_controllen = sizeof(cmsg6);
  268. len = recvmsg(u->fd, &msg, flags);
  269. if (len == -1) {
  270. perror("read failed");
  271. return;
  272. }
  273. for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  274. void *c = CMSG_DATA(cmsgptr);
  275. switch (cmsgptr->cmsg_type) {
  276. case IPV6_PKTINFO:
  277. inp = ((struct in6_pktinfo *) c);
  278. break;
  279. case IPV6_HOPLIMIT:
  280. ttl = (uint8_t) *((int *) c);
  281. break;
  282. default:
  283. fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
  284. return;
  285. }
  286. }
  287. if (!inp)
  288. return;
  289. iface = interface_lookup(inp->ipi6_ifindex, type);
  290. if (!iface)
  291. return;
  292. if (debug > 1) {
  293. char buf[256];
  294. fprintf(stderr, "RX ipv6: %s\n", iface->name);
  295. fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
  296. inet_ntop(AF_INET6, &from.sin6_addr, buf, 256);
  297. fprintf(stderr, " src %s:%d\n", buf, ntohs(from.sin6_port));
  298. inet_ntop(AF_INET6, &inp->ipi6_addr, buf, 256);
  299. fprintf(stderr, " dst %s\n", buf);
  300. fprintf(stderr, " ttl %u\n", ttl);
  301. }
  302. for (size_t i = 0; i < iface->addrs.n_addr; i++) {
  303. if (!interface_valid_src((void *)&iface->addrs.v6[i].addr,
  304. (void *)&iface->addrs.v6[i].mask,
  305. (void *)&from.sin6_addr, 6)) {
  306. valid_src = true;
  307. break;
  308. }
  309. }
  310. if (!valid_src)
  311. return;
  312. dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin6_port), buffer, len);
  313. }
  314. static int
  315. interface_mcast_setup4(struct interface *iface)
  316. {
  317. struct ip_mreqn mreq;
  318. struct sockaddr_in sa = {};
  319. int fd = ufd[SOCK_MC_IPV4].fd;
  320. sa.sin_family = AF_INET;
  321. sa.sin_port = htons(MCAST_PORT);
  322. inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
  323. memset(&mreq, 0, sizeof(mreq));
  324. mreq.imr_multiaddr = sa.sin_addr;
  325. mreq.imr_ifindex = iface->ifindex;
  326. mreq.imr_address.s_addr = iface->addrs.v4[0].addr.s_addr;
  327. /* Some network drivers have issues with dropping membership of
  328. * mcast groups when the iface is down, but don't allow rejoining
  329. * when it comes back up. This is an ugly workaround
  330. * -- this was copied from avahi --
  331. */
  332. setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
  333. setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  334. return 0;
  335. }
  336. static int
  337. interface_mcast_setup6(struct interface *iface)
  338. {
  339. struct ipv6_mreq mreq;
  340. struct sockaddr_in6 sa = {};
  341. int fd = ufd[SOCK_MC_IPV6].fd;
  342. sa.sin6_family = AF_INET6;
  343. sa.sin6_port = htons(MCAST_PORT);
  344. inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
  345. memset(&mreq, 0, sizeof(mreq));
  346. mreq.ipv6mr_multiaddr = sa.sin6_addr;
  347. mreq.ipv6mr_interface = iface->ifindex;
  348. setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
  349. setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  350. return 0;
  351. }
  352. static void interface_start(struct interface *iface)
  353. {
  354. if (iface->type & SOCKTYPE_BIT_UNICAST)
  355. return;
  356. if (iface->type & SOCKTYPE_BIT_IPV6)
  357. interface_mcast_setup6(iface);
  358. else
  359. interface_mcast_setup4(iface);
  360. dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0);
  361. announce_init(iface);
  362. }
  363. static bool
  364. iface_equal(struct interface *if_old, struct interface *if_new)
  365. {
  366. size_t addr_size;
  367. if (if_old->ifindex != if_new->ifindex ||
  368. if_old->addrs.n_addr != if_new->addrs.n_addr)
  369. return false;
  370. if (if_old->type & SOCKTYPE_BIT_IPV6)
  371. addr_size = sizeof(*if_old->addrs.v6);
  372. else
  373. addr_size = sizeof(*if_old->addrs.v4);
  374. addr_size *= if_old->addrs.n_addr;
  375. if (memcmp(if_old->addrs.v4, if_new->addrs.v4, addr_size) != 0)
  376. return false;
  377. return true;
  378. }
  379. static void
  380. iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
  381. struct vlist_node *node_old)
  382. {
  383. struct interface *if_old = container_of_safe(node_old, struct interface, node);
  384. struct interface *if_new = container_of_safe(node_new, struct interface, node);
  385. if (if_old && if_new) {
  386. if (!iface_equal(if_old, if_new))
  387. cache_cleanup(if_old);
  388. free(if_old->addrs.v4);
  389. if_old->addrs = if_new->addrs;
  390. if_old->ifindex = if_new->ifindex;
  391. free(if_new);
  392. return;
  393. }
  394. if (if_old)
  395. interface_free(if_old);
  396. if (if_new)
  397. interface_start(if_new);
  398. }
  399. static int interface_init_socket(enum umdns_socket_type type)
  400. {
  401. struct sockaddr_in6 local6 = {
  402. .sin6_family = AF_INET6
  403. };
  404. struct sockaddr_in local = {
  405. .sin_family = AF_INET
  406. };
  407. uint8_t ttl = 255;
  408. int ittl = 255;
  409. int yes = 1;
  410. int no = 0;
  411. int fd;
  412. int af = (type & SOCKTYPE_BIT_IPV6) ? AF_INET6 : AF_INET;
  413. if (ufd[type].fd >= 0)
  414. return 0;
  415. ufd[type].fd = fd = socket(af, SOCK_DGRAM, 0);
  416. if (fd < 0)
  417. return -1;
  418. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  419. #ifdef SO_REUSEPORT
  420. setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
  421. #endif
  422. switch (type) {
  423. case SOCK_UC_IPV4:
  424. case SOCK_UC_IPV6:
  425. break;
  426. case SOCK_MC_IPV4:
  427. setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  428. setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl));
  429. setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no));
  430. local.sin_port = htons(MCAST_PORT);
  431. break;
  432. case SOCK_MC_IPV6:
  433. setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl));
  434. setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
  435. setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes));
  436. setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no));
  437. local6.sin6_port = htons(MCAST_PORT);
  438. break;
  439. }
  440. if (type & SOCKTYPE_BIT_IPV6) {
  441. ufd[type].cb = read_socket6;
  442. if (bind(fd, (struct sockaddr *)&local6, sizeof(local6)) < 0)
  443. goto error;
  444. setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes));
  445. setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes));
  446. } else {
  447. ufd[type].cb = read_socket4;
  448. if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
  449. goto error;
  450. setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes));
  451. setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes));
  452. }
  453. uloop_fd_add(&ufd[type], ULOOP_READ);
  454. return 0;
  455. error:
  456. close(ufd[type].fd);
  457. return -1;
  458. }
  459. static void
  460. __interface_add(const char *name, enum umdns_socket_type type,
  461. struct interface_addr_list *list)
  462. {
  463. struct interface *iface;
  464. unsigned int ifindex;
  465. char *id_buf;
  466. if (interface_init_socket(type))
  467. goto error;
  468. ifindex = if_nametoindex(name);
  469. if (!ifindex)
  470. goto error;
  471. iface = calloc_a(sizeof(*iface),
  472. &id_buf, strlen(name) + 3);
  473. sprintf(id_buf, "%d_%s", type, name);
  474. iface->name = id_buf + 2;
  475. iface->ifindex = ifindex;
  476. iface->type = type;
  477. iface->addrs = *list;
  478. vlist_add(&interfaces, &iface->node, id_buf);
  479. return;
  480. error:
  481. free(list->v4);
  482. }
  483. int interface_add(const char *name)
  484. {
  485. struct ifaddrs *ifap, *ifa;
  486. struct interface_addr_list addr4 = {}, addr6 = {};
  487. getifaddrs(&ifap);
  488. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  489. if (strcmp(ifa->ifa_name, name))
  490. continue;
  491. if (ifa->ifa_addr->sa_family == AF_INET) {
  492. struct sockaddr_in *sin;
  493. if (cfg_proto && (cfg_proto != 4))
  494. continue;
  495. addr4.v4 = realloc(addr4.v4, (addr4.n_addr + 1) * sizeof(*addr4.v4));
  496. sin = (struct sockaddr_in *) ifa->ifa_addr;
  497. addr4.v4[addr4.n_addr].addr = sin->sin_addr;
  498. sin = (struct sockaddr_in *) ifa->ifa_netmask;
  499. addr4.v4[addr4.n_addr++].mask = sin->sin_addr;
  500. }
  501. if (ifa->ifa_addr->sa_family == AF_INET6) {
  502. uint8_t ll_prefix[] = {0xfe, 0x80 };
  503. struct sockaddr_in6 *sin6;
  504. if (cfg_proto && (cfg_proto != 6))
  505. continue;
  506. sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  507. if (memcmp(&sin6->sin6_addr, &ll_prefix, 2))
  508. continue;
  509. addr6.v6 = realloc(addr6.v6, (addr6.n_addr + 1) * sizeof(*addr6.v6));
  510. sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  511. addr6.v6[addr6.n_addr].addr = sin6->sin6_addr;
  512. sin6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
  513. addr6.v6[addr6.n_addr++].mask = sin6->sin6_addr;
  514. }
  515. }
  516. freeifaddrs(ifap);
  517. if (addr4.n_addr) {
  518. size_t addr_size = addr4.n_addr * sizeof(*addr4.v4);
  519. void *addr_dup = malloc(addr_size);
  520. memcpy(addr_dup, addr4.v4, addr_size);
  521. __interface_add(name, SOCK_UC_IPV4, &addr4);
  522. addr4.v4 = addr_dup;
  523. __interface_add(name, SOCK_MC_IPV4, &addr4);
  524. }
  525. if (addr6.n_addr) {
  526. size_t addr_size = addr6.n_addr * sizeof(*addr6.v6);
  527. void *addr_dup = malloc(addr_size);
  528. memcpy(addr_dup, addr6.v6, addr_size);
  529. __interface_add(name, SOCK_UC_IPV6, &addr6);
  530. addr6.v6 = addr_dup;
  531. __interface_add(name, SOCK_MC_IPV6, &addr6);
  532. }
  533. return !addr4.n_addr && !addr6.n_addr;
  534. }
  535. void interface_shutdown(void)
  536. {
  537. struct interface *iface;
  538. vlist_for_each_element(&interfaces, iface, node)
  539. if (interface_multicast(iface)) {
  540. dns_reply_a(iface, NULL, 0);
  541. service_announce_services(iface, NULL, 0);
  542. }
  543. for (size_t i = 0; i < ARRAY_SIZE(ufd); i++) {
  544. uloop_fd_delete(&ufd[i]);
  545. close(ufd[i].fd);
  546. ufd[i].fd = -1;
  547. }
  548. }
  549. struct interface *interface_get(const char *name, enum umdns_socket_type type)
  550. {
  551. char id_buf[32];
  552. snprintf(id_buf, sizeof(id_buf), "%d_%s", type, name);
  553. struct interface *iface = vlist_find(&interfaces, id_buf, iface, node);
  554. return iface;
  555. }
  556. VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, true, false);