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