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 13KB

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