1 /*
2  * Copyright (c) 2002-2020 Apple Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UDNS_H_
18 #define __UDNS_H_
19 
20 #include "mDNSEmbeddedAPI.h"
21 #include "DNSCommon.h"
22 #include <sys/types.h>
23 #include "dns_sd.h"
24 
25 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH)
26 #include "dso.h"
27 #include "dso-transport.h"
28 #endif
29 
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33 
34 #define RESTART_GOODBYE_DELAY    (6 * mDNSPlatformOneSecond) // delay after restarting LLQ before nuking previous known answers (avoids flutter if we restart before we have networking up)
35 #define INIT_UCAST_POLL_INTERVAL (3 * mDNSPlatformOneSecond) // this interval is used after send failures on network transitions
36                                                              // which typically heal quickly, so we start agressively and exponentially back off
37 #define MAX_UCAST_POLL_INTERVAL (60 * 60 * mDNSPlatformOneSecond)
38 //#define MAX_UCAST_POLL_INTERVAL (1 * 60 * mDNSPlatformOneSecond)
39 #define LLQ_POLL_INTERVAL       (15 * 60 * mDNSPlatformOneSecond) // Polling interval for zones w/ an advertised LLQ port (ie not static zones) if LLQ fails due to NAT, etc.
40 #define RESPONSE_WINDOW (60 * mDNSPlatformOneSecond)         // require server responses within one minute of request
41 #define MAX_UCAST_UNANSWERED_QUERIES 2                       // number of unanswered queries from any one uDNS server before trying another server
42 #define DNSSERVER_PENALTY_TIME (60 * mDNSPlatformOneSecond)  // number of seconds for which new questions don't pick this server
43 
44 // On some interfaces, we want to delay the first retransmission to a minimum of 2 seconds
45 // rather than the default (1 second).
46 #define MIN_UCAST_RETRANS_TIMEOUT (2 * mDNSPlatformOneSecond)
47 
48 #define DEFAULT_UPDATE_LEASE 7200
49 
50 #define QuestionIntervalStep 3
51 #define QuestionIntervalStep2 (QuestionIntervalStep*QuestionIntervalStep)
52 #define QuestionIntervalStep3 (QuestionIntervalStep*QuestionIntervalStep*QuestionIntervalStep)
53 #define InitialQuestionInterval ((mDNSPlatformOneSecond + QuestionIntervalStep-1) / QuestionIntervalStep)
54 #define MaxQuestionInterval         (3600 * mDNSPlatformOneSecond)
55 #define UDNSBackOffMultiplier 2
56 #define MinQuestionInterval (1 * mDNSPlatformOneSecond)
57 
58 // For Unicast record registrations, we initialize the interval to 1 second. When we send any query for
59 // the record registration e.g., GetZoneData, we always back off by QuestionIntervalStep
60 // so that the first retry does not happen until 3 seconds which should be enough for TCP/TLS to be done.
61 #define INIT_RECORD_REG_INTERVAL (1 * mDNSPlatformOneSecond)
62 #define MAX_RECORD_REG_INTERVAL (15 * 60 * mDNSPlatformOneSecond)
63 #define MERGE_DELAY_TIME    (1 * mDNSPlatformOneSecond)
64 
65 // If we are refreshing, we do it at least 5 times with a min update frequency of
66 // 5 minutes
67 #define MAX_UPDATE_REFRESH_COUNT    5
68 #define MIN_UPDATE_REFRESH_TIME     (5 * 60 * mDNSPlatformOneSecond)
69 
70 // For questions that use kDNSServiceFlagsTimeout and we don't have a matching resolver e.g., no dns servers,
71 // then use the default value of 30 seconds
72 #define DEFAULT_UDNS_TIMEOUT    30 // in seconds
73 
74 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH)
75 // Push notification structures
76 struct mDNS_DNSPushNotificationServer
77 {
78     dso_connect_state_t       *connectInfo;       // DSO Connection state information
79     dso_state_t               *connection;        // DNS Stateful Operations/TCP Connection pointer, might be null.
80     mDNSu32                    numberOfQuestions; // Number of questions for this server
81     DNSPushServer_ConnectState connectState;      // Current status of connection attempt to this server
82     mDNSs32                    lastDisconnect;    // Last time we got a disconnect, used to avoid constant reconnects
83     domainname                 serverName;        // The hostname returned by the _dns-push-tls._tcp.<zone> SRV lookup
84     mDNSIPPort                 port;              // The port from the SRV lookup
85 #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
86     mdns_dns_service_t         dnsservice;
87 #else
88     DNSServer                 *qDNSServer;        // DNS server stolen from the question that created this server structure.
89 #endif
90     mDNS                      *m;
91     DNSPushNotificationServer *next;
92 } ;
93 
94 struct mDNS_DNSPushNotificationZone
95 {
96     domainname zoneName;
97     DNSPushNotificationServer *server; // DNS Push Notification Servers for this zone
98     mDNSu32 numberOfQuestions;          // Number of questions for this zone
99     DNSPushNotificationZone *next;
100 } ;
101 #endif
102 
103 // Entry points into unicast-specific routines
104 
105 extern void LLQGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo);
106 extern void startLLQHandshake(mDNS *m, DNSQuestion *q);
107 extern void sendLLQRefresh(mDNS *m, DNSQuestion *q);
108 
109 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH)
110 extern void DNSPushNotificationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo);
111 extern void DiscoverDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
112 extern DNSPushNotificationServer *GetConnectionToDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
113 extern DNSPushNotificationServer *SubscribeToDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
114 extern void UnSubscribeToDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
115 extern void DNSPushReconcileConnection(mDNS *m, DNSQuestion *q);
116 extern void DNSPushServerDrop(DNSPushNotificationServer *server);
117 #endif
118 
119 extern void SleepRecordRegistrations(mDNS *m);
120 
121 // uDNS_UpdateRecord
122 // following fields must be set, and the update validated, upon entry.
123 // rr->NewRData
124 // rr->newrdlength
125 // rr->UpdateCallback
126 
127 extern mStatus uDNS_UpdateRecord(mDNS *m, AuthRecord *rr);
128 
129 extern void SetNextQueryTime(mDNS *const m, const DNSQuestion *const q);
130 extern mStatus mDNS_Register_internal(mDNS *const m, AuthRecord *const rr);
131 extern mStatus mDNS_Deregister_internal(mDNS *const m, AuthRecord *const rr, mDNS_Dereg_type drt);
132 extern mStatus mDNS_StartQuery_internal(mDNS *const m, DNSQuestion *const question);
133 extern mStatus mDNS_StopQuery_internal(mDNS *const m, DNSQuestion *const question);
134 extern mStatus mDNS_StartNATOperation_internal(mDNS *const m, NATTraversalInfo *traversal);
135 
136 extern void RecordRegistrationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneData);
137 extern mStatus uDNS_DeregisterRecord(mDNS *const m, AuthRecord *const rr);
138 extern const domainname *GetServiceTarget(mDNS *m, AuthRecord *const rr);
139 
140 // integer fields of msg header must be in HOST byte order before calling this routine
141 extern void uDNS_ReceiveMsg(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end,
142                             const mDNSAddr *const srcaddr, const mDNSIPPort srcport);
143 
144 extern void uDNS_Tasks(mDNS *const m);
145 extern void UpdateAllSRVRecords(mDNS *m);
146 extern void CheckNATMappings(mDNS *m);
147 
148 extern mStatus         uDNS_SetupDNSConfig(mDNS *const m);
149 
150 // uDNS_SetupWABQueries reads search domains from the platform layer and starts the Wide Area Bonjour
151 // (WAB) domain enumeration queries if necessary.
152 
153 #define UDNS_WAB_BROWSE_QUERY    0x00000001 // Browse queries (b, db)
154 #define UDNS_WAB_LBROWSE_QUERY   0x00000002 // Browse queries (lb)
155 #define UDNS_WAB_REG_QUERY       0x00000004 // Registration queries (r and dr)
156 
157 extern void uDNS_SetupWABQueries(mDNS *const m);
158 extern void uDNS_StartWABQueries(mDNS *const m, int queryType);
159 extern void uDNS_StopWABQueries(mDNS *const m, int queryType);
160 extern domainname      *uDNS_GetNextSearchDomain(mDNSInterfaceID InterfaceID, int *searchIndex, mDNSBool ignoreDotLocal);
161 
162 extern void uDNS_RestartQuestionAsTCP(mDNS *m, DNSQuestion *const q, const mDNSAddr *const srcaddr, const mDNSIPPort srcport);
163 
164 typedef enum
165 {
166     uDNS_LLQ_Not = 0,   // Normal uDNS answer: Flush any stale records from cache, and respect record TTL
167     uDNS_LLQ_Ignore,    // LLQ initial challenge packet: ignore -- has no useful records for us
168     uDNS_LLQ_Entire,    // LLQ initial set of answers: Flush any stale records from cache, but assume TTL is 2 x LLQ refresh interval
169     uDNS_LLQ_Events     // LLQ event packet: don't flush cache; assume TTL is 2 x LLQ refresh interval
170 } uDNS_LLQType;
171 
172 extern uDNS_LLQType    uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end, const mDNSAddr *const srcaddr, const mDNSIPPort srcport, DNSQuestion **matchQuestion);
173 extern DomainAuthInfo *GetAuthInfoForName_internal(mDNS *m, const domainname *const name);
174 extern DomainAuthInfo *GetAuthInfoForQuestion(mDNS *m, const DNSQuestion *const q);
175 extern void DisposeTCPConn(struct tcpInfo_t *tcp);
176 
177 // NAT traversal
178 extern void uDNS_ReceiveNATPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len); // Called for each received PCP or NAT-PMP packet
179 extern void natTraversalHandleAddressReply(mDNS *const m, mDNSu16 err, mDNSv4Addr ExtAddr);
180 extern void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol);
181 
182 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH)
183 // DNS Push Notification
184 extern void SubscribeToDNSPushNotification(mDNS *m, DNSQuestion *q);
185 #endif
186 
187 extern CacheRecord* mDNSCoreReceiveCacheCheck(mDNS *const m, const DNSMessage *const response, uDNS_LLQType LLQType,
188 											  const mDNSu32 slot, CacheGroup *cg,
189                                               CacheRecord ***cfp, mDNSInterfaceID InterfaceID);
190 #ifdef  __cplusplus
191 }
192 #endif
193 
194 #endif // __UDNS_H
195