]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter/ipt_ECN.c
[NET]: Replace CHECKSUM_HW by CHECKSUM_PARTIAL/CHECKSUM_COMPLETE
[net-next-2.6.git] / net / ipv4 / netfilter / ipt_ECN.c
CommitLineData
1da177e4
LT
1/* iptables module for the IPv4 and TCP ECN bits, Version 1.5
2 *
3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
4 *
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 *
9 * ipt_ECN.c,v 1.5 2002/08/18 19:36:51 laforge Exp
10*/
11
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/ip.h>
15#include <linux/tcp.h>
16#include <net/checksum.h>
17
18#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv4/ipt_ECN.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
23MODULE_DESCRIPTION("iptables ECN modification module");
24
25/* set ECT codepoint from IP header.
26 * return 0 if there was an error. */
27static inline int
28set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
29{
30 if (((*pskb)->nh.iph->tos & IPT_ECN_IP_MASK)
31 != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
32 u_int16_t diffs[2];
33
089af26c 34 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
1da177e4
LT
35 return 0;
36
37 diffs[0] = htons((*pskb)->nh.iph->tos) ^ 0xFFFF;
38 (*pskb)->nh.iph->tos &= ~IPT_ECN_IP_MASK;
39 (*pskb)->nh.iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
40 diffs[1] = htons((*pskb)->nh.iph->tos);
41 (*pskb)->nh.iph->check
42 = csum_fold(csum_partial((char *)diffs,
43 sizeof(diffs),
44 (*pskb)->nh.iph->check
45 ^0xFFFF));
1da177e4
LT
46 }
47 return 1;
48}
49
50/* Return 0 if there was an error. */
51static inline int
84fa7933 52set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
1da177e4
LT
53{
54 struct tcphdr _tcph, *tcph;
55 u_int16_t diffs[2];
56
57 /* Not enought header? */
58 tcph = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
59 sizeof(_tcph), &_tcph);
60 if (!tcph)
61 return 0;
62
fd841326
PM
63 if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
64 tcph->ece == einfo->proto.tcp.ece) &&
65 ((!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
66 tcph->cwr == einfo->proto.tcp.cwr)))
1da177e4
LT
67 return 1;
68
089af26c 69 if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
1da177e4
LT
70 return 0;
71 tcph = (void *)(*pskb)->nh.iph + (*pskb)->nh.iph->ihl*4;
72
84fa7933
PM
73 if (((*pskb)->ip_summed == CHECKSUM_PARTIAL ||
74 (*pskb)->ip_summed == CHECKSUM_COMPLETE) &&
75 skb_checksum_help(*pskb))
f93592ff
PM
76 return 0;
77
1da177e4
LT
78 diffs[0] = ((u_int16_t *)tcph)[6];
79 if (einfo->operation & IPT_ECN_OP_SET_ECE)
80 tcph->ece = einfo->proto.tcp.ece;
81 if (einfo->operation & IPT_ECN_OP_SET_CWR)
82 tcph->cwr = einfo->proto.tcp.cwr;
83 diffs[1] = ((u_int16_t *)tcph)[6];
84 diffs[0] = diffs[0] ^ 0xFFFF;
85
f93592ff 86 if ((*pskb)->ip_summed != CHECKSUM_UNNECESSARY)
1da177e4
LT
87 tcph->check = csum_fold(csum_partial((char *)diffs,
88 sizeof(diffs),
89 tcph->check^0xFFFF));
1da177e4
LT
90 return 1;
91}
92
93static unsigned int
94target(struct sk_buff **pskb,
95 const struct net_device *in,
96 const struct net_device *out,
97 unsigned int hooknum,
c4986734 98 const struct xt_target *target,
1da177e4
LT
99 const void *targinfo,
100 void *userinfo)
101{
102 const struct ipt_ECN_info *einfo = targinfo;
103
104 if (einfo->operation & IPT_ECN_OP_SET_IP)
105 if (!set_ect_ip(pskb, einfo))
106 return NF_DROP;
107
108 if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
109 && (*pskb)->nh.iph->protocol == IPPROTO_TCP)
84fa7933 110 if (!set_ect_tcp(pskb, einfo))
1da177e4
LT
111 return NF_DROP;
112
113 return IPT_CONTINUE;
114}
115
116static int
117checkentry(const char *tablename,
2e4e6a17 118 const void *e_void,
c4986734 119 const struct xt_target *target,
1da177e4
LT
120 void *targinfo,
121 unsigned int targinfosize,
122 unsigned int hook_mask)
123{
124 const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
2e4e6a17 125 const struct ipt_entry *e = e_void;
1da177e4 126
1da177e4
LT
127 if (einfo->operation & IPT_ECN_OP_MASK) {
128 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
129 einfo->operation);
130 return 0;
131 }
132 if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
133 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
134 einfo->ip_ect);
135 return 0;
136 }
1da177e4
LT
137 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
138 && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & IPT_INV_PROTO))) {
139 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
140 "non-tcp rule\n");
141 return 0;
142 }
1da177e4
LT
143 return 1;
144}
145
146static struct ipt_target ipt_ecn_reg = {
147 .name = "ECN",
148 .target = target,
1d5cd909
PM
149 .targetsize = sizeof(struct ipt_ECN_info),
150 .table = "mangle",
1da177e4
LT
151 .checkentry = checkentry,
152 .me = THIS_MODULE,
153};
154
65b4b4e8 155static int __init ipt_ecn_init(void)
1da177e4
LT
156{
157 return ipt_register_target(&ipt_ecn_reg);
158}
159
65b4b4e8 160static void __exit ipt_ecn_fini(void)
1da177e4
LT
161{
162 ipt_unregister_target(&ipt_ecn_reg);
163}
164
65b4b4e8
AM
165module_init(ipt_ecn_init);
166module_exit(ipt_ecn_fini);