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.system.OsConstants;
20 
21 import java.nio.ByteBuffer;
22 
23 
24 /**
25  * Various constants and static helper methods for netlink communications.
26  *
27  * Values taken from:
28  *
29  *     <linux_src>/include/uapi/linux/netlink.h
30  *     <linux_src>/include/uapi/linux/rtnetlink.h
31  *
32  * @hide
33  */
34 public class NetlinkConstants {
NetlinkConstants()35     private NetlinkConstants() {}
36 
37     public static final int NLA_ALIGNTO = 4;
38     /**
39      * Flag for dumping struct tcp_info.
40      * Corresponding to enum definition in external/strace/linux/inet_diag.h.
41      */
42     public static final int INET_DIAG_MEMINFO = 1;
43 
44     public static final int SOCKDIAG_MSG_HEADER_SIZE =
45             StructNlMsgHdr.STRUCT_SIZE + StructInetDiagMsg.STRUCT_SIZE;
46 
alignedLengthOf(short length)47     public static final int alignedLengthOf(short length) {
48         final int intLength = (int) length & 0xffff;
49         return alignedLengthOf(intLength);
50     }
51 
alignedLengthOf(int length)52     public static final int alignedLengthOf(int length) {
53         if (length <= 0) { return 0; }
54         return (((length + NLA_ALIGNTO - 1) / NLA_ALIGNTO) * NLA_ALIGNTO);
55     }
56 
stringForAddressFamily(int family)57     public static String stringForAddressFamily(int family) {
58         if (family == OsConstants.AF_INET) { return "AF_INET"; }
59         if (family == OsConstants.AF_INET6) { return "AF_INET6"; }
60         if (family == OsConstants.AF_NETLINK) { return "AF_NETLINK"; }
61         return String.valueOf(family);
62     }
63 
stringForProtocol(int protocol)64     public static String stringForProtocol(int protocol) {
65         if (protocol == OsConstants.IPPROTO_TCP) { return "IPPROTO_TCP"; }
66         if (protocol == OsConstants.IPPROTO_UDP) { return "IPPROTO_UDP"; }
67         return String.valueOf(protocol);
68     }
69 
hexify(byte[] bytes)70     public static String hexify(byte[] bytes) {
71         if (bytes == null) { return "(null)"; }
72         return toHexString(bytes, 0, bytes.length);
73     }
74 
hexify(ByteBuffer buffer)75     public static String hexify(ByteBuffer buffer) {
76         if (buffer == null) { return "(null)"; }
77         return toHexString(
78                 buffer.array(), buffer.position(), buffer.remaining());
79     }
80 
81     // Known values for struct nlmsghdr nlm_type.
82     public static final short NLMSG_NOOP         = 1;   // Nothing
83     public static final short NLMSG_ERROR        = 2;   // Error
84     public static final short NLMSG_DONE         = 3;   // End of a dump
85     public static final short NLMSG_OVERRUN      = 4;   // Data lost
86     public static final short NLMSG_MAX_RESERVED = 15;  // Max reserved value
87 
88     public static final short RTM_NEWLINK        = 16;
89     public static final short RTM_DELLINK        = 17;
90     public static final short RTM_GETLINK        = 18;
91     public static final short RTM_SETLINK        = 19;
92     public static final short RTM_NEWADDR        = 20;
93     public static final short RTM_DELADDR        = 21;
94     public static final short RTM_GETADDR        = 22;
95     public static final short RTM_NEWROUTE       = 24;
96     public static final short RTM_DELROUTE       = 25;
97     public static final short RTM_GETROUTE       = 26;
98     public static final short RTM_NEWNEIGH       = 28;
99     public static final short RTM_DELNEIGH       = 29;
100     public static final short RTM_GETNEIGH       = 30;
101     public static final short RTM_NEWRULE        = 32;
102     public static final short RTM_DELRULE        = 33;
103     public static final short RTM_GETRULE        = 34;
104     public static final short RTM_NEWNDUSEROPT   = 68;
105 
106     /* see &lt;linux_src&gt;/include/uapi/linux/sock_diag.h */
107     public static final short SOCK_DIAG_BY_FAMILY = 20;
108 
109     // Netlink groups.
110     public static final int RTNLGRP_ND_USEROPT = 20;
111     public static final int RTMGRP_ND_USEROPT = 1 << (RTNLGRP_ND_USEROPT - 1);
112 
stringForNlMsgType(short nlm_type)113     public static String stringForNlMsgType(short nlm_type) {
114         switch (nlm_type) {
115             case NLMSG_NOOP: return "NLMSG_NOOP";
116             case NLMSG_ERROR: return "NLMSG_ERROR";
117             case NLMSG_DONE: return "NLMSG_DONE";
118             case NLMSG_OVERRUN: return "NLMSG_OVERRUN";
119             case RTM_NEWLINK: return "RTM_NEWLINK";
120             case RTM_DELLINK: return "RTM_DELLINK";
121             case RTM_GETLINK: return "RTM_GETLINK";
122             case RTM_SETLINK: return "RTM_SETLINK";
123             case RTM_NEWADDR: return "RTM_NEWADDR";
124             case RTM_DELADDR: return "RTM_DELADDR";
125             case RTM_GETADDR: return "RTM_GETADDR";
126             case RTM_NEWROUTE: return "RTM_NEWROUTE";
127             case RTM_DELROUTE: return "RTM_DELROUTE";
128             case RTM_GETROUTE: return "RTM_GETROUTE";
129             case RTM_NEWNEIGH: return "RTM_NEWNEIGH";
130             case RTM_DELNEIGH: return "RTM_DELNEIGH";
131             case RTM_GETNEIGH: return "RTM_GETNEIGH";
132             case RTM_NEWRULE: return "RTM_NEWRULE";
133             case RTM_DELRULE: return "RTM_DELRULE";
134             case RTM_GETRULE: return "RTM_GETRULE";
135             case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT";
136             default:
137                 return "unknown RTM type: " + String.valueOf(nlm_type);
138         }
139     }
140 
141     private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
142             'A', 'B', 'C', 'D', 'E', 'F' };
143     /**
144      * Convert a byte array to a hexadecimal string.
145      */
toHexString(byte[] array, int offset, int length)146     public static String toHexString(byte[] array, int offset, int length) {
147         char[] buf = new char[length * 2];
148 
149         int bufIndex = 0;
150         for (int i = offset; i < offset + length; i++) {
151             byte b = array[i];
152             buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
153             buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
154         }
155 
156         return new String(buf);
157     }
158 }
159