Commit Graph

180 Commits

Author SHA1 Message Date
KH Shi
f92d211c71 Merge "Pass upstream prefixes to bpf tethering offload" into main 2023-10-27 07:23:52 +00:00
KH Shi
3f738fcd3d Pass upstream prefixes to bpf tethering offload
BPF needs upstream prefixes information to filter spoofing IPv6 source
addresses carried in downstream traffic.

We retrieve prefixes from upstream interface's LinkProperties and pass
it to the BpfCoordinator. Forwarding rules will also be updated when
upstream interface's IPv6 link addresses change.

Test: atest TetheringTests
Bug: 261923493
Change-Id: If8cfc3b191e520ca838654d1b5211ab9e9ec021d
2023-10-26 13:13:38 +00:00
Ken Chen
cf25a54909 Correct LINT path on UidOwnerMatchType
UidOwnerMatchType Java definition moved from BpfNetMaps.java to
BpfNetMapsConstants.java in change I6d7ea044e43180.

Bug: 297836825
Test: presubmit
Change-Id: I4fc28406750cac9143ea47e9304b455ab616d462
2023-10-21 01:47:05 +00:00
Maciej Żenczykowski
e1a615ad83 type safety for 'bool downstream'
funky naming 'stream.down', because downstream.downstream is just too long...

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id234654fa8960e7430fc33119f36fd94b858d242
2023-10-10 03:37:14 -07:00
Maciej Żenczykowski
8a6c6d50c0 type safety for 'bool updatetime'
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I99d51524ba913c46bad64b89778c01164639c85e
2023-10-10 03:37:14 -07:00
Maciej Żenczykowski
8d3bde7949 type safety for is_ethernet
(reversing logic, as 'rawip.rawip' is much shorter then 'ethernet.ethernet')

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib48cc2b889e8b587e14edbe89606f887a884af87
2023-10-08 20:16:59 -07: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
3cb494fd28 move BPF_CGROUP_INET*_BIND registration into BpfHandler
(in preparation for moving it into netbpfload)

The programs themselves (in bpf_progs/block.c) required a 5.4+ kernel.

We relax this restriction to 4.19+ as we don't have any 5.4 device coverage
(while the pixel 4a 5G / 5 / 5a are all 4.19 devices).

I believe we could relax it further to 4.14+ but Pixel 4/4xl/4a that
would exercise those code paths are EOL and probably have poor to
non existent test coverage, and we cannot do anything for 4.9 T devices
anyway.

Note: on <4.19 kernels (ie. T devices running 4.9/4.14, U running 4.14)
this results in ConnectivityNativeService going from null to initialized
(as the bpf map will exist).

This doesn't hurt as the set/clear port interfaces are only ever
called by vendor code on devices where the kernel doesn't support
the older mechanism.  And even if you call them it will just set/clear
the bits in the bpf bitmap, they just won't actually affect anything.

We could flag the map itself as being 4.19+ as well, but I think
I prefer the no-op map to exist...

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1085addd22f4f3b709e1875049633832c5dac836
2023-10-06 14:31:20 -07:00
Maciej Żenczykowski
10da6d4e61 move PRIVATE/SHARED/MANDATORY/OPTIONAL/{LOAD,IGNORE}_ON_* into core headers
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I7053ab46e73b763f64f36baf5e7ab64b62421045
2023-10-06 14:25:41 -07:00
Maciej Żenczykowski
a09c2bbe49 use static const instead of #define (DIS)ALLOW
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1b38f6ac0f511c07610a6afec616e6ec880d8905
2023-10-06 14:08:50 -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
Maciej Żenczykowski
2387323a23 switch from defines to static consts
tm-mainline-prod is no longer in use

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8704dccff1432ba811e99a89ea60028fd63365b5
2023-10-03 05:15:18 +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
66da6a9b44 possibly fix clat ingress nat64 drop
Due to try_make_writable's implementation:

  // try to make the 1st 'len' header bytes r/w via DPA
  void try_make_writable(struct __sk_buff* skb, int len) {
    if (len > skb->len) len = skb->len;
    if (skb->data_end - skb->data < len) bpf_skb_pull_data(skb, len);
  }

This *should* normally result in nothing actually being done.

This is because the 'len' we request should trivially be <= skb->len
(by virtue of how we construct the packet / get here),
and because skb->data_end - skb->data < len was previously
(to this patch) already checked below in line 251
(and thus the packet would have been dropped if it was false).

However, there's a tentative theory that we could somehow end up
with the entire payload in the non-linear portion of the packet,
and thus need to move it into the linear header portion where
we actually have direct packet access to it.

