Cache xtime every call to update_wall_time
[usb.git] / net / ipv4 / netfilter / ipt_ecn.c
blobd6925c6740692a730c64fc6313bc788fd8a72e45
1 /* IP tables module for matching the value of the IPv4 and TCP ECN bits
3 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
10 #include <linux/in.h>
11 #include <linux/ip.h>
12 #include <net/ip.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/tcp.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_ecn.h>
21 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
22 MODULE_DESCRIPTION("iptables ECN matching module");
23 MODULE_LICENSE("GPL");
25 static inline bool match_ip(const struct sk_buff *skb,
26 const struct ipt_ecn_info *einfo)
28 return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect;
31 static inline bool match_tcp(const struct sk_buff *skb,
32 const struct ipt_ecn_info *einfo,
33 bool *hotdrop)
35 struct tcphdr _tcph;
36 const struct tcphdr *th;
38 /* In practice, TCP match does this, so can't fail. But let's
39 * be good citizens.
41 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
42 if (th == NULL) {
43 *hotdrop = false;
44 return false;
47 if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
48 if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
49 if (th->ece == 1)
50 return false;
51 } else {
52 if (th->ece == 0)
53 return false;
57 if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
58 if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
59 if (th->cwr == 1)
60 return false;
61 } else {
62 if (th->cwr == 0)
63 return false;
67 return true;
70 static bool match(const struct sk_buff *skb,
71 const struct net_device *in, const struct net_device *out,
72 const struct xt_match *match, const void *matchinfo,
73 int offset, unsigned int protoff, bool *hotdrop)
75 const struct ipt_ecn_info *info = matchinfo;
77 if (info->operation & IPT_ECN_OP_MATCH_IP)
78 if (!match_ip(skb, info))
79 return false;
81 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
82 if (ip_hdr(skb)->protocol != IPPROTO_TCP)
83 return false;
84 if (!match_tcp(skb, info, hotdrop))
85 return false;
88 return true;
91 static bool checkentry(const char *tablename, const void *ip_void,
92 const struct xt_match *match,
93 void *matchinfo, unsigned int hook_mask)
95 const struct ipt_ecn_info *info = matchinfo;
96 const struct ipt_ip *ip = ip_void;
98 if (info->operation & IPT_ECN_OP_MATCH_MASK)
99 return false;
101 if (info->invert & IPT_ECN_OP_MATCH_MASK)
102 return false;
104 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
105 && ip->proto != IPPROTO_TCP) {
106 printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
107 " non-tcp packets\n");
108 return false;
111 return true;
114 static struct xt_match ecn_match __read_mostly = {
115 .name = "ecn",
116 .family = AF_INET,
117 .match = match,
118 .matchsize = sizeof(struct ipt_ecn_info),
119 .checkentry = checkentry,
120 .me = THIS_MODULE,
123 static int __init ipt_ecn_init(void)
125 return xt_register_match(&ecn_match);
128 static void __exit ipt_ecn_fini(void)
130 xt_unregister_match(&ecn_match);
133 module_init(ipt_ecn_init);
134 module_exit(ipt_ecn_fini);