[kernel] refresh generic 2.6.24 patches
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.24 / 140-netfilter_time.patch
1 Index: linux-2.6.24.7/include/linux/netfilter_ipv4/ipt_time.h
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.24.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.24.7/net/ipv4/netfilter/ipt_time.c
25 ===================================================================
26 --- /dev/null
27 +++ linux-2.6.24.7/net/ipv4/netfilter/ipt_time.c
28 @@ -0,0 +1,180 @@
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 bool
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 +      bool *hotdrop)
82 +{
83 +       const struct ipt_time_info *info = matchinfo;   /* match info for rule */
84 +       struct timeval tv;
85 +       struct tm currenttime;                          /* time human readable */
86 +       u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
87 +       u_int16_t packet_time;
88 +
89 +       /* We might not have a timestamp, get one */
90 +       if (skb->tstamp.tv64 == 0)
91 +               __net_timestamp((struct sk_buff *)skb);
92 +
93 +       skb_get_timestamp(skb, &tv);
94 +       /* First we make sure we are in the date start-stop boundaries */
95 +       if ((tv.tv_sec < info->date_start) || (tv.tv_sec > info->date_stop))
96 +               return 0; /* We are outside the date boundaries */
97 +
98 +       /* Transform the timestamp of the packet, in a human readable form */
99 +       localtime(tv.tv_sec, &currenttime);
100 +
101 +       /* check if we match this timestamp, we start by the days... */
102 +       if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
103 +               return 0; /* the day doesn't match */
104 +
105 +       /* ... check the time now */
106 +       packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
107 +       if ((packet_time < info->time_start) || (packet_time > info->time_stop))
108 +               return 0;
109 +
110 +       /* here we match ! */
111 +       return 1;
112 +}
113 +
114 +static bool
115 +checkentry(const char *tablename,
116 +           const void *ip,
117 +          const struct xt_match *match,
118 +           void *matchinfo,
119 +           unsigned int hook_mask)
120 +{
121 +       struct ipt_time_info *info = matchinfo;   /* match info for rule */
122 +
123 +       /* First, check that we are in the correct hooks */
124 +       if (hook_mask
125 +            & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
126 +       {
127 +               printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
128 +               return 0;
129 +       }
130 +
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 +       .matchsize = sizeof(struct ipt_time_info),
146 +       .checkentry = &checkentry,
147 +       .me = THIS_MODULE
148 +};
149 +
150 +static int __init init(void)
151 +{
152 +       printk("ipt_time loading\n");
153 +       return xt_register_match(&time_match);
154 +}
155 +
156 +static void __exit fini(void)
157 +{
158 +       xt_unregister_match(&time_match);
159 +       printk("ipt_time unloaded\n");
160 +}
161 +
162 +module_init(init);
163 +module_exit(fini);
164 +
165 +
166 +/* The part below is borowed and modified from dietlibc */
167 +
168 +/* seconds per day */
169 +#define SPD 24*60*60
170 +
171 +void
172 +localtime(const u32 time, struct tm *r) {
173 +       u32 i, 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 u32 work;
190 +
191 +       timep = time - (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 Index: linux-2.6.24.7/net/ipv4/netfilter/Kconfig
210 ===================================================================
211 --- linux-2.6.24.7.orig/net/ipv4/netfilter/Kconfig
212 +++ linux-2.6.24.7/net/ipv4/netfilter/Kconfig
213 @@ -78,6 +78,22 @@ config IP_NF_MATCH_TOS
214  
215           To compile it as a module, choose M here.  If unsure, say N.
216  
217 +
218 +config IP_NF_MATCH_TIME
219 +       tristate  'TIME match support'
220 +       depends on IP_NF_IPTABLES
221 +       help
222 +         This option adds a `time' match, which allows you
223 +         to match based on the packet arrival time/date
224 +         (arrival time/date at the machine which netfilter is running on) or
225 +         departure time/date (for locally generated packets).
226 +
227 +         If you say Y here, try iptables -m time --help for more information.
228 +         If you want to compile it as a module, say M here and read
229 +
230 +         Documentation/modules.txt.  If unsure, say `N'.
231 +
232 +
233  config IP_NF_MATCH_RECENT
234         tristate "recent match support"
235         depends on IP_NF_IPTABLES
236 Index: linux-2.6.24.7/net/ipv4/netfilter/Makefile
237 ===================================================================
238 --- linux-2.6.24.7.orig/net/ipv4/netfilter/Makefile
239 +++ linux-2.6.24.7/net/ipv4/netfilter/Makefile
240 @@ -50,6 +50,7 @@ obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_
241  obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
242  obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
243  obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
244 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
245  
246  obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
247