1 /* 2 * Copyright (c) 2008, Intel Corporation. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program; if not, see <http://www.gnu.org/licenses/>. 15 * 16 * Author: Alexander Duyck <alexander.h.duyck@intel.com> 17 */ 18 19 #include <linux/module.h> 20 #include <linux/init.h> 21 #include <linux/kernel.h> 22 #include <linux/skbuff.h> 23 #include <linux/rtnetlink.h> 24 #include <net/netlink.h> 25 #include <net/pkt_sched.h> 26 #include <net/ip.h> 27 #include <net/ipv6.h> 28 #include <net/dsfield.h> 29 30 #include <linux/tc_act/tc_skbedit.h> 31 #include <net/tc_act/tc_skbedit.h> 32 33 static unsigned int skbedit_net_id; 34 static struct tc_action_ops act_skbedit_ops; 35 36 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, 37 struct tcf_result *res) 38 { 39 struct tcf_skbedit *d = to_skbedit(a); 40 struct tcf_skbedit_params *params; 41 int action; 42 43 tcf_lastuse_update(&d->tcf_tm); 44 bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); 45 46 rcu_read_lock(); 47 params = rcu_dereference(d->params); 48 action = READ_ONCE(d->tcf_action); 49 50 if (params->flags & SKBEDIT_F_PRIORITY) 51 skb->priority = params->priority; 52 if (params->flags & SKBEDIT_F_INHERITDSFIELD) { 53 int wlen = skb_network_offset(skb); 54 55 switch (tc_skb_protocol(skb)) { 56 case htons(ETH_P_IP): 57 wlen += sizeof(struct iphdr); 58 if (!pskb_may_pull(skb, wlen)) 59 goto err; 60 skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; 61 break; 62 63 case htons(ETH_P_IPV6): 64 wlen += sizeof(struct ipv6hdr); 65 if (!pskb_may_pull(skb, wlen)) 66 goto err; 67 skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; 68 break; 69 } 70 } 71 if (params->flags & SKBEDIT_F_QUEUE_MAPPING && 72 skb->dev->real_num_tx_queues > params->queue_mapping) 73 skb_set_queue_mapping(skb, params->queue_mapping); 74 if (params->flags & SKBEDIT_F_MARK) { 75 skb->mark &= ~params->mask; 76 skb->mark |= params->mark & params->mask; 77 } 78 if (params->flags & SKBEDIT_F_PTYPE) 79 skb->pkt_type = params->ptype; 80 81 unlock: 82 rcu_read_unlock(); 83 return action; 84 err: 85 qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); 86 action = TC_ACT_SHOT; 87 goto unlock; 88 } 89 90 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { 91 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, 92 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, 93 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, 94 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, 95 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, 96 [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, 97 [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, 98 }; 99 100 static int tcf_skbedit_init(struct net *net, struct nlattr *nla, 101 struct nlattr *est, struct tc_action **a, 102 int ovr, int bind, bool rtnl_held, 103 struct netlink_ext_ack *extack) 104 { 105 struct tc_action_net *tn = net_generic(net, skbedit_net_id); 106 struct tcf_skbedit_params *params_old, *params_new; 107 struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; 108 struct tc_skbedit *parm; 109 struct tcf_skbedit *d; 110 u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; 111 u16 *queue_mapping = NULL, *ptype = NULL; 112 bool exists = false; 113 int ret = 0, err; 114 115 if (nla == NULL) 116 return -EINVAL; 117 118 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); 119 if (err < 0) 120 return err; 121 122 if (tb[TCA_SKBEDIT_PARMS] == NULL) 123 return -EINVAL; 124 125 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { 126 flags |= SKBEDIT_F_PRIORITY; 127 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); 128 } 129 130 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { 131 flags |= SKBEDIT_F_QUEUE_MAPPING; 132 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); 133 } 134 135 if (tb[TCA_SKBEDIT_PTYPE] != NULL) { 136 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); 137 if (!skb_pkt_type_ok(*ptype)) 138 return -EINVAL; 139 flags |= SKBEDIT_F_PTYPE; 140 } 141 142 if (tb[TCA_SKBEDIT_MARK] != NULL) { 143 flags |= SKBEDIT_F_MARK; 144 mark = nla_data(tb[TCA_SKBEDIT_MARK]); 145 } 146 147 if (tb[TCA_SKBEDIT_MASK] != NULL) { 148 flags |= SKBEDIT_F_MASK; 149 mask = nla_data(tb[TCA_SKBEDIT_MASK]); 150 } 151 152 if (tb[TCA_SKBEDIT_FLAGS] != NULL) { 153 u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); 154 155 if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) 156 flags |= SKBEDIT_F_INHERITDSFIELD; 157 } 158 159 parm = nla_data(tb[TCA_SKBEDIT_PARMS]); 160 161 err = tcf_idr_check_alloc(tn, &parm->index, a, bind); 162 if (err < 0) 163 return err; 164 exists = err; 165 if (exists && bind) 166 return 0; 167 168 if (!flags) { 169 if (exists) 170 tcf_idr_release(*a, bind); 171 else 172 tcf_idr_cleanup(tn, parm->index); 173 return -EINVAL; 174 } 175 176 if (!exists) { 177 ret = tcf_idr_create(tn, parm->index, est, a, 178 &act_skbedit_ops, bind, true); 179 if (ret) { 180 tcf_idr_cleanup(tn, parm->index); 181 return ret; 182 } 183 184 d = to_skbedit(*a); 185 ret = ACT_P_CREATED; 186 } else { 187 d = to_skbedit(*a); 188 if (!ovr) { 189 tcf_idr_release(*a, bind); 190 return -EEXIST; 191 } 192 } 193 194 ASSERT_RTNL(); 195 196 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); 197 if (unlikely(!params_new)) { 198 if (ret == ACT_P_CREATED) 199 tcf_idr_release(*a, bind); 200 return -ENOMEM; 201 } 202 203 params_new->flags = flags; 204 if (flags & SKBEDIT_F_PRIORITY) 205 params_new->priority = *priority; 206 if (flags & SKBEDIT_F_QUEUE_MAPPING) 207 params_new->queue_mapping = *queue_mapping; 208 if (flags & SKBEDIT_F_MARK) 209 params_new->mark = *mark; 210 if (flags & SKBEDIT_F_PTYPE) 211 params_new->ptype = *ptype; 212 /* default behaviour is to use all the bits */ 213 params_new->mask = 0xffffffff; 214 if (flags & SKBEDIT_F_MASK) 215 params_new->mask = *mask; 216 217 d->tcf_action = parm->action; 218 params_old = rtnl_dereference(d->params); 219 rcu_assign_pointer(d->params, params_new); 220 if (params_old) 221 kfree_rcu(params_old, rcu); 222 223 if (ret == ACT_P_CREATED) 224 tcf_idr_insert(tn, *a); 225 return ret; 226 } 227 228 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, 229 int bind, int ref) 230 { 231 unsigned char *b = skb_tail_pointer(skb); 232 struct tcf_skbedit *d = to_skbedit(a); 233 struct tcf_skbedit_params *params; 234 struct tc_skbedit opt = { 235 .index = d->tcf_index, 236 .refcnt = refcount_read(&d->tcf_refcnt) - ref, 237 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, 238 .action = d->tcf_action, 239 }; 240 u64 pure_flags = 0; 241 struct tcf_t t; 242 243 params = rtnl_dereference(d->params); 244 245 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) 246 goto nla_put_failure; 247 if ((params->flags & SKBEDIT_F_PRIORITY) && 248 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) 249 goto nla_put_failure; 250 if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && 251 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) 252 goto nla_put_failure; 253 if ((params->flags & SKBEDIT_F_MARK) && 254 nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) 255 goto nla_put_failure; 256 if ((params->flags & SKBEDIT_F_PTYPE) && 257 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) 258 goto nla_put_failure; 259 if ((params->flags & SKBEDIT_F_MASK) && 260 nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) 261 goto nla_put_failure; 262 if (params->flags & SKBEDIT_F_INHERITDSFIELD) 263 pure_flags |= SKBEDIT_F_INHERITDSFIELD; 264 if (pure_flags != 0 && 265 nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) 266 goto nla_put_failure; 267 268 tcf_tm_dump(&t, &d->tcf_tm); 269 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) 270 goto nla_put_failure; 271 return skb->len; 272 273 nla_put_failure: 274 nlmsg_trim(skb, b); 275 return -1; 276 } 277 278 static void tcf_skbedit_cleanup(struct tc_action *a) 279 { 280 struct tcf_skbedit *d = to_skbedit(a); 281 struct tcf_skbedit_params *params; 282 283 params = rcu_dereference_protected(d->params, 1); 284 if (params) 285 kfree_rcu(params, rcu); 286 } 287 288 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, 289 struct netlink_callback *cb, int type, 290 const struct tc_action_ops *ops, 291 struct netlink_ext_ack *extack) 292 { 293 struct tc_action_net *tn = net_generic(net, skbedit_net_id); 294 295 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 296 } 297 298 static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index, 299 struct netlink_ext_ack *extack) 300 { 301 struct tc_action_net *tn = net_generic(net, skbedit_net_id); 302 303 return tcf_idr_search(tn, a, index); 304 } 305 306 static int tcf_skbedit_delete(struct net *net, u32 index) 307 { 308 struct tc_action_net *tn = net_generic(net, skbedit_net_id); 309 310 return tcf_idr_delete_index(tn, index); 311 } 312 313 static struct tc_action_ops act_skbedit_ops = { 314 .kind = "skbedit", 315 .type = TCA_ACT_SKBEDIT, 316 .owner = THIS_MODULE, 317 .act = tcf_skbedit, 318 .dump = tcf_skbedit_dump, 319 .init = tcf_skbedit_init, 320 .cleanup = tcf_skbedit_cleanup, 321 .walk = tcf_skbedit_walker, 322 .lookup = tcf_skbedit_search, 323 .delete = tcf_skbedit_delete, 324 .size = sizeof(struct tcf_skbedit), 325 }; 326 327 static __net_init int skbedit_init_net(struct net *net) 328 { 329 struct tc_action_net *tn = net_generic(net, skbedit_net_id); 330 331 return tc_action_net_init(tn, &act_skbedit_ops); 332 } 333 334 static void __net_exit skbedit_exit_net(struct list_head *net_list) 335 { 336 tc_action_net_exit(net_list, skbedit_net_id); 337 } 338 339 static struct pernet_operations skbedit_net_ops = { 340 .init = skbedit_init_net, 341 .exit_batch = skbedit_exit_net, 342 .id = &skbedit_net_id, 343 .size = sizeof(struct tc_action_net), 344 }; 345 346 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); 347 MODULE_DESCRIPTION("SKB Editing"); 348 MODULE_LICENSE("GPL"); 349 350 static int __init skbedit_init_module(void) 351 { 352 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); 353 } 354 355 static void __exit skbedit_cleanup_module(void) 356 { 357 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); 358 } 359 360 module_init(skbedit_init_module); 361 module_exit(skbedit_cleanup_module); 362