| 1 | |
| 2 | |
| 3 | * net/sched/cls_api.c Packet classifier API.
|
| 4 | *
|
| 5 | * Authors: Alexey Kuznetsov, <[email protected]>
|
| 6 | *
|
| 7 | * Changes:
|
| 8 | *
|
| 9 | * Eduardo J. Blanco <[email protected]> :990222: kmod support
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/types.h>
|
| 14 | #include <linux/kernel.h>
|
| 15 | #include <linux/string.h>
|
| 16 | #include <linux/errno.h>
|
| 17 | #include <linux/err.h>
|
| 18 | #include <linux/skbuff.h>
|
| 19 | #include <linux/init.h>
|
| 20 | #include <linux/kmod.h>
|
| 21 | #include <linux/slab.h>
|
| 22 | #include <linux/idr.h>
|
| 23 | #include <linux/jhash.h>
|
| 24 | #include <linux/rculist.h>
|
| 25 | #include <linux/rhashtable.h>
|
| 26 | #include <net/net_namespace.h>
|
| 27 | #include <net/sock.h>
|
| 28 | #include <net/netlink.h>
|
| 29 | #include <net/pkt_sched.h>
|
| 30 | #include <net/pkt_cls.h>
|
| 31 | #include <net/tc_act/tc_pedit.h>
|
| 32 | #include <net/tc_act/tc_mirred.h>
|
| 33 | #include <net/tc_act/tc_vlan.h>
|
| 34 | #include <net/tc_act/tc_tunnel_key.h>
|
| 35 | #include <net/tc_act/tc_csum.h>
|
| 36 | #include <net/tc_act/tc_gact.h>
|
| 37 | #include <net/tc_act/tc_police.h>
|
| 38 | #include <net/tc_act/tc_sample.h>
|
| 39 | #include <net/tc_act/tc_skbedit.h>
|
| 40 | #include <net/tc_act/tc_ct.h>
|
| 41 | #include <net/tc_act/tc_mpls.h>
|
| 42 | #include <net/tc_act/tc_gate.h>
|
| 43 | #include <net/flow_offload.h>
|
| 44 | #include <net/tc_wrapper.h>
|
| 45 |
|
| 46 |
|
| 47 | static LIST_HEAD(tcf_proto_base);
|
| 48 |
|
| 49 |
|
| 50 | static DEFINE_RWLOCK(cls_mod_lock);
|
| 51 |
|
| 52 | static struct xarray tcf_exts_miss_cookies_xa;
|
| 53 | struct tcf_exts_miss_cookie_node {
|
| 54 | const struct tcf_chain *chain;
|
| 55 | const struct tcf_proto *tp;
|
| 56 | const struct tcf_exts *exts;
|
| 57 | u32 chain_index;
|
| 58 | u32 tp_prio;
|
| 59 | u32 handle;
|
| 60 | u32 miss_cookie_base;
|
| 61 | struct rcu_head rcu;
|
| 62 | };
|
| 63 |
|
| 64 | |
| 65 | * action index in the exts tc actions array.
|
| 66 |
|
| 67 | union tcf_exts_miss_cookie {
|
| 68 | struct {
|
| 69 | u32 miss_cookie_base;
|
| 70 | u32 act_index;
|
| 71 | };
|
| 72 | u64 miss_cookie;
|
| 73 | };
|
| 74 |
|
| 75 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
|
| 76 | static int
|
| 77 | tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
|
| 78 | u32 handle)
|
| 79 | {
|
| 80 | struct tcf_exts_miss_cookie_node *n;
|
| 81 | static u32 next;
|
| 82 | int err;
|
| 83 |
|
| 84 | if (WARN_ON(!handle || !tp->ops->get_exts))
|
| 85 | return -EINVAL;
|
| 86 |
|
| 87 | n = kzalloc_obj(*n);
|
| 88 | if (!n)
|
| 89 | return -ENOMEM;
|
| 90 |
|
| 91 | n->chain_index = tp->chain->index;
|
| 92 | n->chain = tp->chain;
|
| 93 | n->tp_prio = tp->prio;
|
| 94 | n->tp = tp;
|
| 95 | n->exts = exts;
|
| 96 | n->handle = handle;
|
| 97 |
|
| 98 | err = xa_alloc_cyclic(&tcf_exts_miss_cookies_xa, &n->miss_cookie_base,
|
| 99 | n, xa_limit_32b, &next, GFP_KERNEL);
|
| 100 | if (err < 0)
|
| 101 | goto err_xa_alloc;
|
| 102 |
|
| 103 | exts->miss_cookie_node = n;
|
| 104 | return 0;
|
| 105 |
|
| 106 | err_xa_alloc:
|
| 107 | kfree(n);
|
| 108 | return err;
|
| 109 | }
|
| 110 |
|
| 111 | static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
|
| 112 | {
|
| 113 | struct tcf_exts_miss_cookie_node *n;
|
| 114 |
|
| 115 | if (!exts->miss_cookie_node)
|
| 116 | return;
|
| 117 |
|
| 118 | n = exts->miss_cookie_node;
|
| 119 | xa_erase(&tcf_exts_miss_cookies_xa, n->miss_cookie_base);
|
| 120 | kfree_rcu(n, rcu);
|
| 121 | }
|
| 122 |
|
| 123 | static struct tcf_exts_miss_cookie_node *
|
| 124 | tcf_exts_miss_cookie_lookup(u64 miss_cookie, int *act_index)
|
| 125 | {
|
| 126 | union tcf_exts_miss_cookie mc = { .miss_cookie = miss_cookie, };
|
| 127 |
|
| 128 | *act_index = mc.act_index;
|
| 129 | return xa_load(&tcf_exts_miss_cookies_xa, mc.miss_cookie_base);
|
| 130 | }
|
| 131 | #else
|
| 132 | static int
|
| 133 | tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
|
| 134 | u32 handle)
|
| 135 | {
|
| 136 | return 0;
|
| 137 | }
|
| 138 |
|
| 139 | static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
|
| 140 | {
|
| 141 | }
|
| 142 | #endif
|
| 143 |
|
| 144 | static u64 tcf_exts_miss_cookie_get(u32 miss_cookie_base, int act_index)
|
| 145 | {
|
| 146 | union tcf_exts_miss_cookie mc = { .act_index = act_index, };
|
| 147 |
|
| 148 | if (!miss_cookie_base)
|
| 149 | return 0;
|
| 150 |
|
| 151 | mc.miss_cookie_base = miss_cookie_base;
|
| 152 | return mc.miss_cookie;
|
| 153 | }
|
| 154 |
|
| 155 | #ifdef CONFIG_NET_CLS_ACT
|
| 156 | DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
|
| 157 | EXPORT_SYMBOL(tc_skb_ext_tc);
|
| 158 |
|
| 159 | void tc_skb_ext_tc_enable(void)
|
| 160 | {
|
| 161 | static_branch_inc(&tc_skb_ext_tc);
|
| 162 | }
|
| 163 | EXPORT_SYMBOL(tc_skb_ext_tc_enable);
|
| 164 |
|
| 165 | void tc_skb_ext_tc_disable(void)
|
| 166 | {
|
| 167 | static_branch_dec(&tc_skb_ext_tc);
|
| 168 | }
|
| 169 | EXPORT_SYMBOL(tc_skb_ext_tc_disable);
|
| 170 | #endif
|
| 171 |
|
| 172 | static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
|
| 173 | {
|
| 174 | return jhash_3words(tp->chain->index, tp->prio,
|
| 175 | (__force __u32)tp->protocol, 0);
|
| 176 | }
|
| 177 |
|
| 178 | static void tcf_proto_signal_destroying(struct tcf_chain *chain,
|
| 179 | struct tcf_proto *tp)
|
| 180 | {
|
| 181 | struct tcf_block *block = chain->block;
|
| 182 |
|
| 183 | mutex_lock(&block->proto_destroy_lock);
|
| 184 | hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
|
| 185 | destroy_obj_hashfn(tp));
|
| 186 | mutex_unlock(&block->proto_destroy_lock);
|
| 187 | }
|
| 188 |
|
| 189 | static bool tcf_proto_cmp(const struct tcf_proto *tp1,
|
| 190 | const struct tcf_proto *tp2)
|
| 191 | {
|
| 192 | return tp1->chain->index == tp2->chain->index &&
|
| 193 | tp1->prio == tp2->prio &&
|
| 194 | tp1->protocol == tp2->protocol;
|
| 195 | }
|
| 196 |
|
| 197 | static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
|
| 198 | struct tcf_proto *tp)
|
| 199 | {
|
| 200 | u32 hash = destroy_obj_hashfn(tp);
|
| 201 | struct tcf_proto *iter;
|
| 202 | bool found = false;
|
| 203 |
|
| 204 | rcu_read_lock();
|
| 205 | hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
|
| 206 | destroy_ht_node, hash) {
|
| 207 | if (tcf_proto_cmp(tp, iter)) {
|
| 208 | found = true;
|
| 209 | break;
|
| 210 | }
|
| 211 | }
|
| 212 | rcu_read_unlock();
|
| 213 |
|
| 214 | return found;
|
| 215 | }
|
| 216 |
|
| 217 | static void
|
| 218 | tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
|
| 219 | {
|
| 220 | struct tcf_block *block = chain->block;
|
| 221 |
|
| 222 | mutex_lock(&block->proto_destroy_lock);
|
| 223 | if (hash_hashed(&tp->destroy_ht_node))
|
| 224 | hash_del_rcu(&tp->destroy_ht_node);
|
| 225 | mutex_unlock(&block->proto_destroy_lock);
|
| 226 | }
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 | static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
|
| 231 | {
|
| 232 | const struct tcf_proto_ops *t, *res = NULL;
|
| 233 |
|
| 234 | if (kind) {
|
| 235 | read_lock(&cls_mod_lock);
|
| 236 | list_for_each_entry(t, &tcf_proto_base, head) {
|
| 237 | if (strcmp(kind, t->kind) == 0) {
|
| 238 | if (try_module_get(t->owner))
|
| 239 | res = t;
|
| 240 | break;
|
| 241 | }
|
| 242 | }
|
| 243 | read_unlock(&cls_mod_lock);
|
| 244 | }
|
| 245 | return res;
|
| 246 | }
|
| 247 |
|
| 248 | static const struct tcf_proto_ops *
|
| 249 | tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
|
| 250 | struct netlink_ext_ack *extack)
|
| 251 | {
|
| 252 | const struct tcf_proto_ops *ops;
|
| 253 |
|
| 254 | ops = __tcf_proto_lookup_ops(kind);
|
| 255 | if (ops)
|
| 256 | return ops;
|
| 257 | #ifdef CONFIG_MODULES
|
| 258 | if (rtnl_held)
|
| 259 | rtnl_unlock();
|
| 260 | request_module(NET_CLS_ALIAS_PREFIX "%s", kind);
|
| 261 | if (rtnl_held)
|
| 262 | rtnl_lock();
|
| 263 | ops = __tcf_proto_lookup_ops(kind);
|
| 264 | |
| 265 | * the module load. So, even if we succeeded in loading
|
| 266 | * the module we have to replay the request. We indicate
|
| 267 | * this using -EAGAIN.
|
| 268 |
|
| 269 | if (ops) {
|
| 270 | module_put(ops->owner);
|
| 271 | return ERR_PTR(-EAGAIN);
|
| 272 | }
|
| 273 | #endif
|
| 274 | NL_SET_ERR_MSG(extack, "TC classifier not found");
|
| 275 | return ERR_PTR(-ENOENT);
|
| 276 | }
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 | int register_tcf_proto_ops(struct tcf_proto_ops *ops)
|
| 281 | {
|
| 282 | struct tcf_proto_ops *t;
|
| 283 | int rc = -EEXIST;
|
| 284 |
|
| 285 | write_lock(&cls_mod_lock);
|
| 286 | list_for_each_entry(t, &tcf_proto_base, head)
|
| 287 | if (!strcmp(ops->kind, t->kind))
|
| 288 | goto out;
|
| 289 |
|
| 290 | list_add_tail(&ops->head, &tcf_proto_base);
|
| 291 | rc = 0;
|
| 292 | out:
|
| 293 | write_unlock(&cls_mod_lock);
|
| 294 | return rc;
|
| 295 | }
|
| 296 | EXPORT_SYMBOL(register_tcf_proto_ops);
|
| 297 |
|
| 298 | static struct workqueue_struct *tc_filter_wq;
|
| 299 |
|
| 300 | void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
|
| 301 | {
|
| 302 | struct tcf_proto_ops *t;
|
| 303 | int rc = -ENOENT;
|
| 304 |
|
| 305 | |
| 306 | * tcf_proto_ops's destroy() handler.
|
| 307 |
|
| 308 | rcu_barrier();
|
| 309 | flush_workqueue(tc_filter_wq);
|
| 310 |
|
| 311 | write_lock(&cls_mod_lock);
|
| 312 | list_for_each_entry(t, &tcf_proto_base, head) {
|
| 313 | if (t == ops) {
|
| 314 | list_del(&t->head);
|
| 315 | rc = 0;
|
| 316 | break;
|
| 317 | }
|
| 318 | }
|
| 319 | write_unlock(&cls_mod_lock);
|
| 320 |
|
| 321 | WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
|
| 322 | }
|
| 323 | EXPORT_SYMBOL(unregister_tcf_proto_ops);
|
| 324 |
|
| 325 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
|
| 326 | {
|
| 327 | INIT_RCU_WORK(rwork, func);
|
| 328 | return queue_rcu_work(tc_filter_wq, rwork);
|
| 329 | }
|
| 330 | EXPORT_SYMBOL(tcf_queue_work);
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 | static inline u32 tcf_auto_prio(struct tcf_proto *tp)
|
| 335 | {
|
| 336 | u32 first = TC_H_MAKE(0xC0000000U, 0U);
|
| 337 |
|
| 338 | if (tp)
|
| 339 | first = tp->prio - 1;
|
| 340 |
|
| 341 | return TC_H_MAJ(first);
|
| 342 | }
|
| 343 |
|
| 344 | static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
|
| 345 | {
|
| 346 | if (kind)
|
| 347 | return nla_strscpy(name, kind, IFNAMSIZ) < 0;
|
| 348 | memset(name, 0, IFNAMSIZ);
|
| 349 | return false;
|
| 350 | }
|
| 351 |
|
| 352 | static bool tcf_proto_is_unlocked(const char *kind)
|
| 353 | {
|
| 354 | const struct tcf_proto_ops *ops;
|
| 355 | bool ret;
|
| 356 |
|
| 357 | if (strlen(kind) == 0)
|
| 358 | return false;
|
| 359 |
|
| 360 | ops = tcf_proto_lookup_ops(kind, false, NULL);
|
| 361 | |
| 362 | * functions will perform lookup again and properly handle errors.
|
| 363 |
|
| 364 | if (IS_ERR(ops))
|
| 365 | return false;
|
| 366 |
|
| 367 | ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
|
| 368 | module_put(ops->owner);
|
| 369 | return ret;
|
| 370 | }
|
| 371 |
|
| 372 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
|
| 373 | u32 prio, struct tcf_chain *chain,
|
| 374 | bool rtnl_held,
|
| 375 | struct netlink_ext_ack *extack)
|
| 376 | {
|
| 377 | struct tcf_proto *tp;
|
| 378 | int err;
|
| 379 |
|
| 380 | tp = kzalloc_obj(*tp);
|
| 381 | if (!tp)
|
| 382 | return ERR_PTR(-ENOBUFS);
|
| 383 |
|
| 384 | tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
|
| 385 | if (IS_ERR(tp->ops)) {
|
| 386 | err = PTR_ERR(tp->ops);
|
| 387 | goto errout;
|
| 388 | }
|
| 389 | tp->classify = tp->ops->classify;
|
| 390 | tp->protocol = protocol;
|
| 391 | tp->prio = prio;
|
| 392 | tp->chain = chain;
|
| 393 | tp->usesw = !tp->ops->reoffload;
|
| 394 | spin_lock_init(&tp->lock);
|
| 395 | refcount_set(&tp->refcnt, 1);
|
| 396 |
|
| 397 | err = tp->ops->init(tp);
|
| 398 | if (err) {
|
| 399 | module_put(tp->ops->owner);
|
| 400 | goto errout;
|
| 401 | }
|
| 402 | return tp;
|
| 403 |
|
| 404 | errout:
|
| 405 | kfree(tp);
|
| 406 | return ERR_PTR(err);
|
| 407 | }
|
| 408 |
|
| 409 | static void tcf_proto_get(struct tcf_proto *tp)
|
| 410 | {
|
| 411 | refcount_inc(&tp->refcnt);
|
| 412 | }
|
| 413 |
|
| 414 | static void tcf_proto_count_usesw(struct tcf_proto *tp, bool add)
|
| 415 | {
|
| 416 | #ifdef CONFIG_NET_CLS_ACT
|
| 417 | struct tcf_block *block = tp->chain->block;
|
| 418 | bool counted = false;
|
| 419 |
|
| 420 | if (!add) {
|
| 421 | if (tp->usesw && tp->counted) {
|
| 422 | if (!atomic_dec_return(&block->useswcnt))
|
| 423 | static_branch_dec(&tcf_sw_enabled_key);
|
| 424 | tp->counted = false;
|
| 425 | }
|
| 426 | return;
|
| 427 | }
|
| 428 |
|
| 429 | spin_lock(&tp->lock);
|
| 430 | if (tp->usesw && !tp->counted) {
|
| 431 | counted = true;
|
| 432 | tp->counted = true;
|
| 433 | }
|
| 434 | spin_unlock(&tp->lock);
|
| 435 |
|
| 436 | if (counted && atomic_inc_return(&block->useswcnt) == 1)
|
| 437 | static_branch_inc(&tcf_sw_enabled_key);
|
| 438 | #endif
|
| 439 | }
|
| 440 |
|
| 441 | static void tcf_chain_put(struct tcf_chain *chain);
|
| 442 |
|
| 443 | static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
|
| 444 | bool sig_destroy, struct netlink_ext_ack *extack)
|
| 445 | {
|
| 446 | |
| 447 | * rtnl_dereference() and mutates shared structures (e.g. the
|
| 448 | * tc_u_common hash list) that are only safe under rtnl_lock. When an
|
| 449 | * unlocked classifier's request (e.g. flower on ingress) loses the
|
| 450 | * tcf_chain_tp_insert_unique() race and ends up dropping the last
|
| 451 | * reference on a locked classifier's proto, destroy() would run
|
| 452 | * without rtnl held. Take it here in that case.
|
| 453 |
|
| 454 | bool not_lockless = !rtnl_held &&
|
| 455 | !(tp->ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
|
| 456 |
|
| 457 | if (not_lockless)
|
| 458 | rtnl_lock();
|
| 459 | tp->ops->destroy(tp, rtnl_held || not_lockless, extack);
|
| 460 | if (not_lockless)
|
| 461 | rtnl_unlock();
|
| 462 | tcf_proto_count_usesw(tp, false);
|
| 463 | if (sig_destroy)
|
| 464 | tcf_proto_signal_destroyed(tp->chain, tp);
|
| 465 | tcf_chain_put(tp->chain);
|
| 466 | module_put(tp->ops->owner);
|
| 467 | kfree_rcu(tp, rcu);
|
| 468 | }
|
| 469 |
|
| 470 | static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
|
| 471 | struct netlink_ext_ack *extack)
|
| 472 | {
|
| 473 | if (refcount_dec_and_test(&tp->refcnt))
|
| 474 | tcf_proto_destroy(tp, rtnl_held, true, extack);
|
| 475 | }
|
| 476 |
|
| 477 | static bool tcf_proto_check_delete(struct tcf_proto *tp)
|
| 478 | {
|
| 479 | if (tp->ops->delete_empty)
|
| 480 | return tp->ops->delete_empty(tp);
|
| 481 |
|
| 482 | tp->deleting = true;
|
| 483 | return tp->deleting;
|
| 484 | }
|
| 485 |
|
| 486 | static void tcf_proto_mark_delete(struct tcf_proto *tp)
|
| 487 | {
|
| 488 | spin_lock(&tp->lock);
|
| 489 | tp->deleting = true;
|
| 490 | spin_unlock(&tp->lock);
|
| 491 | }
|
| 492 |
|
| 493 | static bool tcf_proto_is_deleting(struct tcf_proto *tp)
|
| 494 | {
|
| 495 | bool deleting;
|
| 496 |
|
| 497 | spin_lock(&tp->lock);
|
| 498 | deleting = tp->deleting;
|
| 499 | spin_unlock(&tp->lock);
|
| 500 |
|
| 501 | return deleting;
|
| 502 | }
|
| 503 |
|
| 504 | #define ASSERT_BLOCK_LOCKED(block) \
|
| 505 | lockdep_assert_held(&(block)->lock)
|
| 506 |
|
| 507 | struct tcf_filter_chain_list_item {
|
| 508 | struct list_head list;
|
| 509 | tcf_chain_head_change_t *chain_head_change;
|
| 510 | void *chain_head_change_priv;
|
| 511 | };
|
| 512 |
|
| 513 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
|
| 514 | u32 chain_index)
|
| 515 | {
|
| 516 | struct tcf_chain *chain;
|
| 517 |
|
| 518 | ASSERT_BLOCK_LOCKED(block);
|
| 519 |
|
| 520 | chain = kzalloc_obj(*chain);
|
| 521 | if (!chain)
|
| 522 | return NULL;
|
| 523 | list_add_tail_rcu(&chain->list, &block->chain_list);
|
| 524 | mutex_init(&chain->filter_chain_lock);
|
| 525 | chain->block = block;
|
| 526 | chain->index = chain_index;
|
| 527 | chain->refcnt = 1;
|
| 528 | if (!chain->index)
|
| 529 | block->chain0.chain = chain;
|
| 530 | return chain;
|
| 531 | }
|
| 532 |
|
| 533 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
|
| 534 | struct tcf_proto *tp_head)
|
| 535 | {
|
| 536 | if (item->chain_head_change)
|
| 537 | item->chain_head_change(tp_head, item->chain_head_change_priv);
|
| 538 | }
|
| 539 |
|
| 540 | static void tcf_chain0_head_change(struct tcf_chain *chain,
|
| 541 | struct tcf_proto *tp_head)
|
| 542 | {
|
| 543 | struct tcf_filter_chain_list_item *item;
|
| 544 | struct tcf_block *block = chain->block;
|
| 545 |
|
| 546 | if (chain->index)
|
| 547 | return;
|
| 548 |
|
| 549 | mutex_lock(&block->lock);
|
| 550 | list_for_each_entry(item, &block->chain0.filter_chain_list, list)
|
| 551 | tcf_chain_head_change_item(item, tp_head);
|
| 552 | mutex_unlock(&block->lock);
|
| 553 | }
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 | static bool tcf_chain_detach(struct tcf_chain *chain)
|
| 558 | {
|
| 559 | struct tcf_block *block = chain->block;
|
| 560 |
|
| 561 | ASSERT_BLOCK_LOCKED(block);
|
| 562 |
|
| 563 | list_del_rcu(&chain->list);
|
| 564 | if (!chain->index)
|
| 565 | block->chain0.chain = NULL;
|
| 566 |
|
| 567 | if (list_empty(&block->chain_list) &&
|
| 568 | refcount_read(&block->refcnt) == 0)
|
| 569 | return true;
|
| 570 |
|
| 571 | return false;
|
| 572 | }
|
| 573 |
|
| 574 | static void tcf_block_destroy(struct tcf_block *block)
|
| 575 | {
|
| 576 | mutex_destroy(&block->lock);
|
| 577 | mutex_destroy(&block->proto_destroy_lock);
|
| 578 | xa_destroy(&block->ports);
|
| 579 | kfree_rcu(block, rcu);
|
| 580 | }
|
| 581 |
|
| 582 | static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
|
| 583 | {
|
| 584 | struct tcf_block *block = chain->block;
|
| 585 |
|
| 586 | mutex_destroy(&chain->filter_chain_lock);
|
| 587 | kfree_rcu(chain, rcu);
|
| 588 | if (free_block)
|
| 589 | tcf_block_destroy(block);
|
| 590 | }
|
| 591 |
|
| 592 | static void tcf_chain_hold(struct tcf_chain *chain)
|
| 593 | {
|
| 594 | ASSERT_BLOCK_LOCKED(chain->block);
|
| 595 |
|
| 596 | ++chain->refcnt;
|
| 597 | }
|
| 598 |
|
| 599 | static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
|
| 600 | {
|
| 601 | ASSERT_BLOCK_LOCKED(chain->block);
|
| 602 |
|
| 603 | |
| 604 | * chain should not be shown to the user.
|
| 605 |
|
| 606 | return chain->refcnt == chain->action_refcnt;
|
| 607 | }
|
| 608 |
|
| 609 | static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
|
| 610 | u32 chain_index)
|
| 611 | {
|
| 612 | struct tcf_chain *chain;
|
| 613 |
|
| 614 | ASSERT_BLOCK_LOCKED(block);
|
| 615 |
|
| 616 | list_for_each_entry(chain, &block->chain_list, list) {
|
| 617 | if (chain->index == chain_index)
|
| 618 | return chain;
|
| 619 | }
|
| 620 | return NULL;
|
| 621 | }
|
| 622 |
|
| 623 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
|
| 624 | static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
|
| 625 | u32 chain_index)
|
| 626 | {
|
| 627 | struct tcf_chain *chain;
|
| 628 |
|
| 629 | list_for_each_entry_rcu(chain, &block->chain_list, list) {
|
| 630 | if (chain->index == chain_index)
|
| 631 | return chain;
|
| 632 | }
|
| 633 | return NULL;
|
| 634 | }
|
| 635 | #endif
|
| 636 |
|
| 637 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
|
| 638 | u32 seq, u16 flags, int event, bool unicast,
|
| 639 | struct netlink_ext_ack *extack);
|
| 640 |
|
| 641 | static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
|
| 642 | u32 chain_index, bool create,
|
| 643 | bool by_act)
|
| 644 | {
|
| 645 | struct tcf_chain *chain = NULL;
|
| 646 | bool is_first_reference;
|
| 647 |
|
| 648 | mutex_lock(&block->lock);
|
| 649 | chain = tcf_chain_lookup(block, chain_index);
|
| 650 | if (chain) {
|
| 651 | tcf_chain_hold(chain);
|
| 652 | } else {
|
| 653 | if (!create)
|
| 654 | goto errout;
|
| 655 | chain = tcf_chain_create(block, chain_index);
|
| 656 | if (!chain)
|
| 657 | goto errout;
|
| 658 | }
|
| 659 |
|
| 660 | if (by_act)
|
| 661 | ++chain->action_refcnt;
|
| 662 | is_first_reference = chain->refcnt - chain->action_refcnt == 1;
|
| 663 | mutex_unlock(&block->lock);
|
| 664 |
|
| 665 | |
| 666 | * non-action reference. Until then, the chain acts only as
|
| 667 | * a placeholder for actions pointing to it and user ought
|
| 668 | * not know about them.
|
| 669 |
|
| 670 | if (is_first_reference && !by_act)
|
| 671 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
|
| 672 | RTM_NEWCHAIN, false, NULL);
|
| 673 |
|
| 674 | return chain;
|
| 675 |
|
| 676 | errout:
|
| 677 | mutex_unlock(&block->lock);
|
| 678 | return chain;
|
| 679 | }
|
| 680 |
|
| 681 | static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
|
| 682 | bool create)
|
| 683 | {
|
| 684 | return __tcf_chain_get(block, chain_index, create, false);
|
| 685 | }
|
| 686 |
|
| 687 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
|
| 688 | {
|
| 689 | return __tcf_chain_get(block, chain_index, true, true);
|
| 690 | }
|
| 691 | EXPORT_SYMBOL(tcf_chain_get_by_act);
|
| 692 |
|
| 693 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
|
| 694 | void *tmplt_priv);
|
| 695 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
|
| 696 | void *tmplt_priv, u32 chain_index,
|
| 697 | struct tcf_block *block, struct sk_buff *oskb,
|
| 698 | u32 seq, u16 flags);
|
| 699 |
|
| 700 | static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
|
| 701 | bool explicitly_created)
|
| 702 | {
|
| 703 | struct tcf_block *block = chain->block;
|
| 704 | const struct tcf_proto_ops *tmplt_ops;
|
| 705 | unsigned int refcnt, non_act_refcnt;
|
| 706 | bool free_block = false;
|
| 707 | void *tmplt_priv;
|
| 708 |
|
| 709 | mutex_lock(&block->lock);
|
| 710 | if (explicitly_created) {
|
| 711 | if (!chain->explicitly_created) {
|
| 712 | mutex_unlock(&block->lock);
|
| 713 | return;
|
| 714 | }
|
| 715 | chain->explicitly_created = false;
|
| 716 | }
|
| 717 |
|
| 718 | if (by_act)
|
| 719 | chain->action_refcnt--;
|
| 720 |
|
| 721 | |
| 722 | * However, when block is unlocked chain can be changed concurrently, so
|
| 723 | * save these to temporary variables.
|
| 724 |
|
| 725 | refcnt = --chain->refcnt;
|
| 726 | non_act_refcnt = refcnt - chain->action_refcnt;
|
| 727 | tmplt_ops = chain->tmplt_ops;
|
| 728 | tmplt_priv = chain->tmplt_priv;
|
| 729 |
|
| 730 | if (non_act_refcnt == chain->explicitly_created && !by_act) {
|
| 731 | if (non_act_refcnt == 0)
|
| 732 | tc_chain_notify_delete(tmplt_ops, tmplt_priv,
|
| 733 | chain->index, block, NULL, 0, 0);
|
| 734 |
|
| 735 | chain->flushing = false;
|
| 736 | }
|
| 737 |
|
| 738 | if (refcnt == 0)
|
| 739 | free_block = tcf_chain_detach(chain);
|
| 740 | mutex_unlock(&block->lock);
|
| 741 |
|
| 742 | if (refcnt == 0) {
|
| 743 | tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
|
| 744 | tcf_chain_destroy(chain, free_block);
|
| 745 | }
|
| 746 | }
|
| 747 |
|
| 748 | static void tcf_chain_put(struct tcf_chain *chain)
|
| 749 | {
|
| 750 | __tcf_chain_put(chain, false, false);
|
| 751 | }
|
| 752 |
|
| 753 | void tcf_chain_put_by_act(struct tcf_chain *chain)
|
| 754 | {
|
| 755 | __tcf_chain_put(chain, true, false);
|
| 756 | }
|
| 757 | EXPORT_SYMBOL(tcf_chain_put_by_act);
|
| 758 |
|
| 759 | static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
|
| 760 | {
|
| 761 | __tcf_chain_put(chain, false, true);
|
| 762 | }
|
| 763 |
|
| 764 | static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
|
| 765 | {
|
| 766 | struct tcf_proto *tp, *tp_next;
|
| 767 |
|
| 768 | mutex_lock(&chain->filter_chain_lock);
|
| 769 | tp = tcf_chain_dereference(chain->filter_chain, chain);
|
| 770 | while (tp) {
|
| 771 | tp_next = rcu_dereference_protected(tp->next, 1);
|
| 772 | tcf_proto_signal_destroying(chain, tp);
|
| 773 | tp = tp_next;
|
| 774 | }
|
| 775 | tp = tcf_chain_dereference(chain->filter_chain, chain);
|
| 776 | RCU_INIT_POINTER(chain->filter_chain, NULL);
|
| 777 | tcf_chain0_head_change(chain, NULL);
|
| 778 | chain->flushing = true;
|
| 779 | mutex_unlock(&chain->filter_chain_lock);
|
| 780 |
|
| 781 | while (tp) {
|
| 782 | tp_next = rcu_dereference_protected(tp->next, 1);
|
| 783 | tcf_proto_put(tp, rtnl_held, NULL);
|
| 784 | tp = tp_next;
|
| 785 | }
|
| 786 | }
|
| 787 |
|
| 788 | static int tcf_block_setup(struct tcf_block *block,
|
| 789 | struct flow_block_offload *bo);
|
| 790 |
|
| 791 | static void tcf_block_offload_init(struct flow_block_offload *bo,
|
| 792 | struct net_device *dev, struct Qdisc *sch,
|
| 793 | enum flow_block_command command,
|
| 794 | enum flow_block_binder_type binder_type,
|
| 795 | struct flow_block *flow_block,
|
| 796 | bool shared, struct netlink_ext_ack *extack)
|
| 797 | {
|
| 798 | bo->net = dev_net(dev);
|
| 799 | bo->command = command;
|
| 800 | bo->binder_type = binder_type;
|
| 801 | bo->block = flow_block;
|
| 802 | bo->block_shared = shared;
|
| 803 | bo->extack = extack;
|
| 804 | bo->sch = sch;
|
| 805 | bo->cb_list_head = &flow_block->cb_list;
|
| 806 | INIT_LIST_HEAD(&bo->cb_list);
|
| 807 | }
|
| 808 |
|
| 809 | static void tcf_block_unbind(struct tcf_block *block,
|
| 810 | struct flow_block_offload *bo);
|
| 811 |
|
| 812 | static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
|
| 813 | {
|
| 814 | struct tcf_block *block = block_cb->indr.data;
|
| 815 | struct net_device *dev = block_cb->indr.dev;
|
| 816 | struct Qdisc *sch = block_cb->indr.sch;
|
| 817 | struct netlink_ext_ack extack = {};
|
| 818 | struct flow_block_offload bo = {};
|
| 819 |
|
| 820 | tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
|
| 821 | block_cb->indr.binder_type,
|
| 822 | &block->flow_block, tcf_block_shared(block),
|
| 823 | &extack);
|
| 824 | rtnl_lock();
|
| 825 | down_write(&block->cb_lock);
|
| 826 | list_del(&block_cb->driver_list);
|
| 827 | list_move(&block_cb->list, &bo.cb_list);
|
| 828 | tcf_block_unbind(block, &bo);
|
| 829 | up_write(&block->cb_lock);
|
| 830 | rtnl_unlock();
|
| 831 | }
|
| 832 |
|
| 833 | static bool tcf_block_offload_in_use(struct tcf_block *block)
|
| 834 | {
|
| 835 | return atomic_read(&block->offloadcnt);
|
| 836 | }
|
| 837 |
|
| 838 | static int tcf_block_offload_cmd(struct tcf_block *block,
|
| 839 | struct net_device *dev, struct Qdisc *sch,
|
| 840 | struct tcf_block_ext_info *ei,
|
| 841 | enum flow_block_command command,
|
| 842 | struct netlink_ext_ack *extack)
|
| 843 | {
|
| 844 | struct flow_block_offload bo = {};
|
| 845 |
|
| 846 | tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
|
| 847 | &block->flow_block, tcf_block_shared(block),
|
| 848 | extack);
|
| 849 |
|
| 850 | if (dev->netdev_ops->ndo_setup_tc) {
|
| 851 | int err;
|
| 852 |
|
| 853 | err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
|
| 854 | if (err < 0) {
|
| 855 | if (err != -EOPNOTSUPP)
|
| 856 | NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
|
| 857 | return err;
|
| 858 | }
|
| 859 |
|
| 860 | return tcf_block_setup(block, &bo);
|
| 861 | }
|
| 862 |
|
| 863 | flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
|
| 864 | tc_block_indr_cleanup);
|
| 865 | tcf_block_setup(block, &bo);
|
| 866 |
|
| 867 | return -EOPNOTSUPP;
|
| 868 | }
|
| 869 |
|
| 870 | static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
|
| 871 | struct tcf_block_ext_info *ei,
|
| 872 | struct netlink_ext_ack *extack)
|
| 873 | {
|
| 874 | struct net_device *dev = q->dev_queue->dev;
|
| 875 | int err;
|
| 876 |
|
| 877 | down_write(&block->cb_lock);
|
| 878 |
|
| 879 | |
| 880 | * to already has some offloaded filters, forbid to bind.
|
| 881 |
|
| 882 | if (dev->netdev_ops->ndo_setup_tc &&
|
| 883 | !tc_can_offload(dev) &&
|
| 884 | tcf_block_offload_in_use(block)) {
|
| 885 | NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
|
| 886 | err = -EOPNOTSUPP;
|
| 887 | goto err_unlock;
|
| 888 | }
|
| 889 |
|
| 890 | err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
|
| 891 | if (err == -EOPNOTSUPP)
|
| 892 | goto no_offload_dev_inc;
|
| 893 | if (err)
|
| 894 | goto err_unlock;
|
| 895 |
|
| 896 | up_write(&block->cb_lock);
|
| 897 | return 0;
|
| 898 |
|
| 899 | no_offload_dev_inc:
|
| 900 | if (tcf_block_offload_in_use(block))
|
| 901 | goto err_unlock;
|
| 902 |
|
| 903 | err = 0;
|
| 904 | block->nooffloaddevcnt++;
|
| 905 | err_unlock:
|
| 906 | up_write(&block->cb_lock);
|
| 907 | return err;
|
| 908 | }
|
| 909 |
|
| 910 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
|
| 911 | struct tcf_block_ext_info *ei)
|
| 912 | {
|
| 913 | struct net_device *dev = q->dev_queue->dev;
|
| 914 | int err;
|
| 915 |
|
| 916 | down_write(&block->cb_lock);
|
| 917 | err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
|
| 918 | if (err == -EOPNOTSUPP)
|
| 919 | goto no_offload_dev_dec;
|
| 920 | up_write(&block->cb_lock);
|
| 921 | return;
|
| 922 |
|
| 923 | no_offload_dev_dec:
|
| 924 | WARN_ON(block->nooffloaddevcnt-- == 0);
|
| 925 | up_write(&block->cb_lock);
|
| 926 | }
|
| 927 |
|
| 928 | static int
|
| 929 | tcf_chain0_head_change_cb_add(struct tcf_block *block,
|
| 930 | struct tcf_block_ext_info *ei,
|
| 931 | struct netlink_ext_ack *extack)
|
| 932 | {
|
| 933 | struct tcf_filter_chain_list_item *item;
|
| 934 | struct tcf_chain *chain0;
|
| 935 |
|
| 936 | item = kmalloc_obj(*item);
|
| 937 | if (!item) {
|
| 938 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
|
| 939 | return -ENOMEM;
|
| 940 | }
|
| 941 | item->chain_head_change = ei->chain_head_change;
|
| 942 | item->chain_head_change_priv = ei->chain_head_change_priv;
|
| 943 |
|
| 944 | mutex_lock(&block->lock);
|
| 945 | chain0 = block->chain0.chain;
|
| 946 | if (chain0)
|
| 947 | tcf_chain_hold(chain0);
|
| 948 | else
|
| 949 | list_add(&item->list, &block->chain0.filter_chain_list);
|
| 950 | mutex_unlock(&block->lock);
|
| 951 |
|
| 952 | if (chain0) {
|
| 953 | struct tcf_proto *tp_head;
|
| 954 |
|
| 955 | mutex_lock(&chain0->filter_chain_lock);
|
| 956 |
|
| 957 | tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
|
| 958 | if (tp_head)
|
| 959 | tcf_chain_head_change_item(item, tp_head);
|
| 960 |
|
| 961 | mutex_lock(&block->lock);
|
| 962 | list_add(&item->list, &block->chain0.filter_chain_list);
|
| 963 | mutex_unlock(&block->lock);
|
| 964 |
|
| 965 | mutex_unlock(&chain0->filter_chain_lock);
|
| 966 | tcf_chain_put(chain0);
|
| 967 | }
|
| 968 |
|
| 969 | return 0;
|
| 970 | }
|
| 971 |
|
| 972 | static void
|
| 973 | tcf_chain0_head_change_cb_del(struct tcf_block *block,
|
| 974 | struct tcf_block_ext_info *ei)
|
| 975 | {
|
| 976 | struct tcf_filter_chain_list_item *item;
|
| 977 |
|
| 978 | mutex_lock(&block->lock);
|
| 979 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
|
| 980 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
|
| 981 | (item->chain_head_change == ei->chain_head_change &&
|
| 982 | item->chain_head_change_priv == ei->chain_head_change_priv)) {
|
| 983 | if (block->chain0.chain)
|
| 984 | tcf_chain_head_change_item(item, NULL);
|
| 985 | list_del(&item->list);
|
| 986 | mutex_unlock(&block->lock);
|
| 987 |
|
| 988 | kfree(item);
|
| 989 | return;
|
| 990 | }
|
| 991 | }
|
| 992 | mutex_unlock(&block->lock);
|
| 993 | WARN_ON(1);
|
| 994 | }
|
| 995 |
|
| 996 | struct tcf_net {
|
| 997 | spinlock_t idr_lock;
|
| 998 | struct idr idr;
|
| 999 | };
|
| 1000 |
|
| 1001 | static unsigned int tcf_net_id;
|
| 1002 |
|
| 1003 | static int tcf_block_insert(struct tcf_block *block, struct net *net,
|
| 1004 | struct netlink_ext_ack *extack)
|
| 1005 | {
|
| 1006 | struct tcf_net *tn = net_generic(net, tcf_net_id);
|
| 1007 | int err;
|
| 1008 |
|
| 1009 | idr_preload(GFP_KERNEL);
|
| 1010 | spin_lock(&tn->idr_lock);
|
| 1011 | err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
|
| 1012 | GFP_NOWAIT);
|
| 1013 | spin_unlock(&tn->idr_lock);
|
| 1014 | idr_preload_end();
|
| 1015 |
|
| 1016 | return err;
|
| 1017 | }
|
| 1018 |
|
| 1019 | static void tcf_block_remove(struct tcf_block *block, struct net *net)
|
| 1020 | {
|
| 1021 | struct tcf_net *tn = net_generic(net, tcf_net_id);
|
| 1022 |
|
| 1023 | spin_lock(&tn->idr_lock);
|
| 1024 | idr_remove(&tn->idr, block->index);
|
| 1025 | spin_unlock(&tn->idr_lock);
|
| 1026 | }
|
| 1027 |
|
| 1028 | static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
|
| 1029 | u32 block_index,
|
| 1030 | struct netlink_ext_ack *extack)
|
| 1031 | {
|
| 1032 | struct tcf_block *block;
|
| 1033 |
|
| 1034 | block = kzalloc_obj(*block);
|
| 1035 | if (!block) {
|
| 1036 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
|
| 1037 | return ERR_PTR(-ENOMEM);
|
| 1038 | }
|
| 1039 | mutex_init(&block->lock);
|
| 1040 | mutex_init(&block->proto_destroy_lock);
|
| 1041 | init_rwsem(&block->cb_lock);
|
| 1042 | flow_block_init(&block->flow_block);
|
| 1043 | INIT_LIST_HEAD(&block->chain_list);
|
| 1044 | INIT_LIST_HEAD(&block->owner_list);
|
| 1045 | INIT_LIST_HEAD(&block->chain0.filter_chain_list);
|
| 1046 |
|
| 1047 | refcount_set(&block->refcnt, 1);
|
| 1048 | block->net = net;
|
| 1049 | block->index = block_index;
|
| 1050 | xa_init(&block->ports);
|
| 1051 |
|
| 1052 |
|
| 1053 | if (!tcf_block_shared(block))
|
| 1054 | block->q = q;
|
| 1055 | return block;
|
| 1056 | }
|
| 1057 |
|
| 1058 | struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
|
| 1059 | {
|
| 1060 | struct tcf_net *tn = net_generic(net, tcf_net_id);
|
| 1061 |
|
| 1062 | return idr_find(&tn->idr, block_index);
|
| 1063 | }
|
| 1064 | EXPORT_SYMBOL(tcf_block_lookup);
|
| 1065 |
|
| 1066 | static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
|
| 1067 | {
|
| 1068 | struct tcf_block *block;
|
| 1069 |
|
| 1070 | rcu_read_lock();
|
| 1071 | block = tcf_block_lookup(net, block_index);
|
| 1072 | if (block && !refcount_inc_not_zero(&block->refcnt))
|
| 1073 | block = NULL;
|
| 1074 | rcu_read_unlock();
|
| 1075 |
|
| 1076 | return block;
|
| 1077 | }
|
| 1078 |
|
| 1079 | static struct tcf_chain *
|
| 1080 | __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
|
| 1081 | {
|
| 1082 | mutex_lock(&block->lock);
|
| 1083 | if (chain)
|
| 1084 | chain = list_is_last(&chain->list, &block->chain_list) ?
|
| 1085 | NULL : list_next_entry(chain, list);
|
| 1086 | else
|
| 1087 | chain = list_first_entry_or_null(&block->chain_list,
|
| 1088 | struct tcf_chain, list);
|
| 1089 |
|
| 1090 |
|
| 1091 | while (chain && tcf_chain_held_by_acts_only(chain))
|
| 1092 | chain = list_is_last(&chain->list, &block->chain_list) ?
|
| 1093 | NULL : list_next_entry(chain, list);
|
| 1094 |
|
| 1095 | if (chain)
|
| 1096 | tcf_chain_hold(chain);
|
| 1097 | mutex_unlock(&block->lock);
|
| 1098 |
|
| 1099 | return chain;
|
| 1100 | }
|
| 1101 |
|
| 1102 | |
| 1103 | * block. It properly obtains block->lock and takes reference to chain before
|
| 1104 | * returning it. Users of this function must be tolerant to concurrent chain
|
| 1105 | * insertion/deletion or ensure that no concurrent chain modification is
|
| 1106 | * possible. Note that all netlink dump callbacks cannot guarantee to provide
|
| 1107 | * consistent dump because rtnl lock is released each time skb is filled with
|
| 1108 | * data and sent to user-space.
|
| 1109 |
|
| 1110 |
|
| 1111 | struct tcf_chain *
|
| 1112 | tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
|
| 1113 | {
|
| 1114 | struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
|
| 1115 |
|
| 1116 | if (chain)
|
| 1117 | tcf_chain_put(chain);
|
| 1118 |
|
| 1119 | return chain_next;
|
| 1120 | }
|
| 1121 | EXPORT_SYMBOL(tcf_get_next_chain);
|
| 1122 |
|
| 1123 | static struct tcf_proto *
|
| 1124 | __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
|
| 1125 | {
|
| 1126 | u32 prio = 0;
|
| 1127 |
|
| 1128 | ASSERT_RTNL();
|
| 1129 | mutex_lock(&chain->filter_chain_lock);
|
| 1130 |
|
| 1131 | if (!tp) {
|
| 1132 | tp = tcf_chain_dereference(chain->filter_chain, chain);
|
| 1133 | } else if (tcf_proto_is_deleting(tp)) {
|
| 1134 | |
| 1135 | * unlocked, which means next pointer could be invalid. Restart
|
| 1136 | * search.
|
| 1137 |
|
| 1138 | prio = tp->prio + 1;
|
| 1139 | tp = tcf_chain_dereference(chain->filter_chain, chain);
|
| 1140 |
|
| 1141 | for (; tp; tp = tcf_chain_dereference(tp->next, chain))
|
| 1142 | if (!tp->deleting && tp->prio >= prio)
|
| 1143 | break;
|
| 1144 | } else {
|
| 1145 | tp = tcf_chain_dereference(tp->next, chain);
|
| 1146 | }
|
| 1147 |
|
| 1148 | if (tp)
|
| 1149 | tcf_proto_get(tp);
|
| 1150 |
|
| 1151 | mutex_unlock(&chain->filter_chain_lock);
|
| 1152 |
|
| 1153 | return tp;
|
| 1154 | }
|
| 1155 |
|
| 1156 | |
| 1157 | * chain. Users of this function must be tolerant to concurrent tp
|
| 1158 | * insertion/deletion or ensure that no concurrent chain modification is
|
| 1159 | * possible. Note that all netlink dump callbacks cannot guarantee to provide
|
| 1160 | * consistent dump because rtnl lock is released each time skb is filled with
|
| 1161 | * data and sent to user-space.
|
| 1162 |
|
| 1163 |
|
| 1164 | struct tcf_proto *
|
| 1165 | tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
|
| 1166 | {
|
| 1167 | struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
|
| 1168 |
|
| 1169 | if (tp)
|
| 1170 | tcf_proto_put(tp, true, NULL);
|
| 1171 |
|
| 1172 | return tp_next;
|
| 1173 | }
|
| 1174 | EXPORT_SYMBOL(tcf_get_next_proto);
|
| 1175 |
|
| 1176 | static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
|
| 1177 | {
|
| 1178 | struct tcf_chain *chain;
|
| 1179 |
|
| 1180 | |
| 1181 | * removed concurrently.
|
| 1182 |
|
| 1183 | for (chain = tcf_get_next_chain(block, NULL);
|
| 1184 | chain;
|
| 1185 | chain = tcf_get_next_chain(block, chain)) {
|
| 1186 | tcf_chain_put_explicitly_created(chain);
|
| 1187 | tcf_chain_flush(chain, rtnl_held);
|
| 1188 | }
|
| 1189 | }
|
| 1190 |
|
| 1191 | |
| 1192 | * Set parent, if necessary.
|
| 1193 |
|
| 1194 |
|
| 1195 | static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
|
| 1196 | u32 *parent, int ifindex, bool rtnl_held,
|
| 1197 | struct netlink_ext_ack *extack)
|
| 1198 | {
|
| 1199 | const struct Qdisc_class_ops *cops;
|
| 1200 | struct net_device *dev;
|
| 1201 | int err = 0;
|
| 1202 |
|
| 1203 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
|
| 1204 | return 0;
|
| 1205 |
|
| 1206 | rcu_read_lock();
|
| 1207 |
|
| 1208 |
|
| 1209 | dev = dev_get_by_index_rcu(net, ifindex);
|
| 1210 | if (!dev) {
|
| 1211 | rcu_read_unlock();
|
| 1212 | return -ENODEV;
|
| 1213 | }
|
| 1214 |
|
| 1215 |
|
| 1216 | if (!*parent) {
|
| 1217 | *q = rcu_dereference(dev->qdisc);
|
| 1218 | *parent = (*q)->handle;
|
| 1219 | } else {
|
| 1220 | *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
|
| 1221 | if (!*q) {
|
| 1222 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
|
| 1223 | err = -EINVAL;
|
| 1224 | goto errout_rcu;
|
| 1225 | }
|
| 1226 | }
|
| 1227 |
|
| 1228 | *q = qdisc_refcount_inc_nz(*q);
|
| 1229 | if (!*q) {
|
| 1230 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
|
| 1231 | err = -EINVAL;
|
| 1232 | goto errout_rcu;
|
| 1233 | }
|
| 1234 |
|
| 1235 |
|
| 1236 | cops = (*q)->ops->cl_ops;
|
| 1237 | if (!cops) {
|
| 1238 | NL_SET_ERR_MSG(extack, "Qdisc not classful");
|
| 1239 | err = -EINVAL;
|
| 1240 | goto errout_qdisc;
|
| 1241 | }
|
| 1242 |
|
| 1243 | if (!cops->tcf_block) {
|
| 1244 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
|
| 1245 | err = -EOPNOTSUPP;
|
| 1246 | goto errout_qdisc;
|
| 1247 | }
|
| 1248 |
|
| 1249 | errout_rcu:
|
| 1250 | |
| 1251 | * which means that qdisc holds a reference to net_device
|
| 1252 | * and we hold a reference to qdisc, so it is safe to release
|
| 1253 | * rcu read lock.
|
| 1254 |
|
| 1255 | rcu_read_unlock();
|
| 1256 | return err;
|
| 1257 |
|
| 1258 | errout_qdisc:
|
| 1259 | rcu_read_unlock();
|
| 1260 |
|
| 1261 | if (rtnl_held)
|
| 1262 | qdisc_put(*q);
|
| 1263 | else
|
| 1264 | qdisc_put_unlocked(*q);
|
| 1265 | *q = NULL;
|
| 1266 |
|
| 1267 | return err;
|
| 1268 | }
|
| 1269 |
|
| 1270 | static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
|
| 1271 | int ifindex, struct netlink_ext_ack *extack)
|
| 1272 | {
|
| 1273 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
|
| 1274 | return 0;
|
| 1275 |
|
| 1276 |
|
| 1277 | if (TC_H_MIN(parent)) {
|
| 1278 | const struct Qdisc_class_ops *cops = q->ops->cl_ops;
|
| 1279 |
|
| 1280 | *cl = cops->find(q, parent);
|
| 1281 | if (*cl == 0) {
|
| 1282 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
|
| 1283 | return -ENOENT;
|
| 1284 | }
|
| 1285 | }
|
| 1286 |
|
| 1287 | return 0;
|
| 1288 | }
|
| 1289 |
|
| 1290 | static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
|
| 1291 | unsigned long cl, int ifindex,
|
| 1292 | u32 block_index,
|
| 1293 | struct netlink_ext_ack *extack)
|
| 1294 | {
|
| 1295 | struct tcf_block *block;
|
| 1296 |
|
| 1297 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
|
| 1298 | block = tcf_block_refcnt_get(net, block_index);
|
| 1299 | if (!block) {
|
| 1300 | NL_SET_ERR_MSG(extack, "Block of given index was not found");
|
| 1301 | return ERR_PTR(-EINVAL);
|
| 1302 | }
|
| 1303 | } else {
|
| 1304 | const struct Qdisc_class_ops *cops = q->ops->cl_ops;
|
| 1305 |
|
| 1306 | block = cops->tcf_block(q, cl, extack);
|
| 1307 | if (!block)
|
| 1308 | return ERR_PTR(-EINVAL);
|
| 1309 |
|
| 1310 | if (tcf_block_shared(block)) {
|
| 1311 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
|
| 1312 | return ERR_PTR(-EOPNOTSUPP);
|
| 1313 | }
|
| 1314 |
|
| 1315 | |
| 1316 | * of rules update path of cls API without rtnl lock. Caller
|
| 1317 | * must release block when it is finished using it. 'if' block
|
| 1318 | * of this conditional obtain reference to block by calling
|
| 1319 | * tcf_block_refcnt_get().
|
| 1320 |
|
| 1321 | refcount_inc(&block->refcnt);
|
| 1322 | }
|
| 1323 |
|
| 1324 | return block;
|
| 1325 | }
|
| 1326 |
|
| 1327 | static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
|
| 1328 | struct tcf_block_ext_info *ei, bool rtnl_held)
|
| 1329 | {
|
| 1330 | if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
|
| 1331 | |
| 1332 | * deallocated when last chain is freed. However, if chain_list
|
| 1333 | * is empty, block has to be manually deallocated. After block
|
| 1334 | * reference counter reached 0, it is no longer possible to
|
| 1335 | * increment it or add new chains to block.
|
| 1336 |
|
| 1337 | bool free_block = list_empty(&block->chain_list);
|
| 1338 |
|
| 1339 | mutex_unlock(&block->lock);
|
| 1340 | if (tcf_block_shared(block))
|
| 1341 | tcf_block_remove(block, block->net);
|
| 1342 |
|
| 1343 | if (q)
|
| 1344 | tcf_block_offload_unbind(block, q, ei);
|
| 1345 |
|
| 1346 | if (free_block)
|
| 1347 | tcf_block_destroy(block);
|
| 1348 | else
|
| 1349 | tcf_block_flush_all_chains(block, rtnl_held);
|
| 1350 | } else if (q) {
|
| 1351 | tcf_block_offload_unbind(block, q, ei);
|
| 1352 | }
|
| 1353 | }
|
| 1354 |
|
| 1355 | static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
|
| 1356 | {
|
| 1357 | __tcf_block_put(block, NULL, NULL, rtnl_held);
|
| 1358 | }
|
| 1359 |
|
| 1360 | |
| 1361 | * Set q, parent, cl when appropriate.
|
| 1362 |
|
| 1363 |
|
| 1364 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
|
| 1365 | u32 *parent, unsigned long *cl,
|
| 1366 | int ifindex, u32 block_index,
|
| 1367 | struct netlink_ext_ack *extack)
|
| 1368 | {
|
| 1369 | struct tcf_block *block;
|
| 1370 | int err = 0;
|
| 1371 |
|
| 1372 | ASSERT_RTNL();
|
| 1373 |
|
| 1374 | err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
|
| 1375 | if (err)
|
| 1376 | goto errout;
|
| 1377 |
|
| 1378 | err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
|
| 1379 | if (err)
|
| 1380 | goto errout_qdisc;
|
| 1381 |
|
| 1382 | block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
|
| 1383 | if (IS_ERR(block)) {
|
| 1384 | err = PTR_ERR(block);
|
| 1385 | goto errout_qdisc;
|
| 1386 | }
|
| 1387 |
|
| 1388 | return block;
|
| 1389 |
|
| 1390 | errout_qdisc:
|
| 1391 | if (*q)
|
| 1392 | qdisc_put(*q);
|
| 1393 | errout:
|
| 1394 | *q = NULL;
|
| 1395 | return ERR_PTR(err);
|
| 1396 | }
|
| 1397 |
|
| 1398 | static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
|
| 1399 | bool rtnl_held)
|
| 1400 | {
|
| 1401 | if (!IS_ERR_OR_NULL(block))
|
| 1402 | tcf_block_refcnt_put(block, rtnl_held);
|
| 1403 |
|
| 1404 | if (q) {
|
| 1405 | if (rtnl_held)
|
| 1406 | qdisc_put(q);
|
| 1407 | else
|
| 1408 | qdisc_put_unlocked(q);
|
| 1409 | }
|
| 1410 | }
|
| 1411 |
|
| 1412 | struct tcf_block_owner_item {
|
| 1413 | struct list_head list;
|
| 1414 | struct Qdisc *q;
|
| 1415 | enum flow_block_binder_type binder_type;
|
| 1416 | };
|
| 1417 |
|
| 1418 | static void
|
| 1419 | tcf_block_owner_netif_keep_dst(struct tcf_block *block,
|
| 1420 | struct Qdisc *q,
|
| 1421 | enum flow_block_binder_type binder_type)
|
| 1422 | {
|
| 1423 | if (block->keep_dst &&
|
| 1424 | binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
|
| 1425 | binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
|
| 1426 | netif_keep_dst(qdisc_dev(q));
|
| 1427 | }
|
| 1428 |
|
| 1429 | void tcf_block_netif_keep_dst(struct tcf_block *block)
|
| 1430 | {
|
| 1431 | struct tcf_block_owner_item *item;
|
| 1432 |
|
| 1433 | block->keep_dst = true;
|
| 1434 | list_for_each_entry(item, &block->owner_list, list)
|
| 1435 | tcf_block_owner_netif_keep_dst(block, item->q,
|
| 1436 | item->binder_type);
|
| 1437 | }
|
| 1438 | EXPORT_SYMBOL(tcf_block_netif_keep_dst);
|
| 1439 |
|
| 1440 | static int tcf_block_owner_add(struct tcf_block *block,
|
| 1441 | struct Qdisc *q,
|
| 1442 | enum flow_block_binder_type binder_type)
|
| 1443 | {
|
| 1444 | struct tcf_block_owner_item *item;
|
| 1445 |
|
| 1446 | item = kmalloc_obj(*item);
|
| 1447 | if (!item)
|
| 1448 | return -ENOMEM;
|
| 1449 | item->q = q;
|
| 1450 | item->binder_type = binder_type;
|
| 1451 | list_add(&item->list, &block->owner_list);
|
| 1452 | return 0;
|
| 1453 | }
|
| 1454 |
|
| 1455 | static void tcf_block_owner_del(struct tcf_block *block,
|
| 1456 | struct Qdisc *q,
|
| 1457 | enum flow_block_binder_type binder_type)
|
| 1458 | {
|
| 1459 | struct tcf_block_owner_item *item;
|
| 1460 |
|
| 1461 | list_for_each_entry(item, &block->owner_list, list) {
|
| 1462 | if (item->q == q && item->binder_type == binder_type) {
|
| 1463 | list_del(&item->list);
|
| 1464 | kfree(item);
|
| 1465 | return;
|
| 1466 | }
|
| 1467 | }
|
| 1468 | WARN_ON(1);
|
| 1469 | }
|
| 1470 |
|
| 1471 | static bool tcf_block_tracks_dev(struct tcf_block *block,
|
| 1472 | struct tcf_block_ext_info *ei)
|
| 1473 | {
|
| 1474 | return tcf_block_shared(block) &&
|
| 1475 | (ei->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS ||
|
| 1476 | ei->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS);
|
| 1477 | }
|
| 1478 |
|
| 1479 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
|
| 1480 | struct tcf_block_ext_info *ei,
|
| 1481 | struct netlink_ext_ack *extack)
|
| 1482 | {
|
| 1483 | struct net_device *dev = qdisc_dev(q);
|
| 1484 | struct net *net = qdisc_net(q);
|
| 1485 | struct tcf_block *block = NULL;
|
| 1486 | int err;
|
| 1487 |
|
| 1488 | if (ei->block_index)
|
| 1489 |
|
| 1490 | block = tcf_block_refcnt_get(net, ei->block_index);
|
| 1491 |
|
| 1492 | if (!block) {
|
| 1493 | block = tcf_block_create(net, q, ei->block_index, extack);
|
| 1494 | if (IS_ERR(block))
|
| 1495 | return PTR_ERR(block);
|
| 1496 | if (tcf_block_shared(block)) {
|
| 1497 | err = tcf_block_insert(block, net, extack);
|
| 1498 | if (err)
|
| 1499 | goto err_block_insert;
|
| 1500 | }
|
| 1501 | }
|
| 1502 |
|
| 1503 | err = tcf_block_owner_add(block, q, ei->binder_type);
|
| 1504 | if (err)
|
| 1505 | goto err_block_owner_add;
|
| 1506 |
|
| 1507 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
|
| 1508 |
|
| 1509 | err = tcf_chain0_head_change_cb_add(block, ei, extack);
|
| 1510 | if (err)
|
| 1511 | goto err_chain0_head_change_cb_add;
|
| 1512 |
|
| 1513 | err = tcf_block_offload_bind(block, q, ei, extack);
|
| 1514 | if (err)
|
| 1515 | goto err_block_offload_bind;
|
| 1516 |
|
| 1517 | if (tcf_block_tracks_dev(block, ei)) {
|
| 1518 | err = xa_insert(&block->ports, dev->ifindex, dev, GFP_KERNEL);
|
| 1519 | if (err) {
|
| 1520 | NL_SET_ERR_MSG(extack, "block dev insert failed");
|
| 1521 | goto err_dev_insert;
|
| 1522 | }
|
| 1523 | }
|
| 1524 |
|
| 1525 | *p_block = block;
|
| 1526 | return 0;
|
| 1527 |
|
| 1528 | err_dev_insert:
|
| 1529 | tcf_block_offload_unbind(block, q, ei);
|
| 1530 | err_block_offload_bind:
|
| 1531 | tcf_chain0_head_change_cb_del(block, ei);
|
| 1532 | err_chain0_head_change_cb_add:
|
| 1533 | tcf_block_owner_del(block, q, ei->binder_type);
|
| 1534 | err_block_owner_add:
|
| 1535 | err_block_insert:
|
| 1536 | tcf_block_refcnt_put(block, true);
|
| 1537 | return err;
|
| 1538 | }
|
| 1539 | EXPORT_SYMBOL(tcf_block_get_ext);
|
| 1540 |
|
| 1541 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
|
| 1542 | {
|
| 1543 | struct tcf_proto __rcu **p_filter_chain = priv;
|
| 1544 |
|
| 1545 | rcu_assign_pointer(*p_filter_chain, tp_head);
|
| 1546 | }
|
| 1547 |
|
| 1548 | int tcf_block_get(struct tcf_block **p_block,
|
| 1549 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
|
| 1550 | struct netlink_ext_ack *extack)
|
| 1551 | {
|
| 1552 | struct tcf_block_ext_info ei = {
|
| 1553 | .chain_head_change = tcf_chain_head_change_dflt,
|
| 1554 | .chain_head_change_priv = p_filter_chain,
|
| 1555 | };
|
| 1556 |
|
| 1557 | WARN_ON(!p_filter_chain);
|
| 1558 | return tcf_block_get_ext(p_block, q, &ei, extack);
|
| 1559 | }
|
| 1560 | EXPORT_SYMBOL(tcf_block_get);
|
| 1561 |
|
| 1562 | |
| 1563 | * actions should be all removed after flushing.
|
| 1564 |
|
| 1565 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
|
| 1566 | struct tcf_block_ext_info *ei)
|
| 1567 | {
|
| 1568 | struct net_device *dev = qdisc_dev(q);
|
| 1569 |
|
| 1570 | if (!block)
|
| 1571 | return;
|
| 1572 | if (tcf_block_tracks_dev(block, ei))
|
| 1573 | xa_erase(&block->ports, dev->ifindex);
|
| 1574 | tcf_chain0_head_change_cb_del(block, ei);
|
| 1575 | tcf_block_owner_del(block, q, ei->binder_type);
|
| 1576 |
|
| 1577 | __tcf_block_put(block, q, ei, true);
|
| 1578 | }
|
| 1579 | EXPORT_SYMBOL(tcf_block_put_ext);
|
| 1580 |
|
| 1581 | void tcf_block_put(struct tcf_block *block)
|
| 1582 | {
|
| 1583 | struct tcf_block_ext_info ei = {0, };
|
| 1584 |
|
| 1585 | if (!block)
|
| 1586 | return;
|
| 1587 | tcf_block_put_ext(block, block->q, &ei);
|
| 1588 | }
|
| 1589 |
|
| 1590 | EXPORT_SYMBOL(tcf_block_put);
|
| 1591 |
|
| 1592 | static int
|
| 1593 | tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
|
| 1594 | void *cb_priv, bool add, bool offload_in_use,
|
| 1595 | struct netlink_ext_ack *extack)
|
| 1596 | {
|
| 1597 | struct tcf_chain *chain, *chain_prev;
|
| 1598 | struct tcf_proto *tp, *tp_prev;
|
| 1599 | int err;
|
| 1600 |
|
| 1601 | lockdep_assert_held(&block->cb_lock);
|
| 1602 |
|
| 1603 | for (chain = __tcf_get_next_chain(block, NULL);
|
| 1604 | chain;
|
| 1605 | chain_prev = chain,
|
| 1606 | chain = __tcf_get_next_chain(block, chain),
|
| 1607 | tcf_chain_put(chain_prev)) {
|
| 1608 | if (chain->tmplt_ops && add)
|
| 1609 | chain->tmplt_ops->tmplt_reoffload(chain, true, cb,
|
| 1610 | cb_priv);
|
| 1611 | for (tp = __tcf_get_next_proto(chain, NULL); tp;
|
| 1612 | tp_prev = tp,
|
| 1613 | tp = __tcf_get_next_proto(chain, tp),
|
| 1614 | tcf_proto_put(tp_prev, true, NULL)) {
|
| 1615 | if (tp->ops->reoffload) {
|
| 1616 | err = tp->ops->reoffload(tp, add, cb, cb_priv,
|
| 1617 | extack);
|
| 1618 | if (err && add)
|
| 1619 | goto err_playback_remove;
|
| 1620 | } else if (add && offload_in_use) {
|
| 1621 | err = -EOPNOTSUPP;
|
| 1622 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
|
| 1623 | goto err_playback_remove;
|
| 1624 | }
|
| 1625 | }
|
| 1626 | if (chain->tmplt_ops && !add)
|
| 1627 | chain->tmplt_ops->tmplt_reoffload(chain, false, cb,
|
| 1628 | cb_priv);
|
| 1629 | }
|
| 1630 |
|
| 1631 | return 0;
|
| 1632 |
|
| 1633 | err_playback_remove:
|
| 1634 | tcf_proto_put(tp, true, NULL);
|
| 1635 | tcf_chain_put(chain);
|
| 1636 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
|
| 1637 | extack);
|
| 1638 | return err;
|
| 1639 | }
|
| 1640 |
|
| 1641 | static int tcf_block_bind(struct tcf_block *block,
|
| 1642 | struct flow_block_offload *bo)
|
| 1643 | {
|
| 1644 | struct flow_block_cb *block_cb, *next;
|
| 1645 | int err, i = 0;
|
| 1646 |
|
| 1647 | lockdep_assert_held(&block->cb_lock);
|
| 1648 |
|
| 1649 | list_for_each_entry(block_cb, &bo->cb_list, list) {
|
| 1650 | err = tcf_block_playback_offloads(block, block_cb->cb,
|
| 1651 | block_cb->cb_priv, true,
|
| 1652 | tcf_block_offload_in_use(block),
|
| 1653 | bo->extack);
|
| 1654 | if (err)
|
| 1655 | goto err_unroll;
|
| 1656 | if (!bo->unlocked_driver_cb)
|
| 1657 | block->lockeddevcnt++;
|
| 1658 |
|
| 1659 | i++;
|
| 1660 | }
|
| 1661 | list_splice(&bo->cb_list, &block->flow_block.cb_list);
|
| 1662 |
|
| 1663 | return 0;
|
| 1664 |
|
| 1665 | err_unroll:
|
| 1666 | list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
|
| 1667 | list_del(&block_cb->driver_list);
|
| 1668 | if (i-- > 0) {
|
| 1669 | list_del(&block_cb->list);
|
| 1670 | tcf_block_playback_offloads(block, block_cb->cb,
|
| 1671 | block_cb->cb_priv, false,
|
| 1672 | tcf_block_offload_in_use(block),
|
| 1673 | NULL);
|
| 1674 | if (!bo->unlocked_driver_cb)
|
| 1675 | block->lockeddevcnt--;
|
| 1676 | }
|
| 1677 | flow_block_cb_free(block_cb);
|
| 1678 | }
|
| 1679 |
|
| 1680 | return err;
|
| 1681 | }
|
| 1682 |
|
| 1683 | static void tcf_block_unbind(struct tcf_block *block,
|
| 1684 | struct flow_block_offload *bo)
|
| 1685 | {
|
| 1686 | struct flow_block_cb *block_cb, *next;
|
| 1687 |
|
| 1688 | lockdep_assert_held(&block->cb_lock);
|
| 1689 |
|
| 1690 | list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
|
| 1691 | tcf_block_playback_offloads(block, block_cb->cb,
|
| 1692 | block_cb->cb_priv, false,
|
| 1693 | tcf_block_offload_in_use(block),
|
| 1694 | NULL);
|
| 1695 | list_del(&block_cb->list);
|
| 1696 | flow_block_cb_free(block_cb);
|
| 1697 | if (!bo->unlocked_driver_cb)
|
| 1698 | block->lockeddevcnt--;
|
| 1699 | }
|
| 1700 | }
|
| 1701 |
|
| 1702 | static int tcf_block_setup(struct tcf_block *block,
|
| 1703 | struct flow_block_offload *bo)
|
| 1704 | {
|
| 1705 | int err;
|
| 1706 |
|
| 1707 | switch (bo->command) {
|
| 1708 | case FLOW_BLOCK_BIND:
|
| 1709 | err = tcf_block_bind(block, bo);
|
| 1710 | break;
|
| 1711 | case FLOW_BLOCK_UNBIND:
|
| 1712 | err = 0;
|
| 1713 | tcf_block_unbind(block, bo);
|
| 1714 | break;
|
| 1715 | default:
|
| 1716 | WARN_ON_ONCE(1);
|
| 1717 | err = -EOPNOTSUPP;
|
| 1718 | }
|
| 1719 |
|
| 1720 | return err;
|
| 1721 | }
|
| 1722 |
|
| 1723 | |
| 1724 | * to this qdisc, (optionally) tests for protocol and asks
|
| 1725 | * specific classifiers.
|
| 1726 |
|
| 1727 | static inline int __tcf_classify(struct sk_buff *skb,
|
| 1728 | const struct tcf_proto *tp,
|
| 1729 | const struct tcf_proto *orig_tp,
|
| 1730 | struct tcf_result *res,
|
| 1731 | bool compat_mode,
|
| 1732 | struct tcf_exts_miss_cookie_node *n,
|
| 1733 | int act_index,
|
| 1734 | u32 *last_executed_chain)
|
| 1735 | {
|
| 1736 | #ifdef CONFIG_NET_CLS_ACT
|
| 1737 | const int max_reclassify_loop = 16;
|
| 1738 | const struct tcf_proto *first_tp;
|
| 1739 | int limit = 0;
|
| 1740 |
|
| 1741 | reclassify:
|
| 1742 | #endif
|
| 1743 | for (; tp; tp = rcu_dereference_bh(tp->next)) {
|
| 1744 | __be16 protocol = skb_protocol(skb, false);
|
| 1745 | int err = 0;
|
| 1746 |
|
| 1747 | if (n) {
|
| 1748 | struct tcf_exts *exts;
|
| 1749 |
|
| 1750 | if (n->tp_prio != tp->prio)
|
| 1751 | continue;
|
| 1752 |
|
| 1753 | |
| 1754 | * of having hard refs and locks to them, so do a sanity
|
| 1755 | * check if any of tp,chain,exts was replaced by the
|
| 1756 | * time we got here with a cookie from hardware.
|
| 1757 |
|
| 1758 | if (unlikely(n->tp != tp || n->tp->chain != n->chain ||
|
| 1759 | !tp->ops->get_exts)) {
|
| 1760 | tcf_set_drop_reason(skb,
|
| 1761 | SKB_DROP_REASON_TC_COOKIE_ERROR);
|
| 1762 | return TC_ACT_SHOT;
|
| 1763 | }
|
| 1764 |
|
| 1765 | exts = tp->ops->get_exts(tp, n->handle);
|
| 1766 | if (unlikely(!exts || n->exts != exts)) {
|
| 1767 | tcf_set_drop_reason(skb,
|
| 1768 | SKB_DROP_REASON_TC_COOKIE_ERROR);
|
| 1769 | return TC_ACT_SHOT;
|
| 1770 | }
|
| 1771 |
|
| 1772 | n = NULL;
|
| 1773 | err = tcf_exts_exec_ex(skb, exts, act_index, res);
|
| 1774 | } else {
|
| 1775 | if (tp->protocol != protocol &&
|
| 1776 | tp->protocol != htons(ETH_P_ALL))
|
| 1777 | continue;
|
| 1778 |
|
| 1779 | err = tc_classify(skb, tp, res);
|
| 1780 | }
|
| 1781 | #ifdef CONFIG_NET_CLS_ACT
|
| 1782 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
|
| 1783 | first_tp = orig_tp;
|
| 1784 | *last_executed_chain = first_tp->chain->index;
|
| 1785 | goto reset;
|
| 1786 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
|
| 1787 | first_tp = res->goto_tp;
|
| 1788 | *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
|
| 1789 | goto reset;
|
| 1790 | }
|
| 1791 | #endif
|
| 1792 | if (err >= 0)
|
| 1793 | return err;
|
| 1794 | }
|
| 1795 |
|
| 1796 | if (unlikely(n)) {
|
| 1797 | tcf_set_drop_reason(skb,
|
| 1798 | SKB_DROP_REASON_TC_COOKIE_ERROR);
|
| 1799 | return TC_ACT_SHOT;
|
| 1800 | }
|
| 1801 |
|
| 1802 | return TC_ACT_UNSPEC;
|
| 1803 | #ifdef CONFIG_NET_CLS_ACT
|
| 1804 | reset:
|
| 1805 | if (unlikely(limit++ >= max_reclassify_loop)) {
|
| 1806 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
|
| 1807 | tp->chain->block->index,
|
| 1808 | tp->prio & 0xffff,
|
| 1809 | ntohs(tp->protocol));
|
| 1810 | tcf_set_drop_reason(skb,
|
| 1811 | SKB_DROP_REASON_TC_RECLASSIFY_LOOP);
|
| 1812 | return TC_ACT_SHOT;
|
| 1813 | }
|
| 1814 |
|
| 1815 | tp = first_tp;
|
| 1816 | goto reclassify;
|
| 1817 | #endif
|
| 1818 | }
|
| 1819 |
|
| 1820 | int tcf_classify(struct sk_buff *skb,
|
| 1821 | const struct tcf_block *block,
|
| 1822 | const struct tcf_proto *tp,
|
| 1823 | struct tcf_result *res, bool compat_mode)
|
| 1824 | {
|
| 1825 | #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
|
| 1826 | u32 last_executed_chain = 0;
|
| 1827 |
|
| 1828 | return __tcf_classify(skb, tp, tp, res, compat_mode, NULL, 0,
|
| 1829 | &last_executed_chain);
|
| 1830 | #else
|
| 1831 | u32 last_executed_chain = tp ? tp->chain->index : 0;
|
| 1832 | struct tcf_exts_miss_cookie_node *n = NULL;
|
| 1833 | const struct tcf_proto *orig_tp = tp;
|
| 1834 | struct tc_skb_ext *ext;
|
| 1835 | int act_index = 0;
|
| 1836 | int ret;
|
| 1837 |
|
| 1838 | if (block) {
|
| 1839 | ext = skb_ext_find(skb, TC_SKB_EXT);
|
| 1840 |
|
| 1841 | if (ext && (ext->chain || ext->act_miss)) {
|
| 1842 | struct tcf_chain *fchain;
|
| 1843 | u32 chain;
|
| 1844 |
|
| 1845 | if (ext->act_miss) {
|
| 1846 | n = tcf_exts_miss_cookie_lookup(ext->act_miss_cookie,
|
| 1847 | &act_index);
|
| 1848 | if (!n) {
|
| 1849 | tcf_set_drop_reason(skb,
|
| 1850 | SKB_DROP_REASON_TC_COOKIE_ERROR);
|
| 1851 | return TC_ACT_SHOT;
|
| 1852 | }
|
| 1853 |
|
| 1854 | chain = n->chain_index;
|
| 1855 | } else {
|
| 1856 | chain = ext->chain;
|
| 1857 | }
|
| 1858 |
|
| 1859 | fchain = tcf_chain_lookup_rcu(block, chain);
|
| 1860 | if (!fchain) {
|
| 1861 | tcf_set_drop_reason(skb,
|
| 1862 | SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
|
| 1863 |
|
| 1864 | return TC_ACT_SHOT;
|
| 1865 | }
|
| 1866 |
|
| 1867 |
|
| 1868 | skb_ext_del(skb, TC_SKB_EXT);
|
| 1869 |
|
| 1870 | tp = rcu_dereference_bh(fchain->filter_chain);
|
| 1871 | last_executed_chain = fchain->index;
|
| 1872 | }
|
| 1873 | }
|
| 1874 |
|
| 1875 | ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode, n, act_index,
|
| 1876 | &last_executed_chain);
|
| 1877 |
|
| 1878 | if (tc_skb_ext_tc_enabled()) {
|
| 1879 |
|
| 1880 | if (ret == TC_ACT_UNSPEC && last_executed_chain) {
|
| 1881 | struct tc_skb_cb *cb = tc_skb_cb(skb);
|
| 1882 |
|
| 1883 | ext = tc_skb_ext_alloc(skb);
|
| 1884 | if (!ext) {
|
| 1885 | tcf_set_drop_reason(skb, SKB_DROP_REASON_NOMEM);
|
| 1886 | return TC_ACT_SHOT;
|
| 1887 | }
|
| 1888 | ext->chain = last_executed_chain;
|
| 1889 | ext->mru = cb->mru;
|
| 1890 | ext->post_ct = qdisc_skb_cb(skb)->post_ct;
|
| 1891 | ext->post_ct_snat = qdisc_skb_cb(skb)->post_ct_snat;
|
| 1892 | ext->post_ct_dnat = qdisc_skb_cb(skb)->post_ct_dnat;
|
| 1893 | ext->zone = cb->zone;
|
| 1894 | }
|
| 1895 | }
|
| 1896 |
|
| 1897 | return ret;
|
| 1898 | #endif
|
| 1899 | }
|
| 1900 | EXPORT_SYMBOL(tcf_classify);
|
| 1901 |
|
| 1902 | struct tcf_chain_info {
|
| 1903 | struct tcf_proto __rcu **pprev;
|
| 1904 | struct tcf_proto __rcu *next;
|
| 1905 | };
|
| 1906 |
|
| 1907 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
|
| 1908 | struct tcf_chain_info *chain_info)
|
| 1909 | {
|
| 1910 | return tcf_chain_dereference(*chain_info->pprev, chain);
|
| 1911 | }
|
| 1912 |
|
| 1913 | static int tcf_chain_tp_insert(struct tcf_chain *chain,
|
| 1914 | struct tcf_chain_info *chain_info,
|
| 1915 | struct tcf_proto *tp)
|
| 1916 | {
|
| 1917 | if (chain->flushing)
|
| 1918 | return -EAGAIN;
|
| 1919 |
|
| 1920 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
|
| 1921 | if (*chain_info->pprev == chain->filter_chain)
|
| 1922 | tcf_chain0_head_change(chain, tp);
|
| 1923 | tcf_proto_get(tp);
|
| 1924 | rcu_assign_pointer(*chain_info->pprev, tp);
|
| 1925 |
|
| 1926 | return 0;
|
| 1927 | }
|
| 1928 |
|
| 1929 | static void tcf_chain_tp_remove(struct tcf_chain *chain,
|
| 1930 | struct tcf_chain_info *chain_info,
|
| 1931 | struct tcf_proto *tp)
|
| 1932 | {
|
| 1933 | struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
|
| 1934 |
|
| 1935 | tcf_proto_mark_delete(tp);
|
| 1936 | if (tp == chain->filter_chain)
|
| 1937 | tcf_chain0_head_change(chain, next);
|
| 1938 | RCU_INIT_POINTER(*chain_info->pprev, next);
|
| 1939 | }
|
| 1940 |
|
| 1941 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
|
| 1942 | struct tcf_chain_info *chain_info,
|
| 1943 | u32 protocol, u32 prio,
|
| 1944 | bool prio_allocate,
|
| 1945 | struct netlink_ext_ack *extack);
|
| 1946 |
|
| 1947 | |
| 1948 | * If proto with specified priority already exists, free new proto
|
| 1949 | * and return existing one.
|
| 1950 |
|
| 1951 |
|
| 1952 | static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
|
| 1953 | struct tcf_proto *tp_new,
|
| 1954 | u32 protocol, u32 prio,
|
| 1955 | bool rtnl_held)
|
| 1956 | {
|
| 1957 | struct tcf_chain_info chain_info;
|
| 1958 | struct tcf_proto *tp;
|
| 1959 | int err = 0;
|
| 1960 |
|
| 1961 | mutex_lock(&chain->filter_chain_lock);
|
| 1962 |
|
| 1963 | if (tcf_proto_exists_destroying(chain, tp_new)) {
|
| 1964 | mutex_unlock(&chain->filter_chain_lock);
|
| 1965 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
|
| 1966 | return ERR_PTR(-EAGAIN);
|
| 1967 | }
|
| 1968 |
|
| 1969 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, prio, false, NULL);
|
| 1970 | if (!tp)
|
| 1971 | err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
|
| 1972 | mutex_unlock(&chain->filter_chain_lock);
|
| 1973 |
|
| 1974 | if (tp) {
|
| 1975 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
|
| 1976 | tp_new = tp;
|
| 1977 | } else if (err) {
|
| 1978 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
|
| 1979 | tp_new = ERR_PTR(err);
|
| 1980 | }
|
| 1981 |
|
| 1982 | return tp_new;
|
| 1983 | }
|
| 1984 |
|
| 1985 | static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
|
| 1986 | struct tcf_proto *tp, bool rtnl_held,
|
| 1987 | struct netlink_ext_ack *extack)
|
| 1988 | {
|
| 1989 | struct tcf_chain_info chain_info;
|
| 1990 | struct tcf_proto *tp_iter;
|
| 1991 | struct tcf_proto **pprev;
|
| 1992 | struct tcf_proto *next;
|
| 1993 |
|
| 1994 | mutex_lock(&chain->filter_chain_lock);
|
| 1995 |
|
| 1996 |
|
| 1997 | for (pprev = &chain->filter_chain;
|
| 1998 | (tp_iter = tcf_chain_dereference(*pprev, chain));
|
| 1999 | pprev = &tp_iter->next) {
|
| 2000 | if (tp_iter == tp) {
|
| 2001 | chain_info.pprev = pprev;
|
| 2002 | chain_info.next = tp_iter->next;
|
| 2003 | WARN_ON(tp_iter->deleting);
|
| 2004 | break;
|
| 2005 | }
|
| 2006 | }
|
| 2007 | |
| 2008 | * concurrently.
|
| 2009 | * Mark tp for deletion if it is empty.
|
| 2010 |
|
| 2011 | if (!tp_iter || !tcf_proto_check_delete(tp)) {
|
| 2012 | mutex_unlock(&chain->filter_chain_lock);
|
| 2013 | return;
|
| 2014 | }
|
| 2015 |
|
| 2016 | tcf_proto_signal_destroying(chain, tp);
|
| 2017 | next = tcf_chain_dereference(chain_info.next, chain);
|
| 2018 | if (tp == chain->filter_chain)
|
| 2019 | tcf_chain0_head_change(chain, next);
|
| 2020 | RCU_INIT_POINTER(*chain_info.pprev, next);
|
| 2021 | mutex_unlock(&chain->filter_chain_lock);
|
| 2022 |
|
| 2023 | tcf_proto_put(tp, rtnl_held, extack);
|
| 2024 | }
|
| 2025 |
|
| 2026 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
|
| 2027 | struct tcf_chain_info *chain_info,
|
| 2028 | u32 protocol, u32 prio,
|
| 2029 | bool prio_allocate,
|
| 2030 | struct netlink_ext_ack *extack)
|
| 2031 | {
|
| 2032 | struct tcf_proto **pprev;
|
| 2033 | struct tcf_proto *tp;
|
| 2034 |
|
| 2035 |
|
| 2036 | for (pprev = &chain->filter_chain;
|
| 2037 | (tp = tcf_chain_dereference(*pprev, chain));
|
| 2038 | pprev = &tp->next) {
|
| 2039 | if (tp->prio >= prio) {
|
| 2040 | if (tp->prio == prio) {
|
| 2041 | if (prio_allocate) {
|
| 2042 | NL_SET_ERR_MSG(extack, "Lowest ID from auto-alloc range already in use");
|
| 2043 | return ERR_PTR(-ENOSPC);
|
| 2044 | }
|
| 2045 | if (tp->protocol != protocol && protocol) {
|
| 2046 | NL_SET_ERR_MSG(extack, "Protocol mismatch for filter with specified priority");
|
| 2047 | return ERR_PTR(-EINVAL);
|
| 2048 | }
|
| 2049 | } else {
|
| 2050 | tp = NULL;
|
| 2051 | }
|
| 2052 | break;
|
| 2053 | }
|
| 2054 | }
|
| 2055 | chain_info->pprev = pprev;
|
| 2056 | if (tp) {
|
| 2057 | chain_info->next = tp->next;
|
| 2058 | tcf_proto_get(tp);
|
| 2059 | } else {
|
| 2060 | chain_info->next = NULL;
|
| 2061 | }
|
| 2062 | return tp;
|
| 2063 | }
|
| 2064 |
|
| 2065 | static int tcf_fill_node(struct net *net, struct sk_buff *skb,
|
| 2066 | struct tcf_proto *tp, struct tcf_block *block,
|
| 2067 | struct Qdisc *q, u32 parent, void *fh,
|
| 2068 | u32 portid, u32 seq, u16 flags, int event,
|
| 2069 | bool terse_dump, bool rtnl_held,
|
| 2070 | struct netlink_ext_ack *extack)
|
| 2071 | {
|
| 2072 | struct tcmsg *tcm;
|
| 2073 | struct nlmsghdr *nlh;
|
| 2074 | unsigned char *b = skb_tail_pointer(skb);
|
| 2075 | int ret = -EMSGSIZE;
|
| 2076 |
|
| 2077 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
|
| 2078 | if (!nlh)
|
| 2079 | goto out_nlmsg_trim;
|
| 2080 | tcm = nlmsg_data(nlh);
|
| 2081 | tcm->tcm_family = AF_UNSPEC;
|
| 2082 | tcm->tcm__pad1 = 0;
|
| 2083 | tcm->tcm__pad2 = 0;
|
| 2084 | if (q) {
|
| 2085 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
|
| 2086 | tcm->tcm_parent = parent;
|
| 2087 | } else {
|
| 2088 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
|
| 2089 | tcm->tcm_block_index = block->index;
|
| 2090 | }
|
| 2091 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
|
| 2092 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
|
| 2093 | goto nla_put_failure;
|
| 2094 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
|
| 2095 | goto nla_put_failure;
|
| 2096 | if (!fh) {
|
| 2097 | tcm->tcm_handle = 0;
|
| 2098 | } else if (terse_dump) {
|
| 2099 | if (tp->ops->terse_dump) {
|
| 2100 | if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
|
| 2101 | rtnl_held) < 0)
|
| 2102 | goto nla_put_failure;
|
| 2103 | } else {
|
| 2104 | goto cls_op_not_supp;
|
| 2105 | }
|
| 2106 | } else {
|
| 2107 | if (tp->ops->dump &&
|
| 2108 | tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
|
| 2109 | goto nla_put_failure;
|
| 2110 | }
|
| 2111 |
|
| 2112 | if (extack && extack->_msg &&
|
| 2113 | nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
|
| 2114 | goto nla_put_failure;
|
| 2115 |
|
| 2116 | nlh->nlmsg_len = skb_tail_pointer(skb) - b;
|
| 2117 |
|
| 2118 | return skb->len;
|
| 2119 |
|
| 2120 | cls_op_not_supp:
|
| 2121 | ret = -EOPNOTSUPP;
|
| 2122 | out_nlmsg_trim:
|
| 2123 | nla_put_failure:
|
| 2124 | nlmsg_trim(skb, b);
|
| 2125 | return ret;
|
| 2126 | }
|
| 2127 |
|
| 2128 | static struct sk_buff *tfilter_notify_prep(struct net *net,
|
| 2129 | struct sk_buff *oskb,
|
| 2130 | struct nlmsghdr *n,
|
| 2131 | struct tcf_proto *tp,
|
| 2132 | struct tcf_block *block,
|
| 2133 | struct Qdisc *q, u32 parent,
|
| 2134 | void *fh, int event,
|
| 2135 | u32 portid, bool rtnl_held,
|
| 2136 | struct netlink_ext_ack *extack)
|
| 2137 | {
|
| 2138 | unsigned int size = oskb ? max(NLMSG_GOODSIZE, oskb->len) : NLMSG_GOODSIZE;
|
| 2139 | struct sk_buff *skb;
|
| 2140 | int ret;
|
| 2141 |
|
| 2142 | retry:
|
| 2143 | skb = alloc_skb(size, GFP_KERNEL);
|
| 2144 | if (!skb)
|
| 2145 | return ERR_PTR(-ENOBUFS);
|
| 2146 |
|
| 2147 | ret = tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
|
| 2148 | n->nlmsg_seq, n->nlmsg_flags, event, false,
|
| 2149 | rtnl_held, extack);
|
| 2150 | if (ret <= 0) {
|
| 2151 | kfree_skb(skb);
|
| 2152 | if (ret == -EMSGSIZE) {
|
| 2153 | size += NLMSG_GOODSIZE;
|
| 2154 | goto retry;
|
| 2155 | }
|
| 2156 | return ERR_PTR(-EINVAL);
|
| 2157 | }
|
| 2158 | return skb;
|
| 2159 | }
|
| 2160 |
|
| 2161 | static int tfilter_notify(struct net *net, struct sk_buff *oskb,
|
| 2162 | struct nlmsghdr *n, struct tcf_proto *tp,
|
| 2163 | struct tcf_block *block, struct Qdisc *q,
|
| 2164 | u32 parent, void *fh, int event, bool unicast,
|
| 2165 | bool rtnl_held, struct netlink_ext_ack *extack)
|
| 2166 | {
|
| 2167 | struct sk_buff *skb;
|
| 2168 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
|
| 2169 | int err = 0;
|
| 2170 |
|
| 2171 | if (!unicast && !rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
|
| 2172 | return 0;
|
| 2173 |
|
| 2174 | skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh, event,
|
| 2175 | portid, rtnl_held, extack);
|
| 2176 | if (IS_ERR(skb))
|
| 2177 | return PTR_ERR(skb);
|
| 2178 |
|
| 2179 | if (unicast)
|
| 2180 | err = rtnl_unicast(skb, net, portid);
|
| 2181 | else
|
| 2182 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
|
| 2183 | n->nlmsg_flags & NLM_F_ECHO);
|
| 2184 | return err;
|
| 2185 | }
|
| 2186 |
|
| 2187 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
|
| 2188 | struct nlmsghdr *n, struct tcf_proto *tp,
|
| 2189 | struct tcf_block *block, struct Qdisc *q,
|
| 2190 | u32 parent, void *fh, bool *last, bool rtnl_held,
|
| 2191 | struct netlink_ext_ack *extack)
|
| 2192 | {
|
| 2193 | struct sk_buff *skb;
|
| 2194 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
|
| 2195 | int err;
|
| 2196 |
|
| 2197 | if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
|
| 2198 | return tp->ops->delete(tp, fh, last, rtnl_held, extack);
|
| 2199 |
|
| 2200 | skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh,
|
| 2201 | RTM_DELTFILTER, portid, rtnl_held, extack);
|
| 2202 | if (IS_ERR(skb)) {
|
| 2203 | NL_SET_ERR_MSG(extack, "Failed to build del event notification");
|
| 2204 | return PTR_ERR(skb);
|
| 2205 | }
|
| 2206 |
|
| 2207 | err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
|
| 2208 | if (err) {
|
| 2209 | kfree_skb(skb);
|
| 2210 | return err;
|
| 2211 | }
|
| 2212 |
|
| 2213 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
|
| 2214 | n->nlmsg_flags & NLM_F_ECHO);
|
| 2215 | if (err < 0)
|
| 2216 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
|
| 2217 |
|
| 2218 | return err;
|
| 2219 | }
|
| 2220 |
|
| 2221 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
|
| 2222 | struct tcf_block *block, struct Qdisc *q,
|
| 2223 | u32 parent, struct nlmsghdr *n,
|
| 2224 | struct tcf_chain *chain, int event,
|
| 2225 | struct netlink_ext_ack *extack)
|
| 2226 | {
|
| 2227 | struct tcf_proto *tp;
|
| 2228 |
|
| 2229 | for (tp = tcf_get_next_proto(chain, NULL);
|
| 2230 | tp; tp = tcf_get_next_proto(chain, tp))
|
| 2231 | tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
|
| 2232 | event, false, true, extack);
|
| 2233 | }
|
| 2234 |
|
| 2235 | static void tfilter_put(struct tcf_proto *tp, void *fh)
|
| 2236 | {
|
| 2237 | if (tp->ops->put && fh)
|
| 2238 | tp->ops->put(tp, fh);
|
| 2239 | }
|
| 2240 |
|
| 2241 | static bool is_qdisc_ingress(__u32 classid)
|
| 2242 | {
|
| 2243 | return (TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS));
|
| 2244 | }
|
| 2245 |
|
| 2246 | static bool is_ingress_or_clsact(struct tcf_block *block, struct Qdisc *q)
|
| 2247 | {
|
| 2248 | return tcf_block_shared(block) || (q && !!(q->flags & TCQ_F_INGRESS));
|
| 2249 | }
|
| 2250 |
|
| 2251 | enum tcf_tp_insert_state {
|
| 2252 | TP_NOT_CREATED = 0,
|
| 2253 | TP_CREATED,
|
| 2254 | TP_NOT_OWNED,
|
| 2255 | };
|
| 2256 |
|
| 2257 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
|
| 2258 | struct netlink_ext_ack *extack)
|
| 2259 | {
|
| 2260 | struct net *net = sock_net(skb->sk);
|
| 2261 | struct nlattr *tca[TCA_MAX + 1];
|
| 2262 | char name[IFNAMSIZ];
|
| 2263 | struct tcmsg *t;
|
| 2264 | u32 protocol;
|
| 2265 | u32 prio;
|
| 2266 | bool prio_allocate;
|
| 2267 | u32 parent;
|
| 2268 | u32 chain_index;
|
| 2269 | struct Qdisc *q;
|
| 2270 | struct tcf_chain_info chain_info;
|
| 2271 | struct tcf_chain *chain;
|
| 2272 | struct tcf_block *block;
|
| 2273 | struct tcf_proto *tp;
|
| 2274 | unsigned long cl;
|
| 2275 | void *fh;
|
| 2276 | int err;
|
| 2277 | enum tcf_tp_insert_state tp_state;
|
| 2278 | bool rtnl_held = false;
|
| 2279 | u32 flags;
|
| 2280 |
|
| 2281 | replay:
|
| 2282 | tp_state = TP_NOT_CREATED;
|
| 2283 |
|
| 2284 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
|
| 2285 | rtm_tca_policy, extack);
|
| 2286 | if (err < 0)
|
| 2287 | return err;
|
| 2288 |
|
| 2289 | t = nlmsg_data(n);
|
| 2290 | protocol = TC_H_MIN(t->tcm_info);
|
| 2291 | prio = TC_H_MAJ(t->tcm_info);
|
| 2292 | prio_allocate = false;
|
| 2293 | parent = t->tcm_parent;
|
| 2294 | tp = NULL;
|
| 2295 | cl = 0;
|
| 2296 | block = NULL;
|
| 2297 | q = NULL;
|
| 2298 | chain = NULL;
|
| 2299 | flags = 0;
|
| 2300 |
|
| 2301 | if (prio == 0) {
|
| 2302 | |
| 2303 | * we allocate one.
|
| 2304 |
|
| 2305 | if (n->nlmsg_flags & NLM_F_CREATE) {
|
| 2306 | prio = TC_H_MAKE(0x80000000U, 0U);
|
| 2307 | prio_allocate = true;
|
| 2308 | } else {
|
| 2309 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
|
| 2310 | return -ENOENT;
|
| 2311 | }
|
| 2312 | }
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
|
| 2317 | if (err)
|
| 2318 | return err;
|
| 2319 |
|
| 2320 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
|
| 2321 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
|
| 2322 | err = -EINVAL;
|
| 2323 | goto errout;
|
| 2324 | }
|
| 2325 |
|
| 2326 | |
| 2327 | * block is shared (no qdisc found), qdisc is not unlocked, classifier
|
| 2328 | * type is not specified, classifier is not unlocked.
|
| 2329 |
|
| 2330 | if (rtnl_held ||
|
| 2331 | (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
|
| 2332 | !tcf_proto_is_unlocked(name)) {
|
| 2333 | rtnl_held = true;
|
| 2334 | rtnl_lock();
|
| 2335 | }
|
| 2336 |
|
| 2337 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
|
| 2338 | if (err)
|
| 2339 | goto errout;
|
| 2340 |
|
| 2341 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
|
| 2342 | extack);
|
| 2343 | if (IS_ERR(block)) {
|
| 2344 | err = PTR_ERR(block);
|
| 2345 | goto errout;
|
| 2346 | }
|
| 2347 | block->classid = parent;
|
| 2348 |
|
| 2349 | chain_index = nla_get_u32_default(tca[TCA_CHAIN], 0);
|
| 2350 | if (chain_index > TC_ACT_EXT_VAL_MASK) {
|
| 2351 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
|
| 2352 | err = -EINVAL;
|
| 2353 | goto errout;
|
| 2354 | }
|
| 2355 | chain = tcf_chain_get(block, chain_index, true);
|
| 2356 | if (!chain) {
|
| 2357 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
|
| 2358 | err = -ENOMEM;
|
| 2359 | goto errout;
|
| 2360 | }
|
| 2361 |
|
| 2362 | mutex_lock(&chain->filter_chain_lock);
|
| 2363 | tp = tcf_chain_tp_find(chain, &chain_info, protocol,
|
| 2364 | prio, prio_allocate, extack);
|
| 2365 | if (IS_ERR(tp)) {
|
| 2366 | err = PTR_ERR(tp);
|
| 2367 | goto errout_locked;
|
| 2368 | }
|
| 2369 |
|
| 2370 | if (tp == NULL) {
|
| 2371 | struct tcf_proto *tp_new = NULL;
|
| 2372 |
|
| 2373 | if (chain->flushing) {
|
| 2374 | err = -EAGAIN;
|
| 2375 | goto errout_locked;
|
| 2376 | }
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 | if (tca[TCA_KIND] == NULL || !protocol) {
|
| 2381 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
|
| 2382 | err = -EINVAL;
|
| 2383 | goto errout_locked;
|
| 2384 | }
|
| 2385 |
|
| 2386 | if (!(n->nlmsg_flags & NLM_F_CREATE)) {
|
| 2387 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
|
| 2388 | err = -ENOENT;
|
| 2389 | goto errout_locked;
|
| 2390 | }
|
| 2391 |
|
| 2392 | if (prio_allocate)
|
| 2393 | prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
|
| 2394 | &chain_info));
|
| 2395 |
|
| 2396 | mutex_unlock(&chain->filter_chain_lock);
|
| 2397 | tp_new = tcf_proto_create(name, protocol, prio, chain,
|
| 2398 | rtnl_held, extack);
|
| 2399 | if (IS_ERR(tp_new)) {
|
| 2400 | err = PTR_ERR(tp_new);
|
| 2401 | goto errout_tp;
|
| 2402 | }
|
| 2403 |
|
| 2404 | tp_state = TP_CREATED;
|
| 2405 | tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
|
| 2406 | rtnl_held);
|
| 2407 | if (IS_ERR(tp)) {
|
| 2408 | err = PTR_ERR(tp);
|
| 2409 | goto errout_tp;
|
| 2410 | }
|
| 2411 | if (tp != tp_new)
|
| 2412 | tp_state = TP_NOT_OWNED;
|
| 2413 | } else {
|
| 2414 | mutex_unlock(&chain->filter_chain_lock);
|
| 2415 | }
|
| 2416 |
|
| 2417 | if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
|
| 2418 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
|
| 2419 | err = -EINVAL;
|
| 2420 | goto errout;
|
| 2421 | }
|
| 2422 |
|
| 2423 | fh = tp->ops->get(tp, t->tcm_handle);
|
| 2424 |
|
| 2425 | if (!fh) {
|
| 2426 | if (!(n->nlmsg_flags & NLM_F_CREATE)) {
|
| 2427 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
|
| 2428 | err = -ENOENT;
|
| 2429 | goto errout;
|
| 2430 | }
|
| 2431 | } else if (n->nlmsg_flags & NLM_F_EXCL) {
|
| 2432 | tfilter_put(tp, fh);
|
| 2433 | NL_SET_ERR_MSG(extack, "Filter already exists");
|
| 2434 | err = -EEXIST;
|
| 2435 | goto errout;
|
| 2436 | }
|
| 2437 |
|
| 2438 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
|
| 2439 | tfilter_put(tp, fh);
|
| 2440 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
|
| 2441 | err = -EINVAL;
|
| 2442 | goto errout;
|
| 2443 | }
|
| 2444 |
|
| 2445 | if (!(n->nlmsg_flags & NLM_F_CREATE))
|
| 2446 | flags |= TCA_ACT_FLAGS_REPLACE;
|
| 2447 | if (!rtnl_held)
|
| 2448 | flags |= TCA_ACT_FLAGS_NO_RTNL;
|
| 2449 | if (is_qdisc_ingress(parent))
|
| 2450 | flags |= TCA_ACT_FLAGS_AT_INGRESS;
|
| 2451 | if (is_ingress_or_clsact(block, q))
|
| 2452 | flags |= TCA_ACT_FLAGS_AT_INGRESS_OR_CLSACT;
|
| 2453 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
|
| 2454 | flags, extack);
|
| 2455 | if (err == 0) {
|
| 2456 | tfilter_notify(net, skb, n, tp, block, q, parent, fh,
|
| 2457 | RTM_NEWTFILTER, false, rtnl_held, extack);
|
| 2458 | tfilter_put(tp, fh);
|
| 2459 | tcf_proto_count_usesw(tp, true);
|
| 2460 |
|
| 2461 | if (q)
|
| 2462 | q->flags &= ~TCQ_F_CAN_BYPASS;
|
| 2463 | }
|
| 2464 |
|
| 2465 | errout:
|
| 2466 | if (err && !IS_ERR_OR_NULL(tp) &&
|
| 2467 | (tp_state == TP_CREATED || tp->ops->delete_empty))
|
| 2468 | |
| 2469 | * The request is dropping its reference to tp. If it was
|
| 2470 | * the last user (the idr is empty now), reclaim the proto.
|
| 2471 | * A tp this request created is reclaimed unconditionally:
|
| 2472 | * it is the only owner, so marking it for deletion is
|
| 2473 | * safe. Otherwise only classifiers with a delete_empty
|
| 2474 | * callback are reclaimed -- the callback admits an empty
|
| 2475 | * proto only, so a live shared proto is never unlinked.
|
| 2476 | * Classifiers without one (deleting is set
|
| 2477 | * unconditionally) are rtnl-serialized, so the raced
|
| 2478 | * window this guard closes cannot arise for them.
|
| 2479 |
|
| 2480 | tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
|
| 2481 | errout_tp:
|
| 2482 | if (chain) {
|
| 2483 | if (tp && !IS_ERR(tp))
|
| 2484 | tcf_proto_put(tp, rtnl_held, NULL);
|
| 2485 | if (tp_state == TP_NOT_CREATED)
|
| 2486 | tcf_chain_put(chain);
|
| 2487 | }
|
| 2488 | tcf_block_release(q, block, rtnl_held);
|
| 2489 |
|
| 2490 | if (rtnl_held)
|
| 2491 | rtnl_unlock();
|
| 2492 |
|
| 2493 | if (err == -EAGAIN) {
|
| 2494 | |
| 2495 | * of target chain.
|
| 2496 |
|
| 2497 | rtnl_held = true;
|
| 2498 |
|
| 2499 | goto replay;
|
| 2500 | }
|
| 2501 | return err;
|
| 2502 |
|
| 2503 | errout_locked:
|
| 2504 | mutex_unlock(&chain->filter_chain_lock);
|
| 2505 | goto errout;
|
| 2506 | }
|
| 2507 |
|
| 2508 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
|
| 2509 | struct netlink_ext_ack *extack)
|
| 2510 | {
|
| 2511 | struct net *net = sock_net(skb->sk);
|
| 2512 | struct nlattr *tca[TCA_MAX + 1];
|
| 2513 | char name[IFNAMSIZ];
|
| 2514 | struct tcmsg *t;
|
| 2515 | u32 protocol;
|
| 2516 | u32 prio;
|
| 2517 | u32 parent;
|
| 2518 | u32 chain_index;
|
| 2519 | struct Qdisc *q = NULL;
|
| 2520 | struct tcf_chain_info chain_info;
|
| 2521 | struct tcf_chain *chain = NULL;
|
| 2522 | struct tcf_block *block = NULL;
|
| 2523 | struct tcf_proto *tp = NULL;
|
| 2524 | unsigned long cl = 0;
|
| 2525 | void *fh = NULL;
|
| 2526 | int err;
|
| 2527 | bool rtnl_held = false;
|
| 2528 |
|
| 2529 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
|
| 2530 | rtm_tca_policy, extack);
|
| 2531 | if (err < 0)
|
| 2532 | return err;
|
| 2533 |
|
| 2534 | t = nlmsg_data(n);
|
| 2535 | protocol = TC_H_MIN(t->tcm_info);
|
| 2536 | prio = TC_H_MAJ(t->tcm_info);
|
| 2537 | parent = t->tcm_parent;
|
| 2538 |
|
| 2539 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
|
| 2540 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
|
| 2541 | return -ENOENT;
|
| 2542 | }
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
|
| 2547 | if (err)
|
| 2548 | return err;
|
| 2549 |
|
| 2550 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
|
| 2551 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
|
| 2552 | err = -EINVAL;
|
| 2553 | goto errout;
|
| 2554 | }
|
| 2555 | |
| 2556 | * found), qdisc is not unlocked, classifier type is not specified,
|
| 2557 | * classifier is not unlocked.
|
| 2558 |
|
| 2559 | if (!prio ||
|
| 2560 | (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
|
| 2561 | !tcf_proto_is_unlocked(name)) {
|
| 2562 | rtnl_held = true;
|
| 2563 | rtnl_lock();
|
| 2564 | }
|
| 2565 |
|
| 2566 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
|
| 2567 | if (err)
|
| 2568 | goto errout;
|
| 2569 |
|
| 2570 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
|
| 2571 | extack);
|
| 2572 | if (IS_ERR(block)) {
|
| 2573 | err = PTR_ERR(block);
|
| 2574 | goto errout;
|
| 2575 | }
|
| 2576 |
|
| 2577 | chain_index = nla_get_u32_default(tca[TCA_CHAIN], 0);
|
| 2578 | if (chain_index > TC_ACT_EXT_VAL_MASK) {
|
| 2579 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
|
| 2580 | err = -EINVAL;
|
| 2581 | goto errout;
|
| 2582 | }
|
| 2583 | chain = tcf_chain_get(block, chain_index, false);
|
| 2584 | if (!chain) {
|
| 2585 | |
| 2586 | * so just return success.
|
| 2587 |
|
| 2588 | if (prio == 0) {
|
| 2589 | err = 0;
|
| 2590 | goto errout;
|
| 2591 | }
|
| 2592 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
|
| 2593 | err = -ENOENT;
|
| 2594 | goto errout;
|
| 2595 | }
|
| 2596 |
|
| 2597 | if (prio == 0) {
|
| 2598 | tfilter_notify_chain(net, skb, block, q, parent, n,
|
| 2599 | chain, RTM_DELTFILTER, extack);
|
| 2600 | tcf_chain_flush(chain, rtnl_held);
|
| 2601 | err = 0;
|
| 2602 | goto errout;
|
| 2603 | }
|
| 2604 |
|
| 2605 | mutex_lock(&chain->filter_chain_lock);
|
| 2606 | tp = tcf_chain_tp_find(chain, &chain_info, protocol,
|
| 2607 | prio, false, extack);
|
| 2608 | if (!tp) {
|
| 2609 | err = -ENOENT;
|
| 2610 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
|
| 2611 | goto errout_locked;
|
| 2612 | } else if (IS_ERR(tp)) {
|
| 2613 | err = PTR_ERR(tp);
|
| 2614 | goto errout_locked;
|
| 2615 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
|
| 2616 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
|
| 2617 | err = -EINVAL;
|
| 2618 | goto errout_locked;
|
| 2619 | } else if (t->tcm_handle == 0) {
|
| 2620 | tcf_proto_signal_destroying(chain, tp);
|
| 2621 | tcf_chain_tp_remove(chain, &chain_info, tp);
|
| 2622 | mutex_unlock(&chain->filter_chain_lock);
|
| 2623 |
|
| 2624 | tcf_proto_put(tp, rtnl_held, NULL);
|
| 2625 | tfilter_notify(net, skb, n, tp, block, q, parent, fh,
|
| 2626 | RTM_DELTFILTER, false, rtnl_held, extack);
|
| 2627 | err = 0;
|
| 2628 | goto errout;
|
| 2629 | }
|
| 2630 | mutex_unlock(&chain->filter_chain_lock);
|
| 2631 |
|
| 2632 | fh = tp->ops->get(tp, t->tcm_handle);
|
| 2633 |
|
| 2634 | if (!fh) {
|
| 2635 | NL_SET_ERR_MSG(extack, "Specified filter handle not found");
|
| 2636 | err = -ENOENT;
|
| 2637 | } else {
|
| 2638 | bool last;
|
| 2639 |
|
| 2640 | err = tfilter_del_notify(net, skb, n, tp, block, q, parent, fh,
|
| 2641 | &last, rtnl_held, extack);
|
| 2642 |
|
| 2643 | if (err)
|
| 2644 | goto errout;
|
| 2645 | if (last)
|
| 2646 | tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
|
| 2647 | }
|
| 2648 |
|
| 2649 | errout:
|
| 2650 | if (chain) {
|
| 2651 | if (tp && !IS_ERR(tp))
|
| 2652 | tcf_proto_put(tp, rtnl_held, NULL);
|
| 2653 | tcf_chain_put(chain);
|
| 2654 | }
|
| 2655 | tcf_block_release(q, block, rtnl_held);
|
| 2656 |
|
| 2657 | if (rtnl_held)
|
| 2658 | rtnl_unlock();
|
| 2659 |
|
| 2660 | return err;
|
| 2661 |
|
| 2662 | errout_locked:
|
| 2663 | mutex_unlock(&chain->filter_chain_lock);
|
| 2664 | goto errout;
|
| 2665 | }
|
| 2666 |
|
| 2667 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
|
| 2668 | struct netlink_ext_ack *extack)
|
| 2669 | {
|
| 2670 | struct net *net = sock_net(skb->sk);
|
| 2671 | struct nlattr *tca[TCA_MAX + 1];
|
| 2672 | char name[IFNAMSIZ];
|
| 2673 | struct tcmsg *t;
|
| 2674 | u32 protocol;
|
| 2675 | u32 prio;
|
| 2676 | u32 parent;
|
| 2677 | u32 chain_index;
|
| 2678 | struct Qdisc *q = NULL;
|
| 2679 | struct tcf_chain_info chain_info;
|
| 2680 | struct tcf_chain *chain = NULL;
|
| 2681 | struct tcf_block *block = NULL;
|
| 2682 | struct tcf_proto *tp = NULL;
|
| 2683 | unsigned long cl = 0;
|
| 2684 | void *fh = NULL;
|
| 2685 | int err;
|
| 2686 | bool rtnl_held = false;
|
| 2687 |
|
| 2688 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
|
| 2689 | rtm_tca_policy, extack);
|
| 2690 | if (err < 0)
|
| 2691 | return err;
|
| 2692 |
|
| 2693 | t = nlmsg_data(n);
|
| 2694 | protocol = TC_H_MIN(t->tcm_info);
|
| 2695 | prio = TC_H_MAJ(t->tcm_info);
|
| 2696 | parent = t->tcm_parent;
|
| 2697 |
|
| 2698 | if (prio == 0) {
|
| 2699 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
|
| 2700 | return -ENOENT;
|
| 2701 | }
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
|
| 2706 | if (err)
|
| 2707 | return err;
|
| 2708 |
|
| 2709 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
|
| 2710 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
|
| 2711 | err = -EINVAL;
|
| 2712 | goto errout;
|
| 2713 | }
|
| 2714 | |
| 2715 | * unlocked, classifier type is not specified, classifier is not
|
| 2716 | * unlocked.
|
| 2717 |
|
| 2718 | if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
|
| 2719 | !tcf_proto_is_unlocked(name)) {
|
| 2720 | rtnl_held = true;
|
| 2721 | rtnl_lock();
|
| 2722 | }
|
| 2723 |
|
| 2724 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
|
| 2725 | if (err)
|
| 2726 | goto errout;
|
| 2727 |
|
| 2728 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
|
| 2729 | extack);
|
| 2730 | if (IS_ERR(block)) {
|
| 2731 | err = PTR_ERR(block);
|
| 2732 | goto errout;
|
| 2733 | }
|
| 2734 |
|
| 2735 | chain_index = nla_get_u32_default(tca[TCA_CHAIN], 0);
|
| 2736 | if (chain_index > TC_ACT_EXT_VAL_MASK) {
|
| 2737 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
|
| 2738 | err = -EINVAL;
|
| 2739 | goto errout;
|
| 2740 | }
|
| 2741 | chain = tcf_chain_get(block, chain_index, false);
|
| 2742 | if (!chain) {
|
| 2743 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
|
| 2744 | err = -EINVAL;
|
| 2745 | goto errout;
|
| 2746 | }
|
| 2747 |
|
| 2748 | mutex_lock(&chain->filter_chain_lock);
|
| 2749 | tp = tcf_chain_tp_find(chain, &chain_info, protocol,
|
| 2750 | prio, false, extack);
|
| 2751 | mutex_unlock(&chain->filter_chain_lock);
|
| 2752 | if (!tp) {
|
| 2753 | err = -ENOENT;
|
| 2754 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
|
| 2755 | goto errout;
|
| 2756 | } else if (IS_ERR(tp)) {
|
| 2757 | err = PTR_ERR(tp);
|
| 2758 | goto errout;
|
| 2759 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
|
| 2760 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
|
| 2761 | err = -EINVAL;
|
| 2762 | goto errout;
|
| 2763 | }
|
| 2764 |
|
| 2765 | fh = tp->ops->get(tp, t->tcm_handle);
|
| 2766 |
|
| 2767 | if (!fh) {
|
| 2768 | NL_SET_ERR_MSG(extack, "Specified filter handle not found");
|
| 2769 | err = -ENOENT;
|
| 2770 | } else {
|
| 2771 | err = tfilter_notify(net, skb, n, tp, block, q, parent,
|
| 2772 | fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
|
| 2773 | if (err < 0)
|
| 2774 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
|
| 2775 | }
|
| 2776 |
|
| 2777 | tfilter_put(tp, fh);
|
| 2778 | errout:
|
| 2779 | if (chain) {
|
| 2780 | if (tp && !IS_ERR(tp))
|
| 2781 | tcf_proto_put(tp, rtnl_held, NULL);
|
| 2782 | tcf_chain_put(chain);
|
| 2783 | }
|
| 2784 | tcf_block_release(q, block, rtnl_held);
|
| 2785 |
|
| 2786 | if (rtnl_held)
|
| 2787 | rtnl_unlock();
|
| 2788 |
|
| 2789 | return err;
|
| 2790 | }
|
| 2791 |
|
| 2792 | struct tcf_dump_args {
|
| 2793 | struct tcf_walker w;
|
| 2794 | struct sk_buff *skb;
|
| 2795 | struct netlink_callback *cb;
|
| 2796 | struct tcf_block *block;
|
| 2797 | struct Qdisc *q;
|
| 2798 | u32 parent;
|
| 2799 | bool terse_dump;
|
| 2800 | };
|
| 2801 |
|
| 2802 | static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
|
| 2803 | {
|
| 2804 | struct tcf_dump_args *a = (void *)arg;
|
| 2805 | struct net *net = sock_net(a->skb->sk);
|
| 2806 |
|
| 2807 | return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
|
| 2808 | n, NETLINK_CB(a->cb->skb).portid,
|
| 2809 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
|
| 2810 | RTM_NEWTFILTER, a->terse_dump, true, NULL);
|
| 2811 | }
|
| 2812 |
|
| 2813 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
|
| 2814 | struct sk_buff *skb, struct netlink_callback *cb,
|
| 2815 | long index_start, long *p_index, bool terse)
|
| 2816 | {
|
| 2817 | struct net *net = sock_net(skb->sk);
|
| 2818 | struct tcf_block *block = chain->block;
|
| 2819 | struct tcmsg *tcm = nlmsg_data(cb->nlh);
|
| 2820 | struct tcf_proto *tp, *tp_prev;
|
| 2821 | struct tcf_dump_args arg;
|
| 2822 |
|
| 2823 | for (tp = __tcf_get_next_proto(chain, NULL);
|
| 2824 | tp;
|
| 2825 | tp_prev = tp,
|
| 2826 | tp = __tcf_get_next_proto(chain, tp),
|
| 2827 | tcf_proto_put(tp_prev, true, NULL),
|
| 2828 | (*p_index)++) {
|
| 2829 | if (*p_index < index_start)
|
| 2830 | continue;
|
| 2831 | if (TC_H_MAJ(tcm->tcm_info) &&
|
| 2832 | TC_H_MAJ(tcm->tcm_info) != tp->prio)
|
| 2833 | continue;
|
| 2834 | if (TC_H_MIN(tcm->tcm_info) &&
|
| 2835 | TC_H_MIN(tcm->tcm_info) != tp->protocol)
|
| 2836 | continue;
|
| 2837 | if (*p_index > index_start)
|
| 2838 | memset(&cb->args[1], 0,
|
| 2839 | sizeof(cb->args) - sizeof(cb->args[0]));
|
| 2840 | if (cb->args[1] == 0) {
|
| 2841 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
|
| 2842 | NETLINK_CB(cb->skb).portid,
|
| 2843 | cb->nlh->nlmsg_seq, NLM_F_MULTI,
|
| 2844 | RTM_NEWTFILTER, false, true, NULL) <= 0)
|
| 2845 | goto errout;
|
| 2846 | cb->args[1] = 1;
|
| 2847 | }
|
| 2848 | if (!tp->ops->walk)
|
| 2849 | continue;
|
| 2850 | arg.w.fn = tcf_node_dump;
|
| 2851 | arg.skb = skb;
|
| 2852 | arg.cb = cb;
|
| 2853 | arg.block = block;
|
| 2854 | arg.q = q;
|
| 2855 | arg.parent = parent;
|
| 2856 | arg.w.stop = 0;
|
| 2857 | arg.w.skip = cb->args[1] - 1;
|
| 2858 | arg.w.count = 0;
|
| 2859 | arg.w.cookie = cb->args[2];
|
| 2860 | arg.terse_dump = terse;
|
| 2861 | tp->ops->walk(tp, &arg.w, true);
|
| 2862 | cb->args[2] = arg.w.cookie;
|
| 2863 | cb->args[1] = arg.w.count + 1;
|
| 2864 | if (arg.w.stop)
|
| 2865 | goto errout;
|
| 2866 | }
|
| 2867 | return true;
|
| 2868 |
|
| 2869 | errout:
|
| 2870 | tcf_proto_put(tp, true, NULL);
|
| 2871 | return false;
|
| 2872 | }
|
| 2873 |
|
| 2874 | static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
|
| 2875 | [TCA_CHAIN] = { .type = NLA_U32 },
|
| 2876 | [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
|
| 2877 | };
|
| 2878 |
|
| 2879 |
|
| 2880 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
|
| 2881 | {
|
| 2882 | struct tcf_chain *chain, *chain_prev;
|
| 2883 | struct net *net = sock_net(skb->sk);
|
| 2884 | struct nlattr *tca[TCA_MAX + 1];
|
| 2885 | struct Qdisc *q = NULL;
|
| 2886 | struct tcf_block *block;
|
| 2887 | struct tcmsg *tcm = nlmsg_data(cb->nlh);
|
| 2888 | bool terse_dump = false;
|
| 2889 | long index_start;
|
| 2890 | long index;
|
| 2891 | u32 parent;
|
| 2892 | int err;
|
| 2893 |
|
| 2894 | if (nlmsg_len(cb->nlh) < sizeof(*tcm))
|
| 2895 | return skb->len;
|
| 2896 |
|
| 2897 | err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
|
| 2898 | tcf_tfilter_dump_policy, cb->extack);
|
| 2899 | if (err)
|
| 2900 | return err;
|
| 2901 |
|
| 2902 | if (tca[TCA_DUMP_FLAGS]) {
|
| 2903 | struct nla_bitfield32 flags =
|
| 2904 | nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
|
| 2905 |
|
| 2906 | terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
|
| 2907 | }
|
| 2908 |
|
| 2909 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
|
| 2910 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
|
| 2911 | if (!block)
|
| 2912 | goto out;
|
| 2913 | |
| 2914 | * will never be used in the following code. The check
|
| 2915 | * in tcf_fill_node prevents it. However, compiler does not
|
| 2916 | * see that far, so set parent to zero to silence the warning
|
| 2917 | * about parent being uninitialized.
|
| 2918 |
|
| 2919 | parent = 0;
|
| 2920 | } else {
|
| 2921 | const struct Qdisc_class_ops *cops;
|
| 2922 | struct net_device *dev;
|
| 2923 | unsigned long cl = 0;
|
| 2924 |
|
| 2925 | dev = __dev_get_by_index(net, tcm->tcm_ifindex);
|
| 2926 | if (!dev)
|
| 2927 | return skb->len;
|
| 2928 |
|
| 2929 | parent = tcm->tcm_parent;
|
| 2930 | if (!parent)
|
| 2931 | q = rtnl_dereference(dev->qdisc);
|
| 2932 | else
|
| 2933 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
|
| 2934 | if (!q)
|
| 2935 | goto out;
|
| 2936 | cops = q->ops->cl_ops;
|
| 2937 | if (!cops)
|
| 2938 | goto out;
|
| 2939 | if (!cops->tcf_block)
|
| 2940 | goto out;
|
| 2941 | if (TC_H_MIN(tcm->tcm_parent)) {
|
| 2942 | cl = cops->find(q, tcm->tcm_parent);
|
| 2943 | if (cl == 0)
|
| 2944 | goto out;
|
| 2945 | }
|
| 2946 | block = cops->tcf_block(q, cl, NULL);
|
| 2947 | if (!block)
|
| 2948 | goto out;
|
| 2949 | parent = block->classid;
|
| 2950 | if (tcf_block_shared(block))
|
| 2951 | q = NULL;
|
| 2952 | }
|
| 2953 |
|
| 2954 | index_start = cb->args[0];
|
| 2955 | index = 0;
|
| 2956 |
|
| 2957 | for (chain = __tcf_get_next_chain(block, NULL);
|
| 2958 | chain;
|
| 2959 | chain_prev = chain,
|
| 2960 | chain = __tcf_get_next_chain(block, chain),
|
| 2961 | tcf_chain_put(chain_prev)) {
|
| 2962 | if (tca[TCA_CHAIN] &&
|
| 2963 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)
|
| 2964 | continue;
|
| 2965 | if (!tcf_chain_dump(chain, q, parent, skb, cb,
|
| 2966 | index_start, &index, terse_dump)) {
|
| 2967 | tcf_chain_put(chain);
|
| 2968 | err = -EMSGSIZE;
|
| 2969 | break;
|
| 2970 | }
|
| 2971 | }
|
| 2972 |
|
| 2973 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
|
| 2974 | tcf_block_refcnt_put(block, true);
|
| 2975 | cb->args[0] = index;
|
| 2976 |
|
| 2977 | out:
|
| 2978 |
|
| 2979 | if (skb->len == 0 && err)
|
| 2980 | return err;
|
| 2981 | return skb->len;
|
| 2982 | }
|
| 2983 |
|
| 2984 | static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
|
| 2985 | void *tmplt_priv, u32 chain_index,
|
| 2986 | struct net *net, struct sk_buff *skb,
|
| 2987 | struct tcf_block *block,
|
| 2988 | u32 portid, u32 seq, u16 flags, int event,
|
| 2989 | struct netlink_ext_ack *extack)
|
| 2990 | {
|
| 2991 | unsigned char *b = skb_tail_pointer(skb);
|
| 2992 | const struct tcf_proto_ops *ops;
|
| 2993 | struct nlmsghdr *nlh;
|
| 2994 | struct tcmsg *tcm;
|
| 2995 | void *priv;
|
| 2996 |
|
| 2997 | ops = tmplt_ops;
|
| 2998 | priv = tmplt_priv;
|
| 2999 |
|
| 3000 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
|
| 3001 | if (!nlh)
|
| 3002 | goto out_nlmsg_trim;
|
| 3003 | tcm = nlmsg_data(nlh);
|
| 3004 | tcm->tcm_family = AF_UNSPEC;
|
| 3005 | tcm->tcm__pad1 = 0;
|
| 3006 | tcm->tcm__pad2 = 0;
|
| 3007 | tcm->tcm_handle = 0;
|
| 3008 | tcm->tcm_info = 0;
|
| 3009 | if (block->q) {
|
| 3010 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
|
| 3011 | tcm->tcm_parent = block->q->handle;
|
| 3012 | } else {
|
| 3013 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
|
| 3014 | tcm->tcm_block_index = block->index;
|
| 3015 | }
|
| 3016 |
|
| 3017 | if (nla_put_u32(skb, TCA_CHAIN, chain_index))
|
| 3018 | goto nla_put_failure;
|
| 3019 |
|
| 3020 | if (ops) {
|
| 3021 | if (nla_put_string(skb, TCA_KIND, ops->kind))
|
| 3022 | goto nla_put_failure;
|
| 3023 | if (ops->tmplt_dump(skb, net, priv) < 0)
|
| 3024 | goto nla_put_failure;
|
| 3025 | }
|
| 3026 |
|
| 3027 | if (extack && extack->_msg &&
|
| 3028 | nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
|
| 3029 | goto out_nlmsg_trim;
|
| 3030 |
|
| 3031 | nlh->nlmsg_len = skb_tail_pointer(skb) - b;
|
| 3032 |
|
| 3033 | return skb->len;
|
| 3034 |
|
| 3035 | out_nlmsg_trim:
|
| 3036 | nla_put_failure:
|
| 3037 | nlmsg_trim(skb, b);
|
| 3038 | return -EMSGSIZE;
|
| 3039 | }
|
| 3040 |
|
| 3041 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
|
| 3042 | u32 seq, u16 flags, int event, bool unicast,
|
| 3043 | struct netlink_ext_ack *extack)
|
| 3044 | {
|
| 3045 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
|
| 3046 | struct tcf_block *block = chain->block;
|
| 3047 | struct net *net = block->net;
|
| 3048 | struct sk_buff *skb;
|
| 3049 | int err = 0;
|
| 3050 |
|
| 3051 | if (!unicast && !rtnl_notify_needed(net, flags, RTNLGRP_TC))
|
| 3052 | return 0;
|
| 3053 |
|
| 3054 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
|
| 3055 | if (!skb)
|
| 3056 | return -ENOBUFS;
|
| 3057 |
|
| 3058 | if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
|
| 3059 | chain->index, net, skb, block, portid,
|
| 3060 | seq, flags, event, extack) <= 0) {
|
| 3061 | kfree_skb(skb);
|
| 3062 | return -EINVAL;
|
| 3063 | }
|
| 3064 |
|
| 3065 | if (unicast)
|
| 3066 | err = rtnl_unicast(skb, net, portid);
|
| 3067 | else
|
| 3068 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
|
| 3069 | flags & NLM_F_ECHO);
|
| 3070 |
|
| 3071 | return err;
|
| 3072 | }
|
| 3073 |
|
| 3074 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
|
| 3075 | void *tmplt_priv, u32 chain_index,
|
| 3076 | struct tcf_block *block, struct sk_buff *oskb,
|
| 3077 | u32 seq, u16 flags)
|
| 3078 | {
|
| 3079 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
|
| 3080 | struct net *net = block->net;
|
| 3081 | struct sk_buff *skb;
|
| 3082 |
|
| 3083 | if (!rtnl_notify_needed(net, flags, RTNLGRP_TC))
|
| 3084 | return 0;
|
| 3085 |
|
| 3086 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
|
| 3087 | if (!skb)
|
| 3088 | return -ENOBUFS;
|
| 3089 |
|
| 3090 | if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
|
| 3091 | block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
|
| 3092 | kfree_skb(skb);
|
| 3093 | return -EINVAL;
|
| 3094 | }
|
| 3095 |
|
| 3096 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
|
| 3097 | }
|
| 3098 |
|
| 3099 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
|
| 3100 | struct nlattr **tca,
|
| 3101 | struct netlink_ext_ack *extack)
|
| 3102 | {
|
| 3103 | const struct tcf_proto_ops *ops;
|
| 3104 | char name[IFNAMSIZ];
|
| 3105 | void *tmplt_priv;
|
| 3106 |
|
| 3107 |
|
| 3108 | if (!tca[TCA_KIND])
|
| 3109 | return 0;
|
| 3110 |
|
| 3111 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
|
| 3112 | NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
|
| 3113 | return -EINVAL;
|
| 3114 | }
|
| 3115 |
|
| 3116 | ops = tcf_proto_lookup_ops(name, true, extack);
|
| 3117 | if (IS_ERR(ops))
|
| 3118 | return PTR_ERR(ops);
|
| 3119 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump ||
|
| 3120 | !ops->tmplt_reoffload) {
|
| 3121 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
|
| 3122 | module_put(ops->owner);
|
| 3123 | return -EOPNOTSUPP;
|
| 3124 | }
|
| 3125 |
|
| 3126 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
|
| 3127 | if (IS_ERR(tmplt_priv)) {
|
| 3128 | module_put(ops->owner);
|
| 3129 | return PTR_ERR(tmplt_priv);
|
| 3130 | }
|
| 3131 | chain->tmplt_ops = ops;
|
| 3132 | chain->tmplt_priv = tmplt_priv;
|
| 3133 | return 0;
|
| 3134 | }
|
| 3135 |
|
| 3136 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
|
| 3137 | void *tmplt_priv)
|
| 3138 | {
|
| 3139 |
|
| 3140 | if (!tmplt_ops)
|
| 3141 | return;
|
| 3142 |
|
| 3143 | tmplt_ops->tmplt_destroy(tmplt_priv);
|
| 3144 | module_put(tmplt_ops->owner);
|
| 3145 | }
|
| 3146 |
|
| 3147 |
|
| 3148 |
|
| 3149 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
|
| 3150 | struct netlink_ext_ack *extack)
|
| 3151 | {
|
| 3152 | struct net *net = sock_net(skb->sk);
|
| 3153 | struct nlattr *tca[TCA_MAX + 1];
|
| 3154 | struct tcmsg *t;
|
| 3155 | u32 parent;
|
| 3156 | u32 chain_index;
|
| 3157 | struct Qdisc *q;
|
| 3158 | struct tcf_chain *chain;
|
| 3159 | struct tcf_block *block;
|
| 3160 | unsigned long cl;
|
| 3161 | int err;
|
| 3162 |
|
| 3163 | replay:
|
| 3164 | q = NULL;
|
| 3165 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
|
| 3166 | rtm_tca_policy, extack);
|
| 3167 | if (err < 0)
|
| 3168 | return err;
|
| 3169 |
|
| 3170 | t = nlmsg_data(n);
|
| 3171 | parent = t->tcm_parent;
|
| 3172 | cl = 0;
|
| 3173 |
|
| 3174 | block = tcf_block_find(net, &q, &parent, &cl,
|
| 3175 | t->tcm_ifindex, t->tcm_block_index, extack);
|
| 3176 | if (IS_ERR(block))
|
| 3177 | return PTR_ERR(block);
|
| 3178 |
|
| 3179 | chain_index = nla_get_u32_default(tca[TCA_CHAIN], 0);
|
| 3180 | if (chain_index > TC_ACT_EXT_VAL_MASK) {
|
| 3181 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
|
| 3182 | err = -EINVAL;
|
| 3183 | goto errout_block;
|
| 3184 | }
|
| 3185 |
|
| 3186 | mutex_lock(&block->lock);
|
| 3187 | chain = tcf_chain_lookup(block, chain_index);
|
| 3188 | if (n->nlmsg_type == RTM_NEWCHAIN) {
|
| 3189 | if (chain) {
|
| 3190 | if (tcf_chain_held_by_acts_only(chain)) {
|
| 3191 | |
| 3192 | * some action referencing it.
|
| 3193 |
|
| 3194 | tcf_chain_hold(chain);
|
| 3195 | } else {
|
| 3196 | NL_SET_ERR_MSG(extack, "Filter chain already exists");
|
| 3197 | err = -EEXIST;
|
| 3198 | goto errout_block_locked;
|
| 3199 | }
|
| 3200 | } else {
|
| 3201 | if (!(n->nlmsg_flags & NLM_F_CREATE)) {
|
| 3202 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
|
| 3203 | err = -ENOENT;
|
| 3204 | goto errout_block_locked;
|
| 3205 | }
|
| 3206 | chain = tcf_chain_create(block, chain_index);
|
| 3207 | if (!chain) {
|
| 3208 | NL_SET_ERR_MSG(extack, "Failed to create filter chain");
|
| 3209 | err = -ENOMEM;
|
| 3210 | goto errout_block_locked;
|
| 3211 | }
|
| 3212 | }
|
| 3213 | } else {
|
| 3214 | if (!chain || tcf_chain_held_by_acts_only(chain)) {
|
| 3215 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
|
| 3216 | err = -EINVAL;
|
| 3217 | goto errout_block_locked;
|
| 3218 | }
|
| 3219 | tcf_chain_hold(chain);
|
| 3220 | }
|
| 3221 |
|
| 3222 | if (n->nlmsg_type == RTM_NEWCHAIN) {
|
| 3223 | |
| 3224 | * the chain was successfully added, take a reference to the
|
| 3225 | * chain. This ensures that an empty chain does not disappear at
|
| 3226 | * the end of this function.
|
| 3227 |
|
| 3228 | tcf_chain_hold(chain);
|
| 3229 | chain->explicitly_created = true;
|
| 3230 | }
|
| 3231 | mutex_unlock(&block->lock);
|
| 3232 |
|
| 3233 | switch (n->nlmsg_type) {
|
| 3234 | case RTM_NEWCHAIN:
|
| 3235 | err = tc_chain_tmplt_add(chain, net, tca, extack);
|
| 3236 | if (err) {
|
| 3237 | tcf_chain_put_explicitly_created(chain);
|
| 3238 | goto errout;
|
| 3239 | }
|
| 3240 |
|
| 3241 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
|
| 3242 | RTM_NEWCHAIN, false, extack);
|
| 3243 | break;
|
| 3244 | case RTM_DELCHAIN:
|
| 3245 | tfilter_notify_chain(net, skb, block, q, parent, n,
|
| 3246 | chain, RTM_DELTFILTER, extack);
|
| 3247 |
|
| 3248 | tcf_chain_flush(chain, true);
|
| 3249 | |
| 3250 | * to the chain previously taken during addition.
|
| 3251 |
|
| 3252 | tcf_chain_put_explicitly_created(chain);
|
| 3253 | break;
|
| 3254 | case RTM_GETCHAIN:
|
| 3255 | err = tc_chain_notify(chain, skb, n->nlmsg_seq,
|
| 3256 | n->nlmsg_flags, n->nlmsg_type, true, extack);
|
| 3257 | if (err < 0)
|
| 3258 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
|
| 3259 | break;
|
| 3260 | default:
|
| 3261 | err = -EOPNOTSUPP;
|
| 3262 | NL_SET_ERR_MSG(extack, "Unsupported message type");
|
| 3263 | goto errout;
|
| 3264 | }
|
| 3265 |
|
| 3266 | errout:
|
| 3267 | tcf_chain_put(chain);
|
| 3268 | errout_block:
|
| 3269 | tcf_block_release(q, block, true);
|
| 3270 | if (err == -EAGAIN && n->nlmsg_type == RTM_NEWCHAIN)
|
| 3271 |
|
| 3272 | goto replay;
|
| 3273 | return err;
|
| 3274 |
|
| 3275 | errout_block_locked:
|
| 3276 | mutex_unlock(&block->lock);
|
| 3277 | goto errout_block;
|
| 3278 | }
|
| 3279 |
|
| 3280 |
|
| 3281 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
|
| 3282 | {
|
| 3283 | struct net *net = sock_net(skb->sk);
|
| 3284 | struct nlattr *tca[TCA_MAX + 1];
|
| 3285 | struct Qdisc *q = NULL;
|
| 3286 | struct tcf_block *block;
|
| 3287 | struct tcmsg *tcm = nlmsg_data(cb->nlh);
|
| 3288 | struct tcf_chain *chain;
|
| 3289 | long index_start;
|
| 3290 | long index;
|
| 3291 | int err;
|
| 3292 |
|
| 3293 | if (nlmsg_len(cb->nlh) < sizeof(*tcm))
|
| 3294 | return skb->len;
|
| 3295 |
|
| 3296 | err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
|
| 3297 | rtm_tca_policy, cb->extack);
|
| 3298 | if (err)
|
| 3299 | return err;
|
| 3300 |
|
| 3301 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
|
| 3302 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
|
| 3303 | if (!block)
|
| 3304 | goto out;
|
| 3305 | } else {
|
| 3306 | const struct Qdisc_class_ops *cops;
|
| 3307 | struct net_device *dev;
|
| 3308 | unsigned long cl = 0;
|
| 3309 |
|
| 3310 | dev = __dev_get_by_index(net, tcm->tcm_ifindex);
|
| 3311 | if (!dev)
|
| 3312 | return skb->len;
|
| 3313 |
|
| 3314 | if (!tcm->tcm_parent)
|
| 3315 | q = rtnl_dereference(dev->qdisc);
|
| 3316 | else
|
| 3317 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
|
| 3318 |
|
| 3319 | if (!q)
|
| 3320 | goto out;
|
| 3321 | cops = q->ops->cl_ops;
|
| 3322 | if (!cops)
|
| 3323 | goto out;
|
| 3324 | if (!cops->tcf_block)
|
| 3325 | goto out;
|
| 3326 | if (TC_H_MIN(tcm->tcm_parent)) {
|
| 3327 | cl = cops->find(q, tcm->tcm_parent);
|
| 3328 | if (cl == 0)
|
| 3329 | goto out;
|
| 3330 | }
|
| 3331 | block = cops->tcf_block(q, cl, NULL);
|
| 3332 | if (!block)
|
| 3333 | goto out;
|
| 3334 | if (tcf_block_shared(block))
|
| 3335 | q = NULL;
|
| 3336 | }
|
| 3337 |
|
| 3338 | index_start = cb->args[0];
|
| 3339 | index = 0;
|
| 3340 |
|
| 3341 | mutex_lock(&block->lock);
|
| 3342 | list_for_each_entry(chain, &block->chain_list, list) {
|
| 3343 | if ((tca[TCA_CHAIN] &&
|
| 3344 | nla_get_u32(tca[TCA_CHAIN]) != chain->index))
|
| 3345 | continue;
|
| 3346 | if (index < index_start) {
|
| 3347 | index++;
|
| 3348 | continue;
|
| 3349 | }
|
| 3350 | if (tcf_chain_held_by_acts_only(chain))
|
| 3351 | continue;
|
| 3352 | err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
|
| 3353 | chain->index, net, skb, block,
|
| 3354 | NETLINK_CB(cb->skb).portid,
|
| 3355 | cb->nlh->nlmsg_seq, NLM_F_MULTI,
|
| 3356 | RTM_NEWCHAIN, NULL);
|
| 3357 | if (err <= 0)
|
| 3358 | break;
|
| 3359 | index++;
|
| 3360 | }
|
| 3361 | mutex_unlock(&block->lock);
|
| 3362 |
|
| 3363 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
|
| 3364 | tcf_block_refcnt_put(block, true);
|
| 3365 | cb->args[0] = index;
|
| 3366 |
|
| 3367 | out:
|
| 3368 |
|
| 3369 | if (skb->len == 0 && err)
|
| 3370 | return err;
|
| 3371 | return skb->len;
|
| 3372 | }
|
| 3373 |
|
| 3374 | int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action,
|
| 3375 | int police, struct tcf_proto *tp, u32 handle,
|
| 3376 | bool use_action_miss)
|
| 3377 | {
|
| 3378 | int err = 0;
|
| 3379 |
|
| 3380 | #ifdef CONFIG_NET_CLS_ACT
|
| 3381 | exts->type = 0;
|
| 3382 | exts->nr_actions = 0;
|
| 3383 | exts->miss_cookie_node = NULL;
|
| 3384 | |
| 3385 | * This reference might be taken later from tcf_exts_get_net().
|
| 3386 |
|
| 3387 | exts->net = net;
|
| 3388 | exts->actions = kzalloc_objs(struct tc_action *, TCA_ACT_MAX_PRIO,
|
| 3389 | GFP_KERNEL_ACCOUNT);
|
| 3390 | if (!exts->actions)
|
| 3391 | return -ENOMEM;
|
| 3392 | #endif
|
| 3393 |
|
| 3394 | exts->action = action;
|
| 3395 | exts->police = police;
|
| 3396 |
|
| 3397 | if (!use_action_miss)
|
| 3398 | return 0;
|
| 3399 |
|
| 3400 | err = tcf_exts_miss_cookie_base_alloc(exts, tp, handle);
|
| 3401 | if (err)
|
| 3402 | goto err_miss_alloc;
|
| 3403 |
|
| 3404 | return 0;
|
| 3405 |
|
| 3406 | err_miss_alloc:
|
| 3407 | tcf_exts_destroy(exts);
|
| 3408 | #ifdef CONFIG_NET_CLS_ACT
|
| 3409 | exts->actions = NULL;
|
| 3410 | #endif
|
| 3411 | return err;
|
| 3412 | }
|
| 3413 | EXPORT_SYMBOL(tcf_exts_init_ex);
|
| 3414 |
|
| 3415 | void tcf_exts_destroy(struct tcf_exts *exts)
|
| 3416 | {
|
| 3417 | tcf_exts_miss_cookie_base_destroy(exts);
|
| 3418 |
|
| 3419 | #ifdef CONFIG_NET_CLS_ACT
|
| 3420 | if (exts->actions) {
|
| 3421 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
|
| 3422 | kfree(exts->actions);
|
| 3423 | }
|
| 3424 | exts->nr_actions = 0;
|
| 3425 | #endif
|
| 3426 | }
|
| 3427 | EXPORT_SYMBOL(tcf_exts_destroy);
|
| 3428 |
|
| 3429 | int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
|
| 3430 | struct nlattr *rate_tlv, struct tcf_exts *exts,
|
| 3431 | u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
|
| 3432 | {
|
| 3433 | #ifdef CONFIG_NET_CLS_ACT
|
| 3434 | {
|
| 3435 | int init_res[TCA_ACT_MAX_PRIO] = {};
|
| 3436 | struct tc_action *act;
|
| 3437 | size_t attr_size = 0;
|
| 3438 |
|
| 3439 | if (exts->police && tb[exts->police]) {
|
| 3440 | struct tc_action_ops *a_o;
|
| 3441 |
|
| 3442 | flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
|
| 3443 | a_o = tc_action_load_ops(tb[exts->police], flags,
|
| 3444 | extack);
|
| 3445 | if (IS_ERR(a_o))
|
| 3446 | return PTR_ERR(a_o);
|
| 3447 | act = tcf_action_init_1(net, tp, tb[exts->police],
|
| 3448 | rate_tlv, a_o, init_res, flags,
|
| 3449 | extack);
|
| 3450 | module_put(a_o->owner);
|
| 3451 | if (IS_ERR(act))
|
| 3452 | return PTR_ERR(act);
|
| 3453 |
|
| 3454 | act->type = exts->type = TCA_OLD_COMPAT;
|
| 3455 | exts->actions[0] = act;
|
| 3456 | exts->nr_actions = 1;
|
| 3457 | tcf_idr_insert_many(exts->actions, init_res);
|
| 3458 | } else if (exts->action && tb[exts->action]) {
|
| 3459 | int err;
|
| 3460 |
|
| 3461 | flags |= TCA_ACT_FLAGS_BIND;
|
| 3462 | err = tcf_action_init(net, tp, tb[exts->action],
|
| 3463 | rate_tlv, exts->actions, init_res,
|
| 3464 | &attr_size, flags, fl_flags,
|
| 3465 | extack);
|
| 3466 | if (err < 0)
|
| 3467 | return err;
|
| 3468 | exts->nr_actions = err;
|
| 3469 | }
|
| 3470 | }
|
| 3471 | #else
|
| 3472 | if ((exts->action && tb[exts->action]) ||
|
| 3473 | (exts->police && tb[exts->police])) {
|
| 3474 | NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
|
| 3475 | return -EOPNOTSUPP;
|
| 3476 | }
|
| 3477 | #endif
|
| 3478 |
|
| 3479 | return 0;
|
| 3480 | }
|
| 3481 | EXPORT_SYMBOL(tcf_exts_validate_ex);
|
| 3482 |
|
| 3483 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
|
| 3484 | struct nlattr *rate_tlv, struct tcf_exts *exts,
|
| 3485 | u32 flags, struct netlink_ext_ack *extack)
|
| 3486 | {
|
| 3487 | return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
|
| 3488 | flags, 0, extack);
|
| 3489 | }
|
| 3490 | EXPORT_SYMBOL(tcf_exts_validate);
|
| 3491 |
|
| 3492 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
|
| 3493 | {
|
| 3494 | #ifdef CONFIG_NET_CLS_ACT
|
| 3495 | struct tcf_exts old = *dst;
|
| 3496 |
|
| 3497 | *dst = *src;
|
| 3498 | tcf_exts_destroy(&old);
|
| 3499 | #endif
|
| 3500 | }
|
| 3501 | EXPORT_SYMBOL(tcf_exts_change);
|
| 3502 |
|
| 3503 | #ifdef CONFIG_NET_CLS_ACT
|
| 3504 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
|
| 3505 | {
|
| 3506 | if (exts->nr_actions == 0)
|
| 3507 | return NULL;
|
| 3508 | else
|
| 3509 | return exts->actions[0];
|
| 3510 | }
|
| 3511 | #endif
|
| 3512 |
|
| 3513 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
|
| 3514 | {
|
| 3515 | #ifdef CONFIG_NET_CLS_ACT
|
| 3516 | struct nlattr *nest;
|
| 3517 |
|
| 3518 | if (exts->action && tcf_exts_has_actions(exts)) {
|
| 3519 | |
| 3520 | * again for backward compatible mode - we want
|
| 3521 | * to work with both old and new modes of entering
|
| 3522 | * tc data even if iproute2 was newer - jhs
|
| 3523 |
|
| 3524 | if (exts->type != TCA_OLD_COMPAT) {
|
| 3525 | nest = nla_nest_start_noflag(skb, exts->action);
|
| 3526 | if (nest == NULL)
|
| 3527 | goto nla_put_failure;
|
| 3528 |
|
| 3529 | if (tcf_action_dump(skb, exts->actions, 0, 0, false)
|
| 3530 | < 0)
|
| 3531 | goto nla_put_failure;
|
| 3532 | nla_nest_end(skb, nest);
|
| 3533 | } else if (exts->police) {
|
| 3534 | struct tc_action *act = tcf_exts_first_act(exts);
|
| 3535 | nest = nla_nest_start_noflag(skb, exts->police);
|
| 3536 | if (nest == NULL || !act)
|
| 3537 | goto nla_put_failure;
|
| 3538 | if (tcf_action_dump_old(skb, act, 0, 0) < 0)
|
| 3539 | goto nla_put_failure;
|
| 3540 | nla_nest_end(skb, nest);
|
| 3541 | }
|
| 3542 | }
|
| 3543 | return 0;
|
| 3544 |
|
| 3545 | nla_put_failure:
|
| 3546 | nla_nest_cancel(skb, nest);
|
| 3547 | return -1;
|
| 3548 | #else
|
| 3549 | return 0;
|
| 3550 | #endif
|
| 3551 | }
|
| 3552 | EXPORT_SYMBOL(tcf_exts_dump);
|
| 3553 |
|
| 3554 | int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
|
| 3555 | {
|
| 3556 | #ifdef CONFIG_NET_CLS_ACT
|
| 3557 | struct nlattr *nest;
|
| 3558 |
|
| 3559 | if (!exts->action || !tcf_exts_has_actions(exts))
|
| 3560 | return 0;
|
| 3561 |
|
| 3562 | nest = nla_nest_start_noflag(skb, exts->action);
|
| 3563 | if (!nest)
|
| 3564 | goto nla_put_failure;
|
| 3565 |
|
| 3566 | if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
|
| 3567 | goto nla_put_failure;
|
| 3568 | nla_nest_end(skb, nest);
|
| 3569 | return 0;
|
| 3570 |
|
| 3571 | nla_put_failure:
|
| 3572 | nla_nest_cancel(skb, nest);
|
| 3573 | return -1;
|
| 3574 | #else
|
| 3575 | return 0;
|
| 3576 | #endif
|
| 3577 | }
|
| 3578 | EXPORT_SYMBOL(tcf_exts_terse_dump);
|
| 3579 |
|
| 3580 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
|
| 3581 | {
|
| 3582 | #ifdef CONFIG_NET_CLS_ACT
|
| 3583 | struct tc_action *a = tcf_exts_first_act(exts);
|
| 3584 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
|
| 3585 | return -1;
|
| 3586 | #endif
|
| 3587 | return 0;
|
| 3588 | }
|
| 3589 | EXPORT_SYMBOL(tcf_exts_dump_stats);
|
| 3590 |
|
| 3591 | static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
|
| 3592 | {
|
| 3593 | if (*flags & TCA_CLS_FLAGS_IN_HW)
|
| 3594 | return;
|
| 3595 | *flags |= TCA_CLS_FLAGS_IN_HW;
|
| 3596 | atomic_inc(&block->offloadcnt);
|
| 3597 | }
|
| 3598 |
|
| 3599 | static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
|
| 3600 | {
|
| 3601 | if (!(*flags & TCA_CLS_FLAGS_IN_HW))
|
| 3602 | return;
|
| 3603 | *flags &= ~TCA_CLS_FLAGS_IN_HW;
|
| 3604 | atomic_dec(&block->offloadcnt);
|
| 3605 | }
|
| 3606 |
|
| 3607 | static void tc_cls_offload_cnt_update(struct tcf_block *block,
|
| 3608 | struct tcf_proto *tp, u32 *cnt,
|
| 3609 | u32 *flags, u32 diff, bool add)
|
| 3610 | {
|
| 3611 | lockdep_assert_held(&block->cb_lock);
|
| 3612 |
|
| 3613 | spin_lock(&tp->lock);
|
| 3614 | if (add) {
|
| 3615 | if (!*cnt)
|
| 3616 | tcf_block_offload_inc(block, flags);
|
| 3617 | *cnt += diff;
|
| 3618 | } else {
|
| 3619 | *cnt -= diff;
|
| 3620 | if (!*cnt)
|
| 3621 | tcf_block_offload_dec(block, flags);
|
| 3622 | }
|
| 3623 | spin_unlock(&tp->lock);
|
| 3624 | }
|
| 3625 |
|
| 3626 | static void
|
| 3627 | tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
|
| 3628 | u32 *cnt, u32 *flags)
|
| 3629 | {
|
| 3630 | lockdep_assert_held(&block->cb_lock);
|
| 3631 |
|
| 3632 | spin_lock(&tp->lock);
|
| 3633 | tcf_block_offload_dec(block, flags);
|
| 3634 | *cnt = 0;
|
| 3635 | spin_unlock(&tp->lock);
|
| 3636 | }
|
| 3637 |
|
| 3638 | static int
|
| 3639 | __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
|
| 3640 | void *type_data, bool err_stop)
|
| 3641 | {
|
| 3642 | struct flow_block_cb *block_cb;
|
| 3643 | int ok_count = 0;
|
| 3644 | int err;
|
| 3645 |
|
| 3646 | list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
|
| 3647 | err = block_cb->cb(type, type_data, block_cb->cb_priv);
|
| 3648 | if (err) {
|
| 3649 | if (err_stop)
|
| 3650 | return err;
|
| 3651 | } else {
|
| 3652 | ok_count++;
|
| 3653 | }
|
| 3654 | }
|
| 3655 | return ok_count;
|
| 3656 | }
|
| 3657 |
|
| 3658 | int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
|
| 3659 | void *type_data, bool err_stop, bool rtnl_held)
|
| 3660 | {
|
| 3661 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
|
| 3662 | int ok_count;
|
| 3663 |
|
| 3664 | retry:
|
| 3665 | if (take_rtnl)
|
| 3666 | rtnl_lock();
|
| 3667 | down_read(&block->cb_lock);
|
| 3668 | |
| 3669 | * In block bind code cb_lock is obtained while holding rtnl, so we must
|
| 3670 | * obtain the locks in same order here.
|
| 3671 |
|
| 3672 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
|
| 3673 | up_read(&block->cb_lock);
|
| 3674 | take_rtnl = true;
|
| 3675 | goto retry;
|
| 3676 | }
|
| 3677 |
|
| 3678 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
|
| 3679 |
|
| 3680 | up_read(&block->cb_lock);
|
| 3681 | if (take_rtnl)
|
| 3682 | rtnl_unlock();
|
| 3683 | return ok_count;
|
| 3684 | }
|
| 3685 | EXPORT_SYMBOL(tc_setup_cb_call);
|
| 3686 |
|
| 3687 | |
| 3688 | * successfully offloaded, increment block offloads counter. On failure,
|
| 3689 | * previously offloaded filter is considered to be intact and offloads counter
|
| 3690 | * is not decremented.
|
| 3691 |
|
| 3692 |
|
| 3693 | int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
|
| 3694 | enum tc_setup_type type, void *type_data, bool err_stop,
|
| 3695 | u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
|
| 3696 | {
|
| 3697 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
|
| 3698 | int ok_count;
|
| 3699 |
|
| 3700 | retry:
|
| 3701 | if (take_rtnl)
|
| 3702 | rtnl_lock();
|
| 3703 | down_read(&block->cb_lock);
|
| 3704 | |
| 3705 | * In block bind code cb_lock is obtained while holding rtnl, so we must
|
| 3706 | * obtain the locks in same order here.
|
| 3707 |
|
| 3708 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
|
| 3709 | up_read(&block->cb_lock);
|
| 3710 | take_rtnl = true;
|
| 3711 | goto retry;
|
| 3712 | }
|
| 3713 |
|
| 3714 |
|
| 3715 | if (block->nooffloaddevcnt && err_stop) {
|
| 3716 | ok_count = -EOPNOTSUPP;
|
| 3717 | goto err_unlock;
|
| 3718 | }
|
| 3719 |
|
| 3720 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
|
| 3721 | if (ok_count < 0)
|
| 3722 | goto err_unlock;
|
| 3723 |
|
| 3724 | if (tp->ops->hw_add)
|
| 3725 | tp->ops->hw_add(tp, type_data);
|
| 3726 | if (ok_count > 0)
|
| 3727 | tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
|
| 3728 | ok_count, true);
|
| 3729 | err_unlock:
|
| 3730 | up_read(&block->cb_lock);
|
| 3731 | if (take_rtnl)
|
| 3732 | rtnl_unlock();
|
| 3733 | return min(ok_count, 0);
|
| 3734 | }
|
| 3735 | EXPORT_SYMBOL(tc_setup_cb_add);
|
| 3736 |
|
| 3737 | |
| 3738 | * successfully offloaded, increment block offload counter. On failure,
|
| 3739 | * previously offloaded filter is considered to be destroyed and offload counter
|
| 3740 | * is decremented.
|
| 3741 |
|
| 3742 |
|
| 3743 | int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
|
| 3744 | enum tc_setup_type type, void *type_data, bool err_stop,
|
| 3745 | u32 *old_flags, unsigned int *old_in_hw_count,
|
| 3746 | u32 *new_flags, unsigned int *new_in_hw_count,
|
| 3747 | bool rtnl_held)
|
| 3748 | {
|
| 3749 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
|
| 3750 | int ok_count;
|
| 3751 |
|
| 3752 | retry:
|
| 3753 | if (take_rtnl)
|
| 3754 | rtnl_lock();
|
| 3755 | down_read(&block->cb_lock);
|
| 3756 | |
| 3757 | * In block bind code cb_lock is obtained while holding rtnl, so we must
|
| 3758 | * obtain the locks in same order here.
|
| 3759 |
|
| 3760 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
|
| 3761 | up_read(&block->cb_lock);
|
| 3762 | take_rtnl = true;
|
| 3763 | goto retry;
|
| 3764 | }
|
| 3765 |
|
| 3766 |
|
| 3767 | if (block->nooffloaddevcnt && err_stop) {
|
| 3768 | ok_count = -EOPNOTSUPP;
|
| 3769 | goto err_unlock;
|
| 3770 | }
|
| 3771 |
|
| 3772 | tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
|
| 3773 | if (tp->ops->hw_del)
|
| 3774 | tp->ops->hw_del(tp, type_data);
|
| 3775 |
|
| 3776 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
|
| 3777 | if (ok_count < 0)
|
| 3778 | goto err_unlock;
|
| 3779 |
|
| 3780 | if (tp->ops->hw_add)
|
| 3781 | tp->ops->hw_add(tp, type_data);
|
| 3782 | if (ok_count > 0)
|
| 3783 | tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
|
| 3784 | new_flags, ok_count, true);
|
| 3785 | err_unlock:
|
| 3786 | up_read(&block->cb_lock);
|
| 3787 | if (take_rtnl)
|
| 3788 | rtnl_unlock();
|
| 3789 | return min(ok_count, 0);
|
| 3790 | }
|
| 3791 | EXPORT_SYMBOL(tc_setup_cb_replace);
|
| 3792 |
|
| 3793 | |
| 3794 | * offloaded.
|
| 3795 |
|
| 3796 |
|
| 3797 | int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
|
| 3798 | enum tc_setup_type type, void *type_data, bool err_stop,
|
| 3799 | u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
|
| 3800 | {
|
| 3801 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
|
| 3802 | int ok_count;
|
| 3803 |
|
| 3804 | retry:
|
| 3805 | if (take_rtnl)
|
| 3806 | rtnl_lock();
|
| 3807 | down_read(&block->cb_lock);
|
| 3808 | |
| 3809 | * In block bind code cb_lock is obtained while holding rtnl, so we must
|
| 3810 | * obtain the locks in same order here.
|
| 3811 |
|
| 3812 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
|
| 3813 | up_read(&block->cb_lock);
|
| 3814 | take_rtnl = true;
|
| 3815 | goto retry;
|
| 3816 | }
|
| 3817 |
|
| 3818 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
|
| 3819 |
|
| 3820 | tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
|
| 3821 | if (tp->ops->hw_del)
|
| 3822 | tp->ops->hw_del(tp, type_data);
|
| 3823 |
|
| 3824 | up_read(&block->cb_lock);
|
| 3825 | if (take_rtnl)
|
| 3826 | rtnl_unlock();
|
| 3827 | return min(ok_count, 0);
|
| 3828 | }
|
| 3829 | EXPORT_SYMBOL(tc_setup_cb_destroy);
|
| 3830 |
|
| 3831 | int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
|
| 3832 | bool add, flow_setup_cb_t *cb,
|
| 3833 | enum tc_setup_type type, void *type_data,
|
| 3834 | void *cb_priv, u32 *flags, unsigned int *in_hw_count)
|
| 3835 | {
|
| 3836 | int err = cb(type, type_data, cb_priv);
|
| 3837 |
|
| 3838 | if (err) {
|
| 3839 | if (add && tc_skip_sw(*flags))
|
| 3840 | return err;
|
| 3841 | } else {
|
| 3842 | tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
|
| 3843 | add);
|
| 3844 | }
|
| 3845 |
|
| 3846 | return 0;
|
| 3847 | }
|
| 3848 | EXPORT_SYMBOL(tc_setup_cb_reoffload);
|
| 3849 |
|
| 3850 | static int tcf_act_get_user_cookie(struct flow_action_entry *entry,
|
| 3851 | const struct tc_action *act)
|
| 3852 | {
|
| 3853 | struct tc_cookie *user_cookie;
|
| 3854 | int err = 0;
|
| 3855 |
|
| 3856 | rcu_read_lock();
|
| 3857 | user_cookie = rcu_dereference(act->user_cookie);
|
| 3858 | if (user_cookie) {
|
| 3859 | entry->user_cookie = flow_action_cookie_create(user_cookie->data,
|
| 3860 | user_cookie->len,
|
| 3861 | GFP_ATOMIC);
|
| 3862 | if (!entry->user_cookie)
|
| 3863 | err = -ENOMEM;
|
| 3864 | }
|
| 3865 | rcu_read_unlock();
|
| 3866 | return err;
|
| 3867 | }
|
| 3868 |
|
| 3869 | static void tcf_act_put_user_cookie(struct flow_action_entry *entry)
|
| 3870 | {
|
| 3871 | flow_action_cookie_destroy(entry->user_cookie);
|
| 3872 | }
|
| 3873 |
|
| 3874 | void tc_cleanup_offload_action(struct flow_action *flow_action)
|
| 3875 | {
|
| 3876 | struct flow_action_entry *entry;
|
| 3877 | int i;
|
| 3878 |
|
| 3879 | flow_action_for_each(i, entry, flow_action) {
|
| 3880 | tcf_act_put_user_cookie(entry);
|
| 3881 | if (entry->destructor)
|
| 3882 | entry->destructor(entry->destructor_priv);
|
| 3883 | }
|
| 3884 | }
|
| 3885 | EXPORT_SYMBOL(tc_cleanup_offload_action);
|
| 3886 |
|
| 3887 | static int tc_setup_offload_act(struct tc_action *act,
|
| 3888 | struct flow_action_entry *entry,
|
| 3889 | u32 *index_inc,
|
| 3890 | struct netlink_ext_ack *extack)
|
| 3891 | {
|
| 3892 | #ifdef CONFIG_NET_CLS_ACT
|
| 3893 | if (act->ops->offload_act_setup) {
|
| 3894 | return act->ops->offload_act_setup(act, entry, index_inc, true,
|
| 3895 | extack);
|
| 3896 | } else {
|
| 3897 | NL_SET_ERR_MSG(extack, "Action does not support offload");
|
| 3898 | return -EOPNOTSUPP;
|
| 3899 | }
|
| 3900 | #else
|
| 3901 | return 0;
|
| 3902 | #endif
|
| 3903 | }
|
| 3904 |
|
| 3905 | int tc_setup_action(struct flow_action *flow_action,
|
| 3906 | struct tc_action *actions[],
|
| 3907 | u32 miss_cookie_base,
|
| 3908 | struct netlink_ext_ack *extack)
|
| 3909 | {
|
| 3910 | int i, j, k, index, err = 0;
|
| 3911 | struct tc_action *act;
|
| 3912 |
|
| 3913 | BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
|
| 3914 | BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
|
| 3915 | BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
|
| 3916 |
|
| 3917 | if (!actions)
|
| 3918 | return 0;
|
| 3919 |
|
| 3920 | j = 0;
|
| 3921 | tcf_act_for_each_action(i, act, actions) {
|
| 3922 | struct flow_action_entry *entry;
|
| 3923 |
|
| 3924 | entry = &flow_action->entries[j];
|
| 3925 | spin_lock_bh(&act->tcfa_lock);
|
| 3926 |
|
| 3927 |
|
| 3928 | if (j >= flow_action->num_entries) {
|
| 3929 | NL_SET_ERR_MSG_MOD(extack, "Flow action buffer overflow");
|
| 3930 | err = -ENOSPC;
|
| 3931 | goto err_out_locked;
|
| 3932 | }
|
| 3933 |
|
| 3934 | err = tcf_act_get_user_cookie(entry, act);
|
| 3935 | if (err)
|
| 3936 | goto err_out_locked;
|
| 3937 |
|
| 3938 | index = flow_action->num_entries - j;
|
| 3939 | err = tc_setup_offload_act(act, entry, &index,
|
| 3940 | extack);
|
| 3941 | if (err)
|
| 3942 | goto err_out_locked;
|
| 3943 |
|
| 3944 | for (k = 0; k < index ; k++) {
|
| 3945 | entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
|
| 3946 | entry[k].hw_index = act->tcfa_index;
|
| 3947 | entry[k].cookie = (unsigned long)act;
|
| 3948 | entry[k].miss_cookie =
|
| 3949 | tcf_exts_miss_cookie_get(miss_cookie_base, i);
|
| 3950 | }
|
| 3951 |
|
| 3952 | j += index;
|
| 3953 |
|
| 3954 | spin_unlock_bh(&act->tcfa_lock);
|
| 3955 | }
|
| 3956 |
|
| 3957 | err_out:
|
| 3958 | if (err)
|
| 3959 | tc_cleanup_offload_action(flow_action);
|
| 3960 |
|
| 3961 | return err;
|
| 3962 | err_out_locked:
|
| 3963 | spin_unlock_bh(&act->tcfa_lock);
|
| 3964 | goto err_out;
|
| 3965 | }
|
| 3966 |
|
| 3967 | int tc_setup_offload_action(struct flow_action *flow_action,
|
| 3968 | const struct tcf_exts *exts,
|
| 3969 | struct netlink_ext_ack *extack)
|
| 3970 | {
|
| 3971 | #ifdef CONFIG_NET_CLS_ACT
|
| 3972 | u32 miss_cookie_base;
|
| 3973 |
|
| 3974 | if (!exts)
|
| 3975 | return 0;
|
| 3976 |
|
| 3977 | miss_cookie_base = exts->miss_cookie_node ?
|
| 3978 | exts->miss_cookie_node->miss_cookie_base : 0;
|
| 3979 | return tc_setup_action(flow_action, exts->actions, miss_cookie_base,
|
| 3980 | extack);
|
| 3981 | #else
|
| 3982 | return 0;
|
| 3983 | #endif
|
| 3984 | }
|
| 3985 | EXPORT_SYMBOL(tc_setup_offload_action);
|
| 3986 |
|
| 3987 | unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
|
| 3988 | {
|
| 3989 | unsigned int num_acts = 0;
|
| 3990 | struct tc_action *act;
|
| 3991 | int i;
|
| 3992 |
|
| 3993 | tcf_exts_for_each_action(i, act, exts) {
|
| 3994 | if (is_tcf_pedit(act)) {
|
| 3995 | spin_lock_bh(&act->tcfa_lock);
|
| 3996 | num_acts += tcf_pedit_nkeys_locked(act);
|
| 3997 | spin_unlock_bh(&act->tcfa_lock);
|
| 3998 | } else {
|
| 3999 | num_acts++;
|
| 4000 | }
|
| 4001 | }
|
| 4002 | return num_acts;
|
| 4003 | }
|
| 4004 | EXPORT_SYMBOL(tcf_exts_num_actions);
|
| 4005 |
|
| 4006 | #ifdef CONFIG_NET_CLS_ACT
|
| 4007 | static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
|
| 4008 | u32 *p_block_index,
|
| 4009 | struct netlink_ext_ack *extack)
|
| 4010 | {
|
| 4011 | *p_block_index = nla_get_u32(block_index_attr);
|
| 4012 | if (!*p_block_index) {
|
| 4013 | NL_SET_ERR_MSG(extack, "Block number may not be zero");
|
| 4014 | return -EINVAL;
|
| 4015 | }
|
| 4016 |
|
| 4017 | return 0;
|
| 4018 | }
|
| 4019 |
|
| 4020 | int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
|
| 4021 | enum flow_block_binder_type binder_type,
|
| 4022 | struct nlattr *block_index_attr,
|
| 4023 | struct netlink_ext_ack *extack)
|
| 4024 | {
|
| 4025 | u32 block_index;
|
| 4026 | int err;
|
| 4027 |
|
| 4028 | if (!block_index_attr)
|
| 4029 | return 0;
|
| 4030 |
|
| 4031 | err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
|
| 4032 | if (err)
|
| 4033 | return err;
|
| 4034 |
|
| 4035 | qe->info.binder_type = binder_type;
|
| 4036 | qe->info.chain_head_change = tcf_chain_head_change_dflt;
|
| 4037 | qe->info.chain_head_change_priv = &qe->filter_chain;
|
| 4038 | qe->info.block_index = block_index;
|
| 4039 |
|
| 4040 | return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
|
| 4041 | }
|
| 4042 | EXPORT_SYMBOL(tcf_qevent_init);
|
| 4043 |
|
| 4044 | void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
|
| 4045 | {
|
| 4046 | if (qe->info.block_index)
|
| 4047 | tcf_block_put_ext(qe->block, sch, &qe->info);
|
| 4048 | }
|
| 4049 | EXPORT_SYMBOL(tcf_qevent_destroy);
|
| 4050 |
|
| 4051 | int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
|
| 4052 | struct netlink_ext_ack *extack)
|
| 4053 | {
|
| 4054 | u32 block_index;
|
| 4055 | int err;
|
| 4056 |
|
| 4057 | if (!block_index_attr)
|
| 4058 | return 0;
|
| 4059 |
|
| 4060 | err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
|
| 4061 | if (err)
|
| 4062 | return err;
|
| 4063 |
|
| 4064 |
|
| 4065 | if (block_index != qe->info.block_index) {
|
| 4066 | NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
|
| 4067 | return -EINVAL;
|
| 4068 | }
|
| 4069 |
|
| 4070 | return 0;
|
| 4071 | }
|
| 4072 | EXPORT_SYMBOL(tcf_qevent_validate_change);
|
| 4073 |
|
| 4074 | struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
|
| 4075 | struct sk_buff **to_free, int *ret)
|
| 4076 | {
|
| 4077 | struct tcf_result cl_res;
|
| 4078 | struct tcf_proto *fl;
|
| 4079 |
|
| 4080 | if (!qe->info.block_index)
|
| 4081 | return skb;
|
| 4082 |
|
| 4083 | fl = rcu_dereference_bh(qe->filter_chain);
|
| 4084 |
|
| 4085 | switch (tcf_classify_qdisc(skb, fl, &cl_res, false)) {
|
| 4086 | case TC_ACT_SHOT:
|
| 4087 | qdisc_qstats_drop(sch);
|
| 4088 | __qdisc_drop(skb, to_free);
|
| 4089 | *ret = __NET_XMIT_BYPASS;
|
| 4090 | return NULL;
|
| 4091 | case TC_ACT_STOLEN:
|
| 4092 | case TC_ACT_QUEUED:
|
| 4093 | case TC_ACT_TRAP:
|
| 4094 | __qdisc_drop(skb, to_free);
|
| 4095 | *ret = __NET_XMIT_STOLEN;
|
| 4096 | return NULL;
|
| 4097 | case TC_ACT_CONSUMED:
|
| 4098 | *ret = __NET_XMIT_STOLEN;
|
| 4099 | return NULL;
|
| 4100 | }
|
| 4101 |
|
| 4102 | return skb;
|
| 4103 | }
|
| 4104 | EXPORT_SYMBOL(tcf_qevent_handle);
|
| 4105 |
|
| 4106 | int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
|
| 4107 | {
|
| 4108 | if (!qe->info.block_index)
|
| 4109 | return 0;
|
| 4110 | return nla_put_u32(skb, attr_name, qe->info.block_index);
|
| 4111 | }
|
| 4112 | EXPORT_SYMBOL(tcf_qevent_dump);
|
| 4113 | #endif
|
| 4114 |
|
| 4115 | static __net_init int tcf_net_init(struct net *net)
|
| 4116 | {
|
| 4117 | struct tcf_net *tn = net_generic(net, tcf_net_id);
|
| 4118 |
|
| 4119 | spin_lock_init(&tn->idr_lock);
|
| 4120 | idr_init(&tn->idr);
|
| 4121 | return 0;
|
| 4122 | }
|
| 4123 |
|
| 4124 | static void __net_exit tcf_net_exit(struct net *net)
|
| 4125 | {
|
| 4126 | struct tcf_net *tn = net_generic(net, tcf_net_id);
|
| 4127 |
|
| 4128 | idr_destroy(&tn->idr);
|
| 4129 | }
|
| 4130 |
|
| 4131 | static struct pernet_operations tcf_net_ops = {
|
| 4132 | .init = tcf_net_init,
|
| 4133 | .exit = tcf_net_exit,
|
| 4134 | .id = &tcf_net_id,
|
| 4135 | .size = sizeof(struct tcf_net),
|
| 4136 | };
|
| 4137 |
|
| 4138 | static const struct rtnl_msg_handler tc_filter_rtnl_msg_handlers[] __initconst = {
|
| 4139 | {.msgtype = RTM_NEWTFILTER, .doit = tc_new_tfilter,
|
| 4140 | .flags = RTNL_FLAG_DOIT_UNLOCKED},
|
| 4141 | {.msgtype = RTM_DELTFILTER, .doit = tc_del_tfilter,
|
| 4142 | .flags = RTNL_FLAG_DOIT_UNLOCKED},
|
| 4143 | {.msgtype = RTM_GETTFILTER, .doit = tc_get_tfilter,
|
| 4144 | .dumpit = tc_dump_tfilter, .flags = RTNL_FLAG_DOIT_UNLOCKED},
|
| 4145 | {.msgtype = RTM_NEWCHAIN, .doit = tc_ctl_chain},
|
| 4146 | {.msgtype = RTM_DELCHAIN, .doit = tc_ctl_chain},
|
| 4147 | {.msgtype = RTM_GETCHAIN, .doit = tc_ctl_chain,
|
| 4148 | .dumpit = tc_dump_chain},
|
| 4149 | };
|
| 4150 |
|
| 4151 | static int __init tc_filter_init(void)
|
| 4152 | {
|
| 4153 | int err;
|
| 4154 |
|
| 4155 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
|
| 4156 | if (!tc_filter_wq)
|
| 4157 | return -ENOMEM;
|
| 4158 |
|
| 4159 | err = register_pernet_subsys(&tcf_net_ops);
|
| 4160 | if (err)
|
| 4161 | goto err_register_pernet_subsys;
|
| 4162 |
|
| 4163 | xa_init_flags(&tcf_exts_miss_cookies_xa, XA_FLAGS_ALLOC1);
|
| 4164 | rtnl_register_many(tc_filter_rtnl_msg_handlers);
|
| 4165 |
|
| 4166 | return 0;
|
| 4167 |
|
| 4168 | err_register_pernet_subsys:
|
| 4169 | destroy_workqueue(tc_filter_wq);
|
| 4170 | return err;
|
| 4171 | }
|
| 4172 |
|
| 4173 | subsys_initcall(tc_filter_init);
|