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.system.OsConstants;
20import com.android.internal.util.HexDump;
21
22import java.nio.ByteBuffer;
23
24
25/**
26 * Various constants and static helper methods for netlink communications.
27 *
28 * Values taken from:
29 *
30 *     <linux_src>/include/uapi/linux/netlink.h
31 *     <linux_src>/include/uapi/linux/rtnetlink.h
32 *
33 * @hide
34 */
35public class NetlinkConstants {
36    private NetlinkConstants() {}
37
38    public static final int NLA_ALIGNTO = 4;
39
40    public static final int alignedLengthOf(short length) {
41        final int intLength = (int) length & 0xffff;
42        return alignedLengthOf(intLength);
43    }
44
45    public static final int alignedLengthOf(int length) {
46        if (length <= 0) { return 0; }
47        return (((length + NLA_ALIGNTO - 1) / NLA_ALIGNTO) * NLA_ALIGNTO);
48    }
49
50    public static String stringForAddressFamily(int family) {
51        if (family == OsConstants.AF_INET) { return "AF_INET"; }
52        if (family == OsConstants.AF_INET6) { return "AF_INET6"; }
53        if (family == OsConstants.AF_NETLINK) { return "AF_NETLINK"; }
54        return String.valueOf(family);
55    }
56
57    public static String hexify(byte[] bytes) {
58        if (bytes == null) { return "(null)"; }
59        return HexDump.toHexString(bytes);
60    }
61
62    public static String hexify(ByteBuffer buffer) {
63        if (buffer == null) { return "(null)"; }
64        return HexDump.toHexString(
65                buffer.array(), buffer.position(), buffer.remaining());
66    }
67
68    // Known values for struct nlmsghdr nlm_type.
69    public static final short NLMSG_NOOP         = 1;   // Nothing
70    public static final short NLMSG_ERROR        = 2;   // Error
71    public static final short NLMSG_DONE         = 3;   // End of a dump
72    public static final short NLMSG_OVERRUN      = 4;   // Data lost
73    public static final short NLMSG_MAX_RESERVED = 15;  // Max reserved value
74
75    public static final short RTM_NEWLINK        = 16;
76    public static final short RTM_DELLINK        = 17;
77    public static final short RTM_GETLINK        = 18;
78    public static final short RTM_SETLINK        = 19;
79    public static final short RTM_NEWADDR        = 20;
80    public static final short RTM_DELADDR        = 21;
81    public static final short RTM_GETADDR        = 22;
82    public static final short RTM_NEWROUTE       = 24;
83    public static final short RTM_DELROUTE       = 25;
84    public static final short RTM_GETROUTE       = 26;
85    public static final short RTM_NEWNEIGH       = 28;
86    public static final short RTM_DELNEIGH       = 29;
87    public static final short RTM_GETNEIGH       = 30;
88    public static final short RTM_NEWRULE        = 32;
89    public static final short RTM_DELRULE        = 33;
90    public static final short RTM_GETRULE        = 34;
91    public static final short RTM_NEWNDUSEROPT   = 68;
92
93    public static String stringForNlMsgType(short nlm_type) {
94        switch (nlm_type) {
95            case NLMSG_NOOP: return "NLMSG_NOOP";
96            case NLMSG_ERROR: return "NLMSG_ERROR";
97            case NLMSG_DONE: return "NLMSG_DONE";
98            case NLMSG_OVERRUN: return "NLMSG_OVERRUN";
99            case RTM_NEWLINK: return "RTM_NEWLINK";
100            case RTM_DELLINK: return "RTM_DELLINK";
101            case RTM_GETLINK: return "RTM_GETLINK";
102            case RTM_SETLINK: return "RTM_SETLINK";
103            case RTM_NEWADDR: return "RTM_NEWADDR";
104            case RTM_DELADDR: return "RTM_DELADDR";
105            case RTM_GETADDR: return "RTM_GETADDR";
106            case RTM_NEWROUTE: return "RTM_NEWROUTE";
107            case RTM_DELROUTE: return "RTM_DELROUTE";
108            case RTM_GETROUTE: return "RTM_GETROUTE";
109            case RTM_NEWNEIGH: return "RTM_NEWNEIGH";
110            case RTM_DELNEIGH: return "RTM_DELNEIGH";
111            case RTM_GETNEIGH: return "RTM_GETNEIGH";
112            case RTM_NEWRULE: return "RTM_NEWRULE";
113            case RTM_DELRULE: return "RTM_DELRULE";
114            case RTM_GETRULE: return "RTM_GETRULE";
115            case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT";
116            default:
117                return "unknown RTM type: " + String.valueOf(nlm_type);
118        }
119    }
120}
121