summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/firewall/SetupTetheringTask.java
blob: edf79add18eeec05a1c1d421ef57348918d0133c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * Copyright (c) 2020 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.firewall;

import android.os.AsyncTask;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;

import se.leap.bitmaskclient.tethering.TetheringObservable;
import se.leap.bitmaskclient.tethering.TetheringState;

import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_FORWARD;
import static se.leap.bitmaskclient.firewall.FirewallManager.BITMASK_POSTROUTING;
import static se.leap.bitmaskclient.utils.Cmd.runBlockingCmd;

public class SetupTetheringTask extends AsyncTask<Void, Boolean, Boolean> {

    private static final String TAG = SetupTetheringTask.class.getSimpleName();
    private WeakReference<FirewallCallback> callbackWeakReference;

    SetupTetheringTask(FirewallCallback callback) {
        callbackWeakReference = new WeakReference<>(callback);
    }

    @Override
    protected Boolean doInBackground(Void... args) {
        TetheringState tetheringState = TetheringObservable.getInstance().getTetheringState();
        StringBuilder log = new StringBuilder();

        String[] bitmaskChain = new String[]{
                "su",
                "id",
                "iptables -t filter --list " + BITMASK_FORWARD + " && iptables -t nat --list " + BITMASK_POSTROUTING };

        try {
            boolean hasBitmaskChain = runBlockingCmd(bitmaskChain, log) == 0;
            boolean allowSu = log.toString().contains("uid=0");
            FirewallCallback callback = callbackWeakReference.get();
            if (callback != null) {
                callback.onSuRequested(allowSu);
            }
            if (!allowSu) {
                return false;
            }

            boolean success = true;
            log = new StringBuilder();

            if (!hasBitmaskChain && tetheringState.hasAnyVpnTetheringAllowed() && tetheringState.hasAnyDeviceTetheringEnabled()) {
                createChains(log);
            }

            if (tetheringState.tetherWifiVpn()) {
                log = new StringBuilder();
                success = addWifiTetheringRules(tetheringState, log);
                logError(success, log);
            } else if (!tetheringState.isVpnWifiTetheringAllowed){
                success = removeWifiTetheringRules(tetheringState, log);
                logError(success, log);
            }

            log = new StringBuilder();
            if (tetheringState.tetherUsbVpn()) {
                success = success && addUsbTetheringRules(tetheringState, log);
                logError(success, log);
            } else if (!tetheringState.isVpnUsbTetheringAllowed) {
                success = success && removeUsbTetheringRules(tetheringState, log);
                logError(success, log);
            }

            log = new StringBuilder();
            if (tetheringState.tetherBluetoothVpn()) {
                success = success && addBluetoothTetheringRules(tetheringState, log);
                logError(success, log);
            } else if (!tetheringState.isVpnBluetoothTetheringAllowed) {
                success = success && removeBluetoothTetheringRules(tetheringState, log);
                logError(success, log);
            }
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(FirewallManager.TAG, log.toString());
        }
        return false;
    }

    private void logError(boolean success, StringBuilder log) {
        if (!success) {
            Log.e(TAG, log.toString());
        }
    }


    private void createChains(StringBuilder log) throws Exception {
        boolean success;
        String[] createChains = new String[]{
                "su",
                "iptables -t filter --new-chain " + BITMASK_FORWARD,
                "iptables -t nat --new-chain " + BITMASK_POSTROUTING,
                "iptables -t filter --insert FORWARD --jump " + BITMASK_FORWARD,
                "iptables -t nat --insert POSTROUTING --jump " + BITMASK_POSTROUTING,
        };
        success = runBlockingCmd(createChains, log) == 0;
        Log.d(FirewallManager.TAG, "added " + BITMASK_FORWARD + " and " + BITMASK_POSTROUTING+" to iptables: " + success);
        Log.d(FirewallManager.TAG, log.toString());
    }

