160358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak/*
260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak * IPv6 Hop Limit Target module
360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak * Maciej Soltysiak <solt@dns.toxicfilms.tv>
460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak * Based on HW's ttl target
560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak * This program is distributed under the terms of GNU GPL
660358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak */
760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak#include <stdio.h>
85d9678ad3eabc34ac40dfe055d7f6a8e44445a5aJan Engelhardt#include <xtables.h>
960358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak#include <linux/netfilter_ipv6/ip6t_HL.h>
1060358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
11fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardtenum {
12fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	O_HL_SET = 0,
13fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	O_HL_INC,
14fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	O_HL_DEC,
15fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	F_HL_SET = 1 << O_HL_SET,
16fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	F_HL_INC = 1 << O_HL_INC,
17fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	F_HL_DEC = 1 << O_HL_DEC,
18fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	F_ANY    = F_HL_SET | F_HL_INC | F_HL_DEC,
19fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt};
20fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt
21fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt#define s struct ip6t_HL_info
22fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardtstatic const struct xt_option_entry HL_opts[] = {
23017e7b7e1cf4fb63208e46592d06cc030f6d552dJan Engelhardt	{.name = "hl-set", .type = XTTYPE_UINT8, .id = O_HL_SET,
24fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
25017e7b7e1cf4fb63208e46592d06cc030f6d552dJan Engelhardt	{.name = "hl-dec", .type = XTTYPE_UINT8, .id = O_HL_DEC,
26fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit),
27fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	 .min = 1},
28017e7b7e1cf4fb63208e46592d06cc030f6d552dJan Engelhardt	{.name = "hl-inc", .type = XTTYPE_UINT8, .id = O_HL_INC,
29fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit),
30fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	 .min = 1},
31fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	XTOPT_TABLEEND,
32fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt};
33fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt#undef s
3460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
354d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardtstatic void HL_help(void)
3660358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
3760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	printf(
388b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt"HL target options\n"
3946e8538e2254fcd48517067b659bcdc8ba1c3cc0Maciej Soltysiak"  --hl-set value		Set HL to <value 0-255>\n"
4046e8538e2254fcd48517067b659bcdc8ba1c3cc0Maciej Soltysiak"  --hl-dec value		Decrement HL by <value 1-255>\n"
418b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt"  --hl-inc value		Increment HL by <value 1-255>\n");
4260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
4360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
44fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardtstatic void HL_parse(struct xt_option_call *cb)
4560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
46fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	struct ip6t_HL_info *info = cb->data;
47fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt
48fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	xtables_option_parse(cb);
49fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	switch (cb->entry->id) {
50fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	case O_HL_SET:
51fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		info->mode = IP6T_HL_SET;
52fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		break;
53fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	case O_HL_INC:
54fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		info->mode = IP6T_HL_INC;
55fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		break;
56fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	case O_HL_DEC:
57fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		info->mode = IP6T_HL_DEC;
58fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt		break;
5960358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	}
6060358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
6160358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
62fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardtstatic void HL_check(struct xt_fcheck_call *cb)
6360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
64fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	if (!(cb->xflags & F_ANY))
651829ed482efbc8b390cc760d012b3a4450494e1aJan Engelhardt		xtables_error(PARAMETER_PROBLEM,
6660358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak				"HL: You must specify an action");
6760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
6860358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
694d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardtstatic void HL_save(const void *ip, const struct xt_entry_target *target)
7060358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
7160358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	const struct ip6t_HL_info *info =
7260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		(struct ip6t_HL_info *) target->data;
7360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
7460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	switch (info->mode) {
7560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_SET:
7673866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf(" --hl-set");
7760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
7860358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_DEC:
7973866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf(" --hl-dec");
8060358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
8160358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
8260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_INC:
8373866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf(" --hl-inc");
8460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
8560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	}
8673866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt	printf(" %u", info->hop_limit);
8760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
8860358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
894d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardtstatic void HL_print(const void *ip, const struct xt_entry_target *target,
904d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardt                     int numeric)
9160358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
9260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	const struct ip6t_HL_info *info =
9360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		(struct ip6t_HL_info *) target->data;
9460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
9573866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt	printf(" HL ");
9660358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	switch (info->mode) {
9760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_SET:
9873866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf("set to");
9960358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
10060358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_DEC:
10173866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf("decrement by");
10260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
10360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak		case IP6T_HL_INC:
10473866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt			printf("increment by");
10560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak			break;
10660358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak	}
10773866357e4a7a0fdc1b293bf8863fee2bd56da9eJan Engelhardt	printf(" %u", info->hop_limit);
10860358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
10960358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
1108b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardtstatic struct xtables_target hl_tg6_reg = {
111fcbab568c52643337d42e48a57b45be481db2e05Harald Welte	.name 		= "HL",
1128b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt	.version	= XTABLES_VERSION,
11303d99486d8283552705b58dc55b6085dffc38792Jan Engelhardt	.family		= NFPROTO_IPV6,
1148b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt	.size		= XT_ALIGN(sizeof(struct ip6t_HL_info)),
1158b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_HL_info)),
1164d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardt	.help		= HL_help,
1174d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardt	.print		= HL_print,
1184d150eb5934fd4343b5fac2419c994e2bf97a96cJan Engelhardt	.save		= HL_save,
119fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	.x6_parse	= HL_parse,
120fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	.x6_fcheck	= HL_check,
121fa728c88fd0bfdc3f2bdb79beed91cd9e1fca5e5Jan Engelhardt	.x6_options	= HL_opts,
12260358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak};
12360358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak
12460358d73482620aeafc34f38df36e462875fd244Maciej Soltysiakvoid _init(void)
12560358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak{
1268b7c64d6ba156a99008fcd810cba874c73294333Jan Engelhardt	xtables_register_target(&hl_tg6_reg);
12760358d73482620aeafc34f38df36e462875fd244Maciej Soltysiak}
128