1 /*
2  * Copyright (C) 2019 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.system.OsConstants;
20 import java.nio.ByteBuffer;
21 
22 
23 /**
24  * struct ndmsg
25  *
26  * see: <linux_src>/include/uapi/linux/neighbour.h
27  *
28  * @hide
29  */
30 public class StructNdMsg {
31     // Already aligned.
32     public static final int STRUCT_SIZE = 12;
33 
34     // Neighbor Cache Entry States
35     public static final short NUD_NONE        = 0x00;
36     public static final short NUD_INCOMPLETE  = 0x01;
37     public static final short NUD_REACHABLE   = 0x02;
38     public static final short NUD_STALE       = 0x04;
39     public static final short NUD_DELAY       = 0x08;
40     public static final short NUD_PROBE       = 0x10;
41     public static final short NUD_FAILED      = 0x20;
42     public static final short NUD_NOARP       = 0x40;
43     public static final short NUD_PERMANENT   = 0x80;
44 
stringForNudState(short nudState)45     public static String stringForNudState(short nudState) {
46         switch (nudState) {
47             case NUD_NONE: return "NUD_NONE";
48             case NUD_INCOMPLETE: return "NUD_INCOMPLETE";
49             case NUD_REACHABLE: return "NUD_REACHABLE";
50             case NUD_STALE: return "NUD_STALE";
51             case NUD_DELAY: return "NUD_DELAY";
52             case NUD_PROBE: return "NUD_PROBE";
53             case NUD_FAILED: return "NUD_FAILED";
54             case NUD_NOARP: return "NUD_NOARP";
55             case NUD_PERMANENT: return "NUD_PERMANENT";
56             default:
57                 return "unknown NUD state: " + String.valueOf(nudState);
58         }
59     }
60 
isNudStateConnected(short nudState)61     public static boolean isNudStateConnected(short nudState) {
62         return ((nudState & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)) != 0);
63     }
64 
isNudStateValid(short nudState)65     public static boolean isNudStateValid(short nudState) {
66         return (isNudStateConnected(nudState) ||
67                 ((nudState & (NUD_PROBE|NUD_STALE|NUD_DELAY)) != 0));
68     }
69 
70     // Neighbor Cache Entry Flags
71     public static byte NTF_USE       = (byte) 0x01;
72     public static byte NTF_SELF      = (byte) 0x02;
73     public static byte NTF_MASTER    = (byte) 0x04;
74     public static byte NTF_PROXY     = (byte) 0x08;
75     public static byte NTF_ROUTER    = (byte) 0x80;
76 
stringForNudFlags(byte flags)77     public static String stringForNudFlags(byte flags) {
78         final StringBuilder sb = new StringBuilder();
79         if ((flags & NTF_USE) != 0) {
80             sb.append("NTF_USE");
81         }
82         if ((flags & NTF_SELF) != 0) {
83             if (sb.length() > 0) { sb.append("|"); }
84             sb.append("NTF_SELF");
85         }
86         if ((flags & NTF_MASTER) != 0) {
87             if (sb.length() > 0) { sb.append("|"); }
88             sb.append("NTF_MASTER");
89         }
90         if ((flags & NTF_PROXY) != 0) {
91             if (sb.length() > 0) { sb.append("|");
92         }
93             sb.append("NTF_PROXY"); }
94         if ((flags & NTF_ROUTER) != 0) {
95             if (sb.length() > 0) { sb.append("|"); }
96             sb.append("NTF_ROUTER");
97         }
98         return sb.toString();
99     }
100 
hasAvailableSpace(ByteBuffer byteBuffer)101     private static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
102         return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
103     }
104 
parse(ByteBuffer byteBuffer)105     public static StructNdMsg parse(ByteBuffer byteBuffer) {
106         if (!hasAvailableSpace(byteBuffer)) { return null; }
107 
108         // The ByteOrder must have already been set by the caller.  In most
109         // cases ByteOrder.nativeOrder() is correct, with the possible
110         // exception of usage within unittests.
111         final StructNdMsg struct = new StructNdMsg();
112         struct.ndm_family = byteBuffer.get();
113         final byte pad1 = byteBuffer.get();
114         final short pad2 = byteBuffer.getShort();
115         struct.ndm_ifindex = byteBuffer.getInt();
116         struct.ndm_state = byteBuffer.getShort();
117         struct.ndm_flags = byteBuffer.get();
118         struct.ndm_type = byteBuffer.get();
119         return struct;
120     }
121 
122     public byte ndm_family;
123     public int ndm_ifindex;
124     public short ndm_state;
125     public byte ndm_flags;
126     public byte ndm_type;
127 
StructNdMsg()128     public StructNdMsg() {
129         ndm_family = (byte) OsConstants.AF_UNSPEC;
130     }
131 
pack(ByteBuffer byteBuffer)132     public void pack(ByteBuffer byteBuffer) {
133         // The ByteOrder must have already been set by the caller.  In most
134         // cases ByteOrder.nativeOrder() is correct, with the exception
135         // of usage within unittests.
136         byteBuffer.put(ndm_family);
137         byteBuffer.put((byte) 0);         // pad1
138         byteBuffer.putShort((short) 0);   // pad2
139         byteBuffer.putInt(ndm_ifindex);
140         byteBuffer.putShort(ndm_state);
141         byteBuffer.put(ndm_flags);
142         byteBuffer.put(ndm_type);
143     }
144 
nudConnected()145     public boolean nudConnected() {
146         return isNudStateConnected(ndm_state);
147     }
148 
nudValid()149     public boolean nudValid() {
150         return isNudStateValid(ndm_state);
151     }
152 
153     @Override
toString()154     public String toString() {
155         final String stateStr = "" + ndm_state + " (" + stringForNudState(ndm_state) + ")";
156         final String flagsStr = "" + ndm_flags + " (" + stringForNudFlags(ndm_flags) + ")";
157         return "StructNdMsg{ "
158                 + "family{" + NetlinkConstants.stringForAddressFamily((int) ndm_family) + "}, "
159                 + "ifindex{" + ndm_ifindex + "}, "
160                 + "state{" + stateStr + "}, "
161                 + "flags{" + flagsStr + "}, "
162                 + "type{" + ndm_type + "} "
163                 + "}";
164     }
165 }
166