Commit graph android_packages_modules_Co.../service/src/com/android/server/BpfNetMaps.java
Message SHA1
Set restrict local network aconfig flag for 26q2 dates
Local net protections is introduced in B as a developer opt-in, on dev
builds; the feature flag is meant to be used when enforcement begins in
2026

Bug: 396116600
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.connectivity.PermissionMonitorTest
Change-Id: Id5e7a7cf825f10f90edd600ea2567cfe6efd2e98

Jacque Li   commited 2025-02-12 12:04:04 -05:00
0c67f3579f
Adding access methods for local_net_access_map and
local_net_blocked_uid_map

Test: Manually tested this using dumpsys commands
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.BpfNetMapsTest
Bug: 392855578

Change-Id: I61522f3282aa502f20641d3c2692a66638edf706

Sarup Dalwani   authored and   Jacque Li   commited 2025-02-07 15:10:49 -05:00
448eb43f28
Merge "Revert "Prepare for @RequiresApi class retention"" into main
Treehugger Robot   authored and   Gerrit Code Review   commited 2024-06-26 11:47:49 +00:00
270f0064f6
Revert "Prepare for @RequiresApi class retention"
This reverts commit 53da9ac34c.
And fixes the linter errors.

Bug: 328190227
Test: TH
Change-Id: I92e0f268c770b6e46bdc781d4867f1e0b505d4ad

Motomu Utsumi   commited 2024-06-26 19:14:30 +09:00
a1a0034f0e
SingleWriterBpfMap - introduce getSingleton with caching
Test: TreeHugger, atest BpfMapTest finally happy!
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia4ddcbb4994bb0fd5df7d36e0d306ca5061d8963

Maciej Żenczykowski   commited 2024-06-15 14:04:03 -07:00
52d6c5587f
Use SingleWriterBpfMap in BpfNetMaps.
Most of the maps used by BpfNetMaps are configuration maps that
in production code are only written to by BpfNetMaps. Move these
maps to SingleWriterBpfMap to take advantage of caching.

Specifically, the following are only written by BpfNetMaps in
production code:

- sConfigurationMap
- sUidOwnerMap
- sUidPermissionMap
- sDataSaverEnabledMap
- sIngressDiscardMap

Additionally:
- firewall.cpp writes to sConfigurationMap, sUidOwnerMap, and
  sDataSaverEnabledMap, but that code only runs in tests because
  it requires root privileges.
- NetworkStackBpfNetMaps also uses sConfigurationMap,
  sUidOwnerMap and sDataSaverEnabledMap, but it opens all of them
  with BPF_F_RDONLY so cannot write to them.

sCookieTagMap is left uncached because it's also written to by
netd (as part of socket tagging) and ClatCoordinator.

Fix: 343166906
Test: no-op replacement, covered by existing tests
Change-Id: Ie2619949477323d557b8906cf082e86a78512847

Lorenzo Colitti   commited 2024-06-05 06:48:50 +00:00
8fd925a9b3
Fix getNetPermForUid to use appId instead of uid for query
Key of permission bpf map is an appId not uid.

Test: TH
Bug: 342063557
Change-Id: I0702466cbdf4df1baf08e5995e54d9545947366e

Motomu Utsumi   commited 2024-05-22 19:52:55 +09:00
68ce241658
Add compat flag for ActiveNetworkInfo blocked state
Before aosp/3001087, getNetworkActiveInfo did not return network info
with blocked state for apps without PERMISSION_INTERNET even if the
network is actually blocked.
aosp/3001087 updated getNetworkActiveInfo to return correct blocked
state on V+.
However, this broke some apps that use getNetworkActiveInfo in wrong way
and depends on the previous behavior.
So this CL introduces the compat flag to keep previous behavior for apps
targeting U or older releases.

Test: TH
Bug: 333340911
Change-Id: I0e0950f2148de0ffdb45495903a5a87e0d4c1c0b

