libip6t_HL.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall/*
2437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall * IPv6 Hop Limit Target module
3437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall * Maciej Soltysiak <solt@dns.toxicfilms.tv>
4437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall * Based on HW's ttl target
5437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall * This program is distributed under the terms of GNU GPL
6437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall */
70fbdf6cbd4dfc633454eba2f841c123789e297adJaegeuk Kim
8437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <getopt.h>
9437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <stdbool.h>
10437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <stdio.h>
11437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <string.h>
12437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <stdlib.h>
134f267323a28c3fb50fa08256517e2dae4e347b2dJaegeuk Kim#include <xtables.h>
14437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
15437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#include <linux/netfilter_ipv6/ip6t_HL.h>
16437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
17437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall#define IP6T_HL_USED	1
18ba91378ec599572f0a47c44f4cd6a975a2f99653Sankalp Bose
19ba91378ec599572f0a47c44f4cd6a975a2f99653Sankalp Bosestatic void HL_help(void)
20ba91378ec599572f0a47c44f4cd6a975a2f99653Sankalp Bose{
21ba91378ec599572f0a47c44f4cd6a975a2f99653Sankalp Bose	printf(
22437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall"HL target options\n"
23437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall"  --hl-set value		Set HL to <value 0-255>\n"
24437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall"  --hl-dec value		Decrement HL by <value 1-255>\n"
25437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall"  --hl-inc value		Increment HL by <value 1-255>\n");
26437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall}
27437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
28437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrallstatic int HL_parse(int c, char **argv, int invert, unsigned int *flags,
29437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall                    const void *entry, struct xt_entry_target **target)
30437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall{
31437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall	struct ip6t_HL_info *info = (struct ip6t_HL_info *) (*target)->data;
32437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall	unsigned int value;
33437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
34437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall	if (*flags & IP6T_HL_USED) {
35437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall		xtables_error(PARAMETER_PROBLEM,
36437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall				"Can't specify HL option twice");
37437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall	}
38ba91378ec599572f0a47c44f4cd6a975a2f99653Sankalp Bose
39437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall	if (!optarg)
40437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall		xtables_error(PARAMETER_PROBLEM,
41437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall				"HL: You must specify a value");
42437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
4346bf13d5afbdd87d0cacaa06ec159cf9546c5726Jaegeuk Kim	if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
44437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall		xtables_error(PARAMETER_PROBLEM,
4546bf13d5afbdd87d0cacaa06ec159cf9546c5726Jaegeuk Kim				"HL: unexpected `!'");
4646bf13d5afbdd87d0cacaa06ec159cf9546c5726Jaegeuk Kim
4746bf13d5afbdd87d0cacaa06ec159cf9546c5726Jaegeuk Kim	if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX))
4846bf13d5afbdd87d0cacaa06ec159cf9546c5726Jaegeuk Kim		xtables_error(PARAMETER_PROBLEM,
49437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall		           "HL: Expected value between 0 and 255");
50437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
513c85e737308ef95629b232745d6a8d141d87cc9aJP Abgrall	switch (c) {
52437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall
53437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall		case '1':
54437dbf67730d2765c4dfc8539b07258e1ca3966fJP Abgrall			info->mode = IP6T_HL_SET;
55			break;
56
57		case '2':
58			if (value == 0) {
59				xtables_error(PARAMETER_PROBLEM,
60					"HL: decreasing by 0?");
61			}
62
63			info->mode = IP6T_HL_DEC;
64			break;
65
66		case '3':
67			if (value == 0) {
68				xtables_error(PARAMETER_PROBLEM,
69					"HL: increasing by 0?");
70			}
71
72			info->mode = IP6T_HL_INC;
73			break;
74
75		default:
76			return 0;
77
78	}
79
80	info->hop_limit = value;
81	*flags |= IP6T_HL_USED;
82
83	return 1;
84}
85
86static void HL_check(unsigned int flags)
87{
88	if (!(flags & IP6T_HL_USED))
89		xtables_error(PARAMETER_PROBLEM,
90				"HL: You must specify an action");
91}
92
93static void HL_save(const void *ip, const struct xt_entry_target *target)
94{
95	const struct ip6t_HL_info *info =
96		(struct ip6t_HL_info *) target->data;
97
98	switch (info->mode) {
99		case IP6T_HL_SET:
100			printf("--hl-set ");
101			break;
102		case IP6T_HL_DEC:
103			printf("--hl-dec ");
104			break;
105
106		case IP6T_HL_INC:
107			printf("--hl-inc ");
108			break;
109	}
110	printf("%u ", info->hop_limit);
111}
112
113static void HL_print(const void *ip, const struct xt_entry_target *target,
114                     int numeric)
115{
116	const struct ip6t_HL_info *info =
117		(struct ip6t_HL_info *) target->data;
118
119	printf("HL ");
120	switch (info->mode) {
121		case IP6T_HL_SET:
122			printf("set to ");
123			break;
124		case IP6T_HL_DEC:
125			printf("decrement by ");
126			break;
127		case IP6T_HL_INC:
128			printf("increment by ");
129			break;
130	}
131	printf("%u ", info->hop_limit);
132}
133
134static const struct option HL_opts[] = {
135	{.name = "hl-set", .has_arg = true, .val = '1'},
136	{.name = "hl-dec", .has_arg = true, .val = '2'},
137	{.name = "hl-inc", .has_arg = true, .val = '3'},
138	XT_GETOPT_TABLEEND,
139};
140
141static struct xtables_target hl_tg6_reg = {
142	.name 		= "HL",
143	.version	= XTABLES_VERSION,
144	.family		= NFPROTO_IPV6,
145	.size		= XT_ALIGN(sizeof(struct ip6t_HL_info)),
146	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_HL_info)),
147	.help		= HL_help,
148	.parse		= HL_parse,
149	.final_check	= HL_check,
150	.print		= HL_print,
151	.save		= HL_save,
152	.extra_opts	= HL_opts,
153};
154
155void _init(void)
156{
157	xtables_register_target(&hl_tg6_reg);
158}
159