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