Motomu Utsumi   commited 2024-05-08 21:08:35 +09:00
0b4be79e52
Reapply "Use bpf map based isUidNetworkingBlocked on V+"
This reverts commit a658220234.

Change-Id: I53acd7ff4821a65583b2ad936e22efaa21965a14

Maciej Żenczykowski   authored and   Motomu Utsumi   commited 2024-05-08 14:50:38 +09:00
a08692f7c7
Use Bpf map based isUidRestrictedOnMeteredNetworks on V+
Bug: 328732146
Test: BpfNetMapsTest
Change-Id: I2d3820bf4dd82649941eef2995ad237276937ba9

Motomu Utsumi   commited 2024-05-07 18:36:39 +09:00
e55bcd578b
Add bpf map based getUidNetworkingBlockedReasons
Bug: 328732146
Test: NetworkStackBpfNetMapsTest BpfNetMapsTest
Change-Id: Ie802b3d18463e2b7e8a2b953088cbbcc117a841e

Motomu Utsumi   commited 2024-05-07 18:36:36 +09:00
eb92054f91
Add @RequiresApi(TIRAMISU) to BpfNetMapsUtils
With this annotation, errorprone build will fail if callers don't check
sdk version before calling any methods in BpfNetMapsUtils.

Bug: 328732146
Test: TH
Change-Id: Ifa52dc1d2766f8d46dfdc4f6ca9dbb963406c084

Motomu Utsumi   commited 2024-04-24 13:16:42 +09:00
2c248d2108
Add firewall chains for HAPPY_BOX and user/admin PENALTY_BOX
Follow up CLs will update CS to generate blocked reason from bpf map
instead of asking NPMS.

However, one issue is NPMS set PENALTY_BOX for
BLOCKED_METERED_REASON_USER_RESTRICTED and
BLOCKED_METERED_REASON_ADMIN_DISABLED without telling the reason.
So, CS can not know the reason of PENALTY_BOX.

This CL add new firewall chain and match to distinguish reasons.
NPMS must call setUidFirewallRule with
FIREWALL_CHAIN_METERED_DENY_USER or FIREWALL_CHAIN_METERED_DENY_ADMIN
based on the reason so that CS can know the reason of restriction.

Bug: 332628891
Test: atest com.android.cts.net.HostsideRestrictBackgroundNetworkTests
Change-Id: Ia4ad4bdb345abc22c782630e828edfad2452db36

Motomu Utsumi   commited 2024-04-23 15:04:28 +09:00
11d3345daa
Revert "Use bpf map based isUidNetworkingBlocked on V+"
This reverts commit 9b272fa53a.

Reason for revert: possible root cause of b/333340911.

Change-Id: Ic4923fd19db7e4704a3ef02d79644b47c271e18e

Patrick Rohr   authored and   Gerrit Code Review   commited 2024-04-11 16:54:04 +00:00
a658220234
Use bpf map based isUidNetworkingBlocked on V+
NetworkPolicyManager#isUidNetworkingBlocked does not know about some
firewall chains status(e.g. FIREWALL_CHAIN_OEM_DENY_1)
So BpfNetMaps#isUidNetworkingBlocked is more correct but there could be
some behavior difference.
To avoid breaking devices and have longer verification time, this CL
updates to use BpfNetMaps#isUidNetworkingBlocked only on V+ devices.

Bug: 328732146
Test: TH
Change-Id: I60f84a5fff90386103692841dead465652249446

Motomu Utsumi   commited 2024-03-26 19:16:57 +09:00
9b272fa53a
Move isChainEnabled and getUidRule to BpfNetMapsUtils
This CL also adds @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
to NetworkStackBpfNetMaps

Bug: 328732146
Test: TH
Change-Id: Ifa4cffd046164405b8b49d2e2ee9f27240bec6fa

