secgateway/Platform/modules/trace-relay/trace_init.c

166 lines
5.3 KiB
C
Raw Normal View History

#include <linux/module.h>
#include <linux/rtnetlink.h>
#include <net/netlink.h>
#include "trace_def.h"
#include "libnetlink_k.h"
#include "trace_msg.h"
#include "commuapinl.h"
#include "conntrack_api.h"
#include "pdeliverynl_kinit.h"
#define NLMSG_TAIL(nmsg) \
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len))
#define RTA_ALIGNTO 4U
#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )
static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *ack)
{
trace_ret_t ret = TRACE_FAILURE;
trace_hdr_t *hdr;
void *buf;
int len;
struct sk_buff *reply_skb;
struct nlmsghdr *reply_hdr;
void *reply_data;
trace_reply_t *reply;
int reply_ret;
trace_policy_t *policy;
struct nlattr *rta;
struct dpi dpi;
int ct_ret;
printk(KERN_INFO "Trace recv policy, msg_type:%u", nlh->nlmsg_type);
switch (nlh->nlmsg_type) {
case TRACE_CFG_POLICY_REQ:
buf = nlmsg_data(nlh);
if (buf == NULL) {
printk(KERN_ERR"Receiving data is null");
break;
}
len = nlmsg_len((const struct nlmsghdr *)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:%ld is needed", len, TRACE_REQ_SZ);
}
rta = (struct nlattr *)buf;
hdr = (trace_hdr_t *)RTA_DATA(rta);
printk(KERN_INFO "Receive type:%u of message, version:%u, sequence:%u, is_reply:%u ",
nlh->nlmsg_type, hdr->ver, hdr->seq, hdr->is_reply);
printk(KERN_INFO "Receive netlink-message-pid:%u ", nlh->nlmsg_pid);
policy = (trace_policy_t *)(hdr + 1);
printk(KERN_DEBUG "Receive policy:");
printk(KERN_DEBUG " protocol:%u", policy->protocol);
dpi.aid = policy->app_type;
printk(KERN_DEBUG " app_type:%u", policy->app_type);
dpi.base_aid = policy->base_type;
printk(KERN_DEBUG " base_type:%u", policy->base_type);
printk(KERN_DEBUG " src family:%u", policy->src.family);
if (policy->src.family == TRACE_FAMILY_IP4) {
dpi.tuple.sip = policy->src.addr.ip4.s_addr;
printk(KERN_DEBUG " src ip:%02x", policy->src.addr.ip4.s_addr);
} else {
}
printk(KERN_DEBUG " dst family:%u", policy->dst.family);
if (policy->dst.family == TRACE_FAMILY_IP4) {
dpi.tuple.dip = policy->dst.addr.ip4.s_addr;
printk(KERN_DEBUG " dst ip:%02x", policy->dst.addr.ip4.s_addr);
} else {
}
dpi.tuple.sport = policy->sport;
dpi.tuple.dport = policy->dport;
dpi.tuple.protonum = policy->protocol;
printk(KERN_DEBUG " sport:%u (host order %u)", policy->sport, ntohs(policy->sport));
printk(KERN_DEBUG " dport:%u (host order %u)", policy->dport, ntohs(policy->dport));
printk(KERN_DEBUG " protocol:%u", dpi.tuple.protonum);
ct_ret = set_aid_by_dpi_tuple(&dpi);
if (ct_ret != CMHI_EXT_OK) {
printk(KERN_WARNING"Setting aid is failure:%d", ct_ret);
}
ret = TRACE_SUCCESS;
break;
default:
printk(KERN_WARNING"Unknow msg type:%u", nlh->nlmsg_type);
return 1;
break;
}
if (ret == TRACE_FAILURE) {
return 1;
}
if (hdr->is_reply == REPLY_OP_NO_NEED) {
printk(KERN_DEBUG"Not need reply");
return 0;
}
reply_skb = nlmsg_new(TRACE_REPLY_SZ, GFP_KERNEL);
if (reply_skb == NULL) {
printk(KERN_ERR"Allocating skb memory is failure");
goto FAIL;
}
reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_CFG_POLICY_REPLY, TRACE_REPLY_SZ, 0);
if (reply_hdr == NULL) {
printk(KERN_ERR"Putting length of reply is failure");
goto FAIL;
}
reply_data = nlmsg_data(reply_hdr);
if (reply_data == NULL) {
printk(KERN_ERR"Reply data is null");
goto FAIL;
}
reply = (trace_reply_t *)reply_data;
memcpy(&reply->hdr, hdr, sizeof(*hdr));
reply->result = ret;
reply_ret = pdeliv_unicast(reply_skb, nlh->nlmsg_pid);
if (reply_ret != 0) {
printk(KERN_ERR"Reply message is failure");
goto FAIL;
}
printk(KERN_INFO"Reply message final");
//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_INFO"Trace is initiating");
pdeliv_msgtype_register(TRACE_CFG_POLICY_REQ, trace_rcv_policy, NULL, NULL);
printk(KERN_INFO"Trace is initiated");
return 0;
}
static void __exit trace_exit(void)
{
pdeliv_msgtype_unregister(TRACE_CFG_POLICY_REQ);
}
module_init(trace_init);
module_exit(trace_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Trace process module");
MODULE_AUTHOR("zhangtao");