[kernel] refresh generic-2.4 patches
[openwrt.git] / target / linux / generic-2.4 / patches / 617-netfilter_time.patch
1 Index: linux-2.4.35.4/net/ipv4/netfilter/Config.in
2 ===================================================================
3 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Config.in
4 +++ linux-2.4.35.4/net/ipv4/netfilter/Config.in
5 @@ -47,6 +47,7 @@ if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; 
6    dep_tristate '  netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
7    dep_tristate '  Multiple port match support' CONFIG_IP_NF_MATCH_MULTIPORT $CONFIG_IP_NF_IPTABLES
8    dep_tristate '  TOS match support' CONFIG_IP_NF_MATCH_TOS $CONFIG_IP_NF_IPTABLES
9 +  dep_tristate '  TIME match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_TIME $CONFIG_IP_NF_IPTABLES
10    dep_tristate '  condition match support' CONFIG_IP_NF_MATCH_CONDITION $CONFIG_IP_NF_IPTABLES
11    dep_tristate '  recent match support' CONFIG_IP_NF_MATCH_RECENT $CONFIG_IP_NF_IPTABLES
12    dep_tristate '  ECN match support' CONFIG_IP_NF_MATCH_ECN $CONFIG_IP_NF_IPTABLES
13 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_time.c
14 ===================================================================
15 --- /dev/null
16 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_time.c
17 @@ -0,0 +1,193 @@
18 +/*
19 +  This is a module which is used for time matching
20 +  It is using some modified code from dietlibc (localtime() function)
21 +  that you can find at http://www.fefe.de/dietlibc/
22 +  This file is distributed under the terms of the GNU General Public
23 +  License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
24 +  2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
25 +  2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
26 +     thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
27 +  2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
28 +  2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
29 +     added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
30 +  2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
31 +*/
32 +
33 +#include <linux/module.h>
34 +#include <linux/skbuff.h>
35 +#include <linux/netfilter_ipv4/ip_tables.h>
36 +#include <linux/netfilter_ipv4/ipt_time.h>
37 +#include <linux/time.h>
38 +
39 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
40 +MODULE_DESCRIPTION("Match arrival timestamp/date");
41 +MODULE_LICENSE("GPL");
42 +
43 +struct tm
44 +{
45 +       int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
46 +       int tm_min;                   /* Minutes.     [0-59] */
47 +       int tm_hour;                  /* Hours.       [0-23] */
48 +       int tm_mday;                  /* Day.         [1-31] */
49 +       int tm_mon;                   /* Month.       [0-11] */
50 +       int tm_year;                  /* Year - 1900.  */
51 +       int tm_wday;                  /* Day of week. [0-6] */
52 +       int tm_yday;                  /* Days in year.[0-365] */
53 +       int tm_isdst;                 /* DST.         [-1/0/1]*/
54 +
55 +       long int tm_gmtoff;           /* we don't care, we count from GMT */
56 +       const char *tm_zone;          /* we don't care, we count from GMT */
57 +};
58 +
59 +void
60 +localtime(const time_t *timepr, struct tm *r);
61 +
62 +static int
63 +match(const struct sk_buff *skb,
64 +      const struct net_device *in,
65 +      const struct net_device *out,
66 +      const void *matchinfo,
67 +      int offset,
68 +      const void *hdr,
69 +      u_int16_t datalen,
70 +      int *hotdrop)
71 +{
72 +       const struct ipt_time_info *info = matchinfo;   /* match info for rule */
73 +       struct tm currenttime;                          /* time human readable */
74 +       u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
75 +       u_int16_t packet_time;
76 +       struct timeval kerneltimeval;
77 +       time_t packet_local_time;
78 +
79 +       /* if kerneltime=1, we don't read the skb->timestamp but kernel time instead */
80 +       if (info->kerneltime)
81 +       {
82 +               do_gettimeofday(&kerneltimeval);
83 +               packet_local_time = kerneltimeval.tv_sec;
84 +       }
85 +       else
86 +               packet_local_time = skb->stamp.tv_sec;
87 +
88 +       /* First we make sure we are in the date start-stop boundaries */
89 +       if ((packet_local_time < info->date_start) || (packet_local_time > 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(&packet_local_time, &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 struct ipt_ip *ip,
111 +           void *matchinfo,
112 +           unsigned int matchsize,
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 +       /* we use the kerneltime if we are in forward or output */
125 +       info->kerneltime = 1;
126 +       if (hook_mask & ~((1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT))) 
127 +               /* we use the skb time */
128 +               info->kerneltime = 0;
129 +
130 +       /* Check the size */
131 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_time_info)))
132 +               return 0;
133 +       /* Now check the coherence of the data ... */
134 +       if ((info->time_start > 1439) ||        /* 23*60+59 = 1439*/
135 +           (info->time_stop  > 1439))
136 +       {
137 +               printk(KERN_WARNING "ipt_time: invalid argument\n");
138 +               return 0;
139 +       }
140 +
141 +       return 1;
142 +}
143 +
144 +static struct ipt_match time_match = {
145 +       .name           = "time",
146 +       .match          = match,
147 +       .checkentry     = checkentry,
148 +       .me             = THIS_MODULE,
149 +};
150 +
151 +static int __init init(void)
152 +{
153 +       printk("ipt_time loading\n");
154 +       return ipt_register_match(&time_match);
155 +}
156 +
157 +static void __exit fini(void)
158 +{
159 +       ipt_unregister_match(&time_match);
160 +       printk("ipt_time unloaded\n");
161 +}
162 +
163 +module_init(init);
164 +module_exit(fini);
165 +
166 +
167 +/* The part below is borowed and modified from dietlibc */
168 +
169 +/* seconds per day */
170 +#define SPD 24*60*60
171 +
172 +void
173 +localtime(const time_t *timepr, struct tm *r) {
174 +       time_t i;
175 +       time_t timep;
176 +       extern struct timezone sys_tz;
177 +       const unsigned int __spm[12] =
178 +               { 0,
179 +                 (31),
180 +                 (31+28),
181 +                 (31+28+31),
182 +                 (31+28+31+30),
183 +                 (31+28+31+30+31),
184 +                 (31+28+31+30+31+30),
185 +                 (31+28+31+30+31+30+31),
186 +                 (31+28+31+30+31+30+31+31),
187 +                 (31+28+31+30+31+30+31+31+30),
188 +                 (31+28+31+30+31+30+31+31+30+31),
189 +                 (31+28+31+30+31+30+31+31+30+31+30),
190 +               };
191 +       register time_t work;
192 +
193 +       timep = (*timepr) - (sys_tz.tz_minuteswest * 60);
194 +       work=timep%(SPD);
195 +       r->tm_sec=work%60; work/=60;
196 +       r->tm_min=work%60; r->tm_hour=work/60;
197 +       work=timep/(SPD);
198 +       r->tm_wday=(4+work)%7;
199 +       for (i=1970; ; ++i) {
200 +               register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
201 +               if (work>k)
202 +                       work-=k;
203 +               else
204 +                       break;
205 +       }
206 +       r->tm_year=i-1900;
207 +       for (i=11; i && __spm[i]>work; --i) ;
208 +       r->tm_mon=i;
209 +       r->tm_mday=work-__spm[i]+1;
210 +}
211 Index: linux-2.4.35.4/net/ipv4/netfilter/Makefile
212 ===================================================================
213 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Makefile
214 +++ linux-2.4.35.4/net/ipv4/netfilter/Makefile
215 @@ -111,6 +111,7 @@ obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt
216  obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
217  obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
218  obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
219 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
220  obj-$(CONFIG_IP_NF_MATCH_CONDITION) += ipt_condition.o
221  
222  obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
223 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_time.h
224 ===================================================================
225 --- /dev/null
226 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_time.h
227 @@ -0,0 +1,15 @@
228 +#ifndef __ipt_time_h_included__
229 +#define __ipt_time_h_included__
230 +
231 +
232 +struct ipt_time_info {
233 +       u_int8_t  days_match;   /* 1 bit per day. -SMTWTFS                      */
234 +       u_int16_t time_start;   /* 0 < time_start < 23*60+59 = 1439             */
235 +       u_int16_t time_stop;    /* 0:0 < time_stat < 23:59                      */
236 +       u_int8_t  kerneltime;   /* ignore skb time (and use kerneltime) or not. */
237 +       time_t    date_start;
238 +       time_t    date_stop;
239 +};
240 +
241 +
242 +#endif /* __ipt_time_h_included__ */