Motomu Utsumi   commited 2024-03-19 04:21:29 +00:00
2f79f58c06
Rename BpfNetMapsReader to NetworkStackBpfNetMaps
BpfNetMapsReader is loaded to bootclasspath. So this class should not
contain unnecessary codes.
To make this clear, this CL renames to NetowrkStackBpfNetMaps and add
comments

Bug: 328732146
Test: TH
Change-Id: I557627c9fae087eb6119c2ae31d46526fa9e5d7e

Motomu Utsumi   commited 2024-03-19 04:12:29 +00:00
fc504042f6
Bpf: open code PRE_T - too complex for errorprone linter
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1ad2d0dd288882e84925f0b59961dcd33612f3f7

Maciej Żenczykowski   commited 2024-01-25 16:09:11 -08:00
0d450d3e4d
BpfNetMaps - add RequiresApi(T) annotations
netd.o bpf program was mainlined in T

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I6d2b34e26f5640840b74d4c761b01668e44dc9d6

Maciej Żenczykowski   commited 2024-01-25 15:15:26 -08:00
6b9d921f1c
BpfMap - add R/W constructor alias
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7cef2b19f55e6e056b46bb1b28f85c7f7376f6f4

Maciej Żenczykowski   commited 2024-01-23 16:16:44 -08:00
06155c8040
switch SynchronizeKernelRCU back to jni - but from BpfMap
we can't use java for this, because pre-U-QPR2 does not include:
  https://android-review.googlesource.com/c/platform/system/sepolicy/+/2821590
  system_server dontaudit key_socket getopt

so we'll spew lots of:
  11-08 07:52:43.776 1469 1469 I auditd : type=1400 audit(0.0:4): avc: denied { getopt } for comm="system_server" scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=key_socket permissive=0
  11-08 07:52:44.360 1469 1469 I auditd : type=1400 audit(0.0:5): avc: denied { getopt } for comm="NetworkStats" scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=key_socket permissive=0
  11-08 07:52:44.508 1469 1469 I auditd : type=1400 audit(0.0:7): avc: denied { getopt } for comm="android.bg" scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=key_socket permissive=0
and the like.

This is due to Java's Os.close() in:
  libcore/luni/src/main/java/libcore/io/BlockGuardOs.java;l=100
calling:
  if (fd.isSocket$()) if (isLingerSocket(fd)) ...

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib87fa5e8f0dfd9fbbccb25fb58a9fa78d6a07111

Maciej Żenczykowski   commited 2023-11-09 11:08:58 +00:00
c350db788a
Merge "remove BPF_NET_MAPS_FORCE_DISABLE_JAVA_BPF_MAP" into main
Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2023-11-09 04:31:05 +00:00
49c88a1d44
remove BPF_NET_MAPS_FORCE_DISABLE_JAVA_BPF_MAP
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I550357be83f946bf9f29b9f90037d6a6e66142d1

Maciej Żenczykowski   commited 2023-11-09 03:00:09 +00:00
23912ee104
remove BpfNetMaps.cpp
Test: builds, TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If192c17881736c9fe7f8346ad8c5d41d7164eb03

Maciej Żenczykowski   commited 2023-11-08 07:12:46 +00:00
7b8f475e79
Merge changes Ie1a36c3c,Id727cf7b,Ifba198d9 into main
* changes:
  Remove unnecessary check for skDestroyListener
  Remove TrafficController
  Remove native code path in BpfNetMaps.java

Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2023-11-07 19:32:51 +00:00
87f7f27ad0
Remove unnecessary check for skDestroyListener
skDestroyListener is always running with Java code so removed the
unnecessary check.

Bug: 217624062
Test: TH

Change-Id: Ie1a36c3c736005ec4821c5e9c9983998f7042afa

Motomu Utsumi   commited 2023-11-07 20:25:28 +09:00
ffa6d6152b
Remove native code path in BpfNetMaps.java
BpfNetMaps uses java library for updating bpf map as the default code
path.
This is already released to 100% production without any issues.
So this CL removes the old code path from BpfNetMaps.

