1 /* 2 * Copyright (C) 2013 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 #include "UidMarkMap.h" 18 #include "NetdConstants.h" 19 20 UidMarkMap::UidMarkEntry::UidMarkEntry(int start, int end, int new_mark) : 21 uid_start(start), 22 uid_end(end), 23 mark(new_mark) { 24 }; 25 26 bool UidMarkMap::add(int uid_start, int uid_end, int mark) { 27 android::RWLock::AutoWLock lock(mRWLock); 28 if (uid_start > uid_end) { 29 return false; 30 } 31 32 UidMarkEntry *e = new UidMarkEntry(uid_start, uid_end, mark); 33 mMap.push_front(e); 34 return true; 35 }; 36 37 bool UidMarkMap::remove(int uid_start, int uid_end, int mark) { 38 android::RWLock::AutoWLock lock(mRWLock); 39 android::netd::List<UidMarkEntry*>::iterator it; 40 for (it = mMap.begin(); it != mMap.end(); it++) { 41 UidMarkEntry *entry = *it; 42 if (entry->uid_start == uid_start && entry->uid_end == uid_end && entry->mark == mark) { 43 mMap.erase(it); 44 delete entry; 45 return true; 46 } 47 } 48 return false; 49 }; 50 51 int UidMarkMap::getMark(int uid) { 52 android::RWLock::AutoRLock lock(mRWLock); 53 android::netd::List<UidMarkEntry*>::iterator it; 54 for (it = mMap.begin(); it != mMap.end(); it++) { 55 UidMarkEntry *entry = *it; 56 if (entry->uid_start <= uid && entry->uid_end >= uid) { 57 return entry->mark; 58 } 59 } 60 // If the uid has no mark specified then it should be protected from any VPN rules that might 61 // be affecting the service acting on its behalf. 62 return PROTECT_MARK; 63 }; 64 65 bool UidMarkMap::anyRulesForMark(int mark) { 66 android::RWLock::AutoRLock lock(mRWLock); 67 android::netd::List<UidMarkEntry*>::iterator it; 68 for (it = mMap.begin(); it != mMap.end(); it++) { 69 UidMarkEntry *entry = *it; 70 if (entry->mark == mark) { 71 return true; 72 } 73 } 74 return false; 75 } 76