1 /*
2  * Copyright (C) 2020 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 #include "Link.h"
18 
19 #include "../structs.h"
20 #include "structs.h"
21 
22 #include <net/if.h>
23 
24 namespace android::nl::protocols::route {
25 
26 using DataType = AttributeDefinition::DataType;
27 
28 // clang-format off
Link()29 Link::Link() : MessageDefinition<ifinfomsg>("link", {
30     {RTM_NEWLINK, {"NEWLINK", MessageGenre::New}},
31     {RTM_DELLINK, {"DELLINK", MessageGenre::Delete}},
32     {RTM_GETLINK, {"GETLINK", MessageGenre::Get}},
33 }, {
34     {IFLA_ADDRESS, {"ADDRESS"}},
35     {IFLA_BROADCAST, {"BROADCAST"}},
36     {IFLA_IFNAME, {"IFNAME", DataType::String}},
37     {IFLA_MTU, {"MTU", DataType::Uint}},
38     {IFLA_LINK, {"LINK", DataType::Uint}},
39     {IFLA_QDISC, {"QDISC", DataType::String}},
40     {IFLA_STATS, {"STATS", DataType::Struct, statsToStream<rtnl_link_stats>}},
41     {IFLA_COST, {"COST"}},
42     {IFLA_PRIORITY, {"PRIORITY"}},
43     {IFLA_MASTER, {"MASTER", DataType::Uint}},
44     {IFLA_WIRELESS, {"WIRELESS"}},
45     {IFLA_PROTINFO, {"PROTINFO"}},
46     {IFLA_TXQLEN, {"TXQLEN", DataType::Uint}},
47     {IFLA_MAP, {"MAP", DataType::Struct, mapToStream}},
48     {IFLA_WEIGHT, {"WEIGHT", DataType::Uint}},
49     {IFLA_OPERSTATE, {"OPERSTATE", DataType::Uint}},
50     {IFLA_LINKMODE, {"LINKMODE", DataType::Uint}},
51     {IFLA_LINKINFO, {"LINKINFO", DataType::Nested, AttributeMap{
52         {IFLA_INFO_KIND, {"INFO_KIND", DataType::String}},
53         {IFLA_INFO_DATA, {"INFO_DATA", DataType::Nested}},
54         {IFLA_INFO_XSTATS, {"INFO_XSTATS"}},
55         {IFLA_INFO_SLAVE_KIND, {"INFO_SLAVE_KIND", DataType::String}},
56         {IFLA_INFO_SLAVE_DATA, {"INFO_SLAVE_DATA"}},
57     }}},
58     {IFLA_NET_NS_PID, {"NET_NS_PID", DataType::Uint}},
59     {IFLA_IFALIAS, {"IFALIAS", DataType::String}},
60     {IFLA_NUM_VF, {"NUM_VF", DataType::Uint}},
61     {IFLA_VFINFO_LIST, {"VFINFO_LIST"}},
62     {IFLA_STATS64, {"STATS64", DataType::Struct, statsToStream<rtnl_link_stats64>}},
63     {IFLA_VF_PORTS, {"VF_PORTS"}},
64     {IFLA_PORT_SELF, {"PORT_SELF"}},
65     {IFLA_AF_SPEC, {"AF_SPEC", DataType::Nested, AttributeMap{
66         {AF_INET, {"AF_INET", DataType::Nested, AttributeMap{
67             {IFLA_INET_CONF, {"INET_CONF", DataType::Struct, arrayToStream<int32_t>}},
68         }}},
69         {AF_INET6, {"AF_INET6", DataType::Nested, AttributeMap{
70             {IFLA_INET6_FLAGS, {"INET6_FLAGS", DataType::Uint}},
71             {IFLA_INET6_CONF, {"INET6_CONF", DataType::Struct, arrayToStream<int32_t>}},
72             {IFLA_INET6_STATS, {"INET6_STATS", DataType::Struct, arrayToStream<uint64_t>}},
73             {IFLA_INET6_MCAST, {"INET6_MCAST"}},
74             {IFLA_INET6_CACHEINFO, {"INET6_CACHEINFO", DataType::Struct, ifla_cacheinfoToStream}},
75             {IFLA_INET6_ICMP6STATS, {"INET6_ICMP6STATS", DataType::Struct, arrayToStream<uint64_t>}},
76             {IFLA_INET6_TOKEN, {"INET6_TOKEN"}},
77             {IFLA_INET6_ADDR_GEN_MODE, {"INET6_ADDR_GEN_MODE", DataType::Uint}},
78         }}},
79     }}},
80     {IFLA_GROUP, {"GROUP", DataType::Uint}},
81     {IFLA_NET_NS_FD, {"NET_NS_FD", DataType::Uint}},
82     {IFLA_EXT_MASK, {"EXT_MASK", DataType::Uint}},
83     {IFLA_PROMISCUITY, {"PROMISCUITY", DataType::Uint}},
84     {IFLA_NUM_TX_QUEUES, {"NUM_TX_QUEUES", DataType::Uint}},
85     {IFLA_NUM_RX_QUEUES, {"NUM_RX_QUEUES", DataType::Uint}},
86     {IFLA_CARRIER, {"CARRIER", DataType::Uint}},
87     {IFLA_PHYS_PORT_ID, {"PHYS_PORT_ID"}},
88     {IFLA_CARRIER_CHANGES, {"CARRIER_CHANGES", DataType::Uint}},
89     {IFLA_PHYS_SWITCH_ID, {"PHYS_SWITCH_ID"}},
90     {IFLA_LINK_NETNSID, {"LINK_NETNSID"}},  // NLA_S32
91     {IFLA_PHYS_PORT_NAME, {"PHYS_PORT_NAME", DataType::String}},
92     {IFLA_PROTO_DOWN, {"PROTO_DOWN", DataType::Uint}},
93     {IFLA_GSO_MAX_SEGS, {"GSO_MAX_SEGS", DataType::Uint}},
94     {IFLA_GSO_MAX_SIZE, {"GSO_MAX_SIZE", DataType::Uint}},
95     {IFLA_PAD, {"PAD"}},
96     {IFLA_XDP, {"XDP"}},
97     {IFLA_EVENT, {"EVENT", DataType::Uint}},
98     {IFLA_NEW_NETNSID, {"NEW_NETNSID"}},  // NLA_S32
99     {IFLA_TARGET_NETNSID, {"TARGET_NETNSID"}},  // NLA_S32
100     {IFLA_CARRIER_UP_COUNT, {"CARRIER_UP_COUNT", DataType::Uint}},
101     {IFLA_CARRIER_DOWN_COUNT, {"CARRIER_DOWN_COUNT", DataType::Uint}},
102     {IFLA_NEW_IFINDEX, {"NEW_IFINDEX"}},  // NLA_S32
103     {IFLA_MIN_MTU, {"MIN_MTU", DataType::Uint}},
104     {IFLA_MAX_MTU, {"MAX_MTU", DataType::Uint}},
105     {IFLA_PROP_LIST, {"PROP_LIST"}},
106     {IFLA_ALT_IFNAME, {"ALT_IFNAME", DataType::String}},
107     {IFLA_PERM_ADDRESS, {"PERM_ADDRESS"}},
108 }) {}
109 // clang-format off
110 
toStream(std::stringstream & ss,const ifinfomsg & data) const111 void Link::toStream(std::stringstream& ss, const ifinfomsg& data) const {
112     ss << "ifinfomsg{"
113        << "family=" << unsigned(data.ifi_family) << ", type=" << data.ifi_type
114        << ", index=" << data.ifi_index << ", flags=" << data.ifi_flags
115        << ", change=" << data.ifi_change << "}";
116 }
117 
118 }  // namespace android::nl::protocols::route
119