Added support for ipt_time
[openwrt/svn-archive/archive.git] / openwrt / target / linux / linux-2.4 / patches / generic / 618-netfilter_time.patch
1 diff -urN linux-2.4.32.orig/net/ipv4/netfilter/Config.in linux-2.4.32/net/ipv4/netfilter/Config.in
2 --- linux-2.4.32.orig/net/ipv4/netfilter/Config.in 2006-01-07 12:11:37.000000000 +0100
3 +++ linux-2.4.32/net/ipv4/netfilter/Config.in 2006-01-07 12:15:45.000000000 +0100
4 @@ -48,6 +48,7 @@
5 dep_tristate ' netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
6 dep_tristate ' Multiple port match support' CONFIG_IP_NF_MATCH_MULTIPORT $CONFIG_IP_NF_IPTABLES
7 dep_tristate ' TOS match support' CONFIG_IP_NF_MATCH_TOS $CONFIG_IP_NF_IPTABLES
8 + dep_tristate ' TIME match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_TIME $CONFIG_IP_NF_IPTABLES
9 dep_tristate ' condition match support' CONFIG_IP_NF_MATCH_CONDITION $CONFIG_IP_NF_IPTABLES
10 dep_tristate ' recent match support' CONFIG_IP_NF_MATCH_RECENT $CONFIG_IP_NF_IPTABLES
11 dep_tristate ' ECN match support' CONFIG_IP_NF_MATCH_ECN $CONFIG_IP_NF_IPTABLES
12 diff -urN linux-2.4.32.orig/net/ipv4/netfilter/ipt_time.c linux-2.4.32/net/ipv4/netfilter/ipt_time.c
13 --- linux-2.4.32.orig/net/ipv4/netfilter/ipt_time.c 1970-01-01 01:00:00.000000000 +0100
14 +++ linux-2.4.32/net/ipv4/netfilter/ipt_time.c 2006-01-07 12:14:32.000000000 +0100
15 @@ -0,0 +1,193 @@
16 +/*
17 + This is a module which is used for time matching
18 + It is using some modified code from dietlibc (localtime() function)
19 + that you can find at http://www.fefe.de/dietlibc/
20 + This file is distributed under the terms of the GNU General Public
21 + License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
22 + 2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
23 + 2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
24 + thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
25 + 2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
26 + 2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
27 + added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
28 + 2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
29 +*/
30 +
31 +#include <linux/module.h>
32 +#include <linux/skbuff.h>
33 +#include <linux/netfilter_ipv4/ip_tables.h>
34 +#include <linux/netfilter_ipv4/ipt_time.h>
35 +#include <linux/time.h>
36 +
37 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
38 +MODULE_DESCRIPTION("Match arrival timestamp/date");
39 +MODULE_LICENSE("GPL");
40 +
41 +struct tm
42 +{
43 + int tm_sec; /* Seconds. [0-60] (1 leap second) */
44 + int tm_min; /* Minutes. [0-59] */
45 + int tm_hour; /* Hours. [0-23] */
46 + int tm_mday; /* Day. [1-31] */
47 + int tm_mon; /* Month. [0-11] */
48 + int tm_year; /* Year - 1900. */
49 + int tm_wday; /* Day of week. [0-6] */
50 + int tm_yday; /* Days in year.[0-365] */
51 + int tm_isdst; /* DST. [-1/0/1]*/
52 +
53 + long int tm_gmtoff; /* we don't care, we count from GMT */
54 + const char *tm_zone; /* we don't care, we count from GMT */
55 +};
56 +
57 +void
58 +localtime(const time_t *timepr, struct tm *r);
59 +
60 +static int
61 +match(const struct sk_buff *skb,
62 + const struct net_device *in,
63 + const struct net_device *out,
64 + const void *matchinfo,
65 + int offset,
66 + const void *hdr,
67 + u_int16_t datalen,
68 + int *hotdrop)
69 +{
70 + const struct ipt_time_info *info = matchinfo; /* match info for rule */
71 + struct tm currenttime; /* time human readable */
72 + u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
73 + u_int16_t packet_time;
74 + struct timeval kerneltimeval;
75 + time_t packet_local_time;
76 +
77 + /* if kerneltime=1, we don't read the skb->timestamp but kernel time instead */
78 + if (info->kerneltime)
79 + {
80 + do_gettimeofday(&kerneltimeval);
81 + packet_local_time = kerneltimeval.tv_sec;
82 + }
83 + else
84 + packet_local_time = skb->stamp.tv_sec;
85 +
86 + /* First we make sure we are in the date start-stop boundaries */
87 + if ((packet_local_time < info->date_start) || (packet_local_time > info->date_stop))
88 + return 0; /* We are outside the date boundaries */
89 +
90 + /* Transform the timestamp of the packet, in a human readable form */
91 + localtime(&packet_local_time, &currenttime);
92 +
93 + /* check if we match this timestamp, we start by the days... */
94 + if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
95 + return 0; /* the day doesn't match */
96 +
97 + /* ... check the time now */
98 + packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
99 + if ((packet_time < info->time_start) || (packet_time > info->time_stop))
100 + return 0;
101 +
102 + /* here we match ! */
103 + return 1;
104 +}
105 +
106 +static int
107 +checkentry(const char *tablename,
108 + const struct ipt_ip *ip,
109 + void *matchinfo,
110 + unsigned int matchsize,
111 + unsigned int hook_mask)
112 +{
113 + struct ipt_time_info *info = matchinfo; /* match info for rule */
114 +
115 + /* First, check that we are in the correct hooks */
116 + if (hook_mask
117 + & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
118 + {
119 + printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
120 + return 0;
121 + }
122 + /* we use the kerneltime if we are in forward or output */
123 + info->kerneltime = 1;
124 + if (hook_mask & ~((1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
125 + /* we use the skb time */
126 + info->kerneltime = 0;
127 +
128 + /* Check the size */
129 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_time_info)))
130 + return 0;
131 + /* Now check the coherence of the data ... */
132 + if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
133 + (info->time_stop > 1439))
134 + {
135 + printk(KERN_WARNING "ipt_time: invalid argument\n");
136 + return 0;
137 + }
138 +
139 + return 1;
140 +}
141 +
142 +static struct ipt_match time_match = {
143 + .name = "time",
144 + .match = match,
145 + .checkentry = checkentry,
146 + .me = THIS_MODULE,
147 +};
148 +
149 +static int __init init(void)
150 +{
151 + printk("ipt_time loading\n");
152 + return ipt_register_match(&time_match);
153 +}
154 +
155 +static void __exit fini(void)
156 +{
157 + ipt_unregister_match(&time_match);
158 + printk("ipt_time unloaded\n");
159 +}
160 +
161 +module_init(init);
162 +module_exit(fini);
163 +
164 +
165 +/* The part below is borowed and modified from dietlibc */
166 +
167 +/* seconds per day */
168 +#define SPD 24*60*60
169 +
170 +void
171 +localtime(const time_t *timepr, struct tm *r) {
172 + time_t i;
173 + time_t timep;
174 + extern struct timezone sys_tz;
175 + const unsigned int __spm[12] =
176 + { 0,
177 + (31),
178 + (31+28),
179 + (31+28+31),
180 + (31+28+31+30),
181 + (31+28+31+30+31),
182 + (31+28+31+30+31+30),
183 + (31+28+31+30+31+30+31),
184 + (31+28+31+30+31+30+31+31),
185 + (31+28+31+30+31+30+31+31+30),
186 + (31+28+31+30+31+30+31+31+30+31),
187 + (31+28+31+30+31+30+31+31+30+31+30),
188 + };
189 + register time_t work;
190 +
191 + timep = (*timepr) - (sys_tz.tz_minuteswest * 60);
192 + work=timep%(SPD);
193 + r->tm_sec=work%60; work/=60;
194 + r->tm_min=work%60; r->tm_hour=work/60;
195 + work=timep/(SPD);
196 + r->tm_wday=(4+work)%7;
197 + for (i=1970; ; ++i) {
198 + register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
199 + if (work>k)
200 + work-=k;
201 + else
202 + break;
203 + }
204 + r->tm_year=i-1900;
205 + for (i=11; i && __spm[i]>work; --i) ;
206 + r->tm_mon=i;
207 + r->tm_mday=work-__spm[i]+1;
208 +}
209 diff -urN linux-2.4.32.orig/net/ipv4/netfilter/Makefile linux-2.4.32/net/ipv4/netfilter/Makefile
210 --- linux-2.4.32.orig/net/ipv4/netfilter/Makefile 2006-01-07 12:11:37.000000000 +0100
211 +++ linux-2.4.32/net/ipv4/netfilter/Makefile 2006-01-07 12:16:07.000000000 +0100
212 @@ -118,6 +118,7 @@
213 obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
214 obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
215 obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
216 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
217 obj-$(CONFIG_IP_NF_MATCH_CONDITION) += ipt_condition.o
218
219 obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
220 diff -urN linux-2.4.32.orig/include/linux/netfilter_ipv4/ipt_time.h linux-2.4.32/include/linux/netfilter_ipv4/ipt_time.h
221 --- linux-2.4.32.orig/include/linux/netfilter_ipv4/ipt_time.h 1970-01-01 01:00:00.000000000 +0100
222 +++ linux-2.4.32/include/linux/netfilter_ipv4/ipt_time.h 2006-01-07 12:16:42.000000000 +0100
223 @@ -0,0 +1,15 @@
224 +#ifndef __ipt_time_h_included__
225 +#define __ipt_time_h_included__
226 +
227 +
228 +struct ipt_time_info {
229 + u_int8_t days_match; /* 1 bit per day. -SMTWTFS */
230 + u_int16_t time_start; /* 0 < time_start < 23*60+59 = 1439 */
231 + u_int16_t time_stop; /* 0:0 < time_stat < 23:59 */
232 + u_int8_t kerneltime; /* ignore skb time (and use kerneltime) or not. */
233 + time_t date_start;
234 + time_t date_stop;
235 +};
236 +
237 +
238 +#endif /* __ipt_time_h_included__ */