blob: 87a1eeb7c61f1bee6ad267bdb3ddffde6133a4e5 [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>
13#include <ip6tables.h>
14
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(
21"HL match v%s options:\n"
Stephane Ouellette46525cd2003-05-07 20:08:36 +000022" --hl-eq [!] value Match hop limit value\n"
Maciej Soltysiak60358d72003-01-08 09:14:20 +000023" --hl-lt value Match HL < value\n"
24" --hl-gt value Match HL > value\n"
25, IPTABLES_VERSION);
26}
27
Jan Engelhardt997045f2007-10-04 16:29:21 +000028static int hl_parse(int c, char **argv, int invert, unsigned int *flags,
29 const void *entry, struct xt_entry_match **match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000030{
31 struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data;
32 u_int8_t value;
33
34 check_inverse(optarg, &invert, &optind, 0);
35 value = atoi(argv[optind-1]);
36
37 if (*flags)
38 exit_error(PARAMETER_PROBLEM,
39 "Can't specify HL option twice");
40
41 if (!optarg)
42 exit_error(PARAMETER_PROBLEM,
43 "hl: You must specify a value");
44 switch (c) {
45 case '2':
46 if (invert)
47 info->mode = IP6T_HL_NE;
48 else
49 info->mode = IP6T_HL_EQ;
50
51 /* is 0 allowed? */
52 info->hop_limit = value;
53 *flags = 1;
54
55 break;
56 case '3':
57 if (invert)
58 exit_error(PARAMETER_PROBLEM,
59 "hl: unexpected `!'");
60
61 info->mode = IP6T_HL_LT;
62 info->hop_limit = value;
63 *flags = 1;
64
65 break;
66 case '4':
67 if (invert)
68 exit_error(PARAMETER_PROBLEM,
69 "hl: unexpected `!'");
70
71 info->mode = IP6T_HL_GT;
72 info->hop_limit = value;
73 *flags = 1;
74
75 break;
76 default:
77 return 0;
Maciej Soltysiak60358d72003-01-08 09:14:20 +000078 }
79
80 return 1;
81}
82
Jan Engelhardt997045f2007-10-04 16:29:21 +000083static void hl_check(unsigned int flags)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000084{
85 if (!flags)
86 exit_error(PARAMETER_PROBLEM,
87 "HL match: You must specify one of "
Stephane Ouellette46525cd2003-05-07 20:08:36 +000088 "`--hl-eq', `--hl-lt', `--hl-gt'");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000089}
90
Jan Engelhardt997045f2007-10-04 16:29:21 +000091static void hl_print(const void *ip, const struct xt_entry_match *match,
92 int numeric)
Maciej Soltysiak60358d72003-01-08 09:14:20 +000093{
Stephane Ouellette46525cd2003-05-07 20:08:36 +000094 static const char *op[] = {
95 [IP6T_HL_EQ] = "==",
96 [IP6T_HL_NE] = "!=",
97 [IP6T_HL_LT] = "<",
98 [IP6T_HL_GT] = ">" };
99
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000100 const struct ip6t_hl_info *info =
101 (struct ip6t_hl_info *) match->data;
102
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000103 printf("HL match HL %s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000104}
105
Jan Engelhardt997045f2007-10-04 16:29:21 +0000106static void hl_save(const void *ip, const struct xt_entry_match *match)
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000107{
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000108 static const char *op[] = {
109 [IP6T_HL_EQ] = "eq",
110 [IP6T_HL_NE] = "eq !",
111 [IP6T_HL_LT] = "lt",
112 [IP6T_HL_GT] = "gt" };
113
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000114 const struct ip6t_hl_info *info =
115 (struct ip6t_hl_info *) match->data;
116
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000117 printf("--hl-%s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000118}
119
Jan Engelhardt997045f2007-10-04 16:29:21 +0000120static const struct option hl_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000121 { .name = "hl", .has_arg = 1, .val = '2' },
122 { .name = "hl-eq", .has_arg = 1, .val = '2' },
123 { .name = "hl-lt", .has_arg = 1, .val = '3' },
124 { .name = "hl-gt", .has_arg = 1, .val = '4' },
125 { }
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000126};
127
Jan Engelhardt997045f2007-10-04 16:29:21 +0000128static struct ip6tables_match hl_match6 = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000129 .name = "hl",
130 .version = IPTABLES_VERSION,
131 .size = IP6T_ALIGN(sizeof(struct ip6t_hl_info)),
132 .userspacesize = IP6T_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 Engelhardt997045f2007-10-04 16:29:21 +0000144 register_match6(&hl_match6);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000145}