| 1 | |
| 2 | |
| 3 | * xt_connmark - Netfilter module to operate on connection marks
|
| 4 | *
|
| 5 | * Copyright (C) 2002,2004 MARA Systems AB <https://www.marasystems.com>
|
| 6 | * by Henrik Nordstrom <[email protected]>
|
| 7 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008
|
| 8 | * Jan Engelhardt <[email protected]>
|
| 9 |
|
| 10 |
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/skbuff.h>
|
| 13 | #include <net/netfilter/nf_conntrack.h>
|
| 14 | #include <net/netfilter/nf_conntrack_ecache.h>
|
| 15 | #include <linux/netfilter/x_tables.h>
|
| 16 | #include <linux/netfilter/xt_connmark.h>
|
| 17 |
|
| 18 | MODULE_AUTHOR("Henrik Nordstrom <[email protected]>");
|
| 19 | MODULE_DESCRIPTION("Xtables: connection mark operations");
|
| 20 | MODULE_LICENSE("GPL");
|
| 21 | MODULE_ALIAS("ipt_CONNMARK");
|
| 22 | MODULE_ALIAS("ip6t_CONNMARK");
|
| 23 | MODULE_ALIAS("ipt_connmark");
|
| 24 | MODULE_ALIAS("ip6t_connmark");
|
| 25 |
|
| 26 | static unsigned int
|
| 27 | connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info)
|
| 28 | {
|
| 29 | enum ip_conntrack_info ctinfo;
|
| 30 | u_int32_t new_targetmark;
|
| 31 | struct nf_conn *ct;
|
| 32 | u_int32_t newmark;
|
| 33 | u_int32_t oldmark;
|
| 34 |
|
| 35 | ct = nf_ct_get(skb, &ctinfo);
|
| 36 | if (ct == NULL)
|
| 37 | return XT_CONTINUE;
|
| 38 |
|
| 39 | switch (info->mode) {
|
| 40 | case XT_CONNMARK_SET:
|
| 41 | oldmark = READ_ONCE(ct->mark);
|
| 42 | newmark = (oldmark & ~info->ctmask) ^ info->ctmark;
|
| 43 | if (info->shift_dir == D_SHIFT_RIGHT)
|
| 44 | newmark >>= info->shift_bits;
|
| 45 | else
|
| 46 | newmark <<= info->shift_bits;
|
| 47 |
|
| 48 | if (READ_ONCE(ct->mark) != newmark) {
|
| 49 | WRITE_ONCE(ct->mark, newmark);
|
| 50 | nf_conntrack_event_cache(IPCT_MARK, ct);
|
| 51 | }
|
| 52 | break;
|
| 53 | case XT_CONNMARK_SAVE:
|
| 54 | new_targetmark = (skb->mark & info->nfmask);
|
| 55 | if (info->shift_dir == D_SHIFT_RIGHT)
|
| 56 | new_targetmark >>= info->shift_bits;
|
| 57 | else
|
| 58 | new_targetmark <<= info->shift_bits;
|
| 59 |
|
| 60 | newmark = (READ_ONCE(ct->mark) & ~info->ctmask) ^
|
| 61 | new_targetmark;
|
| 62 | if (READ_ONCE(ct->mark) != newmark) {
|
| 63 | WRITE_ONCE(ct->mark, newmark);
|
| 64 | nf_conntrack_event_cache(IPCT_MARK, ct);
|
| 65 | }
|
| 66 | break;
|
| 67 | case XT_CONNMARK_RESTORE:
|
| 68 | new_targetmark = (READ_ONCE(ct->mark) & info->ctmask);
|
| 69 | if (info->shift_dir == D_SHIFT_RIGHT)
|
| 70 | new_targetmark >>= info->shift_bits;
|
| 71 | else
|
| 72 | new_targetmark <<= info->shift_bits;
|
| 73 |
|
| 74 | newmark = (skb->mark & ~info->nfmask) ^
|
| 75 | new_targetmark;
|
| 76 | skb->mark = newmark;
|
| 77 | break;
|
| 78 | }
|
| 79 | return XT_CONTINUE;
|
| 80 | }
|
| 81 |
|
| 82 | static unsigned int
|
| 83 | connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
| 84 | {
|
| 85 | const struct xt_connmark_tginfo1 *info = par->targinfo;
|
| 86 | const struct xt_connmark_tginfo2 info2 = {
|
| 87 | .ctmark = info->ctmark,
|
| 88 | .ctmask = info->ctmask,
|
| 89 | .nfmask = info->nfmask,
|
| 90 | .mode = info->mode,
|
| 91 | };
|
| 92 |
|
| 93 | return connmark_tg_shift(skb, &info2);
|
| 94 | }
|
| 95 |
|
| 96 | static unsigned int
|
| 97 | connmark_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
|
| 98 | {
|
| 99 | const struct xt_connmark_tginfo2 *info = par->targinfo;
|
| 100 |
|
| 101 | return connmark_tg_shift(skb, info);
|
| 102 | }
|
| 103 |
|
| 104 | static int connmark_tg_check(const struct xt_tgchk_param *par)
|
| 105 | {
|
| 106 | int ret;
|
| 107 |
|
| 108 | ret = nf_ct_netns_get(par->net, par->family);
|
| 109 | if (ret < 0)
|
| 110 | pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
|
| 111 | par->family);
|
| 112 | return ret;
|
| 113 | }
|
| 114 |
|
| 115 | static int connmark_tg_check_v2(const struct xt_tgchk_param *par)
|
| 116 | {
|
| 117 | const struct xt_connmark_tginfo2 *info = par->targinfo;
|
| 118 |
|
| 119 | if (info->shift_dir > D_SHIFT_RIGHT || info->shift_bits >= 32)
|
| 120 | return -EINVAL;
|
| 121 |
|
| 122 | return connmark_tg_check(par);
|
| 123 | }
|
| 124 |
|
| 125 | static void connmark_tg_destroy(const struct xt_tgdtor_param *par)
|
| 126 | {
|
| 127 | nf_ct_netns_put(par->net, par->family);
|
| 128 | }
|
| 129 |
|
| 130 | static bool
|
| 131 | connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
| 132 | {
|
| 133 | const struct xt_connmark_mtinfo1 *info = par->matchinfo;
|
| 134 | enum ip_conntrack_info ctinfo;
|
| 135 | const struct nf_conn *ct;
|
| 136 |
|
| 137 | ct = nf_ct_get(skb, &ctinfo);
|
| 138 | if (ct == NULL)
|
| 139 | return false;
|
| 140 |
|
| 141 | return ((READ_ONCE(ct->mark) & info->mask) == info->mark) ^ info->invert;
|
| 142 | }
|
| 143 |
|
| 144 | static int connmark_mt_check(const struct xt_mtchk_param *par)
|
| 145 | {
|
| 146 | int ret;
|
| 147 |
|
| 148 | ret = nf_ct_netns_get(par->net, par->family);
|
| 149 | if (ret < 0)
|
| 150 | pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
|
| 151 | par->family);
|
| 152 | return ret;
|
| 153 | }
|
| 154 |
|
| 155 | static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
|
| 156 | {
|
| 157 | nf_ct_netns_put(par->net, par->family);
|
| 158 | }
|
| 159 |
|
| 160 | static struct xt_target connmark_tg_reg[] __read_mostly = {
|
| 161 | {
|
| 162 | .name = "CONNMARK",
|
| 163 | .revision = 1,
|
| 164 | .family = NFPROTO_IPV4,
|
| 165 | .checkentry = connmark_tg_check,
|
| 166 | .target = connmark_tg,
|
| 167 | .targetsize = sizeof(struct xt_connmark_tginfo1),
|
| 168 | .destroy = connmark_tg_destroy,
|
| 169 | .me = THIS_MODULE,
|
| 170 | },
|
| 171 | {
|
| 172 | .name = "CONNMARK",
|
| 173 | .revision = 2,
|
| 174 | .family = NFPROTO_IPV4,
|
| 175 | .checkentry = connmark_tg_check_v2,
|
| 176 | .target = connmark_tg_v2,
|
| 177 | .targetsize = sizeof(struct xt_connmark_tginfo2),
|
| 178 | .destroy = connmark_tg_destroy,
|
| 179 | .me = THIS_MODULE,
|
| 180 | },
|
| 181 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
| 182 | {
|
| 183 | .name = "CONNMARK",
|
| 184 | .revision = 1,
|
| 185 | .family = NFPROTO_IPV6,
|
| 186 | .checkentry = connmark_tg_check,
|
| 187 | .target = connmark_tg,
|
| 188 | .targetsize = sizeof(struct xt_connmark_tginfo1),
|
| 189 | .destroy = connmark_tg_destroy,
|
| 190 | .me = THIS_MODULE,
|
| 191 | },
|
| 192 | {
|
| 193 | .name = "CONNMARK",
|
| 194 | .revision = 2,
|
| 195 | .family = NFPROTO_IPV6,
|
| 196 | .checkentry = connmark_tg_check_v2,
|
| 197 | .target = connmark_tg_v2,
|
| 198 | .targetsize = sizeof(struct xt_connmark_tginfo2),
|
| 199 | .destroy = connmark_tg_destroy,
|
| 200 | .me = THIS_MODULE,
|
| 201 | },
|
| 202 | #endif
|
| 203 | };
|
| 204 |
|
| 205 | static struct xt_match connmark_mt_reg __read_mostly = {
|
| 206 | .name = "connmark",
|
| 207 | .revision = 1,
|
| 208 | .family = NFPROTO_UNSPEC,
|
| 209 | .checkentry = connmark_mt_check,
|
| 210 | .match = connmark_mt,
|
| 211 | .matchsize = sizeof(struct xt_connmark_mtinfo1),
|
| 212 | .destroy = connmark_mt_destroy,
|
| 213 | .me = THIS_MODULE,
|
| 214 | };
|
| 215 |
|
| 216 | static int __init connmark_mt_init(void)
|
| 217 | {
|
| 218 | int ret;
|
| 219 |
|
| 220 | ret = xt_register_targets(connmark_tg_reg,
|
| 221 | ARRAY_SIZE(connmark_tg_reg));
|
| 222 | if (ret < 0)
|
| 223 | return ret;
|
| 224 | ret = xt_register_match(&connmark_mt_reg);
|
| 225 | if (ret < 0) {
|
| 226 | xt_unregister_targets(connmark_tg_reg,
|
| 227 | ARRAY_SIZE(connmark_tg_reg));
|
| 228 | return ret;
|
| 229 | }
|
| 230 | return 0;
|
| 231 | }
|
| 232 |
|
| 233 | static void __exit connmark_mt_exit(void)
|
| 234 | {
|
| 235 | xt_unregister_match(&connmark_mt_reg);
|
| 236 | xt_unregister_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg));
|
| 237 | }
|
| 238 |
|
| 239 | module_init(connmark_mt_init);
|
| 240 | module_exit(connmark_mt_exit);
|