You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dns.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * Portions copyright (C) 2004 Anselm M. Hoffmeister
  5. * <stockholm@users.sourceforge.net>.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <byteswap.h>
  26. #include <gpxe/refcnt.h>
  27. #include <gpxe/xfer.h>
  28. #include <gpxe/open.h>
  29. #include <gpxe/resolv.h>
  30. #include <gpxe/retry.h>
  31. #include <gpxe/tcpip.h>
  32. #include <gpxe/dns.h>
  33. /** @file
  34. *
  35. * DNS protocol
  36. *
  37. */
  38. /** The DNS server */
  39. struct sockaddr_tcpip nameserver = {
  40. .st_port = htons ( DNS_PORT ),
  41. };
  42. /** A DNS request */
  43. struct dns_request {
  44. /** Reference counter */
  45. struct refcnt refcnt;
  46. /** Name resolution interface */
  47. struct resolv_interface resolv;
  48. /** Data transfer interface */
  49. struct xfer_interface socket;
  50. /** Retry timer */
  51. struct retry_timer timer;
  52. /** Socket address to fill in with resolved address */
  53. struct sockaddr sa;
  54. /** Current query packet */
  55. struct dns_query query;
  56. /** Location of query info structure within current packet
  57. *
  58. * The query info structure is located immediately after the
  59. * compressed name.
  60. */
  61. struct dns_query_info *qinfo;
  62. /** Recursion counter */
  63. unsigned int recursion;
  64. };
  65. /**
  66. * Mark DNS request as complete
  67. *
  68. * @v dns DNS request
  69. * @v rc Return status code
  70. */
  71. static void dns_done ( struct dns_request *dns, int rc ) {
  72. /* Stop the retry timer */
  73. stop_timer ( &dns->timer );
  74. /* Close data transfer interface */
  75. xfer_nullify ( &dns->socket );
  76. xfer_close ( &dns->socket, rc );
  77. /* Mark name resolution as complete */
  78. resolv_done ( &dns->resolv, &dns->sa, rc );
  79. }
  80. /**
  81. * Compare DNS reply name against the query name from the original request
  82. *
  83. * @v dns DNS request
  84. * @v reply DNS reply
  85. * @v rname Reply name
  86. * @ret zero Names match
  87. * @ret non-zero Names do not match
  88. */
  89. static int dns_name_cmp ( struct dns_request *dns,
  90. const struct dns_header *reply,
  91. const char *rname ) {
  92. const char *qname = dns->query.payload;
  93. int i;
  94. while ( 1 ) {
  95. /* Obtain next section of rname */
  96. while ( ( *rname ) & 0xc0 ) {
  97. rname = ( ( ( char * ) reply ) +
  98. ( ntohs( *((uint16_t *)rname) ) & ~0xc000 ));
  99. }
  100. /* Check that lengths match */
  101. if ( *rname != *qname )
  102. return -1;
  103. /* If length is zero, we have reached the end */
  104. if ( ! *qname )
  105. return 0;
  106. /* Check that data matches */
  107. for ( i = *qname + 1; i > 0 ; i-- ) {
  108. if ( *(rname++) != *(qname++) )
  109. return -1;
  110. }
  111. }
  112. }
  113. /**
  114. * Skip over a (possibly compressed) DNS name
  115. *
  116. * @v name DNS name
  117. * @ret name Next DNS name
  118. */
  119. static const char * dns_skip_name ( const char *name ) {
  120. while ( 1 ) {
  121. if ( ! *name ) {
  122. /* End of name */
  123. return ( name + 1);
  124. }
  125. if ( *name & 0xc0 ) {
  126. /* Start of a compressed name */
  127. return ( name + 2 );
  128. }
  129. /* Uncompressed name portion */
  130. name += *name + 1;
  131. }
  132. }
  133. /**
  134. * Find an RR in a reply packet corresponding to our query
  135. *
  136. * @v dns DNS request
  137. * @v reply DNS reply
  138. * @ret rr DNS RR, or NULL if not found
  139. */
  140. static union dns_rr_info * dns_find_rr ( struct dns_request *dns,
  141. const struct dns_header *reply ) {
  142. int i, cmp;
  143. const char *p = ( ( char * ) reply ) + sizeof ( struct dns_header );
  144. union dns_rr_info *rr_info;
  145. /* Skip over the questions section */
  146. for ( i = ntohs ( reply->qdcount ) ; i > 0 ; i-- ) {
  147. p = dns_skip_name ( p ) + sizeof ( struct dns_query_info );
  148. }
  149. /* Process the answers section */
  150. for ( i = ntohs ( reply->ancount ) ; i > 0 ; i-- ) {
  151. cmp = dns_name_cmp ( dns, reply, p );
  152. p = dns_skip_name ( p );
  153. rr_info = ( ( union dns_rr_info * ) p );
  154. if ( cmp == 0 )
  155. return rr_info;
  156. p += ( sizeof ( rr_info->common ) +
  157. ntohs ( rr_info->common.rdlength ) );
  158. }
  159. return NULL;
  160. }
  161. /**
  162. * Convert a standard NUL-terminated string to a DNS name
  163. *
  164. * @v string Name as a NUL-terminated string
  165. * @v buf Buffer in which to place DNS name
  166. * @ret next Byte following constructed DNS name
  167. *
  168. * DNS names consist of "<length>element" pairs.
  169. */
  170. static char * dns_make_name ( const char *string, char *buf ) {
  171. char *length_byte = buf++;
  172. char c;
  173. while ( ( c = *(string++) ) ) {
  174. if ( c == '.' ) {
  175. *length_byte = buf - length_byte - 1;
  176. length_byte = buf;
  177. }
  178. *(buf++) = c;
  179. }
  180. *length_byte = buf - length_byte - 1;
  181. *(buf++) = '\0';
  182. return buf;
  183. }
  184. /**
  185. * Convert an uncompressed DNS name to a NUL-terminated string
  186. *
  187. * @v name DNS name
  188. * @ret string NUL-terminated string
  189. *
  190. * Produce a printable version of a DNS name. Used only for debugging.
  191. */
  192. static inline char * dns_unmake_name ( char *name ) {
  193. char *p;
  194. unsigned int len;
  195. p = name;
  196. while ( ( len = *p ) ) {
  197. *(p++) = '.';
  198. p += len;
  199. }
  200. return name + 1;
  201. }
  202. /**
  203. * Decompress a DNS name
  204. *
  205. * @v reply DNS replay
  206. * @v name DNS name
  207. * @v buf Buffer into which to decompress DNS name
  208. * @ret next Byte following decompressed DNS name
  209. */
  210. static char * dns_decompress_name ( const struct dns_header *reply,
  211. const char *name, char *buf ) {
  212. int i, len;
  213. do {
  214. /* Obtain next section of name */
  215. while ( ( *name ) & 0xc0 ) {
  216. name = ( ( char * ) reply +
  217. ( ntohs ( *((uint16_t *)name) ) & ~0xc000 ) );
  218. }
  219. /* Copy data */
  220. len = *name;
  221. for ( i = len + 1 ; i > 0 ; i-- ) {
  222. *(buf++) = *(name++);
  223. }
  224. } while ( len );
  225. return buf;
  226. }
  227. /**
  228. * Send next packet in DNS request
  229. *
  230. * @v dns DNS request
  231. */
  232. static int dns_send_packet ( struct dns_request *dns ) {
  233. static unsigned int qid = 0;
  234. size_t qlen;
  235. /* Increment query ID */
  236. dns->query.dns.id = htons ( ++qid );
  237. DBGC ( dns, "DNS %p sending query ID %d\n", dns, qid );
  238. /* Start retransmission timer */
  239. start_timer ( &dns->timer );
  240. /* Send the data */
  241. qlen = ( ( ( void * ) dns->qinfo ) - ( ( void * ) &dns->query )
  242. + sizeof ( dns->qinfo ) );
  243. return xfer_deliver_raw ( &dns->socket, &dns->query, qlen );
  244. }
  245. /**
  246. * Handle DNS retransmission timer expiry
  247. *
  248. * @v timer Retry timer
  249. * @v fail Failure indicator
  250. */
  251. static void dns_timer_expired ( struct retry_timer *timer, int fail ) {
  252. struct dns_request *dns =
  253. container_of ( timer, struct dns_request, timer );
  254. if ( fail ) {
  255. dns_done ( dns, -ETIMEDOUT );
  256. } else {
  257. dns_send_packet ( dns );
  258. }
  259. }
  260. /**
  261. * Receive new data
  262. *
  263. * @v socket UDP socket
  264. * @v data DNS reply
  265. * @v len Length of DNS reply
  266. * @ret rc Return status code
  267. */
  268. static int dns_xfer_deliver_raw ( struct xfer_interface *socket,
  269. const void *data, size_t len ) {
  270. struct dns_request *dns =
  271. container_of ( socket, struct dns_request, socket );
  272. const struct dns_header *reply = data;
  273. union dns_rr_info *rr_info;
  274. struct sockaddr_in *sin;
  275. unsigned int qtype = dns->qinfo->qtype;
  276. /* Sanity check */
  277. if ( len < sizeof ( *reply ) ) {
  278. DBGC ( dns, "DNS %p received underlength packet length %zd\n",
  279. dns, len );
  280. return -EINVAL;
  281. }
  282. /* Check reply ID matches query ID */
  283. if ( reply->id != dns->query.dns.id ) {
  284. DBGC ( dns, "DNS %p received unexpected reply ID %d "
  285. "(wanted %d)\n", dns, ntohs ( reply->id ),
  286. ntohs ( dns->query.dns.id ) );
  287. return -EINVAL;
  288. }
  289. DBGC ( dns, "DNS %p received reply ID %d\n", dns, ntohs ( reply->id ));
  290. /* Stop the retry timer. After this point, each code path
  291. * must either restart the timer by calling dns_send_packet(),
  292. * or mark the DNS operation as complete by calling
  293. * dns_done()
  294. */
  295. stop_timer ( &dns->timer );
  296. /* Search through response for useful answers. Do this
  297. * multiple times, to take advantage of useful nameservers
  298. * which send us e.g. the CNAME *and* the A record for the
  299. * pointed-to name.
  300. */
  301. while ( ( rr_info = dns_find_rr ( dns, reply ) ) ) {
  302. switch ( rr_info->common.type ) {
  303. case htons ( DNS_TYPE_A ):
  304. /* Found the target A record */
  305. DBGC ( dns, "DNS %p found address %s\n",
  306. dns, inet_ntoa ( rr_info->a.in_addr ) );
  307. sin = ( struct sockaddr_in * ) &dns->sa;
  308. sin->sin_family = AF_INET;
  309. sin->sin_addr = rr_info->a.in_addr;
  310. /* Mark operation as complete */
  311. dns_done ( dns, 0 );
  312. return 0;
  313. case htons ( DNS_TYPE_CNAME ):
  314. /* Found a CNAME record; update query and recurse */
  315. DBGC ( dns, "DNS %p found CNAME\n", dns );
  316. dns->qinfo = ( void * ) dns_decompress_name ( reply,
  317. rr_info->cname.cname,
  318. dns->query.payload );
  319. dns->qinfo->qtype = htons ( DNS_TYPE_A );
  320. dns->qinfo->qclass = htons ( DNS_CLASS_IN );
  321. /* Terminate the operation if we recurse too far */
  322. if ( ++dns->recursion > DNS_MAX_CNAME_RECURSION ) {
  323. DBGC ( dns, "DNS %p recursion exceeded\n",
  324. dns );
  325. dns_done ( dns, -ELOOP );
  326. return 0;
  327. }
  328. break;
  329. default:
  330. DBGC ( dns, "DNS %p got unknown record type %d\n",
  331. dns, ntohs ( rr_info->common.type ) );
  332. break;
  333. }
  334. }
  335. /* Determine what to do next based on the type of query we
  336. * issued and the reponse we received
  337. */
  338. switch ( qtype ) {
  339. case htons ( DNS_TYPE_A ):
  340. /* We asked for an A record and got nothing;
  341. * try the CNAME.
  342. */
  343. DBGC ( dns, "DNS %p found no A record; trying CNAME\n", dns );
  344. dns->qinfo->qtype = htons ( DNS_TYPE_CNAME );
  345. dns_send_packet ( dns );
  346. return 0;
  347. case htons ( DNS_TYPE_CNAME ):
  348. /* We asked for a CNAME record. If we got a response
  349. * (i.e. if the next A query is already set up), then
  350. * issue it, otherwise abort.
  351. */
  352. if ( dns->qinfo->qtype == htons ( DNS_TYPE_A ) ) {
  353. dns_send_packet ( dns );
  354. return 0;
  355. } else {
  356. DBGC ( dns, "DNS %p found no CNAME record\n", dns );
  357. dns_done ( dns, -ENXIO );
  358. return 0;
  359. }
  360. default:
  361. assert ( 0 );
  362. dns_done ( dns, -EINVAL );
  363. return 0;
  364. }
  365. }
  366. /**
  367. * Receive new data
  368. *
  369. * @v socket UDP socket
  370. * @v rc Reason for close
  371. */
  372. static void dns_xfer_close ( struct xfer_interface *socket, int rc ) {
  373. struct dns_request *dns =
  374. container_of ( socket, struct dns_request, socket );
  375. if ( ! rc )
  376. rc = -ECONNABORTED;
  377. dns_done ( dns, rc );
  378. }
  379. /** DNS socket operations */
  380. static struct xfer_interface_operations dns_socket_operations = {
  381. .close = dns_xfer_close,
  382. .vredirect = xfer_vopen,
  383. .request = ignore_xfer_request,
  384. .seek = ignore_xfer_seek,
  385. .alloc_iob = default_xfer_alloc_iob,
  386. .deliver_iob = xfer_deliver_as_raw,
  387. .deliver_raw = dns_xfer_deliver_raw,
  388. };
  389. /**
  390. * Resolve name using DNS
  391. *
  392. * @v resolv Name resolution interface
  393. * @v name Name to resolve
  394. * @v sa Socket address to fill in
  395. * @ret rc Return status code
  396. */
  397. static int dns_resolv ( struct resolv_interface *resolv,
  398. const char *name, struct sockaddr *sa ) {
  399. struct dns_request *dns;
  400. int rc;
  401. /* Fail immediately if no DNS servers */
  402. if ( ! nameserver.st_family ) {
  403. DBG ( "DNS not attempting to resolve \"%s\": "
  404. "no DNS servers\n", name );
  405. return -ENXIO;
  406. }
  407. /* Allocate DNS structure */
  408. dns = malloc ( sizeof ( *dns ) );
  409. if ( ! dns )
  410. return -ENOMEM;
  411. memset ( dns, 0, sizeof ( *dns ) );
  412. resolv_init ( &dns->resolv, &null_resolv_ops, &dns->refcnt );
  413. xfer_init ( &dns->socket, &dns_socket_operations, &dns->refcnt );
  414. dns->timer.expired = dns_timer_expired;
  415. memcpy ( &dns->sa, sa, sizeof ( dns->sa ) );
  416. /* Create query */
  417. dns->query.dns.flags = htons ( DNS_FLAG_QUERY | DNS_FLAG_OPCODE_QUERY |
  418. DNS_FLAG_RD );
  419. dns->query.dns.qdcount = htons ( 1 );
  420. dns->qinfo = ( void * ) dns_make_name ( name, dns->query.payload );
  421. dns->qinfo->qtype = htons ( DNS_TYPE_A );
  422. dns->qinfo->qclass = htons ( DNS_CLASS_IN );
  423. /* Open UDP connection */
  424. if ( ( rc = xfer_open_socket ( &dns->socket, SOCK_DGRAM,
  425. ( struct sockaddr * ) &nameserver,
  426. NULL ) ) != 0 ) {
  427. DBGC ( dns, "DNS %p could not open socket: %s\n",
  428. dns, strerror ( rc ) );
  429. goto err;
  430. }
  431. /* Send first DNS packet */
  432. dns_send_packet ( dns );
  433. /* Attach parent interface, mortalise self, and return */
  434. resolv_plug_plug ( &dns->resolv, resolv );
  435. ref_put ( &dns->refcnt );
  436. return 0;
  437. err:
  438. ref_put ( &dns->refcnt );
  439. return rc;
  440. }
  441. /** DNS name resolver */
  442. struct resolver dns_resolver __resolver ( RESOLV_NORMAL ) = {
  443. .name = "DNS",
  444. .resolv = dns_resolv,
  445. };