三、ip_queue代码分析
ip_queue模块的代码较为简单,包含ip_queue.h和ip_queue.c。我们将分别该两个源文件进行分析。
(一)数据结构的定义
ip_queue模块的数据结构定义在头文件ip_queue.h中,主要定义了用于在内核态和用户态传输数据的相关数据结构。其代码如下:
- /*
- * This is a module which is used for queueing IPv4 packets and
- * communicating with userspace via netlink.
- *
- * (C) 2000 James Morris, this code is GPL.
- */
- #ifndef _IP_QUEUE_H
- #define _IP_QUEUE_H
- #ifdef __KERNEL__
- #ifdef DEBUG_IPQ
- #define QDEBUG(x...) printk(KERN_DEBUG ## x)
- #else
- #define QDEBUG(x...)
- #endif /* DEBUG_IPQ */
- #else
- #include <net/if.h>
- #endif /* ! __KERNEL__ */
- /* 内核态发送到用户态的消息的数据结构*/
- typedef struct ipq_packet_msg {
- unsigned long packet_id; /* ID of queued packet */
- unsigned long mark; /* Netfilter mark value */
- long timestamp_sec; /* Packet arrival time (seconds) */
- long timestamp_usec; /* Packet arrvial time (+useconds) */
- unsigned int hook; /* Netfilter hook we rode in on */
- char indev_name[IFNAMSIZ]; /* Name of incoming interface */
- char outdev_name[IFNAMSIZ]; /* Name of outgoing interface */
- unsigned short hw_protocol; /* Hardware protocol (network order) */
- unsigned short hw_type; /* Hardware type */
- unsigned char hw_addrlen; /* Hardware address length */
- unsigned char hw_addr[8]; /* Hardware address */
- size_t data_len; /* Length of packet data */
- unsigned char payload[0]; /* Optional packet data */
- } ipq_packet_msg_t;
- /* 用户态发送到内核态的模式消息 */
- typedef struct ipq_mode_msg {
- unsigned char value; /* Requested mode */
- size_t range; /* Optional range of packet requested */
- } ipq_mode_msg_t;
- /* 用户态发送到内核态的断言消息 */
- typedef struct ipq_verdict_msg {
- unsigned int value; /* Verdict to hand to netfilter */
- unsigned long id; /* Packet ID for this verdict */
- size_t data_len; /* Length of replacement data */
- unsigned char payload[0]; /* Optional replacement packet */
- } ipq_verdict_msg_t;
- /*统一封装起来的用户态发内核态的信息的数据结构*/
- typedef struct ipq_peer_msg {
- union {
- ipq_verdict_msg_t verdict;
- ipq_mode_msg_t mode;
- } msg;
- } ipq_peer_msg_t;
- /* 报文传输的模式*/
- enum {
- IPQ_COPY_NONE, /* Initial mode, packets are dropped */
- IPQ_COPY_META, /* Copy metadata */
- IPQ_COPY_PACKET /* Copy metadata + packet (range) */
- };
- #define IPQ_COPY_MAX IPQ_COPY_PACKET
- /* IP Queue消息的类型 */
- #define IPQM_BASE 0x10 /* standard netlink messages below this */
- #define IPQM_MODE (IPQM_BASE + 1) /* Mode request from peer */
- #define IPQM_VERDICT (IPQM_BASE + 2) /* Verdict from peer */
- #define IPQM_PACKET (IPQM_BASE + 3) /* Packet from kernel */
- #define IPQM_MAX (IPQM_BASE + 4)
- #endif /*_IP_QUEUE_H*/
该头文件中定义的相关数据结构和宏应该和用户空间引用的头文件的内容是保持一致的。因此,对于该文件中的数据结构的详细解释,可以参考本系列文章的篇《Linux内核IP Queue机制的分析(一)——用户态接收数据包》中第二部分IP Queue编程接口。
(二)ip_queue模块的加载和卸载
分析一个内核模块的源代码,通常我们先看模块加载时做哪些工作,这样就可以了解到模块具体的功能会在什么条件下执行。而模块的卸载通常就是将模块加载时注册的函数和申请的资源进行释放等工作。因此,这里简单的分析一下init函数的实现,代码在ip_queue.c中。
- static int __init ip_queue_init(void)
- {
- int status = -ENOMEM;
- struct proc_dir_entry *proc;
- /*注册netlink的通知链*/
- netlink_register_notifier(&ipq_nl_notifier);
- /*内核态创建netlink的socket,并注册ipq_rcv_sk函数实现接收用户空间下发的配置数据*/
- ipqnl = netlink_kernel_create(NETLINK_FIREWALL, 0, ipq_rcv_sk,
- THIS_MODULE);
- if (ipqnl == NULL) {
- printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
- goto cleanup_netlink_notifier;
- }
-
- /*注册proc文件*/
- proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
- if (proc)
- proc->owner = THIS_MODULE;
- else {
- printk(KERN_ERR "ip_queue: failed to create proc entry\n");
- goto cleanup_ipqnl;
- }
- /*注册网络设备的通知链*/
- register_netdevice_notifier(&ipq_dev_notifier);
- ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
- /*注册IP Queue机制的报文处理结构,主要包含一个报文的入队处理函数,下面具体分析*/
- status = nf_register_queue_handler(PF_INET, &nfqh);
- if (status < 0) {
- printk(KERN_ERR "ip_queue: failed to register queue handler\n");
- goto cleanup_sysctl;
- }
- return status;
- cleanup_sysctl:
- unregister_sysctl_table(ipq_sysctl_header);
- unregister_netdevice_notifier(&ipq_dev_notifier);
- proc_net_remove(IPQ_PROC_FS_NAME);
-
- cleanup_ipqnl:
- sock_release(ipqnl->sk_socket);
- mutex_lock(&ipqnl_mutex);
- mutex_unlock(&ipqnl_mutex);
-
- cleanup_netlink_notifier:
- netlink_unregister_notifier(&ipq_nl_notifier);
- return status;
- }
IP Queue模块的初始化函数重要的两个工作:
(1)创建用于IP Queue的netlink socket,实现接收用户态的数据的函数ipq_rcv_sk();
(2)注册IP Queue报文的入队处理函数,并将数据包按照用户的配置将相关信息发向用户空间。当NF框架的hook函数对报文的处理返回NF_QUEUE时,该函数作为报文下一步被处理的入口函数。
模块初始化中还有一些其他诸如注册通知链的工作,这里我们不作具体分析
--未完待续