*/
#include <vnet/vnet.h>
-#include <vnet/udp/udp.h>
+#include <vnet/udp/udp_local.h>
#include <vnet/plugin/plugin.h>
#include <vnet/fib/fib_table.h>
#include <dns/dns.h>
#include <stdbool.h>
/* define message IDs */
-#include <dns/dns_msg_enum.h>
-
-/* define message structures */
-#define vl_typedefs
-#include <dns/dns_all_api_h.h>
-#undef vl_typedefs
-
-/* define generated endian-swappers */
-#define vl_endianfun
-#include <dns/dns_all_api_h.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <dns/dns_all_api_h.h>
-#undef vl_printfun
-
-/* Get the API version number */
-#define vl_api_version(n,v) static u32 api_version=(v);
-#include <dns/dns_all_api_h.h>
-#undef vl_api_version
+#include <dns/dns.api_enum.h>
+#include <dns/dns.api_types.h>
#define REPLY_MSG_ID_BASE dm->msg_id_base
#include <vlibapi/api_helper_macros.h>
/* Macro to finish up custom dump fns */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define FINISH \
vec_add1 (s, 0); \
vl_print (handle, (char *)s); \
if (dm->is_enabled == 0)
return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 1);
/* *INDENT-OFF* */
- pool_foreach (ep, dm->entries,
- ({
+ pool_foreach (ep, dm->entries)
+ {
vec_free (ep->name);
vec_free (ep->pending_requests);
- }));
+ }
/* *INDENT-ON* */
pool_free (dm->entries);
}
static int
-dns_enable_disable (dns_main_t * dm, int is_enable)
+dns_enable_disable (vlib_main_t * vm, dns_main_t * dm, int is_enable)
{
vlib_thread_main_t *tm = &vlib_thread_main;
u32 n_vlib_mains = tm->n_vlib_mains;
- vlib_main_t *vm = dm->vlib_main;
/* Create the resolver process if not done already */
- vnet_dns_create_resolver_process (dm);
+ vnet_dns_create_resolver_process (vm, dm);
if (is_enable)
{
(vl_api_dns_enable_disable_t * mp)
{
vl_api_dns_enable_disable_reply_t *rmp;
+ vlib_main_t *vm = vlib_get_main ();
dns_main_t *dm = &dns_main;
int rv;
- rv = dns_enable_disable (dm, mp->enable);
+ rv = dns_enable_disable (vm, dm, mp->enable);
REPLY_MACRO (VL_API_DNS_ENABLE_DISABLE_REPLY);
}
}
void
-vnet_dns_send_dns4_request (dns_main_t * dm,
+vnet_dns_send_dns4_request (vlib_main_t * vm, dns_main_t * dm,
dns_cache_entry_t * ep, ip4_address_t * server)
{
- vlib_main_t *vm = dm->vlib_main;
f64 now = vlib_time_now (vm);
u32 bi;
vlib_buffer_t *b;
found_src_address:
/* Go get a buffer */
- if (vlib_buffer_alloc (dm->vlib_main, &bi, 1) != 1)
+ if (vlib_buffer_alloc (vm, &bi, 1) != 1)
return;
b = vlib_get_buffer (vm, bi);
}
void
-vnet_dns_send_dns6_request (dns_main_t * dm,
+vnet_dns_send_dns6_request (vlib_main_t * vm, dns_main_t * dm,
dns_cache_entry_t * ep, ip6_address_t * server)
{
- vlib_main_t *vm = dm->vlib_main;
f64 now = vlib_time_now (vm);
u32 bi;
vlib_buffer_t *b;
found_src_address:
/* Go get a buffer */
- if (vlib_buffer_alloc (dm->vlib_main, &bi, 1) != 1)
+ if (vlib_buffer_alloc (vm, &bi, 1) != 1)
return;
b = vlib_get_buffer (vm, bi);
}
void
-vnet_send_dns_request (dns_main_t * dm, dns_cache_entry_t * ep)
+vnet_send_dns_request (vlib_main_t * vm, dns_main_t * dm,
+ dns_cache_entry_t * ep)
{
dns_header_t *h;
dns_query_t *qp;
if (vec_len (dm->ip6_name_servers))
{
vnet_dns_send_dns6_request
- (dm, ep, dm->ip6_name_servers + ep->server_rotor);
+ (vm, dm, ep, dm->ip6_name_servers + ep->server_rotor);
goto out;
}
else
if (vec_len (dm->ip4_name_servers))
{
vnet_dns_send_dns4_request
- (dm, ep, dm->ip4_name_servers + ep->server_rotor);
+ (vm, dm, ep, dm->ip4_name_servers + ep->server_rotor);
goto out;
}
}
if (ep->server_af == 1 /* ip6 */ )
vnet_dns_send_dns6_request
- (dm, ep, dm->ip6_name_servers + ep->server_rotor);
+ (vm, dm, ep, dm->ip6_name_servers + ep->server_rotor);
else
vnet_dns_send_dns4_request
- (dm, ep, dm->ip4_name_servers + ep->server_rotor);
+ (vm, dm, ep, dm->ip4_name_servers + ep->server_rotor);
out:
- vlib_process_signal_event_mt (dm->vlib_main,
+ vlib_process_signal_event_mt (vm,
dm->resolver_process_node_index,
DNS_RESOLVER_EVENT_PENDING, 0);
}
if (dm->is_enabled == 0)
return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 2);
p = hash_get_mem (dm->cache_entry_by_name, name);
if (!p)
{
return VNET_API_ERROR_UNSPECIFIED;
#endif
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 3);
limit = pool_elts (dm->entries);
start_index = random_u32 (&dm->random_seed) % limit;
if (dm->is_enabled == 0)
return VNET_API_ERROR_NAME_RESOLUTION_NOT_ENABLED;
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 4);
p = hash_get_mem (dm->cache_entry_by_name, name);
if (p)
{
/* Note: consumes the name vector */
ep->name = name;
+ /* make sure it NULL-terminated as hash_set_mem will use strlen() */
+ vec_terminate_c_string (ep->name);
hash_set_mem (dm->cache_entry_by_name, ep->name, ep - dm->entries);
ep->flags = DNS_CACHE_ENTRY_FLAG_VALID | DNS_CACHE_ENTRY_FLAG_STATIC;
ep->dns_response = dns_reply_data;
}
int
-vnet_dns_resolve_name (dns_main_t * dm, u8 * name, dns_pending_request_t * t,
- dns_cache_entry_t ** retp)
+vnet_dns_resolve_name (vlib_main_t * vm, dns_main_t * dm, u8 * name,
+ dns_pending_request_t * t, dns_cache_entry_t ** retp)
{
dns_cache_entry_t *ep;
int rv;
dns_pending_request_t *pr;
int count;
- now = vlib_time_now (dm->vlib_main);
+ now = vlib_time_now (vm);
/* In case we can't actually answer the question right now... */
*retp = 0;
if (name[0] == 0)
return VNET_API_ERROR_INVALID_VALUE;
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 5);
search_again:
p = hash_get_mem (dm->cache_entry_by_name, name);
if (p)
name = ep->cname;
goto search_again;
}
-
- /* Note: caller must drop the lock! */
*retp = ep;
+ dns_cache_unlock (dm);
return (0);
}
else
clib_memcpy (pr->dst_address, t->dst_address, count);
}
- vnet_send_dns_request (dm, ep);
+ vnet_send_dns_request (vm, dm, ep);
dns_cache_unlock (dm);
return 0;
}
*/
int
-vnet_dns_cname_indirection_nolock (dns_main_t * dm, u32 ep_index, u8 * reply)
+vnet_dns_cname_indirection_nolock (vlib_main_t * vm, dns_main_t * dm,
+ u32 ep_index, u8 * reply)
{
dns_header_t *h;
dns_query_t *qp;
found_last_request:
- now = vlib_time_now (dm->vlib_main);
+ now = vlib_time_now (vm);
cname = vnet_dns_labels_to_name (rr->rdata, reply, &pos2);
/* Save the cname */
vec_add1 (cname, 0);
*/
vec_add1 (dm->unresolved_entries, next_ep - dm->entries);
- vnet_send_dns_request (dm, next_ep);
+ vnet_send_dns_request (vm, dm, next_ep);
return (1);
}
int
-vnet_dns_response_to_reply (u8 * response,
- vl_api_dns_resolve_name_reply_t * rmp,
- u32 * min_ttlp)
+vnet_dns_response_to_reply (u8 *response, dns_resolve_name_t *rn,
+ u32 *min_ttlp)
{
dns_header_t *h;
dns_query_t *qp;
u16 flags;
u16 rcode;
u32 ttl;
- int pointer_chase;
+ int pointer_chase, addr_set = 0;
h = (dns_header_t *) response;
flags = clib_net_to_host_u16 (h->flags);
{
case DNS_TYPE_A:
/* Collect an ip4 address. Do not pass go. Do not collect $200 */
- memcpy (rmp->ip4_address, rr->rdata, sizeof (ip4_address_t));
- rmp->ip4_set = 1;
+ ip_address_set (&rn->address, rr->rdata, AF_IP4);
ttl = clib_net_to_host_u32 (rr->ttl);
+ addr_set += 1;
if (min_ttlp && *min_ttlp > ttl)
*min_ttlp = ttl;
break;
case DNS_TYPE_AAAA:
/* Collect an ip6 address. Do not pass go. Do not collect $200 */
- memcpy (rmp->ip6_address, rr->rdata, sizeof (ip6_address_t));
+ ip_address_set (&rn->address, rr->rdata, AF_IP6);
ttl = clib_net_to_host_u32 (rr->ttl);
if (min_ttlp && *min_ttlp > ttl)
*min_ttlp = ttl;
- rmp->ip6_set = 1;
+ addr_set += 1;
break;
default:
break;
}
/* Might as well stop ASAP */
- if (rmp->ip4_set && rmp->ip6_set)
+ if (addr_set > 1)
break;
pos += sizeof (*rr) + clib_net_to_host_u16 (rr->rdlength);
curpos = pos;
}
- if ((rmp->ip4_set + rmp->ip6_set) == 0)
+ if (addr_set == 0)
return VNET_API_ERROR_NAME_SERVER_NO_ADDRESSES;
return 0;
}
return 0;
}
+__clib_export int
+dns_resolve_name (u8 *name, dns_cache_entry_t **ep, dns_pending_request_t *t0,
+ dns_resolve_name_t *rn)
+{
+ dns_main_t *dm = &dns_main;
+ vlib_main_t *vm = vlib_get_main ();
+
+ int rv = vnet_dns_resolve_name (vm, dm, name, t0, ep);
+
+ /* Error, e.g. not enabled? Tell the user */
+ if (rv < 0)
+ return rv;
+
+ /* Resolution pending? Don't reply... */
+ if (ep[0] == 0)
+ return 0;
+
+ return vnet_dns_response_to_reply (ep[0]->dns_response, rn, 0 /* ttl-ptr */);
+}
+
static void
vl_api_dns_resolve_name_t_handler (vl_api_dns_resolve_name_t * mp)
{
dns_main_t *dm = &dns_main;
vl_api_dns_resolve_name_reply_t *rmp;
- dns_cache_entry_t *ep;
+ dns_cache_entry_t *ep = 0;
dns_pending_request_t _t0, *t0 = &_t0;
int rv;
+ dns_resolve_name_t rn;
/* Sanitize the name slightly */
mp->name[ARRAY_LEN (mp->name) - 1] = 0;
t0->client_index = mp->client_index;
t0->client_context = mp->context;
- rv = vnet_dns_resolve_name (dm, mp->name, t0, &ep);
+ rv = dns_resolve_name (mp->name, &ep, t0, &rn);
/* Error, e.g. not enabled? Tell the user */
if (rv < 0)
return;
/* *INDENT-OFF* */
- REPLY_MACRO2(VL_API_DNS_RESOLVE_NAME_REPLY,
- ({
- rv = vnet_dns_response_to_reply (ep->dns_response, rmp, 0 /* ttl-ptr */);
- rmp->retval = clib_host_to_net_u32 (rv);
- }));
+ REPLY_MACRO2 (VL_API_DNS_RESOLVE_NAME_REPLY, ({
+ ip_address_copy_addr (rmp->ip4_address, &rn.address);
+ if (ip_addr_version (&rn.address) == AF_IP4)
+ rmp->ip4_set = 1;
+ else
+ rmp->ip6_set = 1;
+ }));
/* *INDENT-ON* */
-
- /*
- * dns_resolve_name leaves the cache locked when it returns
- * a cached result, so unlock it here.
- */
- dns_cache_unlock (dm);
}
static void
vl_api_dns_resolve_ip_t_handler (vl_api_dns_resolve_ip_t * mp)
{
+ vlib_main_t *vm = vlib_get_main ();
dns_main_t *dm = &dns_main;
vl_api_dns_resolve_ip_reply_t *rmp;
dns_cache_entry_t *ep;
t0->client_index = mp->client_index;
t0->client_context = mp->context;
- rv = vnet_dns_resolve_name (dm, lookup_name, t0, &ep);
+ rv = vnet_dns_resolve_name (vm, dm, lookup_name, t0, &ep);
vec_free (lookup_name);
rmp->retval = clib_host_to_net_u32 (rv);
}));
/* *INDENT-ON* */
-
- /*
- * vnet_dns_resolve_name leaves the cache locked when it returns
- * a cached result, so unlock it here.
- */
- dns_cache_unlock (dm);
-}
-
-#define vl_msg_name_crc_list
-#include <dns/dns_all_api_h.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (dns_main_t * dm)
-{
-#define _(id,n,crc) \
- vl_msg_api_add_msg_name_crc (dm->api_main, #n "_" #crc, dm->msg_id_base + id);
- foreach_vl_msg_name_crc_dns;
-#undef _
}
-#define foreach_dns_plugin_api_msg \
-_(DNS_ENABLE_DISABLE, dns_enable_disable) \
-_(DNS_NAME_SERVER_ADD_DEL, dns_name_server_add_del) \
-_(DNS_RESOLVE_NAME, dns_resolve_name) \
-_(DNS_RESOLVE_IP, dns_resolve_ip)
-
static clib_error_t *
dns_config_fn (vlib_main_t * vm, unformat_input_t * input)
{
return s;
}
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 6);
if (name)
{
if (verbose > 0)
{
/* *INDENT-OFF* */
- pool_foreach (ep, dm->entries,
- ({
+ pool_foreach (ep, dm->entries)
+ {
if (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID)
{
ASSERT (ep->dns_response);
verbose);
}
vec_add1 (s, '\n');
- }));
+ }
/* *INDENT-ON* */
}
test_dns_fmt_command_fn (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
{
+ dns_resolve_name_t _rn, *rn = &_rn;
u8 *dns_reply_data = 0;
int verbose = 0;
int rv;
clib_memset (rmp, 0, sizeof (*rmp));
- rv = vnet_dns_response_to_reply (dns_reply_data, rmp, 0 /* ttl-ptr */ );
+ rv = vnet_dns_response_to_reply (dns_reply_data, rn, 0 /* ttl-ptr */);
switch (rv)
{
break;
case 0:
- if (rmp->ip4_set)
- vlib_cli_output (vm, "ip4 address: %U", format_ip4_address,
- (ip4_address_t *) rmp->ip4_address);
- if (rmp->ip6_set)
- vlib_cli_output (vm, "ip6 address: %U", format_ip6_address,
- (ip6_address_t *) rmp->ip6_address);
+ vlib_cli_output (vm, "ip address: %U", format_ip_address, &rn->address);
break;
}
else
return clib_error_return (0, "no name provided");
- dns_cache_lock (dm);
+ dns_cache_lock (dm, 7);
p = hash_get_mem (dm->cache_entry_by_name, name);
if (!p)
#endif
void
-vnet_send_dns6_reply (dns_main_t * dm, dns_pending_request_t * pr,
- dns_cache_entry_t * ep, vlib_buffer_t * b0)
+vnet_send_dns6_reply (vlib_main_t * vm, dns_main_t * dm,
+ dns_pending_request_t * pr, dns_cache_entry_t * ep,
+ vlib_buffer_t * b0)
{
clib_warning ("Unimplemented...");
}
void
-vnet_send_dns4_reply (dns_main_t * dm, dns_pending_request_t * pr,
- dns_cache_entry_t * ep, vlib_buffer_t * b0)
+vnet_send_dns4_reply (vlib_main_t * vm, dns_main_t * dm,
+ dns_pending_request_t * pr, dns_cache_entry_t * ep,
+ vlib_buffer_t * b0)
{
- vlib_main_t *vm = dm->vlib_main;
u32 bi = 0;
fib_prefix_t prefix;
fib_node_index_t fei;
u32 *to_next;
u8 *dns_response;
u8 *reply;
- vl_api_dns_resolve_name_reply_t _rnr, *rnr = &_rnr;
+ /* vl_api_dns_resolve_name_reply_t _rnr, *rnr = &_rnr; */
+ dns_resolve_name_t _rn, *rn = &_rn;
vl_api_dns_resolve_ip_reply_t _rir, *rir = &_rir;
- u32 ttl, tmp;
+ u32 ttl = 64, tmp;
u32 qp_offset;
dns_query_t *qp;
dns_rr_t *rr;
if (pr->request_type == DNS_PEER_PENDING_NAME_TO_IP)
{
/* Quick and dirty way to dig up the A-record address. $$ FIXME */
- clib_memset (rnr, 0, sizeof (*rnr));
- if (vnet_dns_response_to_reply (ep->dns_response, rnr, &ttl))
+ clib_memset (rn, 0, sizeof (*rn));
+ if (vnet_dns_response_to_reply (ep->dns_response, rn, &ttl))
{
/* clib_warning ("response_to_reply failed..."); */
is_fail = 1;
}
- if (rnr->ip4_set == 0)
+ else if (ip_addr_version (&rn->address) != AF_IP4)
{
/* clib_warning ("No A-record..."); */
is_fail = 1;
rr->class = clib_host_to_net_u16 (1 /* internet */ );
rr->ttl = clib_host_to_net_u32 (ttl);
rr->rdlength = clib_host_to_net_u16 (sizeof (ip4_address_t));
- clib_memcpy (rr->rdata, rnr->ip4_address, sizeof (ip4_address_t));
+ ip_address_copy_addr (rr->rdata, &rn->address);
}
else
{
}
}
-static void *vl_api_dns_enable_disable_t_print
- (vl_api_dns_enable_disable_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: dns_enable_disable ");
- s = format (s, "%s ", mp->enable ? "enable" : "disable");
-
- FINISH;
-}
-
-static void *vl_api_dns_name_server_add_del_t_print
- (vl_api_dns_name_server_add_del_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: dns_name_server_add_del ");
- if (mp->is_ip6)
- s = format (s, "%U ", format_ip6_address,
- (ip6_address_t *) mp->server_address);
- else
- s = format (s, "%U ", format_ip4_address,
- (ip4_address_t *) mp->server_address);
-
- if (mp->is_add == 0)
- s = format (s, "del ");
-
- FINISH;
-}
-
-static void *vl_api_dns_resolve_name_t_print
- (vl_api_dns_resolve_name_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: dns_resolve_name ");
- s = format (s, "%s ", mp->name);
- FINISH;
-}
-
-static void *vl_api_dns_resolve_ip_t_print
- (vl_api_dns_resolve_ip_t * mp, void *handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: dns_resolve_ip ");
- if (mp->is_ip6)
- s = format (s, "%U ", format_ip6_address, mp->address);
- else
- s = format (s, "%U ", format_ip4_address, mp->address);
- FINISH;
-}
-
-static void
-dns_custom_dump_configure (dns_main_t * dm)
-{
-#define _(n,f) dm->api_main->msg_print_handlers \
- [VL_API_##n + dm->msg_id_base] \
- = (void *) vl_api_##f##_t_print;
- foreach_dns_plugin_api_msg;
-#undef _
-}
-
-/* Set up the API message handling tables */
-static clib_error_t *
-dns_plugin_api_hookup (vlib_main_t * vm)
-{
- dns_main_t *dmp = &dns_main;
-#define _(N,n) \
- vl_msg_api_set_handlers((VL_API_##N + dmp->msg_id_base), \
- #n, \
- vl_api_##n##_t_handler, \
- vl_noop_handler, \
- vl_api_##n##_t_endian, \
- vl_api_##n##_t_print, \
- sizeof(vl_api_##n##_t), 1);
- foreach_dns_plugin_api_msg;
-#undef _
-
- return 0;
-}
-
+#include <dns/dns.api.c>
static clib_error_t *
dns_init (vlib_main_t * vm)
{
dns_main_t *dm = &dns_main;
- u8 *name;
- dm->vlib_main = vm;
dm->vnet_main = vnet_get_main ();
dm->name_cache_size = 1000;
dm->max_ttl_in_seconds = 86400;
dm->random_seed = 0xDEADDABE;
- dm->api_main = &api_main;
-
- name = format (0, "dns_%08x%c", api_version, 0);
+ dm->api_main = vlibapi_get_main ();
/* Ask for a correctly-sized block of API message decode slots */
- dm->msg_id_base = vl_msg_api_get_msg_ids
- ((char *) name, VL_MSG_FIRST_AVAILABLE);
-
- (void) dns_plugin_api_hookup (vm);
-
- /* Add our API messages to the global name_crc hash table */
- setup_message_id_table (dm);
-
- dns_custom_dump_configure (dm);
-
- vec_free (name);
+ dm->msg_id_base = setup_message_id_table ();
return 0;
}