linux五元组结构体,linux 下 nf_conntrack_tuple 跟踪记录

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef u8 __a_uint8_t;

typedef s8 __a_int8_t;

typedef u16 __a_uint16_t;

typedef s16 __a_int16_t;

typedef u32 __a_uint32_t;

typedef s32 __a_int32_t;

typedef u64 __a_uint64_t;

typedef s64 __a_int64_t;

typedef __a_uint8_t a_uint8_t;

typedef __a_int8_t a_int8_t;

typedef __a_uint16_t a_uint16_t;

typedef __a_int16_t a_int16_t;

typedef __a_uint32_t a_uint32_t;

typedef __a_int32_t a_int32_t;

typedef __a_uint64_t a_uint64_t;

typedef __a_int64_t a_int64_t;

typedef a_uint32_t fal_ip4_addr_t;

#define dprintf printk

#define NAPT_AGE 0xe

static struct task_struct *test_TaskStruct;

typedef struct

{

a_uint32_t entry_id;

a_uint32_t flags;

a_uint32_t status;

a_uint32_t src_addr;

a_uint32_t dst_addr;

a_uint16_t src_port;

a_uint16_t dst_port;

a_uint32_t trans_addr;

a_uint16_t trans_port;

} napt_entry_t;

/*

void cmd_print(char *fmt, ...)

{

va_list args;

va_start(args, fmt);

// if(out_fd)

// vfprintf(out_fd, fmt, args);

// else

vfprintf(stdout, fmt, args);

va_end(args);

}

*/

void cmd_data_print_ip4addr(char * param_name, a_uint32_t * buf, a_uint32_t size)

{

a_uint32_t i;

fal_ip4_addr_t ip4;

ip4 = *((fal_ip4_addr_t *) buf);

printk("%s", param_name);

for (i = 0; i < 3; i++) {

printk("%d.", (ip4 >> (24 - i * 8)) & 0xff);

}

printk("%d", (ip4 & 0xff));

}

void napt_entry_show(uint32_t ct_addr)

{

napt_entry_t realnapt = {0};

napt_entry_t *entry=&realnapt;

if(!ct_addr)

{

return;

}

struct nf_conn *ct = (struct nf_conn *)ct_addr;

struct nf_conntrack_tuple *org_tuple, *rep_tuple;

if ((ct->status & IPS_NAT_MASK) == IPS_SRC_NAT) //snat

{

org_tuple = &(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);

rep_tuple = &(ct->tuplehash[IP_CT_DIR_REPLY].tuple);

}

else //dnat

{

org_tuple = &(ct->tuplehash[IP_CT_DIR_REPLY].tuple);

rep_tuple = &(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);

}

uint8_t protonum = org_tuple->dst.protonum;

entry->src_addr = org_tuple->src.u3.ip;

entry->src_port = org_tuple->src.u.all;

entry->dst_addr = org_tuple->dst.u3.ip;

entry->dst_port = org_tuple->dst.u.all;

entry->trans_addr = rep_tuple->dst.u3.ip;

entry->trans_port = rep_tuple->dst.u.all;

entry->status = NAPT_AGE;

cmd_data_print_ip4addr("\n[src_addr]:",(a_uint32_t *) & (entry->src_addr),sizeof (fal_ip4_addr_t));

cmd_data_print_ip4addr("\n[dst_addr]:",(a_uint32_t *) & (entry->dst_addr),sizeof (fal_ip4_addr_t));

cmd_data_print_ip4addr("\n[trans_addr]:",(a_uint32_t *) & (entry->trans_addr),sizeof (fal_ip4_addr_t));

dprintf("\n[src_port]:0x%x [dst_port]:0x%x [trans_port]:0x%x ", entry->src_port, entry->dst_port, entry->trans_port);

if(org_tuple->src.l3num == AF_INET)

{

if(protonum == IPPROTO_TCP)

{

printk("[protcal]: TCP \n");

}

else if(protonum == IPPROTO_UDP)

{

printk("[protcal]: UDP \n");

}

}

}

uint32_t napt_entry_list(uint32_t *hash, uint32_t *iterate)

{

struct net *net = &init_net;

struct nf_conntrack_tuple_hash *h = NULL;

struct nf_conn *ct = NULL;

struct hlist_nulls_node *pos = (struct hlist_nulls_node *) (*iterate);

while(*hash < nf_conntrack_htable_size)

{

if(pos == 0)

{

/*get head for list*/

pos = rcu_dereference((&net->ct.hash[*hash])->first);

}

hlist_nulls_for_each_entry_from(h, pos, hnnode)

{

(*iterate) = (uint32_t)(pos->next);

ct = nf_ct_tuplehash_to_ctrack(h);

//return (uint32_t) ct;

napt_entry_show(ct);

msleep(100);

}

++(*hash);

pos = 0;

}

}

void threadTask()

{

uint32_t hash = 0, iterate = 0;

while(1)

{

hash = 0;

if(kthread_should_stop())

{

printk("threadTask: kthread_should_stop\n");

break;

}

napt_entry_list(&hash,&iterate);

{

//schedule_timeout(80000*HZ);//让出CPU,使其他线程可以运行。

//或者使用

msleep(2000);//的底层实现就是schedule_timeout().

}

}

}

static int __init init_nat_test_module(void)

{

test_TaskStruct=kthread_create(threadTask,NULL,"KernelThead");

if(IS_ERR(test_TaskStruct))

{

printk("kthread_create error\n");

}

else

{

wake_up_process(test_TaskStruct);

}

return 0;

}

static void __exit cleanup_anat_test_module(void)

{

kthread_stop(test_TaskStruct);

}

module_init(init_nat_test_module);

module_exit(cleanup_anat_test_module);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("suiyuan from comba");

MODULE_DESCRIPTION("Led driver for nat test");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值