1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 package com.android.server;
18 
19 import android.annotation.NonNull;
20 import android.util.SparseBooleanArray;
21 
22 import com.android.internal.annotations.GuardedBy;
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 /**
26  * Class used to reserve and release net IDs.
27  *
28  * <p>Instances of this class are thread-safe.
29  */
30 public class NetIdManager {
31     // Sequence number for Networks; keep in sync with system/netd/NetworkController.cpp
32     public static final int MIN_NET_ID = 100; // some reserved marks
33     // Top IDs reserved by IpSecService
34     public static final int MAX_NET_ID = 65535 - IpSecService.TUN_INTF_NETID_RANGE;
35 
36     @GuardedBy("mNetIdInUse")
37     private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray();
38 
39     @GuardedBy("mNetIdInUse")
40     private int mLastNetId = MIN_NET_ID - 1;
41 
42     private final int mMaxNetId;
43 
NetIdManager()44     public NetIdManager() {
45         this(MAX_NET_ID);
46     }
47 
48     @VisibleForTesting
NetIdManager(int maxNetId)49     NetIdManager(int maxNetId) {
50         mMaxNetId = maxNetId;
51     }
52 
53     /**
54      * Get the first netId that follows the provided lastId and is available.
55      */
getNextAvailableNetIdLocked( int lastId, @NonNull SparseBooleanArray netIdInUse)56     private int getNextAvailableNetIdLocked(
57             int lastId, @NonNull SparseBooleanArray netIdInUse) {
58         int netId = lastId;
59         for (int i = MIN_NET_ID; i <= mMaxNetId; i++) {
60             netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID;
61             if (!netIdInUse.get(netId)) {
62                 return netId;
63             }
64         }
65         throw new IllegalStateException("No free netIds");
66     }
67 
68     /**
69      * Reserve a new ID for a network.
70      */
71     public int reserveNetId() {
72         synchronized (mNetIdInUse) {
73             mLastNetId = getNextAvailableNetIdLocked(mLastNetId, mNetIdInUse);
74             // Make sure NetID unused.  http://b/16815182
75             mNetIdInUse.put(mLastNetId, true);
76             return mLastNetId;
77         }
78     }
79 
80     /**
81      * Clear a previously reserved ID for a network.
82      */
83     public void releaseNetId(int id) {
84         synchronized (mNetIdInUse) {
85             mNetIdInUse.delete(id);
86         }
87     }
88 }
89