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 | 198 ++++++++++++++++++++++++++++++++
5 4 files changed, 231 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 00:00:00.000000000 +0000
9 +++ linux/include/linux/netfilter_ipv4/ipt_time.h 2006-08-29 12:37:43.000000000 +0000
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/ipt_time.c linux/net/ipv4/netfilter/ipt_time.c
30 --- linux.org/net/ipv4/netfilter/ipt_time.c 1970-01-01 00:00:00.000000000 +0000
31 +++ linux/net/ipv4/netfilter/ipt_time.c 2006-08-29 12:37:43.000000000 +0000
34 + This is a module which is used for time matching
35 + It is using some modified code from dietlibc (localtime() function)
36 + that you can find at http://www.fefe.de/dietlibc/
37 + This file is distributed under the terms of the GNU General Public
38 + License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
39 + 2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
40 + 2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
41 + thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
42 + 2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
43 + 2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
44 + added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
45 + 2004-05-02 Fabrice : added support for date matching, from an idea of Fabien COELHO.
48 +#include <linux/module.h>
49 +#include <linux/skbuff.h>
50 +#include <linux/version.h>
51 +#include <linux/netfilter_ipv4/ip_tables.h>
52 +#include <linux/netfilter_ipv4/ipt_time.h>
53 +#include <linux/time.h>
55 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
56 +MODULE_DESCRIPTION("Match arrival timestamp/date");
57 +MODULE_LICENSE("GPL");
61 + int tm_sec; /* Seconds. [0-60] (1 leap second) */
62 + int tm_min; /* Minutes. [0-59] */
63 + int tm_hour; /* Hours. [0-23] */
64 + int tm_mday; /* Day. [1-31] */
65 + int tm_mon; /* Month. [0-11] */
66 + int tm_year; /* Year - 1900. */
67 + int tm_wday; /* Day of week. [0-6] */
68 + int tm_yday; /* Days in year.[0-365] */
69 + int tm_isdst; /* DST. [-1/0/1]*/
71 + long int tm_gmtoff; /* we don't care, we count from GMT */
72 + const char *tm_zone; /* we don't care, we count from GMT */
76 +localtime(const u32 time, struct tm *r);
79 +match(const struct sk_buff *skb,
80 + const struct net_device *in,
81 + const struct net_device *out,
82 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
83 + const struct xt_match *match,
85 + const void *matchinfo,
87 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
88 + unsigned int protoff,
92 + const struct ipt_time_info *info = matchinfo; /* match info for rule */
93 + struct tm currenttime; /* time human readable */
94 + u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
95 + u_int16_t packet_time;
97 + /* We might not have a timestamp, get one */
98 + if (skb->tstamp.off_sec == 0)
99 + __net_timestamp((struct sk_buff *)skb);
101 + /* First we make sure we are in the date start-stop boundaries */
102 + if ((skb->tstamp.off_sec < info->date_start) || (skb->tstamp.off_sec > info->date_stop))
103 + return 0; /* We are outside the date boundaries */
105 + /* Transform the timestamp of the packet, in a human readable form */
106 + localtime(skb->tstamp.off_sec, ¤ttime);
108 + /* check if we match this timestamp, we start by the days... */
109 + if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
110 + return 0; /* the day doesn't match */
112 + /* ... check the time now */
113 + packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
114 + if ((packet_time < info->time_start) || (packet_time > info->time_stop))
117 + /* here we match ! */
122 +checkentry(const char *tablename,
123 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
126 + const struct ipt_ip *ip,
128 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
129 + const struct xt_match *match,
132 + unsigned int matchsize,
133 + unsigned int hook_mask)
135 + struct ipt_time_info *info = matchinfo; /* match info for rule */
137 + /* First, check that we are in the correct hooks */
139 + & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
141 + printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
145 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
146 + /* Check the size */
147 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_time_info)))
151 + /* Now check the coherence of the data ... */
152 + if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
153 + (info->time_stop > 1439))
155 + printk(KERN_WARNING "ipt_time: invalid argument\n");
162 +static struct ipt_match time_match = {
165 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
166 + .matchsize = sizeof(struct ipt_time_info),
168 + .checkentry = &checkentry,
172 +static int __init init(void)
174 + printk("ipt_time loading\n");
175 + return ipt_register_match(&time_match);
178 +static void __exit fini(void)
180 + ipt_unregister_match(&time_match);
181 + printk("ipt_time unloaded\n");
188 +/* The part below is borowed and modified from dietlibc */
190 +/* seconds per day */
191 +#define SPD 24*60*60
194 +localtime(const u32 time, struct tm *r) {
196 + extern struct timezone sys_tz;
197 + const unsigned int __spm[12] =
204 + (31+28+31+30+31+30),
205 + (31+28+31+30+31+30+31),
206 + (31+28+31+30+31+30+31+31),
207 + (31+28+31+30+31+30+31+31+30),
208 + (31+28+31+30+31+30+31+31+30+31),
209 + (31+28+31+30+31+30+31+31+30+31+30),
213 + timep = time - (sys_tz.tz_minuteswest * 60);
215 + r->tm_sec=work%60; work/=60;
216 + r->tm_min=work%60; r->tm_hour=work/60;
218 + r->tm_wday=(4+work)%7;
219 + for (i=1970; ; ++i) {
220 + register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
227 + for (i=11; i && __spm[i]>work; --i) ;
229 + r->tm_mday=work-__spm[i]+1;
231 diff -Nur --exclude '*.orig' linux.org/net/ipv4/netfilter/Kconfig linux/net/ipv4/netfilter/Kconfig
232 --- linux.org/net/ipv4/netfilter/Kconfig 2006-06-18 01:49:35.000000000 +0000
233 +++ linux/net/ipv4/netfilter/Kconfig 2006-08-29 12:37:43.000000000 +0000
235 Allows altering the ARP packet payload: source and destination
236 hardware and network addresses.
238 +config IP_NF_MATCH_TIME
239 + tristate 'TIME match support'
240 + depends on IP_NF_IPTABLES
242 + This option adds a `time' match, which allows you
243 + to match based on the packet arrival time/date
244 + (arrival time/date at the machine which netfilter is running on) or
245 + departure time/date (for locally generated packets).
247 + If you say Y here, try iptables -m time --help for more information.
249 + If you want to compile it as a module, say M here and read
250 + Documentation/modules.txt. If unsure, say `N'.
254 diff -Nur --exclude '*.orig' linux.org/net/ipv4/netfilter/Makefile linux/net/ipv4/netfilter/Makefile
255 --- linux.org/net/ipv4/netfilter/Makefile 2006-06-18 01:49:35.000000000 +0000
256 +++ linux/net/ipv4/netfilter/Makefile 2006-08-29 12:37:43.000000000 +0000
258 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o