Linux之netlink学习


 
//imp2.h
#ifndef __IMP2_H__
#define __IMP2_H__

#define IMP2_U_PID   0
#define IMP2_K_MSG   1
#define IMP2_CLOSE   2

#define NL_IMP2      31
struct packet_info
{
  __u32 src;
  __u32 dest;
};

#endif

//内核态程序imp2_k_my.c
#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#include <linux/netfilter.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4.h>
#include <linux/inet.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/netlink.h>
#include <linux/spinlock.h>
#include <net/sock.h>

#include "imp2.h"

MODULE_LICENSE("Dual BSD/GPL");

#define IMP2_K_MY "imp2_k_my:"
//#define printk(arg) //printkk(KERN_ALERT IMP2_K_MY arg)

DECLARE_MUTEX(receive_sem);
static struct sock *mysock;

struct
{
    __u32 pid;
    rwlock_t lock;
} user_proc;

static int send_to_user(struct packet_info *info)
{
    int ret = 0;
    int size;
    //unsigned char *old_tail;
    sk_buff_data_t old_tail;

    struct sk_buff *skb;
    struct nlmsghdr *nlh;
    struct packet_info *packet;

    printk("%s, begin !!! \n", __func__);

    size = NLMSG_SPACE(sizeof(*info));
    
    skb = alloc_skb(size, GFP_ATOMIC);
    old_tail = skb->tail;

    nlh = NLMSG_PUT(skb, 0, 0, IMP2_K_MSG, size - sizeof(*nlh));
    packet = NLMSG_DATA(nlh);
    memset(packet, 0, sizeof(struct packet_info));

    packet->src = info->src;
    packet->dest = info->dest;

    nlh->nlmsg_len = (__u32)(skb->tail - old_tail);
    
    NETLINK_CB(skb).dst_group = 0;

    read_lock_bh(&user_proc.lock);
    ret = netlink_unicast(mysock, skb, user_proc.pid, MSG_DONTWAIT);
    read_unlock_bh(&user_proc.lock);
    
    printk("%s, end !!! \n", __func__);

    return ret;
    
nlmsg_failure: 
    if(skb)
        kfree_skb(skb);
    return -1;
}

static unsigned int icmp_hook_func(unsigned int hooknum,
             struct sk_buff *skb,
             const struct net_device *in,
             const struct net_device *out,
             int (*okfn)(struct sk_buff *))
{
    struct iphdr *iph = ip_hdr(skb);
    struct packet_info info;

    //printk("%s, begin !!! \n", __func__);

    if (iph->protocol == IPPROTO_ICMP) {
        read_lock_bh(&user_proc.lock);
        
        if (user_proc.pid != 0) {
            info.src = iph->saddr;
            info.dest = iph->daddr;
            printk("%s, src=%u.%u.%u.%u, dst=%u.%u.%u.%u\n", __func__,
                NIPQUAD(info.src), NIPQUAD(info.dest));
            send_to_user(&info);
        } else {
            printk("%s, no user process running..!\n", __func__);
        }
        
        read_unlock_bh(&user_proc.lock);
    } 
    //printk("%s, end !!! \n", __func__);
    
    return NF_ACCEPT;
}

#define NF_IP_PRE_ROUTING    0

static struct nf_hook_ops my_icmp_hook = {
    .hook = icmp_hook_func,
    .pf = PF_INET,
    .hooknum = NF_IP_PRE_ROUTING,
    .owner = THIS_MODULE,
    .priority = NF_IP_PRI_FILTER - 1,
};

static void my_receive(struct sk_buff *skb)
{
    struct nlmsghdr *nlh;
    int len;
    
    printk("%s, begin !!!!!\n", __func__);

    nlh = nlmsg_hdr(skb);
    len = skb->len;

    while (NLMSG_OK(nlh, len)) {

        printk("%s, skb_len = %d!!!!!\n", __func__, len);

        if (nlh->nlmsg_type == IMP2_U_PID) {
        write_lock_bh(&user_proc.lock);
            user_proc.pid = nlh->nlmsg_pid;
        write_unlock_bh(&user_proc.lock);
        } else if (nlh->nlmsg_type == IMP2_CLOSE)
        {	
        write_lock_bh(&user_proc.lock);
	    if(nlh->nlmsg_pid == user_proc.pid)
            	user_proc.pid = 0;
        write_unlock_bh(&user_proc.lock);
        } 
        
        netlink_ack(skb, nlh, 0);
        nlh = NLMSG_NEXT(nlh, len);
    } 
  
    printk("%s, end !!!!!\n", __func__);
}

static int __init imp2_k_my_init(void)
{
    printk("%s, begin !!!!!\n", __func__);
    
    rwlock_init(&user_proc.lock);
    
    mysock = netlink_kernel_create(&init_net, NL_IMP2, 0, my_receive,
        NULL, THIS_MODULE);
    if (!mysock) {
        printk("%s, netlink_kernel_create fail.. !!!!!\n", __func__);
        return -1;
    } 
    
    printk("%s, end !!!!!\n", __func__);

    return nf_register_hook(&my_icmp_hook);
}

static void __exit imp2_k_my_exit(void)
{
    printk("%s, begin !!!!!\n", __func__);
    netlink_kernel_release(mysock);
    nf_unregister_hook(&my_icmp_hook);
    printk("%s, end !!!!!\n", __func__);
}

module_init(imp2_k_my_init);
module_exit(imp2_k_my_exit);

 
//用户态程序imp2_u_my.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <linux/types.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <signal.h>
#include "imp2.h"

struct msg_to_kernel
{
    struct nlmsghdr hdr;
};

