blob: 15d23ba2b7d9c6db61c7cfc7bcf4ff370647100a [file] [log] [blame]
Harald Welte703828f2000-10-04 15:27:07 +00001/* Shared library add-on to iptables for the TTL target
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 *
Pablo Neira8caee8b2004-12-28 13:11:59 +00004 * $Id$
Harald Welte703828f2000-10-04 15:27:07 +00005 *
6 * This program is distributed under the terms of GNU GPL
7 */
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010012#include <xtables.h>
Harald Welte703828f2000-10-04 15:27:07 +000013
14#include <linux/netfilter_ipv4/ip_tables.h>
15#include <linux/netfilter_ipv4/ipt_TTL.h>
16
17#define IPT_TTL_USED 1
18
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000019static void TTL_help(void)
Harald Welte703828f2000-10-04 15:27:07 +000020{
21 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022"TTL target options\n"
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000023" --ttl-set value Set TTL to <value 0-255>\n"
24" --ttl-dec value Decrement TTL by <value 1-255>\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025" --ttl-inc value Increment TTL by <value 1-255>\n");
Harald Welte703828f2000-10-04 15:27:07 +000026}
27
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000028static int TTL_parse(int c, char **argv, int invert, unsigned int *flags,
29 const void *entry, struct xt_entry_target **target)
Harald Welte703828f2000-10-04 15:27:07 +000030{
31 struct ipt_TTL_info *info = (struct ipt_TTL_info *) (*target)->data;
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000032 unsigned int value;
Harald Welte703828f2000-10-04 15:27:07 +000033
34 if (*flags & IPT_TTL_USED) {
35 exit_error(PARAMETER_PROBLEM,
36 "Can't specify TTL option twice");
37 }
38
39 if (!optarg)
40 exit_error(PARAMETER_PROBLEM,
41 "TTL: You must specify a value");
42
Jan Engelhardt0f16c722009-01-30 04:55:38 +010043 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Harald Welte703828f2000-10-04 15:27:07 +000044 exit_error(PARAMETER_PROBLEM,
45 "TTL: unexpected `!'");
46
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010047 if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX))
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000048 exit_error(PARAMETER_PROBLEM,
49 "TTL: Expected value between 0 and 255");
Harald Welte703828f2000-10-04 15:27:07 +000050
51 switch (c) {
52
53 case '1':
54 info->mode = IPT_TTL_SET;
55 break;
56
57 case '2':
58 if (value == 0) {
59 exit_error(PARAMETER_PROBLEM,
60 "TTL: decreasing by 0?");
61 }
62
63 info->mode = IPT_TTL_DEC;
64 break;
65
66 case '3':
67 if (value == 0) {
68 exit_error(PARAMETER_PROBLEM,
69 "TTL: increasing by 0?");
70 }
71
72 info->mode = IPT_TTL_INC;
73 break;
74
75 default:
76 return 0;
77
78 }
79
80 info->ttl = value;
81 *flags |= IPT_TTL_USED;
82
83 return 1;
84}
85
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000086static void TTL_check(unsigned int flags)
Harald Welte703828f2000-10-04 15:27:07 +000087{
88 if (!(flags & IPT_TTL_USED))
89 exit_error(PARAMETER_PROBLEM,
90 "TTL: You must specify an action");
91}
92
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000093static void TTL_save(const void *ip, const struct xt_entry_target *target)
Harald Welte703828f2000-10-04 15:27:07 +000094{
95 const struct ipt_TTL_info *info =
96 (struct ipt_TTL_info *) target->data;
97
98 switch (info->mode) {
99 case IPT_TTL_SET:
100 printf("--ttl-set ");
101 break;
102 case IPT_TTL_DEC:
103 printf("--ttl-dec ");
104 break;
105
106 case IPT_TTL_INC:
107 printf("--ttl-inc ");
108 break;
109 }
110 printf("%u ", info->ttl);
111}
112
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000113static void TTL_print(const void *ip, const struct xt_entry_target *target,
114 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +0000115{
116 const struct ipt_TTL_info *info =
117 (struct ipt_TTL_info *) target->data;
118
119 printf("TTL ");
120 switch (info->mode) {
121 case IPT_TTL_SET:
122 printf("set to ");
123 break;
124 case IPT_TTL_DEC:
125 printf("decrement by ");
126 break;
127 case IPT_TTL_INC:
128 printf("increment by ");
129 break;
130 }
131 printf("%u ", info->ttl);
132}
133
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000134static const struct option TTL_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000135 { "ttl-set", 1, NULL, '1' },
136 { "ttl-dec", 1, NULL, '2' },
137 { "ttl-inc", 1, NULL, '3' },
Max Kellermann9ee386a2008-01-29 13:48:05 +0000138 { .name = NULL }
Harald Welte703828f2000-10-04 15:27:07 +0000139};
140
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200141static struct xtables_target ttl_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000142 .name = "TTL",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200143 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100144 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200145 .size = XT_ALIGN(sizeof(struct ipt_TTL_info)),
146 .userspacesize = XT_ALIGN(sizeof(struct ipt_TTL_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000147 .help = TTL_help,
148 .parse = TTL_parse,
149 .final_check = TTL_check,
150 .print = TTL_print,
151 .save = TTL_save,
152 .extra_opts = TTL_opts,
Harald Welte703828f2000-10-04 15:27:07 +0000153};
154
155void _init(void)
156{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200157 xtables_register_target(&ttl_tg_reg);
Harald Welte703828f2000-10-04 15:27:07 +0000158}