]>
Commit | Line | Data |
---|---|---|
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> | |
e905a9ed | 4 | * |
1da177e4 | 5 | * This program is free software; you can redistribute it and/or modify |
e905a9ed | 6 | * it under the terms of the GNU General Public License version 2 as |
1da177e4 | 7 | * published by the Free Software Foundation. |
1da177e4 LT |
8 | */ |
9 | ||
6709dbbb | 10 | #include <linux/in.h> |
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/ip.h> | |
c9bdd4b5 | 14 | #include <net/ip.h> |
1da177e4 LT |
15 | #include <linux/tcp.h> |
16 | #include <net/checksum.h> | |
17 | ||
6709dbbb | 18 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
19 | #include <linux/netfilter_ipv4/ip_tables.h> |
20 | #include <linux/netfilter_ipv4/ipt_ECN.h> | |
21 | ||
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
24 | MODULE_DESCRIPTION("iptables ECN modification module"); | |
25 | ||
26 | /* set ECT codepoint from IP header. | |
e1931b78 JE |
27 | * return false if there was an error. */ |
28 | static inline bool | |
3db05fea | 29 | set_ect_ip(struct sk_buff *skb, const struct ipt_ECN_info *einfo) |
1da177e4 | 30 | { |
3db05fea | 31 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 32 | |
da878c8e | 33 | if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) { |
43bc0ca7 | 34 | __u8 oldtos; |
3db05fea | 35 | if (!skb_make_writable(skb, sizeof(struct iphdr))) |
e1931b78 | 36 | return false; |
3db05fea | 37 | iph = ip_hdr(skb); |
da878c8e PM |
38 | oldtos = iph->tos; |
39 | iph->tos &= ~IPT_ECN_IP_MASK; | |
40 | iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK); | |
43bc0ca7 | 41 | nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos)); |
e905a9ed | 42 | } |
e1931b78 | 43 | return true; |
1da177e4 LT |
44 | } |
45 | ||
e1931b78 JE |
46 | /* Return false if there was an error. */ |
47 | static inline bool | |
3db05fea | 48 | set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo) |
1da177e4 LT |
49 | { |
50 | struct tcphdr _tcph, *tcph; | |
6a19d614 | 51 | __be16 oldval; |
1da177e4 LT |
52 | |
53 | /* Not enought header? */ | |
3db05fea | 54 | tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph); |
1da177e4 | 55 | if (!tcph) |
e1931b78 | 56 | return false; |
1da177e4 | 57 | |
fd841326 PM |
58 | if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) || |
59 | tcph->ece == einfo->proto.tcp.ece) && | |
7c4e36bc JE |
60 | (!(einfo->operation & IPT_ECN_OP_SET_CWR) || |
61 | tcph->cwr == einfo->proto.tcp.cwr)) | |
e1931b78 | 62 | return true; |
1da177e4 | 63 | |
3db05fea | 64 | if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph))) |
e1931b78 | 65 | return false; |
3db05fea | 66 | tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb); |
1da177e4 | 67 | |
6a19d614 | 68 | oldval = ((__be16 *)tcph)[6]; |
1da177e4 LT |
69 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
70 | tcph->ece = einfo->proto.tcp.ece; | |
71 | if (einfo->operation & IPT_ECN_OP_SET_CWR) | |
72 | tcph->cwr = einfo->proto.tcp.cwr; | |
1da177e4 | 73 | |
3db05fea | 74 | nf_proto_csum_replace2(&tcph->check, skb, |
43bc0ca7 | 75 | oldval, ((__be16 *)tcph)[6], 0); |
e1931b78 | 76 | return true; |
1da177e4 LT |
77 | } |
78 | ||
79 | static unsigned int | |
3db05fea | 80 | target(struct sk_buff *skb, |
1da177e4 LT |
81 | const struct net_device *in, |
82 | const struct net_device *out, | |
83 | unsigned int hooknum, | |
c4986734 | 84 | const struct xt_target *target, |
fe1cb108 | 85 | const void *targinfo) |
1da177e4 LT |
86 | { |
87 | const struct ipt_ECN_info *einfo = targinfo; | |
88 | ||
89 | if (einfo->operation & IPT_ECN_OP_SET_IP) | |
3db05fea | 90 | if (!set_ect_ip(skb, einfo)) |
1da177e4 LT |
91 | return NF_DROP; |
92 | ||
93 | if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR) | |
3db05fea HX |
94 | && ip_hdr(skb)->protocol == IPPROTO_TCP) |
95 | if (!set_ect_tcp(skb, einfo)) | |
1da177e4 LT |
96 | return NF_DROP; |
97 | ||
6709dbbb | 98 | return XT_CONTINUE; |
1da177e4 LT |
99 | } |
100 | ||
e1931b78 | 101 | static bool |
1da177e4 | 102 | checkentry(const char *tablename, |
2e4e6a17 | 103 | const void *e_void, |
c4986734 | 104 | const struct xt_target *target, |
e905a9ed YH |
105 | void *targinfo, |
106 | unsigned int hook_mask) | |
1da177e4 LT |
107 | { |
108 | const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo; | |
2e4e6a17 | 109 | const struct ipt_entry *e = e_void; |
1da177e4 | 110 | |
1da177e4 LT |
111 | if (einfo->operation & IPT_ECN_OP_MASK) { |
112 | printk(KERN_WARNING "ECN: unsupported ECN operation %x\n", | |
113 | einfo->operation); | |
e1931b78 | 114 | return false; |
1da177e4 LT |
115 | } |
116 | if (einfo->ip_ect & ~IPT_ECN_IP_MASK) { | |
117 | printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n", | |
118 | einfo->ip_ect); | |
e1931b78 | 119 | return false; |
1da177e4 | 120 | } |
1da177e4 | 121 | if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)) |
6709dbbb | 122 | && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) { |
1da177e4 LT |
123 | printk(KERN_WARNING "ECN: cannot use TCP operations on a " |
124 | "non-tcp rule\n"); | |
e1931b78 | 125 | return false; |
1da177e4 | 126 | } |
e1931b78 | 127 | return true; |
1da177e4 LT |
128 | } |
129 | ||
9f15c530 | 130 | static struct xt_target ipt_ecn_reg __read_mostly = { |
1da177e4 | 131 | .name = "ECN", |
6709dbbb | 132 | .family = AF_INET, |
1da177e4 | 133 | .target = target, |
1d5cd909 PM |
134 | .targetsize = sizeof(struct ipt_ECN_info), |
135 | .table = "mangle", | |
1da177e4 LT |
136 | .checkentry = checkentry, |
137 | .me = THIS_MODULE, | |
138 | }; | |
139 | ||
65b4b4e8 | 140 | static int __init ipt_ecn_init(void) |
1da177e4 | 141 | { |
6709dbbb | 142 | return xt_register_target(&ipt_ecn_reg); |
1da177e4 LT |
143 | } |
144 | ||
65b4b4e8 | 145 | static void __exit ipt_ecn_fini(void) |
1da177e4 | 146 | { |
6709dbbb | 147 | xt_unregister_target(&ipt_ecn_reg); |
1da177e4 LT |
148 | } |
149 | ||
65b4b4e8 AM |
150 | module_init(ipt_ecn_init); |
151 | module_exit(ipt_ecn_fini); |