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