blob: ff76b74d024d2f1ff7f9f75ef0c98c827567d78b [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
Maciej Soltysiak60358d72003-01-08 09:14:20 +000015#include <linux/netfilter_ipv6/ip6t_hl.h>
16
Jan Engelhardt997045f2007-10-04 16:29:21 +000017static void hl_help(void)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"hl match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020021"[!] --hl-eq value Match hop limit value\n"
Maciej Soltysiak60358d72003-01-08 09:14:20 +000022" --hl-lt value Match HL < value\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023" --hl-gt value Match HL > value\n");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000024}
25
Jan Engelhardt997045f2007-10-04 16:29:21 +000026static int hl_parse(int c, char **argv, int invert, unsigned int *flags,
27 const void *entry, struct xt_entry_match **match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000028{
29 struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data;
30 u_int8_t value;
31
Jan Engelhardt0f16c722009-01-30 04:55:38 +010032 xtables_check_inverse(optarg, &invert, &optind, 0);
Maciej Soltysiak60358d72003-01-08 09:14:20 +000033 value = atoi(argv[optind-1]);
34
35 if (*flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010036 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000037 "Can't specify HL option twice");
38
39 if (!optarg)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010040 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000041 "hl: You must specify a value");
42 switch (c) {
43 case '2':
44 if (invert)
45 info->mode = IP6T_HL_NE;
46 else
47 info->mode = IP6T_HL_EQ;
48
49 /* is 0 allowed? */
50 info->hop_limit = value;
51 *flags = 1;
52
53 break;
54 case '3':
55 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010056 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000057 "hl: unexpected `!'");
58
59 info->mode = IP6T_HL_LT;
60 info->hop_limit = value;
61 *flags = 1;
62
63 break;
64 case '4':
65 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010066 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000067 "hl: unexpected `!'");
68
69 info->mode = IP6T_HL_GT;
70 info->hop_limit = value;
71 *flags = 1;
72
73 break;
74 default:
75 return 0;
Maciej Soltysiak60358d72003-01-08 09:14:20 +000076 }
77
78 return 1;
79}
80
Jan Engelhardt997045f2007-10-04 16:29:21 +000081static void hl_check(unsigned int flags)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000082{
83 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010084 xtables_error(PARAMETER_PROBLEM,
Maciej Soltysiak60358d72003-01-08 09:14:20 +000085 "HL match: You must specify one of "
Stephane Ouellette46525cd2003-05-07 20:08:36 +000086 "`--hl-eq', `--hl-lt', `--hl-gt'");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000087}
88
Jan Engelhardt997045f2007-10-04 16:29:21 +000089static void hl_print(const void *ip, const struct xt_entry_match *match,
90 int numeric)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000091{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020092 static const char *const op[] = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +000093 [IP6T_HL_EQ] = "==",
94 [IP6T_HL_NE] = "!=",
95 [IP6T_HL_LT] = "<",
96 [IP6T_HL_GT] = ">" };
97
Maciej Soltysiak60358d72003-01-08 09:14:20 +000098 const struct ip6t_hl_info *info =
99 (struct ip6t_hl_info *) match->data;
100
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000101 printf("HL match HL %s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000102}
103
Jan Engelhardt997045f2007-10-04 16:29:21 +0000104static void hl_save(const void *ip, const struct xt_entry_match *match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000105{
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100106 static const char *const op[] = {
107 [IP6T_HL_EQ] = "--hl-eq",
108 [IP6T_HL_NE] = "! --hl-eq",
109 [IP6T_HL_LT] = "--hl-lt",
110 [IP6T_HL_GT] = "--hl-gt" };
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000111
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000112 const struct ip6t_hl_info *info =
113 (struct ip6t_hl_info *) match->data;
114
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100115 printf("%s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000116}
117
Jan Engelhardt997045f2007-10-04 16:29:21 +0000118static const struct option hl_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000119 { .name = "hl", .has_arg = 1, .val = '2' },
120 { .name = "hl-eq", .has_arg = 1, .val = '2' },
121 { .name = "hl-lt", .has_arg = 1, .val = '3' },
122 { .name = "hl-gt", .has_arg = 1, .val = '4' },
Max Kellermann9ee386a2008-01-29 13:48:05 +0000123 { .name = NULL }
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000124};
125
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200126static struct xtables_match hl_mt6_reg = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000127 .name = "hl",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200128 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100129 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200130 .size = XT_ALIGN(sizeof(struct ip6t_hl_info)),
131 .userspacesize = XT_ALIGN(sizeof(struct ip6t_hl_info)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000132 .help = hl_help,
133 .parse = hl_parse,
134 .final_check = hl_check,
135 .print = hl_print,
136 .save = hl_save,
137 .extra_opts = hl_opts,
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000138};
139
140
141void _init(void)
142{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200143 xtables_register_match(&hl_mt6_reg);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000144}