Commit Graph

74 Commits

Author SHA1 Message Date
Michael Bestas
2101a20942 Merge tag 'android-14.0.0_r29' into staging/lineage-21.0_merge-android-14.0.0_r29
Android 14.0.0 release 29

# -----BEGIN PGP SIGNATURE-----
#
# iF0EABECAB0WIQRDQNE1cO+UXoOBCWTorT+BmrEOeAUCZeZW5AAKCRDorT+BmrEO
# eAPbAJsEm9Wr9Wa247qVs8FVFwrOfTbQ+QCeNDnfvFnwSAZFPnHgHziapjITPN0=
# =jjQM
# -----END PGP SIGNATURE-----
# gpg: Signature made Tue Mar  5 01:19:00 2024 EET
# gpg:                using DSA key 4340D13570EF945E83810964E8AD3F819AB10E78
# gpg: Good signature from "The Android Open Source Project <initial-contribution@android.com>" [marginal]
# gpg: initial-contribution@android.com: Verified 2357 signatures in the past
#      2 years.  Encrypted 4 messages in the past 2 years.
# gpg: WARNING: This key is not certified with sufficiently trusted signatures!
# gpg:          It is not certain that the signature belongs to the owner.
# Primary key fingerprint: 4340 D135 70EF 945E 8381  0964 E8AD 3F81 9AB1 0E78

# By Maciej Żenczykowski (272) and others
# Via Automerger Merge Worker (13851) and others
* tag 'android-14.0.0_r29': (2002 commits)
  [nearby] Fix test failure
  Exclude user build devices for the test
  [nearby] Add nearby_enable_ble_in_init flag
  Revert "[nearby] Add disable logic"
  [nearby] Add disable logic
  [nearby] Log changes
  [nearby] Sync discoveryTimestamp field name
  [nearby] Enable BLE when init
  [nearby] Add logs to broadcaster
  [nearby] Add discoveryTimestamp field
  [nearby] Fix flakeness in the unit test
  [nearby][clean up] Remove useless legacy code
  [nearby] Fix the user-debug only test
  Don't add LOCAL_NETWORK as forbidden capability
  [nearby] Catches NPE in ChreDiscoveryProvider
  [nearby] Update README to add build env script
  Expose APIs to query the state of an IpSecTransform
  Reland "Support getting transform state in IpSecService"
  Revert "Support getting transform state in IpSecService"
  Add more timeout for verify the interaction of the mock object
  ...

 Conflicts:
	Tethering/src/com/android/networkstack/tethering/UpstreamNetworkMonitor.java
	bpf_progs/netd.c
	service/src/com/android/server/ConnectivityService.java

Change-Id: If3464757e222db1be894c60f27c206be3f195eb8
2024-03-10 19:07:51 +02:00
Tommy Webb
39455f0627 Use BPF to block socket creation when restricted
Adapt BPF's inet socket creation rule to not only check INTERNET
permission but also to ensure the uid is on the allowlist for
restricted networking (has RESTRICTED_MATCH flag).

Also includes squashed change:

Author: Tommy Webb <tommy@calyxinstitute.org>
Date:   Thu Sep 7 03:59:21 2023 -0400

    Deny socket creation for transport-blocked apps

    Prevent apps whose network access is blocked based on transport
    policies from creating sockets, too. Update the logic to match AOSP's
    "funky bit-wise arithmetic" from the latest Connectivity mainline.

    Test: Manual: Turn on Private DNS. Install Terminal Emulator. Connect
    to Wi-Fi (no VPN). Set Terminal Emulator's toggles to disable Wi-Fi.
    Run: `ping duckduckgo.com`. Should receive "unknown host" error, NOT
    "Network is unreachable". Same "unknown host" error should occur when
    testing with overall network access turned off for Terminal Emulator,
    with and without its Wi-Fi access also turned off.

    Issue: calyxos#581
    Change-Id: I995e9929f6f8c1ae0613e05e0cade55a76c35902

Co-authored-by: Oliver Scott <olivercscott@gmail.com>
Change-Id: I912a4a2ee78a29ca8b7d8ff85e5ad7cf617c31a5
2023-12-24 13:43:53 +02:00
Ken Chen
ec0f7ac36f Add a library for DNS resolver to read bpf maps
The library provides an init function and an API for DNS resolver to
query whether the application is allowed to send DNS query based on BPF
maps settings.

