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