1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.connectivity;
import static android.net.NetworkAgent.DSCP_POLICY_STATUS_DELETED;
import static android.net.NetworkAgent.DSCP_POLICY_STATUS_INSUFFICIENT_PROCESSING_RESOURCES;
import static android.net.NetworkAgent.DSCP_POLICY_STATUS_POLICY_NOT_FOUND;
import static android.net.NetworkAgent.DSCP_POLICY_STATUS_REQUEST_DECLINED;
import static android.net.NetworkAgent.DSCP_POLICY_STATUS_SUCCESS;
import static android.system.OsConstants.ETH_P_ALL;
import android.annotation.NonNull;
import android.net.DscpPolicy;
import android.os.RemoteException;
import android.system.ErrnoException;
import android.util.Log;
import android.util.SparseIntArray;
import com.android.net.module.util.BpfMap;
import com.android.net.module.util.Struct;
import com.android.net.module.util.TcUtils;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/**
* DscpPolicyTracker has a single entry point from ConnectivityService handler.
* This guarantees that all code runs on the same thread and no locking is needed.
*/
public class DscpPolicyTracker {
// After tethering and clat priorities.
static final short PRIO_DSCP = 5;
private static final String TAG = DscpPolicyTracker.class.getSimpleName();
private static final String PROG_PATH =
"/sys/fs/bpf/net_shared/prog_dscp_policy_schedcls_set_dscp";
// Name is "map + *.o + map_name + map". Can probably shorten this
private static final String IPV4_POLICY_MAP_PATH = makeMapPath(
"dscp_policy_ipv4_dscp_policies");
private static final String IPV6_POLICY_MAP_PATH = makeMapPath(
"dscp_policy_ipv6_dscp_policies");
private static final int MAX_POLICIES = 16;
private static String makeMapPath(String which) {
return "/sys/fs/bpf/net_shared/map_" + which + "_map";
}
private Set<String> mAttachedIfaces;
private final BpfMap<Struct.U32, DscpPolicyValue> mBpfDscpIpv4Policies;
private final BpfMap<Struct.U32, DscpPolicyValue> mBpfDscpIpv6Policies;
// The actual policy rules used by the BPF code to process packets
// are in mBpfDscpIpv4Policies and mBpfDscpIpv4Policies. Both of
// these can contain up to MAX_POLICIES rules.
//
// A given policy always consumes one entry in both the IPv4 and
// IPv6 maps even if if's an IPv4-only or IPv6-only policy.
//
// Each interface index has a SparseIntArray of rules which maps a
// policy ID to the index of the corresponding rule in the maps.
// mIfaceIndexToPolicyIdBpfMapIndex maps the interface index to
// the per-interface SparseIntArray.
private final HashMap<Integer, SparseIntArray> mIfaceIndexToPolicyIdBpfMapIndex;
public DscpPolicyTracker() throws ErrnoException {
mAttachedIfaces = new HashSet<String>();
mIfaceIndexToPolicyIdBpfMapIndex = new HashMap<Integer, SparseIntArray>();
mBpfDscpIpv4Policies = new BpfMap<Struct.U32, DscpPolicyValue>(IPV4_POLICY_MAP_PATH,
BpfMap.BPF_F_RDWR, Struct.U32.class, DscpPolicyValue.class);
mBpfDscpIpv6Policies = new BpfMap<Struct.U32, DscpPolicyValue>(IPV6_POLICY_MAP_PATH,
BpfMap.BPF_F_RDWR, Struct.U32.class, DscpPolicyValue.class);
}
private boolean isUnusedIndex(int index) {
for (SparseIntArray ifacePolicies : mIfaceIndexToPolicyIdBpfMapIndex.values()) {
if (ifacePolicies.indexOfValue(index) >= 0) return false;
}
return true;
}
private int getFirstFreeIndex() {
if (mIfaceIndexToPolicyIdBpfMapIndex.size() == 0) return 0;
for (int i = 0; i < MAX_POLICIES; i++) {
if (isUnusedIndex(i)) {
return i;
}
}
return MAX_POLICIES;
}
private int findIndex(int policyId, int ifIndex) {
SparseIntArray ifacePolicies = mIfaceIndexToPolicyIdBpfMapIndex.get(ifIndex);
if (ifacePolicies != null) {
final int existingIndex = ifacePolicies.get(policyId, -1);
if (existingIndex != -1) {
return existingIndex;
}
}
final int firstIndex = getFirstFreeIndex();
if (firstIndex >= MAX_POLICIES) {
// New policy is being added, but max policies has already been reached.
return -1;
}
return firstIndex;
}
private void sendStatus(NetworkAgentInfo nai, int policyId, int status) {
try {
nai.networkAgent.onDscpPolicyStatusUpdated(policyId, status);
} catch (RemoteException e) {
Log.e(TAG, "Failed update policy status: ", e);
}
}
private boolean matchesIpv4(DscpPolicy policy) {
return ((policy.getDestinationAddress() == null
|| policy.getDestinationAddress() instanceof Inet4Address)
&& (policy.getSourceAddress() == null
|| policy.getSourceAddress() instanceof Inet4Address));
}
private boolean matchesIpv6(DscpPolicy policy) {
return ((policy.getDestinationAddress() == null
|| policy.getDestinationAddress() instanceof Inet6Address)
&& (policy.getSourceAddress() == null
|| policy.getSourceAddress() instanceof Inet6Address));
}
private int getIfaceIndex(NetworkAgentInfo nai) {
String iface = nai.linkProperties.getInterfaceName();
NetworkInterface netIface;
try {
netIface = NetworkInterface.getByName(iface);
} catch (IOException e) {
Log.e(TAG, "Unable to get iface index for " + iface + ": " + e);
netIface = null;
}
return (netIface != null) ? netIface.getIndex() : 0;
}
private int addDscpPolicyInternal(DscpPolicy policy, int ifIndex) {
// If there is no existing policy with a matching ID, and we are already at
// the maximum number of policies then return INSUFFICIENT_PROCESSING_RESOURCES.
SparseIntArray ifacePolicies = mIfaceIndexToPolicyIdBpfMapIndex.get(ifIndex);
if (ifacePolicies == null) {
ifacePolicies = new SparseIntArray(MAX_POLICIES);
}
// Currently all classifiers are supported, if any are removed return
// DSCP_POLICY_STATUS_REQUESTED_CLASSIFIER_NOT_SUPPORTED,
// and for any other generic error DSCP_POLICY_STATUS_REQUEST_DECLINED
final int addIndex = findIndex(policy.getPolicyId(), ifIndex);
if (addIndex == -1) {
return DSCP_POLICY_STATUS_INSUFFICIENT_PROCESSING_RESOURCES;
}
try {
// Add v4 policy to mBpfDscpIpv4Policies if source and destination address
// are both null or if they are both instances of Inet4Address.
if (matchesIpv4(policy)) {
mBpfDscpIpv4Policies.insertOrReplaceEntry(
new Struct.U32(addIndex),
new DscpPolicyValue(policy.getSourceAddress(),
policy.getDestinationAddress(), ifIndex,
policy.getSourcePort(), policy.getDestinationPortRange(),
(short) policy.getProtocol(), (short) policy.getDscpValue()));
}
// Add v6 policy to mBpfDscpIpv6Policies if source and destination address
// are both null or if they are both instances of Inet6Address.
if (matchesIpv6(policy)) {
mBpfDscpIpv6Policies.insertOrReplaceEntry(
new Struct.U32(addIndex),
new DscpPolicyValue(policy.getSourceAddress(),
policy.getDestinationAddress(), ifIndex,
policy.getSourcePort(), policy.getDestinationPortRange(),
(short) policy.getProtocol(), (short) policy.getDscpValue()));
}
ifacePolicies.put(policy.getPolicyId(), addIndex);
// Only add the policy to the per interface map if the policy was successfully
// added to both bpf maps above. It is safe to assume that if insert fails for
// one map then it fails for both.
mIfaceIndexToPolicyIdBpfMapIndex.put(ifIndex, ifacePolicies);
} catch (ErrnoException e) {
Log.e(TAG, "Failed to insert policy into map: ", e);
return DSCP_POLICY_STATUS_INSUFFICIENT_PROCESSING_RESOURCES;
}
return DSCP_POLICY_STATUS_SUCCESS;
}
/**
* Add the provided DSCP policy to the bpf map. Attach bpf program dscp_policy to iface
* if not already attached. Response will be sent back to nai with status.
*
* DSCP_POLICY_STATUS_SUCCESS - if policy was added successfully
* DSCP_POLICY_STATUS_INSUFFICIENT_PROCESSING_RESOURCES - if max policies were already set
* DSCP_POLICY_STATUS_REQUEST_DECLINED - Interface index was invalid
*/
public void addDscpPolicy(NetworkAgentInfo nai, DscpPolicy policy) {
if (!mAttachedIfaces.contains(nai.linkProperties.getInterfaceName())) {
if (!attachProgram(nai.linkProperties.getInterfaceName())) {
Log.e(TAG, "Unable to attach program");
sendStatus(nai, policy.getPolicyId(),
DSCP_POLICY_STATUS_INSUFFICIENT_PROCESSING_RESOURCES);
return;
}
}
final int ifIndex = getIfaceIndex(nai);
if (ifIndex == 0) {
Log.e(TAG, "Iface index is invalid");
sendStatus(nai, policy.getPolicyId(), DSCP_POLICY_STATUS_REQUEST_DECLINED);
return;
}
int status = addDscpPolicyInternal(policy, ifIndex);
sendStatus(nai, policy.getPolicyId(), status);
}
private void removePolicyFromMap(NetworkAgentInfo nai, int policyId, int index,
boolean sendCallback) {
int status = DSCP_POLICY_STATUS_POLICY_NOT_FOUND;
try {
mBpfDscpIpv4Policies.replaceEntry(new Struct.U32(index), DscpPolicyValue.NONE);
mBpfDscpIpv6Policies.replaceEntry(new Struct.U32(index), DscpPolicyValue.NONE);
status = DSCP_POLICY_STATUS_DELETED;
} catch (ErrnoException e) {
Log.e(TAG, "Failed to delete policy from map: ", e);
}
if (sendCallback) {
sendStatus(nai, policyId, status);
}
}
/**
* Remove specified DSCP policy and detach program if no other policies are active.
*/
public void removeDscpPolicy(NetworkAgentInfo nai, int policyId) {
if (!mAttachedIfaces.contains(nai.linkProperties.getInterfaceName())) {
// Nothing to remove since program is not attached. Send update back for policy id.
sendStatus(nai, policyId, DSCP_POLICY_STATUS_POLICY_NOT_FOUND);
return;
}
SparseIntArray ifacePolicies = mIfaceIndexToPolicyIdBpfMapIndex.get(getIfaceIndex(nai));
if (ifacePolicies == null) return;
final int existingIndex = ifacePolicies.get(policyId, -1);
if (existingIndex == -1) {
Log.e(TAG, "Policy " + policyId + " does not exist in map.");
sendStatus(nai, policyId, DSCP_POLICY_STATUS_POLICY_NOT_FOUND);
return;
}
removePolicyFromMap(nai, policyId, existingIndex, true);
ifacePolicies.delete(policyId);
if (ifacePolicies.size() == 0) {
detachProgram(nai.linkProperties.getInterfaceName());
}
}
/**
* Remove all DSCP policies and detach program. Send callback if requested.
*/
public void removeAllDscpPolicies(NetworkAgentInfo nai, boolean sendCallback) {
if (!mAttachedIfaces.contains(nai.linkProperties.getInterfaceName())) {
// Nothing to remove since program is not attached. Send update for policy
// id 0. The status update must contain a policy ID, and 0 is an invalid id.
if (sendCallback) {
sendStatus(nai, 0, DSCP_POLICY_STATUS_SUCCESS);
}
return;
}
SparseIntArray ifacePolicies = mIfaceIndexToPolicyIdBpfMapIndex.get(getIfaceIndex(nai));
if (ifacePolicies == null) return;
for (int i = 0; i < ifacePolicies.size(); i++) {
removePolicyFromMap(nai, ifacePolicies.keyAt(i), ifacePolicies.valueAt(i),
sendCallback);
}
ifacePolicies.clear();
detachProgram(nai.linkProperties.getInterfaceName());
}
/**
* Attach BPF program
*/
private boolean attachProgram(@NonNull String iface) {
try {
NetworkInterface netIface = NetworkInterface.getByName(iface);
boolean isEth = TcUtils.isEthernet(iface);
String path = PROG_PATH + (isEth ? "_ether" : "_raw_ip");
TcUtils.tcFilterAddDevBpf(netIface.getIndex(), false, PRIO_DSCP, (short) ETH_P_ALL,
path);
} catch (IOException e) {
Log.e(TAG, "Unable to attach to TC on " + iface + ": " + e);
return false;
}
mAttachedIfaces.add(iface);
return true;
}
/**
* Detach BPF program
*/
public void detachProgram(@NonNull String iface) {
try {
NetworkInterface netIface = NetworkInterface.getByName(iface);
if (netIface != null) {
TcUtils.tcFilterDelDev(netIface.getIndex(), false, PRIO_DSCP, (short) ETH_P_ALL);
}
mAttachedIfaces.remove(iface);
} catch (IOException e) {
Log.e(TAG, "Unable to detach to TC on " + iface + ": " + e);
}
}
}
|