Commit Graph

15 Commits

Author SHA1 Message Date
Maciej Żenczykowski
f24beefe55 TcUtils jni: jobject clazz -> jclass clazz
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0a06d2627ef06fe4606b3d1a6525af767d218945
2023-10-04 19:15:53 +00:00
Patrick Rohr
66452f566b bpf_headers: rename KernelVersion.h to KernelUtils.h
Test: TH
Change-Id: Ifacc159c19a8fcb64b571295b945fb9fca82496a
2023-05-17 11:43:02 -07:00
Maciej Żenczykowski
6b8144a07c Revert "Allow BpfMap to be accessed from NetworkStack"
This reverts commit fbe95d914c707c34d2c9d150a467d51c73414fcd.

Reason for revert: I've reconsidered.  This is a bad idea.
(and there are not yet any users)

The NetworkStack is an apk, not an apex, and as such it cannot
ship any bpf .o files (since that requires apex disk image format
instead of apk/jar zip file format).

There's no support for this (NetworkStack shipping bpf) in the
current tip-of-tree bpfloader.
As such there's no chance of this happening before V.
And even in V+ it is *super* unlikely, because... apk...
(We'd have to add apk zip traversal into the bpfloader...)

As such NetworkStack cannot possibly own any bpf programs/maps,
and could only potentially access platform/system bpf maps or
bpf maps owned by another module (ie. the Tethering apex).

Using any bpf maps from the system is not viable, as these
are owned by the platform, and as such may be modified by
vendors/oems.  Ie. their number, names, key/value layout, etc...
cannot be guaranteed.  As such using them from mainline
code is simply not safe.

Furthermore none of the platform bpfs are network related
(and indeed bpfloader enforces this).

As such this the only potential use of this would be
for NetworkStack to use Tethering apex bpf maps/programs.
However, this is also unsafe.

On older devices (pre-S) we don't even have support for
tethering apex shipped programs/maps.

On pre-T only the offload program is shipped, while
roughly equivalent netd.o maps/programs for the other
stuff are still provided by the platform.
(but the format of these cannot be relied upon)

As such use would have to be limited to T+.
(because the offload bpf map isn't interesting
to the network stack)

But on T+ we run into a cross-module versioning problem:
the source (and thus bpf map name/format/struct definitions)
used to build the NetworkStack apk and Tethering apex may differ.
Even modules shipped in tandem are build from separate release branches.  Additionally there's potential for only one module
to update, while the other remains older.  Thus making this
work cross-module would require freezing the map name & format.
ie. they would need to become cross-module API.
This is not something I'm willing to do.

Basically, this can be summarized as:
there is no *safe* way for NetworkStack apk to use bpf maps.

Test: TreeHugger
Bug: 276230058
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I65ecf6ffca6ae88a1b72f6f4c8a5739991d78fe3
2023-05-04 10:18:46 +00:00
Junyu Lai
783a3b82ae Allow BpfMap to be accessed from NetworkStack
When loading BpfMap class, JNI part is needed for native
methods. Allow the static lib can be compiled with NetworkStack
JNI library.

Test: atest FrameworksNetTests:android.net.connectivity.com.android.server.BpfNetMapsTest
Bug: 276230058
Change-Id: I72ebe801dacd02de6711558d2058c1b756cf3080
2023-05-02 11:03:48 +00:00
Maciej Żenczykowski
32be06f45f verify java map key/value struct size matches file descriptor
(this should avoid kernel reading/writing from out of bounds)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I71fe71eee4e4e6a917477eef5fd2266439e803f3
2022-12-21 00:03:37 +00:00
Maciej Żenczykowski
023ad6a087 fix sign of error to be positive when passed to strerror()
Currently we see:
  E TcUtils : NLMSG_ERROR message return error: -2
  E ConnectivityService: TcUtils.tcFilterAddDevIngressPolice(ifaceIndex=6, PRIO_POLICE, ETH_P_ALL, rateInBytesPerSecond=2500000, bpfProgPath=/sys/fs/bpf/netd_shared/prog_netd_schedact_ingress_account) failure:
  E ConnectivityService: java.io.IOException: com_android_net_module_util_TcUtils_tcFilterAddDevIngressPolice error: : Unknown error -2

Bug: 231495412
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib49171e13d06082a37cbb12def1285d8875b5279
2022-06-09 22:35:13 +00:00
Maciej Żenczykowski
28e5347154 BpfMap: cache bpf map file descriptors
We switch back to int from ParcelFileDescriptor,
and eliminate all calls to close().  Bpf Map FDs
now live till process exit.

Bug: 230880517
Test: TreeHugger, atest com.android.networkstack.tethering.BpfMapTest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I89b6dc88ea56cb1e50695f8daf54ed79bce3fba2
2022-05-19 01:26:55 -07:00
Hungming Chen
b411547dd2 BpfMap: wrap native fd with ParcelFileDescriptor to avoid fd leak
ParcelFileDescriptor has implemented finalize(). Wrap native fd into
ParcelFileDescriptor which helps to release fd automatically.

Bug: 230880517
Test: manual test
Steps:
1. Connect to IPv6 only wifi and clat maps are created
$ adb shell cmd wifi set-wifi-enabled enabled
05-12 13:53:41.182  1793  2031 W BpfMap  : open /sys/fs/bpf/net_shared/map_clatd_clat_ingress6_map..: 493
05-12 13:53:41.182  1793  2031 W BpfMap  : open /sys/fs/bpf/net_shared/map_clatd_clat_egress4_map..: 546

$ adb shell ls -all proc/1793/fd | grep bpf
.. system system 64 2022-05-12 13:55:35 .. 493 -> anon_inode:bpf-map
.. system system 64 2022-05-12 13:55:35 .. 546 -> anon_inode:bpf-map

$ adb shell dumpsys connectivity
Forwarding rules:
  BPF ingress map: iif nat64Prefix v6Addr -> v4Addr oif
    47 /64:ff9b::/96 /2a00:79e1:abc:6f02:6efd:1d4b:f05e:25bd -> /192.0.0.4 54
  BPF egress map: iif v4Addr -> v6Addr nat64Prefix oif
    54 /192.0.0.4 -> /2a00:79e1:abc:6f02:6efd:1d4b:f05e:25bd /64:ff9b::/96 47 ether

2. Disconnect from IPv6 only wifi, force GC and clat map fds are released
$ adb shell cmd wifi set-wifi-enabled disabled
$ adb shell kill -10 1793
$ adb shell ls -all proc/1793/fd | grep bpf
(fd 493 and 546 are removed)

Change-Id: I26bbafbd73eccab6f4ae2c71690ecad12bbef7df
2022-05-12 15:41:19 +08:00
Hungming Chen
556c8010c9 TcUtils: add tcQdiscAddDevClsact
Support tc command:
$ tc qdisc add dev .. clsact

Test: TreeHugger
Change-Id: I98abcb59418ab12b6e4de0f42a18ded4677ddbfc
2022-03-17 17:23:04 +08:00
Patrick Rohr
23077d5a49 Include libtcutils inside libnet_utils_device_common_bpfjni
This way, users of libnet_utils_device_common_bpfjni do not also have to
separately list the required libtcutils.

Test: build, boots
Change-Id: Id40863de83b6c40b79f38d638299626f7e025810
2022-02-01 03:07:46 +00:00
markchien
32a6e9cbde Add visibility for use bpfmap by BpfInterfaceMapUpdater
Bug: 215095957
Test: m
Change-Id: Idbb8e3cbf520a29444ccdfd2d1c42553c886a7f0
2022-01-21 22:33:38 +08:00
Patrick Rohr
eb3a8c734b Add tc police support to jni lib
Bug: 157552970
Test: TreeHugger
Change-Id: I63b4324bbb8d20b7d401921dad1f8e73ccc26a39
2022-01-19 14:20:27 +01:00
Patrick Rohr
f714f1b116 Move BpfUtils -- add JNI wrapper for libtcutils
This CL adds the copied JNI wrappers for tcutils.

Bug: 202086915
Bug: 157552970
Test: atest TetheringTests
Change-Id: Ib3f8b24a12a18f1ba64fd17304274c4dba37fc47
2022-01-13 22:24:29 +01:00
markchien
f3448f905a Separate bpf and struct util from netlink util library
1. Separate bpf and struct libraries from netlink library.
2. Rename bpfmap jni library to respect its java side library.
3. Add README to explain the rules of adding shared jni library.
4. Also allow packages/modules/Connectivity to use bpf library.

Bug: 205088391
Test: atest TetheringTests
      atest CtsTetheringTest
      atest TetheringPrivilegedTests
      atest ConnectivityCoverageTests
Change-Id: I6e668818bede63b241cd901c0967f401613ddaf6
2021-11-11 18:50:53 +08:00
Tyler Wear
aafd9c18fb bpfmap: Move to Common Util Location
Multiple packages need access to bpf maps. Moving to common
location to allow access from all necessary packages.

Test: atest BpfMapTest
Bug: 179733303

Change-Id: Idae7b620c15c781b2e7980c3a3157f396cfaf66e
2021-10-29 12:38:05 -07:00