    private boolean addWifiTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "add Wifi tethering Rules");
        String[] addRules = getAdditionRules(state.wifiAddress, state.wifiInterface);
        return runBlockingCmd(addRules, log) == 0;
    }

    private boolean removeWifiTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "remove Wifi tethering Rules");
        String[] removeRules = getDeletionRules(state, state.lastSeenWifiAddress, state.lastSeenWifiInterface);
        return runBlockingCmd(removeRules, log) == 0;
    }

    private boolean addUsbTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "add usb tethering rules");
        String[] addRules = getAdditionRules(state.usbAddress, state.usbInterface);
        return runBlockingCmd(addRules, log) == 0;
    }

    private boolean removeUsbTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "remove usb tethering rules");
        String[] addRules = getDeletionRules(state, state.lastSeenUsbAddress, state.lastSeenUsbInterface);
        return runBlockingCmd(addRules, log) == 0;
    }

    private boolean addBluetoothTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "add bluetooth tethering rules");
        String[] addRules = getAdditionRules(state.bluetoothAddress, state.bluetoothInterface);
        return runBlockingCmd(addRules, log) == 0;
    }

    private boolean removeBluetoothTetheringRules(TetheringState state, StringBuilder log) throws Exception {
        Log.d(TAG, "remove bluetooth tethering rules");
        String[] addRules = getDeletionRules(state, state.lastSeenBluetoothAddress, state.lastSeenBluetoothInterface);
        return runBlockingCmd(addRules, log) == 0;
    }


    private String[] getAdditionRules(String addressRange, String interfaceName) {
        return new String[] {
                "su",
                "iptables -t filter --flush " + BITMASK_FORWARD,
                "iptables -t nat --flush " + BITMASK_POSTROUTING,
                "iptables -t filter --append " + BITMASK_FORWARD + " --jump ACCEPT",
                "iptables -t nat --append " + BITMASK_POSTROUTING + " --jump MASQUERADE",
                "if [[ ! `ip rule show from "+ addressRange+" lookup 61` ]]; " +
                        "then ip rule add from " + addressRange + " lookup 61; " +
                        "fi",
                "if [[ ! `ip route list table 61 | grep 'default dev " + getTunName() + " scope link'` ]]; " +
                        "then ip route add default dev " + getTunName() + " scope link table 61; " +
                        "fi",
                "if [[ ! `ip route list table 61 | grep '"+ addressRange +" dev "+ interfaceName +" scope link'` ]]; " +
                        "then ip route add " + addressRange + " dev " + interfaceName + " scope link table 61; " +
                        "fi",
                "if [[ ! `ip route list table 61 | grep 'broadcast 255.255.255.255 dev " + interfaceName + " scope link'` ]]; " +
                        "then ip route add broadcast 255.255.255.255 dev " + interfaceName + " scope link table 61; " +
                        "fi"
        };
    }

    private String[] getDeletionRules(TetheringState state, String addressRange, String interfaceName) {
        ArrayList<String> list = new ArrayList<>();
        list.add("su");
        list.add("ip route delete broadcast 255.255.255.255 dev " + addressRange +" scope link table 61");
        list.add("ip route delete " + addressRange + " dev " + interfaceName +" scope link table 61");
        if (!state.hasAnyVpnTetheringAllowed() || !state.hasAnyDeviceTetheringEnabled()) {
            list.add("ip route delete default dev " + getTunName() + " scope link table 61");
        }
        list.add("if [[ `ip rule show from " + addressRange + " lookup 61` ]]; " +
                "then ip rule del from " + addressRange + " lookup 61; " +
                "fi");

        return list.toArray(new String[0]);
    }



    private String getTunName() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface networkInterface = en.nextElement();
                if (networkInterface.getName().contains("tun")) {
                    return networkInterface.getName();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        FirewallCallback callback = callbackWeakReference.get();
        if (callback != null) {
            callback.onTetheringStarted(result);
        }
    }
}