• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.internal.util.HexDump;
21 
22 import 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  */
35 public class NetlinkConstants {
NetlinkConstants()36     private NetlinkConstants() {}
37 
38     public static final int NLA_ALIGNTO = 4;
39 
alignedLengthOf(short length)40     public static final int alignedLengthOf(short length) {
41         final int intLength = (int) length & 0xffff;
42         return alignedLengthOf(intLength);
43     }
44 
alignedLengthOf(int length)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 
stringForAddressFamily(int family)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 
stringForProtocol(int protocol)57     public static String stringForProtocol(int protocol) {
58         if (protocol == OsConstants.IPPROTO_TCP) { return "IPPROTO_TCP"; }
59         if (protocol == OsConstants.IPPROTO_UDP) { return "IPPROTO_UDP"; }
60         return String.valueOf(protocol);
61     }
62 
hexify(byte[] bytes)63     public static String hexify(byte[] bytes) {
64         if (bytes == null) { return "(null)"; }
65         return HexDump.toHexString(bytes);
66     }
67 
hexify(ByteBuffer buffer)68     public static String hexify(ByteBuffer buffer) {
69         if (buffer == null) { return "(null)"; }
70         return HexDump.toHexString(
71                 buffer.array(), buffer.position(), buffer.remaining());
72     }
73 
74     // Known values for struct nlmsghdr nlm_type.
75     public static final short NLMSG_NOOP         = 1;   // Nothing
76     public static final short NLMSG_ERROR        = 2;   // Error
77     public static final short NLMSG_DONE         = 3;   // End of a dump
78     public static final short NLMSG_OVERRUN      = 4;   // Data lost
79     public static final short NLMSG_MAX_RESERVED = 15;  // Max reserved value
80 
81     public static final short RTM_NEWLINK        = 16;
82     public static final short RTM_DELLINK        = 17;
83     public static final short RTM_GETLINK        = 18;
84     public static final short RTM_SETLINK        = 19;
85     public static final short RTM_NEWADDR        = 20;
86     public static final short RTM_DELADDR        = 21;
87     public static final short RTM_GETADDR        = 22;
88     public static final short RTM_NEWROUTE       = 24;
89     public static final short RTM_DELROUTE       = 25;
90     public static final short RTM_GETROUTE       = 26;
91     public static final short RTM_NEWNEIGH       = 28;
92     public static final short RTM_DELNEIGH       = 29;
93     public static final short RTM_GETNEIGH       = 30;
94     public static final short RTM_NEWRULE        = 32;
95     public static final short RTM_DELRULE        = 33;
96     public static final short RTM_GETRULE        = 34;
97     public static final short RTM_NEWNDUSEROPT   = 68;
98 
99     /* see &lt;linux_src&gt;/include/uapi/linux/sock_diag.h */
100     public static final short SOCK_DIAG_BY_FAMILY = 20;
101 
stringForNlMsgType(short nlm_type)102     public static String stringForNlMsgType(short nlm_type) {
103         switch (nlm_type) {
104             case NLMSG_NOOP: return "NLMSG_NOOP";
105             case NLMSG_ERROR: return "NLMSG_ERROR";
106             case NLMSG_DONE: return "NLMSG_DONE";
107             case NLMSG_OVERRUN: return "NLMSG_OVERRUN";
108             case RTM_NEWLINK: return "RTM_NEWLINK";
109             case RTM_DELLINK: return "RTM_DELLINK";
110             case RTM_GETLINK: return "RTM_GETLINK";
111             case RTM_SETLINK: return "RTM_SETLINK";
112             case RTM_NEWADDR: return "RTM_NEWADDR";
113             case RTM_DELADDR: return "RTM_DELADDR";
114             case RTM_GETADDR: return "RTM_GETADDR";
115             case RTM_NEWROUTE: return "RTM_NEWROUTE";
116             case RTM_DELROUTE: return "RTM_DELROUTE";
117             case RTM_GETROUTE: return "RTM_GETROUTE";
118             case RTM_NEWNEIGH: return "RTM_NEWNEIGH";
119             case RTM_DELNEIGH: return "RTM_DELNEIGH";
120             case RTM_GETNEIGH: return "RTM_GETNEIGH";
121             case RTM_NEWRULE: return "RTM_NEWRULE";
122             case RTM_DELRULE: return "RTM_DELRULE";
123             case RTM_GETRULE: return "RTM_GETRULE";
124             case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT";
125             default:
126                 return "unknown RTM type: " + String.valueOf(nlm_type);
127         }
128     }
129 }
130