summaryrefslogtreecommitdiff
path: root/common/device/com/android/net/module/util/netlink/StructNdMsg.java
blob: 53ce89918a5c37bd39ccd2ff2c7f7d6df2c947bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.net.module.util.netlink;

import android.system.OsConstants;

import java.nio.ByteBuffer;

/**
 * struct ndmsg
 *
 * see: <linux_src>/include/uapi/linux/neighbour.h
 *
 * @hide
 */
public class StructNdMsg {
    // Already aligned.
    public static final int STRUCT_SIZE = 12;

    // Neighbor Cache Entry States
    public static final short NUD_NONE        = 0x00;
    public static final short NUD_INCOMPLETE  = 0x01;
    public static final short NUD_REACHABLE   = 0x02;
    public static final short NUD_STALE       = 0x04;
    public static final short NUD_DELAY       = 0x08;
    public static final short NUD_PROBE       = 0x10;
    public static final short NUD_FAILED      = 0x20;
    public static final short NUD_NOARP       = 0x40;
    public static final short NUD_PERMANENT   = 0x80;

    /**
     * Convert neighbor cache entry state integer to string.
     */
    public static String stringForNudState(short nudState) {
        switch (nudState) {
            case NUD_NONE: return "NUD_NONE";
            case NUD_INCOMPLETE: return "NUD_INCOMPLETE";
            case NUD_REACHABLE: return "NUD_REACHABLE";
            case NUD_STALE: return "NUD_STALE";
            case NUD_DELAY: return "NUD_DELAY";
            case NUD_PROBE: return "NUD_PROBE";
            case NUD_FAILED: return "NUD_FAILED";
            case NUD_NOARP: return "NUD_NOARP";
            case NUD_PERMANENT: return "NUD_PERMANENT";
            default:
                return "unknown NUD state: " + String.valueOf(nudState);
        }
    }

    /**
     * Check whether a neighbor is connected or not.
     */
    public static boolean isNudStateConnected(short nudState) {
        return ((nudState & (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)) != 0);
    }

    /**
     * Check whether a neighbor is in the valid NUD state or not.
     */
    public static boolean isNudStateValid(short nudState) {
        return (isNudStateConnected(nudState)
                || ((nudState & (NUD_PROBE | NUD_STALE | NUD_DELAY)) != 0));
    }

    // Neighbor Cache Entry Flags
    public static byte NTF_USE       = (byte) 0x01;
    public static byte NTF_SELF      = (byte) 0x02;
    public static byte NTF_MASTER    = (byte) 0x04;
    public static byte NTF_PROXY     = (byte) 0x08;
    public static byte NTF_ROUTER    = (byte) 0x80;

    private static String stringForNudFlags(byte flags) {
        final StringBuilder sb = new StringBuilder();
        if ((flags & NTF_USE) != 0) {
            sb.append("NTF_USE");
        }
        if ((flags & NTF_SELF) != 0) {
            if (sb.length() > 0) {
                sb.append("|");
            }
            sb.append("NTF_SELF");
        }
        if ((flags & NTF_MASTER) != 0) {
            if (sb.length() > 0) {
                sb.append("|");
            }
            sb.append("NTF_MASTER");
        }
        if ((flags & NTF_PROXY) != 0) {
            if (sb.length() > 0) {
                sb.append("|");
            }
            sb.append("NTF_PROXY");
        }
        if ((flags & NTF_ROUTER) != 0) {
            if (sb.length() > 0) {
                sb.append("|");
            }
            sb.append("NTF_ROUTER");
        }
        return sb.toString();
    }

    private static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
        return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
    }

    /**
     * Parse a neighbor discovery netlink message header from a {@link ByteBuffer}.
     *
     * @param byteBuffer The buffer from which to parse the nd netlink message header.
     * @return the parsed nd netlink message header, or {@code null} if the nd netlink message
     *         header could not be parsed successfully (for example, if it was truncated).
     */
    public static StructNdMsg parse(ByteBuffer byteBuffer) {
        if (!hasAvailableSpace(byteBuffer)) return null;

        // The ByteOrder must have already been set by the caller.  In most
        // cases ByteOrder.nativeOrder() is correct, with the possible
        // exception of usage within unittests.
        final StructNdMsg struct = new StructNdMsg();
        struct.ndm_family = byteBuffer.get();
        final byte pad1 = byteBuffer.get();
        final short pad2 = byteBuffer.getShort();
        struct.ndm_ifindex = byteBuffer.getInt();
        struct.ndm_state = byteBuffer.getShort();
        struct.ndm_flags = byteBuffer.get();
        struct.ndm_type = byteBuffer.get();
        return struct;
    }

    public byte ndm_family;
    public int ndm_ifindex;
    public short ndm_state;
    public byte ndm_flags;
    public byte ndm_type;

    public StructNdMsg() {
        ndm_family = (byte) OsConstants.AF_UNSPEC;
    }

    /**
     * Write the neighbor discovery message header to {@link ByteBuffer}.
     */
    public void pack(ByteBuffer byteBuffer) {
        // The ByteOrder must have already been set by the caller.  In most
        // cases ByteOrder.nativeOrder() is correct, with the exception
        // of usage within unittests.
        byteBuffer.put(ndm_family);
        byteBuffer.put((byte) 0);         // pad1
        byteBuffer.putShort((short) 0);   // pad2
        byteBuffer.putInt(ndm_ifindex);
        byteBuffer.putShort(ndm_state);
        byteBuffer.put(ndm_flags);
        byteBuffer.put(ndm_type);
    }

    /**
     * Check whether a neighbor is connected or not.
     */
    public boolean nudConnected() {
        return isNudStateConnected(ndm_state);
    }

    /**
     * Check whether a neighbor is in the valid NUD state or not.
     */
    public boolean nudValid() {
        return isNudStateValid(ndm_state);
    }

    @Override
    public String toString() {
        final String stateStr = "" + ndm_state + " (" + stringForNudState(ndm_state) + ")";
        final String flagsStr = "" + ndm_flags + " (" + stringForNudFlags(ndm_flags) + ")";
        return "StructNdMsg{ "
                + "family{" + NetlinkConstants.stringForAddressFamily((int) ndm_family) + "}, "
                + "ifindex{" + ndm_ifindex + "}, "
                + "state{" + stateStr + "}, "
                + "flags{" + flagsStr + "}, "
                + "type{" + ndm_type + "} "
                + "}";
    }
}