From 46a20ea51777116e22081bb76b4dcfba3aa8d6a2 Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Fri, 14 Jun 2019 13:54:31 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Add=20aaa-12=20trace=20kernel=20process=20c?= =?UTF-8?q?ode=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA?= =?UTF-8?q?=EF=BC=9Azhangtao=20=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9Azhangta?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trace/trace-api => Common}/trace_api.h | 24 +++++-- Platform/build/module.trace_relay.Makefile | 2 +- Platform/build/user.trace.api.Makefile | 2 +- Platform/build/user.trace.test.Makefile | 2 +- .../trace-api => common/trace}/trace_msg.h | 14 ++-- Platform/modules/trace-relay/trace_init.c | 72 ++++++++++++++++++- Platform/user/trace/trace-api/trace_api.c | 5 +- 7 files changed, 102 insertions(+), 19 deletions(-) rename {Platform/user/trace/trace-api => Common}/trace_api.h (70%) rename Platform/{user/trace/trace-api => common/trace}/trace_msg.h (56%) diff --git a/Platform/user/trace/trace-api/trace_api.h b/Common/trace_api.h similarity index 70% rename from Platform/user/trace/trace-api/trace_api.h rename to Common/trace_api.h index 7e7e3c426..319268fef 100755 --- a/Platform/user/trace/trace-api/trace_api.h +++ b/Common/trace_api.h @@ -1,7 +1,6 @@ #ifndef _TRACE_API_H #define _TRACE_API_H -#include #include #include @@ -11,8 +10,21 @@ typedef enum { TRACE_PENDING } trace_ret_t; +#ifndef u8 +typedef unsigned char u8; +#endif + +#ifndef u16 +typedef unsigned short u16; +#endif + +#ifndef u32 +typedef unsigned int u32; +#endif + + typedef struct _addr { - uint8_t family; + u8 family; union { struct in_addr ip4; struct in6_addr ip6; @@ -25,13 +37,13 @@ typedef void (*async_cb)(trace_ret_t ret, void *arg); typedef struct _trace_policy { addr_t src; - uint16_t sport; + u16 sport; addr_t dst; - uint16_t dport; - uint8_t protocol; + u16 dport; + u8 protocol; //PKT_TUPLE - uint16_t app_type; + u16 app_type; } trace_policy_t; trace_ret_t policy_client_init(); diff --git a/Platform/build/module.trace_relay.Makefile b/Platform/build/module.trace_relay.Makefile index ac87e572f..de925e9df 100755 --- a/Platform/build/module.trace_relay.Makefile +++ b/Platform/build/module.trace_relay.Makefile @@ -35,7 +35,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common +PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common -I../common/trace PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) # this line must be at below of thus, because of... diff --git a/Platform/build/user.trace.api.Makefile b/Platform/build/user.trace.api.Makefile index 58a21fc21..e69b124f3 100755 --- a/Platform/build/user.trace.api.Makefile +++ b/Platform/build/user.trace.api.Makefile @@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../user/netlink_uapi +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/trace -I../user/netlink_uapi PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) diff --git a/Platform/build/user.trace.test.Makefile b/Platform/build/user.trace.test.Makefile index 917beb760..a9f211db5 100755 --- a/Platform/build/user.trace.test.Makefile +++ b/Platform/build/user.trace.test.Makefile @@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -I../user/trace/trace-api -I../../Common -I../common +PLAT_ARM64_CFLAGS := -I../user/trace/trace-api -I../../Common -I../common/trace PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) diff --git a/Platform/user/trace/trace-api/trace_msg.h b/Platform/common/trace/trace_msg.h similarity index 56% rename from Platform/user/trace/trace-api/trace_msg.h rename to Platform/common/trace/trace_msg.h index 9e553042e..b1608fd82 100755 --- a/Platform/user/trace/trace-api/trace_msg.h +++ b/Platform/common/trace/trace_msg.h @@ -1,13 +1,16 @@ #ifndef _TRACE_MSG_H #define _TRACE_MSG_H -#include - #include "trace_api.h" +enum { + TRACE_MSG_POLICY_REQ = 0x0, + TRACE_MSG_POLICY_REPLY +}; + typedef struct _trace_hdr { - uint32_t ver; - uint32_t seq; + u32 ver; + u32 seq; } trace_hdr_t; typedef struct _trace_req { @@ -20,4 +23,7 @@ typedef struct _trace_reply { trace_ret_t result; } trace_reply_t; +#define TRACE_REQ_SZ sizeof(trace_req_t) +#define TRACE_REPLY_SZ sizeof(trace_reply_t) + #endif \ No newline at end of file diff --git a/Platform/modules/trace-relay/trace_init.c b/Platform/modules/trace-relay/trace_init.c index a81f553dc..2fc5c6eb0 100755 --- a/Platform/modules/trace-relay/trace_init.c +++ b/Platform/modules/trace-relay/trace_init.c @@ -1,17 +1,83 @@ #include +#include +#include "trace_api.h" #include "../netlink_api/libnetlink_k.h" +#include "trace_msg.h" + static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) -{ - printk(KERN_DEBUG"trace recv policy1111"); +{ + trace_ret_t ret = TRACE_FAILURE; + trace_hdr_t *hdr; + void *buf; + + printk(KERN_DEBUG"Trace recv policy"); + switch (nlh->nlmsg_type) { + case TRACE_MSG_POLICY_REQ: + buf = nlmsg_data(nlh); + if (buf == NULL) { + printk(KERN_ERR"Receiving data is null"); + break; + } + int len = nlmsg_len(nlh); + printk(KERN_DEBUG"Receive data of trace is len:%d", len); + if (len < TRACE_REQ_SZ) { + printk(KERN_WARNING"Receiving data length:%d is less than length:%d is needed", len, TRACE_REQ_SZ); + } + + hdr = (trace_hdr_t *)buf; + trace_policy_t *policy = (trace_policy_t *)(buf + 1); + + ret = TRACE_SUCCESS; + break; + default: + printk(KERN_WARNING"Unknow msg type:%u", nlh->nlmsg_type); + return 1; + break; + } + + struct sk_buff *reply_skb = nlmsg_new(TRACE_REPLY_SZ, GFP_KERNEL); + if (reply_skb == NULL) { + printk(KERN_ERR"Allocating skb memory is failure"); + goto FAIL; + } + struct nlmsghdr *reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_MSG_POLICY_REPLY, TRACE_REPLY_SZ, 0); + if (reply_hdr == NULL) { + printk(KERN_ERR"Putting length of reply is failure"); + goto FAIL; + } + void *reply_data = nlmsg_data(reply_hdr); + if (reply_data == NULL) { + printk(KERN_ERR"Reply data is null"); + goto FAIL; + } + trace_reply_t *reply = (trace_reply_t *)reply_data; + memcpy(&reply->hdr, hdr, sizeof(*hdr)); + reply->result = ret; + + int reply_ret = commnl_unicast(NULL, reply_skb, nlh->nlmsg_pid); + if (reply_ret != 0) { + printk(KERN_ERR"Reply message is failure"); + goto FAIL; + } + + nlmsg_free(reply_skb); return 0; + +FAIL: + if (reply_skb != NULL) { + nlmsg_free(reply_skb); + } + + return 1; } static int __init trace_init(void) { - printk(KERN_DEBUG"trace recv policy"); + printk(KERN_INFO"Trace is initiating"); cfg_msgtype_register(0x12, trace_rcv_policy, NULL, NULL); + printk(KERN_INFO"Trace is initiated"); return 0; } diff --git a/Platform/user/trace/trace-api/trace_api.c b/Platform/user/trace/trace-api/trace_api.c index 78d9cd14a..489939e11 100755 --- a/Platform/user/trace/trace-api/trace_api.c +++ b/Platform/user/trace/trace-api/trace_api.c @@ -29,8 +29,7 @@ #define MAX_QUEUE_COUNT 256 #define MAX_QUEUE_TIMEOUT (EXEC_SYNC_WAIT_TIMEOUT + 2) -#define MAX_TRACE_BUF_SZ (sizeof(struct nlmsghdr) + sizeof(trace_req_t)) - +#define TRACE_POLICY_BUF_SZ (sizeof(struct nlmsghdr) + TRACE_REQ_SZ) #define SESS_HASH_INDEX(seq) (seq >> HASH_SESS_TAB_BITS) @@ -148,7 +147,7 @@ static void *cb_thread(void *arg) static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *policy) { - char buf[MAX_TRACE_BUF_SZ + 10]; + char buf[TRACE_POLICY_BUF_SZ + 10]; struct nlmsghdr *hdr = (struct nlmsghdr *)buf; hdr->nlmsg_len = NLMSG_HDRLEN; From c43bc1f5e6ff07157c531cd34b2abbe5e0fb769e Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Fri, 14 Jun 2019 14:44:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Add=20aaa-12=20netlink=20of=20trace=20is=20?= =?UTF-8?q?defined=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=BA=EF=BC=9Azhangtao=20=E6=A3=80=E8=A7=86=E4=BA=BA?= =?UTF-8?q?=EF=BC=9Azhangtao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/commuapinl.h | 2 +- Platform/modules/trace-relay/trace_init.c | 5 +++-- Platform/user/trace/trace-api/trace_api.c | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Common/commuapinl.h b/Common/commuapinl.h index 7293f156c..77f78fd54 100755 --- a/Common/commuapinl.h +++ b/Common/commuapinl.h @@ -75,7 +75,7 @@ enum commcfgmsgtype{ COMMNMSG_USER, /*for trace*/ - TRACE_CFG1 = 0x20, + TRACE_CFG_POLICY = 0x20, TRACE_CFG_MAX = 0X25, /*add your msg type here:*/ diff --git a/Platform/modules/trace-relay/trace_init.c b/Platform/modules/trace-relay/trace_init.c index 2fc5c6eb0..07b251881 100755 --- a/Platform/modules/trace-relay/trace_init.c +++ b/Platform/modules/trace-relay/trace_init.c @@ -4,6 +4,7 @@ #include "trace_api.h" #include "../netlink_api/libnetlink_k.h" #include "trace_msg.h" +#include "commuapinl.h" static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) @@ -76,14 +77,14 @@ FAIL: static int __init trace_init(void) { printk(KERN_INFO"Trace is initiating"); - cfg_msgtype_register(0x12, trace_rcv_policy, NULL, NULL); + cfg_msgtype_register(TRACE_CFG_POLICY, trace_rcv_policy, NULL, NULL); printk(KERN_INFO"Trace is initiated"); return 0; } static void __exit trace_exit(void) { - cfg_msgtype_unregister(0x12); + cfg_msgtype_unregister(TRACE_CFG_POLICY); } diff --git a/Platform/user/trace/trace-api/trace_api.c b/Platform/user/trace/trace-api/trace_api.c index 489939e11..a1884bf86 100755 --- a/Platform/user/trace/trace-api/trace_api.c +++ b/Platform/user/trace/trace-api/trace_api.c @@ -20,6 +20,7 @@ #include "log.h" #include "collection.h" #include "trace_msg.h" +#include "commuapinl.h" #define HASH_SESS_TAB_BITS 8 #define HASH_SESS_TAB_SIZE (1 << HASH_SESS_TAB_BITS) @@ -152,7 +153,7 @@ static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *p hdr->nlmsg_len = NLMSG_HDRLEN; hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; - hdr->nlmsg_type = 0x12; + hdr->nlmsg_type = TRACE_CFG_POLICY; trace_req_t req; req.hdr.ver = 1; @@ -162,9 +163,8 @@ static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *p commnl_addattr_l(hdr, sizeof(buf), 101, &req, sizeof(trace_req_t)); SYSLOG_DEBUG("Send msg len:%u, msg_flag:%u, msg_type:%u", hdr->nlmsg_len, hdr->nlmsg_flags, hdr->nlmsg_type); - /*发送组装好的netlink消息*/ - if (commcfg_talk(hdr, NULL) < 0) { + if (commcfg_send(hdr) < 0) { SYSLOG_ERR("Message(seq:%u) which been sent is failure", seq); return TRACE_FAILURE; } From 9a7c96790860168ba92f3535b6d2fefbf7a9f223 Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Fri, 14 Jun 2019 15:03:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Add=20aaa-12=20add=20ignore=20idea=20item?= =?UTF-8?q?=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA?= =?UTF-8?q?=EF=BC=9Azhangtao=20=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9Azhangta?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f37f838dc..362e59fbb 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ Platform/build/debug/ Product/build/debug/ .vscode/ _install/ +.idea/