blob: 2280e03eb606cbd29ce4c354fed9aca59da066a7 [file] [log] [blame]
Maciej Soltysiak60358d72003-01-08 09:14:20 +00001/*
2 * IPv6 Hop Limit matching module
3 * Maciej Soltysiak <solt@dns.toxicfilms.tv>
4 * Based on HW's ttl match
5 * This program is released under the terms of GNU GPL
Stephane Ouellette46525cd2003-05-07 20:08:36 +00006 * Cleanups by Stephane Ouellette <ouellettes@videotron.ca>
Maciej Soltysiak60358d72003-01-08 09:14:20 +00007 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010013#include <xtables.h>
Maciej Soltysiak60358d72003-01-08 09:14:20 +000014
15#include <linux/netfilter_ipv6/ip6_tables.h>
16#include <linux/netfilter_ipv6/ip6t_hl.h>
17
Jan Engelhardt997045f2007-10-04 16:29:21 +000018static void hl_help(void)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000019{
20 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021"hl match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020022"[!] --hl-eq value Match hop limit value\n"
Maciej Soltysiak60358d72003-01-08 09:14:20 +000023" --hl-lt value Match HL < value\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020024" --hl-gt value Match HL > value\n");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000025}
26
Jan Engelhardt997045f2007-10-04 16:29:21 +000027static int hl_parse(int c, char **argv, int invert, unsigned int *flags,
28 const void *entry, struct xt_entry_match **match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000029{
30 struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data;
31 u_int8_t value;
32
Jan Engelhardt0f16c722009-01-30 04:55:38 +010033 xtables_check_inverse(optarg, &invert, &optind, 0);
Maciej Soltysiak60358d72003-01-08 09:14:20 +000034 value = atoi(argv[optind-1]);
35
36 if (*flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010037 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000038 "Can't specify HL option twice");
39
40 if (!optarg)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010041 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000042 "hl: You must specify a value");
43 switch (c) {
44 case '2':
45 if (invert)
46 info->mode = IP6T_HL_NE;
47 else
48 info->mode = IP6T_HL_EQ;
49
50 /* is 0 allowed? */
51 info->hop_limit = value;
52 *flags = 1;
53
54 break;
55 case '3':
56 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010057 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000058 "hl: unexpected `!'");
59
60 info->mode = IP6T_HL_LT;
61 info->hop_limit = value;
62 *flags = 1;
63
64 break;
65 case '4':
66 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010067 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000068 "hl: unexpected `!'");
69
70 info->mode = IP6T_HL_GT;
71 info->hop_limit = value;
72 *flags = 1;
73
74 break;
75 default:
76 return 0;
Maciej Soltysiak60358d72003-01-08 09:14:20 +000077 }
78
79 return 1;
80}
81
Jan Engelhardt997045f2007-10-04 16:29:21 +000082static void hl_check(unsigned int flags)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000083{
84 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010085 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000086 "HL match: You must specify one of "
Stephane Ouellette46525cd2003-05-07 20:08:36 +000087 "`--hl-eq', `--hl-lt', `--hl-gt'");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000088}
89
Jan Engelhardt997045f2007-10-04 16:29:21 +000090static void hl_print(const void *ip, const struct xt_entry_match *match,
91 int numeric)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000092{
Stephane Ouellette46525cd2003-05-07 20:08:36 +000093 static const char *op[] = {
94 [IP6T_HL_EQ] = "==",
95 [IP6T_HL_NE] = "!=",
96 [IP6T_HL_LT] = "<",
97 [IP6T_HL_GT] = ">" };
98
Maciej Soltysiak60358d72003-01-08 09:14:20 +000099 const struct ip6t_hl_info *info =
100 (struct ip6t_hl_info *) match->data;
101
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000102 printf("HL match HL %s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000103}
104
Jan Engelhardt997045f2007-10-04 16:29:21 +0000105static void hl_save(const void *ip, const struct xt_entry_match *match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000106{
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100107 static const char *const op[] = {
108 [IP6T_HL_EQ] = "--hl-eq",
109 [IP6T_HL_NE] = "! --hl-eq",
110 [IP6T_HL_LT] = "--hl-lt",
111 [IP6T_HL_GT] = "--hl-gt" };
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000112
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000113 const struct ip6t_hl_info *info =
114 (struct ip6t_hl_info *) match->data;
115
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100116 printf("%s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000117}
118
Jan Engelhardt997045f2007-10-04 16:29:21 +0000119static const struct option hl_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000120 { .name = "hl", .has_arg = 1, .val = '2' },
121 { .name = "hl-eq", .has_arg = 1, .val = '2' },
122 { .name = "hl-lt", .has_arg = 1, .val = '3' },
123 { .name = "hl-gt", .has_arg = 1, .val = '4' },
Max Kellermann9ee386a2008-01-29 13:48:05 +0000124 { .name = NULL }
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000125};
126
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200127static struct xtables_match hl_mt6_reg = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000128 .name = "hl",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200129 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100130 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200131 .size = XT_ALIGN(sizeof(struct ip6t_hl_info)),
132 .userspacesize = XT_ALIGN(sizeof(struct ip6t_hl_info)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000133 .help = hl_help,
134 .parse = hl_parse,
135 .final_check = hl_check,
136 .print = hl_print,
137 .save = hl_save,
138 .extra_opts = hl_opts,
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000139};
140
141
142void _init(void)
143{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200144 xtables_register_match(&hl_mt6_reg);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000145}