]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ipv4/netfilter/ipt_ecn.c
[NETFILTER]: x_tables: switch xt_target->checkentry to bool
[mirror_ubuntu-jammy-kernel.git] / net / ipv4 / netfilter / ipt_ecn.c
CommitLineData
1da177e4
LT
1/* IP tables module for matching the value of the IPv4 and TCP ECN bits
2 *
1da177e4
LT
3 * (C) 2002 by Harald Welte <laforge@gnumonks.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
6709dbbb
JE
10#include <linux/in.h>
11#include <linux/ip.h>
c9bdd4b5 12#include <net/ip.h>
1da177e4
LT
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/tcp.h>
16
6709dbbb 17#include <linux/netfilter/x_tables.h>
1da177e4
LT
18#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv4/ipt_ecn.h>
20
21MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
22MODULE_DESCRIPTION("iptables ECN matching module");
23MODULE_LICENSE("GPL");
24
1d93a9cb
JE
25static inline bool match_ip(const struct sk_buff *skb,
26 const struct ipt_ecn_info *einfo)
1da177e4 27{
eddc9ec5 28 return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect;
1da177e4
LT
29}
30
1d93a9cb
JE
31static inline bool match_tcp(const struct sk_buff *skb,
32 const struct ipt_ecn_info *einfo,
33 bool *hotdrop)
1da177e4
LT
34{
35 struct tcphdr _tcph, *th;
36
37 /* In practice, TCP match does this, so can't fail. But let's
38 * be good citizens.
39 */
c9bdd4b5 40 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
1da177e4 41 if (th == NULL) {
cff533ac 42 *hotdrop = false;
1d93a9cb 43 return false;
1da177e4
LT
44 }
45
46 if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
47 if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
48 if (th->ece == 1)
1d93a9cb 49 return false;
1da177e4
LT
50 } else {
51 if (th->ece == 0)
1d93a9cb 52 return false;
1da177e4
LT
53 }
54 }
55
56 if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
57 if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
58 if (th->cwr == 1)
1d93a9cb 59 return false;
1da177e4
LT
60 } else {
61 if (th->cwr == 0)
1d93a9cb 62 return false;
1da177e4
LT
63 }
64 }
65
1d93a9cb 66 return true;
1da177e4
LT
67}
68
1d93a9cb
JE
69static bool match(const struct sk_buff *skb,
70 const struct net_device *in, const struct net_device *out,
71 const struct xt_match *match, const void *matchinfo,
72 int offset, unsigned int protoff, bool *hotdrop)
1da177e4
LT
73{
74 const struct ipt_ecn_info *info = matchinfo;
75
76 if (info->operation & IPT_ECN_OP_MATCH_IP)
77 if (!match_ip(skb, info))
1d93a9cb 78 return false;
1da177e4
LT
79
80 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
eddc9ec5 81 if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1d93a9cb 82 return false;
1da177e4 83 if (!match_tcp(skb, info, hotdrop))
1d93a9cb 84 return false;
1da177e4
LT
85 }
86
1d93a9cb 87 return true;
1da177e4
LT
88}
89
ccb79bdc
JE
90static bool checkentry(const char *tablename, const void *ip_void,
91 const struct xt_match *match,
92 void *matchinfo, unsigned int hook_mask)
1da177e4
LT
93{
94 const struct ipt_ecn_info *info = matchinfo;
2e4e6a17 95 const struct ipt_ip *ip = ip_void;
1da177e4 96
1da177e4 97 if (info->operation & IPT_ECN_OP_MATCH_MASK)
ccb79bdc 98 return false;
1da177e4
LT
99
100 if (info->invert & IPT_ECN_OP_MATCH_MASK)
ccb79bdc 101 return false;
1da177e4
LT
102
103 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
104 && ip->proto != IPPROTO_TCP) {
105 printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
106 " non-tcp packets\n");
ccb79bdc 107 return false;
1da177e4
LT
108 }
109
ccb79bdc 110 return true;
1da177e4
LT
111}
112
6709dbbb 113static struct xt_match ecn_match = {
1da177e4 114 .name = "ecn",
6709dbbb 115 .family = AF_INET,
1d5cd909
PM
116 .match = match,
117 .matchsize = sizeof(struct ipt_ecn_info),
118 .checkentry = checkentry,
1da177e4
LT
119 .me = THIS_MODULE,
120};
121
65b4b4e8 122static int __init ipt_ecn_init(void)
1da177e4 123{
6709dbbb 124 return xt_register_match(&ecn_match);
1da177e4
LT
125}
126
65b4b4e8 127static void __exit ipt_ecn_fini(void)
1da177e4 128{
6709dbbb 129 xt_unregister_match(&ecn_match);
1da177e4
LT
130}
131
65b4b4e8
AM
132module_init(ipt_ecn_init);
133module_exit(ipt_ecn_fini);