arc: Move platform2/arc/network/ to platform2/patchpanel

Next step in the arc-networkd -> patchpanel rename, this patch moves the
location of the code.

BUG=b:151879931
TEST=units,flashed image to atlas
TEST=tasts arc.PlayStore, crostini.LaunchTerminal.download

Change-Id: I1b5cf8d670e1631d46f6449b725395157bf88dde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2115863
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/broadcast_forwarder.h b/patchpanel/broadcast_forwarder.h
new file mode 100644
index 0000000..a4df2f2
--- /dev/null
+++ b/patchpanel/broadcast_forwarder.h
@@ -0,0 +1,99 @@
+// Copyright 2020 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PATCHPANEL_BROADCAST_FORWARDER_H_
+#define PATCHPANEL_BROADCAST_FORWARDER_H_
+
+#include <sys/socket.h>
+
+#include <shill/net/rtnl_listener.h>
+#include "patchpanel/shill_client.h"
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include <base/files/file_descriptor_watcher_posix.h>
+#include <base/macros.h>
+
+#include "patchpanel/net_util.h"
+
+namespace patchpanel {
+
+constexpr uint32_t kBcastAddr = Ipv4Addr(255, 255, 255, 255);
+
+// Listens to broadcast messages sent by applications and forwards them between
+// network interfaces of host and guest.
+// BroadcastForwarder assumes that guest addresses, including broadcast and
+// netmask, are constant.
+class BroadcastForwarder {
+ public:
+  explicit BroadcastForwarder(const std::string& dev_ifname);
+  virtual ~BroadcastForwarder() = default;
+
+  bool AddGuest(const std::string& br_ifname);
+  void RemoveGuest(const std::string& br_ifname);
+
+ protected:
+  // Socket is used to keep track of an fd and its watcher.
+  // It also stores addresses corresponding to the interface it is bound to.
+  struct Socket {
+    Socket(base::ScopedFD fd,
+           const base::Callback<void(int)>& callback,
+           uint32_t addr,
+           uint32_t broadaddr,
+           uint32_t netmask = 0);
+    base::ScopedFD fd;
+    std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher;
+    uint32_t addr;
+    uint32_t broadaddr;
+    uint32_t netmask;
+  };
+
+  // Bind will create a broadcast socket and return its fd.
+  // This is used for sending broadcasts.
+  static base::ScopedFD Bind(const std::string& ifname, uint16_t port);
+
+  // BindRaw will create a broadcast socket that listens to all IP packets.
+  // It filters the packets to only broadcast packets that is sent by
+  // applications.
+  // This is used to listen on broadcasts.
+  static base::ScopedFD BindRaw(const std::string& ifname);
+
+  // SendToNetwork sends |data| using a socket bound to |src_port| and
+  // |dev_ifname_| using a temporary socket.
+  bool SendToNetwork(uint16_t src_port,
+                     const void* data,
+                     ssize_t len,
+                     const struct sockaddr_in& dst);
+
+  // SendToGuests will forward the broadcast packet to all Chrome OS guests'
+  // (ARC++, Crostini, etc) internal fd.
+  bool SendToGuests(const void* ip_pkt,
+                    ssize_t len,
+                    const struct sockaddr_in& dst);
+
+  // Callback from RTNetlink listener, invoked when the lan interface IPv4
+  // address is changed.
+  void AddrMsgHandler(const shill::RTNLMessage& msg);
+
+  // Listens for RTMGRP_IPV4_IFADDR messages and invokes AddrMsgHandler.
+  std::unique_ptr<shill::RTNLListener> addr_listener_;
+
+  const std::string dev_ifname_;
+  std::unique_ptr<Socket> dev_socket_;
+
+  // Mapping from guest bridge interface name to its sockets.
+  std::map<std::string, std::unique_ptr<Socket>> br_sockets_;
+
+ private:
+  void OnFileCanReadWithoutBlocking(int fd);
+
+  base::WeakPtrFactory<BroadcastForwarder> weak_factory_{this};
+  DISALLOW_COPY_AND_ASSIGN(BroadcastForwarder);
+};
+
+}  // namespace patchpanel
+
+#endif  // PATCHPANEL_BROADCAST_FORWARDER_H_