• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
32 
33 #define s struct idletimer_tg_info
34 static const struct xt_option_entry idletimer_tg_opts[] = {
35 	{.name = "timeout", .id = O_TIMEOUT, .type = XTTYPE_UINT32,
36 	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, timeout)},
37 	{.name = "label", .id = O_LABEL, .type = XTTYPE_STRING,
38 	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, label)},
39 	XTOPT_TABLEEND,
40 };
41 #undef s
42 
43 #define s struct idletimer_tg_info_v1
44 static const struct xt_option_entry idletimer_tg_opts_v1[] = {
45 	{.name = "timeout", .id = O_TIMEOUT, .type = XTTYPE_UINT32,
46 	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, timeout)},
47 	{.name = "label", .id = O_LABEL, .type = XTTYPE_STRING,
48 	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, label)},
49 	{.name = "alarm", .id = O_ALARM, .type = XTTYPE_NONE},
50 	XTOPT_TABLEEND,
51 };
52 #undef s
53 
idletimer_tg_help(void)54 static void idletimer_tg_help(void)
55 {
56 	printf(
57 "IDLETIMER target options:\n"
58 " --timeout time	Timeout until the notification is sent (in seconds)\n"
59 " --label string	Unique rule identifier\n"
60 "\n");
61 }
62 
idletimer_tg_help_v1(void)63 static void idletimer_tg_help_v1(void)
64 {
65 	printf(
66 "IDLETIMER target options:\n"
67 " --timeout time	Timeout until the notification is sent (in seconds)\n"
68 " --label string	Unique rule identifier\n"
69 " --alarm	Use alarm instead of default timer\n"
70 "\n");
71 }
72 
idletimer_tg_print(const void * ip,const struct xt_entry_target * target,int numeric)73 static void idletimer_tg_print(const void *ip,
74 			       const struct xt_entry_target *target,
75 			       int numeric)
76 {
77 	struct idletimer_tg_info *info =
78 		(struct idletimer_tg_info *) target->data;
79 
80 	printf(" timeout:%u", info->timeout);
81 	printf(" label:%s", info->label);
82 }
83 
idletimer_tg_print_v1(const void * ip,const struct xt_entry_target * target,int numeric)84 static void idletimer_tg_print_v1(const void *ip,
85 			       const struct xt_entry_target *target,
86 			       int numeric)
87 {
88 	struct idletimer_tg_info_v1 *info =
89 		(struct idletimer_tg_info_v1 *) target->data;
90 
91 	printf(" timeout:%u", info->timeout);
92 	printf(" label:%s", info->label);
93 	if (info->timer_type == XT_IDLETIMER_ALARM)
94 		printf(" alarm");
95 }
96 
97 
idletimer_tg_save(const void * ip,const struct xt_entry_target * target)98 static void idletimer_tg_save(const void *ip,
99 			      const struct xt_entry_target *target)
100 {
101 	struct idletimer_tg_info *info =
102 		(struct idletimer_tg_info *) target->data;
103 
104 	printf(" --timeout %u", info->timeout);
105 	printf(" --label %s", info->label);
106 }
107 
idletimer_tg_save_v1(const void * ip,const struct xt_entry_target * target)108 static void idletimer_tg_save_v1(const void *ip,
109 			      const struct xt_entry_target *target)
110 {
111 	struct idletimer_tg_info_v1 *info =
112 		(struct idletimer_tg_info_v1 *) target->data;
113 
114 	printf(" --timeout %u", info->timeout);
115 	printf(" --label %s", info->label);
116 	if (info->timer_type == XT_IDLETIMER_ALARM)
117 		printf(" --alarm");
118 }
119 
idletimer_tg_parse_v1(struct xt_option_call * cb)120 static void idletimer_tg_parse_v1(struct xt_option_call *cb)
121 {
122 	struct idletimer_tg_info_v1 *info = cb->data;
123 
124 	xtables_option_parse(cb);
125 	if (cb->entry->id == O_ALARM)
126 		info->timer_type = XT_IDLETIMER_ALARM;
127 }
128 
129 static struct xtables_target idletimer_tg_reg[] = {
130 	{
131 		.family	       = NFPROTO_UNSPEC,
132 		.name	       = "IDLETIMER",
133 		.version       = XTABLES_VERSION,
134 		.revision      = 0,
135 		.size	       = XT_ALIGN(sizeof(struct idletimer_tg_info)),
136 		.userspacesize = offsetof(struct idletimer_tg_info, timer),
137 		.help	       = idletimer_tg_help,
138 		.x6_parse      = xtables_option_parse,
139 		.print	       = idletimer_tg_print,
140 		.save	       = idletimer_tg_save,
141 		.x6_options    = idletimer_tg_opts,
142 	},
143 	{
144 		.family	       = NFPROTO_UNSPEC,
145 		.name	       = "IDLETIMER",
146 		.version       = XTABLES_VERSION,
147 		.revision      = 1,
148 		.size	       = XT_ALIGN(sizeof(struct idletimer_tg_info_v1)),
149 		.userspacesize = offsetof(struct idletimer_tg_info_v1, timer),
150 		.help	       = idletimer_tg_help_v1,
151 		.x6_parse      = idletimer_tg_parse_v1,
152 		.print	       = idletimer_tg_print_v1,
153 		.save	       = idletimer_tg_save_v1,
154 		.x6_options    = idletimer_tg_opts_v1,
155 	},
156 
157 };
158 
_init(void)159 void _init(void)
160 {
161 	xtables_register_targets(idletimer_tg_reg, ARRAY_SIZE(idletimer_tg_reg));
162 }
163