Home | History | Annotate | Download | only in server
      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 
     17 #ifndef NETD_SERVER_NETLINK_UTIL_H
     18 #define NETD_SERVER_NETLINK_UTIL_H
     19 
     20 #include <functional>
     21 #include <linux/netlink.h>
     22 #include <linux/rtnetlink.h>
     23 
     24 #include "NetdConstants.h"
     25 
     26 namespace android {
     27 namespace net {
     28 
     29 const sockaddr_nl KERNEL_NLADDR = {AF_NETLINK, 0, 0, 0};
     30 
     31 const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
     32 const uint16_t NETLINK_ROUTE_CREATE_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
     33 // Don't create rules with NLM_F_EXCL, because operations such as changing network permissions rely
     34 // on make-before-break. The kernel did not complain about duplicate rules until ~4.9, at which
     35 // point it started returning EEXIST. See for example b/69607866 . We can't just ignore the EEXIST
     36 // because if we hit it, the rule was not created, but we will think it was, and we'll then trip up
     37 // trying to delete it.
     38 const uint16_t NETLINK_RULE_CREATE_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE;
     39 const uint16_t NETLINK_DUMP_FLAGS = NLM_F_REQUEST | NLM_F_DUMP;
     40 
     41 // Generic code for processing netlink dumps.
     42 const int kNetlinkDumpBufferSize = 8192;
     43 typedef std::function<void(nlmsghdr *)> NetlinkDumpCallback;
     44 typedef std::function<bool(nlmsghdr *)> NetlinkDumpFilter;
     45 
     46 // Opens an RTNetlink socket and connects it to the kernel.
     47 WARN_UNUSED_RESULT int openNetlinkSocket(int protocol);
     48 
     49 // Receives a netlink ACK. Returns 0 if the command succeeded or negative errno if the command
     50 // failed or receiving the ACK failed.
     51 WARN_UNUSED_RESULT int recvNetlinkAck(int sock);
     52 
     53 // Sends a netlink request and possibly expects an ACK. The first element of iov should be null and
     54 // will be set to the netlink message headerheader. The subsequent elements are the contents of the
     55 // request.
     56 
     57 // Disable optimizations in ASan build.
     58 // ASan reports an out-of-bounds 32-bit(!) access in the first loop of the
     59 // function (over iov[]).
     60 #ifdef __clang__
     61 #if __has_feature(address_sanitizer)
     62 __attribute__((optnone))
     63 #endif
     64 #endif
     65 WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
     66                                           const NetlinkDumpCallback *callback);
     67 
     68 // Processes a netlink dump, passing every message to the specified |callback|.
     69 WARN_UNUSED_RESULT int processNetlinkDump(int sock, const NetlinkDumpCallback& callback);
     70 
     71 // Flushes netlink objects that take an rtmsg structure (FIB rules, routes...). |getAction| and
     72 // |deleteAction| specify the netlink message types, e.g., RTM_GETRULE and RTM_DELRULE.
     73 // |shouldDelete| specifies whether a given object should be deleted or not. |what| is a
     74 // human-readable name for the objects being flushed, e.g. "rules".
     75 WARN_UNUSED_RESULT int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction,
     76                                       const char *what, const NetlinkDumpFilter& shouldDelete);
     77 
     78 // Returns the value of the specific __u32 attribute, or 0 if the attribute was not present.
     79 uint32_t getRtmU32Attribute(const nlmsghdr *nlh, int attribute);
     80 
     81 }  // namespace net
     82 }  // namespace android
     83 
     84 #endif  // NETD_SERVER_NETLINK_UTIL_H
     85