Netfilter的使用和实现
本文主要内容:Netfilter的原理和实现浅析,以及示例模块。
内核版本:2.6.37
Author:zhangskd @ csdn blog
概述
Netfilter为多种网络协议(IPv4、IPv6、ARP等)各提供了一套钩子函数。
在IPv4中定义了5个钩子函数,这些钩子函数在数据包流经协议栈的5个关键点被调用。
这就像有5个钓鱼台,在每个钓鱼台放了一个鱼钩(钩子函数),把经过的数据包钓上来,
然后根据自定义的规则,来决定数据包的命运:
可以原封不动的放回IPv4协议,继续向上层递交;可以进行修改,再放回IPv4协议;也可以直接丢弃。
Netfilter主要采用连接跟踪(Connection Tracking)、包过滤(Packet Filtering)、地址转换(NAT)、包处理
(Packet Mangling)四种技术。
(1) IP层的5个钓鱼台
- enum nf_inet_hooks {
- NF_INET_PRE_ROUTING,
- NF_INET_LOCAL_IN,
- NF_INET_FORWARD,
- NF_INET_LOCAL_OUT,
- NF_INET_POST_ROUTING,
- NF_INET_NUMHOOKS
- };
支持的协议类型:
- enum {
- NFPROTO_UNSPEC = 0,
- NFPROTO_IPV4 = 2,
- NFPROTO_ARP = 3,
- NFPROTO_BRIDGE = 7,
- NFPROTO_IPV6 = 10,
- NFPROTO_DECNET = 12,
- NFPROTO_NUMPROTO,
- };
(2) 钩子函数
- typedef unsigned int nf_hookfn(unsigned int hooknum,
- struct sk_buff *skb,
- const struct net_device *in,
- const struct net_device *out,
- int (*okfn) (struct sk_buff *));
- /* 处理函数返回值 */
- #define NF_DROP 0 /* drop the packet, don't continue traversal */
- #define NF_ACCEPT 1 /* continue traversal as normal */
- #define NF_STOLEN 2 /* I've taken over the packet, don't continue traversal */
- #define NF_QUEUE 3 /* queue the packet (usually for userspace handling) */
- #define NF_REPEAT 4 /* call this hook again */
- #define NF_STOP 5
- #define NF_MAX_VERDICT NF_STOP
(3) Netfilter实体
在使用Netfilter时,需要定义一个nf_hook_ops实例。
- struct nf_hook_ops {
- struct list_head list;
- /* User fills in from here down. */
- nf_hookfn *hook; /* 要注册的钩子函数 */
- struct module *owner;
- u_int8_t pf; /* 协议类型 */
- unsigned int hooknum; /* 哪个钓鱼台 */
- /* Hooks are ordered in asending priority. */
- int priority; /* 数值越小,优先级越高 */
- };
- typedef __u8 u_int8_t;
(4) 注册与注销
- /* Functions to register/unregister hook points. */
- int nf_register_hook(struct nf_hook_ops *reg);
- void nf_unregister_hook(struct nf_hook_ops *reg);
实现
Netfilter定义了一个全局链表:
- struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
- EXPORT_SYMBOL(nf_hooks);
- static DEFINE_MUTEX(nf_hook_mutex);
(1) 注册函数
注册函数会把nf_hook_ops放入nf_hooks相应的位置中。
- int nf_register_hook(struct nf_hook_ops *reg)
- {
- struct nf_hook_ops *elem;
- int err;
- err = mutex_lock_interruptible(&nf_hook_mutex);
- if (err < 0)
- return err;
- list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
- if (reg->priority < elem->priority)
- break;
- }
- list_add_rcu(®->list, elem->list.prev); /* 把netfilter实例添加到队列中 */
- mutex_unlock(&nf_hook_mutex);
- return 0;
- }
(2) 注销函数
- void nf_unregister_hook(struct nf_hook_ops *reg)
- {
- mutex_lock(&nf_hook_mutex);
- list_del_rcu(®->list); /* 把netfilter实例从队列中删除 */
- mutex_unlock(&nf_hook_mutex);
- synchronize_net();
- }
(3) 内核接口
内核的Netfilter钩子函数调用:
NF_HOOK
|--> NF_HOOK_THRESH
|--> nf_hook_thresh
|--> nf_hook_slow
|--> nf_iterate
- static inline int NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
- struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *))
- {
- /* INT_MIN表示要调用钓鱼台的所有钩子函数 */
- return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
- }
- static inline int NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
- struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *), int thresh)
- {
- int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
- if (ret == 1)
- ret = okfn(skb); /* 如果skb没被处理掉,调用此函数 */
- return ret;
- }
- /**
- * nf_hook_thresh - call a netfilter hook
- * Returns 1 if the hook has allowed the packet to pass.
- * The function okfn must be invoked by the caller in this case.
- * Any other return value indicates the packet has been consumed by the hook.
- */
- static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
- struct net_device *indev, struct net_device *outdev, int (*okfn)(struct sk_buff *), int thresh)
- {
- #ifndef CONFIG_NETFILTER_DEBUG
- /* 如果协议pf的hook点上没有已注册的nf_hook_ops实例,直接返回1 */
- if (list_empty(&nf_hooks[pf][hook]))
- return 1;
- #endif
- return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
- }
- /* Returns 1 if okfn() needs to be executed by the caller, -EPERM for NF_DROP, 0 otherwise. */
- int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, struct net_device *indev,
- struct net_device *outdev, int (*okfn)(struct sk_buff *), int hook_thresh)
- {
- struct list_head *elem;
- unsigned int verdict;
- int ret = 0;
- /* We may already have this, but read-locks nest anyway */
- rcu_read_lock();
- elem = &nf_hooks[pf][hook];
- next_hook:
- verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, outdev, &elem, okfn, hook_thresh);
- if (verdict == NF_ACCEPT || verdict == NF_STOP) {
- ret = 1;
- } else if (verdict == NF_DROP) {
- kfree_skb(skb);
- ret = -EPERM;
- } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
- if (! nf_queue(skb, elem, ph, hook, indev, outdev, okfn, verdict >> NF_VERDICT_BITS))
- goto next_hook;
- }
- rcu_read_unlock();
- return ret;
- }
- unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb, unsigned int hook,
- const struct net_device *indev, const struct net_device *outdev, struct list_head **i,
- int (*okfn)(struct sk_buff *), int hook_thresh)
- {
- unsigned int verdict;
- /*
- * The caller must not block between calls to this function because of risk of
- * continuing from deleted element.
- */
- list_for_each_continue_rcu(*i, head) {
- struct nf_hook_ops *elem = (struct nf_hook_ops *) *i;
- /* 优先级>=hook_thresh的都会被执行 */
- if (hook_thresh > elem_priority)
- continue;
- verdict = elem->hook(hook, skb, indev, outdev, okfn); /* 已注册的执行函数 */
- if (verdict != NF_ACCEPT) {
- #ifdef CONFIG_NETFILTER_DEBUG
- if (unlikely((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT)) {
- NFDEBUG("Evil return from %p(%u).\n", elem->hook, hook);
- continue;
- }
- #endif
- if (verdict != NF_REPEAT)
- return verdict;
- *i = (*i)->prev;
- }
- }
- return NF_ACCEPT;
- }
使用
以下是一个简单的模块,加载到一个HTTP服务器上。
通过在PRE_ROUTING处注册my_hookfn,改变接收数据包的源IP为8.8.8.8(Google DNS server)。
当客户端向服务器发送一个请求时,肯定收不到服务器的响应:)
- #include <linux/netfilter.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/ip.h>
- #include <linux/inet.h>
- /**
- * Hook function to be called.
- * We modify the packet's src IP.
- */
- unsigned int my_hookfn(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;
- iph = ip_hdr(skb);
- /* log the original src IP */
- printk(KERN_INFO"src IP %pI4\n", &iph->saddr);
- /* modify the packet's src IP */
- iph->saddr = in_aton("8.8.8.8");
- return NF_ACCEPT;
- }
- /* A netfilter instance to use */
- static struct nf_hook_ops nfho = {
- .hook = my_hookfn,
- .pf = PF_INET,
- .hooknum = NF_INET_PRE_ROUTING,
- .priority = NF_IP_PRI_FIRST,
- .owner = THIS_MODULE,
- };
- static int __init sknf_init(void)
- {
- if (nf_register_hook(&nfho)) {
- printk(KERN_ERR"nf_register_hook() failed\n");
- return -1;
- }
- return 0;
- }
- static void __exit sknf_exit(void)
- {
- nf_unregister_hook(&nfho);
- }
- module_init(sknf_init);
- module_exit(sknf_exit);
- MODULE_AUTHOR("zhangsk");
- MODULE_LICENSE("GPL");
Netfilter的使用和实现相关推荐
- linux 防火墙 -netfilter
2019独角兽企业重金招聘Python工程师标准>>> 关于iptables 什么是iptables? 常见于linx系统下的应用层防火墙工具 firewalld 和netfilte ...
- netfilter的笔记3--那些内置的表
通过netfilter的笔记2的例子,我们知道了怎么使用netfilter的框架,对于内核的设计原则来说,策略和机制分离,所以提供了iptables来供用户配置防火墙策略. 那么,怎么使用iptabl ...
- Linux centos7 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法
一. Linux网络相关 yum install net-tools ifconfig查看网卡ip ifup ens33开启网卡 ifdown ens33关闭网卡 设定虚拟网卡ens33:0 mii- ...
- 基于linux的netfilter处理数据包的过程分析,基于Linux的Netfilter处理数据包的过程分析...
基于Linux的Netfilter处理数据包的过程分析 防火墙技术在保护网络安全方面的作用越来越明显.相比较window,Linux有更好的网络性能,因此基于Linux的Netfilter技术 (本文 ...
- 七周三次课(11月29日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...
2019独角兽企业重金招聘Python工程师标准>>> 10.11 Linux网络相关 ifconfig 查看网卡ip (yum install net-tools) 安装 -a ...
- Linux网络相关、firewalld、netfilter及其5表5链、iptables语法
2019独角兽企业重金招聘Python工程师标准>>> 1.Linux 网络相关 ifconfig 查看网卡IP,见下图, net-tools 包之前安装过了,这边还可以执行这个命令 ...
- Linux Kernel TCP/IP Stack — L3 Layer — netfilter 框架 — conntrack(CT,连接跟踪)
目录 文章目录 目录 CT CT CT(conntrack,connection tracking,连接跟踪),顾名思义,就是跟踪(并记录)连接的状态,是许多网络应用的基础.例如:iptables.L ...
- Linux Kernel TCP/IP Stack — L3 Layer — netfilter 框架
目录 文章目录 目录 netfilter 框架 netfilter 的组成模块 netfilter 的 Hook 机制实现 netfilter 的工作原理 规则(Rules) 链(Chains) 表( ...
- Linux Kernel TCP/IP Stack — L3 Layer — netfilter/iptables 防火墙
目录 文章目录 目录 iptables/netfilter 框架 iptables-service iptables 指令应用 查看规则 添加规则 删除规则 修改规则 保存和加载规则 常规初始化配置 ...
- Linux中netfilter模块编程实践
上篇我们看了netfilter的实现机制,这篇来实现下netfilter模块实操一把. 为了注册一个钩子,需要填充nf_hook_ops结构体,包括优先级,钩子地点和钩子函数.然后调用nf_regis ...
最新文章
- 关于文件保存/关闭时报错:文件正由另一进程使用,因此该进程无法访问此文件。...
- Elasticsearch 安装配置 外网访问 及 后台启动
- LeetCode MySQL 1527. Patients With a Condition(like)
- python opencv显示图片动态_opencv-python计算机视觉图像处理学习笔记2——打开图片,保存图片,显示图片...
- 创建数据库和表的SQL语句
- upload-labs19记录
- xticks函数--Matplotlib
- ipad编程软件c语言2020,App Store 上的“计算机等级考试C语言版 2020最新”
- MyBatis 传入参数之parameterType
- 如何自主搭建信息管理系统
- Windows Azure AppFabric (一) 平台简介
- html如何居中svg,垂直居中SVG标签
- 2.4-2.8段地址x16+偏移地址=物理地址
- 服务器磁盘配置信息,服务器磁盘阵列配置
- 计算机相关经典书籍推荐
- Spring的AOP(一):什么是AOP
- 一心多用多线程-Thread的api探险
- Win11怎么开启任务管理器 Win11任务管理器开启方法
- 真正内心强大的人是什么样子???
- linux-鸟哥私房菜,基础命令全掌握