Skip to content

Commit

Permalink
Keep oafd running all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
destan19 committed Mar 20, 2021
1 parent 0a5d531 commit e1b21d2
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 186 deletions.
2 changes: 1 addition & 1 deletion luci-app-oaf/root/etc/uci-defaults/91_luci-oaf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
uci -q batch <<-EOF >/dev/null
del ucitrack.@appfilter[-1]
add ucitrack appfilter
set ucitrack.@appfilter[-1].exec="/etc/init.d/appfilter restart"
set ucitrack.@appfilter[-1].exec="/usr/bin/oaf_rule reload"
commit ucitrack
EOF

Expand Down
32 changes: 25 additions & 7 deletions oaf/src/af_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ nf_client_list_clear(void)
AF_CLIENT_UNLOCK_W();
}


void af_client_list_reset_report_num(void)
{
int i;
af_client_info_t * node = NULL;
char mac_str[32] = {0};

AF_INFO("reset report num");
AF_CLIENT_LOCK_W();
for (i = 0; i < MAX_AF_CLIENT_HASH_SIZE;i++){
list_for_each_entry(node, &af_client_list_table[i], hlist){
node->report_count = 0;
printk("reset mac="MAC_FMT" report num to 0\n", MAC_ARRAY(node->mac));
}
}
AF_CLIENT_UNLOCK_W();
}

int get_mac_hash_code(unsigned char *mac)
{
if (!mac)
Expand Down Expand Up @@ -184,7 +202,7 @@ void flush_expired_visit_info(af_client_info_t *node)

}

