wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_QUIC_QBONE_PLATFORM_RTNETLINK_MESSAGE_H_ |
| 6 | #define QUICHE_QUIC_QBONE_PLATFORM_RTNETLINK_MESSAGE_H_ |
| 7 | |
| 8 | #include <linux/netlink.h> |
| 9 | #include <linux/rtnetlink.h> |
| 10 | #include <stdint.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/uio.h> |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
| 19 | |
| 20 | namespace quic { |
| 21 | |
| 22 | // This base class is used to construct an array struct iovec that represents a |
| 23 | // rtnetlink message as defined in man 7 rtnet. Padding for message header |
| 24 | // alignment to conform NLMSG_* and RTA_* macros is added at the end of each |
| 25 | // iovec::iov_base. |
| 26 | class RtnetlinkMessage { |
| 27 | public: |
| 28 | virtual ~RtnetlinkMessage(); |
| 29 | |
| 30 | enum class Operation { |
| 31 | NEW, |
| 32 | DEL, |
| 33 | GET, |
| 34 | }; |
| 35 | |
| 36 | // Appends a struct rtattr to the message. nlmsg_len and rta_len is handled |
| 37 | // properly. |
| 38 | // Override this to perform check on type. |
| 39 | virtual void AppendAttribute(uint16_t type, |
| 40 | const void* data, |
| 41 | uint16_t data_length); |
| 42 | |
| 43 | // Builds the array of iovec that can be fed into sendmsg directly. |
| 44 | std::unique_ptr<struct iovec[]> BuildIoVec() const; |
| 45 | |
| 46 | // The size of the array of iovec if BuildIovec is called. |
| 47 | size_t IoVecSize() const; |
| 48 | |
| 49 | protected: |
| 50 | // Subclass should add their own message header immediately after the |
| 51 | // nlmsghdr. Make this private to force the creation of such header. |
| 52 | RtnetlinkMessage(uint16_t type, |
| 53 | uint16_t flags, |
| 54 | uint32_t seq, |
| 55 | uint32_t pid, |
| 56 | const void* payload_header, |
| 57 | size_t payload_header_length); |
| 58 | |
| 59 | // Adjusts nlmsg_len in the header assuming additional_data_length is appended |
| 60 | // at the end. |
| 61 | void AdjustMessageLength(size_t additional_data_length); |
| 62 | |
| 63 | private: |
| 64 | // Convenient function for accessing the nlmsghdr. |
| 65 | struct nlmsghdr* MessageHeader(); |
| 66 | |
| 67 | std::vector<struct iovec> message_; |
| 68 | }; |
| 69 | |
| 70 | // Message for manipulating link level configuration as defined in man 7 |
| 71 | // rtnetlink. RTM_NEWLINK, RTM_DELLINK and RTM_GETLINK are supported. |
| 72 | class LinkMessage : public RtnetlinkMessage { |
| 73 | public: |
| 74 | static LinkMessage New(RtnetlinkMessage::Operation request_operation, |
| 75 | uint16_t flags, |
| 76 | uint32_t seq, |
| 77 | uint32_t pid, |
| 78 | const struct ifinfomsg* interface_info_header); |
| 79 | |
| 80 | private: |
| 81 | using RtnetlinkMessage::RtnetlinkMessage; |
| 82 | }; |
| 83 | |
| 84 | // Message for manipulating address level configuration as defined in man 7 |
| 85 | // rtnetlink. RTM_NEWADDR, RTM_NEWADDR and RTM_GETADDR are supported. |
| 86 | class AddressMessage : public RtnetlinkMessage { |
| 87 | public: |
| 88 | static AddressMessage New(RtnetlinkMessage::Operation request_operation, |
| 89 | uint16_t flags, |
| 90 | uint32_t seq, |
| 91 | uint32_t pid, |
| 92 | const struct ifaddrmsg* interface_address_header); |
| 93 | |
| 94 | private: |
| 95 | using RtnetlinkMessage::RtnetlinkMessage; |
| 96 | }; |
| 97 | |
| 98 | // Message for manipulating routing table as defined in man 7 rtnetlink. |
| 99 | // RTM_NEWROUTE, RTM_DELROUTE and RTM_GETROUTE are supported. |
| 100 | class RouteMessage : public RtnetlinkMessage { |
| 101 | public: |
| 102 | static RouteMessage New(RtnetlinkMessage::Operation request_operation, |
| 103 | uint16_t flags, |
| 104 | uint32_t seq, |
| 105 | uint32_t pid, |
| 106 | const struct rtmsg* route_message_header); |
| 107 | |
| 108 | private: |
| 109 | using RtnetlinkMessage::RtnetlinkMessage; |
| 110 | }; |
| 111 | |
| 112 | class RuleMessage : public RtnetlinkMessage { |
| 113 | public: |
| 114 | static RuleMessage New(RtnetlinkMessage::Operation request_operation, |
| 115 | uint16_t flags, |
| 116 | uint32_t seq, |
| 117 | uint32_t pid, |
| 118 | const struct rtmsg* rule_message_header); |
| 119 | |
| 120 | private: |
| 121 | using RtnetlinkMessage::RtnetlinkMessage; |
| 122 | }; |
| 123 | |
| 124 | } // namespace quic |
| 125 | |
| 126 | #endif // QUICHE_QUIC_QBONE_PLATFORM_RTNETLINK_MESSAGE_H_ |