]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/ipv4/netfilter/ipt_TTL.c
[NETFILTER]: Rename init functions.
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / netfilter / ipt_TTL.c
CommitLineData
5f2c3b91
HW
1/* TTL modification target for IP tables
2 * (C) 2000,2005 by Harald Welte <laforge@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13#include <net/checksum.h>
14
15#include <linux/netfilter_ipv4/ip_tables.h>
16#include <linux/netfilter_ipv4/ipt_TTL.h>
17
18MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
19MODULE_DESCRIPTION("IP tables TTL modification module");
20MODULE_LICENSE("GPL");
21
22static unsigned int
c4986734
PM
23ipt_ttl_target(struct sk_buff **pskb,
24 const struct net_device *in, const struct net_device *out,
25 unsigned int hooknum, const struct xt_target *target,
26 const void *targinfo, void *userinfo)
5f2c3b91
HW
27{
28 struct iphdr *iph;
29 const struct ipt_TTL_info *info = targinfo;
30 u_int16_t diffs[2];
31 int new_ttl;
32
33 if (!skb_make_writable(pskb, (*pskb)->len))
34 return NF_DROP;
35
36 iph = (*pskb)->nh.iph;
37
38 switch (info->mode) {
39 case IPT_TTL_SET:
40 new_ttl = info->ttl;
41 break;
42 case IPT_TTL_INC:
43 new_ttl = iph->ttl + info->ttl;
44 if (new_ttl > 255)
45 new_ttl = 255;
46 break;
47 case IPT_TTL_DEC:
48 new_ttl = iph->ttl - info->ttl;
49 if (new_ttl < 0)
50 new_ttl = 0;
51 break;
52 default:
53 new_ttl = iph->ttl;
54 break;
55 }
56
57 if (new_ttl != iph->ttl) {
58 diffs[0] = htons(((unsigned)iph->ttl) << 8) ^ 0xFFFF;
59 iph->ttl = new_ttl;
60 diffs[1] = htons(((unsigned)iph->ttl) << 8);
61 iph->check = csum_fold(csum_partial((char *)diffs,
62 sizeof(diffs),
63 iph->check^0xFFFF));
64 }
65
66 return IPT_CONTINUE;
67}
68
69static int ipt_ttl_checkentry(const char *tablename,
2e4e6a17 70 const void *e,
c4986734 71 const struct xt_target *target,
5f2c3b91
HW
72 void *targinfo,
73 unsigned int targinfosize,
74 unsigned int hook_mask)
75{
76 struct ipt_TTL_info *info = targinfo;
77
5f2c3b91
HW
78 if (info->mode > IPT_TTL_MAXMODE) {
79 printk(KERN_WARNING "ipt_TTL: invalid or unknown Mode %u\n",
80 info->mode);
81 return 0;
82 }
5f2c3b91
HW
83 if ((info->mode != IPT_TTL_SET) && (info->ttl == 0))
84 return 0;
5f2c3b91
HW
85 return 1;
86}
87
88static struct ipt_target ipt_TTL = {
89 .name = "TTL",
90 .target = ipt_ttl_target,
1d5cd909
PM
91 .targetsize = sizeof(struct ipt_TTL_info),
92 .table = "mangle",
5f2c3b91
HW
93 .checkentry = ipt_ttl_checkentry,
94 .me = THIS_MODULE,
95};
96
65b4b4e8 97static int __init ipt_ttl_init(void)
5f2c3b91
HW
98{
99 return ipt_register_target(&ipt_TTL);
100}
101
65b4b4e8 102static void __exit ipt_ttl_fini(void)
5f2c3b91
HW
103{
104 ipt_unregister_target(&ipt_TTL);
105}
106
65b4b4e8
AM
107module_init(ipt_ttl_init);
108module_exit(ipt_ttl_fini);