dns.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <time.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <asm/byteorder.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. const char*
  39. dns_type_string(uint16_t type)
  40. {
  41. switch (type) {
  42. case TYPE_A:
  43. return "A";
  44. case TYPE_AAAA:
  45. return "AAAA";
  46. case TYPE_PTR:
  47. return "PTR";
  48. case TYPE_TXT:
  49. return "TXT";
  50. case TYPE_SRV:
  51. return "SRV";
  52. case TYPE_ANY:
  53. return "ANY";
  54. }
  55. return "N/A";
  56. }
  57. void
  58. dns_send_question(struct interface *iface, const char *question, int type)
  59. {
  60. static struct dns_header h = {
  61. .questions = cpu_to_be16(1),
  62. };
  63. static struct dns_question q = {
  64. .class = cpu_to_be16(1),
  65. };
  66. static struct iovec iov[] = {
  67. {
  68. .iov_base = &h,
  69. .iov_len = sizeof(h),
  70. },
  71. {
  72. .iov_base = name_buffer,
  73. },
  74. {
  75. .iov_base = &q,
  76. .iov_len = sizeof(q),
  77. }
  78. };
  79. int len;
  80. q.type = __cpu_to_be16(type);
  81. len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
  82. if (len < 1)
  83. return;
  84. iov[1].iov_len = len;
  85. if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
  86. fprintf(stderr, "failed to send question\n");
  87. else
  88. DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
  89. }
  90. struct dns_reply {
  91. int type;
  92. struct dns_answer a;
  93. uint16_t rdlength;
  94. uint8_t *rdata;
  95. char *buffer;
  96. };
  97. #define MAX_ANSWER 8
  98. static struct dns_reply dns_reply[1 + (MAX_ANSWER * 3)];
  99. static int dns_answer_cnt;
  100. void
  101. dns_init_answer(void)
  102. {
  103. dns_answer_cnt = 0;
  104. }
  105. void
  106. dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
  107. {
  108. struct dns_reply *a = &dns_reply[dns_answer_cnt];
  109. if (dns_answer_cnt == MAX_ANSWER)
  110. return;
  111. a->rdata = memdup(rdata, rdlength);
  112. a->type = type;
  113. a->rdlength = rdlength;
  114. dns_answer_cnt++;
  115. }
  116. void
  117. dns_send_answer(struct interface *iface, const char *answer)
  118. {
  119. uint8_t buffer[256];
  120. struct dns_header h = { 0 };
  121. struct iovec *iov;
  122. int len, i;
  123. if (!dns_answer_cnt)
  124. return;
  125. h.answers = __cpu_to_be16(dns_answer_cnt);
  126. h.flags = __cpu_to_be16(0x8400);
  127. iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
  128. iov[0].iov_base = &h;
  129. iov[0].iov_len = sizeof(struct dns_header);
  130. for (i = 0; i < dns_answer_cnt; i++) {
  131. struct dns_answer *a = &dns_reply[i].a;
  132. int id = (i * 3) + 1;
  133. memset(a, 0, sizeof(*a));
  134. a->type = __cpu_to_be16(dns_reply[i].type);
  135. a->class = __cpu_to_be16(1);
  136. a->ttl = __cpu_to_be32(announce_ttl);
  137. a->rdlength = __cpu_to_be16(dns_reply[i].rdlength);
  138. len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
  139. if (len < 1)
  140. return;
  141. dns_reply[i].buffer = iov[id].iov_base = memdup(buffer, len);
  142. iov[id].iov_len = len;
  143. iov[id + 1].iov_base = a;
  144. iov[id + 1].iov_len = sizeof(struct dns_answer);
  145. iov[id + 2].iov_base = dns_reply[i].rdata;
  146. iov[id + 2].iov_len = dns_reply[i].rdlength;
  147. DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
  148. }
  149. if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
  150. fprintf(stderr, "failed to send question\n");
  151. for (i = 0; i < dns_answer_cnt; i++) {
  152. free(dns_reply[i].buffer);
  153. free(dns_reply[i].rdata);
  154. }
  155. dns_answer_cnt = 0;
  156. }
  157. static int
  158. scan_name(const uint8_t *buffer, int len)
  159. {
  160. int offset = 0;
  161. while (len && (*buffer != '\0')) {
  162. int l = *buffer;
  163. if (IS_COMPRESSED(l))
  164. return offset + 2;
  165. len -= l + 1;
  166. offset += l + 1;
  167. buffer += l + 1;
  168. }
  169. if (!len || !offset || (*buffer != '\0'))
  170. return -1;
  171. return offset + 1;
  172. }
  173. static struct dns_header*
  174. dns_consume_header(uint8_t **data, int *len)
  175. {
  176. struct dns_header *h = (struct dns_header *) *data;
  177. uint16_t *swap = (uint16_t *) h;
  178. int endianess = 6;
  179. if (*len < sizeof(struct dns_header))
  180. return NULL;
  181. while (endianess--) {
  182. *swap = __be16_to_cpu(*swap);
  183. swap++;
  184. }
  185. *len -= sizeof(struct dns_header);
  186. *data += sizeof(struct dns_header);
  187. return h;
  188. }
  189. static struct dns_question*
  190. dns_consume_question(uint8_t **data, int *len)
  191. {
  192. struct dns_question *q = (struct dns_question *) *data;
  193. uint16_t *swap = (uint16_t *) q;
  194. int endianess = 2;
  195. if (*len < sizeof(struct dns_question))
  196. return NULL;
  197. while (endianess--) {
  198. *swap = __be16_to_cpu(*swap);
  199. swap++;
  200. }
  201. *len -= sizeof(struct dns_question);
  202. *data += sizeof(struct dns_question);
  203. return q;
  204. }
  205. static struct dns_answer*
  206. dns_consume_answer(uint8_t **data, int *len)
  207. {
  208. struct dns_answer *a = (struct dns_answer *) *data;
  209. if (*len < sizeof(struct dns_answer))
  210. return NULL;
  211. a->type = __be16_to_cpu(a->type);
  212. a->class = __be16_to_cpu(a->class);
  213. a->ttl = __be32_to_cpu(a->ttl);
  214. a->rdlength = __be16_to_cpu(a->rdlength);
  215. *len -= sizeof(struct dns_answer);
  216. *data += sizeof(struct dns_answer);
  217. return a;
  218. }
  219. static char *
  220. dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
  221. {
  222. int nlen = scan_name(*data, *len);
  223. if (nlen < 1)
  224. return NULL;
  225. if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
  226. perror("dns_consume_name/dn_expand");
  227. return NULL;
  228. }
  229. *len -= nlen;
  230. *data += nlen;
  231. return name_buffer;
  232. }
  233. static int
  234. parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
  235. {
  236. char *name = dns_consume_name(buffer, len, b, rlen);
  237. struct dns_answer *a;
  238. uint8_t *rdata;
  239. if (!name) {
  240. fprintf(stderr, "dropping: bad question\n");
  241. return -1;
  242. }
  243. a = dns_consume_answer(b, rlen);
  244. if (!a) {
  245. fprintf(stderr, "dropping: bad question\n");
  246. return -1;
  247. }
  248. rdata = *b;
  249. if (a->rdlength > *rlen) {
  250. fprintf(stderr, "dropping: bad question\n");
  251. return -1;
  252. }
  253. *rlen -= a->rdlength;
  254. *b += a->rdlength;
  255. if (cache)
  256. cache_answer(iface, buffer, len, name, a, rdata);
  257. return 0;
  258. }
  259. static void
  260. parse_question(struct interface *iface, char *name, struct dns_question *q)
  261. {
  262. char *host;
  263. DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
  264. switch (q->type) {
  265. case TYPE_ANY:
  266. host = service_name("local");
  267. if (!strcmp(name, host))
  268. service_reply(iface, NULL);
  269. break;
  270. case TYPE_PTR:
  271. service_announce_services(iface, name);
  272. service_reply(iface, name);
  273. break;
  274. case TYPE_AAAA:
  275. case TYPE_A:
  276. host = strstr(name, ".local");
  277. if (host)
  278. *host = '\0';
  279. if (!strcmp(hostname, name))
  280. service_reply_a(iface, q->type);
  281. break;
  282. };
  283. }
  284. void
  285. dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
  286. {
  287. struct dns_header *h;
  288. uint8_t *b = buffer;
  289. int rlen = len;
  290. h = dns_consume_header(&b, &rlen);
  291. if (!h) {
  292. fprintf(stderr, "dropping: bad header\n");
  293. return;
  294. }
  295. while (h->questions-- > 0) {
  296. char *name = dns_consume_name(buffer, len, &b, &rlen);
  297. struct dns_question *q;
  298. if (!name) {
  299. fprintf(stderr, "dropping: bad name\n");
  300. return;
  301. }
  302. q = dns_consume_question(&b, &rlen);
  303. if (!q) {
  304. fprintf(stderr, "dropping: bad question\n");
  305. return;
  306. }
  307. if (!(h->flags & FLAG_RESPONSE))
  308. parse_question(iface, name, q);
  309. }
  310. if (!(h->flags & FLAG_RESPONSE))
  311. return;
  312. while (h->answers-- > 0)
  313. parse_answer(iface, buffer, len, &b, &rlen, 1);
  314. while (h->authority-- > 0)
  315. parse_answer(iface, buffer, len, &b, &rlen, 0);
  316. while (h->additional-- > 0)
  317. parse_answer(iface, buffer, len, &b, &rlen, 1);
  318. }