1 /*
2 * Shared library add-on for iptables to add IDLETIMER support.
3 *
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23 #include <stdio.h>
24 #include <xtables.h>
25 #include <linux/netfilter/xt_IDLETIMER.h>
26
27 enum {
28 O_TIMEOUT = 0,
29 O_LABEL,
30 O_ALARM,
31 O_NETLINK,
32 };
33
34 #define s struct idletimer_tg_info
35 static const struct xt_option_entry idletimer_tg_opts[] = {
36 {.name = "timeout", .id = O_TIMEOUT, .type = XTTYPE_UINT32,
37 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, timeout)},
38 {.name = "label", .id = O_LABEL, .type = XTTYPE_STRING,
39 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, label)},
40 XTOPT_TABLEEND,
41 };
42 #undef s
43
44 #define s struct idletimer_tg_info_v1
45 static const struct xt_option_entry idletimer_tg_opts_v1[] = {
46 {.name = "timeout", .id = O_TIMEOUT, .type = XTTYPE_UINT32,
47 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, timeout)},
48 {.name = "label", .id = O_LABEL, .type = XTTYPE_STRING,
49 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, label)},
50 {.name = "alarm", .id = O_ALARM, .type = XTTYPE_NONE},
51 {.name = "send_nl_msg", .id = O_NETLINK, .type = XTTYPE_NONE},
52 XTOPT_TABLEEND,
53 };
54 #undef s
55
idletimer_tg_help(void)56 static void idletimer_tg_help(void)
57 {
58 printf(
59 "IDLETIMER target options:\n"
60 " --timeout time Timeout until the notification is sent (in seconds)\n"
61 " --label string Unique rule identifier\n"
62 "\n");
63 }
64
idletimer_tg_help_v1(void)65 static void idletimer_tg_help_v1(void)
66 {
67 printf(
68 "IDLETIMER target options:\n"
69 " --timeout time Timeout until the notification is sent (in seconds)\n"
70 " --label string Unique rule identifier\n"
71 " --alarm Use alarm instead of default timer\n"
72 " --send_nl_msg Enable netlink messages and show remaining time in sysfs.\n"
73 "\n");
74 }
75
idletimer_tg_print(const void * ip,const struct xt_entry_target * target,int numeric)76 static void idletimer_tg_print(const void *ip,
77 const struct xt_entry_target *target,
78 int numeric)
79 {
80 struct idletimer_tg_info *info =
81 (struct idletimer_tg_info *) target->data;
82
83 printf(" timeout:%u", info->timeout);
84 printf(" label:%s", info->label);
85 }
86
idletimer_tg_print_v1(const void * ip,const struct xt_entry_target * target,int numeric)87 static void idletimer_tg_print_v1(const void *ip,
88 const struct xt_entry_target *target,
89 int numeric)
90 {
91 struct idletimer_tg_info_v1 *info =
92 (struct idletimer_tg_info_v1 *) target->data;
93
94 printf(" timeout:%u", info->timeout);
95 printf(" label:%s", info->label);
96 if (info->timer_type == XT_IDLETIMER_ALARM)
97 printf(" alarm");
98 if (info->send_nl_msg)
99 printf(" send_nl_msg");
100 }
101
102
idletimer_tg_save(const void * ip,const struct xt_entry_target * target)103 static void idletimer_tg_save(const void *ip,
104 const struct xt_entry_target *target)
105 {
106 struct idletimer_tg_info *info =
107 (struct idletimer_tg_info *) target->data;
108
109 printf(" --timeout %u", info->timeout);
110 printf(" --label %s", info->label);
111 }
112
idletimer_tg_save_v1(const void * ip,const struct xt_entry_target * target)113 static void idletimer_tg_save_v1(const void *ip,
114 const struct xt_entry_target *target)
115 {
116 struct idletimer_tg_info_v1 *info =
117 (struct idletimer_tg_info_v1 *) target->data;
118
119 printf(" --timeout %u", info->timeout);
120 printf(" --label %s", info->label);
121 if (info->timer_type == XT_IDLETIMER_ALARM)
122 printf(" --alarm");
123 if (info->send_nl_msg)
124 printf(" --send_nl_msg");
125 }
126
idletimer_tg_parse_v1(struct xt_option_call * cb)127 static void idletimer_tg_parse_v1(struct xt_option_call *cb)
128 {
129 struct idletimer_tg_info_v1 *info = cb->data;
130
131 xtables_option_parse(cb);
132 if (cb->entry->id == O_ALARM)
133 info->timer_type = XT_IDLETIMER_ALARM;
134 if (cb->entry->id == O_NETLINK)
135 info->send_nl_msg = 1;
136 }
137
138 static struct xtables_target idletimer_tg_reg[] = {
139 {
140 .family = NFPROTO_UNSPEC,
141 .name = "IDLETIMER",
142 .version = XTABLES_VERSION,
143 .revision = 0,
144 .size = XT_ALIGN(sizeof(struct idletimer_tg_info)),
145 .userspacesize = offsetof(struct idletimer_tg_info, timer),
146 .help = idletimer_tg_help,
147 .x6_parse = xtables_option_parse,
148 .print = idletimer_tg_print,
149 .save = idletimer_tg_save,
150 .x6_options = idletimer_tg_opts,
151 },
152 {
153 .family = NFPROTO_UNSPEC,
154 .name = "IDLETIMER",
155 .version = XTABLES_VERSION,
156 .revision = 1,
157 .size = XT_ALIGN(sizeof(struct idletimer_tg_info_v1)),
158 .userspacesize = offsetof(struct idletimer_tg_info_v1, timer),
159 .help = idletimer_tg_help_v1,
160 .x6_parse = idletimer_tg_parse_v1,
161 .print = idletimer_tg_print_v1,
162 .save = idletimer_tg_save_v1,
163 .x6_options = idletimer_tg_opts_v1,
164 },
165
166 };
167
_init(void)168 void _init(void)
169 {
170 xtables_register_targets(idletimer_tg_reg, ARRAY_SIZE(idletimer_tg_reg));
171 }
172