Loading common/moduleutils/src/android/net/ip/IpNeighborMonitor.java +1 −1 Original line number Diff line number Diff line Loading @@ -105,7 +105,7 @@ public class IpNeighborMonitor extends NetlinkMonitor { public String toString() { final StringJoiner j = new StringJoiner(",", "NeighborEvent{", "}"); return j.add("@" + elapsedMs) .add(stringForNlMsgType(msgType)) .add(stringForNlMsgType(msgType, NETLINK_ROUTE)) .add("if=" + ifindex) .add(ip.getHostAddress()) .add(StructNdMsg.stringForNudState(nudState)) Loading common/moduleutils/src/android/net/ip/NetlinkMonitor.java +1 −1 Original line number Diff line number Diff line Loading @@ -108,7 +108,7 @@ public class NetlinkMonitor extends PacketReader { while (byteBuffer.remaining() > 0) { try { final int position = byteBuffer.position(); final NetlinkMessage nlMsg = NetlinkMessage.parse(byteBuffer); final NetlinkMessage nlMsg = NetlinkMessage.parse(byteBuffer, mFamily); if (nlMsg == null || nlMsg.getHeader() == null) { byteBuffer.position(position); mLog.e("unparsable netlink msg: " + hexify(byteBuffer)); Loading common/netlinkclient/src/android/net/netlink/InetDiagMessage.java +3 −2 Original line number Diff line number Diff line Loading @@ -120,7 +120,7 @@ public class InetDiagMessage extends NetlinkMessage { NetlinkSocket.sendMessage(fd, msg, 0, msg.length, TIMEOUT_MS); ByteBuffer response = NetlinkSocket.recvMessage(fd, DEFAULT_RECV_BUFSIZE, TIMEOUT_MS); final NetlinkMessage nlMsg = NetlinkMessage.parse(response); final NetlinkMessage nlMsg = NetlinkMessage.parse(response, NETLINK_INET_DIAG); final StructNlMsgHdr hdr = nlMsg.getHeader(); if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) { return INVALID_UID; Loading Loading @@ -213,7 +213,8 @@ public class InetDiagMessage extends NetlinkMessage { @Override public String toString() { return "InetDiagMessage{ " + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, " + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString(NETLINK_INET_DIAG)) + "}, " + "inet_diag_msg{" + (mStructInetDiagMsg == null ? "" : mStructInetDiagMsg.toString()) + "} " + "}"; Loading common/netlinkclient/src/android/net/netlink/NetlinkConstants.java +47 −4 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android.net.netlink; import android.annotation.NonNull; import android.system.OsConstants; import java.nio.ByteBuffer; Loading Loading @@ -110,12 +111,26 @@ public class NetlinkConstants { public static final int RTNLGRP_ND_USEROPT = 20; public static final int RTMGRP_ND_USEROPT = 1 << (RTNLGRP_ND_USEROPT - 1); public static String stringForNlMsgType(short nlm_type) { switch (nlm_type) { /** * Convert a netlink message type to a string for control message. */ @NonNull private static String stringForCtlMsgType(short nlmType) { switch (nlmType) { case NLMSG_NOOP: return "NLMSG_NOOP"; case NLMSG_ERROR: return "NLMSG_ERROR"; case NLMSG_DONE: return "NLMSG_DONE"; case NLMSG_OVERRUN: return "NLMSG_OVERRUN"; default: return "unknown control message type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string for NETLINK_ROUTE. */ @NonNull private static String stringForRtMsgType(short nlmType) { switch (nlmType) { case RTM_NEWLINK: return "RTM_NEWLINK"; case RTM_DELLINK: return "RTM_DELLINK"; case RTM_GETLINK: return "RTM_GETLINK"; Loading @@ -133,9 +148,37 @@ public class NetlinkConstants { case RTM_DELRULE: return "RTM_DELRULE"; case RTM_GETRULE: return "RTM_GETRULE"; case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT"; default: return "unknown RTM type: " + String.valueOf(nlm_type); default: return "unknown RTM type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string for NETLINK_INET_DIAG. */ @NonNull private static String stringForInetDiagMsgType(short nlmType) { switch (nlmType) { case SOCK_DIAG_BY_FAMILY: return "SOCK_DIAG_BY_FAMILY"; default: return "unknown SOCK_DIAG type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string by netlink family. */ @NonNull public static String stringForNlMsgType(short nlmType, int nlFamily) { // Reserved control messages. The netlink family is ignored. // See NLMSG_MIN_TYPE in include/uapi/linux/netlink.h. if (nlmType <= NLMSG_MAX_RESERVED) return stringForCtlMsgType(nlmType); // Netlink family messages. The netlink family is required. Note that the reason for using // if-statement is that switch-case can't be used because the OsConstants.NETLINK_* are // not constant. if (nlFamily == OsConstants.NETLINK_ROUTE) return stringForRtMsgType(nlmType); if (nlFamily == OsConstants.NETLINK_INET_DIAG) return stringForInetDiagMsgType(nlmType); return "unknown type: " + String.valueOf(nlmType); } private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', Loading common/netlinkclient/src/android/net/netlink/NetlinkMessage.java +70 −25 Original line number Diff line number Diff line Loading @@ -16,6 +16,10 @@ package android.net.netlink; import android.annotation.NonNull; import android.annotation.Nullable; import android.system.OsConstants; import java.nio.ByteBuffer; Loading @@ -35,7 +39,12 @@ import java.nio.ByteBuffer; public class NetlinkMessage { private final static String TAG = "NetlinkMessage"; public static NetlinkMessage parse(ByteBuffer byteBuffer) { /** * Parsing netlink messages for reserved control message or specific netlink message. The * netlink family is required for parsing specific netlink message. See man-pages/netlink. */ @Nullable public static NetlinkMessage parse(@NonNull ByteBuffer byteBuffer, int nlFamily) { final int startPosition = (byteBuffer != null) ? byteBuffer.position() : -1; final StructNlMsgHdr nlmsghdr = StructNlMsgHdr.parse(byteBuffer); if (nlmsghdr == null) { Loading @@ -50,31 +59,23 @@ public class NetlinkMessage { return null; } switch (nlmsghdr.nlmsg_type) { //case NetlinkConstants.NLMSG_NOOP: case NetlinkConstants.NLMSG_ERROR: return (NetlinkMessage) NetlinkErrorMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.NLMSG_DONE: byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); //case NetlinkConstants.NLMSG_OVERRUN: case NetlinkConstants.RTM_NEWNEIGH: case NetlinkConstants.RTM_DELNEIGH: case NetlinkConstants.RTM_GETNEIGH: return (NetlinkMessage) RtNetlinkNeighborMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.SOCK_DIAG_BY_FAMILY: return (NetlinkMessage) InetDiagMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.RTM_NEWNDUSEROPT: return (NetlinkMessage) NduseroptMessage.parse(nlmsghdr, byteBuffer); default: // Reserved control messages. The netlink family is ignored. // See NLMSG_MIN_TYPE in include/uapi/linux/netlink.h. if (nlmsghdr.nlmsg_type <= NetlinkConstants.NLMSG_MAX_RESERVED) { // Netlink control message. Just parse the header for now, // pretending the whole message was consumed. byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); return parseCtlMessage(nlmsghdr, byteBuffer, payloadLength); } return null; // Netlink family messages. The netlink family is required. Note that the reason for using // if-statement is that switch-case can't be used because the OsConstants.NETLINK_* are // not constant. if (nlFamily == OsConstants.NETLINK_ROUTE) { return parseRtMessage(nlmsghdr, byteBuffer); } if (nlFamily == OsConstants.NETLINK_INET_DIAG) { return parseInetDiagMessage(nlmsghdr, byteBuffer); } return null; } protected StructNlMsgHdr mHeader; Loading @@ -89,6 +90,50 @@ public class NetlinkMessage { @Override public String toString() { // The netlink family is not provided to StructNlMsgHdr#toString because NetlinkMessage // doesn't store the information. So the netlink message type can't be transformed into // a string by StructNlMsgHdr#toString and just keep as an integer. The specific message // which inherits NetlinkMessage could override NetlinkMessage#toString and provide the // specific netlink family to StructNlMsgHdr#toString. return "NetlinkMessage{" + (mHeader == null ? "" : mHeader.toString()) + "}"; } @NonNull private static NetlinkMessage parseCtlMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer, int payloadLength) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.NLMSG_ERROR: return (NetlinkMessage) NetlinkErrorMessage.parse(nlmsghdr, byteBuffer); default: { // Other netlink control messages. Just parse the header for now, // pretending the whole message was consumed. byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); } } } @Nullable private static NetlinkMessage parseRtMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.RTM_NEWNEIGH: case NetlinkConstants.RTM_DELNEIGH: case NetlinkConstants.RTM_GETNEIGH: return (NetlinkMessage) RtNetlinkNeighborMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.RTM_NEWNDUSEROPT: return (NetlinkMessage) NduseroptMessage.parse(nlmsghdr, byteBuffer); default: return null; } } @Nullable private static NetlinkMessage parseInetDiagMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.SOCK_DIAG_BY_FAMILY: return (NetlinkMessage) InetDiagMessage.parse(nlmsghdr, byteBuffer); default: return null; } } } Loading
common/moduleutils/src/android/net/ip/IpNeighborMonitor.java +1 −1 Original line number Diff line number Diff line Loading @@ -105,7 +105,7 @@ public class IpNeighborMonitor extends NetlinkMonitor { public String toString() { final StringJoiner j = new StringJoiner(",", "NeighborEvent{", "}"); return j.add("@" + elapsedMs) .add(stringForNlMsgType(msgType)) .add(stringForNlMsgType(msgType, NETLINK_ROUTE)) .add("if=" + ifindex) .add(ip.getHostAddress()) .add(StructNdMsg.stringForNudState(nudState)) Loading
common/moduleutils/src/android/net/ip/NetlinkMonitor.java +1 −1 Original line number Diff line number Diff line Loading @@ -108,7 +108,7 @@ public class NetlinkMonitor extends PacketReader { while (byteBuffer.remaining() > 0) { try { final int position = byteBuffer.position(); final NetlinkMessage nlMsg = NetlinkMessage.parse(byteBuffer); final NetlinkMessage nlMsg = NetlinkMessage.parse(byteBuffer, mFamily); if (nlMsg == null || nlMsg.getHeader() == null) { byteBuffer.position(position); mLog.e("unparsable netlink msg: " + hexify(byteBuffer)); Loading
common/netlinkclient/src/android/net/netlink/InetDiagMessage.java +3 −2 Original line number Diff line number Diff line Loading @@ -120,7 +120,7 @@ public class InetDiagMessage extends NetlinkMessage { NetlinkSocket.sendMessage(fd, msg, 0, msg.length, TIMEOUT_MS); ByteBuffer response = NetlinkSocket.recvMessage(fd, DEFAULT_RECV_BUFSIZE, TIMEOUT_MS); final NetlinkMessage nlMsg = NetlinkMessage.parse(response); final NetlinkMessage nlMsg = NetlinkMessage.parse(response, NETLINK_INET_DIAG); final StructNlMsgHdr hdr = nlMsg.getHeader(); if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) { return INVALID_UID; Loading Loading @@ -213,7 +213,8 @@ public class InetDiagMessage extends NetlinkMessage { @Override public String toString() { return "InetDiagMessage{ " + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, " + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString(NETLINK_INET_DIAG)) + "}, " + "inet_diag_msg{" + (mStructInetDiagMsg == null ? "" : mStructInetDiagMsg.toString()) + "} " + "}"; Loading
common/netlinkclient/src/android/net/netlink/NetlinkConstants.java +47 −4 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android.net.netlink; import android.annotation.NonNull; import android.system.OsConstants; import java.nio.ByteBuffer; Loading Loading @@ -110,12 +111,26 @@ public class NetlinkConstants { public static final int RTNLGRP_ND_USEROPT = 20; public static final int RTMGRP_ND_USEROPT = 1 << (RTNLGRP_ND_USEROPT - 1); public static String stringForNlMsgType(short nlm_type) { switch (nlm_type) { /** * Convert a netlink message type to a string for control message. */ @NonNull private static String stringForCtlMsgType(short nlmType) { switch (nlmType) { case NLMSG_NOOP: return "NLMSG_NOOP"; case NLMSG_ERROR: return "NLMSG_ERROR"; case NLMSG_DONE: return "NLMSG_DONE"; case NLMSG_OVERRUN: return "NLMSG_OVERRUN"; default: return "unknown control message type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string for NETLINK_ROUTE. */ @NonNull private static String stringForRtMsgType(short nlmType) { switch (nlmType) { case RTM_NEWLINK: return "RTM_NEWLINK"; case RTM_DELLINK: return "RTM_DELLINK"; case RTM_GETLINK: return "RTM_GETLINK"; Loading @@ -133,9 +148,37 @@ public class NetlinkConstants { case RTM_DELRULE: return "RTM_DELRULE"; case RTM_GETRULE: return "RTM_GETRULE"; case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT"; default: return "unknown RTM type: " + String.valueOf(nlm_type); default: return "unknown RTM type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string for NETLINK_INET_DIAG. */ @NonNull private static String stringForInetDiagMsgType(short nlmType) { switch (nlmType) { case SOCK_DIAG_BY_FAMILY: return "SOCK_DIAG_BY_FAMILY"; default: return "unknown SOCK_DIAG type: " + String.valueOf(nlmType); } } /** * Convert a netlink message type to a string by netlink family. */ @NonNull public static String stringForNlMsgType(short nlmType, int nlFamily) { // Reserved control messages. The netlink family is ignored. // See NLMSG_MIN_TYPE in include/uapi/linux/netlink.h. if (nlmType <= NLMSG_MAX_RESERVED) return stringForCtlMsgType(nlmType); // Netlink family messages. The netlink family is required. Note that the reason for using // if-statement is that switch-case can't be used because the OsConstants.NETLINK_* are // not constant. if (nlFamily == OsConstants.NETLINK_ROUTE) return stringForRtMsgType(nlmType); if (nlFamily == OsConstants.NETLINK_INET_DIAG) return stringForInetDiagMsgType(nlmType); return "unknown type: " + String.valueOf(nlmType); } private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', Loading
common/netlinkclient/src/android/net/netlink/NetlinkMessage.java +70 −25 Original line number Diff line number Diff line Loading @@ -16,6 +16,10 @@ package android.net.netlink; import android.annotation.NonNull; import android.annotation.Nullable; import android.system.OsConstants; import java.nio.ByteBuffer; Loading @@ -35,7 +39,12 @@ import java.nio.ByteBuffer; public class NetlinkMessage { private final static String TAG = "NetlinkMessage"; public static NetlinkMessage parse(ByteBuffer byteBuffer) { /** * Parsing netlink messages for reserved control message or specific netlink message. The * netlink family is required for parsing specific netlink message. See man-pages/netlink. */ @Nullable public static NetlinkMessage parse(@NonNull ByteBuffer byteBuffer, int nlFamily) { final int startPosition = (byteBuffer != null) ? byteBuffer.position() : -1; final StructNlMsgHdr nlmsghdr = StructNlMsgHdr.parse(byteBuffer); if (nlmsghdr == null) { Loading @@ -50,31 +59,23 @@ public class NetlinkMessage { return null; } switch (nlmsghdr.nlmsg_type) { //case NetlinkConstants.NLMSG_NOOP: case NetlinkConstants.NLMSG_ERROR: return (NetlinkMessage) NetlinkErrorMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.NLMSG_DONE: byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); //case NetlinkConstants.NLMSG_OVERRUN: case NetlinkConstants.RTM_NEWNEIGH: case NetlinkConstants.RTM_DELNEIGH: case NetlinkConstants.RTM_GETNEIGH: return (NetlinkMessage) RtNetlinkNeighborMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.SOCK_DIAG_BY_FAMILY: return (NetlinkMessage) InetDiagMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.RTM_NEWNDUSEROPT: return (NetlinkMessage) NduseroptMessage.parse(nlmsghdr, byteBuffer); default: // Reserved control messages. The netlink family is ignored. // See NLMSG_MIN_TYPE in include/uapi/linux/netlink.h. if (nlmsghdr.nlmsg_type <= NetlinkConstants.NLMSG_MAX_RESERVED) { // Netlink control message. Just parse the header for now, // pretending the whole message was consumed. byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); return parseCtlMessage(nlmsghdr, byteBuffer, payloadLength); } return null; // Netlink family messages. The netlink family is required. Note that the reason for using // if-statement is that switch-case can't be used because the OsConstants.NETLINK_* are // not constant. if (nlFamily == OsConstants.NETLINK_ROUTE) { return parseRtMessage(nlmsghdr, byteBuffer); } if (nlFamily == OsConstants.NETLINK_INET_DIAG) { return parseInetDiagMessage(nlmsghdr, byteBuffer); } return null; } protected StructNlMsgHdr mHeader; Loading @@ -89,6 +90,50 @@ public class NetlinkMessage { @Override public String toString() { // The netlink family is not provided to StructNlMsgHdr#toString because NetlinkMessage // doesn't store the information. So the netlink message type can't be transformed into // a string by StructNlMsgHdr#toString and just keep as an integer. The specific message // which inherits NetlinkMessage could override NetlinkMessage#toString and provide the // specific netlink family to StructNlMsgHdr#toString. return "NetlinkMessage{" + (mHeader == null ? "" : mHeader.toString()) + "}"; } @NonNull private static NetlinkMessage parseCtlMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer, int payloadLength) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.NLMSG_ERROR: return (NetlinkMessage) NetlinkErrorMessage.parse(nlmsghdr, byteBuffer); default: { // Other netlink control messages. Just parse the header for now, // pretending the whole message was consumed. byteBuffer.position(byteBuffer.position() + payloadLength); return new NetlinkMessage(nlmsghdr); } } } @Nullable private static NetlinkMessage parseRtMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.RTM_NEWNEIGH: case NetlinkConstants.RTM_DELNEIGH: case NetlinkConstants.RTM_GETNEIGH: return (NetlinkMessage) RtNetlinkNeighborMessage.parse(nlmsghdr, byteBuffer); case NetlinkConstants.RTM_NEWNDUSEROPT: return (NetlinkMessage) NduseroptMessage.parse(nlmsghdr, byteBuffer); default: return null; } } @Nullable private static NetlinkMessage parseInetDiagMessage(@NonNull StructNlMsgHdr nlmsghdr, @NonNull ByteBuffer byteBuffer) { switch (nlmsghdr.nlmsg_type) { case NetlinkConstants.SOCK_DIAG_BY_FAMILY: return (NetlinkMessage) InetDiagMessage.parse(nlmsghdr, byteBuffer); default: return null; } } }