1/* Shared library add-on to iptables for the TTL target
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 *
4 * This program is distributed under the terms of GNU GPL
5 */
6#include <stdio.h>
7#include <xtables.h>
8#include <linux/netfilter_ipv4/ipt_TTL.h>
9
10enum {
11	O_TTL_SET = 0,
12	O_TTL_INC,
13	O_TTL_DEC,
14	F_TTL_SET = 1 << O_TTL_SET,
15	F_TTL_INC = 1 << O_TTL_INC,
16	F_TTL_DEC = 1 << O_TTL_DEC,
17	F_ANY     = F_TTL_SET | F_TTL_INC | F_TTL_DEC,
18};
19
20#define s struct ipt_TTL_info
21static const struct xt_option_entry TTL_opts[] = {
22	{.name = "ttl-set", .type = XTTYPE_UINT8, .id = O_TTL_SET,
23	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
24	{.name = "ttl-dec", .type = XTTYPE_UINT8, .id = O_TTL_DEC,
25	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
26	 .min = 1},
27	{.name = "ttl-inc", .type = XTTYPE_UINT8, .id = O_TTL_INC,
28	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
29	 .min = 1},
30	XTOPT_TABLEEND,
31};
32#undef s
33
34static void TTL_help(void)
35{
36	printf(
37"TTL target options\n"
38"  --ttl-set value		Set TTL to <value 0-255>\n"
39"  --ttl-dec value		Decrement TTL by <value 1-255>\n"
40"  --ttl-inc value		Increment TTL by <value 1-255>\n");
41}
42
43static void TTL_parse(struct xt_option_call *cb)
44{
45	struct ipt_TTL_info *info = cb->data;
46
47	xtables_option_parse(cb);
48	switch (cb->entry->id) {
49	case O_TTL_SET:
50		info->mode = IPT_TTL_SET;
51		break;
52	case O_TTL_DEC:
53		info->mode = IPT_TTL_DEC;
54		break;
55	case O_TTL_INC:
56		info->mode = IPT_TTL_INC;
57		break;
58	}
59}
60
61static void TTL_check(struct xt_fcheck_call *cb)
62{
63	if (!(cb->xflags & F_ANY))
64		xtables_error(PARAMETER_PROBLEM,
65				"TTL: You must specify an action");
66}
67
68static void TTL_save(const void *ip, const struct xt_entry_target *target)
69{
70	const struct ipt_TTL_info *info =
71		(struct ipt_TTL_info *) target->data;
72
73	switch (info->mode) {
74		case IPT_TTL_SET:
75			printf(" --ttl-set");
76			break;
77		case IPT_TTL_DEC:
78			printf(" --ttl-dec");
79			break;
80
81		case IPT_TTL_INC:
82			printf(" --ttl-inc");
83			break;
84	}
85	printf(" %u", info->ttl);
86}
87
88static void TTL_print(const void *ip, const struct xt_entry_target *target,
89                      int numeric)
90{
91	const struct ipt_TTL_info *info =
92		(struct ipt_TTL_info *) target->data;
93
94	printf(" TTL ");
95	switch (info->mode) {
96		case IPT_TTL_SET:
97			printf("set to");
98			break;
99		case IPT_TTL_DEC:
100			printf("decrement by");
101			break;
102		case IPT_TTL_INC:
103			printf("increment by");
104			break;
105	}
106	printf(" %u", info->ttl);
107}
108
109static struct xtables_target ttl_tg_reg = {
110	.name		= "TTL",
111	.version	= XTABLES_VERSION,
112	.family		= NFPROTO_IPV4,
113	.size		= XT_ALIGN(sizeof(struct ipt_TTL_info)),
114	.userspacesize	= XT_ALIGN(sizeof(struct ipt_TTL_info)),
115	.help		= TTL_help,
116	.print		= TTL_print,
117	.save		= TTL_save,
118	.x6_parse	= TTL_parse,
119	.x6_fcheck	= TTL_check,
120	.x6_options	= TTL_opts,
121};
122
123void _init(void)
124{
125	xtables_register_target(&ttl_tg_reg);
126}
127