dns.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <ifaddrs.h>
  17. #include <time.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <arpa/nameser.h>
  25. #include <resolv.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libubox/uloop.h>
  29. #include <libubox/usock.h>
  30. #include <libubox/utils.h>
  31. #include "announce.h"
  32. #include "util.h"
  33. #include "dns.h"
  34. #include "cache.h"
  35. #include "service.h"
  36. #include "interface.h"
  37. static char name_buffer[MAX_NAME_LEN + 1];
  38. static char dns_buffer[MAX_NAME_LEN];
  39. static struct blob_buf ans_buf;
  40. const char*
  41. dns_type_string(uint16_t type)
  42. {
  43. static const struct {
  44. uint16_t type;
  45. char str[5];
  46. } type_str[] = {
  47. { TYPE_A, "A" },
  48. { TYPE_AAAA, "AAAA" },
  49. { TYPE_PTR, "PTR" },
  50. { TYPE_TXT, "TXT" },
  51. { TYPE_SRV, "SRV" },
  52. { TYPE_ANY, "ANY" },
  53. };
  54. int i;
  55. for (i = 0; i < ARRAY_SIZE(type_str); i++) {
  56. if (type == type_str[i].type)
  57. return type_str[i].str;
  58. }
  59. return "N/A";
  60. }
  61. void
  62. dns_send_question(struct interface *iface, struct sockaddr *to,
  63. const char *question, int type, int multicast)
  64. {
  65. static struct dns_header h;
  66. static struct dns_question q;
  67. static struct iovec iov[] = {
  68. {
  69. .iov_base = &h,
  70. .iov_len = sizeof(h),
  71. },
  72. {
  73. .iov_base = dns_buffer,
  74. },
  75. {
  76. .iov_base = &q,
  77. .iov_len = sizeof(q),
  78. }
  79. };
  80. int len;
  81. h.questions = cpu_to_be16(1);
  82. q.class = cpu_to_be16((multicast ? 0 : CLASS_UNICAST) | 1);
  83. q.type = cpu_to_be16(type);
  84. len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
  85. if (len < 1)
  86. return;
  87. iov[1].iov_len = len;
  88. DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
  89. if (interface_send_packet(iface, to, iov, ARRAY_SIZE(iov)) < 0)
  90. perror("failed to send question");
  91. }
  92. struct dns_reply {
  93. int type;
  94. struct dns_answer a;
  95. uint16_t rdlength;
  96. uint8_t *rdata;
  97. char *buffer;
  98. };
  99. static int dns_answer_cnt;
  100. void
  101. dns_init_answer(void)
  102. {
  103. dns_answer_cnt = 0;
  104. blob_buf_init(&ans_buf, 0);
  105. }
  106. void
  107. dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
  108. {
  109. struct blob_attr *attr;
  110. struct dns_answer *a;
  111. attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
  112. a = blob_data(attr);
  113. a->type = cpu_to_be16(type);
  114. a->class = cpu_to_be16(1);
  115. a->ttl = cpu_to_be32(ttl);
  116. a->rdlength = cpu_to_be16(rdlength);
  117. memcpy(a + 1, rdata, rdlength);
  118. dns_answer_cnt++;
  119. }
  120. void
  121. dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer)
  122. {
  123. uint8_t buffer[256];
  124. struct blob_attr *attr;
  125. struct dns_header h = { 0 };
  126. struct iovec *iov;
  127. int answer_len, rem;
  128. int n_iov = 0;
  129. if (!dns_answer_cnt)
  130. return;
  131. h.answers = cpu_to_be16(dns_answer_cnt);
  132. h.flags = cpu_to_be16(0x8400);
  133. iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
  134. iov[n_iov].iov_base = &h;
  135. iov[n_iov].iov_len = sizeof(struct dns_header);
  136. n_iov++;
  137. answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
  138. if (answer_len < 1)
  139. return;
  140. blob_for_each_attr(attr, ans_buf.head, rem) {
  141. struct dns_answer *a = blob_data(attr);
  142. iov[n_iov].iov_base = buffer;
  143. iov[n_iov].iov_len = answer_len;
  144. n_iov++;
  145. iov[n_iov].iov_base = blob_data(attr);
  146. iov[n_iov].iov_len = blob_len(attr);
  147. n_iov++;
  148. DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
  149. }
  150. if (interface_send_packet(iface, to, iov, n_iov) < 0)
  151. perror("failed to send answer");
  152. }
  153. void
  154. dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl, const char *hostname)
  155. {
  156. struct ifaddrs *ifap, *ifa;
  157. struct sockaddr_in *sa;
  158. struct sockaddr_in6 *sa6;
  159. getifaddrs(&ifap);
  160. dns_init_answer();
  161. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  162. if (strcmp(ifa->ifa_name, iface->name))
  163. continue;
  164. if (ifa->ifa_addr->sa_family == AF_INET) {
  165. sa = (struct sockaddr_in *) ifa->ifa_addr;
  166. dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
  167. }
  168. if (ifa->ifa_addr->sa_family == AF_INET6) {
  169. sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  170. dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
  171. }
  172. }
  173. dns_send_answer(iface, to, hostname ? hostname : mdns_hostname_local);
  174. freeifaddrs(ifap);
  175. }
  176. void
  177. dns_reply_a_additional(struct interface *iface, struct sockaddr *to, int ttl)
  178. {
  179. struct hostname *h;
  180. vlist_for_each_element(&hostnames, h, node)
  181. dns_reply_a(iface, to, ttl, h->hostname);
  182. }
  183. static int
  184. scan_name(const uint8_t *buffer, int len)
  185. {
  186. int offset = 0;
  187. while (len && (*buffer != '\0')) {
  188. int l = *buffer;
  189. if (IS_COMPRESSED(l))
  190. return offset + 2;
  191. if (l + 1 > len) return -1;
  192. len -= l + 1;
  193. offset += l + 1;
  194. buffer += l + 1;
  195. }
  196. if (!len || !offset || (*buffer != '\0'))
  197. return -1;
  198. return offset + 1;
  199. }
  200. static struct dns_header*
  201. dns_consume_header(uint8_t **data, int *len)
  202. {
  203. struct dns_header *h = (struct dns_header *) *data;
  204. if (*len < sizeof(struct dns_header))
  205. return NULL;
  206. h->id = be16_to_cpu(h->id);
  207. h->flags = be16_to_cpu(h->flags);
  208. h->questions = be16_to_cpu(h->questions);
  209. h->answers = be16_to_cpu(h->answers);
  210. h->authority = be16_to_cpu(h->authority);
  211. h->additional = be16_to_cpu(h->additional);
  212. *len -= sizeof(struct dns_header);
  213. *data += sizeof(struct dns_header);
  214. return h;
  215. }
  216. static struct dns_question*
  217. dns_consume_question(uint8_t **data, int *len)
  218. {
  219. struct dns_question *q = (struct dns_question *) *data;
  220. if (*len < sizeof(struct dns_question))
  221. return NULL;
  222. q->type = be16_to_cpu(q->type);
  223. q->class = be16_to_cpu(q->class);
  224. *len -= sizeof(struct dns_question);
  225. *data += sizeof(struct dns_question);
  226. return q;
  227. }
  228. static struct dns_answer*
  229. dns_consume_answer(uint8_t **data, int *len)
  230. {
  231. struct dns_answer *a = (struct dns_answer *) *data;
  232. if (*len < sizeof(struct dns_answer))
  233. return NULL;
  234. a->type = be16_to_cpu(a->type);
  235. a->class = be16_to_cpu(a->class);
  236. a->ttl = be32_to_cpu(a->ttl);
  237. a->rdlength = be16_to_cpu(a->rdlength);
  238. *len -= sizeof(struct dns_answer);
  239. *data += sizeof(struct dns_answer);
  240. return a;
  241. }
  242. static char *
  243. dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
  244. {
  245. int nlen = scan_name(*data, *len);
  246. if (nlen < 1)
  247. return NULL;
  248. if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
  249. perror("dns_consume_name/dn_expand");
  250. return NULL;
  251. }
  252. *len -= nlen;
  253. *data += nlen;
  254. return name_buffer;
  255. }
  256. static int parse_answer(struct interface *iface, struct sockaddr *from,
  257. uint8_t *buffer, int len, uint8_t **b, int *rlen,
  258. int cache)
  259. {
  260. char *name = dns_consume_name(buffer, len, b, rlen);
  261. struct dns_answer *a;
  262. uint8_t *rdata;
  263. if (!name || *rlen < 0) {
  264. fprintf(stderr, "dropping: bad question\n");
  265. return -1;
  266. }
  267. a = dns_consume_answer(b, rlen);
  268. if (!a) {
  269. fprintf(stderr, "dropping: bad question\n");
  270. return -1;
  271. }
  272. if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
  273. return -1;
  274. rdata = *b;
  275. if (a->rdlength > *rlen) {
  276. fprintf(stderr, "dropping: bad question\n");
  277. return -1;
  278. }
  279. *rlen -= a->rdlength;
  280. *b += a->rdlength;
  281. if (cache)
  282. cache_answer(iface, from, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
  283. return 0;
  284. }
  285. static void
  286. parse_question(struct interface *iface, struct sockaddr *from, char *name, struct dns_question *q)
  287. {
  288. struct sockaddr *to = NULL;
  289. struct hostname *h;
  290. char *host;
  291. /* TODO: Multicast if more than one quarter of TTL has passed */
  292. if (q->class & CLASS_UNICAST) {
  293. to = from;
  294. if (interface_multicast(iface))
  295. iface = interface_get(iface->name, iface->type | SOCKTYPE_BIT_UNICAST);
  296. }
  297. DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
  298. switch (q->type) {
  299. case TYPE_ANY:
  300. if (!strcmp(name, mdns_hostname_local)) {
  301. dns_reply_a(iface, to, announce_ttl, NULL);
  302. dns_reply_a_additional(iface, to, announce_ttl);
  303. service_reply(iface, to, NULL, NULL, announce_ttl);
  304. }
  305. break;
  306. case TYPE_PTR:
  307. if (!strcmp(name, C_DNS_SD)) {
  308. dns_reply_a(iface, to, announce_ttl, NULL);
  309. dns_reply_a_additional(iface, to, announce_ttl);
  310. service_announce_services(iface, to, announce_ttl);
  311. } else {
  312. if (name[0] == '_') {
  313. service_reply(iface, to, NULL, name, announce_ttl);
  314. } else {
  315. /* First dot separates instance name from the rest */
  316. char *dot = strchr(name, '.');
  317. if (dot) {
  318. *dot = '\0';
  319. service_reply(iface, to, name, dot + 1, announce_ttl);
  320. *dot = '.';
  321. }
  322. }
  323. }
  324. break;
  325. case TYPE_AAAA:
  326. case TYPE_A:
  327. host = strstr(name, ".local");
  328. if (host)
  329. *host = '\0';
  330. if (!strcmp(umdns_host_label, name)) {
  331. dns_reply_a(iface, to, announce_ttl, NULL);
  332. } else {
  333. if (host)
  334. *host = '.';
  335. vlist_for_each_element(&hostnames, h, node)
  336. if (!strcmp(h->hostname, name))
  337. dns_reply_a(iface, to, announce_ttl, h->hostname);
  338. }
  339. break;
  340. };
  341. }
  342. void
  343. dns_handle_packet(struct interface *iface, struct sockaddr *from, uint16_t port, uint8_t *buffer, int len)
  344. {
  345. struct dns_header *h;
  346. uint8_t *b = buffer;
  347. int rlen = len;
  348. h = dns_consume_header(&b, &rlen);
  349. if (!h) {
  350. fprintf(stderr, "dropping: bad header\n");
  351. return;
  352. }
  353. if (h->questions && !interface_multicast(iface) && port != MCAST_PORT)
  354. /* silently drop unicast questions that dont originate from port 5353 */
  355. return;
  356. while (h->questions-- > 0) {
  357. char *name = dns_consume_name(buffer, len, &b, &rlen);
  358. struct dns_question *q;
  359. if (!name || rlen < 0) {
  360. fprintf(stderr, "dropping: bad name\n");
  361. return;
  362. }
  363. q = dns_consume_question(&b, &rlen);
  364. if (!q) {
  365. fprintf(stderr, "dropping: bad question\n");
  366. return;
  367. }
  368. if (!(h->flags & FLAG_RESPONSE))
  369. parse_question(iface, from, name, q);
  370. }
  371. if (!(h->flags & FLAG_RESPONSE))
  372. return;
  373. while (h->answers-- > 0)
  374. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  375. return;
  376. while (h->authority-- > 0)
  377. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  378. return;
  379. while (h->additional-- > 0)
  380. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  381. return;
  382. }