^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* net/sched/act_ctinfo.c netfilter ctinfo connmark actions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2019 Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/act_api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <uapi/linux/tc_act/tc_ctinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/tc_act/tc_ctinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <net/netfilter/nf_conntrack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <net/netfilter/nf_conntrack_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/netfilter/nf_conntrack_ecache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/netfilter/nf_conntrack_zones.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct tc_action_ops act_ctinfo_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static unsigned int ctinfo_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static void tcf_ctinfo_dscp_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct tcf_ctinfo_params *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct sk_buff *skb, int wlen, int proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u8 dscp, newdscp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) newdscp = (((ct->mark & cp->dscpmask) >> cp->dscpmaskshift) << 2) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ~INET_ECN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) switch (proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case NFPROTO_IPV4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) dscp = ipv4_get_dsfield(ip_hdr(skb)) & ~INET_ECN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (dscp != newdscp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (likely(!skb_try_make_writable(skb, wlen))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ipv4_change_dsfield(ip_hdr(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) INET_ECN_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) newdscp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ca->stats_dscp_set++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ca->stats_dscp_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) case NFPROTO_IPV6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & ~INET_ECN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (dscp != newdscp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (likely(!skb_try_make_writable(skb, wlen))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ipv6_change_dsfield(ipv6_hdr(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) INET_ECN_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) newdscp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ca->stats_dscp_set++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ca->stats_dscp_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void tcf_ctinfo_cpmark_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct tcf_ctinfo_params *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ca->stats_cpmark_set++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) skb->mark = ct->mark & cp->cpmarkmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int tcf_ctinfo_act(struct sk_buff *skb, const struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const struct nf_conntrack_tuple_hash *thash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct tcf_ctinfo *ca = to_ctinfo(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct nf_conntrack_tuple tuple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct nf_conntrack_zone zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) enum ip_conntrack_info ctinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct tcf_ctinfo_params *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct nf_conn *ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int proto, wlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cp = rcu_dereference_bh(ca->params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) tcf_lastuse_update(&ca->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) bstats_update(&ca->tcf_bstats, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) action = READ_ONCE(ca->tcf_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) wlen = skb_network_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) switch (skb_protocol(skb, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) wlen += sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!pskb_may_pull(skb, wlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) proto = NFPROTO_IPV4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) wlen += sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!pskb_may_pull(skb, wlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) proto = NFPROTO_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ct = nf_ct_get(skb, &ctinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!ct) { /* look harder, usually ingress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) proto, cp->net, &tuple))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) zone.id = cp->zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) zone.dir = NF_CT_DEFAULT_ZONE_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) thash = nf_conntrack_find_get(cp->net, &zone, &tuple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!thash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ct = nf_ct_tuplehash_to_ctrack(thash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (cp->mode & CTINFO_MODE_DSCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!cp->dscpstatemask || (ct->mark & cp->dscpstatemask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) tcf_ctinfo_dscp_set(ct, ca, cp, skb, wlen, proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (cp->mode & CTINFO_MODE_CPMARK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) tcf_ctinfo_cpmark_set(ct, ca, cp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (thash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) nf_ct_put(ct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static const struct nla_policy ctinfo_policy[TCA_CTINFO_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) [TCA_CTINFO_ACT] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) NLA_POLICY_EXACT_LEN(sizeof(struct tc_ctinfo)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) [TCA_CTINFO_ZONE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) [TCA_CTINFO_PARMS_DSCP_MASK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) [TCA_CTINFO_PARMS_DSCP_STATEMASK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) [TCA_CTINFO_PARMS_CPMARK_MASK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int tcf_ctinfo_init(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct nlattr *est, struct tc_action **a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int ovr, int bind, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct tcf_proto *tp, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u32 dscpmask = 0, dscpstatemask, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct nlattr *tb[TCA_CTINFO_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct tcf_ctinfo_params *cp_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct tcf_chain *goto_ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct tc_ctinfo *actparm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct tcf_ctinfo *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u8 dscpmaskshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int ret = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!nla) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) NL_SET_ERR_MSG_MOD(extack, "ctinfo requires attributes to be passed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) err = nla_parse_nested(tb, TCA_CTINFO_MAX, nla, ctinfo_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!tb[TCA_CTINFO_ACT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) NL_SET_ERR_MSG_MOD(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) "Missing required TCA_CTINFO_ACT attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) actparm = nla_data(tb[TCA_CTINFO_ACT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* do some basic validation here before dynamically allocating things */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* that we would otherwise have to clean up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dscpmask = nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* need contiguous 6 bit mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) dscpmaskshift = dscpmask ? __ffs(dscpmask) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if ((~0 & (dscpmask >> dscpmaskshift)) != 0x3f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) tb[TCA_CTINFO_PARMS_DSCP_MASK],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) "dscp mask must be 6 contiguous bits");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dscpstatemask = tb[TCA_CTINFO_PARMS_DSCP_STATEMASK] ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* mask & statemask must not overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (dscpmask & dscpstatemask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) tb[TCA_CTINFO_PARMS_DSCP_STATEMASK],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) "dscp statemask must not overlap dscp mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* done the validation:now to the actual action allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) index = actparm->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) err = tcf_idr_check_alloc(tn, &index, a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ret = tcf_idr_create(tn, index, est, a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) &act_ctinfo_ops, bind, false, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = ACT_P_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) } else if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (bind) /* don't override defaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!ovr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) err = tcf_action_check_ctrlact(actparm->action, tp, &goto_ch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) goto release_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ci = to_ctinfo(*a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) cp_new = kzalloc(sizeof(*cp_new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (unlikely(!cp_new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) cp_new->net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) cp_new->zone = tb[TCA_CTINFO_ZONE] ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) nla_get_u16(tb[TCA_CTINFO_ZONE]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (dscpmask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) cp_new->dscpmask = dscpmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) cp_new->dscpmaskshift = dscpmaskshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) cp_new->dscpstatemask = dscpstatemask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) cp_new->mode |= CTINFO_MODE_DSCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) cp_new->cpmarkmask =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) nla_get_u32(tb[TCA_CTINFO_PARMS_CPMARK_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) cp_new->mode |= CTINFO_MODE_CPMARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) spin_lock_bh(&ci->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) goto_ch = tcf_action_set_ctrlact(*a, actparm->action, goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) cp_new = rcu_replace_pointer(ci->params, cp_new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) lockdep_is_held(&ci->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) spin_unlock_bh(&ci->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (cp_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) kfree_rcu(cp_new, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) put_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) release_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) tcf_idr_release(*a, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int tcf_ctinfo_dump(struct sk_buff *skb, struct tc_action *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int bind, int ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct tcf_ctinfo *ci = to_ctinfo(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct tc_ctinfo opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .index = ci->tcf_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .refcnt = refcount_read(&ci->tcf_refcnt) - ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned char *b = skb_tail_pointer(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct tcf_ctinfo_params *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct tcf_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock_bh(&ci->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) cp = rcu_dereference_protected(ci->params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) lockdep_is_held(&ci->tcf_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) tcf_tm_dump(&t, &ci->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (nla_put_64bit(skb, TCA_CTINFO_TM, sizeof(t), &t, TCA_CTINFO_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) opt.action = ci->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (nla_put(skb, TCA_CTINFO_ACT, sizeof(opt), &opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (nla_put_u16(skb, TCA_CTINFO_ZONE, cp->zone))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (cp->mode & CTINFO_MODE_DSCP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) cp->dscpmask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_STATEMASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) cp->dscpstatemask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (cp->mode & CTINFO_MODE_CPMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (nla_put_u32(skb, TCA_CTINFO_PARMS_CPMARK_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cp->cpmarkmask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ci->stats_dscp_set, TCA_CTINFO_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ci->stats_dscp_error, TCA_CTINFO_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_CPMARK_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ci->stats_cpmark_set, TCA_CTINFO_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) spin_unlock_bh(&ci->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) spin_unlock_bh(&ci->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) nlmsg_trim(skb, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static int tcf_ctinfo_walker(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) struct netlink_callback *cb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) const struct tc_action_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return tcf_generic_walker(tn, skb, cb, type, ops, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int tcf_ctinfo_search(struct net *net, struct tc_action **a, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return tcf_idr_search(tn, a, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static void tcf_ctinfo_cleanup(struct tc_action *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct tcf_ctinfo *ci = to_ctinfo(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct tcf_ctinfo_params *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cp = rcu_dereference_protected(ci->params, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) kfree_rcu(cp, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static struct tc_action_ops act_ctinfo_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .kind = "ctinfo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .id = TCA_ID_CTINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) .act = tcf_ctinfo_act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) .dump = tcf_ctinfo_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) .init = tcf_ctinfo_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) .cleanup= tcf_ctinfo_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) .walk = tcf_ctinfo_walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) .lookup = tcf_ctinfo_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) .size = sizeof(struct tcf_ctinfo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static __net_init int ctinfo_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct tc_action_net *tn = net_generic(net, ctinfo_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return tc_action_net_init(net, tn, &act_ctinfo_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static void __net_exit ctinfo_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) tc_action_net_exit(net_list, ctinfo_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static struct pernet_operations ctinfo_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .init = ctinfo_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .exit_batch = ctinfo_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .id = &ctinfo_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .size = sizeof(struct tc_action_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int __init ctinfo_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return tcf_register_action(&act_ctinfo_ops, &ctinfo_net_ops);
^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 __exit ctinfo_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) tcf_unregister_action(&act_ctinfo_ops, &ctinfo_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) module_init(ctinfo_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) module_exit(ctinfo_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) MODULE_AUTHOR("Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) MODULE_DESCRIPTION("Connection tracking mark actions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) MODULE_LICENSE("GPL");