Home | History | Annotate | Download | only in netlink
      1 /*
      2  * Copyright (C) 2017 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 static android.net.netlink.NetlinkConstants.alignedLengthOf;
     20 import static android.net.netlink.StructNlAttr.makeNestedType;
     21 import static android.net.netlink.StructNlAttr.NLA_HEADERLEN;
     22 import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK;
     23 import static android.net.netlink.StructNlMsgHdr.NLM_F_DUMP;
     24 import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE;
     25 import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
     26 import static android.net.util.NetworkConstants.IPV4_ADDR_LEN;
     27 import static java.nio.ByteOrder.BIG_ENDIAN;
     28 
     29 import android.system.OsConstants;
     30 import android.util.Log;
     31 import libcore.io.SizeOf;
     32 
     33 import java.net.Inet4Address;
     34 import java.net.Inet6Address;
     35 import java.net.InetAddress;
     36 import java.nio.ByteBuffer;
     37 import java.nio.ByteOrder;
     38 
     39 
     40 /**
     41  * A NetlinkMessage subclass for netlink conntrack messages.
     42  *
     43  * see also: <linux_src>/include/uapi/linux/netfilter/nfnetlink_conntrack.h
     44  *
     45  * @hide
     46  */
     47 public class ConntrackMessage extends NetlinkMessage {
     48     public static final int STRUCT_SIZE = StructNlMsgHdr.STRUCT_SIZE + StructNfGenMsg.STRUCT_SIZE;
     49 
     50     public static final short NFNL_SUBSYS_CTNETLINK = 1;
     51     public static final short IPCTNL_MSG_CT_NEW = 0;
     52 
     53     // enum ctattr_type
     54     public static final short CTA_TUPLE_ORIG  = 1;
     55     public static final short CTA_TUPLE_REPLY = 2;
     56     public static final short CTA_TIMEOUT     = 7;
     57 
     58     // enum ctattr_tuple
     59     public static final short CTA_TUPLE_IP    = 1;
     60     public static final short CTA_TUPLE_PROTO = 2;
     61 
     62     // enum ctattr_ip
     63     public static final short CTA_IP_V4_SRC = 1;
     64     public static final short CTA_IP_V4_DST = 2;
     65 
     66     // enum ctattr_l4proto
     67     public static final short CTA_PROTO_NUM      = 1;
     68     public static final short CTA_PROTO_SRC_PORT = 2;
     69     public static final short CTA_PROTO_DST_PORT = 3;
     70 
     71     public static byte[] newIPv4TimeoutUpdateRequest(
     72             int proto, Inet4Address src, int sport, Inet4Address dst, int dport, int timeoutSec) {
     73         // *** STYLE WARNING ***
     74         //
     75         // Code below this point uses extra block indentation to highlight the
     76         // packing of nested tuple netlink attribute types.
     77         final StructNlAttr ctaTupleOrig = new StructNlAttr(CTA_TUPLE_ORIG,
     78                 new StructNlAttr(CTA_TUPLE_IP,
     79                         new StructNlAttr(CTA_IP_V4_SRC, src),
     80                         new StructNlAttr(CTA_IP_V4_DST, dst)),
     81                 new StructNlAttr(CTA_TUPLE_PROTO,
     82                         new StructNlAttr(CTA_PROTO_NUM, (byte) proto),
     83                         new StructNlAttr(CTA_PROTO_SRC_PORT, (short) sport, BIG_ENDIAN),
     84                         new StructNlAttr(CTA_PROTO_DST_PORT, (short) dport, BIG_ENDIAN)));
     85 
     86         final StructNlAttr ctaTimeout = new StructNlAttr(CTA_TIMEOUT, timeoutSec, BIG_ENDIAN);
     87 
     88         final int payloadLength = ctaTupleOrig.getAlignedLength() + ctaTimeout.getAlignedLength();
     89         final byte[] bytes = new byte[STRUCT_SIZE + payloadLength];
     90         final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
     91         byteBuffer.order(ByteOrder.nativeOrder());
     92 
     93         final ConntrackMessage ctmsg = new ConntrackMessage();
     94         ctmsg.mHeader.nlmsg_len = bytes.length;
     95         ctmsg.mHeader.nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_NEW;
     96         ctmsg.mHeader.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE;
     97         ctmsg.mHeader.nlmsg_seq = 1;
     98         ctmsg.pack(byteBuffer);
     99 
    100         ctaTupleOrig.pack(byteBuffer);
    101         ctaTimeout.pack(byteBuffer);
    102 
    103         return bytes;
    104     }
    105 
    106     protected StructNfGenMsg mNfGenMsg;
    107 
    108     private ConntrackMessage() {
    109         super(new StructNlMsgHdr());
    110         mNfGenMsg = new StructNfGenMsg((byte) OsConstants.AF_INET);
    111     }
    112 
    113     public void pack(ByteBuffer byteBuffer) {
    114         mHeader.pack(byteBuffer);
    115         mNfGenMsg.pack(byteBuffer);
    116     }
    117 }
    118