1 /* 2 * Copyright (C) 2017 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.networkstack.tethering; 18 19 import android.annotation.Nullable; 20 import android.app.usage.NetworkStatsManager; 21 import android.bluetooth.BluetoothAdapter; 22 import android.bluetooth.BluetoothPan; 23 import android.content.Context; 24 import android.net.ConnectivityManager; 25 import android.net.INetd; 26 import android.net.connectivity.ConnectivityInternalApiUtil; 27 import android.net.ip.IpServer; 28 import android.os.Binder; 29 import android.os.Build; 30 import android.os.Handler; 31 import android.os.IBinder; 32 import android.os.Looper; 33 import android.os.SystemProperties; 34 import android.text.TextUtils; 35 36 import androidx.annotation.NonNull; 37 import androidx.annotation.RequiresApi; 38 39 import com.android.modules.utils.build.SdkLevel; 40 import com.android.net.module.util.RoutingCoordinatorManager; 41 import com.android.net.module.util.RoutingCoordinatorService; 42 import com.android.net.module.util.SharedLog; 43 import com.android.networkstack.apishim.BluetoothPanShimImpl; 44 import com.android.networkstack.apishim.common.BluetoothPanShim; 45 import com.android.networkstack.tethering.metrics.TetheringMetrics; 46 import com.android.networkstack.tethering.wear.WearableConnectionManager; 47 48 import java.util.ArrayList; 49 50 51 /** 52 * Capture tethering dependencies, for injection. 53 * 54 * @hide 55 */ 56 public abstract class TetheringDependencies { 57 /** 58 * Make the BpfCoordinator to be used by tethering. 59 */ makeBpfCoordinator( @onNull BpfCoordinator.Dependencies deps)60 public @NonNull BpfCoordinator makeBpfCoordinator( 61 @NonNull BpfCoordinator.Dependencies deps) { 62 return new BpfCoordinator(deps); 63 } 64 65 /** 66 * Make the offload hardware interface to be used by tethering. 67 */ makeOffloadHardwareInterface(Handler h, SharedLog log)68 public OffloadHardwareInterface makeOffloadHardwareInterface(Handler h, SharedLog log) { 69 return new OffloadHardwareInterface(h, log); 70 } 71 72 /** 73 * Make the offload controller to be used by tethering. 74 */ 75 @NonNull makeOffloadController(@onNull Handler h, @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps)76 public OffloadController makeOffloadController(@NonNull Handler h, 77 @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps) { 78 final NetworkStatsManager statsManager = 79 (NetworkStatsManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE); 80 return new OffloadController(h, makeOffloadHardwareInterface(h, log), 81 getContext().getContentResolver(), statsManager, log, deps); 82 } 83 84 85 /** 86 * Make the UpstreamNetworkMonitor to be used by tethering. 87 */ makeUpstreamNetworkMonitor(Context ctx, Handler h, SharedLog log, UpstreamNetworkMonitor.EventListener listener)88 public UpstreamNetworkMonitor makeUpstreamNetworkMonitor(Context ctx, Handler h, 89 SharedLog log, UpstreamNetworkMonitor.EventListener listener) { 90 return new UpstreamNetworkMonitor(ctx, h, log, listener); 91 } 92 93 /** 94 * Make the IPv6TetheringCoordinator to be used by tethering. 95 */ makeIPv6TetheringCoordinator( ArrayList<IpServer> notifyList, SharedLog log)96 public IPv6TetheringCoordinator makeIPv6TetheringCoordinator( 97 ArrayList<IpServer> notifyList, SharedLog log) { 98 return new IPv6TetheringCoordinator(notifyList, log); 99 } 100 101 /** 102 * Make dependencies to be used by IpServer. 103 */ makeIpServerDependencies()104 public abstract IpServer.Dependencies makeIpServerDependencies(); 105 106 /** 107 * Make the EntitlementManager to be used by tethering. 108 */ makeEntitlementManager(Context ctx, Handler h, SharedLog log, Runnable callback)109 public EntitlementManager makeEntitlementManager(Context ctx, Handler h, SharedLog log, 110 Runnable callback) { 111 return new EntitlementManager(ctx, h, log, callback); 112 } 113 114 /** 115 * Generate a new TetheringConfiguration according to input sub Id. 116 */ generateTetheringConfiguration(Context ctx, SharedLog log, int subId)117 public TetheringConfiguration generateTetheringConfiguration(Context ctx, SharedLog log, 118 int subId) { 119 return new TetheringConfiguration(ctx, log, subId); 120 } 121 122 /** 123 * Get a reference to INetd to be used by tethering. 124 */ getINetd(Context context, SharedLog log)125 public INetd getINetd(Context context, SharedLog log) { 126 final INetd netd = 127 INetd.Stub.asInterface((IBinder) context.getSystemService(Context.NETD_SERVICE)); 128 if (netd == null) { 129 log.wtf("INetd is null"); 130 } 131 return netd; 132 } 133 134 /** 135 * Get the routing coordinator. 136 */ getRoutingCoordinator(Context context, SharedLog log)137 public RoutingCoordinatorManager getRoutingCoordinator(Context context, SharedLog log) { 138 IBinder binder; 139 if (!SdkLevel.isAtLeastS()) { 140 final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class); 141 binder = 142 new RoutingCoordinatorService( 143 getINetd(context, log), cm::getAllNetworks, context); 144 } else { 145 binder = ConnectivityInternalApiUtil.getRoutingCoordinator(context); 146 } 147 return new RoutingCoordinatorManager(context, binder); 148 } 149 150 /** 151 * Make the TetheringNotificationUpdater to be used by tethering. 152 */ makeNotificationUpdater(@onNull final Context ctx, @NonNull final Looper looper)153 public TetheringNotificationUpdater makeNotificationUpdater(@NonNull final Context ctx, 154 @NonNull final Looper looper) { 155 return new TetheringNotificationUpdater(ctx, looper); 156 } 157 158 /** 159 * Make tethering thread looper. 160 */ makeTetheringLooper()161 public abstract Looper makeTetheringLooper(); 162 163 /** 164 * Get Context of TetheringService. 165 */ getContext()166 public abstract Context getContext(); 167 168 /** 169 * Get a reference to BluetoothAdapter to be used by tethering. 170 */ 171 @Nullable getBluetoothAdapter()172 public abstract BluetoothAdapter getBluetoothAdapter(); 173 174 /** 175 * Get SystemProperties which indicate whether tethering is denied. 176 */ isTetheringDenied()177 public boolean isTetheringDenied() { 178 return TextUtils.equals(SystemProperties.get("ro.tether.denied"), "true"); 179 } 180 181 /** 182 * Make BluetoothPanShim object to enable/disable bluetooth tethering. 183 * 184 * TODO: use BluetoothPan directly when mainline module is built with API 32. 185 */ makeBluetoothPanShim(BluetoothPan pan)186 public BluetoothPanShim makeBluetoothPanShim(BluetoothPan pan) { 187 return BluetoothPanShimImpl.newInstance(pan); 188 } 189 190 /** 191 * Make the TetheringMetrics to be used by tethering. 192 */ makeTetheringMetrics(Context ctx)193 public TetheringMetrics makeTetheringMetrics(Context ctx) { 194 return new TetheringMetrics(ctx); 195 } 196 197 /** 198 * Returns the implementation of WearableConnectionManager. 199 */ 200 @RequiresApi(Build.VERSION_CODES.TIRAMISU) makeWearableConnectionManager(Context ctx)201 public WearableConnectionManager makeWearableConnectionManager(Context ctx) { 202 return new WearableConnectionManager(ctx); 203 } 204 205 /** 206 * Wrapper to get the binder calling uid for unit testing. 207 */ getBinderCallingUid()208 public int getBinderCallingUid() { 209 return Binder.getCallingUid(); 210 } 211 212 /** 213 * Returns true if the tethering with soft ap config feature is enabled. 214 */ isTetheringWithSoftApConfigEnabled()215 public boolean isTetheringWithSoftApConfigEnabled() { 216 return SdkLevel.isAtLeastB(); 217 } 218 } 219