1//
2// Copyright (C) 2012 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 "shill/net/rtnl_message.h"
18
19#include <sys/socket.h>
20#include <linux/if.h>  // NOLINT(build/include_alpha) - needs sockaddr.
21#include <linux/netlink.h>
22#include <linux/rtnetlink.h>
23
24#include <string>
25
26#include <gtest/gtest.h>
27
28#include "shill/net/byte_string.h"
29#include "shill/net/ip_address.h"
30
31using std::string;
32using testing::Test;
33
34namespace shill {
35
36namespace {
37
38// These test strings were generated by cutting up the output of the
39// "rtmon" utility into individual strings.  This tool outputs the raw
40// RTNL packet contents sent by the kernel.  The comments above each
41// of these strings is the markup that the "ip monitor" command
42// outputs for each of these RTNL packets.
43
44
45// 2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP
46//     link/ether c0:f8:da:05:03:0b brd ff:ff:ff:ff:ff:ff
47
48const int kNewLinkMessageWlan0InterfaceIndex = 2;
49const unsigned int kNewLinkMessageWlan0InterfaceFlags =
50    IFF_BROADCAST | IFF_MULTICAST | IFF_UP | IFF_LOWER_UP | IFF_RUNNING;
51const unsigned int kNewLinkMessageWlan0InterfaceFlagsChange = 0;
52const uint32_t kNewLinkMessageWlan0MTU = 1500;
53const char kNewLinkMessageWlan0MACAddress[] = "\xc0\xf8\xda\x05\x03\x0b";
54const char kNewLinkMessageWlan0InterfaceName[] = "wlan0";
55const char kNewLinkMessageWlan0Qdisc[] = "mq";
56const int kNewLinkMessageWlan0OperState = IF_OPER_UP;
57
58const unsigned char kNewLinkMessageWlan0[] = {
59  0xe0, 0x03, 0x00, 0x00, 0x10, 0x00, 0x02, 0x00,
60  0x53, 0x18, 0x1f, 0x4e, 0xac, 0x77, 0x00, 0x00,
61  0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
62  0x43, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
63  0x0a, 0x00, 0x03, 0x00, 0x77, 0x6c, 0x61, 0x6e,
64  0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
65  0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
66  0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
67  0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
68  0xdc, 0x05, 0x00, 0x00, 0x07, 0x00, 0x06, 0x00,
69  0x6d, 0x71, 0x00, 0x00, 0x20, 0x00, 0x0e, 0x00,
70  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73  0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00,
74  0xc0, 0xf8, 0xda, 0x05, 0x03, 0x0b, 0x00, 0x00,
75  0x0a, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff,
76  0xff, 0xff, 0x00, 0x00, 0x60, 0x00, 0x07, 0x00,
77  0xa6, 0x65, 0x0f, 0x00, 0xa6, 0x0b, 0x00, 0x00,
78  0xd2, 0x29, 0x61, 0x06, 0xd7, 0x25, 0x07, 0x00,
79  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80  0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
81  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88  0x00, 0x00, 0x00, 0x00, 0xbc, 0x00, 0x17, 0x00,
89  0xa6, 0x65, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
90  0xa6, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91  0xd2, 0x29, 0x61, 0x06, 0x00, 0x00, 0x00, 0x00,
92  0xd7, 0x25, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
93  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95  0x84, 0x53, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
96  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112  0x08, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00,
113  0x30, 0x02, 0x1a, 0x00, 0x6c, 0x00, 0x02, 0x00,
114  0x68, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
115  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
117  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
118  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
119  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127  0xc0, 0x01, 0x0a, 0x00, 0x08, 0x00, 0x01, 0x00,
128  0x10, 0x00, 0x00, 0x80, 0x14, 0x00, 0x05, 0x00,
129  0xff, 0xff, 0x00, 0x00, 0x22, 0x05, 0x00, 0x00,
130  0x4f, 0x53, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00,
131  0x78, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
132  0x40, 0x00, 0x00, 0x00, 0xdc, 0x05, 0x00, 0x00,
133  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
134  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
135  0x03, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x00, 0x00,
136  0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137  0x80, 0x3a, 0x09, 0x00, 0x80, 0x51, 0x01, 0x00,
138  0x05, 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00,
139  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
141  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146  0xfc, 0x00, 0x03, 0x00, 0x1f, 0x00, 0x00, 0x00,
147  0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00,
148  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155  0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
156  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157  0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
158  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167  0x00, 0x00, 0x00, 0x00, 0x15, 0xe6, 0x04, 0x00,
168  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
169  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171  0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
172  0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00,
173  0x00, 0x00, 0x00, 0x00, 0x9b, 0x95, 0xb9, 0x01,
174  0x00, 0x00, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00,
175  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177  0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00,
178  0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179  0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181  0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183};
184
185
186// Deleted 8: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
187//    link/ether 68:7f:74:ba:ef:c7 brd ff:ff:ff:ff:ff:ff
188
189const int kDelLinkMessageEth0InterfaceIndex = 8;
190const unsigned int kDelLinkMessageEth0InterfaceFlags =
191    IFF_BROADCAST | IFF_MULTICAST;
192const unsigned int kDelLinkMessageEth0InterfaceFlagsChange = 0xffffffff;
193const uint32_t kDelLinkMessageEth0MTU = 1500;
194const char kDelLinkMessageEth0MACAddress[] = "\x68\x7f\x74\xba\xef\xc7";
195const char kDelLinkMessageEth0InterfacName[] = "eth0";
196const char kDelLinkMessageEth0Qdisc[] = "noop";
197const int kDelLinkMessageEth0OperState = IF_OPER_DOWN;
198
199const unsigned char kDelLinkMessageEth0[] = {
200  0xb8, 0x01, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
201  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202  0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
203  0x02, 0x10, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
204  0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
205  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
206  0xe8, 0x03, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
207  0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
208  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
209  0xdc, 0x05, 0x00, 0x00, 0x09, 0x00, 0x06, 0x00,
210  0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00,
211  0x20, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
212  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215  0x0a, 0x00, 0x01, 0x00, 0x68, 0x7f, 0x74, 0xba,
216  0xef, 0xc7, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00,
217  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
218  0x60, 0x00, 0x07, 0x00, 0x09, 0x00, 0x00, 0x00,
219  0x0e, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
220  0x34, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230  0xbc, 0x00, 0x17, 0x00, 0x09, 0x00, 0x00, 0x00,
231  0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
232  0x00, 0x00, 0x00, 0x00, 0xfa, 0x05, 0x00, 0x00,
233  0x00, 0x00, 0x00, 0x00, 0x34, 0x06, 0x00, 0x00,
234  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
250  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x15, 0x00,
254  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x1a, 0x00,
255};
256
257// 8: if8    inet 192.168.10.100/24 brd 192.168.10.255 scope global eth0
258
259const int kNewAddrIPV4InterfaceIndex = 8;
260const char kNewAddrIPV4Address[] = "192.168.10.100";
261const int kNewAddrIPV4AddressPrefix = 24;
262const unsigned char kNewAddrIPV4Scope = RT_SCOPE_UNIVERSE;
263
264const unsigned char kNewAddrIPV4[] = {
265  0x3c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
266  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267  0x02, 0x18, 0x80, 0x00, 0x08, 0x00, 0x00, 0x00,
268  0x08, 0x00, 0x01, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
269  0x08, 0x00, 0x02, 0x00, 0xc0, 0xa8, 0x0a, 0x64,
270  0x08, 0x00, 0x04, 0x00, 0xc0, 0xa8, 0x0a, 0xff,
271  0x09, 0x00, 0x03, 0x00, 0x65, 0x74, 0x68, 0x30,
272  0x00, 0x00, 0x00, 0x00,
273};
274
275// Deleted 15: if15    inet6 fe80::6a7f:74ff:feba:efc7/64 scope link
276//       valid_lft forever preferred_lft forever
277
278const int kDelAddrIPV6InterfaceIndex = 15;
279const char kDelAddrIPV6Address[] = "fe80::6a7f:74ff:feba:efc7";
280const int kDelAddrIPV6AddressPrefix = 64;
281const unsigned char kDelAddrIPV6Scope = RT_SCOPE_LINK;
282
283const unsigned char kDelAddrIPV6[] = {
284  0x40, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
285  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286  0x0a, 0x40, 0x80, 0xfd, 0x0f, 0x00, 0x00, 0x00,
287  0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00,
288  0x00, 0x00, 0x00, 0x00, 0x6a, 0x7f, 0x74, 0xff,
289  0xfe, 0xba, 0xef, 0xc7, 0x14, 0x00, 0x06, 0x00,
290  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
291  0xbf, 0xdb, 0x02, 0x00, 0xbf, 0xdb, 0x02, 0x00,
292};
293
294// Deleted ff02::1:ffa0:688 via ff02::1:ffa0:688 dev if2  metric 0
295
296const int kDelRouteIPV6InterfaceIndex = 2;
297const char kDelRouteIPV6Address[] = "ff02::1:ffa0:688";
298const int kDelRouteIPV6Prefix = 128;
299const int kDelRouteIPV6Metric = 0;
300
301const unsigned char kDelRouteIPV6[] = {
302  0x80, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
303  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304  0x0a, 0x80, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x01,
305  0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
306  0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
307  0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308  0x00, 0x00, 0x00, 0x01, 0xff, 0xa0, 0x06, 0x88,
309  0x14, 0x00, 0x05, 0x00, 0xff, 0x02, 0x00, 0x00,
310  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
311  0xff, 0xa0, 0x06, 0x88, 0x08, 0x00, 0x04, 0x00,
312  0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
313  0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0c, 0x00,
314  0x00, 0x00, 0x00, 0x00, 0x5f, 0x0c, 0x00, 0x00,
315  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318};
319
320// default via 192.168.17.254 dev if12  metric 9
321
322const int kAddRouteIPV4InterfaceIndex = 12;
323const char kAddRouteIPV4Address[] = "192.168.17.254";
324const int kAddRouteIPV4Metric = 9;
325
326const unsigned char kAddRouteIPV4[] = {
327  0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
328  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329  0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
330  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
331  0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
332  0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00,
333  0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
334  0x0c, 0x00, 0x00, 0x00,
335};
336
337
338// This is the same as kAddRouteIPV4 above, except manually corrupted
339// the second to last parameter
340const unsigned char kAddRouteBusted[] = {
341  0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
342  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343  0x02, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01,
344  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00,
345  0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00,
346  0x09, 0x00, 0x00, 0x00, 0x28, 0x00, 0x05, 0x00,
347  0xc0, 0xa8, 0x11, 0xfe, 0x08, 0x00, 0x04, 0x00,
348  0x0c, 0x00, 0x00, 0x00,
349};
350
351// 192.168.10.1 dev if8 lladdr 00:14:d1:cd:d5:2c REACHABLE
352const unsigned char kAddNeighborMessage[] = {
353  0x4c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
354  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355  0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
356  0x02, 0x00, 0x00, 0x01, 0x08, 0x00, 0x01, 0x00,
357  0xc0, 0xa8, 0x0a, 0x01, 0x0a, 0x00, 0x02, 0x00,
358  0x00, 0x14, 0xd1, 0xcd, 0xd5, 0x2c, 0x00, 0x00,
359  0x08, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
360  0x14, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
361  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362  0x04, 0x00, 0x00, 0x00,
363};
364
365// RDNSS notification
366// Lifetime: infinity (0xffffffff)
367// Server addresses: 2001:db8:100:f101::1, 2001:db8:100:f101::2
368const unsigned char kNdRdnssMessage[] = {
369  0x5c, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
370  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
371  0x0a, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00,
372  0x86, 0x00, 0x03, 0x00, 0x14, 0x00, 0x01, 0x00,
373  0x19, 0x05, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
374  0x20, 0x01, 0x0d, 0xb8, 0x01, 0x00, 0xf1, 0x01,
375  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
376  0x20, 0x01, 0x0d, 0xb8, 0x01, 0x00, 0xf1, 0x01,
377  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
378  0x14, 0x00, 0x01, 0x00, 0xfe, 0x80, 0x00, 0x00,
379  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380  0x50, 0xf8, 0x86, 0xff,
381};
382
383}  // namespace
384
385class RTNLMessageTest : public Test {
386 protected:
387  void TestParseLink(const ByteString& packet,
388                     RTNLMessage::Mode mode,
389                     int interface_index,
390                     unsigned int flags,
391                     unsigned int change,
392                     ByteString address,
393                     ByteString name,
394                     uint32_t mtu,
395                     ByteString qdisc,
396                     int oper_state) {
397    RTNLMessage msg;
398    EXPECT_TRUE(msg.Decode(packet));
399
400    EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
401    EXPECT_EQ(mode, msg.mode());
402    EXPECT_EQ(interface_index, msg.interface_index());
403
404    RTNLMessage::LinkStatus status = msg.link_status();
405    EXPECT_EQ(flags, status.flags);
406    EXPECT_EQ(change, status.change);
407
408    EXPECT_TRUE(msg.HasAttribute(IFLA_ADDRESS));
409    EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFLA_ADDRESS).GetLength());
410    EXPECT_TRUE(msg.GetAttribute(IFLA_ADDRESS).Equals(address));
411
412    EXPECT_TRUE(msg.HasAttribute(IFLA_IFNAME));
413    EXPECT_EQ(name.GetLength(), msg.GetAttribute(IFLA_IFNAME).GetLength());
414    EXPECT_TRUE(msg.GetAttribute(IFLA_IFNAME).Equals(name));
415
416    EXPECT_TRUE(msg.HasAttribute(IFLA_MTU));
417    uint32_t mtu_val;
418    EXPECT_TRUE(msg.GetAttribute(IFLA_MTU).ConvertToCPUUInt32(&mtu_val));
419    EXPECT_EQ(mtu, mtu_val);
420
421    EXPECT_TRUE(msg.HasAttribute(IFLA_QDISC));
422    EXPECT_EQ(qdisc.GetLength(), msg.GetAttribute(IFLA_QDISC).GetLength());
423    EXPECT_TRUE(msg.GetAttribute(IFLA_QDISC).Equals(qdisc));
424
425    EXPECT_TRUE(msg.HasAttribute(IFLA_OPERSTATE));
426    EXPECT_EQ(oper_state, msg.GetAttribute(IFLA_OPERSTATE).GetConstData()[0]);
427  }
428
429  void TestParseAddress(const ByteString& packet,
430                        RTNLMessage::Mode mode,
431                        int interface_index,
432                        const IPAddress& address,
433                        unsigned char scope) {
434    RTNLMessage msg;
435
436    EXPECT_TRUE(msg.Decode(packet));
437    EXPECT_EQ(RTNLMessage::kTypeAddress, msg.type());
438    EXPECT_EQ(mode, msg.mode());
439    EXPECT_EQ(interface_index, msg.interface_index());
440    EXPECT_EQ(address.family(), msg.family());
441
442    RTNLMessage::AddressStatus status = msg.address_status();
443    EXPECT_EQ(scope, status.scope);
444
445    EXPECT_TRUE(msg.HasAttribute(IFA_ADDRESS));
446    EXPECT_EQ(address.GetLength(), msg.GetAttribute(IFA_ADDRESS).GetLength());
447    EXPECT_TRUE(
448        IPAddress(address.family(),
449                  msg.GetAttribute(IFA_ADDRESS),
450                  status.prefix_len).Equals(address));
451  }
452
453  void TestParseRoute(const ByteString& packet,
454                      RTNLMessage::Mode /*mode*/,
455                      IPAddress::Family family,
456                      int interface_index,
457                      const IPAddress& dst,
458                      const IPAddress& src,
459                      const IPAddress& gateway,
460                      unsigned char table,
461                      int protocol,
462                      unsigned char scope,
463                      unsigned char type,
464                      int metric) {
465    RTNLMessage msg;
466
467    EXPECT_TRUE(msg.Decode(packet));
468    EXPECT_EQ(RTNLMessage::kTypeRoute, msg.type());
469    EXPECT_EQ(0, msg.interface_index());
470    EXPECT_EQ(family, msg.family());
471
472    RTNLMessage::RouteStatus status = msg.route_status();
473    EXPECT_EQ(table, status.table);
474    EXPECT_EQ(protocol, status.protocol);
475    EXPECT_EQ(scope, status.scope);
476    EXPECT_EQ(type, status.type);
477
478    if (!dst.IsDefault()) {
479      EXPECT_TRUE(msg.HasAttribute(RTA_DST));
480      EXPECT_TRUE(IPAddress(family,
481                            msg.GetAttribute(RTA_DST),
482                            status.dst_prefix).Equals(dst));
483    }
484
485    if (!src.IsDefault()) {
486      EXPECT_TRUE(msg.HasAttribute(RTA_SRC));
487      EXPECT_TRUE(IPAddress(family,
488                            msg.GetAttribute(RTA_SRC),
489                            status.src_prefix).Equals(src));
490    }
491
492    if (!gateway.IsDefault()) {
493      EXPECT_TRUE(msg.HasAttribute(RTA_GATEWAY));
494      EXPECT_TRUE(IPAddress(family,
495                            msg.GetAttribute(RTA_GATEWAY)).Equals(gateway));
496    }
497
498    if (interface_index >= 0) {
499      EXPECT_TRUE(msg.HasAttribute(RTA_OIF));
500      uint32_t int_val;
501      EXPECT_TRUE(msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&int_val));
502      EXPECT_EQ(interface_index, int_val);
503    } else {
504      EXPECT_FALSE(msg.HasAttribute(RTA_OIF));
505    }
506    if (metric >= 0) {
507      EXPECT_TRUE(msg.HasAttribute(RTA_PRIORITY));
508      uint32_t metric_val;
509      EXPECT_TRUE(
510          msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&metric_val));
511      EXPECT_EQ(metric, metric_val);
512    } else {
513      EXPECT_FALSE(msg.HasAttribute(RTA_PRIORITY));
514    }
515  }
516
517  void TestParseRdnss(const ByteString& packet,
518                      RTNLMessage::Mode mode,
519                      int interface_index,
520                      uint32_t lifetime,
521                      const std::string& dns_server_addresses) {
522    RTNLMessage msg;
523
524    EXPECT_TRUE(msg.Decode(packet));
525    EXPECT_EQ(RTNLMessage::kTypeRdnss, msg.type());
526    EXPECT_EQ(mode, msg.mode());
527    EXPECT_EQ(interface_index, msg.interface_index());
528
529    RTNLMessage::RdnssOption rdnss = msg.rdnss_option();
530
531    // Format addresses string for verification.
532    std::string addresses;
533    bool first = true;
534    for (auto& ip : rdnss.addresses) {
535      if (!first) {
536        addresses += ", ";
537      } else {
538        first = false;
539      }
540      addresses += ip.ToString();
541    }
542
543    // Verify life time and addresses.
544    EXPECT_EQ(lifetime, rdnss.lifetime);
545    EXPECT_EQ(dns_server_addresses, addresses);
546  }
547
548  void TestParseNeighbor(const ByteString& packet,
549                         RTNLMessage::Mode mode,
550                         IPAddress::Family family,
551                         int interface_index,
552                         uint16_t state,
553                         uint8_t flags,
554                         uint8_t type) {
555    RTNLMessage msg;
556
557    EXPECT_TRUE(msg.Decode(packet));
558    EXPECT_EQ(RTNLMessage::kTypeNeighbor, msg.type());
559    EXPECT_EQ(mode, msg.mode());
560    EXPECT_EQ(family, msg.family());
561    EXPECT_EQ(interface_index, msg.interface_index());
562
563    RTNLMessage::NeighborStatus neighbor = msg.neighbor_status();
564
565    EXPECT_EQ(neighbor.state, state);
566    EXPECT_EQ(neighbor.flags, flags);
567    EXPECT_EQ(neighbor.type, type);
568  }
569};
570
571TEST_F(RTNLMessageTest, NewLinkWlan0) {
572  TestParseLink(ByteString(kNewLinkMessageWlan0, sizeof(kNewLinkMessageWlan0)),
573                RTNLMessage::kModeAdd,
574                kNewLinkMessageWlan0InterfaceIndex,
575                kNewLinkMessageWlan0InterfaceFlags,
576                kNewLinkMessageWlan0InterfaceFlagsChange,
577                ByteString(string(kNewLinkMessageWlan0MACAddress), false),
578                ByteString(string(kNewLinkMessageWlan0InterfaceName), true),
579                kNewLinkMessageWlan0MTU,
580                ByteString(string(kNewLinkMessageWlan0Qdisc), true),
581                kNewLinkMessageWlan0OperState);
582}
583
584TEST_F(RTNLMessageTest, DelLinkEth0) {
585  TestParseLink(ByteString(kDelLinkMessageEth0, sizeof(kDelLinkMessageEth0)),
586                RTNLMessage::kModeDelete,
587                kDelLinkMessageEth0InterfaceIndex,
588                kDelLinkMessageEth0InterfaceFlags,
589                kDelLinkMessageEth0InterfaceFlagsChange,
590                ByteString(string(kDelLinkMessageEth0MACAddress), false),
591                ByteString(string(kDelLinkMessageEth0InterfacName), true),
592                kDelLinkMessageEth0MTU,
593                ByteString(string(kDelLinkMessageEth0Qdisc), true),
594                kDelLinkMessageEth0OperState);
595}
596
597TEST_F(RTNLMessageTest, NewAddrIPv4) {
598  IPAddress addr(IPAddress::kFamilyIPv4);
599
600  EXPECT_TRUE(addr.SetAddressFromString(kNewAddrIPV4Address));
601  addr.set_prefix(kNewAddrIPV4AddressPrefix);
602  TestParseAddress(ByteString(kNewAddrIPV4, sizeof(kNewAddrIPV4)),
603                   RTNLMessage::kModeAdd,
604                   kNewAddrIPV4InterfaceIndex,
605                   addr,
606                   kNewAddrIPV4Scope);
607}
608
609TEST_F(RTNLMessageTest, DelAddrIPv6) {
610  IPAddress addr(IPAddress::kFamilyIPv6);
611
612  EXPECT_TRUE(addr.SetAddressFromString(kDelAddrIPV6Address));
613  addr.set_prefix(kDelAddrIPV6AddressPrefix);
614  TestParseAddress(ByteString(kDelAddrIPV6, sizeof(kDelAddrIPV6)),
615                   RTNLMessage::kModeDelete,
616                   kDelAddrIPV6InterfaceIndex,
617                   addr,
618                   kDelAddrIPV6Scope);
619}
620
621TEST_F(RTNLMessageTest, DelRouteIPv6) {
622  IPAddress dst(IPAddress::kFamilyIPv6);
623  IPAddress src(IPAddress::kFamilyIPv6);
624  IPAddress gateway(IPAddress::kFamilyIPv6);
625
626  EXPECT_TRUE(dst.SetAddressFromString(kDelRouteIPV6Address));
627  dst.set_prefix(kDelRouteIPV6Prefix);
628  src.SetAddressToDefault();
629  EXPECT_TRUE(gateway.SetAddressFromString(kDelRouteIPV6Address));
630
631  TestParseRoute(ByteString(kDelRouteIPV6, sizeof(kDelRouteIPV6)),
632                 RTNLMessage::kModeDelete,
633                 IPAddress::kFamilyIPv6,
634                 kDelRouteIPV6InterfaceIndex,
635                 dst,
636                 src,
637                 gateway,
638                 RT_TABLE_MAIN,
639                 RTPROT_UNSPEC,
640                 RT_SCOPE_UNIVERSE,
641                 RTN_UNICAST,
642                 kDelRouteIPV6Metric);
643}
644
645TEST_F(RTNLMessageTest, AddRouteIPv4) {
646  IPAddress dst(IPAddress::kFamilyIPv4);
647  IPAddress src(IPAddress::kFamilyIPv4);
648  IPAddress gateway(IPAddress::kFamilyIPv4);
649
650  dst.SetAddressToDefault();
651  src.SetAddressToDefault();
652  EXPECT_TRUE(gateway.SetAddressFromString(kAddRouteIPV4Address));
653
654  TestParseRoute(ByteString(kAddRouteIPV4, sizeof(kAddRouteIPV4)),
655                 RTNLMessage::kModeAdd,
656                 IPAddress::kFamilyIPv4,
657                 kAddRouteIPV4InterfaceIndex,
658                 dst,
659                 src,
660                 gateway,
661                 RT_TABLE_MAIN,
662                 RTPROT_BOOT,
663                 RT_SCOPE_UNIVERSE,
664                 RTN_UNICAST,
665                 kAddRouteIPV4Metric);
666}
667
668TEST_F(RTNLMessageTest, NewRdnssOption) {
669  int interface_index = 1;
670  uint32_t lifetime = 0xffffffff;
671  std::string dns_server_addresses =
672      "2001:db8:100:f101::1, 2001:db8:100:f101::2";
673
674  TestParseRdnss(ByteString(kNdRdnssMessage, sizeof(kNdRdnssMessage)),
675                 RTNLMessage::kModeAdd,
676                 interface_index,
677                 lifetime,
678                 dns_server_addresses);
679}
680
681TEST_F(RTNLMessageTest, AddRouteBusted) {
682  // RTNLMessage should list parse errors as kMessageUnknown
683  RTNLMessage msg;
684  EXPECT_FALSE(msg.Decode(
685      ByteString(kAddRouteBusted, sizeof(kAddRouteBusted))));
686}
687
688TEST_F(RTNLMessageTest, AddNeighbor) {
689  TestParseNeighbor(
690      ByteString(kAddNeighborMessage, sizeof(kAddNeighborMessage)),
691      RTNLMessage::kModeAdd,
692      IPAddress::kFamilyIPv4,
693      8,
694      NUD_REACHABLE,
695      0,
696      NDA_DST);
697}
698
699TEST_F(RTNLMessageTest, EncodeDelNeighbor) {
700  RTNLMessage msg(RTNLMessage::kTypeNeighbor,
701                  RTNLMessage::kModeDelete,
702                  0, 1, 2, 0,
703                  IPAddress::kFamilyIPv4);
704  msg.set_neighbor_status(RTNLMessage::NeighborStatus(
705      0, NTF_ROUTER, NDA_LLADDR));
706
707  TestParseNeighbor(msg.Encode(),
708                    RTNLMessage::kModeDelete,
709                    IPAddress::kFamilyIPv4,
710                    0,
711                    0,
712                    NTF_ROUTER,
713                    NDA_LLADDR);
714}
715
716TEST_F(RTNLMessageTest, EncodeRouteAdd) {
717  RTNLMessage msg(RTNLMessage::kTypeRoute,
718                  RTNLMessage::kModeAdd,
719                  0, 1, 2, 0,
720                  IPAddress::kFamilyIPv4);
721  IPAddress dst(IPAddress::kFamilyIPv4);
722  IPAddress src(IPAddress::kFamilyIPv4);
723  IPAddress gateway(IPAddress::kFamilyIPv4);
724
725  dst.SetAddressToDefault();
726  src.SetAddressToDefault();
727  EXPECT_TRUE(gateway.SetAddressFromString("192.168.0.1"));
728
729  msg.set_route_status(RTNLMessage::RouteStatus(
730      0, 0, RT_TABLE_MAIN, RTPROT_BOOT, RT_SCOPE_UNIVERSE, RTN_UNICAST, 0));
731  msg.SetAttribute(RTA_DST, dst.address());
732  msg.SetAttribute(RTA_SRC, src.address());
733  msg.SetAttribute(RTA_GATEWAY, gateway.address());
734  msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(12));
735  msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(13));
736
737  TestParseRoute(msg.Encode(),
738                 RTNLMessage::kModeAdd,
739                 IPAddress::kFamilyIPv4,
740                 12,
741                 dst,
742                 src,
743                 gateway,
744                 RT_TABLE_MAIN,
745                 RTPROT_BOOT,
746                 RT_SCOPE_UNIVERSE,
747                 RTN_UNICAST,
748                 13);
749}
750
751TEST_F(RTNLMessageTest, EncodeLinkDel) {
752  const int kInterfaceIndex = 0x1234;
753  RTNLMessage pmsg(RTNLMessage::kTypeLink,
754                  RTNLMessage::kModeDelete,
755                  NLM_F_REQUEST,
756                  0,
757                  0,
758                  kInterfaceIndex,
759                  IPAddress::kFamilyUnknown);
760
761  RTNLMessage msg;
762  EXPECT_TRUE(msg.Decode(pmsg.Encode()));
763
764  EXPECT_EQ(RTNLMessage::kTypeLink, msg.type());
765  EXPECT_EQ(RTNLMessage::kModeDelete, msg.mode());
766  EXPECT_EQ(kInterfaceIndex, msg.interface_index());
767
768  RTNLMessage::LinkStatus status = msg.link_status();
769  EXPECT_EQ(0, status.flags);
770  EXPECT_EQ(0, status.change);
771
772  EXPECT_FALSE(msg.HasAttribute(IFLA_ADDRESS));
773  EXPECT_FALSE(msg.HasAttribute(IFLA_IFNAME));
774  EXPECT_FALSE(msg.HasAttribute(IFLA_MTU));
775  EXPECT_FALSE(msg.HasAttribute(IFLA_QDISC));
776  EXPECT_FALSE(msg.HasAttribute(IFLA_OPERSTATE));
777}
778
779}  // namespace shill
780