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