Note that native_init only opens the bpf maps if sEnableJavaBpfMap is
true.
But if sEnableJavaBpfMap is true, no native code in TrafficController
access the bpf map.
So this CL can remove native_init.

Following CL will remove the nativce codes for old code path.

Bug: 217624062
Test: NetworkStaticLibsTests
Change-Id: Ifba198d9e6f93b53fd1dbf3b2aafb644da0b147d

Motomu Utsumi   commited 2023-11-07 17:45:32 +09:00
96b6c39229
Merge "Add methods for updating ingressDiscardRule bpf map to BpfNetMaps" into main
Motomu Utsumi   authored and   Gerrit Code Review   commited 2023-11-06 10:41:53 +00:00
d872c3c960
[BR02] Implement isUidNetworkingBlocked
This is needed for data stall detection mechanism in NetworkStack
to get the information about whether the network is blocked for
a given uid and conditions. Because the API will be called
frequently from NetworkStack to resolve all status for all uids
on the device, the API cannot call into the service which
creates IPC. Instead, the API need to directly access bpf maps
in the user process to retrieve the status. In this case the
user process is the network stack, the access control is provided
by linux file permission and selinux.

Test: atest FrameworksNetTests:android.net.connectivity.android.net.BpfNetMapsReaderTest
Test: atest FrameworksNetTests:android.net.connectivity.android.net.ConnectivityManagerTest
NO_IFTTT=Refactor only change for firewall chains definitions
Bug: 297836825

Change-Id: Iaf983b71ec98cbfe5152dcfade8a3120f938f135

Junyu Lai   commited 2023-10-31 16:33:41 +08:00
e003152e2c
Add methods for updating ingressDiscardRule bpf map to BpfNetMaps
Bug 295800201
Test: NetworkStaticLibsTests

Change-Id: I42bc0adc22c3018480029d624053f758d815e526

Motomu Utsumi   commited 2023-10-31 17:07:46 +09:00
77b49996d2
Write Data Saver setting to BPF map
The information is needed by modules who want to know whether a
specific UID is blocked by Data Saver feature.

1. Add a one-element map data_saver_enabled_map.
2. Update current data saver setting to the map.

Bug: 288340533
Test: atest FrameworksNetTests:android.net.connectivity.com.android.serv
er.BpfNetMapsTest
Test: atest bpf_existence_test

Change-Id: I981da4b569247c33cba2d365cb6f2691f673474e

Ken Chen   commited 2023-10-31 01:36:03 +00:00
243301748e
[BR01.1] Support BpfNetMapsReader
A helper class to *read* java BpfMaps. This is designed to
provide direct bpf access in the caller process through
ConnectivityManager APIs.

The change also removes any statical link to
net-utils-device-common-struct from service-connectivity.
This is because net-utils-device-common-struct is already
included in framework-connectivity. Including it again in
service-connectivity would create a r8 build fail by circular
dependency.

Test: atest FrameworksNetTests:android.net.connectivity.com.android.server.BpfNetMapsTest
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.net.module.util.StructTest
Test: atest FrameworksNetTests:android.net.connectivity.android.net.BpfNetMapsReaderTest
Bug: 297836825
Change-Id: I7a6d2eb816d0dc7343167bddd672806b199f44fe

Junyu Lai   commited 2023-10-23 20:11:02 +08:00
626045a601
Update the logic of isFeatureEnabled and isFeatureNotChickenedOut
If the flag value is unset or 0, isFeatureEnabled return false and
isFeatureNotChickenedOut return true.
If the flag value is -1 (force disable), both return false.
If the flag value is other values, both query the package version and
return `flagValue <= packageVersion`

Now the only difference is a default behavior when the flag is not set
or 0.
So isFeatureEnabled and isFeatureNotChickenedOut can use the same flag.