Bug: 288340533
Test: atest dns_helper_unit_test (with test CL)
Change-Id: Ibfb383bfb074da2104a25aa4f04ebc32b22d11da
2023-11-01 06:23:04 +08:00
Ken Chen
243301748e 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
2023-10-31 01:36:03 +00:00
Ken Chen
784696fdb9 Move is_system_uid utility from netd.c to netd.h
1. Move it to header file so that it can be reused by others.
2. Correct the return type from int to bool.
3. Replace __always_inline by inline to avoid -Werror,-Wunused-function.

Bug: 288340533
Test: build
Change-Id: I9062686d9c2f98c2d24e4673f82b1732b180ffc4
2023-10-28 02:46:22 +08:00
Maciej Żenczykowski
a8852b2488 type safety for 'bool egress'
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3f0a12f139478bc94d351d58a08d4a9bd19fa320
2023-10-08 20:16:59 -07:00
Maciej Żenczykowski
f060849988 netd.c: factor out get_app_permissions()
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If33414eccefb35e6aefbd4ec0c24b208e564ca7e
2023-10-07 19:38:48 +00:00
Maciej Żenczykowski
901c71051e bpf_progs: use macros for kernel version
Generated via:
  for f in bpf_progs/{block,dscpPolicy,netd,offload,test}.c; do
    sed -i -r 's@KVER[(]([45]), ([0-9]+), 0[)]@KVER_\1_\2@g' "${f}"
  done

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8f211e47bf259fc75aa1caaaf82f41c1929ceab2
2023-10-06 15:54:39 -07:00
Maciej Żenczykowski
3a64568abb type safety for kernel version
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If05c0c5383dceeb65964143f5574d2ee1a484907
2023-10-06 15:54:38 -07:00
Maciej Żenczykowski
c112629307 bpf: add constants PRIVATE, SHARED, MANDATORY, OPTIONAL
use them & IGNORE_ON_* LOAD_ON_* as needed.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ibadd782d289e6a2ce1467778a1930c6f1b609f98
2023-10-03 05:30:56 +00:00
Ryan Zuklie
9fb8f18c1b Remove debug only restrictions to network tracing
Network tracing was only available on userdebug and eng builds. This
change makes it available on all build types behind a flag.

Bug: 298197881
Test: flash & trace, toggle flag on/off
Change-Id: I75d854aee74adf7e23f7a970b20233790f9b0354
2023-09-29 16:52:11 -07:00
Maciej Żenczykowski
7e2f53e762 reverse BpfMap & BpfMapRO inheritence to make sense
Bug: 235590615
Bug: 235907076
Bug: 286003437
Test: TreeHugger, m droid gpuservice_unittest libtimeinstate_test bpf_benchmark bpf_module_test libbpf_load_test && mma
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I020a898fe8b257040a100f146654d4a04b19f843
2023-09-29 00:44:33 +00:00
Ken Chen
f7d23e1a60 [Refactor] Make uid owner match comparison logic into a function
As an inline function, the logic can be reused by others.

Bug: Bug: 288340533
Test: build; presubmit
Change-Id: I8e57829e304e829eed72cc165b051cd22088260d
2023-09-17 00:11:48 +08:00
Maciej Żenczykowski
b9cf34715c netd.c - nettrace - add 'wakeup' boolean
This is based on network driver populated skb->mark magic bit.
This is the bit used by netd's WakeupController.

We mandated the location of this bit in U, though we haven't
(yet??) mandated it being supported by all network drivers.

If the driver doesn't support it, it could always
be false (skb->mark should default to 0),
or potentially (this is very very unlikely) be garbage.

