blob: 208da33f0ba3c628e404a6c1f4e8728271ee70bf [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
18static void help(void)
19{
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
Maciej Soltysiak60358d72003-01-08 09:14:20 +000028static int parse(int c, char **argv, int invert, unsigned int *flags,
29 const struct ip6t_entry *entry, unsigned int *nfcache,
30 struct ip6t_entry_match **match)
31{
32 struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data;
33 u_int8_t value;
34
35 check_inverse(optarg, &invert, &optind, 0);
36 value = atoi(argv[optind-1]);
37
38 if (*flags)
39 exit_error(PARAMETER_PROBLEM,
40 "Can't specify HL option twice");
41
42 if (!optarg)
43 exit_error(PARAMETER_PROBLEM,
44 "hl: You must specify a value");
45 switch (c) {
46 case '2':
47 if (invert)
48 info->mode = IP6T_HL_NE;
49 else
50 info->mode = IP6T_HL_EQ;
51
52 /* is 0 allowed? */
53 info->hop_limit = value;
54 *flags = 1;
55
56 break;
57 case '3':
58 if (invert)
59 exit_error(PARAMETER_PROBLEM,
60 "hl: unexpected `!'");
61
62 info->mode = IP6T_HL_LT;
63 info->hop_limit = value;
64 *flags = 1;
65
66 break;
67 case '4':
68 if (invert)
69 exit_error(PARAMETER_PROBLEM,
70 "hl: unexpected `!'");
71
72 info->mode = IP6T_HL_GT;
73 info->hop_limit = value;
74 *flags = 1;
75
76 break;
77 default:
78 return 0;
Maciej Soltysiak60358d72003-01-08 09:14:20 +000079 }
80
81 return 1;
82}
83
84static void final_check(unsigned int flags)
85{
86 if (!flags)
87 exit_error(PARAMETER_PROBLEM,
88 "HL match: You must specify one of "
Stephane Ouellette46525cd2003-05-07 20:08:36 +000089 "`--hl-eq', `--hl-lt', `--hl-gt'");
Maciej Soltysiak60358d72003-01-08 09:14:20 +000090}
91
92static void print(const struct ip6t_ip6 *ip,
93 const struct ip6t_entry_match *match,
94 int numeric)
95{
Stephane Ouellette46525cd2003-05-07 20:08:36 +000096 static const char *op[] = {
97 [IP6T_HL_EQ] = "==",
98 [IP6T_HL_NE] = "!=",
99 [IP6T_HL_LT] = "<",
100 [IP6T_HL_GT] = ">" };
101
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000102 const struct ip6t_hl_info *info =
103 (struct ip6t_hl_info *) match->data;
104
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000105 printf("HL match HL %s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000106}
107
108static void save(const struct ip6t_ip6 *ip,
109 const struct ip6t_entry_match *match)
110{
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000111 static const char *op[] = {
112 [IP6T_HL_EQ] = "eq",
113 [IP6T_HL_NE] = "eq !",
114 [IP6T_HL_LT] = "lt",
115 [IP6T_HL_GT] = "gt" };
116
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000117 const struct ip6t_hl_info *info =
118 (struct ip6t_hl_info *) match->data;
119
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000120 printf("--hl-%s %u ", op[info->mode], info->hop_limit);
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000121}
122
123static struct option opts[] = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000124 { .name = "hl", .has_arg = 1, .flag = 0, .val = '2' },
125 { .name = "hl-eq", .has_arg = 1, .flag = 0, .val = '2' },
126 { .name = "hl-lt", .has_arg = 1, .flag = 0, .val = '3' },
127 { .name = "hl-gt", .has_arg = 1, .flag = 0, .val = '4' },
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000128 { 0 }
129};
130
131static
132struct ip6tables_match hl = {
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000133 .name = "hl",
134 .version = IPTABLES_VERSION,
135 .size = IP6T_ALIGN(sizeof(struct ip6t_hl_info)),
136 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_hl_info)),
137 .help = &help,
Stephane Ouellette46525cd2003-05-07 20:08:36 +0000138 .parse = &parse,
139 .final_check = &final_check,
140 .print = &print,
141 .save = &save,
142 .extra_opts = opts
Maciej Soltysiak60358d72003-01-08 09:14:20 +0000143};
144
145
146void _init(void)
147{
148 register_match6(&hl);
149}