dns.c 9.6 KB

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