Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2aa1a124 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

Replace useCanSockets hack with flexible per-process domain selection

Bug: 158011272
Test: manual
Change-Id: I29d88a42e2309483a740cde9f9a463d83c7a560c
parent cef964c1
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -22,8 +22,6 @@

namespace android::netdevice {

socketparams::Params socketparams::current = general;

unsigned int nametoindex(const std::string& ifname) {
    const auto ifidx = if_nametoindex(ifname.c_str());
    if (ifidx != 0) return ifidx;
+0 −15
Original line number Diff line number Diff line
@@ -25,21 +25,6 @@

namespace android::netdevice {

namespace socketparams {

struct Params {
    int domain;
    int type;
    int protocol;
};

constexpr Params general = {AF_INET, SOCK_DGRAM, 0};
constexpr Params can = {PF_CAN, SOCK_RAW, CAN_RAW};

extern Params current;

}  // namespace socketparams

/**
 * Returns the index of a given network interface.
 *
+26 −2
Original line number Diff line number Diff line
@@ -21,11 +21,35 @@
#include <android-base/logging.h>
#include <android-base/unique_fd.h>

#include <map>

namespace android::netdevice::ifreqs {

static constexpr int defaultSocketDomain = AF_INET;
std::atomic_int socketDomain = defaultSocketDomain;

struct SocketParams {
    int domain;
    int type;
    int protocol;
};

static const std::map<int, SocketParams> socketParams = {
        {AF_INET, {AF_INET, SOCK_DGRAM, 0}},
        {AF_CAN, {AF_CAN, SOCK_RAW, CAN_RAW}},
};

static SocketParams getSocketParams(int domain) {
    if (socketParams.count(domain)) return socketParams.find(domain)->second;

    auto params = socketParams.find(defaultSocketDomain)->second;
    params.domain = domain;
    return params;
}

bool send(unsigned long request, struct ifreq& ifr) {
    base::unique_fd sock(socket(socketparams::current.domain, socketparams::current.type,
                                socketparams::current.protocol));
    const auto sp = getSocketParams(socketDomain);
    base::unique_fd sock(socket(sp.domain, sp.type, sp.protocol));
    if (!sock.ok()) {
        LOG(ERROR) << "Can't create socket";
        return false;
+5 −0
Original line number Diff line number Diff line
@@ -22,6 +22,11 @@

namespace android::netdevice::ifreqs {

/**
 * \see useSocketDomain()
 */
extern std::atomic_int socketDomain;

/**
 * Sends ioctl interface request.
 *
+5 −6
Original line number Diff line number Diff line
@@ -27,15 +27,14 @@ namespace android::netdevice {
typedef std::array<uint8_t, ETH_ALEN> hwaddr_t;

/**
 * Configures libnetdevice to use PF_CAN sockets instead of AF_INET,
 * Configures libnetdevice to use other socket domain than AF_INET,
 * what requires less permissive SEPolicy rules for a given process.
 *
 * In such case, the process would only be able to control CAN interfaces.
 *
 * TODO(b/158011272): consider less hacky solution
 * \param yes true to use CAN sockets, false for general sockets
 * In such case, the process would only be able to control interfaces of a given kind.

 * \param domain Socket domain to use (e.g. AF_CAN), see socket(2) for details
 */
void useCanSockets(bool yes);
void useSocketDomain(int domain);

/**
 * Checks, if the network interface exists.
Loading