Commit | Line | Data |
---|---|---|
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 | */ | |
ff67e4e4 | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
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 | ||
21 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
2ae15b64 | 22 | MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match for IPv4"); |
1da177e4 LT |
23 | MODULE_LICENSE("GPL"); |
24 | ||
1d93a9cb JE |
25 | static inline bool match_ip(const struct sk_buff *skb, |
26 | const struct ipt_ecn_info *einfo) | |
1da177e4 | 27 | { |
db898aa2 PM |
28 | return ((ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect) ^ |
29 | !!(einfo->invert & IPT_ECN_OP_MATCH_IP); | |
1da177e4 LT |
30 | } |
31 | ||
1d93a9cb JE |
32 | static inline bool match_tcp(const struct sk_buff *skb, |
33 | const struct ipt_ecn_info *einfo, | |
34 | bool *hotdrop) | |
1da177e4 | 35 | { |
a47362a2 JE |
36 | struct tcphdr _tcph; |
37 | const struct tcphdr *th; | |
1da177e4 LT |
38 | |
39 | /* In practice, TCP match does this, so can't fail. But let's | |
40 | * be good citizens. | |
41 | */ | |
c9bdd4b5 | 42 | th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph); |
1da177e4 | 43 | if (th == NULL) { |
cff533ac | 44 | *hotdrop = false; |
1d93a9cb | 45 | return false; |
1da177e4 LT |
46 | } |
47 | ||
48 | if (einfo->operation & IPT_ECN_OP_MATCH_ECE) { | |
49 | if (einfo->invert & IPT_ECN_OP_MATCH_ECE) { | |
50 | if (th->ece == 1) | |
1d93a9cb | 51 | return false; |
1da177e4 LT |
52 | } else { |
53 | if (th->ece == 0) | |
1d93a9cb | 54 | return false; |
1da177e4 LT |
55 | } |
56 | } | |
57 | ||
58 | if (einfo->operation & IPT_ECN_OP_MATCH_CWR) { | |
59 | if (einfo->invert & IPT_ECN_OP_MATCH_CWR) { | |
60 | if (th->cwr == 1) | |
1d93a9cb | 61 | return false; |
1da177e4 LT |
62 | } else { |
63 | if (th->cwr == 0) | |
1d93a9cb | 64 | return false; |
1da177e4 LT |
65 | } |
66 | } | |
67 | ||
1d93a9cb | 68 | return true; |
1da177e4 LT |
69 | } |
70 | ||
62fc8051 | 71 | static bool ecn_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 72 | { |
f7108a20 | 73 | const struct ipt_ecn_info *info = par->matchinfo; |
1da177e4 LT |
74 | |
75 | if (info->operation & IPT_ECN_OP_MATCH_IP) | |
76 | if (!match_ip(skb, info)) | |
1d93a9cb | 77 | return false; |
1da177e4 LT |
78 | |
79 | if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) { | |
b4ba2611 | 80 | if (!match_tcp(skb, info, &par->hotdrop)) |
1d93a9cb | 81 | return false; |
1da177e4 LT |
82 | } |
83 | ||
1d93a9cb | 84 | return true; |
1da177e4 LT |
85 | } |
86 | ||
b0f38452 | 87 | static int ecn_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 88 | { |
9b4fce7a JE |
89 | const struct ipt_ecn_info *info = par->matchinfo; |
90 | const struct ipt_ip *ip = par->entryinfo; | |
1da177e4 | 91 | |
1da177e4 | 92 | if (info->operation & IPT_ECN_OP_MATCH_MASK) |
bd414ee6 | 93 | return -EINVAL; |
1da177e4 LT |
94 | |
95 | if (info->invert & IPT_ECN_OP_MATCH_MASK) | |
bd414ee6 | 96 | return -EINVAL; |
1da177e4 | 97 | |
3666ed1c | 98 | if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR) && |
58d5a025 | 99 | (ip->proto != IPPROTO_TCP || ip->invflags & IPT_INV_PROTO)) { |
ff67e4e4 | 100 | pr_info("cannot match TCP bits in rule for non-tcp packets\n"); |
bd414ee6 | 101 | return -EINVAL; |
1da177e4 LT |
102 | } |
103 | ||
bd414ee6 | 104 | return 0; |
1da177e4 LT |
105 | } |
106 | ||
d3c5ee6d | 107 | static struct xt_match ecn_mt_reg __read_mostly = { |
1da177e4 | 108 | .name = "ecn", |
ee999d8b | 109 | .family = NFPROTO_IPV4, |
d3c5ee6d | 110 | .match = ecn_mt, |
1d5cd909 | 111 | .matchsize = sizeof(struct ipt_ecn_info), |
d3c5ee6d | 112 | .checkentry = ecn_mt_check, |
1da177e4 LT |
113 | .me = THIS_MODULE, |
114 | }; | |
115 | ||
d3c5ee6d | 116 | static int __init ecn_mt_init(void) |
1da177e4 | 117 | { |
d3c5ee6d | 118 | return xt_register_match(&ecn_mt_reg); |
1da177e4 LT |
119 | } |
120 | ||
d3c5ee6d | 121 | static void __exit ecn_mt_exit(void) |
1da177e4 | 122 | { |
d3c5ee6d | 123 | xt_unregister_match(&ecn_mt_reg); |
1da177e4 LT |
124 | } |
125 | ||
d3c5ee6d JE |
126 | module_init(ecn_mt_init); |
127 | module_exit(ecn_mt_exit); |