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 #ifndef COMMON_LIBS_NET_NETLINK_REQUEST_H_ 17 #define COMMON_LIBS_NET_NETLINK_REQUEST_H_ 18 19 #include <linux/netlink.h> 20 #include <stddef.h> 21 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "common/libs/auto_resources/auto_resources.h" 27 28 namespace cvd { 29 // Abstraction of Network link request. 30 // Used to supply kernel with information about which interface needs to be 31 // changed, and how. 32 class NetlinkRequest { 33 public: 34 // Create new Netlink Request structure. 35 // Parameter |type| specifies netlink request type (eg. RTM_NEWLINK), while 36 // |flags| are netlink and request specific flags (eg. NLM_F_DUMP). 37 NetlinkRequest(int type, int flags); 38 NetlinkRequest(NetlinkRequest&& other); 39 40 ~NetlinkRequest() = default; 41 42 // Add an IFLA tag followed by a string. 43 // Returns true, if successful. 44 void AddString(uint16_t type, const std::string& value); 45 46 // Add an IFLA tag followed by an integer. 47 template <typename T> AddInt(uint16_t type,T value)48 void AddInt(uint16_t type, T value) { 49 static_assert(std::is_integral<T>::value, 50 "AddInt must be used on integer types."); 51 AppendTag(type, &value, sizeof(value)); 52 } 53 54 // Add an interface info structure. 55 // Parameter |if_index| specifies particular interface index to which the 56 // attributes following the IfInfo apply. 57 void AddIfInfo(int32_t if_index, bool is_operational); 58 59 // Add an address info to a specific interface. 60 void AddAddrInfo(int32_t if_index, int prefix_len = 24); 61 62 // Creates new list. 63 // List mimmic recursive structures in a flat, contiuous representation. 64 // Each call to PushList() should have a corresponding call to PopList 65 // indicating end of sub-attribute list. 66 void PushList(uint16_t type); 67 68 // Indicates end of previously declared list. 69 void PopList(); 70 71 // Request data. 72 void* RequestData() const; 73 74 // Request length. 75 size_t RequestLength() const; 76 77 // Request Sequence Number. 78 uint32_t SeqNo() const; 79 80 // Append raw data to buffer. 81 // data must not be null. 82 // Returns pointer to copied location. 83 void* AppendRaw(const void* data, size_t length); 84 85 // Reserve |length| number of bytes in the buffer. 86 // Returns pointer to reserved location. 87 void* ReserveRaw(size_t length); 88 89 // Append specialized data. Append(const T & data)90 template <typename T> T* Append(const T& data) { 91 return static_cast<T*>(AppendRaw(&data, sizeof(T))); 92 } 93 94 // Reserve specialized data. Reserve()95 template <typename T> T* Reserve() { 96 return static_cast<T*>(ReserveRaw(sizeof(T))); 97 } 98 99 private: 100 nlattr* AppendTag(uint16_t type, const void* data, uint16_t length); 101 102 std::vector<std::pair<nlattr*, int32_t>> lists_; 103 AutoFreeBuffer request_; 104 nlmsghdr* header_; 105 106 NetlinkRequest(const NetlinkRequest&) = delete; 107 NetlinkRequest& operator= (const NetlinkRequest&) = delete; 108 }; 109 } // namespace cvd 110 #endif // COMMON_LIBS_NET_NETLINK_REQUEST_H_ 111