1 include/linux/netfilter_ipv4/ipt_time.h | 18 +++
2 net/ipv4/netfilter/Kconfig | 14 ++
3 net/ipv4/netfilter/Makefile | 1
4 net/ipv4/netfilter/ipt_time.c | 179 ++++++++++++++++++++++++++++++++
5 4 files changed, 212 insertions(+)
7 diff -Nur --exclude '*.orig' linux.org/include/linux/netfilter_ipv4/ipt_time.h linux/include/linux/netfilter_ipv4/ipt_time.h
8 --- linux.org/include/linux/netfilter_ipv4/ipt_time.h 1970-01-01 01:00:00.000000000 +0100
9 +++ linux/include/linux/netfilter_ipv4/ipt_time.h 2006-05-04 10:29:15.000000000 +0200
11 +#ifndef __ipt_time_h_included__
12 +#define __ipt_time_h_included__
15 +struct ipt_time_info {
16 + u_int8_t days_match; /* 1 bit per day. -SMTWTFS */
17 + u_int16_t time_start; /* 0 < time_start < 23*60+59 = 1439 */
18 + u_int16_t time_stop; /* 0:0 < time_stat < 23:59 */
20 + /* FIXME: Keep this one for userspace iptables binary compability: */
21 + u_int8_t kerneltime; /* ignore skb time (and use kerneltime) or not. */
28 +#endif /* __ipt_time_h_included__ */
29 diff -Nur --exclude '*.orig' linux.org/net/ipv4/netfilter/Kconfig linux/net/ipv4/netfilter/Kconfig
30 --- linux.org/net/ipv4/netfilter/Kconfig 2006-05-02 23:38:44.000000000 +0200
31 +++ linux/net/ipv4/netfilter/Kconfig 2006-05-04 10:29:15.000000000 +0200
33 Allows altering the ARP packet payload: source and destination
34 hardware and network addresses.
36 +config IP_NF_MATCH_TIME
37 + tristate 'TIME match support'
38 + depends on IP_NF_IPTABLES
40 + This option adds a `time' match, which allows you
41 + to match based on the packet arrival time/date
42 + (arrival time/date at the machine which netfilter is running on) or
43 + departure time/date (for locally generated packets).
45 + If you say Y here, try iptables -m time --help for more information.
47 + If you want to compile it as a module, say M here and read
48 + Documentation/modules.txt. If unsure, say `N'.
52 diff -Nur --exclude '*.orig' linux.org/net/ipv4/netfilter/Makefile linux/net/ipv4/netfilter/Makefile
53 --- linux.org/net/ipv4/netfilter/Makefile 2006-05-02 23:38:44.000000000 +0200
54 +++ linux/net/ipv4/netfilter/Makefile 2006-05-04 10:29:15.000000000 +0200
56 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
57 diff -Nur --exclude '*.orig' linux.org/net/ipv4/netfilter/ipt_time.c linux/net/ipv4/netfilter/ipt_time.c
58 --- linux.org/net/ipv4/netfilter/ipt_time.c 1970-01-01 01:00:00.000000000 +0100
59 +++ linux/net/ipv4/netfilter/ipt_time.c 2006-05-04 10:29:15.000000000 +0200
62 + This is a module which is used for time matching
63 + It is using some modified code from dietlibc (localtime() function)
64 + that you can find at http://www.fefe.de/dietlibc/
65 + This file is distributed under the terms of the GNU General Public
66 + License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
67 + 2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
68 + 2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
69 + thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
70 + 2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
71 + 2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
72 + added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
73 + 2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
76 +#include <linux/module.h>
77 +#include <linux/skbuff.h>
78 +#include <linux/netfilter_ipv4/ip_tables.h>
79 +#include <linux/netfilter_ipv4/ipt_time.h>
80 +#include <linux/time.h>
82 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
83 +MODULE_DESCRIPTION("Match arrival timestamp/date");
84 +MODULE_LICENSE("GPL");
88 + int tm_sec; /* Seconds. [0-60] (1 leap second) */
89 + int tm_min; /* Minutes. [0-59] */
90 + int tm_hour; /* Hours. [0-23] */
91 + int tm_mday; /* Day. [1-31] */
92 + int tm_mon; /* Month. [0-11] */
93 + int tm_year; /* Year - 1900. */
94 + int tm_wday; /* Day of week. [0-6] */
95 + int tm_yday; /* Days in year.[0-365] */
96 + int tm_isdst; /* DST. [-1/0/1]*/
98 + long int tm_gmtoff; /* we don't care, we count from GMT */
99 + const char *tm_zone; /* we don't care, we count from GMT */
103 +localtime(const u32 time, struct tm *r);
106 +match(const struct sk_buff *skb,
107 + const struct net_device *in,
108 + const struct net_device *out,
109 + const void *matchinfo,
111 + unsigned int protoff,
114 + const struct ipt_time_info *info = matchinfo; /* match info for rule */
115 + struct tm currenttime; /* time human readable */
116 + u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
117 + u_int16_t packet_time;
119 + /* We might not have a timestamp, get one */
120 + if (skb->tstamp.off_sec == 0)
121 + __net_timestamp((struct sk_buff *)skb);
123 + /* First we make sure we are in the date start-stop boundaries */
124 + if ((skb->tstamp.off_sec < info->date_start) || (skb->tstamp.off_sec > info->date_stop))
125 + return 0; /* We are outside the date boundaries */
127 + /* Transform the timestamp of the packet, in a human readable form */
128 + localtime(skb->tstamp.off_sec, ¤ttime);
130 + /* check if we match this timestamp, we start by the days... */
131 + if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
132 + return 0; /* the day doesn't match */
134 + /* ... check the time now */
135 + packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
136 + if ((packet_time < info->time_start) || (packet_time > info->time_stop))
139 + /* here we match ! */
144 +checkentry(const char *tablename,
145 + const struct ipt_ip *ip,
147 + unsigned int matchsize,
148 + unsigned int hook_mask)
150 + struct ipt_time_info *info = matchinfo; /* match info for rule */
152 + /* First, check that we are in the correct hooks */
154 + & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
156 + printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
160 + /* Check the size */
161 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_time_info)))
163 + /* Now check the coherence of the data ... */
164 + if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
165 + (info->time_stop > 1439))
167 + printk(KERN_WARNING "ipt_time: invalid argument\n");
174 +static struct ipt_match time_match = {
177 + .checkentry = &checkentry,
181 +static int __init init(void)
183 + printk("ipt_time loading\n");
184 + return ipt_register_match(&time_match);
187 +static void __exit fini(void)
189 + ipt_unregister_match(&time_match);
190 + printk("ipt_time unloaded\n");
197 +/* The part below is borowed and modified from dietlibc */
199 +/* seconds per day */
200 +#define SPD 24*60*60
203 +localtime(const u32 time, struct tm *r) {
205 + extern struct timezone sys_tz;
206 + const unsigned int __spm[12] =
213 + (31+28+31+30+31+30),
214 + (31+28+31+30+31+30+31),
215 + (31+28+31+30+31+30+31+31),
216 + (31+28+31+30+31+30+31+31+30),
217 + (31+28+31+30+31+30+31+31+30+31),
218 + (31+28+31+30+31+30+31+31+30+31+30),
222 + timep = time - (sys_tz.tz_minuteswest * 60);
224 + r->tm_sec=work%60; work/=60;
225 + r->tm_min=work%60; r->tm_hour=work/60;
227 + r->tm_wday=(4+work)%7;
228 + for (i=1970; ; ++i) {
229 + register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
236 + for (i=11; i && __spm[i]>work; --i) ;
238 + r->tm_mday=work-__spm[i]+1;