blob: 897a0a72fa326eb230972056d891fd70eb59d409 [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"
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090013#include "patchpanel/firewall.h"
Garrick Evans3388a032020-03-24 11:25:55 +090014#include "patchpanel/minijailed_process_runner.h"
15#include "patchpanel/ndproxy.h"
Taoyu Liba04fe32020-01-14 13:19:35 +090016
Garrick Evans3388a032020-03-24 11:25:55 +090017void OnSocketReadReady(patchpanel::NDProxy* proxy, int fd) {
Taoyu Liba04fe32020-01-14 13:19:35 +090018 proxy->ReadAndProcessOneFrame(fd);
19}
20
Garrick Evans3388a032020-03-24 11:25:55 +090021void OnGuestIpDiscovery(patchpanel::Datapath* datapath,
Taoyu Liba04fe32020-01-14 13:19:35 +090022 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
31int 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 Evans3388a032020-03-24 11:25:55 +090042 patchpanel::MinijailedProcessRunner runner;
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090043 patchpanel::Firewall firewall;
Garrick Evans8e8e3472020-01-23 14:03:50 +090044 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 Liba04fe32020-01-14 13:19:35 +090048 LOG(ERROR) << "Failed to enable " << accept_ra_sysctl_cmd << ".";
49 return EXIT_FAILURE;
50 }
Garrick Evans8e8e3472020-01-23 14:03:50 +090051 if (runner.sysctl_w("net.ipv6.conf.all.forwarding", "1") != 0) {
Taoyu Liba04fe32020-01-14 13:19:35 +090052 LOG(ERROR) << "Failed to enable net.ipv6.conf.all.forwarding.";
53 return EXIT_FAILURE;
54 }
Jason Jeremy Imana7273a32020-08-04 11:25:31 +090055 patchpanel::Datapath datapath(&runner, &firewall);
Taoyu Liba04fe32020-01-14 13:19:35 +090056
Garrick Evans3388a032020-03-24 11:25:55 +090057 patchpanel::NDProxy proxy;
Taoyu Liba04fe32020-01-14 13:19:35 +090058 if (!proxy.Init()) {
59 PLOG(ERROR) << "Failed to initialize NDProxy internal state";
60 return EXIT_FAILURE;
61 }
Stephen Barber7e24d3d2020-02-07 11:02:14 -080062
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 Li7dca19a2020-03-16 16:27:07 +090067 if (proxy.AddInterfacePair(args[0], args[1])) {
Stephen Barber7e24d3d2020-02-07 11:02:14 -080068 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 Liba04fe32020-01-14 13:19:35 +090079 proxy.RegisterOnGuestIpDiscoveryHandler(
80 base::Bind(&OnGuestIpDiscovery, &datapath));
81
Garrick Evans3388a032020-03-24 11:25:55 +090082 base::ScopedFD fd = patchpanel::NDProxy::PreparePacketSocket();
Taoyu Liba04fe32020-01-14 13:19:35 +090083 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}