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