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