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