| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | .\" Generated by Mmark Markdown Processer - mmark.miek.nl | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | .TH "COREDNS-VIEW" 7 "June 2025" "CoreDNS" "CoreDNS Plugins" | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .SH "NAME" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | \fIview\fP - defines conditions that must be met for a DNS request to be routed to the server block. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SH "DESCRIPTION" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | \fIview\fP defines an expression that must evaluate to true for a DNS request to be routed to the server block. | 
					
						
							|  |  |  | This enables advanced server block routing functions such as split dns. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SH "SYNTAX" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | .RS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .nf | 
					
						
							|  |  |  | view NAME { | 
					
						
							|  |  |  |   expr EXPRESSION | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .fi | 
					
						
							|  |  |  | .RE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCview\fR \fBNAME\fP - The name of the view used by metrics and exported as metadata for requests that match the | 
					
						
							|  |  |  | view's expression | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCexpr\fR \fBEXPRESSION\fP - CoreDNS will only route incoming queries to the enclosing server block | 
					
						
							|  |  |  | if the \fBEXPRESSION\fP evaluates to true. See the \fBExpressions\fP section for available variables and functions. | 
					
						
							|  |  |  | If multiple instances of view are defined, all \fBEXPRESSION\fP must evaluate to true for CoreDNS will only route | 
					
						
							|  |  |  | incoming queries to the enclosing server block. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | For expression syntax and examples, see the Expressions and Examples sections. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SH "EXAMPLES" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | Implement CIDR based split DNS routing.  This will return a different | 
					
						
							|  |  |  | answer for \fB\fCtest.\fR depending on client's IP address.  It returns ... | 
					
						
							|  |  |  | * \fB\fCtest. 3600 IN A 1.1.1.1\fR, for queries with a source address in 127.0.0.0/24 | 
					
						
							|  |  |  | * \fB\fCtest. 3600 IN A 2.2.2.2\fR, for queries with a source address in 192.168.0.0/16 | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | * \fB\fCtest. 3600 IN AAAA 2001:0DB8::1\fR, for queries with a source address in 2001:0DB8::/32 | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | * \fB\fCtest. 3600 IN A 3.3.3.3\fR, for all others | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | .RS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .nf | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   view example1 { | 
					
						
							|  |  |  |     expr incidr(client\_ip(), '127.0.0.0/24') | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   hosts { | 
					
						
							|  |  |  |     1.1.1.1 test | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   view example2 { | 
					
						
							|  |  |  |     expr incidr(client\_ip(), '192.168.0.0/16') | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   hosts { | 
					
						
							|  |  |  |     2.2.2.2 test | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | \&. { | 
					
						
							|  |  |  |   view v6\_example1 { | 
					
						
							|  |  |  |     expr incidr(client\_ip(), '2001:0DB8::/32') | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   hosts { | 
					
						
							|  |  |  |     2001:0DB8::1 test | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | \&. { | 
					
						
							|  |  |  |   hosts { | 
					
						
							|  |  |  |     3.3.3.3 test | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .fi | 
					
						
							|  |  |  | .RE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | Send all \fB\fCA\fR and \fB\fCAAAA\fR requests to \fB\fC10.0.0.6\fR, and all other requests to \fB\fC10.0.0.1\fR. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | .RS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .nf | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   view example { | 
					
						
							|  |  |  |     expr type() in ['A', 'AAAA'] | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   forward . 10.0.0.6 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   forward . 10.0.0.1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .fi | 
					
						
							|  |  |  | .RE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | Send all requests for \fB\fCabc.*.example.com\fR (where * can be any number of labels), to \fB\fC10.0.0.2\fR, and all other | 
					
						
							|  |  |  | requests to \fB\fC10.0.0.1\fR. | 
					
						
							|  |  |  | Note that the regex pattern is enclosed in single quotes, and backslashes are escaped with backslashes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | .RS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .nf | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   view example { | 
					
						
							|  |  |  |     expr name() matches '^abc\\\\..*\\\\.example\\\\.com\\\\.$' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   forward . 10.0.0.2 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \&. { | 
					
						
							|  |  |  |   forward . 10.0.0.1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .fi | 
					
						
							|  |  |  | .RE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SH "EXPRESSIONS" | 
					
						
							|  |  |  | .PP | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | To evaluate expressions, \fIview\fP uses the expr-lang/expr package ( https://github.com/expr-lang/expr | 
					
						
							|  |  |  | \[la]https://github.com/expr-lang/expr\[ra] ). | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | For example, an expression could look like: | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | \fB\fC(type() == 'A' && name() == 'example.com.') || client_ip() == '1.2.3.4'\fR. | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | All expressions should be written to evaluate to a boolean value. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .PP | 
					
						
							| 
									
										
										
										
											2024-08-11 13:00:49 +01:00
										 |  |  | See https://github.com/expr-lang/expr/blob/master/docs/Language-Definition.md | 
					
						
							|  |  |  | \[la]https://github.com/expr-lang/expr/blob/master/docs/Language-Definition.md\[ra] as a detailed reference for valid syntax. | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .SS "AVAILABLE EXPRESSION FUNCTIONS" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | In the context of the \fIview\fP plugin, expressions can reference DNS query information by using utility | 
					
						
							|  |  |  | functions defined below. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SS "DNS QUERY FUNCTIONS" | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCbufsize() int\fR: the EDNS0 buffer size advertised in the query | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCclass() string\fR: class of the request (IN, CH, ...) | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCclient_ip() string\fR: client's IP address, for IPv6 addresses these are enclosed in brackets: \fB\fC[::1]\fR | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCdo() bool\fR: the EDNS0 DO (DNSSEC OK) bit set in the query | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCid() int\fR: query ID | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | \fB\fCname() string\fR: name of the request (the domain name requested ending with a dot): \fB\fCexample.com.\fR | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCopcode() int\fR: query OPCODE | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCport() string\fR: client's port | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCproto() string\fR: protocol used (tcp or udp) | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCserver_ip() string\fR: server's IP address; for IPv6 addresses these are enclosed in brackets: \fB\fC[::1]\fR | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							| 
									
										
										
										
											2025-06-05 18:20:24 +02:00
										 |  |  | \fB\fCserver_port() string\fR : server's port | 
					
						
							| 
									
										
										
										
											2022-09-11 10:31:46 +00:00
										 |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCsize() int\fR: request size in bytes | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCtype() string\fR: type of the request (A, AAAA, TXT, ...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SS "UTILITY FUNCTIONS" | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCincidr(ip string, cidr string) bool\fR: returns true if \fIip\fP is within \fIcidr\fP | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCmetadata(label string)\fR - returns the value for the metadata matching \fIlabel\fP | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .SH "METADATA" | 
					
						
							|  |  |  | .PP | 
					
						
							|  |  |  | The view plugin will publish the following metadata, if the \fImetadata\fP | 
					
						
							|  |  |  | plugin is also enabled: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .IP \(bu 4 | 
					
						
							|  |  |  | \fB\fCview/name\fR: the name of the view handling the current request | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |