Commit Graph

21 Commits

Author SHA1 Message Date
Maciej Żenczykowski
cc5b26dffe Merge "clat: use AF_PACKET/SOCK_RAW not SOCK_DGRAM sockets" 2023-03-14 03:01:08 +00:00
Maciej Żenczykowski
2460294974 clat: use AF_PACKET/SOCK_RAW not SOCK_DGRAM sockets
This switches incoming packets from L3 to L2,
ie. they now include the ethernet header for non-rawip cases
(like wifi)

Note: depends on userspace clatd change to offset packet
processing index by tpacket_auxdata->tp_net L3 header offset,
an increase in the read buffer size,  and on change to use
IPv6 header relative cBPF for ingress packet filtering!

Test: TreeHugger, atest libclat_test, ping 1.1.1.1 on v6-only wifi
Bug: 265591307
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ie5893d18b0dd796751f95150680a0d4cd5f64c3c
2023-03-14 00:50:18 +00:00
Maciej Żenczykowski
f3440dd85e clat: switch to relative cbpf filter
Needed to support AF_PACKET/SOCK_RAW sockets

Test: TreeHugger, ping on ipv6-only wifi works
Bug: 265591307
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: If324ee1e97a90b1a0a2ee0fabe1d91ef0603fdac
2023-03-10 23:46:15 +00:00
Maciej Żenczykowski
26b8e85a14 clatutils - code cleanup
add const, etc.

Test: TreeHugger
Bug: 265591307
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I8daa05891759370f6f5d7369a283d8ffc543a790
2023-03-10 23:25:41 +00:00
Maciej Żenczykowski
8bf5967d1e clat: just always set mark unconditionally, part 2
Setting SO_MARK to MARK_UNSET, ie. 0, is harmless,
since the default for a brand new socket is already 0.

See ClatCoordinator.java getFwMark() for the mark we actually pass in,
which is guaranteed to have at least 4 bits (16,17,18,19) set.

See also:
  https://android-review.git.corp.google.com/c/platform/packages/modules/Connectivity/+/2392272

Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I3b4eebc0a5ad20390a13efcc58cecf94868686ba
2023-01-30 19:31:27 +00:00
Maciej Żenczykowski
4fcf8a0061 clat: just return max uint32 from AF_PACKET cbpf filter
kernel's net/packet/af_packet.c packet_rcv() does (paraphrased):

  unsigned int snaplen = skb->len;
  unsigned int res = run_filter(skb, sk, snaplen);
  if (!res) goto drop_n_restore;
  if (snaplen > res) snaplen = res;

which makes it clear that cbpf filter returning 0 means drop,
while any other unsigned int (ie. u32) value means capture that
many bytes - but no more than packet length.

Might as well just use the maximum u32 as the snaplen,
since it will be truncated to skb->len as needed.

Of course additionally IPv6 packets can have a payload size of 65535
(which does not include the IPv6 header itself, and assumes we
don't bother with IPv6 jumbograms, which we can't translate to IPv4
anyways), so the L3 mtu should actually be 65535 + 40.
Except that is also too large to translate to ipv4,
so instead the max L3 mtu should be 65535 - 20 + 40 + 8
(which is the max IPv4 packet size - sizeof ipv4 header + sizeof
ipv6 header + sizeof ipv6 fragmentation extension header).

Since the cBPF currently deals with L3 packets it should return
an L3 length (ie. not including L2 headers), but this will change
when we switch to using L2 af_packet sockets (this change will
mean we will not need to change this code at that point in time).

Furthermore, this should have always returned MAXMTU, and not
PACKETLEN, as it does not care about the tun_pi extra header
(which is added later).

ie. this *should* have always been:
  #define MAXMTU (0xFFFF + 28)
  BPF_STMT(BPF_RET | BPF_K, MAXMTU)
but:
  BPF_STMT(BPF_RET | BPF_K, 0xFFFFFFFFu)
is even simpler.

Bug: 259872525
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I2cc4960f0092720b5ee196e8716f07826bd7f362
2023-01-29 18:48:08 +00:00
t-m-w
130e75b0ab Bypass VPN lockdown for clat initialization
This allows clat to initialize properly when VPNs are configured
with "Block connections without VPN", rather than to error out with
"no IPv6 addresses were available for clat".

