[NETFILTER]: x_tables: switch xt_target->checkentry to bool
[deliverable/linux.git] / net / ipv4 / netfilter / ipt_ECN.c
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
10 #include <linux/in.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <net/ip.h>
15 #include <linux/tcp.h>
16 #include <net/checksum.h>
17
18 #include <linux/netfilter/x_tables.h>
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.
27 * return false if there was an error. */
28 static inline bool
29 set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
30 {
31 struct iphdr *iph = ip_hdr(*pskb);
32
33 if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
34 __u8 oldtos;
35 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
36 return false;
37 iph = ip_hdr(*pskb);
38 oldtos = iph->tos;
39 iph->tos &= ~IPT_ECN_IP_MASK;
40 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
41 nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
42 }
43 return true;
44 }
45
46 /* Return false if there was an error. */
47 static inline bool
48 set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
49 {
50 struct tcphdr _tcph, *tcph;
51 __be16 oldval;
52
53 /* Not enought header? */
54 tcph = skb_header_pointer(*pskb, ip_hdrlen(*pskb),
55 sizeof(_tcph), &_tcph);
56 if (!tcph)
57 return false;
58
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)))
63 return true;
64
65 if (!skb_make_writable(pskb, ip_hdrlen(*pskb) + sizeof(*tcph)))
66 return false;
67 tcph = (void *)ip_hdr(*pskb) + ip_hdrlen(*pskb);
68
69 oldval = ((__be16 *)tcph)[6];
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;
74
75 nf_proto_csum_replace2(&tcph->check, *pskb,
76 oldval, ((__be16 *)tcph)[6], 0);
77 return true;
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,
85 const struct xt_target *target,
86 const void *targinfo)
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)
95 && ip_hdr(*pskb)->protocol == IPPROTO_TCP)
96 if (!set_ect_tcp(pskb, einfo))
97 return NF_DROP;
98
99 return XT_CONTINUE;
100 }
101
102 static bool
103 checkentry(const char *tablename,
104 const void *e_void,
105 const struct xt_target *target,
106 void *targinfo,
107 unsigned int hook_mask)
108 {
109 const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
110 const struct ipt_entry *e = e_void;
111
112 if (einfo->operation & IPT_ECN_OP_MASK) {
113 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
114 einfo->operation);
115 return false;
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);
120 return false;
121 }
122 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
123 && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
124 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
125 "non-tcp rule\n");
126 return false;
127 }
128 return true;
129 }
130
131 static struct xt_target ipt_ecn_reg = {
132 .name = "ECN",
133 .family = AF_INET,
134 .target = target,
135 .targetsize = sizeof(struct ipt_ECN_info),
136 .table = "mangle",
137 .checkentry = checkentry,
138 .me = THIS_MODULE,
139 };
140
141 static int __init ipt_ecn_init(void)
142 {
143 return xt_register_target(&ipt_ecn_reg);
144 }
145
146 static void __exit ipt_ecn_fini(void)
147 {
148 xt_unregister_target(&ipt_ecn_reg);
149 }
150
151 module_init(ipt_ecn_init);
152 module_exit(ipt_ecn_fini);
This page took 0.033926 seconds and 5 git commands to generate.