Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 17 | #pragma once |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 18 | |
| 19 | #include <functional> |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 20 | #include <linux/netlink.h> |
| 21 | #include <linux/rtnetlink.h> |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 22 | |
| 23 | #include "NetdConstants.h" |
| 24 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 25 | namespace android::net { |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 26 | |
| 27 | const sockaddr_nl KERNEL_NLADDR = {AF_NETLINK, 0, 0, 0}; |
| 28 | |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 29 | const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK; |
Lorenzo Colitti | 5c43799 | 2017-11-28 01:26:02 +0900 | [diff] [blame] | 30 | const uint16_t NETLINK_ROUTE_CREATE_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL; |
| 31 | // Don't create rules with NLM_F_EXCL, because operations such as changing network permissions rely |
| 32 | // on make-before-break. The kernel did not complain about duplicate rules until ~4.9, at which |
| 33 | // point it started returning EEXIST. See for example b/69607866 . We can't just ignore the EEXIST |
| 34 | // because if we hit it, the rule was not created, but we will think it was, and we'll then trip up |
| 35 | // trying to delete it. |
| 36 | const uint16_t NETLINK_RULE_CREATE_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE; |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 37 | const uint16_t NETLINK_DUMP_FLAGS = NLM_F_REQUEST | NLM_F_DUMP; |
| 38 | |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 39 | // Generic code for processing netlink dumps. |
| 40 | const int kNetlinkDumpBufferSize = 8192; |
| 41 | typedef std::function<void(nlmsghdr *)> NetlinkDumpCallback; |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 42 | typedef std::function<bool(nlmsghdr *)> NetlinkDumpFilter; |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 43 | |
| 44 | // Opens an RTNetlink socket and connects it to the kernel. |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 45 | [[nodiscard]] int openNetlinkSocket(int protocol); |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 46 | |
| 47 | // Receives a netlink ACK. Returns 0 if the command succeeded or negative errno if the command |
| 48 | // failed or receiving the ACK failed. |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 49 | [[nodiscard]] int recvNetlinkAck(int sock); |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 50 | |
| 51 | // Sends a netlink request and possibly expects an ACK. The first element of iov should be null and |
| 52 | // will be set to the netlink message headerheader. The subsequent elements are the contents of the |
| 53 | // request. |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 54 | [[nodiscard]] int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen, |
| 55 | const NetlinkDumpCallback* callback); |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 56 | |
| 57 | // Processes a netlink dump, passing every message to the specified |callback|. |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 58 | [[nodiscard]] int processNetlinkDump(int sock, const NetlinkDumpCallback& callback); |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 59 | |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 60 | // Flushes netlink objects that take an rtmsg structure (FIB rules, routes...). |getAction| and |
| 61 | // |deleteAction| specify the netlink message types, e.g., RTM_GETRULE and RTM_DELRULE. |
| 62 | // |shouldDelete| specifies whether a given object should be deleted or not. |what| is a |
| 63 | // human-readable name for the objects being flushed, e.g. "rules". |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 64 | [[nodiscard]] int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction, const char* what, |
| 65 | const NetlinkDumpFilter& shouldDelete); |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 66 | |
| 67 | // Returns the value of the specific __u32 attribute, or 0 if the attribute was not present. |
| 68 | uint32_t getRtmU32Attribute(const nlmsghdr *nlh, int attribute); |
| 69 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame^] | 70 | } // namespace android::net |