This issue primarily affects particular mobile networks configured
with NAT64 (without direct IPv4 connectivity).

Bug: 255040839
Change-Id: I4a8ee0295e0f5d1e330f7529856347b8bd10360c
2022-10-25 01:54:51 +00:00
Hungming Chen
2477caa08b ClatUtils: add test GenerateIpv6AddressFailWithUlaSocketAddress
Used for increasing the test coverage of generateIpv6Address.

For now, generateIpv6Address test coverage: 0% (0/27 lines)
https://blackbox.googleplex.com/testsuite?branch=git_tm-mainline-prod-daily-release&target=mainline_modules_x86_coverage_r_released&testsuite=coverage%2Fmts_tethering_native_t_on_t&type=coverage

This is not a realistic test. The generateIpv6Address functionality is
not real verified. See also test MakeChecksumNeutral.

TODO: remove this test once EthernetTetheringTest can
test on mainline test coverage branch.

Test: atest ClatUtils

Change-Id: I8e4fbc5af2cd0dd0a1bb181388ea0d4b200baed3
2022-06-05 22:37:57 +08:00
Hungming Chen
14575d38cc Add native test libclat_test to MTS.
In order to get counted by mts code coverage, this native test needs to
be run as part of mts.

Bug: 233904825
Test: m mts && mts-tradefed run mts-tethering-coverage
Change-Id: I4ec7108577a8a50d4419bbf387535f92f2f6d099
2022-05-30 15:37:25 +08:00
Hungming Chen
b4bec86e0b Remove unused TcUtils{, Test}, bpfhelper from libclat
Don't need anymore since ClatCoordinator uses common lib to access
clat bpf prog/map.

Test: atest libclat_test
Change-Id: I2c3242f8b14b0058420ccfd85bb5728062c852bc
2022-03-31 16:04:42 +08:00
Hungming Chen
bd85f8903e ClatCoordinator: use Java class BpfMap to remove clat rule
This a preparation to replace duplicated native functions with the
existing Java class.

Bug: 221213090
Test: atest FrameworksNetTests
Test: manual check

Steps:
1. Connect wlan and check map content {iface index, v4addr, v6addr, pfx96} manually
after clat started.
$ adb shell dumpsys netd
  ClatdController
    BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)
      47(wlan0) 64:ff9b::/96 2a00:79e1:abc:6f02:2479:2f51:25a9:28f1 -> 192.0.0.4 53(v4-wlan0)
    BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)
      53(v4-wlan0) 192.0.0.4 -> 2a00:79e1:abc:6f02:2479:2f51:25a9:28f1 64:ff9b::/96 47(wlan0) ether

2. Disconnect wlan and check the bpf map is removed.
$ adb shell dumpsys netd
  ClatdController
    BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)
    BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)

Change-Id: Ia0c90c7104c4a42d34a28a5ddf3cf13a289f8c76
2022-03-31 16:04:42 +08:00
Hungming Chen
07fcf9ca49 ClatCoordinator: use Java class BpfMap to add clat rule
This a preparation to replace duplicated native functions with the
existing Java class.

Bug: 221213090
Test: atest FrameworksNetTests
Test: check map content {iface index, v4addr, v6addr, pfx96} manually.
The map fileds are the same as "ip addr" and clatd logging.

$ adb shell dumpsys netd
ClatdController
  BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)
    47(wlan0) 64:ff9b::/96 2a00:79e1:abc:6f02:f9e1:8c2d:604e:bc06 -> 192.0.0.4 53(v4-wlan0)
  BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)
    53(v4-wlan0) 192.0.0.4 -> 2a00:79e1:abc:6f02:f9e1:8c2d:604e:bc06 64:ff9b::/96 47(wlan0) ether

$ adb shell ip addr
47: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ..
53: v4-wlan0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1472 ..

03-14 18:35:04.822 30852 30852 I clatd   : Starting clat version 1.5 on wlan0 plat=64:ff9b:: v4=192.0.0.4 v6=2a00:79e1:abc:6f02:f9e1:8c2d:604e:bc06

