summaryrefslogtreecommitdiff
path: root/server/WakeupController.cpp
blob: eb36291c27f108ff6117dfa60a744a876d383512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "WakeupController"

#include <arpa/inet.h>
#include <iostream>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_log.h>
#include <sys/socket.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>

#include <android-base/strings.h>
#include <android-base/stringprintf.h>
#include <log/log.h>
#include <netdutils/Netfilter.h>
#include <netdutils/Netlink.h>

#include "IptablesRestoreController.h"
#include "NetlinkManager.h"
#include "WakeupController.h"

namespace android {
namespace net {

using base::StringPrintf;
using netdutils::Slice;
using netdutils::Status;

const char WakeupController::LOCAL_MANGLE_INPUT[] = "wakeupctrl_mangle_INPUT";

const uint32_t WakeupController::kDefaultPacketCopyRange =
        sizeof(struct tcphdr) + sizeof(struct ip6_hdr);

static void extractIpPorts(WakeupController::ReportArgs& args, Slice payload) {
    switch (args.ipNextHeader) {
        case IPPROTO_TCP: {
            struct tcphdr header;
            if (extract(payload, header) < sizeof(struct tcphdr)) {
                return;
            }
            args.srcPort = ntohs(header.th_sport);
            args.dstPort = ntohs(header.th_dport);
            break;
        }
        case IPPROTO_UDP: {
            struct udphdr header;
            if (extract(payload, header) < sizeof(struct udphdr)) {
                return;
            }
            args.srcPort = ntohs(header.uh_sport);
            args.dstPort = ntohs(header.uh_dport);
            break;
        }
        default:
            break;
    }
}

static void extractIpHeader(WakeupController::ReportArgs& args, Slice payload) {
    switch (args.ethertype) {
        case ETH_P_IP: {
            struct iphdr header;
            if (extract(payload, header) < sizeof(struct iphdr)) {
                return;
            }
            args.ipNextHeader = header.protocol;
            char addr[INET_ADDRSTRLEN] = {};
            inet_ntop(AF_INET, &header.saddr, addr, sizeof(addr));
            args.srcIp = addr;
            inet_ntop(AF_INET, &header.daddr, addr, sizeof(addr));
            args.dstIp = addr;
            extractIpPorts(args, drop(payload, header.ihl * 4)); // ipv4 IHL counts 32 bit words.
            break;
        }
        case ETH_P_IPV6: {
            struct ip6_hdr header;
            if (extract(payload, header) < sizeof(struct ip6_hdr)) {
                return;
            }
            args.ipNextHeader = header.ip6_nxt;
            char addr[INET6_ADDRSTRLEN] = {};
            inet_ntop(AF_INET6, &header.ip6_src, addr, sizeof(addr));
            args.srcIp = addr;
            inet_ntop(AF_INET6, &header.ip6_dst, addr, sizeof(addr));
            args.dstIp = addr;
            // TODO: also deal with extension headers
            if (args.ipNextHeader == IPPROTO_TCP || args.ipNextHeader == IPPROTO_UDP) {
                extractIpPorts(args, drop(payload, sizeof(header)));
            }
            break;
        }
        default:
            break;
    }
}

WakeupController::~WakeupController() {
    expectOk(mListener->unsubscribe(NetlinkManager::NFLOG_WAKEUP_GROUP));
}

netdutils::Status WakeupController::init(NFLogListenerInterface* listener) {
    mListener = listener;
    const auto msgHandler = [this](const nlmsghdr&, const nfgenmsg&, const Slice msg) {

        struct WakeupController::ReportArgs args = {
            .uid = -1,
            .gid = -1,
            .ethertype = -1,
            .ipNextHeader = -1,
            .srcPort = -1,
            .dstPort = -1,
            // and all other fields set to 0 as the default
        };
        bool parseAgain = false;

        const auto attrHandler = [&args, &parseAgain](const nlattr attr, const Slice payload) {
            switch (attr.nla_type) {
                case NFULA_TIMESTAMP: {
                    timespec ts = {};
                    extract(payload, ts);
                    constexpr uint64_t kNsPerS = 1000000000ULL;
                    args.timestampNs = ntohl(ts.tv_nsec) + (ntohl(ts.tv_sec) * kNsPerS);
                    break;
                }
                case NFULA_PREFIX:
                    // Strip trailing '\0'
                    args.prefix = toString(take(payload, payload.size() - 1));
                    break;
                case NFULA_UID:
                    extract(payload, args.uid);
                    args.uid = ntohl(args.uid);
                    break;
                case NFULA_GID:
                    extract(payload, args.gid);
                    args.gid = ntohl(args.gid);
                    break;
                case NFULA_HWADDR: {
                    struct nfulnl_msg_packet_hw hwaddr = {};
                    extract(payload, hwaddr);
                    size_t hwAddrLen = ntohs(hwaddr.hw_addrlen);
                    hwAddrLen = std::min(hwAddrLen, sizeof(hwaddr.hw_addr));
                    args.dstHw.assign(hwaddr.hw_addr, hwaddr.hw_addr + hwAddrLen);
                    break;
                }
                case NFULA_PACKET_HDR: {
                    struct nfulnl_msg_packet_hdr packetHdr = {};
                    extract(payload, packetHdr);
                    args.ethertype = ntohs(packetHdr.hw_protocol);
                    break;
                }
                case NFULA_PAYLOAD:
                    // The packet payload is expected to come last in the Netlink message.
                    // At that point NFULA_PACKET_HDR has already been parsed and processed.
                    // If this is not the case, set parseAgain to true.
                    parseAgain = (args.ethertype == -1);
                    extractIpHeader(args, payload);
                    break;
                default:
                    break;
            }
        };

        forEachNetlinkAttribute(msg, attrHandler);
        if (parseAgain) {
            // NFULA_PAYLOAD was parsed before NFULA_PACKET_HDR.
            // Now that the ethertype is known, reparse msg for correctly extracting the payload.
            forEachNetlinkAttribute(msg, attrHandler);
        }
        mReport(args);
    };
    return mListener->subscribe(NetlinkManager::NFLOG_WAKEUP_GROUP,
            WakeupController::kDefaultPacketCopyRange, msgHandler);
}

Status WakeupController::addInterface(const std::string& ifName, const std::string& prefix,
                                    uint32_t mark, uint32_t mask) {
    return execIptables("-A", ifName, prefix, mark, mask);
}

Status WakeupController::delInterface(const std::string& ifName, const std::string& prefix,
                                    uint32_t mark, uint32_t mask) {
    return execIptables("-D", ifName, prefix, mark, mask);
}

Status WakeupController::execIptables(const std::string& action, const std::string& ifName,
                                      const std::string& prefix, uint32_t mark, uint32_t mask) {
    // NFLOG messages to batch before releasing to userspace
    constexpr int kBatch = 8;
    // Max log message rate in packets/second
    constexpr int kRateLimit = 10;
    const char kFormat[] =
        "*mangle\n%s %s -i %s -j NFLOG --nflog-prefix %s --nflog-group %d --nflog-threshold %d"
        " -m mark --mark 0x%08x/0x%08x -m limit --limit %d/s\nCOMMIT\n";
    const auto cmd = StringPrintf(
            kFormat, action.c_str(), WakeupController::LOCAL_MANGLE_INPUT, ifName.c_str(),
            prefix.c_str(), NetlinkManager::NFLOG_WAKEUP_GROUP, kBatch, mark, mask, kRateLimit);

    std::string out;
    auto rv = mIptables->execute(V4V6, cmd, &out);
    if (rv != 0) {
        auto s = Status(rv, "Failed to execute iptables cmd: " + cmd + ", out: " + out);
        ALOGE("%s", toString(s).c_str());
        return s;
    }
    return netdutils::status::ok;
}

}  // namespace net
}  // namespace android