struct u_packet_info
{
  struct nlmsghdr hdr;
  struct packet_info icmp_info;
};

static int skfd;

static void sig_int(int signo)
{
    struct sockaddr_nl kpeer;
    struct msg_to_kernel message;

    memset(&kpeer, 0, sizeof(kpeer));
    kpeer.nl_family = AF_NETLINK;
    kpeer.nl_pid = 0;
    kpeer.nl_groups = 0;

    memset(&message, 0, sizeof(message));
    message.hdr.nlmsg_len = NLMSG_LENGTH(0);
    message.hdr.nlmsg_flags = 0;
    message.hdr.nlmsg_type = IMP2_CLOSE;
    message.hdr.nlmsg_pid = getpid();

    sendto(skfd, &message, message.hdr.nlmsg_len, 0, (struct sockaddr *)(&kpeer),
         sizeof(kpeer));

    close(skfd);
    exit(0);
}

int main(void)
{
    struct sockaddr_nl local;
    struct sockaddr_nl kpeer;
    int kpeerlen;
    struct msg_to_kernel message;
    struct u_packet_info info;
    int sendlen = 0;
    int rcvlen = 0;
    struct in_addr addr;

    skfd = socket(PF_NETLINK, SOCK_RAW, NL_IMP2);
    if(skfd < 0) {
        printf("can not create a netlink socket\n");
        exit(0);
    }

    memset(&local, 0, sizeof(local));
    local.nl_family = AF_NETLINK;
    local.nl_pid = getpid();
    local.nl_groups = 0;
    if(bind(skfd, (struct sockaddr*)&local, sizeof(local)) != 0) {
        printf("bind() error\n");
        return -1;
    }

    signal(SIGINT, sig_int);

    memset(&kpeer, 0, sizeof(kpeer));
    kpeer.nl_family = AF_NETLINK;
    kpeer.nl_pid = 0;
    kpeer.nl_groups = 0;

    memset(&message, 0, sizeof(message));
    message.hdr.nlmsg_len = NLMSG_LENGTH(0);
    message.hdr.nlmsg_flags = 0;
    message.hdr.nlmsg_type = IMP2_U_PID;
    message.hdr.nlmsg_pid = local.nl_pid;

    sendto(skfd, &message, message.hdr.nlmsg_len, 0,
        (struct sockaddr*)&kpeer, sizeof(kpeer));

    while(1) {
        kpeerlen = sizeof(struct sockaddr_nl);
        rcvlen = recvfrom(skfd, &info, sizeof(struct u_packet_info),
        0, (struct sockaddr*)&kpeer, &kpeerlen);

        addr.s_addr = info.icmp_info.src;
        printf("src: %s, ", inet_ntoa(addr));
        addr.s_addr = info.icmp_info.dest;
        printf("dest: %s\n", inet_ntoa(addr));
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux Netlink 是一种用于内核与用户空间进行通信的机制。它提供了一种可靠的、高效的跨进程通信方式,用于在内核和用户空间之间传递网络相关的信息和事件。 Netlink 机制通过一组专用的 socket 进行通信,允许用户空间程序发送请求到内核,以获取网络配置、状态和事件等信息,并可以控制网络设备的行为。同时,内核也可以通过 Netlink 通知用户空间应用程序有关网络相关的事件,例如连接建立、断开、路由更新等。 Netlink 消息由一个头部和一个可选的负载组成。头部包含了消息的类型、标志和序列号等信息,而负载则是具体的数据内容。不同的消息类型对应着不同的操作,例如查询配置信息、添加/删除路由、修改网络接口属性等。 Netlink 通信的优势之一是其灵活性和可扩展性。通过定义不同的消息类型,可以实现各种网络管理的功能。例如,可以使用 Netlink 来获取网络接口的状态和统计信息,监控网络连接的建立和断开,甚至控制网络设备的驱动程序。 此外,Netlink 还支持一种多播通信方式,允许多个用户空间程序同时订阅网络相关的事件。这样,当有事件发生时,内核可以将通知发送给所有订阅者,实现实时的网络状态监控和响应。 总的来说,Linux Netlink 是一种用于内核与用户空间之间进行网络相关信息传递和交互的机制。它提供了灵活、可扩展的通信方式,用于实现网络管理和监控等功能。 ### 回答2: Linux Netlink是用于在Linux内核和用户空间之间进行通信的一种机制。它可以让用户空间程序与内核进行信息交互,如获取系统状态信息、配置网络接口等。 Netlink套接字是通过调用socket()函数创建的,它允许进程使用Netlink协议与内核通信。Netlink协议有多个不同的消息类型,每个类型对应不同的功能。比如,NETLINK_ROUTE用于配置和查询路由信息,NETLINK_INET_DIAG用于获取网络连接的状态信息。 在应用程序中,可以使用套接字的send()和recv()函数发送和接收消息。消息的格式由Netlink通信协议规定,一般包括一个消息头和一个NLMSG_PAYLOAD部分,后者可以包含消息所携带的数据。 对于内核模块来说,可以使用netlink_kernel_create()函数创建一个Netlink套接字,并通过netlink_kernel_recvmsg()函数接收和处理来自用户空间的消息。 通过Netlink机制,用户空间程序可以实时监控内核的状态变化,比如网络接口的变更、路由表的变动等。同时,内核也可以通过Netlink向用户空间发送事件通知,以便及时更新相关信息。 总的来说,Linux Netlink提供了一个灵活、高效的通信机制,有效地实现了内核和用户空间之间的信息交互。它在很多情况下被广泛应用,如网络管理工具、性能监控工具等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值