1 /* 2 * xt_connmark - Netfilter module to operate on connection marks 3 * 4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com> 5 * by Henrik Nordstrom <hno@marasystems.com> 6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008 7 * Jan Engelhardt <jengelh@medozas.de> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/skbuff.h> 25 #include <net/netfilter/nf_conntrack.h> 26 #include <net/netfilter/nf_conntrack_ecache.h> 27 #include <linux/netfilter/x_tables.h> 28 #include <linux/netfilter/xt_connmark.h> 29 30 MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>"); 31 MODULE_DESCRIPTION("Xtables: connection mark operations"); 32 MODULE_LICENSE("GPL"); 33 MODULE_ALIAS("ipt_CONNMARK"); 34 MODULE_ALIAS("ip6t_CONNMARK"); 35 MODULE_ALIAS("ipt_connmark"); 36 MODULE_ALIAS("ip6t_connmark"); 37 38 static unsigned int 39 connmark_tg_shift(struct sk_buff *skb, 40 const struct xt_connmark_tginfo1 *info, 41 u8 shift_bits, u8 shift_dir) 42 { 43 enum ip_conntrack_info ctinfo; 44 struct nf_conn *ct; 45 u_int32_t newmark; 46 47 ct = nf_ct_get(skb, &ctinfo); 48 if (ct == NULL) 49 return XT_CONTINUE; 50 51 switch (info->mode) { 52 case XT_CONNMARK_SET: 53 newmark = (ct->mark & ~info->ctmask) ^ info->ctmark; 54 if (shift_dir == D_SHIFT_RIGHT) 55 newmark >>= shift_bits; 56 else 57 newmark <<= shift_bits; 58 if (ct->mark != newmark) { 59 ct->mark = newmark; 60 nf_conntrack_event_cache(IPCT_MARK, ct); 61 } 62 break; 63 case XT_CONNMARK_SAVE: 64 newmark = (ct->mark & ~info->ctmask) ^ 65 (skb->mark & info->nfmask); 66 if (shift_dir == D_SHIFT_RIGHT) 67 newmark >>= shift_bits; 68 else 69 newmark <<= shift_bits; 70 if (ct->mark != newmark) { 71 ct->mark = newmark; 72 nf_conntrack_event_cache(IPCT_MARK, ct); 73 } 74 break; 75 case XT_CONNMARK_RESTORE: 76 newmark = (skb->mark & ~info->nfmask) ^ 77 (ct->mark & info->ctmask); 78 if (shift_dir == D_SHIFT_RIGHT) 79 newmark >>= shift_bits; 80 else 81 newmark <<= shift_bits; 82 skb->mark = newmark; 83 break; 84 } 85 return XT_CONTINUE; 86 } 87 88 static unsigned int 89 connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) 90 { 91 const struct xt_connmark_tginfo1 *info = par->targinfo; 92 93 return connmark_tg_shift(skb, info, 0, 0); 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, (const struct xt_connmark_tginfo1 *)info, 102 info->shift_bits, info->shift_dir); 103 } 104 105 static int connmark_tg_check(const struct xt_tgchk_param *par) 106 { 107 int ret; 108 109 ret = nf_ct_netns_get(par->net, par->family); 110 if (ret < 0) 111 pr_info_ratelimited("cannot load conntrack support for proto=%u\n", 112 par->family); 113 return ret; 114 } 115 116 static void connmark_tg_destroy(const struct xt_tgdtor_param *par) 117 { 118 nf_ct_netns_put(par->net, par->family); 119 } 120 121 static bool 122 connmark_mt(const struct sk_buff *skb, struct xt_action_param *par) 123 { 124 const struct xt_connmark_mtinfo1 *info = par->matchinfo; 125 enum ip_conntrack_info ctinfo; 126 const struct nf_conn *ct; 127 128 ct = nf_ct_get(skb, &ctinfo); 129 if (ct == NULL) 130 return false; 131 132 return ((ct->mark & info->mask) == info->mark) ^ info->invert; 133 } 134 135 static int connmark_mt_check(const struct xt_mtchk_param *par) 136 { 137 int ret; 138 139 ret = nf_ct_netns_get(par->net, par->family); 140 if (ret < 0) 141 pr_info_ratelimited("cannot load conntrack support for proto=%u\n", 142 par->family); 143 return ret; 144 } 145 146 static void connmark_mt_destroy(const struct xt_mtdtor_param *par) 147 { 148 nf_ct_netns_put(par->net, par->family); 149 } 150 151 static struct xt_target connmark_tg_reg[] __read_mostly = { 152 { 153 .name = "CONNMARK", 154 .revision = 1, 155 .family = NFPROTO_UNSPEC, 156 .checkentry = connmark_tg_check, 157 .target = connmark_tg, 158 .targetsize = sizeof(struct xt_connmark_tginfo1), 159 .destroy = connmark_tg_destroy, 160 .me = THIS_MODULE, 161 }, 162 { 163 .name = "CONNMARK", 164 .revision = 2, 165 .family = NFPROTO_UNSPEC, 166 .checkentry = connmark_tg_check, 167 .target = connmark_tg_v2, 168 .targetsize = sizeof(struct xt_connmark_tginfo2), 169 .destroy = connmark_tg_destroy, 170 .me = THIS_MODULE, 171 } 172 }; 173 174 static struct xt_match connmark_mt_reg __read_mostly = { 175 .name = "connmark", 176 .revision = 1, 177 .family = NFPROTO_UNSPEC, 178 .checkentry = connmark_mt_check, 179 .match = connmark_mt, 180 .matchsize = sizeof(struct xt_connmark_mtinfo1), 181 .destroy = connmark_mt_destroy, 182 .me = THIS_MODULE, 183 }; 184 185 static int __init connmark_mt_init(void) 186 { 187 int ret; 188 189 ret = xt_register_targets(connmark_tg_reg, 190 ARRAY_SIZE(connmark_tg_reg)); 191 if (ret < 0) 192 return ret; 193 ret = xt_register_match(&connmark_mt_reg); 194 if (ret < 0) { 195 xt_unregister_targets(connmark_tg_reg, 196 ARRAY_SIZE(connmark_tg_reg)); 197 return ret; 198 } 199 return 0; 200 } 201 202 static void __exit connmark_mt_exit(void) 203 { 204 xt_unregister_match(&connmark_mt_reg); 205 xt_unregister_target(connmark_tg_reg); 206 } 207 208 module_init(connmark_mt_init); 209 module_exit(connmark_mt_exit); 210