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