This CL also fixes the issue in rollback.
Before this CL, isFeatureNotChickenedOut did not check the module
version and could have a issue if there is a rollback.

Test: NetworkStaticLibsTests
Bug: 279108992
Change-Id: I12d6ebadff3aee7b7c614aca4eb0a34ef0db9857

Motomu Utsumi   commited 2023-09-27 17:30:01 +09:00
ed4e7ec954
remove TrafficController::dump
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I399ac8dfc4992f4c604f1d32c2f1b8ae06710808

Maciej Żenczykowski   commited 2023-09-06 16:50:06 +00:00
d70a33061e
Split BpfNetMaps constants and utilities into standalone classes
This is a no-op refactoring that splits constants and utility
methods into standalone classes which will be shared with
a bpf reader class in subsequent CLs.

NO_IFTTT=No-op refactoring

Test: atest FrameworksNetTests:android.net.connectivity.com.android.server.BpfNetMapsTest
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.net.module.util.StructTest

Bug: 297836825
Change-Id: I6d7ea044e43180ae001573009a166be74ebe6a5d

Junyu Lai   commited 2023-08-29 08:20:10 +00:00
29b7b6385a
Use Java code path for bpf map operations in BpfNetMaps
Experiment to verify bpf map operations refactoring showed no
change of map size metrics and no system health regressions.
So this CL update BpfNetMaps to always use new code path with kill
switch.

Test: m
Bug: 217624062
Change-Id: Ic17470ac5c075884871ea2df2c13c054f5ac2929

Motomu Utsumi   commited 2023-08-16 20:08:20 +09:00
7628ea30b0
Revert "KNOWN BAD CHANGE - it should cause lint failures (but doesn't??)."
This reverts commit fc9875f537.

Reason for revert: known to be bad.

Change-Id: I9b8db53c054119f454e50007f56232312a0ba595

