]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/uDNSPathEvalulation.c
mDNSResponder-878.250.4.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / uDNSPathEvalulation.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2013, 2015 Apple Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "mDNSMacOSX.h"
19 #include <libproc.h>
20
21 #if __has_include(<nw/private.h>)
22 #include <nw/private.h>
23 #else
24 #include <network/private.h>
25 #endif
26
27 #include "dns_sd_internal.h"
28
29 //Gets the DNSPolicy from NW PATH EVALUATOR
30 mDNSexport void mDNSPlatformGetDNSRoutePolicy(DNSQuestion *q, mDNSBool *isBlocked)
31 {
32 q->ServiceID = -1; // initialize the ServiceID to default value of -1
33
34 // Return for non-unicast DNS queries, invalid pid, if NWPathEvaluation is already done by the client, or NWPathEvaluation not available on this OS
35 if (mDNSOpaque16IsZero(q->TargetQID) || (q->pid < 0) || (q->flags & kDNSServiceFlagsPathEvaluationDone) || !nw_endpoint_create_host)
36 {
37 *isBlocked = mDNSfalse;
38 return;
39 }
40
41 mDNSs32 service_id;
42 mDNSu32 client_ifindex, dnspol_ifindex;
43 int retval;
44 struct proc_uniqidentifierinfo info;
45 mDNSBool isUUIDSet;
46
47 char unenc_name[MAX_ESCAPED_DOMAIN_NAME];
48 ConvertDomainNameToCString(&q->qname, unenc_name);
49
50 nw_endpoint_t host = nw_endpoint_create_host(unenc_name, "0");
51 if (host == NULL)
52 LogMsg("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: Query for %##s (%s), PID[%d], EUID[%d], ServiceID[%d] nw_endpoint_t host is NULL", q->qname.c,
53 DNSTypeName(q->qtype), q->pid, q->euid, q->ServiceID);
54
55 nw_parameters_t parameters = nw_parameters_create();
56 if (parameters == NULL)
57 LogMsg("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: Query for %##s (%s), PID[%d], EUID[%d], ServiceID[%d] nw_endpoint_t parameters is NULL", q->qname.c,
58 DNSTypeName(q->qtype), q->pid, q->euid, q->ServiceID);
59
60 // Check for all the special (negative) internal value interface indices before initializing client_ifindex
61 if ( (q->InterfaceID == mDNSInterface_Any)
62 || (q->InterfaceID == mDNSInterface_Unicast)
63 || (q->InterfaceID == mDNSInterface_LocalOnly)
64 || (q->InterfaceID == mDNSInterfaceMark)
65 || (q->InterfaceID == mDNSInterface_P2P)
66 || (q->InterfaceID == mDNSInterface_BLE)
67 || (q->InterfaceID == uDNSInterfaceMark))
68 {
69 client_ifindex = 0;
70 }
71 else
72 {
73 client_ifindex = (mDNSu32)(uintptr_t)q->InterfaceID;
74 }
75
76
77 if (client_ifindex > 0)
78 {
79 nw_interface_t client_intf = nw_interface_create_with_index(client_ifindex);
80 nw_parameters_require_interface(parameters, client_intf);
81 if (client_intf != NULL)
82 network_release(client_intf);
83 else
84 LogInfo("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: client_intf returned by nw_interface_create_with_index() is NULL");
85 }
86
87 nw_parameters_set_uid(parameters,(uid_t)q->euid);
88
89 if (q->pid != 0)
90 {
91 nw_parameters_set_pid(parameters, q->pid);
92 retval = proc_pidinfo(q->pid, PROC_PIDUNIQIDENTIFIERINFO, 1, &info, sizeof(info));
93 if (retval == (int)sizeof(info))
94 {
95 nw_parameters_set_e_proc_uuid(parameters, info.p_uuid);
96 isUUIDSet = mDNStrue;
97 }
98 else
99 {
100 debugf("mDNSPlatformGetDNSRoutePolicy: proc_pidinfo returned %d", retval);
101 isUUIDSet = mDNSfalse;
102 }
103 }
104 else
105 {
106 nw_parameters_set_e_proc_uuid(parameters, q->uuid);
107 isUUIDSet = mDNStrue;
108 }
109
110 nw_path_evaluator_t evaluator = nw_path_create_evaluator_for_endpoint(host, parameters);
111 if (evaluator == NULL)
112 LogMsg("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: Query for %##s (%s), PID[%d], EUID[%d], ServiceID[%d] nw_path_evaluator_t evaluator is NULL", q->qname.c,
113 DNSTypeName(q->qtype), q->pid, q->euid, q->ServiceID);
114
115 if (host != NULL)
116 network_release(host);
117 if (parameters != NULL)
118 network_release(parameters);
119
120 nw_path_t path = nw_path_evaluator_copy_path(evaluator);
121 if (path == NULL)
122 LogMsg("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: Query for %##s (%s), PID[%d], EUID[%d], ServiceID[%d] nw_path_t path is NULL", q->qname.c,
123 DNSTypeName(q->qtype), q->pid, q->euid, q->ServiceID);
124
125 service_id = nw_path_get_flow_divert_unit(path);
126 if (service_id != 0)
127 {
128 q->ServiceID = service_id;
129 LogInfo("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy: Query for %##s service ID is set ->service_ID:[%d] ", q->qname.c, service_id);
130 }
131 else
132 {
133 nw_interface_t nwpath_intf = nw_path_copy_scoped_interface(path);
134 if (nwpath_intf != NULL)
135 {
136 // Use the new scoped interface given by NW PATH EVALUATOR
137 dnspol_ifindex = nw_interface_get_index(nwpath_intf);
138 q->InterfaceID = (mDNSInterfaceID)(uintptr_t)dnspol_ifindex;
139
140 network_release(nwpath_intf);
141
142 if (dnspol_ifindex != client_ifindex)
143 LogInfo("mDNSPlatformGetDNSRoutePolicy: DNS Route Policy has changed the scoped ifindex from [%d] to [%d]",
144 client_ifindex, dnspol_ifindex);
145 }
146 else
147 {
148 debugf("mDNSPlatformGetDNSRoutePolicy: Query for %##s (%s), PID[%d], EUID[%d], ServiceID[%d] nw_interface_t nwpath_intf is NULL ", q->qname.c, DNSTypeName(q->qtype), q->pid, q->euid, q->ServiceID);
149 }
150 }
151
152 if (isUUIDSet && (nw_path_get_status(path) == nw_path_status_unsatisfied) && (nw_path_get_reason(path) == nw_path_reason_policy_drop))
153 *isBlocked = mDNStrue;
154 else
155 *isBlocked = mDNSfalse;
156
157 if (path != NULL)
158 network_release(path);
159 if (evaluator != NULL)
160 network_release(evaluator);
161
162 }