1 /*
2  * Copyright (C) 2015 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 package android.net.netlink;
18 
19 import android.net.netlink.NetlinkConstants;
20 import android.net.netlink.NetlinkMessage;
21 import android.net.netlink.RtNetlinkNeighborMessage;
22 import android.net.netlink.StructNdMsg;
23 import android.net.netlink.StructNlMsgHdr;
24 import android.test.suitebuilder.annotation.SmallTest;
25 import android.system.OsConstants;
26 import android.util.Log;
27 import libcore.util.HexEncoding;
28 
29 import java.net.Inet4Address;
30 import java.net.InetAddress;
31 import java.net.UnknownHostException;
32 import java.nio.ByteBuffer;
33 import java.nio.ByteOrder;
34 import java.util.Arrays;
35 import junit.framework.TestCase;
36 
37 
38 public class RtNetlinkNeighborMessageTest extends TestCase {
39     private final String TAG = "RtNetlinkNeighborMessageTest";
40 
41     // Hexadecimal representation of packet capture.
42     public static final String RTM_DELNEIGH_HEX =
43             // struct nlmsghdr
44             "4c000000" +     // length = 76
45             "1d00" +         // type = 29 (RTM_DELNEIGH)
46             "0000" +         // flags
47             "00000000" +     // seqno
48             "00000000" +     // pid (0 == kernel)
49             // struct ndmsg
50             "02" +           // family
51             "00" +           // pad1
52             "0000" +         // pad2
53             "15000000" +     // interface index (21  == wlan0, on test device)
54             "0400" +         // NUD state (0x04 == NUD_STALE)
55             "00" +           // flags
56             "01" +           // type
57             // struct nlattr: NDA_DST
58             "0800" +         // length = 8
59             "0100" +         // type (1 == NDA_DST, for neighbor messages)
60             "c0a89ffe" +     // IPv4 address (== 192.168.159.254)
61             // struct nlattr: NDA_LLADDR
62             "0a00" +         // length = 10
63             "0200" +         // type (2 == NDA_LLADDR, for neighbor messages)
64             "00005e000164" + // MAC Address (== 00:00:5e:00:01:64)
65             "0000" +         // padding, for 4 byte alignment
66             // struct nlattr: NDA_PROBES
67             "0800" +         // length = 8
68             "0400" +         // type (4 == NDA_PROBES, for neighbor messages)
69             "01000000" +     // number of probes
70             // struct nlattr: NDA_CACHEINFO
71             "1400" +         // length = 20
72             "0300" +         // type (3 == NDA_CACHEINFO, for neighbor messages)
73             "05190000" +     // ndm_used, as "clock ticks ago"
74             "05190000" +     // ndm_confirmed, as "clock ticks ago"
75             "190d0000" +     // ndm_updated, as "clock ticks ago"
76             "00000000";      // ndm_refcnt
77     public static final byte[] RTM_DELNEIGH =
78             HexEncoding.decode(RTM_DELNEIGH_HEX.toCharArray(), false);
79 
80     // Hexadecimal representation of packet capture.
81     public static final String RTM_NEWNEIGH_HEX =
82             // struct nlmsghdr
83             "58000000" +     // length = 88
84             "1c00" +         // type = 28 (RTM_NEWNEIGH)
85             "0000" +         // flags
86             "00000000" +     // seqno
87             "00000000" +     // pid (0 == kernel)
88             // struct ndmsg
89             "0a" +           // family
90             "00" +           // pad1
91             "0000" +         // pad2
92             "15000000" +     // interface index (21  == wlan0, on test device)
93             "0400" +         // NUD state (0x04 == NUD_STALE)
94             "80" +           // flags
95             "01" +           // type
96             // struct nlattr: NDA_DST
97             "1400" +         // length = 20
98             "0100" +         // type (1 == NDA_DST, for neighbor messages)
99             "fe8000000000000086c9b2fffe6aed4b" + // IPv6 address (== fe80::86c9:b2ff:fe6a:ed4b)
100             // struct nlattr: NDA_LLADDR
101             "0a00" +         // length = 10
102             "0200" +         // type (2 == NDA_LLADDR, for neighbor messages)
103             "84c9b26aed4b" + // MAC Address (== 84:c9:b2:6a:ed:4b)
104             "0000" +         // padding, for 4 byte alignment
105             // struct nlattr: NDA_PROBES
106             "0800" +         // length = 8
107             "0400" +         // type (4 == NDA_PROBES, for neighbor messages)
108             "01000000" +     // number of probes
109             // struct nlattr: NDA_CACHEINFO
110             "1400" +         // length = 20
111             "0300" +         // type (3 == NDA_CACHEINFO, for neighbor messages)
112             "eb0e0000" +     // ndm_used, as "clock ticks ago"
113             "861f0000" +     // ndm_confirmed, as "clock ticks ago"
114             "00000000" +     // ndm_updated, as "clock ticks ago"
115             "05000000";      // ndm_refcnt
116     public static final byte[] RTM_NEWNEIGH =
117             HexEncoding.decode(RTM_NEWNEIGH_HEX.toCharArray(), false);
118 
119     // An example of the full response from an RTM_GETNEIGH query.
120     private static final String RTM_GETNEIGH_RESPONSE_HEX =
121             // <-- struct nlmsghr             -->|<-- struct ndmsg           -->|<-- struct nlattr: NDA_DST             -->|<-- NDA_LLADDR          -->|<-- NDA_PROBES -->|<-- NDA_CACHEINFO                         -->|
122             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000001 0a00 0200 333300000001 0000 0800 0400 00000000 1400 0300 a2280000 32110000 32110000 01000000" +
123             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff000001 0a00 0200 3333ff000001 0000 0800 0400 00000000 1400 0300 0d280000 9d100000 9d100000 00000000" +
124             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 0400 80 01 1400 0100 20010db800040ca00000000000000001 0a00 0200 84c9b26aed4b 0000 0800 0400 04000000 1400 0300 90100000 90100000 90080000 01000000" +
125             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff47da19 0a00 0200 3333ff47da19 0000 0800 0400 00000000 1400 0300 a1280000 31110000 31110000 01000000" +
126             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff020000000000000000000000000016 0a00 0200 333300000016 0000 0800 0400 00000000 1400 0300 912a0000 21130000 21130000 00000000" +
127             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff0200000000000000000001ffeace3b 0a00 0200 3333ffeace3b 0000 0800 0400 00000000 1400 0300 922a0000 22130000 22130000 00000000" +
128             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff5c2a83 0a00 0200 3333ff5c2a83 0000 0800 0400 00000000 1400 0300 391c0000 c9040000 c9040000 01000000" +
129             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 01000000 4000 00 02 1400 0100 00000000000000000000000000000000 0a00 0200 000000000000 0000 0800 0400 00000000 1400 0300 cd180200 5d010200 5d010200 08000000" +
130             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000002 0a00 0200 333300000002 0000 0800 0400 00000000 1400 0300 352a0000 c5120000 c5120000 00000000" +
131             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000016 0a00 0200 333300000016 0000 0800 0400 00000000 1400 0300 982a0000 28130000 28130000 00000000" +
132             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 0800 80 01 1400 0100 fe8000000000000086c9b2fffe6aed4b 0a00 0200 84c9b26aed4b 0000 0800 0400 00000000 1400 0300 23000000 24000000 57000000 13000000" +
133             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ffeace3b 0a00 0200 3333ffeace3b 0000 0800 0400 00000000 1400 0300 992a0000 29130000 29130000 01000000" +
134             "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff020000000000000000000000000002 0a00 0200 333300000002 0000 0800 0400 00000000 1400 0300 2e2a0000 be120000 be120000 00000000" +
135             "44000000 1c00 0200 00000000 3e2b0000 02 00 0000 18000000 4000 00 03 0800 0100 00000000                         0400 0200                   0800 0400 00000000 1400 0300 75280000 05110000 05110000 22000000";
136     public static final byte[] RTM_GETNEIGH_RESPONSE =
137             HexEncoding.decode(RTM_GETNEIGH_RESPONSE_HEX.replaceAll(" ", "").toCharArray(), false);
138 
139     @SmallTest
testParseRtmDelNeigh()140     public void testParseRtmDelNeigh() {
141         final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_DELNEIGH);
142         byteBuffer.order(ByteOrder.LITTLE_ENDIAN);  // For testing.
143         final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
144         assertNotNull(msg);
145         assertTrue(msg instanceof RtNetlinkNeighborMessage);
146         final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
147 
148         final StructNlMsgHdr hdr = neighMsg.getHeader();
149         assertNotNull(hdr);
150         assertEquals(76, hdr.nlmsg_len);
151         assertEquals(NetlinkConstants.RTM_DELNEIGH, hdr.nlmsg_type);
152         assertEquals(0, hdr.nlmsg_flags);
153         assertEquals(0, hdr.nlmsg_seq);
154         assertEquals(0, hdr.nlmsg_pid);
155 
156         final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
157         assertNotNull(ndmsgHdr);
158         assertEquals((byte) OsConstants.AF_INET, ndmsgHdr.ndm_family);
159         assertEquals(21, ndmsgHdr.ndm_ifindex);
160         assertEquals(StructNdMsg.NUD_STALE, ndmsgHdr.ndm_state);
161         final InetAddress destination = neighMsg.getDestination();
162         assertNotNull(destination);
163         assertEquals(InetAddress.parseNumericAddress("192.168.159.254"), destination);
164     }
165 
166     @SmallTest
testParseRtmNewNeigh()167     public void testParseRtmNewNeigh() {
168         final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_NEWNEIGH);
169         byteBuffer.order(ByteOrder.LITTLE_ENDIAN);  // For testing.
170         final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
171         assertNotNull(msg);
172         assertTrue(msg instanceof RtNetlinkNeighborMessage);
173         final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
174 
175         final StructNlMsgHdr hdr = neighMsg.getHeader();
176         assertNotNull(hdr);
177         assertEquals(88, hdr.nlmsg_len);
178         assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
179         assertEquals(0, hdr.nlmsg_flags);
180         assertEquals(0, hdr.nlmsg_seq);
181         assertEquals(0, hdr.nlmsg_pid);
182 
183         final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
184         assertNotNull(ndmsgHdr);
185         assertEquals((byte) OsConstants.AF_INET6, ndmsgHdr.ndm_family);
186         assertEquals(21, ndmsgHdr.ndm_ifindex);
187         assertEquals(StructNdMsg.NUD_STALE, ndmsgHdr.ndm_state);
188         final InetAddress destination = neighMsg.getDestination();
189         assertNotNull(destination);
190         assertEquals(InetAddress.parseNumericAddress("fe80::86c9:b2ff:fe6a:ed4b"), destination);
191     }
192 
193     @SmallTest
testParseRtmGetNeighResponse()194     public void testParseRtmGetNeighResponse() {
195         final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_GETNEIGH_RESPONSE);
196         byteBuffer.order(ByteOrder.LITTLE_ENDIAN);  // For testing.
197 
198         int messageCount = 0;
199         while (byteBuffer.remaining() > 0) {
200             final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
201             assertNotNull(msg);
202             assertTrue(msg instanceof RtNetlinkNeighborMessage);
203             final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
204 
205             final StructNlMsgHdr hdr = neighMsg.getHeader();
206             assertNotNull(hdr);
207             assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
208             assertEquals(StructNlMsgHdr.NLM_F_MULTI, hdr.nlmsg_flags);
209             assertEquals(0, hdr.nlmsg_seq);
210             assertEquals(11070, hdr.nlmsg_pid);
211 
212             messageCount++;
213         }
214         // TODO: add more detailed spot checks.
215         assertEquals(14, messageCount);
216     }
217 
218     @SmallTest
testCreateRtmNewNeighMessage()219     public void testCreateRtmNewNeighMessage() {
220         final int seqNo = 2635;
221         final int ifIndex = 14;
222         final byte[] llAddr =
223                 new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6 };
224 
225         // Hexadecimal representation of our created packet.
226         final String expectedNewNeighHex =
227                 // struct nlmsghdr
228                 "30000000" +     // length = 48
229                 "1c00" +         // type = 28 (RTM_NEWNEIGH)
230                 "0501" +         // flags (NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE)
231                 "4b0a0000" +     // seqno
232                 "00000000" +     // pid (0 == kernel)
233                 // struct ndmsg
234                 "02" +           // family
235                 "00" +           // pad1
236                 "0000" +         // pad2
237                 "0e000000" +     // interface index (14)
238                 "0800" +         // NUD state (0x08 == NUD_DELAY)
239                 "00" +           // flags
240                 "00" +           // type
241                 // struct nlattr: NDA_DST
242                 "0800" +         // length = 8
243                 "0100" +         // type (1 == NDA_DST, for neighbor messages)
244                 "7f000001" +     // IPv4 address (== 127.0.0.1)
245                 // struct nlattr: NDA_LLADDR
246                 "0a00" +         // length = 10
247                 "0200" +         // type (2 == NDA_LLADDR, for neighbor messages)
248                 "010203040506" + // MAC Address (== 01:02:03:04:05:06)
249                 "0000";          // padding, for 4 byte alignment
250         final byte[] expectedNewNeigh =
251                 HexEncoding.decode(expectedNewNeighHex.toCharArray(), false);
252 
253         final byte[] bytes = RtNetlinkNeighborMessage.newNewNeighborMessage(
254             seqNo, Inet4Address.LOOPBACK, StructNdMsg.NUD_DELAY, ifIndex, llAddr);
255         if (!Arrays.equals(expectedNewNeigh, bytes)) {
256             assertEquals(expectedNewNeigh.length, bytes.length);
257             for (int i = 0; i < Math.min(expectedNewNeigh.length, bytes.length); i++) {
258                 assertEquals(expectedNewNeigh[i], bytes[i]);
259             }
260         }
261     }
262 }
263