Note also that we already called this in line 71, so it should
be safe to add another call without causing bpf verifier unhappiness...

Test: TreeHugger
Bug: 298879031
Signed-off-by: Maciej Żenczykowski <maze@google.com
Change-Id: If3531c3cf6932ac3f1d384a43d28326d17544aa3
2023-09-08 18:20:21 +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
a693bac180 StatsValue - define operator+=
while this is a little bit more code,
it seems much better for the accumulation operation
to be next to the struct definition itself
(in case we ever add more fields)

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I26022db4566e69c964298d7b3f2cc4fa4a9a5152
2023-08-13 09:16:23 +00:00
Maciej Żenczykowski
4bcac5dadf eliminate 'struct Stats' in favour of StatsValue
Bug: 294604315
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib45a130e7fa36ad90334c00de637d523455e000e
2023-08-12 16:24:49 +00:00
Maciej Żenczykowski
15b63b308e eliminate unused tcp{Rx,Tx}Packets
(next step is to replace use of Stats struct with
 identical (except field order) StatsValue struct)

Test: TreeHugger
Bug: 294604315
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I9be3c411f9592bf4edc75386b1c5b386ebeb5905
2023-08-12 08:20:39 +00:00
KH Shi
c8fb1a2530 bpf tethering offload - add src subnet to upstream ipv6 direction
This patch is based on aosp/2535559 from maze@.

Add source prefix into the upstream key such that only packets which
source IPv6 address matches it will be forwarded to the upstream
interface.

In this patch, the source prefix is set to zero so there is no
behavior changes. Next CL in patch series will use the real source
prefixes retrieved from upstream interface.

Test: atest TetheringTests
Bug: 261923493
Change-Id: I43d068a29b937c7dfeb6fab632a8effb47ee2263
2023-08-10 16:01:21 +08:00
Maciej Żenczykowski
43d31bbaf6 clatd.c - add support for UDPLITE
This is trivial - as the UDPLITE pseudoheader is identical
to the UDP pseudoheader (except that the UDPLITE pseudo length
is derived from the IPv4 total length / IPv6 payload length
field, instead of being copied from the UDPLITE header 'coverage
length' field - but this doesn't matter, as it [ie. the udplite
payload length] doesn't change during 464xlat translation).

Additionally UDPLITE never sends a checksum value of 0,
as at least 8 bytes (the UDPLITE header) *must* be included
in the checksum field, and a 0 must be sent as 0xFFFF.

See: https://datatracker.ietf.org/doc/html/rfc3828

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I00a110b793fcf3cf705a9a706811da7866c3e810
2023-07-25 16:17:48 +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
7a03c187f5 remove inprocess tethering
It is just a constant source of bugs, with no real tests,
let's stop pretending this is a supported configuration.

The only tested configuration is out-of-process tethering
updatable apex.

Test: TreeHugger
Bug: 279942846
Change-Id: I4b659a3cd32b89a65549b56006b926a5ac755f7b
2023-04-28 20:11:14 +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
58c6337906 sed -i -r 's@/[*] updatetime [*]/ false@NO_UPDATETIME@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I69947ef2bfc330d38c56b64e9d43dbea1b78cf69
2023-04-20 04:05:06 +00:00
Maciej Żenczykowski
653bb006b7 sed -i -r 's@/[*] updatetime [*]/ true@UPDATETIME@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie62454bee2c7c0a23cc6827ee626640281527f19
2023-04-20 04:04:58 +00: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
66f52f4ea7 sed -i -r 's@/[*] is_ethernet [*]/ false@RAWIP@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id488301a50e28e2636936340c78bf8ac5ed2f34e
2023-04-20 04:04:04 +00:00
Maciej Żenczykowski
63fadd1c67 sed -i -r 's@/[*] is_ethernet [*]/ true@ETHER@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib3967f1768820e92011eee78f589cc54ee3677fc
2023-04-20 04:03:55 +00:00
Maciej Żenczykowski
cad569fef5 sed -i -r 's@/[*] downstream [*]/ true@DOWNSTREAM@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ia1eeca7e8cda672562f3da5b28b52cc25cae2f2a
2023-04-20 04:03:48 +00:00
Maciej Żenczykowski
941ea039db sed -i -r 's@/[*] downstream [*]/ false@UPSTREAM@g' offload.c
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ieaf2cb8d93b4b62fba1be9893ed7e2510b5ed4b7
2023-04-20 04:03:41 +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
2541a1924e offload.c - feed kver in to bpf progs
Bug: 263884894
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1ad6df9e4cf4e39b4fe785ca9d8ff0eaa31d33c5
2023-04-19 20:51:18 +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