prepare for the transition to linux 2.6.22 - make it possible to override the kernel...
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.22 / 140-netfilter_time.patch
1 diff -urN linux-2.6.21.1.old/include/linux/netfilter_ipv4/ipt_time.h linux-2.6.21.1.dev/include/linux/netfilter_ipv4/ipt_time.h
2 --- linux-2.6.21.1.old/include/linux/netfilter_ipv4/ipt_time.h  1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.dev/include/linux/netfilter_ipv4/ipt_time.h  2007-05-26 20:31:03.596402160 +0200
4 @@ -0,0 +1,18 @@
5 +#ifndef __ipt_time_h_included__
6 +#define __ipt_time_h_included__
7 +
8 +
9 +struct ipt_time_info {
10 +       u_int8_t  days_match;   /* 1 bit per day. -SMTWTFS                      */
11 +       u_int16_t time_start;   /* 0 < time_start < 23*60+59 = 1439             */
12 +       u_int16_t time_stop;    /* 0:0 < time_stat < 23:59                      */
13 +
14 +                               /* FIXME: Keep this one for userspace iptables binary compability: */
15 +       u_int8_t  kerneltime;   /* ignore skb time (and use kerneltime) or not. */
16 +
17 +       time_t    date_start;
18 +       time_t    date_stop;
19 +};
20 +
21 +
22 +#endif /* __ipt_time_h_included__ */
23 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/ipt_time.c linux-2.6.21.1.dev/net/ipv4/netfilter/ipt_time.c
24 --- linux-2.6.21.1.old/net/ipv4/netfilter/ipt_time.c    1970-01-01 01:00:00.000000000 +0100
25 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/ipt_time.c    2007-05-26 20:31:03.596402160 +0200
26 @@ -0,0 +1,180 @@
27 +/*
28 +  This is a module which is used for time matching
29 +  It is using some modified code from dietlibc (localtime() function)
30 +  that you can find at http://www.fefe.de/dietlibc/
31 +  This file is distributed under the terms of the GNU General Public
32 +  License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
33 +  2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
34 +  2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
35 +     thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
36 +  2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
37 +  2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
38 +     added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
39 +  2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
40 +*/
41 +
42 +#include <linux/module.h>
43 +#include <linux/skbuff.h>
44 +#include <linux/netfilter_ipv4/ip_tables.h>
45 +#include <linux/netfilter_ipv4/ipt_time.h>
46 +#include <linux/time.h>
47 +
48 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
49 +MODULE_DESCRIPTION("Match arrival timestamp/date");
50 +MODULE_LICENSE("GPL");
51 +
52 +struct tm
53 +{
54 +       int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
55 +       int tm_min;                   /* Minutes.     [0-59] */
56 +       int tm_hour;                  /* Hours.       [0-23] */
57 +       int tm_mday;                  /* Day.         [1-31] */
58 +       int tm_mon;                   /* Month.       [0-11] */
59 +       int tm_year;                  /* Year - 1900.  */
60 +       int tm_wday;                  /* Day of week. [0-6] */
61 +       int tm_yday;                  /* Days in year.[0-365] */
62 +       int tm_isdst;                 /* DST.         [-1/0/1]*/
63 +
64 +       long int tm_gmtoff;           /* we don't care, we count from GMT */
65 +       const char *tm_zone;          /* we don't care, we count from GMT */
66 +};
67 +
68 +void
69 +localtime(const u32 time, struct tm *r);
70 +
71 +static int
72 +match(const struct sk_buff *skb,
73 +      const struct net_device *in,
74 +      const struct net_device *out,
75 +      const struct xt_match *match,
76 +      const void *matchinfo,
77 +      int offset,
78 +      unsigned int protoff,
79 +      int *hotdrop)
80 +{
81 +       const struct ipt_time_info *info = matchinfo;   /* match info for rule */
82 +       struct timeval tv;
83 +       struct tm currenttime;                          /* time human readable */
84 +       u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
85 +       u_int16_t packet_time;
86 +
87 +       /* We might not have a timestamp, get one */
88 +       if (skb->tstamp.tv64 == 0)
89 +               __net_timestamp((struct sk_buff *)skb);
90 +
91 +       skb_get_timestamp(skb, &tv);
92 +       /* First we make sure we are in the date start-stop boundaries */
93 +       if ((tv.tv_sec < info->date_start) || (tv.tv_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(tv.tv_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 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/Kconfig linux-2.6.21.1.dev/net/ipv4/netfilter/Kconfig
208 --- linux-2.6.21.1.old/net/ipv4/netfilter/Kconfig       2007-05-26 20:31:03.452424048 +0200
209 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/Kconfig       2007-05-26 20:31:03.597402008 +0200
210 @@ -96,6 +96,22 @@
211  
212           To compile it as a module, choose M here.  If unsure, say N.
213  
214 +
215 +config IP_NF_MATCH_TIME
216 +       tristate  'TIME match support'
217 +       depends on IP_NF_IPTABLES
218 +       help
219 +         This option adds a `time' match, which allows you
220 +         to match based on the packet arrival time/date
221 +         (arrival time/date at the machine which netfilter is running on) or
222 +         departure time/date (for locally generated packets).
223 +
224 +         If you say Y here, try iptables -m time --help for more information.
225 +         If you want to compile it as a module, say M here and read
226 +
227 +         Documentation/modules.txt.  If unsure, say `N'.
228 +
229 +
230  config IP_NF_MATCH_RECENT
231         tristate "recent match support"
232         depends on IP_NF_IPTABLES
233 diff -urN linux-2.6.21.1.old/net/ipv4/netfilter/Makefile linux-2.6.21.1.dev/net/ipv4/netfilter/Makefile
234 --- linux-2.6.21.1.old/net/ipv4/netfilter/Makefile      2007-05-26 20:31:03.452424048 +0200
235 +++ linux-2.6.21.1.dev/net/ipv4/netfilter/Makefile      2007-05-26 20:31:03.597402008 +0200
236 @@ -44,6 +44,7 @@
237  obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
238  obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
239  obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
240 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
241  obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
242  obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
243  obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o