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.app.usage.NetworkStatsManager; 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothPan; 22 import android.content.Context; 23 import android.net.INetd; 24 import android.net.ip.IpServer; 25 import android.net.util.SharedLog; 26 import android.os.Handler; 27 import android.os.IBinder; 28 import android.os.Looper; 29 import android.os.SystemProperties; 30 import android.text.TextUtils; 31 32 import androidx.annotation.NonNull; 33 34 import com.android.internal.util.StateMachine; 35 import com.android.networkstack.apishim.BluetoothPanShimImpl; 36 import com.android.networkstack.apishim.common.BluetoothPanShim; 37 38 import java.util.ArrayList; 39 40 41 /** 42 * Capture tethering dependencies, for injection. 43 * 44 * @hide 45 */ 46 public abstract class TetheringDependencies { 47 /** 48 * Get a reference to the BpfCoordinator to be used by tethering. 49 */ getBpfCoordinator( @onNull BpfCoordinator.Dependencies deps)50 public @NonNull BpfCoordinator getBpfCoordinator( 51 @NonNull BpfCoordinator.Dependencies deps) { 52 return new BpfCoordinator(deps); 53 } 54 55 /** 56 * Get a reference to the offload hardware interface to be used by tethering. 57 */ getOffloadHardwareInterface(Handler h, SharedLog log)58 public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) { 59 return new OffloadHardwareInterface(h, log); 60 } 61 62 /** 63 * Get a reference to the offload controller to be used by tethering. 64 */ 65 @NonNull getOffloadController(@onNull Handler h, @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps)66 public OffloadController getOffloadController(@NonNull Handler h, 67 @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps) { 68 final NetworkStatsManager statsManager = 69 (NetworkStatsManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE); 70 return new OffloadController(h, getOffloadHardwareInterface(h, log), 71 getContext().getContentResolver(), statsManager, log, deps); 72 } 73 74 75 /** 76 * Get a reference to the UpstreamNetworkMonitor to be used by tethering. 77 */ getUpstreamNetworkMonitor(Context ctx, StateMachine target, SharedLog log, int what)78 public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, StateMachine target, 79 SharedLog log, int what) { 80 return new UpstreamNetworkMonitor(ctx, target, log, what); 81 } 82 83 /** 84 * Get a reference to the IPv6TetheringCoordinator to be used by tethering. 85 */ getIPv6TetheringCoordinator( ArrayList<IpServer> notifyList, SharedLog log)86 public IPv6TetheringCoordinator getIPv6TetheringCoordinator( 87 ArrayList<IpServer> notifyList, SharedLog log) { 88 return new IPv6TetheringCoordinator(notifyList, log); 89 } 90 91 /** 92 * Get dependencies to be used by IpServer. 93 */ getIpServerDependencies()94 public abstract IpServer.Dependencies getIpServerDependencies(); 95 96 /** 97 * Get a reference to the EntitlementManager to be used by tethering. 98 */ getEntitlementManager(Context ctx, Handler h, SharedLog log, Runnable callback)99 public EntitlementManager getEntitlementManager(Context ctx, Handler h, SharedLog log, 100 Runnable callback) { 101 return new EntitlementManager(ctx, h, log, callback); 102 } 103 104 /** 105 * Generate a new TetheringConfiguration according to input sub Id. 106 */ generateTetheringConfiguration(Context ctx, SharedLog log, int subId)107 public TetheringConfiguration generateTetheringConfiguration(Context ctx, SharedLog log, 108 int subId) { 109 return new TetheringConfiguration(ctx, log, subId); 110 } 111 112 /** 113 * Get a reference to INetd to be used by tethering. 114 */ getINetd(Context context)115 public INetd getINetd(Context context) { 116 return INetd.Stub.asInterface( 117 (IBinder) context.getSystemService(Context.NETD_SERVICE)); 118 } 119 120 /** 121 * Get a reference to the TetheringNotificationUpdater to be used by tethering. 122 */ getNotificationUpdater(@onNull final Context ctx, @NonNull final Looper looper)123 public TetheringNotificationUpdater getNotificationUpdater(@NonNull final Context ctx, 124 @NonNull final Looper looper) { 125 return new TetheringNotificationUpdater(ctx, looper); 126 } 127 128 /** 129 * Get tethering thread looper. 130 */ getTetheringLooper()131 public abstract Looper getTetheringLooper(); 132 133 /** 134 * Get Context of TetheringSerice. 135 */ getContext()136 public abstract Context getContext(); 137 138 /** 139 * Get a reference to BluetoothAdapter to be used by tethering. 140 */ getBluetoothAdapter()141 public abstract BluetoothAdapter getBluetoothAdapter(); 142 143 /** 144 * Get SystemProperties which indicate whether tethering is denied. 145 */ isTetheringDenied()146 public boolean isTetheringDenied() { 147 return TextUtils.equals(SystemProperties.get("ro.tether.denied"), "true"); 148 } 149 150 /** 151 * Get a reference to PrivateAddressCoordinator to be used by Tethering. 152 */ getPrivateAddressCoordinator(Context ctx, TetheringConfiguration cfg)153 public PrivateAddressCoordinator getPrivateAddressCoordinator(Context ctx, 154 TetheringConfiguration cfg) { 155 return new PrivateAddressCoordinator(ctx, cfg); 156 } 157 158 /** 159 * Get BluetoothPanShim object to enable/disable bluetooth tethering. 160 * 161 * TODO: use BluetoothPan directly when mainline module is built with API 32. 162 */ getBluetoothPanShim(BluetoothPan pan)163 public BluetoothPanShim getBluetoothPanShim(BluetoothPan pan) { 164 return BluetoothPanShimImpl.newInstance(pan); 165 } 166 } 167