add generic 2.6.28 patches
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.28 / 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,180 @@
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 bool
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 +      bool *hotdrop)
78 +{
79 +       const struct ipt_time_info *info = matchinfo;   /* match info for rule */
80 +       struct timeval tv;
81 +       struct tm currenttime;                          /* time human readable */
82 +       u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
83 +       u_int16_t packet_time;
84 +
85 +       /* We might not have a timestamp, get one */
86 +       if (skb->tstamp.tv64 == 0)
87 +               __net_timestamp((struct sk_buff *)skb);
88 +
89 +       skb_get_timestamp(skb, &tv);
90 +       /* First we make sure we are in the date start-stop boundaries */
91 +       if ((tv.tv_sec < info->date_start) || (tv.tv_sec > info->date_stop))
92 +               return 0; /* We are outside the date boundaries */
93 +
94 +       /* Transform the timestamp of the packet, in a human readable form */
95 +       localtime(tv.tv_sec, &currenttime);
96 +
97 +       /* check if we match this timestamp, we start by the days... */
98 +       if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
99 +               return 0; /* the day doesn't match */
100 +
101 +       /* ... check the time now */
102 +       packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
103 +       if ((packet_time < info->time_start) || (packet_time > info->time_stop))
104 +               return 0;
105 +
106 +       /* here we match ! */
107 +       return 1;
108 +}
109 +
110 +static bool
111 +checkentry(const char *tablename,
112 +           const void *ip,
113 +          const struct xt_match *match,
114 +           void *matchinfo,
115 +           unsigned int hook_mask)
116 +{
117 +       struct ipt_time_info *info = matchinfo;   /* match info for rule */
118 +
119 +       /* First, check that we are in the correct hooks */
120 +       if (hook_mask
121 +            & ~((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) | (1 << NF_INET_LOCAL_OUT)))
122 +       {
123 +               printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
124 +               return 0;
125 +       }
126 +
127 +       /* Now check the coherence of the data ... */
128 +       if ((info->time_start > 1439) ||        /* 23*60+59 = 1439*/
129 +           (info->time_stop  > 1439))
130 +       {
131 +               printk(KERN_WARNING "ipt_time: invalid argument\n");
132 +               return 0;
133 +       }
134 +
135 +       return 1;
136 +}
137 +
138 +static struct ipt_match time_match = {
139 +       .name = "time",
140 +       .match = &match,
141 +       .matchsize = sizeof(struct ipt_time_info),
142 +       .checkentry = &checkentry,
143 +       .me = THIS_MODULE
144 +};
145 +
146 +static int __init init(void)
147 +{
148 +       printk("ipt_time loading\n");
149 +       return xt_register_match(&time_match);
150 +}
151 +
152 +static void __exit fini(void)
153 +{
154 +       xt_unregister_match(&time_match);
155 +       printk("ipt_time unloaded\n");
156 +}
157 +
158 +module_init(init);
159 +module_exit(fini);
160 +
161 +
162 +/* The part below is borowed and modified from dietlibc */
163 +
164 +/* seconds per day */
165 +#define SPD 24*60*60
166 +
167 +void
168 +localtime(const u32 time, struct tm *r) {
169 +       u32 i, timep;
170 +       extern struct timezone sys_tz;
171 +       const unsigned int __spm[12] =
172 +               { 0,
173 +                 (31),
174 +                 (31+28),
175 +                 (31+28+31),
176 +                 (31+28+31+30),
177 +                 (31+28+31+30+31),
178 +                 (31+28+31+30+31+30),
179 +                 (31+28+31+30+31+30+31),
180 +                 (31+28+31+30+31+30+31+31),
181 +                 (31+28+31+30+31+30+31+31+30),
182 +                 (31+28+31+30+31+30+31+31+30+31),
183 +                 (31+28+31+30+31+30+31+31+30+31+30),
184 +               };
185 +       register u32 work;
186 +
187 +       timep = time - (sys_tz.tz_minuteswest * 60);
188 +       work=timep%(SPD);
189 +       r->tm_sec=work%60; work/=60;
190 +       r->tm_min=work%60; r->tm_hour=work/60;
191 +       work=timep/(SPD);
192 +       r->tm_wday=(4+work)%7;
193 +       for (i=1970; ; ++i) {
194 +               register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
195 +               if (work>k)
196 +                       work-=k;
197 +               else
198 +                       break;
199 +       }
200 +       r->tm_year=i-1900;
201 +       for (i=11; i && __spm[i]>work; --i) ;
202 +       r->tm_mon=i;
203 +       r->tm_mday=work-__spm[i]+1;
204 +}
205 --- a/net/ipv4/netfilter/Kconfig
206 +++ b/net/ipv4/netfilter/Kconfig
207 @@ -70,6 +70,22 @@ config IP_NF_MATCH_IPP2P
208         help
209           Module for matching traffic of various Peer-to-Peer applications
210  
211 +
212 +config IP_NF_MATCH_TIME
213 +       tristate  'TIME match support'
214 +       depends on NETFILTER_ADVANCED
215 +       help
216 +         This option adds a `time' match, which allows you
217 +         to match based on the packet arrival time/date
218 +         (arrival time/date at the machine which netfilter is running on) or
219 +         departure time/date (for locally generated packets).
220 +
221 +         If you say Y here, try iptables -m time --help for more information.
222 +         If you want to compile it as a module, say M here and read
223 +
224 +         Documentation/modules.txt.  If unsure, say `N'.
225 +
226 +
227  config IP_NF_MATCH_ADDRTYPE
228         tristate '"addrtype" address type match support'
229         depends on NETFILTER_ADVANCED
230 --- a/net/ipv4/netfilter/Makefile
231 +++ b/net/ipv4/netfilter/Makefile
232 @@ -53,6 +53,7 @@ obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
233  obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
234  obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
235  obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
236 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
237  
238  obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
239