b07c89418502d43f71deb34de98cc364e1fa05c9
[openwrt.git] / target / linux / generic-2.6 / patches / 140-netfilter_time.patch
1 diff -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_time.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_time.h
2 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_time.h    1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_time.h    2006-12-14 03:13:45.000000000 +0100
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.19.old/net/ipv4/netfilter/ipt_time.c linux-2.6.19.dev/net/ipv4/netfilter/ipt_time.c
24 --- linux-2.6.19.old/net/ipv4/netfilter/ipt_time.c      1970-01-01 01:00:00.000000000 +0100
25 +++ linux-2.6.19.dev/net/ipv4/netfilter/ipt_time.c      2006-12-14 03:13:45.000000000 +0100
26 @@ -0,0 +1,178 @@
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 tm currenttime;                          /* time human readable */
83 +       u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
84 +       u_int16_t packet_time;
85 +
86 +       /* We might not have a timestamp, get one */
87 +       if (skb->tstamp.off_sec == 0)
88 +               __net_timestamp((struct sk_buff *)skb);
89 +
90 +       /* First we make sure we are in the date start-stop boundaries */
91 +       if ((skb->tstamp.off_sec < info->date_start) || (skb->tstamp.off_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(skb->tstamp.off_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 int
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_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_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 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Kconfig linux-2.6.19.dev/net/ipv4/netfilter/Kconfig
206 --- linux-2.6.19.old/net/ipv4/netfilter/Kconfig 2006-12-14 03:13:45.000000000 +0100
207 +++ linux-2.6.19.dev/net/ipv4/netfilter/Kconfig 2006-12-14 03:13:45.000000000 +0100
208 @@ -263,6 +263,22 @@
209  
210           To compile it as a module, choose M here.  If unsure, say N.
211  
212 +
213 +config IP_NF_MATCH_TIME
214 +       tristate  'TIME match support'
215 +       depends on IP_NF_IPTABLES
216 +       help
217 +         This option adds a `time' match, which allows you
218 +         to match based on the packet arrival time/date
219 +         (arrival time/date at the machine which netfilter is running on) or
220 +         departure time/date (for locally generated packets).
221 +
222 +         If you say Y here, try iptables -m time --help for more information.
223 +         If you want to compile it as a module, say M here and read
224 +
225 +         Documentation/modules.txt.  If unsure, say `N'.
226 +
227 +
228  config IP_NF_MATCH_RECENT
229         tristate "recent match support"
230         depends on IP_NF_IPTABLES
231 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Makefile linux-2.6.19.dev/net/ipv4/netfilter/Makefile
232 --- linux-2.6.19.old/net/ipv4/netfilter/Makefile        2006-12-14 03:13:45.000000000 +0100
233 +++ linux-2.6.19.dev/net/ipv4/netfilter/Makefile        2006-12-14 03:13:45.000000000 +0100
234 @@ -58,6 +58,7 @@
235  obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
236  obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
237  obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
238 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
239  obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
240  obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
241  obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o