1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #pragma once
13 
14 /*****
15 ***** Module Info
16 *****/
17 
18 /*! \file
19  * \brief
20  * The interface manager monitors the operating system's list
21  * of network interfaces, creating and destroying listeners
22  * as needed.
23  *
24  * Reliability:
25  *\li	No impact expected.
26  *
27  * Resources:
28  *
29  * Security:
30  * \li	The server will only be able to bind to the DNS port on
31  *	newly discovered interfaces if it is running as root.
32  *
33  * Standards:
34  *\li	The API for scanning varies greatly among operating systems.
35  *	This module attempts to hide the differences.
36  */
37 
38 /***
39  *** Imports
40  ***/
41 
42 #include <stdbool.h>
43 
44 #include <isc/magic.h>
45 #include <isc/mem.h>
46 #include <isc/netmgr.h>
47 #include <isc/refcount.h>
48 #include <isc/result.h>
49 
50 #include <dns/geoip.h>
51 
52 #include <ns/listenlist.h>
53 #include <ns/types.h>
54 
55 /***
56  *** Types
57  ***/
58 
59 #define IFACE_MAGIC	      ISC_MAGIC('I', ':', '-', ')')
60 #define NS_INTERFACE_VALID(t) ISC_MAGIC_VALID(t, IFACE_MAGIC)
61 
62 #define NS_INTERFACEFLAG_ANYADDR 0x01U /*%< bound to "any" address */
63 #define MAX_UDP_DISPATCH                           \
64 	128 /*%< Maximum number of UDP dispatchers \
65 	     *           to start per interface */
66 /*% The nameserver interface structure */
67 struct ns_interface {
68 	unsigned int	   magic; /*%< Magic number. */
69 	ns_interfacemgr_t *mgr;	  /*%< Interface manager. */
70 	isc_mutex_t	   lock;
71 	isc_refcount_t	   references;
72 	unsigned int	   generation; /*%< Generation number. */
73 	isc_sockaddr_t	   addr;       /*%< Address and port. */
74 	unsigned int	   flags;      /*%< Interface flags */
75 	char		   name[32];   /*%< Null terminated. */
76 	isc_nmsocket_t *   udplistensocket;
77 	isc_nmsocket_t *   tcplistensocket;
78 	isc_nmsocket_t *   http_listensocket;
79 	isc_nmsocket_t *   http_secure_listensocket;
80 	isc_dscp_t	   dscp;	  /*%< "listen-on" DSCP value */
81 	isc_refcount_t	   ntcpaccepting; /*%< Number of clients
82 					   *   ready to accept new
83 					   *   TCP connections on this
84 					   *   interface */
85 	isc_refcount_t ntcpactive;	  /*%< Number of clients
86 					   *   servicing TCP queries
87 					   *   (whether accepting or
88 					   *   connected) */
89 	ns_clientmgr_t *clientmgr;	  /*%< Client manager. */
90 	ISC_LINK(ns_interface_t) link;
91 };
92 
93 /***
94  *** Functions
95  ***/
96 
97 isc_result_t
98 ns_interfacemgr_create(isc_mem_t *mctx, ns_server_t *sctx,
99 		       isc_taskmgr_t *taskmgr, isc_timermgr_t *timermgr,
100 		       isc_socketmgr_t *socketmgr, isc_nm_t *nm,
101 		       dns_dispatchmgr_t *dispatchmgr, isc_task_t *task,
102 		       dns_geoip_databases_t *geoip, int ncpus,
103 		       ns_interfacemgr_t **mgrp);
104 /*%<
105  * Create a new interface manager.
106  *
107  * Initially, the new manager will not listen on any interfaces.
108  * Call ns_interfacemgr_setlistenon() and/or ns_interfacemgr_setlistenon6()
109  * to set nonempty listen-on lists.
110  */
111 
112 void
113 ns_interfacemgr_attach(ns_interfacemgr_t *source, ns_interfacemgr_t **target);
114 
115 void
116 ns_interfacemgr_detach(ns_interfacemgr_t **targetp);
117 
118 void
119 ns_interfacemgr_shutdown(ns_interfacemgr_t *mgr);
120 
121 void
122 ns_interfacemgr_setbacklog(ns_interfacemgr_t *mgr, int backlog);
123 /*%<
124  * Set the size of the listen() backlog queue.
125  */
126 
127 bool
128 ns_interfacemgr_islistening(ns_interfacemgr_t *mgr);
129 /*%<
130  * Return if the manager is listening on any interface. It can be called
131  * after a scan or adjust.
132  */
133 
134 isc_result_t
135 ns_interfacemgr_scan(ns_interfacemgr_t *mgr, bool verbose);
136 /*%<
137  * Scan the operatings system's list of network interfaces
138  * and create listeners when new interfaces are discovered.
139  * Shut down the sockets for interfaces that go away.
140  *
141  * This should be called once on server startup and then
142  * periodically according to the 'interface-interval' option
143  * in named.conf.
144  */
145 
146 void
147 ns_interfacemgr_setlistenon4(ns_interfacemgr_t *mgr, ns_listenlist_t *value);
148 /*%<
149  * Set the IPv4 "listen-on" list of 'mgr' to 'value'.
150  * The previous IPv4 listen-on list is freed.
151  */
152 
153 void
154 ns_interfacemgr_setlistenon6(ns_interfacemgr_t *mgr, ns_listenlist_t *value);
155 /*%<
156  * Set the IPv6 "listen-on" list of 'mgr' to 'value'.
157  * The previous IPv6 listen-on list is freed.
158  */
159 
160 dns_aclenv_t *
161 ns_interfacemgr_getaclenv(ns_interfacemgr_t *mgr);
162 
163 void
164 ns_interface_attach(ns_interface_t *source, ns_interface_t **target);
165 
166 void
167 ns_interface_detach(ns_interface_t **targetp);
168 
169 void
170 ns_interface_shutdown(ns_interface_t *ifp);
171 /*%<
172  * Stop listening for queries on interface 'ifp'.
173  * May safely be called multiple times.
174  */
175 
176 void
177 ns_interfacemgr_dumprecursing(FILE *f, ns_interfacemgr_t *mgr);
178 
179 bool
180 ns_interfacemgr_listeningon(ns_interfacemgr_t *mgr, const isc_sockaddr_t *addr);
181 
182 ns_server_t *
183 ns_interfacemgr_getserver(ns_interfacemgr_t *mgr);
184 /*%<
185  * Returns the ns_server object associated with the interface manager.
186  */
187 
188 ns_clientmgr_t *
189 ns_interfacemgr_getclientmgr(ns_interfacemgr_t *mgr);
190 /*%<
191  *
192  * Returns the client manager for the current worker thread.
193  * (This cannot be run from outside a network manager thread.)
194  */
195 
196 ns_interface_t *
197 ns__interfacemgr_getif(ns_interfacemgr_t *mgr);
198 ns_interface_t *
199 ns__interfacemgr_nextif(ns_interface_t *ifp);
200 /*%<
201  * Functions to allow external callers to walk the interfaces list.
202  * (Not intended for use outside this module and associated tests.)
203  */
204