Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Stephen Barber | 7e24d3d | 2020-02-07 11:02:14 -0800 | [diff] [blame] | 4 | #include <unistd.h> |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 5 | |
| 6 | #include <base/bind.h> |
| 7 | #include <base/command_line.h> |
| 8 | #include <base/files/scoped_file.h> |
| 9 | #include <base/macros.h> |
| 10 | #include <brillo/daemons/daemon.h> |
| 11 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 12 | #include "patchpanel/minijailed_process_runner.h" |
| 13 | #include "patchpanel/ndproxy.h" |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 14 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 15 | void OnSocketReadReady(patchpanel::NDProxy* proxy, int fd) { |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 16 | proxy->ReadAndProcessOneFrame(fd); |
| 17 | } |
| 18 | |
Hugo Benichi | 3994ea2 | 2021-01-12 09:46:05 +0900 | [diff] [blame^] | 19 | void OnGuestIpDiscovery(patchpanel::MinijailedProcessRunner* runner, |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 20 | const std::string& ifname, |
| 21 | const std::string& ip6addr) { |
Hugo Benichi | 3994ea2 | 2021-01-12 09:46:05 +0900 | [diff] [blame^] | 22 | if (runner->ip6("route", "replace", {ip6addr + "/128", "dev", ifname}) != 0) |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 23 | LOG(WARNING) << "Failed to setup the IPv6 route for interface " << ifname; |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Stand-alone daemon to proxy ND frames between a pair of interfaces |
| 27 | // Usage: ndproxyd $physical_ifname $guest_ifname |
| 28 | int main(int argc, char* argv[]) { |
| 29 | base::CommandLine::Init(argc, argv); |
| 30 | base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
| 31 | base::CommandLine::StringVector args = cl->GetArgs(); |
| 32 | if (args.size() < 2) { |
| 33 | LOG(ERROR) << "Missing command line arguments; exiting"; |
| 34 | return EXIT_FAILURE; |
| 35 | } |
| 36 | |
| 37 | brillo::Daemon daemon; |
| 38 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 39 | patchpanel::MinijailedProcessRunner runner; |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 40 | char accept_ra_sysctl_cmd[40] = {0}; |
| 41 | snprintf(accept_ra_sysctl_cmd, sizeof(accept_ra_sysctl_cmd), |
| 42 | "net.ipv6.conf.%s.accept_ra", args[0].c_str()); |
| 43 | if (runner.sysctl_w(accept_ra_sysctl_cmd, "2") != 0) { |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 44 | LOG(ERROR) << "Failed to enable " << accept_ra_sysctl_cmd << "."; |
| 45 | return EXIT_FAILURE; |
| 46 | } |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 47 | if (runner.sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0) { |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 48 | LOG(ERROR) << "Failed to enable net.ipv6.conf.all.forwarding."; |
| 49 | return EXIT_FAILURE; |
| 50 | } |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 51 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 52 | patchpanel::NDProxy proxy; |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 53 | if (!proxy.Init()) { |
| 54 | PLOG(ERROR) << "Failed to initialize NDProxy internal state"; |
| 55 | return EXIT_FAILURE; |
| 56 | } |
Stephen Barber | 7e24d3d | 2020-02-07 11:02:14 -0800 | [diff] [blame] | 57 | |
| 58 | // Crostini depends on another daemon (LXD) creating the guest bridge |
| 59 | // interface. This can take a few seconds, so retry if necessary. |
| 60 | bool added_interfaces = false; |
| 61 | for (int i = 0; i < 10; i++) { |
Taoyu Li | 7dca19a | 2020-03-16 16:27:07 +0900 | [diff] [blame] | 62 | if (proxy.AddInterfacePair(args[0], args[1])) { |
Stephen Barber | 7e24d3d | 2020-02-07 11:02:14 -0800 | [diff] [blame] | 63 | added_interfaces = true; |
| 64 | break; |
| 65 | } |
| 66 | usleep(1000 * 1000 /* 1 second */); |
| 67 | } |
| 68 | if (!added_interfaces) { |
| 69 | LOG(ERROR) << "Network interfaces " << args[0] << " and " << args[1] |
| 70 | << " could not be added; do they exist?"; |
| 71 | return EXIT_FAILURE; |
| 72 | } |
| 73 | |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 74 | proxy.RegisterOnGuestIpDiscoveryHandler( |
Hugo Benichi | 3994ea2 | 2021-01-12 09:46:05 +0900 | [diff] [blame^] | 75 | base::Bind(&OnGuestIpDiscovery, &runner)); |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 76 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 77 | base::ScopedFD fd = patchpanel::NDProxy::PreparePacketSocket(); |
Taoyu Li | ba04fe3 | 2020-01-14 13:19:35 +0900 | [diff] [blame] | 78 | if (!fd.is_valid()) { |
| 79 | PLOG(ERROR) << "Failed to initialize data socket"; |
| 80 | return EXIT_FAILURE; |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher = |
| 84 | base::FileDescriptorWatcher::WatchReadable( |
| 85 | fd.get(), base::Bind(&OnSocketReadReady, &proxy, fd.get())); |
| 86 | |
| 87 | return daemon.Run(); |
| 88 | } |