RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / netfilter / ipt_time.c
blobcff542c86318c40b63a449c7028eef689c2be73f
1 /*
2 This is a module which is used for time matching
3 It is using some modified code from dietlibc (localtime() function)
4 that you can find at http://www.fefe.de/dietlibc/
5 This file is distributed under the terms of the GNU General Public
6 License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
7 2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
8 2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
9 thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
10 2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
11 2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
12 added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
13 2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_time.h>
20 #include <linux/time.h>
22 MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
23 MODULE_DESCRIPTION("Match arrival timestamp/date");
24 MODULE_LICENSE("GPL");
26 struct tm
28 int tm_sec; /* Seconds. [0-60] (1 leap second) */
29 int tm_min; /* Minutes. [0-59] */
30 int tm_hour; /* Hours. [0-23] */
31 int tm_mday; /* Day. [1-31] */
32 int tm_mon; /* Month. [0-11] */
33 int tm_year; /* Year - 1900. */
34 int tm_wday; /* Day of week. [0-6] */
35 int tm_yday; /* Days in year.[0-365] */
36 int tm_isdst; /* DST. [-1/0/1]*/
38 long int tm_gmtoff; /* we don't care, we count from GMT */
39 const char *tm_zone; /* we don't care, we count from GMT */
42 void
43 localtime(const u32 time, struct tm *r);
45 static int
46 match(const struct sk_buff *skb,
47 const struct net_device *in,
48 const struct net_device *out,
49 const struct xt_match *match,
50 const void *matchinfo,
51 int offset,
52 unsigned int protoff,
53 int *hotdrop)
55 const struct ipt_time_info *info = matchinfo; /* match info for rule */
56 struct timeval tv;
57 struct tm currenttime; /* time human readable */
58 u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
59 u_int16_t packet_time;
61 /* We might not have a timestamp, get one */
62 if (skb->tstamp.tv64 == 0)
63 __net_timestamp((struct sk_buff *)skb);
65 skb_get_timestamp(skb, &tv);
67 /* First we make sure we are in the date start-stop boundaries */
68 if ((tv.tv_sec < info->date_start) || (tv.tv_sec > info->date_stop))
69 return 0; /* We are outside the date boundaries */
71 /* Transform the timestamp of the packet, in a human readable form */
72 localtime(tv.tv_sec, &currenttime);
74 /* check if we match this timestamp, we start by the days... */
75 if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
76 return 0; /* the day doesn't match */
78 /* ... check the time now */
79 packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
80 if (info->time_start < info->time_stop) {
81 if (packet_time < info->time_start ||
82 packet_time > info->time_stop)
83 return 0;
84 } else {
85 if (packet_time < info->time_start &&
86 packet_time > info->time_stop)
87 return 0;
90 /* here we match ! */
91 return 1;
94 static int
95 checkentry(const char *tablename,
96 const void *ip,
97 const struct xt_match *match,
98 void *matchinfo,
99 unsigned int hook_mask)
101 struct ipt_time_info *info = matchinfo; /* match info for rule */
103 /* First, check that we are in the correct hooks */
104 if (hook_mask
105 & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
107 printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
108 return 0;
111 /* Now check the coherence of the data ... */
112 if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
113 (info->time_stop > 1439))
115 printk(KERN_WARNING "ipt_time: invalid argument\n");
116 return 0;
119 return 1;
122 static struct ipt_match time_match = {
123 .name = "time",
124 .family = AF_INET,
125 .match = &match,
126 .matchsize = sizeof(struct ipt_time_info),
127 .checkentry = &checkentry,
128 .me = THIS_MODULE
131 static int __init init(void)
133 printk("ipt_time loading\n");
134 return xt_register_match(&time_match);
137 static void __exit fini(void)
139 xt_unregister_match(&time_match);
140 printk("ipt_time unloaded\n");
143 module_init(init);
144 module_exit(fini);
147 /* The part below is borowed and modified from dietlibc */
149 /* seconds per day */
150 #define SPD 24*60*60
152 void
153 localtime(const u32 time, struct tm *r) {
154 u32 i, timep;
155 extern struct timezone sys_tz;
156 const unsigned int __spm[12] =
157 { 0,
158 (31),
159 (31+28),
160 (31+28+31),
161 (31+28+31+30),
162 (31+28+31+30+31),
163 (31+28+31+30+31+30),
164 (31+28+31+30+31+30+31),
165 (31+28+31+30+31+30+31+31),
166 (31+28+31+30+31+30+31+31+30),
167 (31+28+31+30+31+30+31+31+30+31),
168 (31+28+31+30+31+30+31+31+30+31+30),
170 register u32 work;
172 timep = time - (sys_tz.tz_minuteswest * 60);
173 work=timep%(SPD);
174 r->tm_sec=work%60; work/=60;
175 r->tm_min=work%60; r->tm_hour=work/60;
176 work=timep/(SPD);
177 r->tm_wday=(4+work)%7;
178 for (i=1970; ; ++i) {
179 register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
180 if (work>k)
181 work-=k;
182 else
183 break;
185 r->tm_year=i-1900;
186 for (i=11; i && __spm[i]>work; --i) ;
187 r->tm_mon=i;
188 r->tm_mday=work-__spm[i]+1;