Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b25871b1 authored by Hungming Chen's avatar Hungming Chen
Browse files

[NFCT.NS.2] Respect the nlfamily in the nlmsg_type string converter

The netlink message type belongs to the specific message family.
Current stringForNlMsgType(nltype) can't distinguish the netlink
message type correctly. For example, both netlink message type
RTM_NEWADDR (20) and SOCK_DIAG_BY_FAMILY (20) have the same
type integer but they belong to different netlink families.

Test: atest TetheringCoverageTest
Change-Id: I679518d018044bded1494bc356a3f802e37389e7
parent e1561909
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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))
+2 −1
Original line number Diff line number Diff line
@@ -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()) + "} "
                + "}";
+47 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.net.netlink;

import android.annotation.NonNull;
import android.system.OsConstants;

import java.nio.ByteBuffer;
@@ -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";
@@ -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',
+5 −0
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ 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()) + "}";
    }

+3 −2
Original line number Diff line number Diff line
@@ -23,8 +23,8 @@ import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;

import android.system.OsConstants;

import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@@ -234,7 +234,8 @@ public class RtNetlinkNeighborMessage extends NetlinkMessage {
    public String toString() {
        final String ipLiteral = (mDestination == null) ? "" : mDestination.getHostAddress();
        return "RtNetlinkNeighborMessage{ "
                + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, "
                + "nlmsghdr{"
                + (mHeader == null ? "" : mHeader.toString(OsConstants.NETLINK_ROUTE)) + "}, "
                + "ndmsg{" + (mNdmsg == null ? "" : mNdmsg.toString()) + "}, "
                + "destination{" + ipLiteral + "} "
                + "linklayeraddr{" + NetlinkConstants.hexify(mLinkLayerAddr) + "} "
Loading