dns.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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)
  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, mdns_hostname_local);
  174. freeifaddrs(ifap);
  175. }
  176. static int
  177. scan_name(const uint8_t *buffer, int len)
  178. {
  179. int offset = 0;
  180. while (len && (*buffer != '\0')) {
  181. int l = *buffer;
  182. if (IS_COMPRESSED(l))
  183. return offset + 2;
  184. if (l + 1 > len) return -1;
  185. len -= l + 1;
  186. offset += l + 1;
  187. buffer += l + 1;
  188. }
  189. if (!len || !offset || (*buffer != '\0'))
  190. return -1;
  191. return offset + 1;
  192. }
  193. static struct dns_header*
  194. dns_consume_header(uint8_t **data, int *len)
  195. {
  196. struct dns_header *h = (struct dns_header *) *data;
  197. if (*len < sizeof(struct dns_header))
  198. return NULL;
  199. h->id = be16_to_cpu(h->id);
  200. h->flags = be16_to_cpu(h->flags);
  201. h->questions = be16_to_cpu(h->questions);
  202. h->answers = be16_to_cpu(h->answers);
  203. h->authority = be16_to_cpu(h->authority);
  204. h->additional = be16_to_cpu(h->additional);
  205. *len -= sizeof(struct dns_header);
  206. *data += sizeof(struct dns_header);
  207. return h;
  208. }
  209. static struct dns_question*
  210. dns_consume_question(uint8_t **data, int *len)
  211. {
  212. struct dns_question *q = (struct dns_question *) *data;
  213. if (*len < sizeof(struct dns_question))
  214. return NULL;
  215. q->type = be16_to_cpu(q->type);
  216. q->class = be16_to_cpu(q->class);
  217. *len -= sizeof(struct dns_question);
  218. *data += sizeof(struct dns_question);
  219. return q;
  220. }
  221. static struct dns_answer*
  222. dns_consume_answer(uint8_t **data, int *len)
  223. {
  224. struct dns_answer *a = (struct dns_answer *) *data;
  225. if (*len < sizeof(struct dns_answer))
  226. return NULL;
  227. a->type = be16_to_cpu(a->type);
  228. a->class = be16_to_cpu(a->class);
  229. a->ttl = be32_to_cpu(a->ttl);
  230. a->rdlength = be16_to_cpu(a->rdlength);
  231. *len -= sizeof(struct dns_answer);
  232. *data += sizeof(struct dns_answer);
  233. return a;
  234. }
  235. static char *
  236. dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
  237. {
  238. int nlen = scan_name(*data, *len);
  239. if (nlen < 1)
  240. return NULL;
  241. if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
  242. perror("dns_consume_name/dn_expand");
  243. return NULL;
  244. }
  245. *len -= nlen;
  246. *data += nlen;
  247. return name_buffer;
  248. }
  249. static int parse_answer(struct interface *iface, struct sockaddr *from,
  250. uint8_t *buffer, int len, uint8_t **b, int *rlen,
  251. int cache)
  252. {
  253. char *name = dns_consume_name(buffer, len, b, rlen);
  254. struct dns_answer *a;
  255. uint8_t *rdata;
  256. if (!name || *rlen < 0) {
  257. fprintf(stderr, "dropping: bad question\n");
  258. return -1;
  259. }
  260. a = dns_consume_answer(b, rlen);
  261. if (!a) {
  262. fprintf(stderr, "dropping: bad question\n");
  263. return -1;
  264. }
  265. if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
  266. return -1;
  267. rdata = *b;
  268. if (a->rdlength > *rlen) {
  269. fprintf(stderr, "dropping: bad question\n");
  270. return -1;
  271. }
  272. *rlen -= a->rdlength;
  273. *b += a->rdlength;
  274. if (cache)
  275. cache_answer(iface, from, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
  276. return 0;
  277. }
  278. static void
  279. parse_question(struct interface *iface, struct sockaddr *from, char *name, struct dns_question *q)
  280. {
  281. struct sockaddr *to = NULL;
  282. char *host;
  283. /* TODO: Multicast if more than one quarter of TTL has passed */
  284. if (q->class & CLASS_UNICAST) {
  285. to = from;
  286. if (interface_multicast(iface))
  287. iface = interface_get(iface->name, iface->type | SOCKTYPE_BIT_UNICAST);
  288. }
  289. DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
  290. switch (q->type) {
  291. case TYPE_ANY:
  292. if (!strcmp(name, mdns_hostname_local)) {
  293. dns_reply_a(iface, to, announce_ttl);
  294. service_reply(iface, to, NULL, NULL, announce_ttl);
  295. }
  296. break;
  297. case TYPE_PTR:
  298. if (!strcmp(name, C_DNS_SD)) {
  299. dns_reply_a(iface, to, announce_ttl);
  300. service_announce_services(iface, to, announce_ttl);
  301. } else {
  302. if (name[0] == '_') {
  303. service_reply(iface, to, NULL, name, announce_ttl);
  304. } else {
  305. /* First dot separates instance name from the rest */
  306. char *dot = strchr(name, '.');
  307. if (dot) {
  308. *dot = '\0';
  309. service_reply(iface, to, name, dot + 1, announce_ttl);
  310. *dot = '.';
  311. }
  312. }
  313. }
  314. break;
  315. case TYPE_AAAA:
  316. case TYPE_A:
  317. host = strstr(name, ".local");
  318. if (host)
  319. *host = '\0';
  320. if (!strcmp(umdns_host_label, name))
  321. dns_reply_a(iface, to, announce_ttl);
  322. break;
  323. };
  324. }
  325. void
  326. dns_handle_packet(struct interface *iface, struct sockaddr *from, uint16_t port, uint8_t *buffer, int len)
  327. {
  328. struct dns_header *h;
  329. uint8_t *b = buffer;
  330. int rlen = len;
  331. h = dns_consume_header(&b, &rlen);
  332. if (!h) {
  333. fprintf(stderr, "dropping: bad header\n");
  334. return;
  335. }
  336. if (h->questions && !interface_multicast(iface) && port != MCAST_PORT)
  337. /* silently drop unicast questions that dont originate from port 5353 */
  338. return;
  339. while (h->questions-- > 0) {
  340. char *name = dns_consume_name(buffer, len, &b, &rlen);
  341. struct dns_question *q;
  342. if (!name || rlen < 0) {
  343. fprintf(stderr, "dropping: bad name\n");
  344. return;
  345. }
  346. q = dns_consume_question(&b, &rlen);
  347. if (!q) {
  348. fprintf(stderr, "dropping: bad question\n");
  349. return;
  350. }
  351. if (!(h->flags & FLAG_RESPONSE))
  352. parse_question(iface, from, name, q);
  353. }
  354. if (!(h->flags & FLAG_RESPONSE))
  355. return;
  356. while (h->answers-- > 0)
  357. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  358. return;
  359. while (h->authority-- > 0)
  360. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  361. return;
  362. while (h->additional-- > 0)
  363. if (parse_answer(iface, from, buffer, len, &b, &rlen, 1))
  364. return;
  365. }