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