blob: 2b7ac2cf0c40fabfb58128dd5cd50accb397b3a2 [file] [log] [blame]
Taoyu Liba04fe32020-01-14 13:19:35 +09001// 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 Barber7e24d3d2020-02-07 11:02:14 -08004#include <unistd.h>
Taoyu Liba04fe32020-01-14 13:19:35 +09005
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 Evans3388a032020-03-24 11:25:55 +090012#include "patchpanel/datapath.h"
13#include "patchpanel/minijailed_process_runner.h"
14#include "patchpanel/ndproxy.h"
Taoyu Liba04fe32020-01-14 13:19:35 +090015
Garrick Evans3388a032020-03-24 11:25:55 +090016void OnSocketReadReady(patchpanel::NDProxy* proxy, int fd) {
Taoyu Liba04fe32020-01-14 13:19:35 +090017 proxy->ReadAndProcessOneFrame(fd);
18}
19
Garrick Evans3388a032020-03-24 11:25:55 +090020void OnGuestIpDiscovery(patchpanel::Datapath* datapath,
Taoyu Liba04fe32020-01-14 13:19:35 +090021 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
30int 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 Evans3388a032020-03-24 11:25:55 +090041 patchpanel::MinijailedProcessRunner runner;
Garrick Evans8e8e3472020-01-23 14:03:50 +090042 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 Liba04fe32020-01-14 13:19:35 +090046 LOG(ERROR) << "Failed to enable " << accept_ra_sysctl_cmd << ".";
47 return EXIT_FAILURE;
48 }
Garrick Evans8e8e3472020-01-23 14:03:50 +090049 if (runner.sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0) {
Taoyu Liba04fe32020-01-14 13:19:35 +090050 LOG(ERROR) << "Failed to enable net.ipv6.conf.all.forwarding.";
51 return EXIT_FAILURE;
52 }
Garrick Evans3388a032020-03-24 11:25:55 +090053 patchpanel::Datapath datapath(&runner);
Taoyu Liba04fe32020-01-14 13:19:35 +090054
Garrick Evans3388a032020-03-24 11:25:55 +090055 patchpanel::NDProxy proxy;
Taoyu Liba04fe32020-01-14 13:19:35 +090056 if (!proxy.Init()) {
57 PLOG(ERROR) << "Failed to initialize NDProxy internal state";
58 return EXIT_FAILURE;
59 }
Stephen Barber7e24d3d2020-02-07 11:02:14 -080060
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 Li7dca19a2020-03-16 16:27:07 +090065 if (proxy.AddInterfacePair(args[0], args[1])) {
Stephen Barber7e24d3d2020-02-07 11:02:14 -080066 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 Liba04fe32020-01-14 13:19:35 +090077 proxy.RegisterOnGuestIpDiscoveryHandler(
78 base::Bind(&OnGuestIpDiscovery, &datapath));
79
Garrick Evans3388a032020-03-24 11:25:55 +090080 base::ScopedFD fd = patchpanel::NDProxy::PreparePacketSocket();
Taoyu Liba04fe32020-01-14 13:19:35 +090081 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}