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