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