www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / 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;
34
35         /* In practice, TCP match does this, so can't fail.  But let's
36            be good citizens. */
37         if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0) {
38                 *hotdrop = 0;
39                 return 0;
40         }
41
42         if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
43                 if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
44                         if (tcph.ece == 1)
45                                 return 0;
46                 } else {
47                         if (tcph.ece == 0)
48                                 return 0;
49                 }
50         }
51
52         if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
53                 if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
54                         if (tcph.cwr == 1)
55                                 return 0;
56                 } else {
57                         if (tcph.cwr == 0)
58                                 return 0;
59                 }
60         }
61
62         return 1;
63 }
64
65 static int match(const struct sk_buff *skb, const struct net_device *in,
66                  const struct net_device *out, const void *matchinfo,
67                  int offset, int *hotdrop)
68 {
69         const struct ipt_ecn_info *info = matchinfo;
70
71         if (info->operation & IPT_ECN_OP_MATCH_IP)
72                 if (!match_ip(skb, info))
73                         return 0;
74
75         if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
76                 if (skb->nh.iph->protocol != IPPROTO_TCP)
77                         return 0;
78                 if (!match_tcp(skb, info, hotdrop))
79                         return 0;
80         }
81
82         return 1;
83 }
84
85 static int checkentry(const char *tablename, const struct ipt_ip *ip,
86                       void *matchinfo, unsigned int matchsize,
87                       unsigned int hook_mask)
88 {
89         const struct ipt_ecn_info *info = matchinfo;
90
91         if (matchsize != IPT_ALIGN(sizeof(struct ipt_ecn_info)))
92                 return 0;
93
94         if (info->operation & IPT_ECN_OP_MATCH_MASK)
95                 return 0;
96
97         if (info->invert & IPT_ECN_OP_MATCH_MASK)
98                 return 0;
99
100         if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
101             && ip->proto != IPPROTO_TCP) {
102                 printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
103                        " non-tcp packets\n");
104                 return 0;
105         }
106
107         return 1;
108 }
109
110 static struct ipt_match ecn_match = {
111         .name           = "ecn",
112         .match          = &match,
113         .checkentry     = &checkentry,
114         .me             = THIS_MODULE,
115 };
116
117 static int __init init(void)
118 {
119         return ipt_register_match(&ecn_match);
120 }
121
122 static void __exit fini(void)
123 {
124         ipt_unregister_match(&ecn_match);
125 }
126
127 module_init(init);
128 module_exit(fini);