int af_report_visit_info(af_client_info_t *node){
int __af_visit_info_report(af_client_info_t *node){
unsigned char mac_str[32] = {0};
unsigned char ip_str[32] = {0};
int i, j;
Expand Down Expand Up @@ -235,15 +253,15 @@ int af_report_visit_info(af_client_info_t *node){
kfree(out);
return 0;
}
void af_visit_info_timer_handle(void){
void af_visit_info_report(void){
af_client_info_t *node;
int i;
AF_CLIENT_LOCK_W();
for (i = 0; i < MAX_AF_CLIENT_HASH_SIZE; i++){
list_for_each_entry(node, &af_client_list_table[i], hlist) {
//flush_expired_visit_info(node);
AF_INFO("report %s\n", node->mac);
af_report_visit_info(node);
__af_visit_info_report(node);
}
}
AF_CLIENT_UNLOCK_W();
Expand All @@ -259,11 +277,11 @@ static inline int get_packet_dir(struct net_device *in)
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
static u_int32_t nfclient_hook(void *priv,
static u_int32_t af_client_hook(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state) {
#else
static u_int32_t nfclient_hook(unsigned int hook,
static u_int32_t af_client_hook(unsigned int hook,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
Expand Down Expand Up @@ -334,7 +352,7 @@ static u_int32_t nfclient_hook(unsigned int hook,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
static struct nf_hook_ops af_client_ops[] = {
{
.hook = nfclient_hook,
.hook = af_client_hook,
.pf = PF_INET,
.hooknum = NF_INET_FORWARD,
.priority = NF_IP_PRI_FIRST + 1,
Expand All @@ -343,7 +361,7 @@ static struct nf_hook_ops af_client_ops[] = {
#else
static struct nf_hook_ops af_client_ops[] = {
{
.hook = nfclient_hook,
.hook = af_client_hook,
.owner = THIS_MODULE,
.pf = PF_INET,
.hooknum = NF_INET_FORWARD,
Expand Down
4 changes: 3 additions & 1 deletion oaf/src/af_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ af_client_info_t * find_af_client_by_ip(unsigned int ip);

void check_client_expire(void);

void af_visit_info_timer_handle(void);
void af_visit_info_report(void);

void af_client_list_reset_report_num(void);

#endif
64 changes: 6 additions & 58 deletions oaf/src/af_client_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void *af_client_seq_next(struct seq_file *s, void *v, loff_t *pos)

static void af_client_seq_stop(struct seq_file *s, void *v)
{
seq_printf(s, "%s", "]");
// seq_printf(s, "%s", "]");
AF_CLIENT_UNLOCK_R();
}

Expand All @@ -104,74 +104,22 @@ static int af_client_seq_show(struct seq_file *s, void *v)
unsigned char mac_str[32] = {0};
unsigned char ip_str[32] = {0};
static int index = 0;
int i;
int j;
cJSON *visit_info_array = NULL;
cJSON *root_obj = NULL;
cJSON *visit_obj = NULL;
cJSON *history_array = NULL;
cJSON *history_obj = NULL;
char *out = NULL;
af_client_info_t *node = (af_client_info_t *)v;

if (v == SEQ_START_TOKEN) {
index = 0;
seq_printf(s, "%s", "[");
seq_printf(s, "%-4s %-20s %-20s\n", "Id", "Mac", "Ip");
return 0;
}
if(index > 0)
seq_printf(s, "%s", ",");
// if(index > 0)
// seq_printf(s, "%s", ",");
index++;

root_obj = cJSON_CreateObject();
if(!root_obj){
AF_ERROR("create json obj failed");
return 0;
}
sprintf(mac_str, MAC_FMT, MAC_ARRAY(node->mac));
sprintf(ip_str, "%pI4", &node->ip);
cJSON_AddStringToObject(root_obj, "mac", mac_str);
cJSON_AddStringToObject(root_obj, "ip", ip_str);
cJSON_AddNumberToObject(root_obj, "app_num", node->visit_app_num);
visit_info_array = cJSON_CreateArray();

for(i = 0; i < MAX_RECORD_APP_NUM; i++){
if(node->visit_info[i].app_id == 0)
continue;
if(node->visit_info[i].total_num < 3)
continue;
visit_obj = cJSON_CreateObject();
cJSON_AddNumberToObject(visit_obj, "appid", node->visit_info[i].app_id);
cJSON_AddNumberToObject(visit_obj, "latest_action", node->visit_info[i].latest_action);
cJSON_AddNumberToObject(visit_obj, "latest_time", node->visit_info[i].latest_time);
cJSON_AddNumberToObject(visit_obj, "total_num", node->visit_info[i].total_num);
cJSON_AddNumberToObject(visit_obj, "drop_num", node->visit_info[i].drop_num);
history_array = cJSON_CreateArray();
for(j = 0; j < MAX_VISIT_HISTORY_TIME; j++){
if(node->visit_info[i].history_time[j] <= 0)
continue;
history_obj = cJSON_CreateObject();
cJSON_AddNumberToObject(visit_obj, "action", node->visit_info[i].history_time[j]);
cJSON_AddNumberToObject(visit_obj, "time", node->visit_info[i].action[j]);
cJSON_AddItemToArray(history_array, history_obj);
}

cJSON_AddItemToObject(visit_obj, "history_info", history_array);
cJSON_AddItemToArray(visit_info_array, visit_obj);
}

cJSON_AddItemToObject(root_obj, "visit_info", visit_info_array);
out = cJSON_Print(root_obj);
if(!out)
return 0;
cJSON_Minify(out);
seq_printf(s, "%s", out);
kfree(out);

cJSON_Delete(root_obj);
seq_printf(s, "%-4d %-20s %-20s\n", index, mac_str, ip_str);
return 0;
}


static const struct seq_operations nf_client_seq_ops = {
.start = af_client_seq_start,
.next = af_client_seq_next,
Expand Down
64 changes: 43 additions & 21 deletions oaf/src/app_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,16 +927,22 @@ void TEST_cJSON(void)


struct timer_list oaf_timer;

#define OAF_TIMER_INTERVAL 60
int report_flag = 0;
#define OAF_TIMER_INTERVAL 1
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static void oaf_timer_func(struct timer_list *t)
#else
static void oaf_timer_func(unsigned long ptr)
#endif
{
check_client_expire();
af_visit_info_timer_handle();
static int count = 0;
if (count % 60 == 0)
check_client_expire();
if (count % 60 == 0 || report_flag){
report_flag = 0;
af_visit_info_report();
}
count++;
mod_timer(&oaf_timer, jiffies + OAF_TIMER_INTERVAL * HZ);
}

Expand All @@ -961,12 +967,7 @@ void fini_oaf_timer(void)

static struct sock *oaf_sock = NULL;

struct af_msg_hdr{
int magic;
int len;
};
#define OAF_NETLINK_ID 29
#define MAX_OAF_NL_MSG_LEN 1024

int af_send_msg_to_user(char *pbuf, uint16_t len)
{
struct sk_buff *nl_skb;
Expand Down Expand Up @@ -1003,30 +1004,51 @@ int af_send_msg_to_user(char *pbuf, uint16_t len)
ret = netlink_unicast(oaf_sock, nl_skb, 999, MSG_DONTWAIT);
return ret;
}

#define MAX_OAF_NETLINK_MSG_LEN 1024

static void oaf_user_msg_handle(af_msg_t *msg){
printk("oaf msg handle, action = %d\n", msg->action);
switch(msg->action){
case AF_MSG_INIT:
printk("module init.........\n");
af_client_list_reset_report_num();
report_flag = 1;
break;
default:
break;
}
}
static void oaf_msg_rcv(struct sk_buff *skb)
{
struct nlmsghdr *nlh = NULL;
char *umsg = NULL;
char *kmsg = "ok";

printk("recv user msg\n");
if(skb->len >= nlmsg_total_size(0))
{
nlh = nlmsg_hdr(skb);
umsg = NLMSG_DATA(nlh);
if(umsg)
{
AF_INFO("kernel recv from user: %s\n", umsg);
af_send_msg_to_user(kmsg, strlen(kmsg));
}

struct af_msg_hdr *af_hdr = (struct af_msg_hdr *)umsg;
if (af_hdr->magic != 0xa0b0c0d0){
printk("magic error %x\n", af_hdr->magic);
return;
}
if (af_hdr->len <= 0 || af_hdr->len >= MAX_OAF_NETLINK_MSG_LEN){
printk("data len error\n");
return;
}
void *udata = umsg + sizeof(struct af_msg_hdr);

if(udata)
oaf_user_msg_handle((af_msg_t *)udata);
}
}

int netlink_oaf_init(void)
{
struct netlink_kernel_cfg bm_nl_cfg = {0};
bm_nl_cfg.input = oaf_msg_rcv;
oaf_sock = netlink_kernel_create(&init_net, OAF_NETLINK_ID, &bm_nl_cfg);
struct netlink_kernel_cfg nl_cfg = {0};
nl_cfg.input = oaf_msg_rcv;
oaf_sock = netlink_kernel_create(&init_net, OAF_NETLINK_ID, &nl_cfg);

if (NULL == oaf_sock)
{
Expand Down
19 changes: 19 additions & 0 deletions oaf/src/app_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ enum AF_FEATURE_PARAM_INDEX{
AF_DICT_PARAM_INDEX,
};


#define OAF_NETLINK_ID 29
#define MAX_OAF_NL_MSG_LEN 1024

enum E_MSG_TYPE{
AF_MSG_INIT,
AF_MSG_MAX
};

typedef struct af_msg{
int action;
void *data;
}af_msg_t;

struct af_msg_hdr{
int magic;
int len;
};

enum e_http_method{
HTTP_METHOD_GET = 1,
HTTP_METHOD_POST,
Expand Down
1 change: 1 addition & 0 deletions open-app-filter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ define Package/appfilter/install
$(CP) ./files/feature.cfg $(1)/etc/appfilter/
$(CP) ./files/app_class.txt $(1)/etc/appfilter/
$(INSTALL_BIN) ./files/appfilter.init $(1)/etc/init.d/appfilter
$(INSTALL_BIN) ./files/oaf_rule $(1)/usr/bin
$(INSTALL_BIN) ./files/gen_class.sh $(1)/usr/bin
$(INSTALL_BIN) ./files/appfilter.config $(1)/etc/config/appfilter
$(INSTALL_BIN) $(PKG_BUILD_DIR)/oafd $(1)/usr/bin
Expand Down
Loading

0 comments on commit e1b21d2

Please sign in to comment.