MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / net / ipv4 / netfilter / ipt_time.c
blob428968b8aa9acb38b482349e4ab0f8ca9c4d4f6a
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 static 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 tm currenttime; /* time human readable */
57 u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
58 u_int16_t packet_time;
60 /* We might not have a timestamp, get one */
61 if (skb->tstamp.off_sec == 0)
62 __net_timestamp((struct sk_buff *)skb);
64 /* First we make sure we are in the date start-stop boundaries */
65 if ((skb->tstamp.off_sec < info->date_start) || (skb->tstamp.off_sec > info->date_stop))
66 return 0; /* We are outside the date boundaries */
68 /* Transform the timestamp of the packet, in a human readable form */
69 localtime(skb->tstamp.off_sec, &currenttime);
71 /* check if we match this timestamp, we start by the days... */
72 if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
73 return 0; /* the day doesn't match */
75 /* ... check the time now */
76 packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
77 if ((packet_time < info->time_start) || (packet_time > info->time_stop))
78 return 0;
80 /* here we match ! */
81 return 1;
84 static int
85 checkentry(const char *tablename,
86 const void *ip,
87 const struct xt_match *match,
88 void *matchinfo,
89 unsigned int hook_mask)
91 struct ipt_time_info *info = matchinfo; /* match info for rule */
93 /* First, check that we are in the correct hooks */
94 if (hook_mask
95 & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
97 printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
98 return 0;
101 /* Now check the coherence of the data ... */
102 if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
103 (info->time_stop > 1439))
105 printk(KERN_WARNING "ipt_time: invalid argument\n");
106 return 0;
109 return 1;
112 static struct ipt_match time_match = {
113 .name = "time",
114 .match = &match,
115 .matchsize = sizeof(struct ipt_time_info),
116 .checkentry = &checkentry,
117 .me = THIS_MODULE
120 static int __init init(void)
122 printk("ipt_time loading\n");
123 return ipt_register_match(&time_match);
126 static void __exit fini(void)
128 ipt_unregister_match(&time_match);
129 printk("ipt_time unloaded\n");
132 module_init(init);
133 module_exit(fini);
136 /* The part below is borowed and modified from dietlibc */
138 /* seconds per day */
139 #define SPD 24*60*60
141 static void
142 localtime(const u32 time, struct tm *r) {
143 u32 i, timep;
144 extern struct timezone sys_tz;
145 const unsigned int __spm[12] =
146 { 0,
147 (31),
148 (31+28),
149 (31+28+31),
150 (31+28+31+30),
151 (31+28+31+30+31),
152 (31+28+31+30+31+30),
153 (31+28+31+30+31+30+31),
154 (31+28+31+30+31+30+31+31),
155 (31+28+31+30+31+30+31+31+30),
156 (31+28+31+30+31+30+31+31+30+31),
157 (31+28+31+30+31+30+31+31+30+31+30),
159 register u32 work;
161 timep = time - (sys_tz.tz_minuteswest * 60);
162 work=timep%(SPD);
163 r->tm_sec=work%60; work/=60;
164 r->tm_min=work%60; r->tm_hour=work/60;
165 work=timep/(SPD);
166 r->tm_wday=(4+work)%7;
167 for (i=1970; ; ++i) {
168 register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
169 if (work>k)
170 work-=k;
171 else
172 break;
174 r->tm_year=i-1900;
175 for (i=11; i && __spm[i]>work; --i) ;
176 r->tm_mon=i;
177 r->tm_mday=work-__spm[i]+1;