dns.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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, const char *question, int type, int multicast)
  63. {
  64. static struct dns_header h;
  65. static struct dns_question q;
  66. static struct iovec iov[] = {
  67. {
  68. .iov_base = &h,
  69. .iov_len = sizeof(h),
  70. },
  71. {
  72. .iov_base = dns_buffer,
  73. },
  74. {
  75. .iov_base = &q,
  76. .iov_len = sizeof(q),
  77. }
  78. };
  79. int len;
  80. h.questions = cpu_to_be16(1);
  81. q.class = cpu_to_be16((multicast ? 0 : CLASS_UNICAST) | 1);
  82. q.type = cpu_to_be16(type);
  83. len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
  84. if (len < 1)
  85. return;
  86. iov[1].iov_len = len;
  87. DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
  88. if (interface_send_packet(iface, NULL, iov, ARRAY_SIZE(iov)) < 0)
  89. perror("failed to send question :");
  90. }
  91. struct dns_reply {
  92. int type;
  93. struct dns_answer a;
  94. uint16_t rdlength;
  95. uint8_t *rdata;
  96. char *buffer;
  97. };
  98. static int dns_answer_cnt;
  99. void
  100. dns_init_answer(void)
  101. {
  102. dns_answer_cnt = 0;
  103. blob_buf_init(&ans_buf, 0);
  104. }
  105. void
  106. dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
  107. {
  108. struct blob_attr *attr;
  109. struct dns_answer *a;
  110. attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
  111. a = blob_data(attr);
  112. a->type = cpu_to_be16(type);
  113. a->class = cpu_to_be16(1);
  114. a->ttl = cpu_to_be32(ttl);
  115. a->rdlength = cpu_to_be16(rdlength);
  116. memcpy(a + 1, rdata, rdlength);
  117. dns_answer_cnt++;
  118. }
  119. void
  120. dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer)
  121. {
  122. uint8_t buffer[256];
  123. struct blob_attr *attr;
  124. struct dns_header h = { 0 };
  125. struct iovec *iov;
  126. int answer_len, rem;
  127. int n_iov = 0;
  128. if (!dns_answer_cnt)
  129. return;
  130. h.answers = cpu_to_be16(dns_answer_cnt);
  131. h.flags = cpu_to_be16(0x8400);
  132. iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
  133. iov[n_iov].iov_base = &h;
  134. iov[n_iov].iov_len = sizeof(struct dns_header);
  135. n_iov++;
  136. answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
  137. if (answer_len < 1)
  138. return;
  139. blob_for_each_attr(attr, ans_buf.head, rem) {
  140. struct dns_answer *a = blob_data(attr);
  141. iov[n_iov].iov_base = buffer;
  142. iov[n_iov].iov_len = answer_len;
  143. n_iov++;
  144. iov[n_iov].iov_base = blob_data(attr);
  145. iov[n_iov].iov_len = blob_len(attr);
  146. n_iov++;
  147. DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
  148. }
  149. if (interface_send_packet(iface, to, iov, n_iov) < 0)
  150. fprintf(stderr, "failed to send question\n");
  151. }
  152. void
  153. dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl)
  154. {
  155. struct ifaddrs *ifap, *ifa;
  156. struct sockaddr_in *sa;
  157. struct sockaddr_in6 *sa6;
  158. getifaddrs(&ifap);
  159. dns_init_answer();
  160. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  161. if (strcmp(ifa->ifa_name, iface->name))
  162. continue;
  163. if (ifa->ifa_addr->sa_family == AF_INET) {
  164. sa = (struct sockaddr_in *) ifa->ifa_addr;
  165. dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
  166. }
  167. if (ifa->ifa_addr->sa_family == AF_INET6) {
  168. uint8_t ll_prefix[] = {0xfe, 0x80 };
  169. sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  170. if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
  171. dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
  172. }
  173. }
  174. dns_send_answer(iface, to, mdns_hostname_local);
  175. freeifaddrs(ifap);
  176. }
  177. static int
  178. scan_name(const uint8_t *buffer, int len)
  179. {
  180. int offset = 0;
  181. while (len && (*buffer != '\0')) {
  182. int l = *buffer;
  183. if (IS_COMPRESSED(l))
  184. return offset + 2;
  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. uint16_t *swap = (uint16_t *) h;
  198. int endianess = 6;
  199. if (*len < sizeof(struct dns_header))
  200. return NULL;
  201. while (endianess--) {
  202. *swap = be16_to_cpu(*swap);
  203. swap++;
  204. }
  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. uint16_t *swap = (uint16_t *) q;
  214. int endianess = 2;
  215. if (*len < sizeof(struct dns_question))
  216. return NULL;
  217. while (endianess--) {
  218. *swap = be16_to_cpu(*swap);
  219. swap++;
  220. }
  221. *len -= sizeof(struct dns_question);
  222. *data += sizeof(struct dns_question);
  223. return q;
  224. }
  225. static struct dns_answer*
  226. dns_consume_answer(uint8_t **data, int *len)
  227. {
  228. struct dns_answer *a = (struct dns_answer *) *data;
  229. if (*len < sizeof(struct dns_answer))
  230. return NULL;
  231. a->type = be16_to_cpu(a->type);
  232. a->class = be16_to_cpu(a->class);
  233. a->ttl = be32_to_cpu(a->ttl);
  234. a->rdlength = be16_to_cpu(a->rdlength);
  235. *len -= sizeof(struct dns_answer);
  236. *data += sizeof(struct dns_answer);
  237. return a;
  238. }
  239. static char *
  240. dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
  241. {
  242. int nlen = scan_name(*data, *len);
  243. if (nlen < 1)
  244. return NULL;
  245. if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
  246. perror("dns_consume_name/dn_expand");
  247. return NULL;
  248. }
  249. *len -= nlen;
  250. *data += nlen;
  251. return name_buffer;
  252. }
  253. static int
  254. parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
  255. {
  256. char *name = dns_consume_name(buffer, len, b, rlen);
  257. struct dns_answer *a;
  258. uint8_t *rdata;
  259. if (!name) {
  260. fprintf(stderr, "dropping: bad question\n");
  261. return -1;
  262. }
  263. a = dns_consume_answer(b, rlen);
  264. if (!a) {
  265. fprintf(stderr, "dropping: bad question\n");
  266. return -1;
  267. }
  268. if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
  269. return -1;
  270. rdata = *b;
  271. if (a->rdlength > *rlen) {
  272. fprintf(stderr, "dropping: bad question\n");
  273. return -1;
  274. }
  275. *rlen -= a->rdlength;
  276. *b += a->rdlength;
  277. if (cache)
  278. cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
  279. return 0;
  280. }
  281. static void
  282. parse_question(struct interface *iface, char *name, struct dns_question *q)
  283. {
  284. char *host;
  285. if ((q->class & CLASS_UNICAST) && iface->multicast)
  286. iface = iface->peer;
  287. DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
  288. switch (q->type) {
  289. case TYPE_ANY:
  290. if (!strcmp(name, mdns_hostname_local)) {
  291. service_reply(iface, NULL, announce_ttl);
  292. dns_reply_a(iface, NULL, announce_ttl);
  293. }
  294. break;
  295. case TYPE_PTR:
  296. if (!strcmp(name, sdudp)) {
  297. service_announce_services(iface, announce_ttl);
  298. } else {
  299. /* First dot separates instance name from the rest */
  300. char *dot = strchr(name, '.');
  301. /* Length of queried instance */
  302. size_t len = dot ? dot - name : 0;
  303. /* Make sure it's query for the instance name we use */
  304. if (len && len == strlen(mdns_hostname) &&
  305. !strncmp(name, mdns_hostname, len))
  306. service_reply(iface, dot + 1, announce_ttl);
  307. }
  308. break;
  309. case TYPE_AAAA:
  310. case TYPE_A:
  311. host = strstr(name, ".local");
  312. if (host)
  313. *host = '\0';
  314. if (!strcmp(mdns_hostname, name))
  315. dns_reply_a(iface, NULL, announce_ttl);
  316. break;
  317. };
  318. }
  319. void
  320. dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
  321. {
  322. struct dns_header *h;
  323. uint8_t *b = buffer;
  324. int rlen = len;
  325. h = dns_consume_header(&b, &rlen);
  326. if (!h) {
  327. fprintf(stderr, "dropping: bad header\n");
  328. return;
  329. }
  330. if (h->questions && !iface->multicast && port != 5353)
  331. /* silently drop unicast questions that dont originate from port 5353 */
  332. return;
  333. while (h->questions-- > 0) {
  334. char *name = dns_consume_name(buffer, len, &b, &rlen);
  335. struct dns_question *q;
  336. if (!name) {
  337. fprintf(stderr, "dropping: bad name\n");
  338. return;
  339. }
  340. q = dns_consume_question(&b, &rlen);
  341. if (!q) {
  342. fprintf(stderr, "dropping: bad question\n");
  343. return;
  344. }
  345. if (!(h->flags & FLAG_RESPONSE))
  346. parse_question(iface, name, q);
  347. }
  348. if (!(h->flags & FLAG_RESPONSE))
  349. return;
  350. while (h->answers-- > 0)
  351. if (parse_answer(iface, buffer, len, &b, &rlen, 1))
  352. return;
  353. while (h->authority-- > 0)
  354. if (parse_answer(iface, buffer, len, &b, &rlen, 1))
  355. return;
  356. while (h->additional-- > 0)
  357. if (parse_answer(iface, buffer, len, &b, &rlen, 1))
  358. return;
  359. }