162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci=================== 462306a36Sopenharmony_ciDNS Resolver Module 562306a36Sopenharmony_ci=================== 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci.. Contents: 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci - Overview. 1062306a36Sopenharmony_ci - Compilation. 1162306a36Sopenharmony_ci - Setting up. 1262306a36Sopenharmony_ci - Usage. 1362306a36Sopenharmony_ci - Mechanism. 1462306a36Sopenharmony_ci - Debugging. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ciOverview 1862306a36Sopenharmony_ci======== 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ciThe DNS resolver module provides a way for kernel services to make DNS queries 2162306a36Sopenharmony_ciby way of requesting a key of key type dns_resolver. These queries are 2262306a36Sopenharmony_ciupcalled to userspace through /sbin/request-key. 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciThese routines must be supported by userspace tools dns.upcall, cifs.upcall and 2562306a36Sopenharmony_cirequest-key. It is under development and does not yet provide the full feature 2662306a36Sopenharmony_ciset. The features it does support include: 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci (*) Implements the dns_resolver key_type to contact userspace. 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciIt does not yet support the following AFS features: 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci (*) Dns query support for AFSDB resource record. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciThis code is extracted from the CIFS filesystem. 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciCompilation 3862306a36Sopenharmony_ci=========== 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciThe module should be enabled by turning on the kernel configuration options:: 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci CONFIG_DNS_RESOLVER - tristate "DNS Resolver support" 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ciSetting up 4662306a36Sopenharmony_ci========== 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ciTo set up this facility, the /etc/request-key.conf file must be altered so that 4962306a36Sopenharmony_ci/sbin/request-key can appropriately direct the upcalls. For example, to handle 5062306a36Sopenharmony_cibasic dname to IPv4/IPv6 address resolution, the following line should be 5162306a36Sopenharmony_ciadded:: 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci #OP TYPE DESC CO-INFO PROGRAM ARG1 ARG2 ARG3 ... 5562306a36Sopenharmony_ci #====== ============ ======= ======= ========================== 5662306a36Sopenharmony_ci create dns_resolver * * /usr/sbin/cifs.upcall %k 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ciTo direct a query for query type 'foo', a line of the following should be added 5962306a36Sopenharmony_cibefore the more general line given above as the first match is the one taken:: 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci create dns_resolver foo:* * /usr/sbin/dns.foo %k 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ciUsage 6562306a36Sopenharmony_ci===== 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciTo make use of this facility, one of the following functions that are 6862306a36Sopenharmony_ciimplemented in the module can be called after doing:: 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci #include <linux/dns_resolver.h> 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci :: 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci int dns_query(const char *type, const char *name, size_t namelen, 7562306a36Sopenharmony_ci const char *options, char **_result, time_t *_expiry); 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci This is the basic access function. It looks for a cached DNS query and if 7862306a36Sopenharmony_ci it doesn't find it, it upcalls to userspace to make a new DNS query, which 7962306a36Sopenharmony_ci may then be cached. The key description is constructed as a string of the 8062306a36Sopenharmony_ci form:: 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci [<type>:]<name> 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci where <type> optionally specifies the particular upcall program to invoke, 8562306a36Sopenharmony_ci and thus the type of query to do, and <name> specifies the string to be 8662306a36Sopenharmony_ci looked up. The default query type is a straight hostname to IP address 8762306a36Sopenharmony_ci set lookup. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci The name parameter is not required to be a NUL-terminated string, and its 9062306a36Sopenharmony_ci length should be given by the namelen argument. 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci The options parameter may be NULL or it may be a set of options 9362306a36Sopenharmony_ci appropriate to the query type. 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci The return value is a string appropriate to the query type. For instance, 9662306a36Sopenharmony_ci for the default query type it is just a list of comma-separated IPv4 and 9762306a36Sopenharmony_ci IPv6 addresses. The caller must free the result. 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci The length of the result string is returned on success, and a negative 10062306a36Sopenharmony_ci error code is returned otherwise. -EKEYREJECTED will be returned if the 10162306a36Sopenharmony_ci DNS lookup failed. 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci If _expiry is non-NULL, the expiry time (TTL) of the result will be 10462306a36Sopenharmony_ci returned also. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ciThe kernel maintains an internal keyring in which it caches looked up keys. 10762306a36Sopenharmony_ciThis can be cleared by any process that has the CAP_SYS_ADMIN capability by 10862306a36Sopenharmony_cithe use of KEYCTL_KEYRING_CLEAR on the keyring ID. 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciReading DNS Keys from Userspace 11262306a36Sopenharmony_ci=============================== 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ciKeys of dns_resolver type can be read from userspace using keyctl_read() or 11562306a36Sopenharmony_ci"keyctl read/print/pipe". 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ciMechanism 11962306a36Sopenharmony_ci========= 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ciThe dnsresolver module registers a key type called "dns_resolver". Keys of 12262306a36Sopenharmony_cithis type are used to transport and cache DNS lookup results from userspace. 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ciWhen dns_query() is invoked, it calls request_key() to search the local 12562306a36Sopenharmony_cikeyrings for a cached DNS result. If that fails to find one, it upcalls to 12662306a36Sopenharmony_ciuserspace to get a new result. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciUpcalls to userspace are made through the request_key() upcall vector, and are 12962306a36Sopenharmony_cidirected by means of configuration lines in /etc/request-key.conf that tell 13062306a36Sopenharmony_ci/sbin/request-key what program to run to instantiate the key. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciThe upcall handler program is responsible for querying the DNS, processing the 13362306a36Sopenharmony_ciresult into a form suitable for passing to the keyctl_instantiate_key() 13462306a36Sopenharmony_ciroutine. This then passes the data to dns_resolver_instantiate() which strips 13562306a36Sopenharmony_cioff and processes any options included in the data, and then attaches the 13662306a36Sopenharmony_ciremainder of the string to the key as its payload. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciThe upcall handler program should set the expiry time on the key to that of the 13962306a36Sopenharmony_cilowest TTL of all the records it has extracted a result from. This means that 14062306a36Sopenharmony_cithe key will be discarded and recreated when the data it holds has expired. 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_cidns_query() returns a copy of the value attached to the key, or an error if 14362306a36Sopenharmony_cithat is indicated instead. 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciSee <file:Documentation/security/keys/request-key.rst> for further 14662306a36Sopenharmony_ciinformation about request-key function. 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ciDebugging 15062306a36Sopenharmony_ci========= 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ciDebugging messages can be turned on dynamically by writing a 1 into the 15362306a36Sopenharmony_cifollowing file:: 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci /sys/module/dnsresolver/parameters/debug 156