1 /* 2 * Copyright (c) 2002-2021 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 * https://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 "nullability.h" 21 #include "mDNSEmbeddedAPI.h" 22 #include "DNSCommon.h" 23 #include <sys/types.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_MIN 15 40 #define LLQ_POLL_INTERVAL (LLQ_POLL_INTERVAL_MIN * 60 * mDNSPlatformOneSecond) // Polling interval for zones w/ an advertised LLQ port (ie not static zones) if LLQ fails due to NAT, etc. 41 #define RESPONSE_WINDOW (60 * mDNSPlatformOneSecond) // require server responses within one minute of request 42 #define MAX_UCAST_UNANSWERED_QUERIES 2 // number of unanswered queries from any one uDNS server before trying another server 43 #define DNSSERVER_PENALTY_TIME (60 * mDNSPlatformOneSecond) // number of seconds for which new questions don't pick this server 44 45 // On some interfaces, we want to delay the first retransmission to a minimum of 2 seconds 46 // rather than the default (1 second). 47 #define MIN_UCAST_RETRANS_TIMEOUT (2 * mDNSPlatformOneSecond) 48 49 #define DEFAULT_UPDATE_LEASE 7200 50 51 #define QuestionIntervalStep 3 52 #define QuestionIntervalStep2 (QuestionIntervalStep*QuestionIntervalStep) 53 #define QuestionIntervalStep3 (QuestionIntervalStep*QuestionIntervalStep*QuestionIntervalStep) 54 #define InitialQuestionInterval ((mDNSPlatformOneSecond + QuestionIntervalStep-1) / QuestionIntervalStep) 55 #define MaxQuestionInterval (3600 * mDNSPlatformOneSecond) 56 #define UDNSBackOffMultiplier 2 57 #define MinQuestionInterval (1 * mDNSPlatformOneSecond) 58 59 // For Unicast record registrations, we initialize the interval to 1 second. When we send any query for 60 // the record registration e.g., GetZoneData, we always back off by QuestionIntervalStep 61 // so that the first retry does not happen until 3 seconds which should be enough for TCP/TLS to be done. 62 #define INIT_RECORD_REG_INTERVAL (1 * mDNSPlatformOneSecond) 63 #define MAX_RECORD_REG_INTERVAL (15 * 60 * mDNSPlatformOneSecond) 64 #define MERGE_DELAY_TIME (1 * mDNSPlatformOneSecond) 65 66 // If we are refreshing, we do it at least 5 times with a min update frequency of 67 // 5 minutes 68 #define MAX_UPDATE_REFRESH_COUNT 5 69 #define MIN_UPDATE_REFRESH_TIME (5 * 60 * mDNSPlatformOneSecond) 70 71 // For questions that use kDNSServiceFlagsTimeout and we don't have a matching resolver e.g., no dns servers, 72 // then use the default value of 30 seconds 73 #define DEFAULT_UDNS_TIMEOUT 30 // in seconds 74 75 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH) 76 77 // Reference count helper for DNSPushServer and DNSPushZone. 78 #define DNS_PUSH_RETAIN(OBJ) \ 79 do \ 80 { \ 81 (OBJ)->refCount++; \ 82 LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG, \ 83 "DNS push object retained - object: %p, refCount after retaining: %u.", (OBJ), (OBJ)->refCount); \ 84 } while (mDNSfalse) 85 86 #define DNS_PUSH_RELEASE(OBJ, FINALIZER) \ 87 do \ 88 { \ 89 (OBJ)->refCount--; \ 90 LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG, \ 91 "DNS push object released - object: %p, refCount after releasing: %u.", (OBJ), (OBJ)->refCount); \ 92 if ((OBJ)->refCount == 0) \ 93 { \ 94 FINALIZER((OBJ)); \ 95 (OBJ) = NULL; \ 96 } \ 97 } while (mDNSfalse) 98 99 // Push notification structures. 100 struct mDNS_DNSPushServer 101 { 102 uint32_t serial; // The serial number that can be used to identify a specific server. 103 uint32_t refCount; // Reference count used by DNS_PUSH_RETAIN and DNS_PUSH_RELEASE. 104 dso_connect_state_t *connectInfo; // DSO Connection state information. 105 dso_state_t *connection; // DNS Stateful Operations/TCP Connection pointer, might be null. 106 DNSPushServer_ConnectState connectState; // Current status of connection attempt to this server. 107 mDNSs32 lastDisconnect; // Last time we got a disconnect, used to avoid constant reconnects. 108 domainname serverName; // The hostname returned by the _dns-push-tls._tcp.<zone> SRV lookup. 109 mDNSIPPort port; // The port from the SRV lookup. 110 #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER) 111 mdns_dns_service_t dnsservice; 112 #else 113 DNSServer *qDNSServer; // DNS server stolen from the question that created this server structure. 114 #endif 115 mDNS *m; 116 mDNSBool canceling; // Indicates if the current server is being canceled. 117 DNSPushServer *next; 118 } ; 119 120 struct mDNS_DNSPushZone 121 { 122 uint32_t refCount; // Reference count used by DNS_PUSH_RETAIN and DNS_PUSH_RELEASE. 123 domainname zoneName; 124 DNSPushServer *server; // DNS Push Servers for this zone. 125 DNSPushZone *next; 126 } ; 127 #endif 128 129 // Entry points into unicast-specific routines 130 131 extern void LLQGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo); 132 extern void startLLQHandshake(mDNS *m, DNSQuestion *q); 133 extern void sendLLQRefresh(mDNS *m, DNSQuestion *q); 134 135 #if MDNSRESPONDER_SUPPORTS(COMMON, DNS_PUSH) 136 extern void DNSPushGotZoneData(mDNS *m, mStatus err, const ZoneData *zoneInfo); 137 extern void DiscoverDNSPushServer(mDNS *m, DNSQuestion *q); 138 extern void UnsubscribeQuestionFromDNSPushServer(mDNS *m, DNSQuestion *q, mDNSBool fallBackToLLQPoll); 139 extern void UnsubscribeAllQuestionsFromDNSPushServer(mDNS *m, DNSPushServer *server); 140 extern void DNSPushZoneRemove(mDNS *m, const DNSPushServer *server); 141 extern void DNSPushZoneFinalize(DNSPushZone *zone); 142 extern mDNSInterfaceID DNSPushServerGetInterfaceID(mDNS *m, const DNSPushServer *server); 143 extern void DNSPushServerCancel(DNSPushServer *server, mDNSBool alreadyRemovedFromSystem); 144 extern void DNSPushServerFinalize(DNSPushServer *server); 145 extern void DNSPushUpdateQuestionDuplicate(DNSQuestion *primary, DNSQuestion *duplicate); 146 #endif 147 148 extern void GetZoneData_QuestionCallback(mDNS *m, DNSQuestion *question, const ResourceRecord *answer, 149 QC_result AddRecord); 150 151 extern void SleepRecordRegistrations(mDNS *m); 152 153 // uDNS_UpdateRecord 154 // following fields must be set, and the update validated, upon entry. 155 // rr->NewRData 156 // rr->newrdlength 157 // rr->UpdateCallback 158 159 extern mStatus uDNS_UpdateRecord(mDNS *m, AuthRecord *rr); 160 161 extern void SetNextQueryTime(mDNS *const m, const DNSQuestion *const q); 162 extern mStatus mDNS_Register_internal(mDNS *const m, AuthRecord *const rr); 163 extern mStatus mDNS_Deregister_internal(mDNS *const m, AuthRecord *const rr, mDNS_Dereg_type drt); 164 extern mStatus mDNS_StartQuery_internal(mDNS *const m, DNSQuestion *const question); 165 extern mStatus mDNS_StopQuery_internal(mDNS *const m, DNSQuestion *const question); 166 extern mStatus mDNS_StartNATOperation_internal(mDNS *const m, NATTraversalInfo *traversal); 167 168 extern void RecordRegistrationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneData); 169 extern mStatus uDNS_DeregisterRecord(mDNS *const m, AuthRecord *const rr); 170 extern const domainname *GetServiceTarget(mDNS *m, AuthRecord *const rr); 171 172 // integer fields of msg header must be in HOST byte order before calling this routine 173 extern void uDNS_ReceiveMsg(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end, 174 const mDNSAddr *const srcaddr, const mDNSIPPort srcport); 175 176 extern void uDNS_Tasks(mDNS *const m); 177 extern void UpdateAllSRVRecords(mDNS *m); 178 extern void CheckNATMappings(mDNS *m); 179 180 extern mStatus uDNS_SetupDNSConfig(mDNS *const m); 181 182 // uDNS_SetupWABQueries reads search domains from the platform layer and starts the Wide Area Bonjour 183 // (WAB) domain enumeration queries if necessary. 184 185 #define UDNS_WAB_BROWSE_QUERY 0x00000001 // Browse queries (b, db) 186 #define UDNS_WAB_LBROWSE_QUERY 0x00000002 // Browse queries (lb) 187 #define UDNS_WAB_REG_QUERY 0x00000004 // Registration queries (r and dr) 188 189 extern void uDNS_SetupWABQueries(mDNS *const m); 190 extern void uDNS_StartWABQueries(mDNS *const m, int queryType); 191 extern void uDNS_StopWABQueries(mDNS *const m, int queryType); 192 extern domainname *uDNS_GetNextSearchDomain(mDNSInterfaceID InterfaceID, int *searchIndex, mDNSBool ignoreDotLocal); 193 194 extern void uDNS_RestartQuestionAsTCP(mDNS *m, DNSQuestion *const q, const mDNSAddr *const srcaddr, const mDNSIPPort srcport); 195 196 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); 197 extern DomainAuthInfo *GetAuthInfoForName_internal(mDNS *m, const domainname *const name); 198 extern DomainAuthInfo *GetAuthInfoForQuestion(mDNS *m, const DNSQuestion *const q); 199 extern void DisposeTCPConn(struct tcpInfo_t *tcp); 200 201 // NAT traversal 202 extern void uDNS_ReceiveNATPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len); // Called for each received PCP or NAT-PMP packet 203 extern void natTraversalHandleAddressReply(mDNS *const m, mDNSu16 err, mDNSv4Addr ExtAddr); 204 extern void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol); 205 206 extern CacheRecord* mDNSCoreReceiveCacheCheck(mDNS *const m, const DNSMessage *const response, uDNS_LLQType LLQType, 207 const mDNSu32 slot, CacheGroup *cg, 208 CacheRecord ***cfp, mDNSInterfaceID InterfaceID); 209 #ifdef __cplusplus 210 } 211 #endif 212 213 #endif // __UDNS_H_ 214