Maciej Żenczykowski   authored and   Gerrit Code Review   commited 2023-07-18 20:14:38 +00:00
70fbfa43ed
KNOWN BAD CHANGE - it should cause lint failures (but doesn't??).
Build/TestCop - revert this if you see failures (just document where).
It erroneously(?) doesn't appear to cause any failures in TH
on aosp/master.

Bug: 291519274
Change-Id: I78b049302ae9bd2a8db8c03e79388dc4e51e110f

Maciej Żenczykowski   commited 2023-07-18 20:09:20 +00:00
fc9875f537
BpfNetMaps.java - add @Requires(T) to all native_ methods
Bug: 291519274
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0d6c68e639a975cf35ec6cc948be5744b104c98d

Maciej Żenczykowski   commited 2023-07-18 14:19:44 +00:00
e7da6fdff1
Close sockets from ConnectivityService#setFirewallChainEnabled
And replace netd.socketDestroy by Java implementation

Bug: 270298713
Test: atest FrameworksNetTests CtsNetTestCases
Change-Id: I0e200247ca010f9649254eeaac02740bd2bfdb21

Motomu Utsumi   commited 2023-05-11 12:22:09 +09:00
d44a33adb9
Fix isFeatureEnabled namespace and overload usage
From service-connectivity, NAMESPACE_TETHERING must be used, as
NAMESPACE_CONNECTIVITY is used for the NetworkStack module. Also the
isFeatureEnabled with module name must be used, otherwise the "android"
package version is queried instead of the tethering module version.

BpfNetMaps is using flag value 1 to workaround this issue.
flags in NsdService, AutomaticOnOffKeepaliveTracker, and ConnectivityFlags
were not pushed currently.
So, this change has no effect to devices.

Bug: 279108992
Test: TH
Change-Id: I2b4b6a13c048c20beef52b1f43b9e21aca0c637a

Motomu Utsumi   commited 2023-04-28 19:18:43 +09:00
278db58bf5
Add getUidRule method to BpfNetMaps
Upcoming CLs use this method to add chain status to failure log of
ConnectivityManagerTest#testFirewallBlocking which is a flaky test

Test: atest BpfNetMapsTest
Bug: 262141231
Change-Id: Ib130a28c708f675c79b889bf293dfe6b0f8d7f60

Motomu Utsumi   commited 2023-02-01 17:05:36 +09:00
56c412a9c8
check for U+ before checking device feature flags
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I12e147e61febd96324cdbb6642e0894e5c399526

Maciej Żenczykowski   commited 2022-12-27 10:57:59 +00:00
e7ebb15424
rename bpf_shared.h to netd.h
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2e1569c8d70e98f9a3fdbab41fd2ba7f7b84dd37

Maciej Żenczykowski   commited 2022-12-18 20:02:52 +00:00
513474c1a7
Enable java bpf map always in U devices
Having SdkLevel.isAtLeastU() as a default value of
DeviceConfigUtils.isFeatureEnabled could result confusing behavior.
For example, when flag value is pushed to only the T device and the T
device with the flag is ugraded to U, this device could disable java bpf
map while the device keeps the flag value.
This CL prevents above confusing behavior.

Bug: 246218229
Test: atest BpfNetMaps
Change-Id: I615603c337bb94101dccac77ea0e6fa857041259

Motomu Utsumi   commited 2022-12-06 15:03:20 +09:00
0e9c379c3e
Add sEnableJavaBpfMap value to the dump
Bug: 217624062
Test: dumpsys connectivity trafficcontroller
Change-Id: Icf3df3aac827e65415a8304df87a685272f38f9e

Motomu Utsumi   commited 2022-10-05 18:28:16 +09:00
316d057ded
Move CookieTag map dump to BpfNetMaps
Also removes dump tests in TrafficController

Bug: 217624062
Test: atest BpfNetMaps TagSocketTest
Change-Id: I92adad60c55443435d7c7a0bfc84b38162fd51b0

Motomu Utsumi   commited 2022-10-05 18:28:13 +09:00
ef546a9bd5
Move ownerMatch config dump to BpfNetMaps
Information in the dump does not change
....
current ownerMatch configuration: 516 DOZABLE_MATCH OEM_DENY_1_MATCH
....

Bug: 217624062
Test: dumpsys connectivity trafficcontroller, atest BpfNetMapsTest
Change-Id: I5f7b56e4ba03256414f49d0e82d65477fb97f05a

Motomu Utsumi   commited 2022-10-04 15:02:43 +09:00
372c9b45ee
Move current StatsMap dump to BpfNetMaps
Information in the dump does not change
....
current statsMap configuration: 0 SELECT_MAP_A
....

Bug: 217624062
Test: dumpsys connectivity trafficcontroller, atest BpfNetMapsTest
Change-Id: I42a844227f258b91ba5b368d8e8abf82a773a801

Motomu Utsumi   commited 2022-10-04 15:02:43 +09:00
c675d6fc01
Move uidOwnerMap dump to BpfNetMaps
Before this CL
....
mUidOwnerMap:
10050  IIF_MATCH LOCKDOWN_VPN_MATCH 0
10061  HAPPY_BOX_MATCH
10091  HAPPY_BOX_MATCH IIF_MATCH LOCKDOWN_VPN_MATCH 0
....

After this CL
....
sUidOwnerMap:
  10050 IIF_MATCH LOCKDOWN_VPN_MATCH 0
  10061 HAPPY_BOX_MATCH
  10091 HAPPY_BOX_MATCH IIF_MATCH LOCKDOWN_VPN_MATCH 0
....

Bug: 217624062
Test: dumpsys connectivity trafficcontroller, atest BpfNetMapsTest
Change-Id: I0e2358e462824273a89fcba6e19e75a233f9fe18

Motomu Utsumi   commited 2022-10-04 15:02:41 +09:00
956d86ccdf