1 /* 2 * Copyright (C) 2021 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.net.module.util; 18 19 import static android.net.INetd.IF_STATE_DOWN; 20 import static android.net.INetd.IF_STATE_UP; 21 22 import android.net.INetd; 23 import android.net.InterfaceConfigurationParcel; 24 import android.os.RemoteException; 25 import android.os.ServiceSpecificException; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.VisibleForTesting; 29 30 import java.util.ArrayList; 31 32 /** 33 * Collection of utilities for netd. 34 */ 35 public class NetdUtils { 36 /** 37 * Get InterfaceConfigurationParcel from netd. 38 */ getInterfaceConfigParcel(@onNull INetd netd, @NonNull String iface)39 public static InterfaceConfigurationParcel getInterfaceConfigParcel(@NonNull INetd netd, 40 @NonNull String iface) { 41 try { 42 return netd.interfaceGetCfg(iface); 43 } catch (RemoteException | ServiceSpecificException e) { 44 throw new IllegalStateException(e); 45 } 46 } 47 validateFlag(String flag)48 private static void validateFlag(String flag) { 49 if (flag.indexOf(' ') >= 0) { 50 throw new IllegalArgumentException("flag contains space: " + flag); 51 } 52 } 53 54 @VisibleForTesting removeAndAddFlags(@onNull String[] flags, @NonNull String remove, @NonNull String add)55 protected static String[] removeAndAddFlags(@NonNull String[] flags, @NonNull String remove, 56 @NonNull String add) { 57 final ArrayList<String> result = new ArrayList<>(); 58 try { 59 // Validate the add flag first, so that the for-loop can be ignore once the format of 60 // add flag is invalid. 61 validateFlag(add); 62 for (String flag : flags) { 63 // Simply ignore both of remove and add flags first, then add the add flag after 64 // exiting the loop to prevent adding the duplicate flag. 65 if (remove.equals(flag) || add.equals(flag)) continue; 66 result.add(flag); 67 } 68 result.add(add); 69 return result.toArray(new String[result.size()]); 70 } catch (IllegalArgumentException iae) { 71 throw new IllegalStateException("Invalid InterfaceConfigurationParcel", iae); 72 } 73 } 74 75 /** 76 * Set interface configuration to netd by passing InterfaceConfigurationParcel. 77 */ setInterfaceConfig(INetd netd, InterfaceConfigurationParcel configParcel)78 public static void setInterfaceConfig(INetd netd, InterfaceConfigurationParcel configParcel) { 79 try { 80 netd.interfaceSetCfg(configParcel); 81 } catch (RemoteException | ServiceSpecificException e) { 82 throw new IllegalStateException(e); 83 } 84 } 85 86 /** 87 * Set the given interface up. 88 */ setInterfaceUp(INetd netd, String iface)89 public static void setInterfaceUp(INetd netd, String iface) { 90 final InterfaceConfigurationParcel configParcel = getInterfaceConfigParcel(netd, iface); 91 configParcel.flags = removeAndAddFlags(configParcel.flags, IF_STATE_DOWN /* remove */, 92 IF_STATE_UP /* add */); 93 setInterfaceConfig(netd, configParcel); 94 } 95 96 /** 97 * Set the given interface down. 98 */ setInterfaceDown(INetd netd, String iface)99 public static void setInterfaceDown(INetd netd, String iface) { 100 final InterfaceConfigurationParcel configParcel = getInterfaceConfigParcel(netd, iface); 101 configParcel.flags = removeAndAddFlags(configParcel.flags, IF_STATE_UP /* remove */, 102 IF_STATE_DOWN /* add */); 103 setInterfaceConfig(netd, configParcel); 104 } 105 } 106