2019-06-13 02:53:14 +00:00
|
|
|
#include <linux/module.h>
|
2019-06-14 05:54:31 +00:00
|
|
|
#include <net/netlink.h>
|
2019-06-13 02:53:14 +00:00
|
|
|
|
2019-06-14 09:42:25 +00:00
|
|
|
#include "trace_def.h"
|
2019-06-13 04:12:49 +00:00
|
|
|
#include "../netlink_api/libnetlink_k.h"
|
2019-06-14 05:54:31 +00:00
|
|
|
#include "trace_msg.h"
|
2019-06-14 06:44:45 +00:00
|
|
|
#include "commuapinl.h"
|
2019-06-14 05:54:31 +00:00
|
|
|
|
2019-06-13 02:53:14 +00:00
|
|
|
|
|
|
|
static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh)
|
2019-06-14 05:54:31 +00:00
|
|
|
{
|
|
|
|
trace_ret_t ret = TRACE_FAILURE;
|
|
|
|
trace_hdr_t *hdr;
|
|
|
|
void *buf;
|
2019-06-14 09:42:25 +00:00
|
|
|
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;
|
2019-06-14 05:54:31 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-06-14 09:42:25 +00:00
|
|
|
len = nlmsg_len((const struct nlmsghdr *)nlh);
|
2019-06-14 05:54:31 +00:00
|
|
|
printk(KERN_DEBUG"Receive data of trace is len:%d", len);
|
|
|
|
if (len < TRACE_REQ_SZ) {
|
2019-06-14 09:42:25 +00:00
|
|
|
printk(KERN_WARNING"Receiving data length:%d is less than length:%ld is needed", len, TRACE_REQ_SZ);
|
2019-06-14 05:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr = (trace_hdr_t *)buf;
|
2019-06-14 09:42:25 +00:00
|
|
|
policy = (trace_policy_t *)(buf + 1);
|
2019-06-14 05:54:31 +00:00
|
|
|
|
|
|
|
ret = TRACE_SUCCESS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printk(KERN_WARNING"Unknow msg type:%u", nlh->nlmsg_type);
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-14 09:42:25 +00:00
|
|
|
if (ret == TRACE_FAILURE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply_skb = nlmsg_new(TRACE_REPLY_SZ, GFP_KERNEL);
|
2019-06-14 05:54:31 +00:00
|
|
|
if (reply_skb == NULL) {
|
|
|
|
printk(KERN_ERR"Allocating skb memory is failure");
|
|
|
|
goto FAIL;
|
|
|
|
}
|
2019-06-14 09:42:25 +00:00
|
|
|
reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_MSG_POLICY_REPLY, TRACE_REPLY_SZ, 0);
|
2019-06-14 05:54:31 +00:00
|
|
|
if (reply_hdr == NULL) {
|
|
|
|
printk(KERN_ERR"Putting length of reply is failure");
|
|
|
|
goto FAIL;
|
|
|
|
}
|
2019-06-14 09:42:25 +00:00
|
|
|
reply_data = nlmsg_data(reply_hdr);
|
2019-06-14 05:54:31 +00:00
|
|
|
if (reply_data == NULL) {
|
|
|
|
printk(KERN_ERR"Reply data is null");
|
|
|
|
goto FAIL;
|
|
|
|
}
|
2019-06-14 09:42:25 +00:00
|
|
|
reply = (trace_reply_t *)reply_data;
|
2019-06-14 05:54:31 +00:00
|
|
|
memcpy(&reply->hdr, hdr, sizeof(*hdr));
|
|
|
|
reply->result = ret;
|
|
|
|
|
2019-06-14 09:42:25 +00:00
|
|
|
reply_ret = commnl_unicast(NULL, reply_skb, nlh->nlmsg_pid);
|
2019-06-14 05:54:31 +00:00
|
|
|
if (reply_ret != 0) {
|
|
|
|
printk(KERN_ERR"Reply message is failure");
|
|
|
|
goto FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlmsg_free(reply_skb);
|
2019-06-13 02:53:14 +00:00
|
|
|
return 0;
|
2019-06-14 05:54:31 +00:00
|
|
|
|
|
|
|
FAIL:
|
|
|
|
if (reply_skb != NULL) {
|
|
|
|
nlmsg_free(reply_skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2019-06-13 02:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init trace_init(void)
|
|
|
|
{
|
2019-06-14 05:54:31 +00:00
|
|
|
printk(KERN_INFO"Trace is initiating");
|
2019-06-14 06:44:45 +00:00
|
|
|
cfg_msgtype_register(TRACE_CFG_POLICY, trace_rcv_policy, NULL, NULL);
|
2019-06-14 05:54:31 +00:00
|
|
|
printk(KERN_INFO"Trace is initiated");
|
2019-06-13 02:53:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit trace_exit(void)
|
|
|
|
{
|
2019-06-14 06:44:45 +00:00
|
|
|
cfg_msgtype_unregister(TRACE_CFG_POLICY);
|
2019-06-13 02:53:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(trace_init);
|
|
|
|
module_exit(trace_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Trace process module");
|
|
|
|
MODULE_AUTHOR("zhangtao");
|
|
|
|
|