changing trunk/trunk to trunk
[iptables.git] / extensions / libip6t_HL.c
1 /*
2  * IPv6 Hop Limit Target module
3  * Maciej Soltysiak <solt@dns.toxicfilms.tv>
4  * Based on HW's ttl target
5  * This program is distributed under the terms of GNU GPL
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12 #include <ip6tables.h>
13
14 #include <linux/netfilter_ipv6/ip6_tables.h>
15 #include <linux/netfilter_ipv6/ip6t_HL.h>
16
17 #define IP6T_HL_USED    1
18
19 static void HL_help(void)
20 {
21         printf(
22 "HL target options\n"
23 "  --hl-set value               Set HL to <value 0-255>\n"
24 "  --hl-dec value               Decrement HL by <value 1-255>\n"
25 "  --hl-inc value               Increment HL by <value 1-255>\n");
26 }
27
28 static int HL_parse(int c, char **argv, int invert, unsigned int *flags,
29                     const void *entry, struct xt_entry_target **target)
30 {
31         struct ip6t_HL_info *info = (struct ip6t_HL_info *) (*target)->data;
32         unsigned int value;
33
34         if (*flags & IP6T_HL_USED) {
35                 exit_error(PARAMETER_PROBLEM, 
36                                 "Can't specify HL option twice");
37         }
38
39         if (!optarg) 
40                 exit_error(PARAMETER_PROBLEM, 
41                                 "HL: You must specify a value");
42
43         if (check_inverse(optarg, &invert, NULL, 0))
44                 exit_error(PARAMETER_PROBLEM,
45                                 "HL: unexpected `!'");
46         
47         if (string_to_number(optarg, 0, 255, &value) == -1)     
48                 exit_error(PARAMETER_PROBLEM,   
49                            "HL: Expected value between 0 and 255");
50
51         switch (c) {
52
53                 case '1':
54                         info->mode = IP6T_HL_SET;
55                         break;
56
57                 case '2':
58                         if (value == 0) {
59                                 exit_error(PARAMETER_PROBLEM,
60                                         "HL: decreasing by 0?");
61                         }
62
63                         info->mode = IP6T_HL_DEC;
64                         break;
65
66                 case '3':
67                         if (value == 0) {
68                                 exit_error(PARAMETER_PROBLEM,
69                                         "HL: increasing by 0?");
70                         }
71
72                         info->mode = IP6T_HL_INC;
73                         break;
74
75                 default:
76                         return 0;
77
78         }
79         
80         info->hop_limit = value;
81         *flags |= IP6T_HL_USED;
82
83         return 1;
84 }
85
86 static void HL_check(unsigned int flags)
87 {
88         if (!(flags & IP6T_HL_USED))
89                 exit_error(PARAMETER_PROBLEM,
90                                 "HL: You must specify an action");
91 }
92
93 static void HL_save(const void *ip, const struct xt_entry_target *target)
94 {
95         const struct ip6t_HL_info *info = 
96                 (struct ip6t_HL_info *) target->data;
97
98         switch (info->mode) {
99                 case IP6T_HL_SET:
100                         printf("--hl-set ");
101                         break;
102                 case IP6T_HL_DEC:
103                         printf("--hl-dec ");
104                         break;
105
106                 case IP6T_HL_INC:
107                         printf("--hl-inc ");
108                         break;
109         }
110         printf("%u ", info->hop_limit);
111 }
112
113 static void HL_print(const void *ip, const struct xt_entry_target *target,
114                      int numeric)
115 {
116         const struct ip6t_HL_info *info =
117                 (struct ip6t_HL_info *) target->data;
118
119         printf("HL ");
120         switch (info->mode) {
121                 case IP6T_HL_SET:
122                         printf("set to ");
123                         break;
124                 case IP6T_HL_DEC:
125                         printf("decrement by ");
126                         break;
127                 case IP6T_HL_INC:
128                         printf("increment by ");
129                         break;
130         }
131         printf("%u ", info->hop_limit);
132 }
133
134 static const struct option HL_opts[] = {
135         { "hl-set", 1, NULL, '1' },
136         { "hl-dec", 1, NULL, '2' },
137         { "hl-inc", 1, NULL, '3' },
138         { .name = NULL }
139 };
140
141 static struct xtables_target hl_tg6_reg = {
142         .name           = "HL",
143         .version        = XTABLES_VERSION,
144         .family         = PF_INET6,
145         .size           = XT_ALIGN(sizeof(struct ip6t_HL_info)),
146         .userspacesize  = XT_ALIGN(sizeof(struct ip6t_HL_info)),
147         .help           = HL_help,
148         .parse          = HL_parse,
149         .final_check    = HL_check,
150         .print          = HL_print,
151         .save           = HL_save,
152         .extra_opts     = HL_opts,
153 };
154
155 void _init(void)
156 {
157         xtables_register_target(&hl_tg6_reg);
158 }