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 #ifndef NAMED_INTERFACEMGR_H
13 #define NAMED_INTERFACEMGR_H 1
14 
15 /*****
16  ***** Module Info
17  *****/
18 
19 /*! \file
20  * \brief
21  * The interface manager monitors the operating system's list
22  * of network interfaces, creating and destroying listeners
23  * as needed.
24  *
25  * Reliability:
26  *\li	No impact expected.
27  *
28  * Resources:
29  *
30  * Security:
31  * \li	The server will only be able to bind to the DNS port on
32  *	newly discovered interfaces if it is running as root.
33  *
34  * Standards:
35  *\li	The API for scanning varies greatly among operating systems.
36  *	This module attempts to hide the differences.
37  */
38 
39 /***
40  *** Imports
41  ***/
42 
43 #include <stdbool.h>
44 
45 #include <isc/magic.h>
46 #include <isc/mem.h>
47 #include <isc/socket.h>
48 #include <isc/refcount.h>
49 
50 #include <dns/result.h>
51 
52 #include <named/listenlist.h>
53 #include <named/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 128		/*%< Maximum number of UDP dispatchers
64 						     to start per interface */
65 /*% The nameserver interface structure */
66 struct ns_interface {
67 	unsigned int		magic;		/*%< Magic number. */
68 	ns_interfacemgr_t *	mgr;		/*%< Interface manager. */
69 	isc_mutex_t		lock;
70 	int			references;	/*%< Locked */
71 	unsigned int		generation;     /*%< Generation number. */
72 	isc_sockaddr_t		addr;           /*%< Address and port. */
73 	unsigned int		flags;		/*%< Interface characteristics */
74 	char 			name[32];	/*%< Null terminated. */
75 	dns_dispatch_t *	udpdispatch[MAX_UDP_DISPATCH];
76 						/*%< UDP dispatchers. */
77 	isc_socket_t *		tcpsocket;	/*%< TCP socket. */
78 	isc_dscp_t		dscp;		/*%< "listen-on" DSCP value */
79 	isc_refcount_t		ntcpaccepting;	/*%< Number of clients
80 						     ready to accept new
81 						     TCP connections on this
82 						     interface */
83 	isc_refcount_t		ntcpactive;	/*%< Number of clients
84 						     servicing TCP queries
85 						     (whether accepting or
86 						     connected) */
87 	int			nudpdispatch;	/*%< Number of UDP dispatches */
88 	ns_clientmgr_t *	clientmgr;	/*%< Client manager. */
89 	ISC_LINK(ns_interface_t) link;
90 };
91 
92 /***
93  *** Functions
94  ***/
95 
96 isc_result_t
97 ns_interfacemgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
98 		       isc_socketmgr_t *socketmgr,
99 		       dns_dispatchmgr_t *dispatchmgr,
100 		       isc_task_t *task, ns_interfacemgr_t **mgrp);
101 /*%
102  * Create a new interface manager.
103  *
104  * Initially, the new manager will not listen on any interfaces.
105  * Call ns_interfacemgr_setlistenon() and/or ns_interfacemgr_setlistenon6()
106  * to set nonempty listen-on lists.
107  */
108 
109 void
110 ns_interfacemgr_attach(ns_interfacemgr_t *source, ns_interfacemgr_t **target);
111 
112 void
113 ns_interfacemgr_detach(ns_interfacemgr_t **targetp);
114 
115 void
116 ns_interfacemgr_shutdown(ns_interfacemgr_t *mgr);
117 
118 bool
119 ns_interfacemgr_islistening(ns_interfacemgr_t *mgr);
120 /*%
121  * Return if the manager is listening on any interface. It can be called
122  * after a scan or adjust.
123  */
124 
125 isc_result_t
126 ns_interfacemgr_scan(ns_interfacemgr_t *mgr, bool verbose);
127 /*%
128  * Scan the operatings system's list of network interfaces
129  * and create listeners when new interfaces are discovered.
130  * Shut down the sockets for interfaces that go away.
131  *
132  * This should be called once on server startup and then
133  * periodically according to the 'interface-interval' option
134  * in named.conf.
135  */
136 
137 isc_result_t
138 ns_interfacemgr_adjust(ns_interfacemgr_t *mgr, ns_listenlist_t *list,
139 		       bool verbose);
140 /*%
141  * Similar to ns_interfacemgr_scan(), but this function also tries to see the
142  * need for an explicit listen-on when a list element in 'list' is going to
143  * override an already-listening a wildcard interface.
144  *
145  * This function does not update localhost and localnets ACLs.
146  *
147  * This should be called once on server startup, after configuring views and
148  * zones.
149  */
150 
151 void
152 ns_interfacemgr_setlistenon4(ns_interfacemgr_t *mgr, ns_listenlist_t *value);
153 /*%
154  * Set the IPv4 "listen-on" list of 'mgr' to 'value'.
155  * The previous IPv4 listen-on list is freed.
156  */
157 
158 void
159 ns_interfacemgr_setlistenon6(ns_interfacemgr_t *mgr, ns_listenlist_t *value);
160 /*%
161  * Set the IPv6 "listen-on" list of 'mgr' to 'value'.
162  * The previous IPv6 listen-on list is freed.
163  */
164 
165 dns_aclenv_t *
166 ns_interfacemgr_getaclenv(ns_interfacemgr_t *mgr);
167 
168 void
169 ns_interface_attach(ns_interface_t *source, ns_interface_t **target);
170 
171 void
172 ns_interface_detach(ns_interface_t **targetp);
173 
174 void
175 ns_interface_shutdown(ns_interface_t *ifp);
176 /*%
177  * Stop listening for queries on interface 'ifp'.
178  * May safely be called multiple times.
179  */
180 
181 void
182 ns_interfacemgr_dumprecursing(FILE *f, ns_interfacemgr_t *mgr);
183 
184 bool
185 ns_interfacemgr_listeningon(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr);
186 
187 #endif /* NAMED_INTERFACEMGR_H */
188