ipt_ttl.c 1.6 KB
Newer Older
1
/* IP tables module for matching the value of the TTL
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
 *
 * (C) 2000,2001 by Harald Welte <laforge@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

10
#include <linux/ip.h>
L
Linus Torvalds 已提交
11 12 13 14
#include <linux/module.h>
#include <linux/skbuff.h>

#include <linux/netfilter_ipv4/ipt_ttl.h>
15
#include <linux/netfilter/x_tables.h>
L
Linus Torvalds 已提交
16 17 18 19 20

MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
MODULE_DESCRIPTION("IP tables TTL matching module");
MODULE_LICENSE("GPL");

21 22 23 24
static bool match(const struct sk_buff *skb,
		  const struct net_device *in, const struct net_device *out,
		  const struct xt_match *match, const void *matchinfo,
		  int offset, unsigned int protoff, bool *hotdrop)
L
Linus Torvalds 已提交
25 26
{
	const struct ipt_ttl_info *info = matchinfo;
27
	const u8 ttl = ip_hdr(skb)->ttl;
L
Linus Torvalds 已提交
28 29 30

	switch (info->mode) {
		case IPT_TTL_EQ:
31
			return ttl == info->ttl;
L
Linus Torvalds 已提交
32
		case IPT_TTL_NE:
33
			return ttl != info->ttl;
L
Linus Torvalds 已提交
34
		case IPT_TTL_LT:
35
			return ttl < info->ttl;
L
Linus Torvalds 已提交
36
		case IPT_TTL_GT:
37
			return ttl > info->ttl;
L
Linus Torvalds 已提交
38
		default:
39
			printk(KERN_WARNING "ipt_ttl: unknown mode %d\n",
L
Linus Torvalds 已提交
40
				info->mode);
41
			return false;
L
Linus Torvalds 已提交
42 43
	}

44
	return false;
L
Linus Torvalds 已提交
45 46
}

47
static struct xt_match ttl_match __read_mostly = {
L
Linus Torvalds 已提交
48
	.name		= "ttl",
49
	.family		= AF_INET,
50 51
	.match		= match,
	.matchsize	= sizeof(struct ipt_ttl_info),
L
Linus Torvalds 已提交
52 53 54
	.me		= THIS_MODULE,
};

55
static int __init ipt_ttl_init(void)
L
Linus Torvalds 已提交
56
{
57
	return xt_register_match(&ttl_match);
L
Linus Torvalds 已提交
58 59
}

60
static void __exit ipt_ttl_fini(void)
L
Linus Torvalds 已提交
61
{
62
	xt_unregister_match(&ttl_match);
L
Linus Torvalds 已提交
63 64
}

65 66
module_init(ipt_ttl_init);
module_exit(ipt_ttl_fini);