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");
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 */
43 localtime(const u32 time
, struct tm
*r
);
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
,
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
, ¤ttime
);
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
))
85 checkentry(const char *tablename
,
87 const struct xt_match
*match
,
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 */
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");
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");
112 static struct ipt_match time_match
= {
115 .matchsize
= sizeof(struct ipt_time_info
),
116 .checkentry
= &checkentry
,
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");
136 /* The part below is borowed and modified from dietlibc */
138 /* seconds per day */
142 localtime(const u32 time
, struct tm
*r
) {
144 extern struct timezone sys_tz
;
145 const unsigned int __spm
[12] =
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),
161 timep
= time
- (sys_tz
.tz_minuteswest
* 60);
163 r
->tm_sec
=work
%60; work
/=60;
164 r
->tm_min
=work
%60; r
->tm_hour
=work
/60;
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;
175 for (i
=11; i
&& __spm
[i
]>work
; --i
) ;
177 r
->tm_mday
=work
-__spm
[i
]+1;