blob: 4dfac063519636e66106e10982d9411958fe7ec6 [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>
12#include <iptables.h>
13
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(
22"TTL target v%s 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"
25" --ttl-inc value Increment TTL by <value 1-255>\n"
Harald Welte80fe35d2002-05-29 13:08:15 +000026, IPTABLES_VERSION);
Harald Welte703828f2000-10-04 15:27:07 +000027}
28
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000029static int TTL_parse(int c, char **argv, int invert, unsigned int *flags,
30 const void *entry, struct xt_entry_target **target)
Harald Welte703828f2000-10-04 15:27:07 +000031{
32 struct ipt_TTL_info *info = (struct ipt_TTL_info *) (*target)->data;
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000033 unsigned int value;
Harald Welte703828f2000-10-04 15:27:07 +000034
35 if (*flags & IPT_TTL_USED) {
36 exit_error(PARAMETER_PROBLEM,
37 "Can't specify TTL option twice");
38 }
39
40 if (!optarg)
41 exit_error(PARAMETER_PROBLEM,
42 "TTL: You must specify a value");
43
Harald Welteb77f1da2002-03-14 11:35:58 +000044 if (check_inverse(optarg, &invert, NULL, 0))
Harald Welte703828f2000-10-04 15:27:07 +000045 exit_error(PARAMETER_PROBLEM,
46 "TTL: unexpected `!'");
47
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000048 if (string_to_number(optarg, 0, 255, &value) == -1)
49 exit_error(PARAMETER_PROBLEM,
50 "TTL: Expected value between 0 and 255");
Harald Welte703828f2000-10-04 15:27:07 +000051
52 switch (c) {
53
54 case '1':
55 info->mode = IPT_TTL_SET;
56 break;
57
58 case '2':
59 if (value == 0) {
60 exit_error(PARAMETER_PROBLEM,
61 "TTL: decreasing by 0?");
62 }
63
64 info->mode = IPT_TTL_DEC;
65 break;
66
67 case '3':
68 if (value == 0) {
69 exit_error(PARAMETER_PROBLEM,
70 "TTL: increasing by 0?");
71 }
72
73 info->mode = IPT_TTL_INC;
74 break;
75
76 default:
77 return 0;
78
79 }
80
81 info->ttl = value;
82 *flags |= IPT_TTL_USED;
83
84 return 1;
85}
86
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000087static void TTL_check(unsigned int flags)
Harald Welte703828f2000-10-04 15:27:07 +000088{
89 if (!(flags & IPT_TTL_USED))
90 exit_error(PARAMETER_PROBLEM,
91 "TTL: You must specify an action");
92}
93
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000094static void TTL_save(const void *ip, const struct xt_entry_target *target)
Harald Welte703828f2000-10-04 15:27:07 +000095{
96 const struct ipt_TTL_info *info =
97 (struct ipt_TTL_info *) target->data;
98
99 switch (info->mode) {
100 case IPT_TTL_SET:
101 printf("--ttl-set ");
102 break;
103 case IPT_TTL_DEC:
104 printf("--ttl-dec ");
105 break;
106
107 case IPT_TTL_INC:
108 printf("--ttl-inc ");
109 break;
110 }
111 printf("%u ", info->ttl);
112}
113
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000114static void TTL_print(const void *ip, const struct xt_entry_target *target,
115 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +0000116{
117 const struct ipt_TTL_info *info =
118 (struct ipt_TTL_info *) target->data;
119
120 printf("TTL ");
121 switch (info->mode) {
122 case IPT_TTL_SET:
123 printf("set to ");
124 break;
125 case IPT_TTL_DEC:
126 printf("decrement by ");
127 break;
128 case IPT_TTL_INC:
129 printf("increment by ");
130 break;
131 }
132 printf("%u ", info->ttl);
133}
134
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000135static const struct option TTL_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000136 { "ttl-set", 1, NULL, '1' },
137 { "ttl-dec", 1, NULL, '2' },
138 { "ttl-inc", 1, NULL, '3' },
139 { }
Harald Welte703828f2000-10-04 15:27:07 +0000140};
141
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000142static struct iptables_target ttl_target = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000143 .next = NULL,
144 .name = "TTL",
145 .version = IPTABLES_VERSION,
146 .size = IPT_ALIGN(sizeof(struct ipt_TTL_info)),
147 .userspacesize = IPT_ALIGN(sizeof(struct ipt_TTL_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000148 .help = TTL_help,
149 .parse = TTL_parse,
150 .final_check = TTL_check,
151 .print = TTL_print,
152 .save = TTL_save,
153 .extra_opts = TTL_opts,
Harald Welte703828f2000-10-04 15:27:07 +0000154};
155
156void _init(void)
157{
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000158 register_target(&ttl_target);
Harald Welte703828f2000-10-04 15:27:07 +0000159}