IFIRC nettrace isn't enabled on pre-U devices anyway.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2b3b91315d77c08c022396253b26954593dd3f5a
2023-09-13 22:05:30 +00:00
Maciej Żenczykowski
73896a7b70 nettrace - skip over a single hopopts or dstops ipv6 extension header
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iad7800387e1b84587794be93d037f712ac95fa07
2023-09-13 06:11:18 +00:00
Maciej Żenczykowski
a8cb825710 netd.c - nettrace - add support for icmp type/code & udplite/sctp ports.
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I37c2aefe8759fdbab3078c60e0f97b57799697fc
2023-09-12 03:42:45 +00:00
Maciej Żenczykowski
6109d94ec5 netd bpf - implement ingress discard based on {dstip,ifindex}
Test: TreeHugger
Bug: 295800201
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I82771644045e0e37f73725730bd0bd2265ac5b77
2023-08-29 19:35:12 +00:00
Maciej Żenczykowski
fca4ee4f08 netd.c - only check clat uid on egress
On ingress:
(a) the socket is not a normal socket (it's AF_PACKET)
    and thus (likely) doesn't hit this code path
    [if it did... we'd have double or more accounting
    of any traffic captured by AF_PACKET sockets,
    I haven't checked - but I assume that doesn't happen]
(b) is created by the system server (so not AID_CLAT)
(c) is not tagged by the system server (so not AID_CLAT)

So this is a no-op, but it simplifies the bpf program,
since 'egress' is a compile time evaluated constant.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iec693548789eb2752f9f30038e72e35c876f986c
2023-08-29 15:03:29 +00:00
Maciej Żenczykowski
de1342a033 bpf_progs: disable BTF on <=U && user builds
This is to cut down bpfloader boot time.
Potential savings might be on the order of 30+% (300ms).

Loading BTF requires fork-execing the btfloader,
and currently BTF is only used to facilitate debugging.

Bug: 286369326
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ifa5f0052135b9dc826b18ca4622784615ed9c3c8
2023-06-09 05:58:57 +00:00
Maciej Żenczykowski
9fb415a7c0 Merge "netd.c: simplify impossible code path" 2023-05-22 17:44:16 +00:00
Maciej Żenczykowski
83dde6b40a netd.c - cleanup AID_CLAT handling
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie2fab051f3d923b3dd2be09d82c3cf47e91f6224
2023-05-20 17:25:49 +00:00
Maciej Żenczykowski
399c9f296e netd.c: simplify impossible code path
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I04f3f1b6ba1093f6543f6e80f62d978ebfee707d
2023-05-20 17:16:06 +00:00
Maciej Żenczykowski
c5af9f2084 Drop support for beta versions of Android T.
Android T beta3/4 haven't been tested in ages,
and were really only tested for the transition to final T
nearly a year ago.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I520e60026179c078859572231b86184796182142
2023-04-25 15:08:28 -07:00
Maciej Żenczykowski
b27132f716 sed -i -r 's@/[*]ignore_on_user[*]/true@IGNORE_ON_USER@g' netd.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I756e4451d252ab43b4fbe57f185c950cd519b359
2023-04-20 04:04:51 +00:00
Maciej Żenczykowski
b6232eb6f0 sed -i -r 's@/[*]ignore_on_user[*]/false@LOAD_ON_USER@g' netd.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I437fe4cca4bd59b12531ac722a6d464bdce512a6
2023-04-20 04:04:27 +00:00
Maciej Żenczykowski
e94f47674b sed -i -r 's@/[*]ignore_on_userdebug[*]/false@LOAD_ON_USERDEBUG@g' netd.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I850678bd050026d5d4da3cff74a44a4df274b48a
2023-04-20 04:04:19 +00:00
Maciej Żenczykowski
95e074380f sed -i -r 's@/[*]ignore_on_eng[*]/false@LOAD_ON_ENG@g' netd.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ife0c4bc8fe468456172b9a8e7567d3633c1b7404
2023-04-20 04:04:11 +00:00
Maciej Żenczykowski
4c33f5c958 bpf_net_helpers: define some boolean constants
This will make the code more legibble once we switch to using these.

Also moving them out of the .c files so we can share the same
constants across multiple files.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5cc9058cee8d1ea10d2f9e62a38313d0728f07d3
2023-04-20 04:03:26 +00:00
Maciej Żenczykowski
99a1a26a2f netd.c - feed kver deeper into bpf programs
Bug: 263884894
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I4a1ca71d529602a1651b711439ccd0a60fe3c550
2023-04-19 19:34:57 +00:00
Maciej Żenczykowski
bdccc50824 allow ingress TCP FINs in doze mode
I don't know if this will truly help:

We'll still drop the expected egress TCP ACK (or FIN-ACK) reply
to the newly allowed ingress TCP FIN...

However: I don't think this will make things worse.

The presence of an ingress packet is proof the hardware already woke up to receive it.  This behaviour doesn't change when allowing ingress *anything*.

ie. the main reason we don't allow ingress packets is
that it would be illogical to be asymmetrical.

So even if we do immediately send back a reply (I think a RST is the only real possibility at the moment, since ACK would still be dropped).  Worst case we're waking the hardware up from RX processing to full blown TX processing.

Furthermore if an inbound FIN causes an outbound RST, then that
RST will most likely prevent receiving future FIN retransmits.

So we're trading an RX->TX hardware wake up now,
for less RX wakeups in the (near) future.

This *might* just be an overall win.

I think a true solution likely needs to be smarter still
and allow skb->sk state != BPF_TCP_ESTABLISHED (or something)

Bug: 259199087
Bug: 264903985
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I143f12342f72d89f9450560c8d60dad4c79ffe64
2023-04-18 07:05:00 +00:00
Maciej Żenczykowski
ce7431f1fc bpf network stats - move double accounting tag!=0 out of bpf
Instead of also accounting tag!=0 traffic against tag==0 slot,
while the bpf code writes into the map, move this logic into
the userspace jni code which reads from the map.

Simplifies the bpf program making things easier on the
kernel's bpf verifier, and is better for performance,
since a per-packet fixup operation becomes a per-poll fixup.

Test: TreeHugger, atest libnetworkstats_test FrameworksNetTests
Bug: 276296921
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ic220a201781a1170bcffe327fe5664fc12b65dd9
2023-04-14 07:18:01 +00:00
Maciej Żenczykowski
896ef4e01b Merge "bpf netd - minor perf improvement - move uid check first" 2023-03-03 08:04:49 +00:00
Maciej Żenczykowski
e4c0473bd9 bpf netd - minor perf improvement - move uid check first
effectively no-op, but since it's a trivial check (uid < APP_START),
better do it first, rather than the complex packet parsing in
skip_owner_match().

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I35a9188e108987d48f03a18cdf70ec4cdd715376
2023-03-02 00:19:23 +00:00
Maciej Żenczykowski
8e4a794519 netd bpf - no-op code hygiene fix for DROP_UNLESS_DNS
We only ever return DROP_UNLESS_DNS on ingress,
so the ordering doesn't actually matter.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I742b85748433f5319d518bebc05d976d630b72e7
2023-03-02 00:12:08 +00:00
Ryan Zuklie
9419d25fe2 Add packet tracing bpf implementation
This adds the core BPF implementation of Android network packet tracing.
The new code looks into the skb to pull out various bits of information.
Both the program and the ring buffer are restricted to 5.8+ kernels and
userdebug or eng builds.

With the packet_info_config map defaulting to zero, userdebug and eng
builds won't run any of the tracing today. The only effect will be 32k
memory increase for the ringbuf and the check on the config array.

Bug: 246985031
Test: build & flash both userdebug and user
Change-Id: I144da2971c0738b565ad58abc17e456209f13bde
2023-01-23 09:51:31 -08:00
Ryan Zuklie
1db34f3b91 Add BPF helpers for ipv4/ipv6 and tcp/udp offsets.
Test: build & install connectivity module
Change-Id: I869810d05ad8266b6a4107a5276864fdbcdbd9b0
2023-01-23 09:50:31 -08:00
Ryan Zuklie
0c57d8567c Update BPF EXT maps with new ignore_on fields.
These all default to false, never ignoring the maps.

Bug: 246985031
Test: build connectivity module
Change-Id: I404d56dcb311b34587d56dd6edc292029c4ad83f
2023-01-19 16:16:02 -08:00
Ryan Zuklie
cc72fa8f9d Update bpf helpers to support new ignore_on fields
This change updates callers to include the new ignore_on and bpfloader
arguments as per the change in aosp/2374598.

Bug: 246985031
Test: tethering build & install, full platform build & install
Change-Id: Id940a6003ae4cb0bbfc65db8ff96590c4f3c847b
2023-01-05 13:26:30 -08:00
Maciej Żenczykowski
0966bbee1a netd.c - switch from is_4_19 to simply passing KVER(4, 19, 0) around
will make it easier to extend this for 5.4+ behaviour as well
without having to introduce another is_5_4 boolean

Bug: 263884894
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id4f6512d813dd460cb2b9a7ccb6a5f7b7e937575
2022-12-30 18:59:49 +00:00
Maciej Żenczykowski
28b9a29e04 netd.c - simplify, if not map A then by definition map B
easier on bpf verifier with no third case

Bug: 263884894
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5076de6f83ba522ed4783bca0a9d7fca4024986a
2022-12-29 12:06:49 +00:00
Maciej Żenczykowski
513474c1a7 rename bpf_shared.h to netd.h
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2e1569c8d70e98f9a3fdbab41fd2ba7f7b84dd37
2022-12-18 20:02:52 +00:00
Maciej Żenczykowski
d0c03cf20f Merge "bpf netd.c - switch from 'int direction' to 'bool egress'" 2022-11-21 20:29:40 +00:00
Maciej Żenczykowski
55474984e8 bpf netd.c - switch from 'int direction' to 'bool egress'
and get rid of some macros while we're at it.

This is just slightly easier to read.

(side note: this is all resolved at compile time!)

Bug: 259199087
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7b38afd4b6f9d73b4f34a90040639f0780544ac8
2022-11-21 16:53:38 +00:00
Maciej Żenczykowski
1b7c1f1c97 bpf netd.c - add a TODO
Bug: 259889995
Test: N/A, comment only
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0511433663738550b556208c140b08f76ca7898b
2022-11-21 09:41:33 +00:00
Maciej Żenczykowski
3621cbddf1 bpf netd.c - refactor skip_owner_match() tcp flag processing
(no-op simplification)

Bug: 259199087
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib9e126f1ca84f60b4057ccbbd0071f9684b20b92
2022-11-20 13:48:24 +00:00
Maciej Żenczykowski
879839a1d8 netd.c: on 4.19+ bpf_skb_load_bytes -> bpf_skb_load_bytes_relative
Bug: 145166494
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5fd18deee1c12386bdb8b554a33ac2845f881cc0
2022-10-28 01:40:41 +00:00
Lorenzo Colitti
3505b584d3 Un-drop support for pre-4.14 kernels.
This effectively reverts commit 6ed2ab9b57,
while ensuring that the program has the right permissions as
defined in r.android.com/2130014 :

oriole:/ # ls -lZ /sys/fs/bpf/netd_shared/prog_netd_cgroupsock_inet_create
-r--r----- 1 root root u:object_r:fs_bpf_netd_readonly:s0  0 2022-10-27 20:05 /sys/fs/bpf/netd_shared/prog_netd_cgroupsock_inet_create

Reason for revert: need to support 4.9 devices upgrading to T.

The only thing that cannot currently be supported on those
devices is the inet_create program which implements the
INTERNET permission.

Also, update bpf_existence_test so it does not check for the
existence of the program on pre-4.14 devices.

Bug: 254001921
Test: atest bpf_existence_test
Change-Id: I14f26cee5feeaae93b4d9710a7b9a2f835ff405f
2022-10-27 20:06:16 +09:00
Maciej Żenczykowski
285f705ced resolve nit from 'netd.c - update some permission related comments'
Test: TreeHugger
Bug: 236672779
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I5dcf6c32ef9bfd067afbfbbbd90e67ff678691f4
2022-08-09 17:50:46 +00:00
Maciej Żenczykowski
06085b0425 netd.c - update some permission related comments
Test: TreeHugger
Bug: 236672779
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I66187d61ccdd3d625a9873090ab8c0dff18ac6a3
2022-08-09 14:15:59 +00:00
Maciej Żenczykowski
c41e35d830 update CRITICAL annotations for mainline
(these only affect boot time logging)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3f315c5eabe73d3378e6ca0059f05221df7bab5e
2022-08-04 13:59:24 +00:00