blob: 7c0d4a86bd2be2be996c1e8d8fa5407f5ff77366 [file] [log] [blame]
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +09001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NETD_SERVER_NETLINK_UTIL_H
18#define NETD_SERVER_NETLINK_UTIL_H
19
20#include <functional>
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090021#include <linux/netlink.h>
22#include <linux/rtnetlink.h>
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090023
24#include "NetdConstants.h"
25
26namespace android {
27namespace net {
28
29const sockaddr_nl KERNEL_NLADDR = {AF_NETLINK, 0, 0, 0};
30
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090031const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
32const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
33const uint16_t NETLINK_DUMP_FLAGS = NLM_F_REQUEST | NLM_F_DUMP;
34
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090035// Generic code for processing netlink dumps.
36const int kNetlinkDumpBufferSize = 8192;
37typedef std::function<void(nlmsghdr *)> NetlinkDumpCallback;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090038typedef std::function<bool(nlmsghdr *)> NetlinkDumpFilter;
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090039
40// Opens an RTNetlink socket and connects it to the kernel.
Nathan Harold1a371532017-01-30 12:30:48 -080041WARN_UNUSED_RESULT int openNetlinkSocket(int protocol);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090042
43// Receives a netlink ACK. Returns 0 if the command succeeded or negative errno if the command
44// failed or receiving the ACK failed.
45WARN_UNUSED_RESULT int recvNetlinkAck(int sock);
46
47// Sends a netlink request and possibly expects an ACK. The first element of iov should be null and
48// will be set to the netlink message headerheader. The subsequent elements are the contents of the
49// request.
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090050
51// Disable optimizations in ASan build.
52// ASan reports an out-of-bounds 32-bit(!) access in the first loop of the
53// function (over iov[]).
54#ifdef __clang__
55#if __has_feature(address_sanitizer)
56__attribute__((optnone))
57#endif
58#endif
59WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090060 const NetlinkDumpCallback *callback);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090061
62// Processes a netlink dump, passing every message to the specified |callback|.
63WARN_UNUSED_RESULT int processNetlinkDump(int sock, const NetlinkDumpCallback& callback);
64
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090065// Flushes netlink objects that take an rtmsg structure (FIB rules, routes...). |getAction| and
66// |deleteAction| specify the netlink message types, e.g., RTM_GETRULE and RTM_DELRULE.
67// |shouldDelete| specifies whether a given object should be deleted or not. |what| is a
68// human-readable name for the objects being flushed, e.g. "rules".
69WARN_UNUSED_RESULT int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction,
70 const char *what, const NetlinkDumpFilter& shouldDelete);
71
72// Returns the value of the specific __u32 attribute, or 0 if the attribute was not present.
73uint32_t getRtmU32Attribute(const nlmsghdr *nlh, int attribute);
74
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090075} // namespace net
76} // namespace android
77
78#endif // NETD_SERVER_NETLINK_UTIL_H