Change-Id: I91d0cb6e76c4ef7bacf91b996786308ff4918f35
2022-03-31 15:53:33 +08:00
Yi Kong
9de79582a2 libclat: Remove redundant "using android::net::.*"
They are already under the same namespace.

Test: presubmit
Bug: 219872355
Change-Id: I5f51cf2c38755c99f220870c2ffe66ee8c454514
2022-02-17 16:36:13 +00:00
Maciej Żenczykowski
2f7dac1bad remove spurious headers
bpf_connectivity_headers already pulls in bpf_headers,
which already pulls in bpf_syscall_wrappers.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib22da8d433bd3187349f06f41ec064360c38982f
2022-02-02 16:38:36 -08:00
Maciej Żenczykowski
0bbe3d2ce4 libclat only depends on libbase_headers not libbase itself
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iad66f0d4bed4100261e8aa7d135e62336cdd1ae8
2022-02-02 16:38:36 -08:00
Hungming Chen
8ebdb6f1fe [CLATJ#18] libclat: move clat tc and bpf utilis from netd
Move native functions from ClatdController and TcUtils to libclat
for jni ClatdCoordinator.

Bug: 212345928
Test: atest libclat_test
Change-Id: Ia4b344b07537d57c3e4ff1285b0e7b17e0c63c3e
2022-01-22 20:07:19 +08:00
Hungming Chen
8ff032b990 [CLATJ#6] libclat: move configure_packet_socket() from netd to apex
configure_packet_socket() is moved from ClatdController. Only change
the failure return value from errno to -errno. The remaining parts
are the same.

The test is modified from ClatdControllerTest::ConfigureIpv6Address.
Simply set socket filter to an interface and check that the packet
socket is bound to the interface. We can't check the socket filter
because there is no way to fetch it from the kernel.

Bug: 212345928
Test: build and boot
      atest libclat_test
Change-Id: Ia33c21051b040829c7d2aa55ed5b7de8766c98c5
2022-01-20 22:50:12 +08:00
Hungming Chen
86a56de609 [CLATJ#5] libclat: move detect_mtu() from netd to apex
detect_mtu() is moved from ClatdController without behavior
change. The unique_fd for socket fd is replaced by primitive int
because libbase is not supported in mainline.

Bug: 212345928
Test: build and boot
      atest libclat_test
Change-Id: Ib9c1a9d4b9e1c141d88164e8489c5044fdf70685
2022-01-20 22:50:12 +08:00
Hungming Chen
2f623f381c [CLATJ#4] libclat: move selectIpv4Address() and isIpv4AddressFree() from netd to apex
isIpv4AddressFree() is moved from ClatdController without behavior
change.

selectIpv4Address() and its test are moved from ClatdController as well.
They have been refactored slightly for testing because function pointer
of isIpv4AddressFreeFunc is not a global variable anymore. We can't set
global function pointer to change selectIpv4Address() behavior for
testing. Instead isIpv4AddressFree is sent as an argument of function
pointer. For public caller, selectIpv4Address() has no behavior change.
For test caller, they can call an internal selectIpv4AddressInternal
(.., fn) to change isIpv4AddrFreeFn function pointer for testing.

Bug: 212345928
Test: build and boot
      atest libclat_test
Change-Id: Iab5e6fd5ebbccf10e7b3be2251b45949cf3f3464
2022-01-20 22:50:12 +08:00
Hungming Chen
6139d87623 [CLATJ#3] libclat: move generateIpv6Address() from netd to apex
generateIpv6Address() is moved from ClatdController without behavior
change. The unique_fd for socket fd is replaced by primitive int
because libbase is not supported in mainline.

Bug: 212345928
Test: build and boot
      atest libclat_test
Change-Id: I42e152e6fe3ad577a0274e1d55e737318f61176d
2022-01-20 22:50:12 +08:00
Hungming Chen
ed7b4602b5 [CLATJ#1] libclat: move makeChecksumNeutral() from netd to apex
Introduce the library libclat to implement clat functions for jni.
It is helpful for unit test.

makeChecksumNeutral() and its test are moved from ClatdController
without behavior change.

Bug: 212345928
Test: build and boot
      atest libclat_test

Change-Id: I1c0981d98141fc1cc07b2d3a0f3cbddf38683ff3
2022-01-20 22:50:08 +08:00