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 | |
1da177e4 LT |
29 | set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo) |
30 | { | |
eddc9ec5 | 31 | struct iphdr *iph = ip_hdr(*pskb); |
1da177e4 | 32 | |
da878c8e | 33 | if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) { |
43bc0ca7 | 34 | __u8 oldtos; |
089af26c | 35 | if (!skb_make_writable(pskb, sizeof(struct iphdr))) |
e1931b78 | 36 | return false; |
eddc9ec5 | 37 | iph = ip_hdr(*pskb); |
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 | |
84fa7933 | 48 | set_ect_tcp(struct sk_buff **pskb, 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? */ | |
c9bdd4b5 | 54 | tcph = skb_header_pointer(*pskb, ip_hdrlen(*pskb), |
1da177e4 LT |
55 | sizeof(_tcph), &_tcph); |
56 | if (!tcph) | |
e1931b78 | 57 | return false; |
1da177e4 | 58 | |
fd841326 PM |
59 | if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) || |
60 | tcph->ece == einfo->proto.tcp.ece) && | |
61 | ((!(einfo->operation & IPT_ECN_OP_SET_CWR) || | |
62 | tcph->cwr == einfo->proto.tcp.cwr))) | |
e1931b78 | 63 | return true; |
1da177e4 | 64 | |
c9bdd4b5 | 65 | if (!skb_make_writable(pskb, ip_hdrlen(*pskb) + sizeof(*tcph))) |
e1931b78 | 66 | return false; |
eddc9ec5 | 67 | tcph = (void *)ip_hdr(*pskb) + ip_hdrlen(*pskb); |
1da177e4 | 68 | |
6a19d614 | 69 | oldval = ((__be16 *)tcph)[6]; |
1da177e4 LT |
70 | if (einfo->operation & IPT_ECN_OP_SET_ECE) |
71 | tcph->ece = einfo->proto.tcp.ece; | |
72 | if (einfo->operation & IPT_ECN_OP_SET_CWR) | |
73 | tcph->cwr = einfo->proto.tcp.cwr; | |
1da177e4 | 74 | |
43bc0ca7 AV |
75 | nf_proto_csum_replace2(&tcph->check, *pskb, |
76 | oldval, ((__be16 *)tcph)[6], 0); | |
e1931b78 | 77 | return true; |
1da177e4 LT |
78 | } |
79 | ||
80 | static unsigned int | |
81 | target(struct sk_buff **pskb, | |
82 | const struct net_device *in, | |
83 | const struct net_device *out, | |
84 | unsigned int hooknum, | |
c4986734 | 85 | const struct xt_target *target, |
fe1cb108 | 86 | const void *targinfo) |
1da177e4 LT |
87 | { |
88 | const struct ipt_ECN_info *einfo = targinfo; | |
89 | ||
90 | if (einfo->operation & IPT_ECN_OP_SET_IP) | |
91 | if (!set_ect_ip(pskb, einfo)) | |
92 | return NF_DROP; | |
93 | ||
94 | if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR) | |
eddc9ec5 | 95 | && ip_hdr(*pskb)->protocol == IPPROTO_TCP) |
84fa7933 | 96 | if (!set_ect_tcp(pskb, einfo)) |
1da177e4 LT |
97 | return NF_DROP; |
98 | ||
6709dbbb | 99 | return XT_CONTINUE; |
1da177e4 LT |
100 | } |
101 | ||
e1931b78 | 102 | static bool |
1da177e4 | 103 | checkentry(const char *tablename, |
2e4e6a17 | 104 | const void *e_void, |
c4986734 | 105 | const struct xt_target *target, |
e905a9ed YH |
106 | void *targinfo, |
107 | unsigned int hook_mask) | |
1da177e4 LT |
108 | { |
109 | const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo; | |
2e4e6a17 | 110 | const struct ipt_entry *e = e_void; |
1da177e4 | 111 | |
1da177e4 LT |
112 | if (einfo->operation & IPT_ECN_OP_MASK) { |
113 | printk(KERN_WARNING "ECN: unsupported ECN operation %x\n", | |
114 | einfo->operation); | |
e1931b78 | 115 | return false; |
1da177e4 LT |
116 | } |
117 | if (einfo->ip_ect & ~IPT_ECN_IP_MASK) { | |
118 | printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n", | |
119 | einfo->ip_ect); | |
e1931b78 | 120 | return false; |
1da177e4 | 121 | } |
1da177e4 | 122 | if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)) |
6709dbbb | 123 | && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) { |
1da177e4 LT |
124 | printk(KERN_WARNING "ECN: cannot use TCP operations on a " |
125 | "non-tcp rule\n"); | |
e1931b78 | 126 | return false; |
1da177e4 | 127 | } |
e1931b78 | 128 | return true; |
1da177e4 LT |
129 | } |
130 | ||
6709dbbb | 131 | static struct xt_target ipt_ecn_reg = { |
1da177e4 | 132 | .name = "ECN", |
6709dbbb | 133 | .family = AF_INET, |
1da177e4 | 134 | .target = target, |
1d5cd909 PM |
135 | .targetsize = sizeof(struct ipt_ECN_info), |
136 | .table = "mangle", | |
1da177e4 LT |
137 | .checkentry = checkentry, |
138 | .me = THIS_MODULE, | |
139 | }; | |
140 | ||
65b4b4e8 | 141 | static int __init ipt_ecn_init(void) |
1da177e4 | 142 | { |
6709dbbb | 143 | return xt_register_target(&ipt_ecn_reg); |
1da177e4 LT |
144 | } |
145 | ||
65b4b4e8 | 146 | static void __exit ipt_ecn_fini(void) |
1da177e4 | 147 | { |
6709dbbb | 148 | xt_unregister_target(&ipt_ecn_reg); |
1da177e4 LT |
149 | } |
150 | ||
65b4b4e8 AM |
151 | module_init(ipt_ecn_init); |
152 | module_exit(ipt_ecn_fini); |