Tethering.java revision 9c7e2c2cb491268db0322ad78856faf0582e61d1
1/*
2 * Copyright (C) 2010 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
17package com.android.server.connectivity;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.pm.PackageManager;
27import android.content.res.Resources;
28import android.hardware.usb.UsbManager;
29import android.net.ConnectivityManager;
30import android.net.IConnectivityManager;
31import android.net.INetworkStatsService;
32import android.net.InterfaceConfiguration;
33import android.net.LinkAddress;
34import android.net.LinkProperties;
35import android.net.NetworkInfo;
36import android.net.NetworkUtils;
37import android.net.RouteInfo;
38import android.os.Binder;
39import android.os.INetworkManagementService;
40import android.os.Looper;
41import android.os.Message;
42import android.os.RemoteException;
43import android.os.UserHandle;
44import android.provider.Settings;
45import android.util.Log;
46
47import com.android.internal.telephony.Phone;
48import com.android.internal.telephony.PhoneConstants;
49import com.android.internal.util.IState;
50import com.android.internal.util.State;
51import com.android.internal.util.StateMachine;
52import com.android.server.IoThread;
53import com.android.server.net.BaseNetworkObserver;
54
55import java.io.FileDescriptor;
56import java.io.PrintWriter;
57import java.net.InetAddress;
58import java.net.Inet4Address;
59import java.util.ArrayList;
60import java.util.Collection;
61import java.util.HashMap;
62import java.util.Iterator;
63import java.util.Set;
64
65/**
66 * @hide
67 *
68 * Timeout
69 *
70 * TODO - look for parent classes and code sharing
71 */
72public class Tethering extends BaseNetworkObserver {
73
74    private Context mContext;
75    private final static String TAG = "Tethering";
76    private final static boolean DBG = true;
77    private final static boolean VDBG = false;
78
79    // TODO - remove both of these - should be part of interface inspection/selection stuff
80    private String[] mTetherableUsbRegexs;
81    private String[] mTetherableWifiRegexs;
82    private String[] mTetherableBluetoothRegexs;
83    private Collection<Integer> mUpstreamIfaceTypes;
84
85    // used to synchronize public access to members
86    private Object mPublicSync;
87
88    private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE);
89    private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI);
90    private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN);
91
92    // if we have to connect to mobile, what APN type should we use?  Calculated by examining the
93    // upstream type list and the DUN_REQUIRED secure-setting
94    private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE;
95
96    private final INetworkManagementService mNMService;
97    private final INetworkStatsService mStatsService;
98    private final IConnectivityManager mConnService;
99    private Looper mLooper;
100
101    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
102
103    private BroadcastReceiver mStateReceiver;
104
105    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
106    private static final int USB_PREFIX_LENGTH        = 24;
107
108    // USB is  192.168.42.1 and 255.255.255.0
109    // Wifi is 192.168.43.1 and 255.255.255.0
110    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
111    // with 255.255.255.0
112    // P2P is 192.168.49.1 and 255.255.255.0
113
114    private String[] mDhcpRange;
115    private static final String[] DHCP_DEFAULT_RANGE = {
116        "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
117        "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
118        "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
119        "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
120    };
121
122    private String[] mDefaultDnsServers;
123    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
124    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
125
126    private StateMachine mTetherMasterSM;
127
128    private Notification mTetheredNotification;
129
130    private boolean mRndisEnabled;       // track the RNDIS function enabled state
131    private boolean mUsbTetherRequested; // true if USB tethering should be started
132                                         // when RNDIS is enabled
133
134    public Tethering(Context context, INetworkManagementService nmService,
135            INetworkStatsService statsService, IConnectivityManager connService, Looper looper) {
136        mContext = context;
137        mNMService = nmService;
138        mStatsService = statsService;
139        mConnService = connService;
140        mLooper = looper;
141
142        mPublicSync = new Object();
143
144        mIfaces = new HashMap<String, TetherInterfaceSM>();
145
146        // make our own thread so we don't anr the system
147        mLooper = IoThread.get().getLooper();
148        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
149        mTetherMasterSM.start();
150
151        mStateReceiver = new StateReceiver();
152        IntentFilter filter = new IntentFilter();
153        filter.addAction(UsbManager.ACTION_USB_STATE);
154        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
155        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
156        mContext.registerReceiver(mStateReceiver, filter);
157
158        filter = new IntentFilter();
159        filter.addAction(Intent.ACTION_MEDIA_SHARED);
160        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
161        filter.addDataScheme("file");
162        mContext.registerReceiver(mStateReceiver, filter);
163
164        mDhcpRange = context.getResources().getStringArray(
165                com.android.internal.R.array.config_tether_dhcp_range);
166        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
167            mDhcpRange = DHCP_DEFAULT_RANGE;
168        }
169
170        // load device config info
171        updateConfiguration();
172
173        // TODO - remove and rely on real notifications of the current iface
174        mDefaultDnsServers = new String[2];
175        mDefaultDnsServers[0] = DNS_DEFAULT_SERVER1;
176        mDefaultDnsServers[1] = DNS_DEFAULT_SERVER2;
177    }
178
179    void updateConfiguration() {
180        String[] tetherableUsbRegexs = mContext.getResources().getStringArray(
181                com.android.internal.R.array.config_tether_usb_regexs);
182        String[] tetherableWifiRegexs = mContext.getResources().getStringArray(
183                com.android.internal.R.array.config_tether_wifi_regexs);
184        String[] tetherableBluetoothRegexs = mContext.getResources().getStringArray(
185                com.android.internal.R.array.config_tether_bluetooth_regexs);
186
187        int ifaceTypes[] = mContext.getResources().getIntArray(
188                com.android.internal.R.array.config_tether_upstream_types);
189        Collection<Integer> upstreamIfaceTypes = new ArrayList();
190        for (int i : ifaceTypes) {
191            upstreamIfaceTypes.add(new Integer(i));
192        }
193
194        synchronized (mPublicSync) {
195            mTetherableUsbRegexs = tetherableUsbRegexs;
196            mTetherableWifiRegexs = tetherableWifiRegexs;
197            mTetherableBluetoothRegexs = tetherableBluetoothRegexs;
198            mUpstreamIfaceTypes = upstreamIfaceTypes;
199        }
200
201        // check if the upstream type list needs to be modified due to secure-settings
202        checkDunRequired();
203    }
204
205    public void interfaceStatusChanged(String iface, boolean up) {
206        if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
207        boolean found = false;
208        boolean usb = false;
209        synchronized (mPublicSync) {
210            if (isWifi(iface)) {
211                found = true;
212            } else if (isUsb(iface)) {
213                found = true;
214                usb = true;
215            } else if (isBluetooth(iface)) {
216                found = true;
217            }
218            if (found == false) return;
219
220            TetherInterfaceSM sm = mIfaces.get(iface);
221            if (up) {
222                if (sm == null) {
223                    sm = new TetherInterfaceSM(iface, mLooper, usb);
224                    mIfaces.put(iface, sm);
225                    sm.start();
226                }
227            } else {
228                if (isUsb(iface)) {
229                    // ignore usb0 down after enabling RNDIS
230                    // we will handle disconnect in interfaceRemoved instead
231                    if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
232                } else if (sm != null) {
233                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
234                    mIfaces.remove(iface);
235                }
236            }
237        }
238    }
239
240    public void interfaceLinkStateChanged(String iface, boolean up) {
241        if (VDBG) Log.d(TAG, "interfaceLinkStateChanged " + iface + ", " + up);
242        interfaceStatusChanged(iface, up);
243    }
244
245    private boolean isUsb(String iface) {
246        synchronized (mPublicSync) {
247            for (String regex : mTetherableUsbRegexs) {
248                if (iface.matches(regex)) return true;
249            }
250            return false;
251        }
252    }
253
254    public boolean isWifi(String iface) {
255        synchronized (mPublicSync) {
256            for (String regex : mTetherableWifiRegexs) {
257                if (iface.matches(regex)) return true;
258            }
259            return false;
260        }
261    }
262
263    public boolean isBluetooth(String iface) {
264        synchronized (mPublicSync) {
265            for (String regex : mTetherableBluetoothRegexs) {
266                if (iface.matches(regex)) return true;
267            }
268            return false;
269        }
270    }
271
272    public void interfaceAdded(String iface) {
273        if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
274        boolean found = false;
275        boolean usb = false;
276        synchronized (mPublicSync) {
277            if (isWifi(iface)) {
278                found = true;
279            }
280            if (isUsb(iface)) {
281                found = true;
282                usb = true;
283            }
284            if (isBluetooth(iface)) {
285                found = true;
286            }
287            if (found == false) {
288                if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
289                return;
290            }
291
292            TetherInterfaceSM sm = mIfaces.get(iface);
293            if (sm != null) {
294                if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
295                return;
296            }
297            sm = new TetherInterfaceSM(iface, mLooper, usb);
298            mIfaces.put(iface, sm);
299            sm.start();
300        }
301    }
302
303    public void interfaceRemoved(String iface) {
304        if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
305        synchronized (mPublicSync) {
306            TetherInterfaceSM sm = mIfaces.get(iface);
307            if (sm == null) {
308                if (VDBG) {
309                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
310                }
311                return;
312            }
313            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
314            mIfaces.remove(iface);
315        }
316    }
317
318    public int tether(String iface) {
319        if (DBG) Log.d(TAG, "Tethering " + iface);
320        TetherInterfaceSM sm = null;
321        synchronized (mPublicSync) {
322            sm = mIfaces.get(iface);
323        }
324        if (sm == null) {
325            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
326            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
327        }
328        if (!sm.isAvailable() && !sm.isErrored()) {
329            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
330            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
331        }
332        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
333        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
334    }
335
336    public int untether(String iface) {
337        if (DBG) Log.d(TAG, "Untethering " + iface);
338        TetherInterfaceSM sm = null;
339        synchronized (mPublicSync) {
340            sm = mIfaces.get(iface);
341        }
342        if (sm == null) {
343            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
344            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
345        }
346        if (sm.isErrored()) {
347            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
348            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
349        }
350        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
351        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
352    }
353
354    public int getLastTetherError(String iface) {
355        TetherInterfaceSM sm = null;
356        synchronized (mPublicSync) {
357            sm = mIfaces.get(iface);
358            if (sm == null) {
359                Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
360                        ", ignoring");
361                return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
362            }
363            return sm.getLastError();
364        }
365    }
366
367    // TODO - move all private methods used only by the state machine into the state machine
368    // to clarify what needs synchronized protection.
369    private void sendTetherStateChangedBroadcast() {
370        try {
371            if (!mConnService.isTetheringSupported()) return;
372        } catch (RemoteException e) {
373            return;
374        }
375
376        ArrayList<String> availableList = new ArrayList<String>();
377        ArrayList<String> activeList = new ArrayList<String>();
378        ArrayList<String> erroredList = new ArrayList<String>();
379
380        boolean wifiTethered = false;
381        boolean usbTethered = false;
382        boolean bluetoothTethered = false;
383
384        synchronized (mPublicSync) {
385            Set ifaces = mIfaces.keySet();
386            for (Object iface : ifaces) {
387                TetherInterfaceSM sm = mIfaces.get(iface);
388                if (sm != null) {
389                    if (sm.isErrored()) {
390                        erroredList.add((String)iface);
391                    } else if (sm.isAvailable()) {
392                        availableList.add((String)iface);
393                    } else if (sm.isTethered()) {
394                        if (isUsb((String)iface)) {
395                            usbTethered = true;
396                        } else if (isWifi((String)iface)) {
397                            wifiTethered = true;
398                      } else if (isBluetooth((String)iface)) {
399                            bluetoothTethered = true;
400                        }
401                        activeList.add((String)iface);
402                    }
403                }
404            }
405        }
406        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
407        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
408                Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
409        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
410                availableList);
411        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
412        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
413                erroredList);
414        mContext.sendStickyBroadcastAsUser(broadcast, UserHandle.ALL);
415        if (DBG) {
416            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
417                    activeList.size() + ", " + erroredList.size());
418        }
419
420        if (usbTethered) {
421            if (wifiTethered || bluetoothTethered) {
422                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
423            } else {
424                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
425            }
426        } else if (wifiTethered) {
427            if (bluetoothTethered) {
428                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
429            } else {
430                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
431            }
432        } else if (bluetoothTethered) {
433            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
434        } else {
435            clearTetheredNotification();
436        }
437    }
438
439    private void showTetheredNotification(int icon) {
440        NotificationManager notificationManager =
441                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
442        if (notificationManager == null) {
443            return;
444        }
445
446        if (mTetheredNotification != null) {
447            if (mTetheredNotification.icon == icon) {
448                return;
449            }
450            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
451                    UserHandle.ALL);
452        }
453
454        Intent intent = new Intent();
455        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
456        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
457
458        PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
459                null, UserHandle.CURRENT);
460
461        Resources r = Resources.getSystem();
462        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
463        CharSequence message = r.getText(com.android.internal.R.string.
464                tethered_notification_message);
465
466        if (mTetheredNotification == null) {
467            mTetheredNotification = new Notification();
468            mTetheredNotification.when = 0;
469        }
470        mTetheredNotification.icon = icon;
471        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
472        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
473        mTetheredNotification.tickerText = title;
474        mTetheredNotification.visibility = Notification.VISIBILITY_PUBLIC;
475        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
476        mTetheredNotification.category = Notification.CATEGORY_STATUS;
477
478        notificationManager.notifyAsUser(null, mTetheredNotification.icon,
479                mTetheredNotification, UserHandle.ALL);
480    }
481
482    private void clearTetheredNotification() {
483        NotificationManager notificationManager =
484            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
485        if (notificationManager != null && mTetheredNotification != null) {
486            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
487                    UserHandle.ALL);
488            mTetheredNotification = null;
489        }
490    }
491
492    private class StateReceiver extends BroadcastReceiver {
493        @Override
494        public void onReceive(Context content, Intent intent) {
495            String action = intent.getAction();
496            if (action == null) { return; }
497            if (action.equals(UsbManager.ACTION_USB_STATE)) {
498                synchronized (Tethering.this.mPublicSync) {
499                    boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
500                    mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false);
501                    // start tethering if we have a request pending
502                    if (usbConnected && mRndisEnabled && mUsbTetherRequested) {
503                        tetherUsb(true);
504                    }
505                    mUsbTetherRequested = false;
506                }
507            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
508                NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(
509                        ConnectivityManager.EXTRA_NETWORK_INFO);
510                if (networkInfo != null &&
511                        networkInfo.getDetailedState() != NetworkInfo.DetailedState.FAILED) {
512                    if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
513                    mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
514                }
515            } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
516                updateConfiguration();
517            }
518        }
519    }
520
521    private void tetherUsb(boolean enable) {
522        if (VDBG) Log.d(TAG, "tetherUsb " + enable);
523
524        String[] ifaces = new String[0];
525        try {
526            ifaces = mNMService.listInterfaces();
527        } catch (Exception e) {
528            Log.e(TAG, "Error listing Interfaces", e);
529            return;
530        }
531        for (String iface : ifaces) {
532            if (isUsb(iface)) {
533                int result = (enable ? tether(iface) : untether(iface));
534                if (result == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
535                    return;
536                }
537            }
538        }
539        Log.e(TAG, "unable start or stop USB tethering");
540    }
541
542    // configured when we start tethering and unconfig'd on error or conclusion
543    private boolean configureUsbIface(boolean enabled) {
544        if (VDBG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
545
546        // toggle the USB interfaces
547        String[] ifaces = new String[0];
548        try {
549            ifaces = mNMService.listInterfaces();
550        } catch (Exception e) {
551            Log.e(TAG, "Error listing Interfaces", e);
552            return false;
553        }
554        for (String iface : ifaces) {
555            if (isUsb(iface)) {
556                InterfaceConfiguration ifcg = null;
557                try {
558                    ifcg = mNMService.getInterfaceConfig(iface);
559                    if (ifcg != null) {
560                        InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
561                        ifcg.setLinkAddress(new LinkAddress(addr, USB_PREFIX_LENGTH));
562                        if (enabled) {
563                            ifcg.setInterfaceUp();
564                        } else {
565                            ifcg.setInterfaceDown();
566                        }
567                        ifcg.clearFlag("running");
568                        mNMService.setInterfaceConfig(iface, ifcg);
569                    }
570                } catch (Exception e) {
571                    Log.e(TAG, "Error configuring interface " + iface, e);
572                    return false;
573                }
574            }
575         }
576
577        return true;
578    }
579
580    // TODO - return copies so people can't tamper
581    public String[] getTetherableUsbRegexs() {
582        return mTetherableUsbRegexs;
583    }
584
585    public String[] getTetherableWifiRegexs() {
586        return mTetherableWifiRegexs;
587    }
588
589    public String[] getTetherableBluetoothRegexs() {
590        return mTetherableBluetoothRegexs;
591    }
592
593    public int setUsbTethering(boolean enable) {
594        if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
595        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
596
597        synchronized (mPublicSync) {
598            if (enable) {
599                if (mRndisEnabled) {
600                    tetherUsb(true);
601                } else {
602                    mUsbTetherRequested = true;
603                    usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
604                }
605            } else {
606                tetherUsb(false);
607                if (mRndisEnabled) {
608                    usbManager.setCurrentFunction(null, false);
609                }
610                mUsbTetherRequested = false;
611            }
612        }
613        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
614    }
615
616    public int[] getUpstreamIfaceTypes() {
617        int values[];
618        synchronized (mPublicSync) {
619            updateConfiguration();  // TODO - remove?
620            values = new int[mUpstreamIfaceTypes.size()];
621            Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
622            for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
623                values[i] = iterator.next();
624            }
625        }
626        return values;
627    }
628
629    public void checkDunRequired() {
630        int secureSetting = Settings.Global.getInt(mContext.getContentResolver(),
631                Settings.Global.TETHER_DUN_REQUIRED, 2);
632        synchronized (mPublicSync) {
633            // 2 = not set, 0 = DUN not required, 1 = DUN required
634            if (secureSetting != 2) {
635                int requiredApn = (secureSetting == 1 ?
636                        ConnectivityManager.TYPE_MOBILE_DUN :
637                        ConnectivityManager.TYPE_MOBILE_HIPRI);
638                if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
639                    while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
640                        mUpstreamIfaceTypes.remove(MOBILE_TYPE);
641                    }
642                    while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
643                        mUpstreamIfaceTypes.remove(HIPRI_TYPE);
644                    }
645                    if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
646                        mUpstreamIfaceTypes.add(DUN_TYPE);
647                    }
648                } else {
649                    while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
650                        mUpstreamIfaceTypes.remove(DUN_TYPE);
651                    }
652                    if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
653                        mUpstreamIfaceTypes.add(MOBILE_TYPE);
654                    }
655                    if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
656                        mUpstreamIfaceTypes.add(HIPRI_TYPE);
657                    }
658                }
659            }
660            if (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
661                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_DUN;
662            } else {
663                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_HIPRI;
664            }
665        }
666    }
667
668    // TODO review API - maybe return ArrayList<String> here and below?
669    public String[] getTetheredIfaces() {
670        ArrayList<String> list = new ArrayList<String>();
671        synchronized (mPublicSync) {
672            Set keys = mIfaces.keySet();
673            for (Object key : keys) {
674                TetherInterfaceSM sm = mIfaces.get(key);
675                if (sm.isTethered()) {
676                    list.add((String)key);
677                }
678            }
679        }
680        String[] retVal = new String[list.size()];
681        for (int i=0; i < list.size(); i++) {
682            retVal[i] = list.get(i);
683        }
684        return retVal;
685    }
686
687    public String[] getTetherableIfaces() {
688        ArrayList<String> list = new ArrayList<String>();
689        synchronized (mPublicSync) {
690            Set keys = mIfaces.keySet();
691            for (Object key : keys) {
692                TetherInterfaceSM sm = mIfaces.get(key);
693                if (sm.isAvailable()) {
694                    list.add((String)key);
695                }
696            }
697        }
698        String[] retVal = new String[list.size()];
699        for (int i=0; i < list.size(); i++) {
700            retVal[i] = list.get(i);
701        }
702        return retVal;
703    }
704
705    public String[] getTetheredDhcpRanges() {
706        return mDhcpRange;
707    }
708
709    public String[] getErroredIfaces() {
710        ArrayList<String> list = new ArrayList<String>();
711        synchronized (mPublicSync) {
712            Set keys = mIfaces.keySet();
713            for (Object key : keys) {
714                TetherInterfaceSM sm = mIfaces.get(key);
715                if (sm.isErrored()) {
716                    list.add((String)key);
717                }
718            }
719        }
720        String[] retVal = new String[list.size()];
721        for (int i= 0; i< list.size(); i++) {
722            retVal[i] = list.get(i);
723        }
724        return retVal;
725    }
726
727    //TODO: Temporary handling upstream change triggered without
728    //      CONNECTIVITY_ACTION. Only to accomodate interface
729    //      switch during HO.
730    //      @see bug/4455071
731    public void handleTetherIfaceChange() {
732        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
733    }
734
735    class TetherInterfaceSM extends StateMachine {
736        // notification from the master SM that it's not in tether mode
737        static final int CMD_TETHER_MODE_DEAD            =  1;
738        // request from the user that it wants to tether
739        static final int CMD_TETHER_REQUESTED            =  2;
740        // request from the user that it wants to untether
741        static final int CMD_TETHER_UNREQUESTED          =  3;
742        // notification that this interface is down
743        static final int CMD_INTERFACE_DOWN              =  4;
744        // notification that this interface is up
745        static final int CMD_INTERFACE_UP                =  5;
746        // notification from the master SM that it had an error turning on cellular dun
747        static final int CMD_CELL_DUN_ERROR              =  6;
748        // notification from the master SM that it had trouble enabling IP Forwarding
749        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
750        // notification from the master SM that it had trouble disabling IP Forwarding
751        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
752        // notification from the master SM that it had trouble staring tethering
753        static final int CMD_START_TETHERING_ERROR       =  9;
754        // notification from the master SM that it had trouble stopping tethering
755        static final int CMD_STOP_TETHERING_ERROR        = 10;
756        // notification from the master SM that it had trouble setting the DNS forwarders
757        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
758        // the upstream connection has changed
759        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
760
761        private State mDefaultState;
762
763        private State mInitialState;
764        private State mStartingState;
765        private State mTetheredState;
766
767        private State mUnavailableState;
768
769        private boolean mAvailable;
770        private boolean mTethered;
771        int mLastError;
772
773        String mIfaceName;
774        String mMyUpstreamIfaceName;  // may change over time
775
776        boolean mUsb;
777
778        TetherInterfaceSM(String name, Looper looper, boolean usb) {
779            super(name, looper);
780            mIfaceName = name;
781            mUsb = usb;
782            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
783
784            mInitialState = new InitialState();
785            addState(mInitialState);
786            mStartingState = new StartingState();
787            addState(mStartingState);
788            mTetheredState = new TetheredState();
789            addState(mTetheredState);
790            mUnavailableState = new UnavailableState();
791            addState(mUnavailableState);
792
793            setInitialState(mInitialState);
794        }
795
796        public String toString() {
797            String res = new String();
798            res += mIfaceName + " - ";
799            IState current = getCurrentState();
800            if (current == mInitialState) res += "InitialState";
801            if (current == mStartingState) res += "StartingState";
802            if (current == mTetheredState) res += "TetheredState";
803            if (current == mUnavailableState) res += "UnavailableState";
804            if (mAvailable) res += " - Available";
805            if (mTethered) res += " - Tethered";
806            res += " - lastError =" + mLastError;
807            return res;
808        }
809
810        public int getLastError() {
811            synchronized (Tethering.this.mPublicSync) {
812                return mLastError;
813            }
814        }
815
816        private void setLastError(int error) {
817            synchronized (Tethering.this.mPublicSync) {
818                mLastError = error;
819
820                if (isErrored()) {
821                    if (mUsb) {
822                        // note everything's been unwound by this point so nothing to do on
823                        // further error..
824                        Tethering.this.configureUsbIface(false);
825                    }
826                }
827            }
828        }
829
830        public boolean isAvailable() {
831            synchronized (Tethering.this.mPublicSync) {
832                return mAvailable;
833            }
834        }
835
836        private void setAvailable(boolean available) {
837            synchronized (Tethering.this.mPublicSync) {
838                mAvailable = available;
839            }
840        }
841
842        public boolean isTethered() {
843            synchronized (Tethering.this.mPublicSync) {
844                return mTethered;
845            }
846        }
847
848        private void setTethered(boolean tethered) {
849            synchronized (Tethering.this.mPublicSync) {
850                mTethered = tethered;
851            }
852        }
853
854        public boolean isErrored() {
855            synchronized (Tethering.this.mPublicSync) {
856                return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
857            }
858        }
859
860        class InitialState extends State {
861            @Override
862            public void enter() {
863                setAvailable(true);
864                setTethered(false);
865                sendTetherStateChangedBroadcast();
866            }
867
868            @Override
869            public boolean processMessage(Message message) {
870                if (DBG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
871                boolean retValue = true;
872                switch (message.what) {
873                    case CMD_TETHER_REQUESTED:
874                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
875                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
876                                TetherInterfaceSM.this);
877                        transitionTo(mStartingState);
878                        break;
879                    case CMD_INTERFACE_DOWN:
880                        transitionTo(mUnavailableState);
881                        break;
882                    default:
883                        retValue = false;
884                        break;
885                }
886                return retValue;
887            }
888        }
889
890        class StartingState extends State {
891            @Override
892            public void enter() {
893                setAvailable(false);
894                if (mUsb) {
895                    if (!Tethering.this.configureUsbIface(true)) {
896                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
897                                TetherInterfaceSM.this);
898                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
899
900                        transitionTo(mInitialState);
901                        return;
902                    }
903                }
904                sendTetherStateChangedBroadcast();
905
906                // Skipping StartingState
907                transitionTo(mTetheredState);
908            }
909            @Override
910            public boolean processMessage(Message message) {
911                if (DBG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
912                boolean retValue = true;
913                switch (message.what) {
914                    // maybe a parent class?
915                    case CMD_TETHER_UNREQUESTED:
916                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
917                                TetherInterfaceSM.this);
918                        if (mUsb) {
919                            if (!Tethering.this.configureUsbIface(false)) {
920                                setLastErrorAndTransitionToInitialState(
921                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
922                                break;
923                            }
924                        }
925                        transitionTo(mInitialState);
926                        break;
927                    case CMD_CELL_DUN_ERROR:
928                    case CMD_IP_FORWARDING_ENABLE_ERROR:
929                    case CMD_IP_FORWARDING_DISABLE_ERROR:
930                    case CMD_START_TETHERING_ERROR:
931                    case CMD_STOP_TETHERING_ERROR:
932                    case CMD_SET_DNS_FORWARDERS_ERROR:
933                        setLastErrorAndTransitionToInitialState(
934                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
935                        break;
936                    case CMD_INTERFACE_DOWN:
937                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
938                                TetherInterfaceSM.this);
939                        transitionTo(mUnavailableState);
940                        break;
941                    default:
942                        retValue = false;
943                }
944                return retValue;
945            }
946        }
947
948        class TetheredState extends State {
949            @Override
950            public void enter() {
951                try {
952                    mNMService.tetherInterface(mIfaceName);
953                } catch (Exception e) {
954                    Log.e(TAG, "Error Tethering: " + e.toString());
955                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
956
957                    transitionTo(mInitialState);
958                    return;
959                }
960                if (DBG) Log.d(TAG, "Tethered " + mIfaceName);
961                setAvailable(false);
962                setTethered(true);
963                sendTetherStateChangedBroadcast();
964            }
965
966            private void cleanupUpstream() {
967                if (mMyUpstreamIfaceName != null) {
968                    // note that we don't care about errors here.
969                    // sometimes interfaces are gone before we get
970                    // to remove their rules, which generates errors.
971                    // just do the best we can.
972                    try {
973                        // about to tear down NAT; gather remaining statistics
974                        mStatsService.forceUpdate();
975                    } catch (Exception e) {
976                        if (VDBG) Log.e(TAG, "Exception in forceUpdate: " + e.toString());
977                    }
978                    try {
979                        mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
980                    } catch (Exception e) {
981                        if (VDBG) Log.e(TAG, "Exception in disableNat: " + e.toString());
982                    }
983                    mMyUpstreamIfaceName = null;
984                }
985                return;
986            }
987
988            @Override
989            public boolean processMessage(Message message) {
990                if (DBG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
991                boolean retValue = true;
992                boolean error = false;
993                switch (message.what) {
994                    case CMD_TETHER_UNREQUESTED:
995                    case CMD_INTERFACE_DOWN:
996                        cleanupUpstream();
997                        try {
998                            mNMService.untetherInterface(mIfaceName);
999                        } catch (Exception e) {
1000                            setLastErrorAndTransitionToInitialState(
1001                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
1002                            break;
1003                        }
1004                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
1005                                TetherInterfaceSM.this);
1006                        if (message.what == CMD_TETHER_UNREQUESTED) {
1007                            if (mUsb) {
1008                                if (!Tethering.this.configureUsbIface(false)) {
1009                                    setLastError(
1010                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
1011                                }
1012                            }
1013                            transitionTo(mInitialState);
1014                        } else if (message.what == CMD_INTERFACE_DOWN) {
1015                            transitionTo(mUnavailableState);
1016                        }
1017                        if (DBG) Log.d(TAG, "Untethered " + mIfaceName);
1018                        break;
1019                    case CMD_TETHER_CONNECTION_CHANGED:
1020                        String newUpstreamIfaceName = (String)(message.obj);
1021                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
1022                                (mMyUpstreamIfaceName != null &&
1023                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
1024                            if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
1025                            break;
1026                        }
1027                        cleanupUpstream();
1028                        if (newUpstreamIfaceName != null) {
1029                            try {
1030                                mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
1031                            } catch (Exception e) {
1032                                Log.e(TAG, "Exception enabling Nat: " + e.toString());
1033                                try {
1034                                    mNMService.untetherInterface(mIfaceName);
1035                                } catch (Exception ee) {}
1036
1037                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
1038                                transitionTo(mInitialState);
1039                                return true;
1040                            }
1041                        }
1042                        mMyUpstreamIfaceName = newUpstreamIfaceName;
1043                        break;
1044                    case CMD_CELL_DUN_ERROR:
1045                    case CMD_IP_FORWARDING_ENABLE_ERROR:
1046                    case CMD_IP_FORWARDING_DISABLE_ERROR:
1047                    case CMD_START_TETHERING_ERROR:
1048                    case CMD_STOP_TETHERING_ERROR:
1049                    case CMD_SET_DNS_FORWARDERS_ERROR:
1050                        error = true;
1051                        // fall through
1052                    case CMD_TETHER_MODE_DEAD:
1053                        cleanupUpstream();
1054                        try {
1055                            mNMService.untetherInterface(mIfaceName);
1056                        } catch (Exception e) {
1057                            setLastErrorAndTransitionToInitialState(
1058                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
1059                            break;
1060                        }
1061                        if (error) {
1062                            setLastErrorAndTransitionToInitialState(
1063                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
1064                            break;
1065                        }
1066                        if (DBG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
1067                        sendTetherStateChangedBroadcast();
1068                        if (mUsb) {
1069                            if (!Tethering.this.configureUsbIface(false)) {
1070                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
1071                            }
1072                        }
1073                        transitionTo(mInitialState);
1074                        break;
1075                    default:
1076                        retValue = false;
1077                        break;
1078                }
1079                return retValue;
1080            }
1081        }
1082
1083        class UnavailableState extends State {
1084            @Override
1085            public void enter() {
1086                setAvailable(false);
1087                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
1088                setTethered(false);
1089                sendTetherStateChangedBroadcast();
1090            }
1091            @Override
1092            public boolean processMessage(Message message) {
1093                boolean retValue = true;
1094                switch (message.what) {
1095                    case CMD_INTERFACE_UP:
1096                        transitionTo(mInitialState);
1097                        break;
1098                    default:
1099                        retValue = false;
1100                        break;
1101                }
1102                return retValue;
1103            }
1104        }
1105
1106        void setLastErrorAndTransitionToInitialState(int error) {
1107            setLastError(error);
1108            transitionTo(mInitialState);
1109        }
1110
1111    }
1112
1113    class TetherMasterSM extends StateMachine {
1114        // an interface SM has requested Tethering
1115        static final int CMD_TETHER_MODE_REQUESTED   = 1;
1116        // an interface SM has unrequested Tethering
1117        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
1118        // upstream connection change - do the right thing
1119        static final int CMD_UPSTREAM_CHANGED        = 3;
1120        // we received notice that the cellular DUN connection is up
1121        static final int CMD_CELL_CONNECTION_RENEW   = 4;
1122        // we don't have a valid upstream conn, check again after a delay
1123        static final int CMD_RETRY_UPSTREAM          = 5;
1124
1125        // This indicates what a timeout event relates to.  A state that
1126        // sends itself a delayed timeout event and handles incoming timeout events
1127        // should inc this when it is entered and whenever it sends a new timeout event.
1128        // We do not flush the old ones.
1129        private int mSequenceNumber;
1130
1131        private State mInitialState;
1132        private State mTetherModeAliveState;
1133
1134        private State mSetIpForwardingEnabledErrorState;
1135        private State mSetIpForwardingDisabledErrorState;
1136        private State mStartTetheringErrorState;
1137        private State mStopTetheringErrorState;
1138        private State mSetDnsForwardersErrorState;
1139
1140        private ArrayList<TetherInterfaceSM> mNotifyList;
1141
1142        private int mCurrentConnectionSequence;
1143        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1144
1145        private String mUpstreamIfaceName = null;
1146
1147        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
1148        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
1149
1150        TetherMasterSM(String name, Looper looper) {
1151            super(name, looper);
1152
1153            //Add states
1154            mInitialState = new InitialState();
1155            addState(mInitialState);
1156            mTetherModeAliveState = new TetherModeAliveState();
1157            addState(mTetherModeAliveState);
1158
1159            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
1160            addState(mSetIpForwardingEnabledErrorState);
1161            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
1162            addState(mSetIpForwardingDisabledErrorState);
1163            mStartTetheringErrorState = new StartTetheringErrorState();
1164            addState(mStartTetheringErrorState);
1165            mStopTetheringErrorState = new StopTetheringErrorState();
1166            addState(mStopTetheringErrorState);
1167            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
1168            addState(mSetDnsForwardersErrorState);
1169
1170            mNotifyList = new ArrayList<TetherInterfaceSM>();
1171            setInitialState(mInitialState);
1172        }
1173
1174        class TetherMasterUtilState extends State {
1175            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
1176            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
1177
1178            @Override
1179            public boolean processMessage(Message m) {
1180                return false;
1181            }
1182            protected String enableString(int apnType) {
1183                switch (apnType) {
1184                case ConnectivityManager.TYPE_MOBILE_DUN:
1185                    return Phone.FEATURE_ENABLE_DUN_ALWAYS;
1186                case ConnectivityManager.TYPE_MOBILE:
1187                case ConnectivityManager.TYPE_MOBILE_HIPRI:
1188                    return Phone.FEATURE_ENABLE_HIPRI;
1189                }
1190                return null;
1191            }
1192            protected boolean turnOnUpstreamMobileConnection(int apnType) {
1193                boolean retValue = true;
1194                if (apnType == ConnectivityManager.TYPE_NONE) return false;
1195                if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection();
1196                int result = PhoneConstants.APN_REQUEST_FAILED;
1197                String enableString = enableString(apnType);
1198                if (enableString == null) return false;
1199                try {
1200                    result = mConnService.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1201                            enableString, new Binder());
1202                } catch (Exception e) {
1203                }
1204                switch (result) {
1205                case PhoneConstants.APN_ALREADY_ACTIVE:
1206                case PhoneConstants.APN_REQUEST_STARTED:
1207                    mMobileApnReserved = apnType;
1208                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
1209                    m.arg1 = ++mCurrentConnectionSequence;
1210                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
1211                    break;
1212                case PhoneConstants.APN_REQUEST_FAILED:
1213                default:
1214                    retValue = false;
1215                    break;
1216                }
1217
1218                return retValue;
1219            }
1220            protected boolean turnOffUpstreamMobileConnection() {
1221                // ignore pending renewal requests
1222                ++mCurrentConnectionSequence;
1223                if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) {
1224                    try {
1225                        mConnService.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1226                                enableString(mMobileApnReserved));
1227                    } catch (Exception e) {
1228                        return false;
1229                    }
1230                    mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1231                }
1232                return true;
1233            }
1234            protected boolean turnOnMasterTetherSettings() {
1235                try {
1236                    mNMService.setIpForwardingEnabled(true);
1237                } catch (Exception e) {
1238                    transitionTo(mSetIpForwardingEnabledErrorState);
1239                    return false;
1240                }
1241                try {
1242                    mNMService.startTethering(mDhcpRange);
1243                } catch (Exception e) {
1244                    try {
1245                        mNMService.stopTethering();
1246                        mNMService.startTethering(mDhcpRange);
1247                    } catch (Exception ee) {
1248                        transitionTo(mStartTetheringErrorState);
1249                        return false;
1250                    }
1251                }
1252                try {
1253                    mNMService.setDnsForwarders(mDefaultDnsServers);
1254                } catch (Exception e) {
1255                    transitionTo(mSetDnsForwardersErrorState);
1256                    return false;
1257                }
1258                return true;
1259            }
1260            protected boolean turnOffMasterTetherSettings() {
1261                try {
1262                    mNMService.stopTethering();
1263                } catch (Exception e) {
1264                    transitionTo(mStopTetheringErrorState);
1265                    return false;
1266                }
1267                try {
1268                    mNMService.setIpForwardingEnabled(false);
1269                } catch (Exception e) {
1270                    transitionTo(mSetIpForwardingDisabledErrorState);
1271                    return false;
1272                }
1273                transitionTo(mInitialState);
1274                return true;
1275            }
1276
1277            protected void chooseUpstreamType(boolean tryCell) {
1278                int upType = ConnectivityManager.TYPE_NONE;
1279                String iface = null;
1280
1281                updateConfiguration(); // TODO - remove?
1282
1283                synchronized (mPublicSync) {
1284                    if (VDBG) {
1285                        Log.d(TAG, "chooseUpstreamType has upstream iface types:");
1286                        for (Integer netType : mUpstreamIfaceTypes) {
1287                            Log.d(TAG, " " + netType);
1288                        }
1289                    }
1290
1291                    for (Integer netType : mUpstreamIfaceTypes) {
1292                        NetworkInfo info = null;
1293                        try {
1294                            info = mConnService.getNetworkInfo(netType.intValue());
1295                        } catch (RemoteException e) { }
1296                        if ((info != null) && info.isConnected()) {
1297                            upType = netType.intValue();
1298                            break;
1299                        }
1300                    }
1301                }
1302
1303                if (DBG) {
1304                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn ="
1305                            + mPreferredUpstreamMobileApn + ", got type=" + upType);
1306                }
1307
1308                // if we're on DUN, put our own grab on it
1309                if (upType == ConnectivityManager.TYPE_MOBILE_DUN ||
1310                        upType == ConnectivityManager.TYPE_MOBILE_HIPRI) {
1311                    turnOnUpstreamMobileConnection(upType);
1312                } else if (upType != ConnectivityManager.TYPE_NONE) {
1313                    /* If we've found an active upstream connection that's not DUN/HIPRI
1314                     * we should stop any outstanding DUN/HIPRI start requests.
1315                     *
1316                     * If we found NONE we don't want to do this as we want any previous
1317                     * requests to keep trying to bring up something we can use.
1318                     */
1319                    turnOffUpstreamMobileConnection();
1320                }
1321
1322                if (upType == ConnectivityManager.TYPE_NONE) {
1323                    boolean tryAgainLater = true;
1324                    if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) &&
1325                            (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) {
1326                        // we think mobile should be coming up - don't set a retry
1327                        tryAgainLater = false;
1328                    }
1329                    if (tryAgainLater) {
1330                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
1331                    }
1332                } else {
1333                    LinkProperties linkProperties = null;
1334                    try {
1335                        linkProperties = mConnService.getLinkPropertiesForType(upType);
1336                    } catch (RemoteException e) { }
1337                    if (linkProperties != null) {
1338                        // Find the interface with the default IPv4 route. It may be the
1339                        // interface described by linkProperties, or one of the interfaces
1340                        // stacked on top of it.
1341                        Log.i(TAG, "Finding IPv4 upstream interface on: " + linkProperties);
1342                        RouteInfo ipv4Default = RouteInfo.selectBestRoute(
1343                            linkProperties.getAllRoutes(), Inet4Address.ANY);
1344                        if (ipv4Default != null) {
1345                            iface = ipv4Default.getInterface();
1346                            Log.i(TAG, "Found interface " + ipv4Default.getInterface());
1347                        } else {
1348                            Log.i(TAG, "No IPv4 upstream interface, giving up.");
1349                        }
1350                    }
1351
1352                    if (iface != null) {
1353                        String[] dnsServers = mDefaultDnsServers;
1354                        Collection<InetAddress> dnses = linkProperties.getDnsServers();
1355                        if (dnses != null) {
1356                            // we currently only handle IPv4
1357                            ArrayList<InetAddress> v4Dnses =
1358                                    new ArrayList<InetAddress>(dnses.size());
1359                            for (InetAddress dnsAddress : dnses) {
1360                                if (dnsAddress instanceof Inet4Address) {
1361                                    v4Dnses.add(dnsAddress);
1362                                }
1363                            }
1364                            if (v4Dnses.size() > 0) {
1365                                dnsServers = NetworkUtils.makeStrings(v4Dnses);
1366                            }
1367                        }
1368                        try {
1369                            mNMService.setDnsForwarders(dnsServers);
1370                        } catch (Exception e) {
1371                            transitionTo(mSetDnsForwardersErrorState);
1372                        }
1373                    }
1374                }
1375                notifyTetheredOfNewUpstreamIface(iface);
1376            }
1377
1378            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1379                if (DBG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1380                mUpstreamIfaceName = ifaceName;
1381                for (TetherInterfaceSM sm : mNotifyList) {
1382                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1383                            ifaceName);
1384                }
1385            }
1386        }
1387
1388        class InitialState extends TetherMasterUtilState {
1389            @Override
1390            public void enter() {
1391            }
1392            @Override
1393            public boolean processMessage(Message message) {
1394                if (DBG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
1395                boolean retValue = true;
1396                switch (message.what) {
1397                    case CMD_TETHER_MODE_REQUESTED:
1398                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1399                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
1400                        mNotifyList.add(who);
1401                        transitionTo(mTetherModeAliveState);
1402                        break;
1403                    case CMD_TETHER_MODE_UNREQUESTED:
1404                        who = (TetherInterfaceSM)message.obj;
1405                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
1406                        int index = mNotifyList.indexOf(who);
1407                        if (index != -1) {
1408                            mNotifyList.remove(who);
1409                        }
1410                        break;
1411                    default:
1412                        retValue = false;
1413                        break;
1414                }
1415                return retValue;
1416            }
1417        }
1418
1419        class TetherModeAliveState extends TetherMasterUtilState {
1420            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
1421            @Override
1422            public void enter() {
1423                turnOnMasterTetherSettings(); // may transition us out
1424
1425                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
1426                                                        // or crazy tests cases will fail
1427                chooseUpstreamType(mTryCell);
1428                mTryCell = !mTryCell;
1429            }
1430            @Override
1431            public void exit() {
1432                turnOffUpstreamMobileConnection();
1433                notifyTetheredOfNewUpstreamIface(null);
1434            }
1435            @Override
1436            public boolean processMessage(Message message) {
1437                if (DBG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
1438                boolean retValue = true;
1439                switch (message.what) {
1440                    case CMD_TETHER_MODE_REQUESTED:
1441                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1442                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
1443                        mNotifyList.add(who);
1444                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1445                                mUpstreamIfaceName);
1446                        break;
1447                    case CMD_TETHER_MODE_UNREQUESTED:
1448                        who = (TetherInterfaceSM)message.obj;
1449                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
1450                        int index = mNotifyList.indexOf(who);
1451                        if (index != -1) {
1452                            if (DBG) Log.d(TAG, "TetherModeAlive removing notifyee " + who);
1453                            mNotifyList.remove(index);
1454                            if (mNotifyList.isEmpty()) {
1455                                turnOffMasterTetherSettings(); // transitions appropriately
1456                            } else {
1457                                if (DBG) {
1458                                    Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
1459                                            " live requests:");
1460                                    for (Object o : mNotifyList) Log.d(TAG, "  " + o);
1461                                }
1462                            }
1463                        } else {
1464                           Log.e(TAG, "TetherModeAliveState UNREQUESTED has unknown who: " + who);
1465                        }
1466                        break;
1467                    case CMD_UPSTREAM_CHANGED:
1468                        // need to try DUN immediately if Wifi goes down
1469                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
1470                        chooseUpstreamType(mTryCell);
1471                        mTryCell = !mTryCell;
1472                        break;
1473                    case CMD_CELL_CONNECTION_RENEW:
1474                        // make sure we're still using a requested connection - may have found
1475                        // wifi or something since then.
1476                        if (mCurrentConnectionSequence == message.arg1) {
1477                            if (VDBG) {
1478                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1479                                        CELL_CONNECTION_RENEW_MS + "ms");
1480                            }
1481                            turnOnUpstreamMobileConnection(mMobileApnReserved);
1482                        }
1483                        break;
1484                    case CMD_RETRY_UPSTREAM:
1485                        chooseUpstreamType(mTryCell);
1486                        mTryCell = !mTryCell;
1487                        break;
1488                    default:
1489                        retValue = false;
1490                        break;
1491                }
1492                return retValue;
1493            }
1494        }
1495
1496        class ErrorState extends State {
1497            int mErrorNotification;
1498            @Override
1499            public boolean processMessage(Message message) {
1500                boolean retValue = true;
1501                switch (message.what) {
1502                    case CMD_TETHER_MODE_REQUESTED:
1503                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1504                        who.sendMessage(mErrorNotification);
1505                        break;
1506                    default:
1507                       retValue = false;
1508                }
1509                return retValue;
1510            }
1511            void notify(int msgType) {
1512                mErrorNotification = msgType;
1513                for (Object o : mNotifyList) {
1514                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
1515                    sm.sendMessage(msgType);
1516                }
1517            }
1518
1519        }
1520        class SetIpForwardingEnabledErrorState extends ErrorState {
1521            @Override
1522            public void enter() {
1523                Log.e(TAG, "Error in setIpForwardingEnabled");
1524                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
1525            }
1526        }
1527
1528        class SetIpForwardingDisabledErrorState extends ErrorState {
1529            @Override
1530            public void enter() {
1531                Log.e(TAG, "Error in setIpForwardingDisabled");
1532                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
1533            }
1534        }
1535
1536        class StartTetheringErrorState extends ErrorState {
1537            @Override
1538            public void enter() {
1539                Log.e(TAG, "Error in startTethering");
1540                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
1541                try {
1542                    mNMService.setIpForwardingEnabled(false);
1543                } catch (Exception e) {}
1544            }
1545        }
1546
1547        class StopTetheringErrorState extends ErrorState {
1548            @Override
1549            public void enter() {
1550                Log.e(TAG, "Error in stopTethering");
1551                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
1552                try {
1553                    mNMService.setIpForwardingEnabled(false);
1554                } catch (Exception e) {}
1555            }
1556        }
1557
1558        class SetDnsForwardersErrorState extends ErrorState {
1559            @Override
1560            public void enter() {
1561                Log.e(TAG, "Error in setDnsForwarders");
1562                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
1563                try {
1564                    mNMService.stopTethering();
1565                } catch (Exception e) {}
1566                try {
1567                    mNMService.setIpForwardingEnabled(false);
1568                } catch (Exception e) {}
1569            }
1570        }
1571    }
1572
1573    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1574        if (mContext.checkCallingOrSelfPermission(
1575                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
1576            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
1577                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
1578                    Binder.getCallingUid());
1579                    return;
1580        }
1581
1582        synchronized (mPublicSync) {
1583            pw.println("mUpstreamIfaceTypes: ");
1584            for (Integer netType : mUpstreamIfaceTypes) {
1585                pw.println(" " + netType);
1586            }
1587
1588            pw.println();
1589            pw.println("Tether state:");
1590            for (Object o : mIfaces.values()) {
1591                pw.println(" " + o);
1592            }
1593        }
1594        pw.println();
1595        return;
1596    }
1597}
1598