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