fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / netfilter / ipt_ecn.c
1 /* IP tables module for matching the value of the IPv4 and TCP ECN bits
2  *
3  * ipt_ecn.c,v 1.3 2002/05/29 15:09:00 laforge Exp
4  *
5  * (C) 2002 by Harald Welte <laforge@gnumonks.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/tcp.h>
15
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter_ipv4/ipt_ecn.h>
18
19 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
20 MODULE_DESCRIPTION("iptables ECN matching module");
21 MODULE_LICENSE("GPL");
22
23 static inline int match_ip(const struct sk_buff *skb,
24                            const struct ipt_ecn_info *einfo)
25 {
26         return ((skb->nh.iph->tos&IPT_ECN_IP_MASK) == einfo->ip_ect);
27 }
28
29 static inline int match_tcp(const struct sk_buff *skb,
30                             const struct ipt_ecn_info *einfo,
31                             int *hotdrop)
32 {
33         struct tcphdr _tcph, *th;
34
35         /* In practice, TCP match does this, so can't fail.  But let's
36          * be good citizens.
37          */
38         th = skb_header_pointer(skb, skb->nh.iph->ihl * 4,
39                                 sizeof(_tcph), &_tcph);
40         if (th == NULL) {
41                 *hotdrop = 0;
42                 return 0;
43         }
44
45         if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
46                 if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
47                         if (th->ece == 1)
48                                 return 0;
49                 } else {
50                         if (th->ece == 0)
51                                 return 0;
52                 }
53         }
54
55         if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
56                 if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
57                         if (th->cwr == 1)
58                                 return 0;
59                 } else {
60                         if (th->cwr == 0)
61                                 return 0;
62                 }
63         }
64
65         return 1;
66 }
67
68 static int match(const struct sk_buff *skb,
69                  const struct net_device *in, const struct net_device *out,
70                  const struct xt_match *match, const void *matchinfo,
71                  int offset, unsigned int protoff, int *hotdrop)
72 {
73         const struct ipt_ecn_info *info = matchinfo;
74
75         if (info->operation & IPT_ECN_OP_MATCH_IP)
76                 if (!match_ip(skb, info))
77                         return 0;
78
79         if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
80                 if (skb->nh.iph->protocol != IPPROTO_TCP)
81                         return 0;
82                 if (!match_tcp(skb, info, hotdrop))
83                         return 0;
84         }
85
86         return 1;
87 }
88
89 static int checkentry(const char *tablename, const void *ip_void,
90                       const struct xt_match *match,
91                       void *matchinfo, unsigned int hook_mask)
92 {
93         const struct ipt_ecn_info *info = matchinfo;
94         const struct ipt_ip *ip = ip_void;
95
96         if (info->operation & IPT_ECN_OP_MATCH_MASK)
97                 return 0;
98
99         if (info->invert & IPT_ECN_OP_MATCH_MASK)
100                 return 0;
101
102         if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
103             && ip->proto != IPPROTO_TCP) {
104                 printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
105                        " non-tcp packets\n");
106                 return 0;
107         }
108
109         return 1;
110 }
111
112 static struct ipt_match ecn_match = {
113         .name           = "ecn",
114         .match          = match,
115         .matchsize      = sizeof(struct ipt_ecn_info),
116         .checkentry     = checkentry,
117         .me             = THIS_MODULE,
118 };
119
120 static int __init ipt_ecn_init(void)
121 {
122         return ipt_register_match(&ecn_match);
123 }
124
125 static void __exit ipt_ecn_fini(void)
126 {
127         ipt_unregister_match(&ecn_match);
128 }
129
130 module_init(ipt_ecn_init);
131 module_exit(ipt_ecn_fini);