1

我正在創建一個模塊,將捕獲TCP/IP數據包從TCP/IP堆棧並將其發送到從發送到用戶空間的內核。我的代碼現在已經在下半年完成了。它從Kernel向用戶空間發送一條消息。有人可以幫助我捕獲數據包。我不想用libcap如何捕獲TCP/IP數據包

#include <linux/module.h> 
#include <net/sock.h> 
#include <linux/netlink.h> 
#include <linux/skbuff.h> 

#define NETLINK_USER 31 

struct sock *nl_sk = NULL; 

static void hello_nl_recv_msg(struct sk_buff *skb) { 

struct nlmsghdr *nlh; 
int pid; 
struct sk_buff *skb_out; 
int msg_size; 
char *msg="Hello from kernel";//here we have to send the packet data 
int res; 

printk(KERN_INFO "Entering: %s\n", __FUNCTION__); 

msg_size=strlen(msg); 

nlh=(struct nlmsghdr*)skb->data; 
printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh)); 
pid = nlh->nlmsg_pid; /*pid of sending process */ 

skb_out = nlmsg_new(msg_size,0); 

if(!skb_out) 
{ 

    printk(KERN_ERR "Failed to allocate new skb\n"); 
    return; 

} 
nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); 
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */ 
strncpy(nlmsg_data(nlh),msg,msg_size); 

res=nlmsg_unicast(nl_sk,skb_out,pid); 

if(res<0) 
    printk(KERN_INFO "Error while sending bak to user\n"); 

} 

static int __init hello_init(void) 
{ 
printk("Entering: %s\n",__FUNCTION__); 
nl_sk=netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL, THIS_MODULE); 
if(!nl_sk) 
{ 

    printk(KERN_ALERT "Error creating socket.\n"); 
    return -10; 

} 

return 0; 

} 

static void __exit hello_exit(void) { 

printk(KERN_INFO "exiting hello module\n"); 
netlink_kernel_release(nl_sk); 

} 

module_init(hello_init); module_exit(hello_exit); 

MODULE_LICENSE("GPL"); 

user.c的

#include<stdio.h> 
#include<stdlib.h> 
#include<linux/string.h> 
#include <sys/socket.h> 
#include <linux/netlink.h> 

#define NETLINK_USER 31 

#define MAX_PAYLOAD 1024 /* maximum payload size*/ 
struct sockaddr_nl src_addr, dest_addr; 
struct nlmsghdr *nlh = NULL; 
struct iovec iov; 
int sock_fd; 
struct msghdr msg; 

int main() 
{ 
printf("socket "); 
sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER); 

if(sock_fd<0) 
{ 
printf("socket invalid\n"); 
    return -1; 
} 
printf("socket created\n"); 
memset(&src_addr, 0, sizeof(src_addr)); 
src_addr.nl_family = AF_NETLINK; 
src_addr.nl_pid = getpid(); /* self pid */ 

bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr)); 

memset(&dest_addr, 0, sizeof(dest_addr)); 
memset(&dest_addr, 0, sizeof(dest_addr)); 
dest_addr.nl_family = AF_NETLINK; 
dest_addr.nl_pid = 0; /* For Linux Kernel */ 
dest_addr.nl_groups = 0; /* unicast */ 

nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); 
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD)); 
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); 
nlh->nlmsg_pid = getpid(); 
nlh->nlmsg_flags = 0; 

strcpy(NLMSG_DATA(nlh), "Hello"); 

iov.iov_base = (void *)nlh; 
iov.iov_len = nlh->nlmsg_len; 
msg.msg_name = (void *)&dest_addr; 
msg.msg_namelen = sizeof(dest_addr); 
msg.msg_iov = &iov; 
msg.msg_iovlen = 1; 

printf("Sending message to kernel\n"); 
sendmsg(sock_fd,&msg,0); 
printf("Waiting for message from kernel\n"); 

/* Read message from kernel */ 
recvmsg(sock_fd, &msg, 0); 
printf("Received message payload: %s\n", NLMSG_DATA(nlh)); 
close(sock_fd); 
return 0; 
} 
+1

請問爲什麼不想要使用libpcap的原因嗎?這似乎是最明顯的方式,我很好奇 – 2012-01-11 19:50:24

回答

3

您可以使用netfilter的API做這個內核的土地。 Here is一個很好的文章,用內核模塊過濾一些UDP數據包。

我可以知道不使用libpcap的原因嗎?

+0

這是我的主要項目,開發一個模塊來做到這一點使用netfilter ...我已閱讀通過文章你引用並寫了一個代碼使用一個簡單的hello世界netfilter並修改它來讀取IP頭,但我的電腦掛起大部分時間,所以我進一步改變它,但現在我得到一個錯誤 – 2012-01-18 07:40:57

+0

如果((iph-> protocol == IPPROTO_TCP)){ tcph =(struct tcphdr *)(skb_network_header(* skb)+ ip_hdrlen(* skb)); 第49行是int init_module() { nfho.hook = hook_func;錯誤: make -C /lib/modules/2.6.35-22-generic-pae/build M =/home/divdelleah模塊 /home/divdelleah/trial3.c:在函數'hook_func'中: /home/divdelleah /trial3.c:27:錯誤:隱式聲明函數'ip_hdrlen' /home/divdelleah/trial3.c:在函數'init_module'中: /home/divdelleah/trial3.c:49:警告:從不兼容的指針鍵入 你能告訴我我是否做錯了嗎? – 2012-01-18 07:47:18

+0

沒有你的新代碼,有點難以分辨......你確定你已經包含了net/ip.h? – 2012-01-18 10:46:11

0

聽起來像tun設備可能會做你以後的事情。請參閱brief tun description

源被包含在/usr/src/linux/drivers/net/tun.c