Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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_flower.c		Flower classifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/rhashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/mpls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <net/sch_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <net/flow_dissector.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <net/geneve.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <net/vxlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <net/erspan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <net/dst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <uapi/linux/netfilter/nf_conntrack_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define TCA_FLOWER_KEY_CT_FLAGS_MAX \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 		((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define TCA_FLOWER_KEY_CT_FLAGS_MASK \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 		(TCA_FLOWER_KEY_CT_FLAGS_MAX - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) struct fl_flow_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	struct flow_dissector_key_meta meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct flow_dissector_key_control control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	struct flow_dissector_key_control enc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	struct flow_dissector_key_basic basic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	struct flow_dissector_key_eth_addrs eth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct flow_dissector_key_vlan vlan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct flow_dissector_key_vlan cvlan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		struct flow_dissector_key_ipv4_addrs ipv4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		struct flow_dissector_key_ipv6_addrs ipv6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	struct flow_dissector_key_ports tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	struct flow_dissector_key_icmp icmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	struct flow_dissector_key_arp arp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	struct flow_dissector_key_keyid enc_key_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		struct flow_dissector_key_ipv4_addrs enc_ipv4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		struct flow_dissector_key_ipv6_addrs enc_ipv6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	struct flow_dissector_key_ports enc_tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	struct flow_dissector_key_mpls mpls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	struct flow_dissector_key_tcp tcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	struct flow_dissector_key_ip ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	struct flow_dissector_key_ip enc_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	struct flow_dissector_key_enc_opts enc_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		struct flow_dissector_key_ports tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 			struct flow_dissector_key_ports tp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 			struct flow_dissector_key_ports tp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	} tp_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct flow_dissector_key_ct ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	struct flow_dissector_key_hash hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) struct fl_flow_mask_range {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	unsigned short int start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	unsigned short int end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) struct fl_flow_mask {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct fl_flow_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct fl_flow_mask_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	struct rhash_head ht_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct rhashtable ht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	struct rhashtable_params filter_ht_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct flow_dissector dissector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct list_head filters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	refcount_t refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) struct fl_flow_tmplt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct fl_flow_key dummy_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct fl_flow_key mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct flow_dissector dissector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct tcf_chain *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) struct cls_fl_head {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	struct rhashtable ht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	spinlock_t masks_lock; /* Protect masks list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct list_head masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct list_head hw_filters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct idr handle_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) struct cls_fl_filter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	struct fl_flow_mask *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	struct rhash_head ht_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct fl_flow_key mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct tcf_exts exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct tcf_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	struct fl_flow_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	struct list_head hw_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	u32 handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	u32 in_hw_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct net_device *hw_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	/* Flower classifier is unlocked, which means that its reference counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	 * can be changed concurrently without any kind of external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	 * synchronization. Use atomic reference counter to be concurrency-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	refcount_t refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	bool deleted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) static const struct rhashtable_params mask_ht_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	.key_offset = offsetof(struct fl_flow_mask, key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	.key_len = sizeof(struct fl_flow_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	.head_offset = offsetof(struct fl_flow_mask, ht_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	.automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	return mask->range.end - mask->range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) static void fl_mask_update_range(struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	const u8 *bytes = (const u8 *) &mask->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	size_t size = sizeof(mask->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	size_t i, first = 0, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		if (bytes[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 			first = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	last = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	for (i = size - 1; i != first; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		if (bytes[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			last = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	mask->range.start = rounddown(first, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	mask->range.end = roundup(last + 1, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static void *fl_key_get_start(struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			      const struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	return (u8 *) key + mask->range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			      struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	const long *lkey = fl_key_get_start(key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	const long *lmask = fl_key_get_start(&mask->key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	long *lmkey = fl_key_get_start(mkey, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		*lmkey++ = *lkey++ & *lmask++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			       struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	const long *lmask = fl_key_get_start(&mask->key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	const long *ltmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (!tmplt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	ltmplt = fl_key_get_start(&tmplt->mask, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		if (~*ltmplt++ & *lmask++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) static void fl_clear_masked_range(struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				  struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 				  struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 				  struct fl_flow_key *mkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	u16 min_mask, max_mask, min_val, max_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	min_val = ntohs(filter->key.tp_range.tp_min.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	max_val = ntohs(filter->key.tp_range.tp_max.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	if (min_mask && max_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		if (ntohs(key->tp_range.tp.dst) < min_val ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		    ntohs(key->tp_range.tp.dst) > max_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		/* skb does not have min and max values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return true;
^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 fl_range_port_src_cmp(struct cls_fl_filter *filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				  struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 				  struct fl_flow_key *mkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	u16 min_mask, max_mask, min_val, max_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	min_mask = ntohs(filter->mask->key.tp_range.tp_min.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	max_mask = ntohs(filter->mask->key.tp_range.tp_max.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	min_val = ntohs(filter->key.tp_range.tp_min.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	max_val = ntohs(filter->key.tp_range.tp_max.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	if (min_mask && max_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		if (ntohs(key->tp_range.tp.src) < min_val ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		    ntohs(key->tp_range.tp.src) > max_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		/* skb does not have min and max values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	return true;
^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) static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 					 struct fl_flow_key *mkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 				      mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 					     struct fl_flow_key *mkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 					     struct fl_flow_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	struct cls_fl_filter *filter, *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	list_for_each_entry_rcu(filter, &mask->filters, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		if (!fl_range_port_dst_cmp(filter, key, mkey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		if (!fl_range_port_src_cmp(filter, key, mkey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		f = __fl_lookup(mask, mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		if (f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	return NULL;
^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 noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct fl_flow_key mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	fl_set_masked_key(&mkey, key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		return fl_lookup_range(mask, &mkey, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	return __fl_lookup(mask, &mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) static u16 fl_ct_info_to_flower_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	[IP_CT_ESTABLISHED] =		TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 					TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	[IP_CT_RELATED] =		TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 					TCA_FLOWER_KEY_CT_FLAGS_RELATED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	[IP_CT_ESTABLISHED_REPLY] =	TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 					TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	[IP_CT_RELATED_REPLY] =		TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 					TCA_FLOWER_KEY_CT_FLAGS_RELATED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	[IP_CT_NEW] =			TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 					TCA_FLOWER_KEY_CT_FLAGS_NEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		       struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct cls_fl_head *head = rcu_dereference_bh(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct fl_flow_key skb_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	struct fl_flow_mask *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	struct cls_fl_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	list_for_each_entry_rcu(mask, &head->masks, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		fl_clear_masked_range(&skb_key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		/* skb_flow_dissect() does not set n_proto in case an unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		 * protocol, so do it rather here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		skb_key.basic.n_proto = skb_protocol(skb, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 				    fl_ct_info_to_flower_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 				    ARRAY_SIZE(fl_ct_info_to_flower_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		f = fl_mask_lookup(mask, &skb_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		if (f && !tc_skip_sw(f->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			*res = f->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 			return tcf_exts_exec(skb, &f->exts, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	return -1;
^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) static int fl_init(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	struct cls_fl_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	head = kzalloc(sizeof(*head), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	spin_lock_init(&head->masks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	INIT_LIST_HEAD_RCU(&head->masks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	INIT_LIST_HEAD(&head->hw_filters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	rcu_assign_pointer(tp->root, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	idr_init(&head->handle_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	return rhashtable_init(&head->ht, &mask_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	/* temporary masks don't have their filters list and ht initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (mask_init_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		WARN_ON(!list_empty(&mask->filters));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		rhashtable_destroy(&mask->ht);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	kfree(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static void fl_mask_free_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	struct fl_flow_mask *mask = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 						 struct fl_flow_mask, rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	fl_mask_free(mask, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) static void fl_uninit_mask_free_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct fl_flow_mask *mask = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 						 struct fl_flow_mask, rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	fl_mask_free(mask, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (!refcount_dec_and_test(&mask->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	spin_lock(&head->masks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	list_del_rcu(&mask->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	spin_unlock(&head->masks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	tcf_queue_work(&mask->rwork, fl_mask_free_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	/* Flower classifier only changes root pointer during init and destroy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	 * Users must obtain reference to tcf_proto instance before calling its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	 * API, so tp->root pointer is protected from concurrent call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	 * fl_destroy() by reference counting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return rcu_dereference_raw(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static void __fl_destroy_filter(struct cls_fl_filter *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	tcf_exts_destroy(&f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	tcf_exts_put_net(&f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	kfree(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) static void fl_destroy_filter_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	struct cls_fl_filter *f = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 					struct cls_fl_filter, rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	__fl_destroy_filter(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 				 bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	cls_flower.command = FLOW_CLS_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	cls_flower.cookie = (unsigned long) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			    &f->flags, &f->in_hw_count, rtnl_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) static int fl_hw_replace_filter(struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 				struct cls_fl_filter *f, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	bool skip_sw = tc_skip_sw(f->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (!cls_flower.rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	cls_flower.command = FLOW_CLS_REPLACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	cls_flower.cookie = (unsigned long) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	cls_flower.rule->match.dissector = &f->mask->dissector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	cls_flower.rule->match.mask = &f->mask->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	cls_flower.rule->match.key = &f->mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	cls_flower.classid = f->res.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		kfree(cls_flower.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		if (skip_sw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			      skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	tc_cleanup_flow_action(&cls_flower.rule->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	kfree(cls_flower.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			       bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	cls_flower.command = FLOW_CLS_STATS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	cls_flower.cookie = (unsigned long) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	cls_flower.classid = f->res.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			 rtnl_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			      cls_flower.stats.pkts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			      cls_flower.stats.drops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			      cls_flower.stats.lastused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			      cls_flower.stats.used_hw_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			      cls_flower.stats.used_hw_stats_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) static void __fl_put(struct cls_fl_filter *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (!refcount_dec_and_test(&f->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (tcf_exts_get_net(&f->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		tcf_queue_work(&f->rwork, fl_destroy_filter_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		__fl_destroy_filter(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	struct cls_fl_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	f = idr_find(&head->handle_idr, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (f && !refcount_inc_not_zero(&f->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		       bool *last, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		       struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	*last = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (f->deleted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	f->deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			       f->mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	idr_remove(&head->handle_idr, f->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	list_del_rcu(&f->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	*last = fl_mask_put(head, f->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	if (!tc_skip_hw(f->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		fl_hw_destroy_filter(tp, f, rtnl_held, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	tcf_unbind_filter(tp, &f->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) static void fl_destroy_sleepable(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct cls_fl_head *head = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 						struct cls_fl_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 						rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	rhashtable_destroy(&head->ht);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	kfree(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		       struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	struct fl_flow_mask *mask, *next_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	struct cls_fl_filter *f, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	bool last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		list_for_each_entry_safe(f, next, &mask->filters, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			__fl_delete(tp, f, &last, rtnl_held, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			if (last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	idr_destroy(&head->handle_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	tcf_queue_work(&head->rwork, fl_destroy_sleepable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) static void fl_put(struct tcf_proto *tp, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	struct cls_fl_filter *f = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) static void *fl_get(struct tcf_proto *tp, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	return __fl_get(head, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	[TCA_FLOWER_UNSPEC]		= { .type = NLA_UNSPEC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	[TCA_FLOWER_CLASSID]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	[TCA_FLOWER_INDEV]		= { .type = NLA_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 					    .len = IFNAMSIZ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	[TCA_FLOWER_KEY_ETH_DST]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	[TCA_FLOWER_KEY_ETH_DST_MASK]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	[TCA_FLOWER_KEY_ETH_SRC]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	[TCA_FLOWER_KEY_ETH_SRC_MASK]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	[TCA_FLOWER_KEY_ETH_TYPE]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	[TCA_FLOWER_KEY_IP_PROTO]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	[TCA_FLOWER_KEY_IPV4_SRC]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	[TCA_FLOWER_KEY_IPV4_SRC_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	[TCA_FLOWER_KEY_IPV4_DST]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	[TCA_FLOWER_KEY_IPV4_DST_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	[TCA_FLOWER_KEY_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	[TCA_FLOWER_KEY_IPV6_SRC_MASK]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	[TCA_FLOWER_KEY_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	[TCA_FLOWER_KEY_IPV6_DST_MASK]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	[TCA_FLOWER_KEY_TCP_SRC]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	[TCA_FLOWER_KEY_TCP_DST]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	[TCA_FLOWER_KEY_UDP_SRC]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	[TCA_FLOWER_KEY_UDP_DST]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	[TCA_FLOWER_KEY_VLAN_ID]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	[TCA_FLOWER_KEY_VLAN_PRIO]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	[TCA_FLOWER_KEY_VLAN_ETH_TYPE]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	[TCA_FLOWER_KEY_ENC_KEY_ID]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	[TCA_FLOWER_KEY_ENC_IPV4_SRC]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	[TCA_FLOWER_KEY_ENC_IPV4_DST]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	[TCA_FLOWER_KEY_ENC_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	[TCA_FLOWER_KEY_ENC_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	[TCA_FLOWER_KEY_TCP_SRC_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	[TCA_FLOWER_KEY_TCP_DST_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	[TCA_FLOWER_KEY_UDP_SRC_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	[TCA_FLOWER_KEY_UDP_DST_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	[TCA_FLOWER_KEY_SCTP_SRC_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	[TCA_FLOWER_KEY_SCTP_DST_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	[TCA_FLOWER_KEY_SCTP_SRC]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	[TCA_FLOWER_KEY_SCTP_DST]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	[TCA_FLOWER_KEY_FLAGS]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	[TCA_FLOWER_KEY_FLAGS_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	[TCA_FLOWER_KEY_ICMPV4_TYPE]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	[TCA_FLOWER_KEY_ICMPV4_CODE]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	[TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	[TCA_FLOWER_KEY_ICMPV6_TYPE]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	[TCA_FLOWER_KEY_ICMPV6_CODE]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	[TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	[TCA_FLOWER_KEY_ARP_SIP]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	[TCA_FLOWER_KEY_ARP_SIP_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	[TCA_FLOWER_KEY_ARP_TIP]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	[TCA_FLOWER_KEY_ARP_TIP_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	[TCA_FLOWER_KEY_ARP_OP]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	[TCA_FLOWER_KEY_ARP_OP_MASK]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	[TCA_FLOWER_KEY_ARP_SHA]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	[TCA_FLOWER_KEY_ARP_SHA_MASK]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	[TCA_FLOWER_KEY_ARP_THA]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	[TCA_FLOWER_KEY_ARP_THA_MASK]	= { .len = ETH_ALEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	[TCA_FLOWER_KEY_MPLS_TTL]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	[TCA_FLOWER_KEY_MPLS_BOS]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	[TCA_FLOWER_KEY_MPLS_TC]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	[TCA_FLOWER_KEY_MPLS_LABEL]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	[TCA_FLOWER_KEY_MPLS_OPTS]	= { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	[TCA_FLOWER_KEY_TCP_FLAGS]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	[TCA_FLOWER_KEY_TCP_FLAGS_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	[TCA_FLOWER_KEY_IP_TOS]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	[TCA_FLOWER_KEY_IP_TOS_MASK]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	[TCA_FLOWER_KEY_IP_TTL]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	[TCA_FLOWER_KEY_IP_TTL_MASK]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	[TCA_FLOWER_KEY_CVLAN_ID]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	[TCA_FLOWER_KEY_CVLAN_PRIO]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	[TCA_FLOWER_KEY_ENC_IP_TOS]	= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	[TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	[TCA_FLOWER_KEY_ENC_IP_TTL]	 = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	[TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	[TCA_FLOWER_KEY_ENC_OPTS]	= { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	[TCA_FLOWER_KEY_ENC_OPTS_MASK]	= { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	[TCA_FLOWER_KEY_CT_STATE]	=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	[TCA_FLOWER_KEY_CT_STATE_MASK]	=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	[TCA_FLOWER_KEY_CT_ZONE]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	[TCA_FLOWER_KEY_CT_ZONE_MASK]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	[TCA_FLOWER_KEY_CT_MARK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	[TCA_FLOWER_KEY_CT_MARK_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	[TCA_FLOWER_KEY_CT_LABELS]	= { .type = NLA_BINARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 					    .len = 128 / BITS_PER_BYTE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	[TCA_FLOWER_KEY_CT_LABELS_MASK]	= { .type = NLA_BINARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 					    .len = 128 / BITS_PER_BYTE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	[TCA_FLOWER_FLAGS]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	[TCA_FLOWER_KEY_HASH]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	[TCA_FLOWER_KEY_HASH_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	[TCA_FLOWER_KEY_ENC_OPTS_UNSPEC]        = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		.strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]        = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	[TCA_FLOWER_KEY_ENC_OPTS_VXLAN]         = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN]        = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]      = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]       = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]       = { .type = NLA_BINARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 						       .len = 128 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]         = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) static const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]        = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]      = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]        = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]       = { .type = NLA_U8 },
^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 const struct nla_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]    = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]      = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]      = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]       = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]    = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) static void fl_set_key_val(struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			   void *val, int val_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			   void *mask, int mask_type, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	if (!tb[val_type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	nla_memcpy(val, tb[val_type], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		memset(mask, 0xff, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		nla_memcpy(mask, tb[mask_type], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				 struct fl_flow_key *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				 struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	fl_set_key_val(tb, &key->tp_range.tp_min.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		       TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	fl_set_key_val(tb, &key->tp_range.tp_max.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		       TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	fl_set_key_val(tb, &key->tp_range.tp_min.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		       TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	fl_set_key_val(tb, &key->tp_range.tp_max.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		       TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	    ntohs(key->tp_range.tp_max.dst) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	    ntohs(key->tp_range.tp_min.dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 				    tb[TCA_FLOWER_KEY_PORT_DST_MIN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 				    "Invalid destination port range (min must be strictly smaller than max)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	    ntohs(key->tp_range.tp_max.src) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	    ntohs(key->tp_range.tp_min.src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 				    tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 				    "Invalid source port range (min must be strictly smaller than max)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	return 0;
^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) static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			       struct flow_dissector_key_mpls *key_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			       struct flow_dissector_key_mpls *key_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			       struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct flow_dissector_mpls_lse *lse_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct flow_dissector_mpls_lse *lse_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	u8 lse_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	u8 depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			       mpls_stack_entry_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	/* LSE depth starts at 1, for consistency with terminology used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 				    tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 				    "Invalid MPLS depth");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	lse_index = depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	dissector_set_mpls_lse(key_val, lse_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	dissector_set_mpls_lse(key_mask, lse_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	lse_val = &key_val->ls[lse_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	lse_mask = &key_mask->ls[lse_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		lse_mask->mpls_ttl = MPLS_TTL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		if (bos & ~MPLS_BOS_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 					    tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 					    "Bottom Of Stack (BOS) must be 0 or 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		lse_val->mpls_bos = bos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		lse_mask->mpls_bos = MPLS_BOS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		if (tc & ~MPLS_TC_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 					    tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 					    "Traffic Class (TC) must be between 0 and 7");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		lse_val->mpls_tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		lse_mask->mpls_tc = MPLS_TC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		if (label & ~MPLS_LABEL_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 					    tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 					    "Label must be between 0 and 1048575");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		lse_val->mpls_label = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		lse_mask->mpls_label = MPLS_LABEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				struct flow_dissector_key_mpls *key_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				struct flow_dissector_key_mpls *key_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 				struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct nlattr *nla_lse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 				    "NLA_F_NESTED is missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			NL_SET_ERR_MSG_ATTR(extack, nla_lse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 					    "Invalid MPLS option type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	if (rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			       "Bytes leftover after parsing MPLS options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) static int fl_set_key_mpls(struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 			   struct flow_dissector_key_mpls *key_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			   struct flow_dissector_key_mpls *key_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			   struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct flow_dissector_mpls_lse *lse_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct flow_dissector_mpls_lse *lse_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		    tb[TCA_FLOWER_KEY_MPLS_BOS] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		    tb[TCA_FLOWER_KEY_MPLS_TC] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		    tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 					    tb[TCA_FLOWER_KEY_MPLS_OPTS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 					    "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 					    key_val, key_mask, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	lse_val = &key_val->ls[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	lse_mask = &key_mask->ls[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		lse_mask->mpls_ttl = MPLS_TTL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		dissector_set_mpls_lse(key_val, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		dissector_set_mpls_lse(key_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		if (bos & ~MPLS_BOS_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 					    tb[TCA_FLOWER_KEY_MPLS_BOS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 					    "Bottom Of Stack (BOS) must be 0 or 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		lse_val->mpls_bos = bos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		lse_mask->mpls_bos = MPLS_BOS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		dissector_set_mpls_lse(key_val, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		dissector_set_mpls_lse(key_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		if (tc & ~MPLS_TC_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 					    tb[TCA_FLOWER_KEY_MPLS_TC],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 					    "Traffic Class (TC) must be between 0 and 7");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		lse_val->mpls_tc = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		lse_mask->mpls_tc = MPLS_TC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		dissector_set_mpls_lse(key_val, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		dissector_set_mpls_lse(key_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		if (label & ~MPLS_LABEL_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			NL_SET_ERR_MSG_ATTR(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 					    tb[TCA_FLOWER_KEY_MPLS_LABEL],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 					    "Label must be between 0 and 1048575");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		lse_val->mpls_label = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		lse_mask->mpls_label = MPLS_LABEL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		dissector_set_mpls_lse(key_val, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		dissector_set_mpls_lse(key_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) static void fl_set_key_vlan(struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			    __be16 ethertype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			    int vlan_id_key, int vlan_prio_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			    struct flow_dissector_key_vlan *key_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			    struct flow_dissector_key_vlan *key_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) #define VLAN_PRIORITY_MASK	0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (tb[vlan_id_key]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		key_val->vlan_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		key_mask->vlan_id = VLAN_VID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (tb[vlan_prio_key]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		key_val->vlan_priority =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			nla_get_u8(tb[vlan_prio_key]) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			VLAN_PRIORITY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		key_mask->vlan_priority = VLAN_PRIORITY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	key_val->vlan_tpid = ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	key_mask->vlan_tpid = cpu_to_be16(~0);
^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) static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			    u32 *dissector_key, u32 *dissector_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			    u32 flower_flag_bit, u32 dissector_flag_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (flower_mask & flower_flag_bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		*dissector_mask |= dissector_flag_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		if (flower_key & flower_flag_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			*dissector_key |= dissector_flag_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			    u32 *flags_mask, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	u32 key, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	/* mask is mandatory for flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		NL_SET_ERR_MSG(extack, "Missing flags mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	*flags_key  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	*flags_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	fl_set_key_flag(key, mask, flags_key, flags_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	fl_set_key_flag(key, mask, flags_key, flags_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			FLOW_DIS_FIRST_FRAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	return 0;
^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) static void fl_set_key_ip(struct nlattr **tb, bool encap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			  struct flow_dissector_key_ip *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			  struct flow_dissector_key_ip *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			     int depth, int option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	struct nlattr *class = NULL, *type = NULL, *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct geneve_opt *opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	int err, data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if (option_len > sizeof(struct geneve_opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		data_len = option_len - sizeof(struct geneve_opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	memset(opt, 0xff, option_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	opt->length = data_len / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	opt->r1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	opt->r2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	opt->r3 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	/* If no mask has been prodived we assume an exact match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		return sizeof(struct geneve_opt) + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	err = nla_parse_nested_deprecated(tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 					  TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 					  nla, geneve_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	/* We are not allowed to omit any of CLASS, TYPE or DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	 * fields from the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	if (!option_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	    (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	/* Omitting any of CLASS, TYPE or DATA fields is allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	 * for the mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		int new_len = key->enc_opts.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		data_len = nla_len(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		if (data_len < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		if (data_len % 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		new_len += sizeof(struct geneve_opt) + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		opt->length = data_len / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		memcpy(opt->opt_data, nla_data(data), data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		opt->opt_class = nla_get_be16(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		opt->type = nla_get_u8(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	return sizeof(struct geneve_opt) + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			    int depth, int option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	struct vxlan_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	memset(md, 0xff, sizeof(*md));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		return sizeof(*md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			       vxlan_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		return -EINVAL;
^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) 	if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		md->gbp &= VXLAN_GBP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	return sizeof(*md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			     int depth, int option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	struct erspan_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	memset(md, 0xff, sizeof(*md));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	md->version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		return sizeof(*md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			       erspan_opt_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (md->version == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			memset(&md->u, 0x00, sizeof(md->u));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			md->u.index = nla_get_be32(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	} else if (md->version == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 				    !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			md->u.md2.dir = nla_get_u8(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			set_hwid(&md->u.md2, nla_get_u8(nla));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	return sizeof(*md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			  struct fl_flow_key *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			  struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	int err, option_len, key_depth, msk_depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 					     TCA_FLOWER_KEY_ENC_OPTS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 					     enc_opts_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 						     TCA_FLOWER_KEY_ENC_OPTS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 						     enc_opts_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		if (!nla_ok(nla_opt_msk, msk_depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	nla_for_each_attr(nla_opt_key, nla_enc_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			  nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		switch (nla_type(nla_opt_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			if (key->enc_opts.dst_opt_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			    key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 				NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			option_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 			key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 			option_len = fl_set_geneve_opt(nla_opt_key, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 						       key_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 						       extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			key->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 			/* At the same time we need to parse through the mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			 * in order to verify exact and mask attribute lengths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			option_len = fl_set_geneve_opt(nla_opt_msk, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 						       msk_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 						       extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			mask->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			if (key->enc_opts.len != mask->enc_opts.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 				NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			if (key->enc_opts.dst_opt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 				NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			option_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			option_len = fl_set_vxlan_opt(nla_opt_key, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 						      key_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 						      extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			key->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			/* At the same time we need to parse through the mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			 * in order to verify exact and mask attribute lengths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 						      msk_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 						      extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			mask->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			if (key->enc_opts.len != mask->enc_opts.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 				NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			if (key->enc_opts.dst_opt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 				NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			option_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			option_len = fl_set_erspan_opt(nla_opt_key, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 						       key_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 						       extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			key->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			/* At the same time we need to parse through the mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			 * in order to verify exact and mask attribute lengths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			option_len = fl_set_erspan_opt(nla_opt_msk, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 						       msk_depth, option_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 						       extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			if (option_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				return option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			mask->enc_opts.len += option_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			if (key->enc_opts.len != mask->enc_opts.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 				NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		if (!msk_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		if (!nla_ok(nla_opt_msk, msk_depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) static int fl_validate_ct_state(u16 state, struct nlattr *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 				struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		NL_SET_ERR_MSG_ATTR(extack, tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 				    "no trk, so no other flag can be set");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	    state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		NL_SET_ERR_MSG_ATTR(extack, tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 				    "new and est are mutually exclusive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) static int fl_set_key_ct(struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			 struct flow_dissector_key_ct *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			 struct flow_dissector_key_ct *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			 struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	if (tb[TCA_FLOWER_KEY_CT_STATE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			       &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			       sizeof(key->ct_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		err = fl_validate_ct_state(key->ct_state & mask->ct_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 					   tb[TCA_FLOWER_KEY_CT_STATE_MASK],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 					   extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			       &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 			       sizeof(key->ct_zone));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (tb[TCA_FLOWER_KEY_CT_MARK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			       &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 			       sizeof(key->ct_mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			       mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			       sizeof(key->ct_labels));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static int fl_set_key(struct net *net, struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		      struct fl_flow_key *key, struct fl_flow_key *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		      struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	__be16 ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	if (tb[TCA_FLOWER_INDEV]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		key->meta.ingress_ifindex = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		mask->meta.ingress_ifindex = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		       mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		       sizeof(key->eth.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		       mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		       sizeof(key->eth.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		if (eth_type_vlan(ethertype)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 					TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 					&mask->vlan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 			if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 				if (eth_type_vlan(ethertype)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 					fl_set_key_vlan(tb, ethertype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 							TCA_FLOWER_KEY_CVLAN_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 							TCA_FLOWER_KEY_CVLAN_PRIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 							&key->cvlan, &mask->cvlan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 					fl_set_key_val(tb, &key->basic.n_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 						       TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 						       &mask->basic.n_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 						       TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 						       sizeof(key->basic.n_proto));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 					key->basic.n_proto = ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 					mask->basic.n_proto = cpu_to_be16(~0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 			key->basic.n_proto = ethertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			mask->basic.n_proto = cpu_to_be16(~0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	if (key->basic.n_proto == htons(ETH_P_IP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	    key->basic.n_proto == htons(ETH_P_IPV6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			       &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			       sizeof(key->basic.ip_proto));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		fl_set_key_ip(tb, false, &key->ip, &mask->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		mask->control.addr_type = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			       &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			       sizeof(key->ipv4.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			       &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			       sizeof(key->ipv4.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	} else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		mask->control.addr_type = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			       &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 			       sizeof(key->ipv6.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 			       &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			       sizeof(key->ipv6.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	if (key->basic.ip_proto == IPPROTO_TCP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			       &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			       sizeof(key->tp.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 			       &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 			       sizeof(key->tp.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			       &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			       sizeof(key->tcp.flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	} else if (key->basic.ip_proto == IPPROTO_UDP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			       &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			       sizeof(key->tp.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			       &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 			       sizeof(key->tp.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	} else if (key->basic.ip_proto == IPPROTO_SCTP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			       &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			       sizeof(key->tp.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 			       &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 			       sizeof(key->tp.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	} else if (key->basic.n_proto == htons(ETH_P_IP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		   key->basic.ip_proto == IPPROTO_ICMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			       &mask->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 			       TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 			       sizeof(key->icmp.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			       &mask->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			       TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			       sizeof(key->icmp.code));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	} else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		   key->basic.ip_proto == IPPROTO_ICMPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			       &mask->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			       TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 			       sizeof(key->icmp.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			       &mask->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			       TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			       sizeof(key->icmp.code));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		   key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		   key->basic.n_proto == htons(ETH_P_RARP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			       &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			       sizeof(key->arp.sip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			       &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			       sizeof(key->arp.tip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			       &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 			       sizeof(key->arp.op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 			       mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 			       sizeof(key->arp.sha));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 			       mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			       sizeof(key->arp.tha));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	if (key->basic.ip_proto == IPPROTO_TCP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	    key->basic.ip_proto == IPPROTO_UDP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	    key->basic.ip_proto == IPPROTO_SCTP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		ret = fl_set_key_port_range(tb, key, mask, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	    tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		mask->enc_control.addr_type = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		fl_set_key_val(tb, &key->enc_ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			       TCA_FLOWER_KEY_ENC_IPV4_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 			       &mask->enc_ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			       TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			       sizeof(key->enc_ipv4.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		fl_set_key_val(tb, &key->enc_ipv4.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			       TCA_FLOWER_KEY_ENC_IPV4_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			       &mask->enc_ipv4.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			       TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			       sizeof(key->enc_ipv4.dst));
^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) 	if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	    tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		mask->enc_control.addr_type = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		fl_set_key_val(tb, &key->enc_ipv6.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 			       TCA_FLOWER_KEY_ENC_IPV6_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 			       &mask->enc_ipv6.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			       TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			       sizeof(key->enc_ipv6.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		fl_set_key_val(tb, &key->enc_ipv6.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			       TCA_FLOWER_KEY_ENC_IPV6_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			       &mask->enc_ipv6.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			       TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			       sizeof(key->enc_ipv6.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		       &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		       sizeof(key->enc_key_id.keyid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		       &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		       sizeof(key->enc_tp.src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		       &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		       sizeof(key->enc_tp.dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		       &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		       sizeof(key->hash.hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		ret = fl_set_enc_opt(tb, key, mask, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	if (tb[TCA_FLOWER_KEY_FLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		ret = fl_set_key_flags(tb, &key->control.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 				       &mask->control.flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) static void fl_mask_copy(struct fl_flow_mask *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 			 struct fl_flow_mask *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	const void *psrc = fl_key_get_start(&src->key, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	void *pdst = fl_key_get_start(&dst->key, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	memcpy(pdst, psrc, fl_mask_range(src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	dst->range = src->range;
^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) static const struct rhashtable_params fl_ht_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	.key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	.head_offset = offsetof(struct cls_fl_filter, ht_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	.automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	mask->filter_ht_params = fl_ht_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	mask->filter_ht_params.key_len = fl_mask_range(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	mask->filter_ht_params.key_offset += mask->range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	return rhashtable_init(&mask->ht, &mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) #define FL_KEY_IS_MASKED(mask, member)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		   0, FL_KEY_MEMBER_SIZE(member))				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) #define FL_KEY_SET(keys, cnt, id, member)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	do {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		keys[cnt].key_id = id;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		cnt++;								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	} while(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	do {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		if (FL_KEY_IS_MASKED(mask, member))				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			FL_KEY_SET(keys, cnt, id, member);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	} while(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) static void fl_init_dissector(struct flow_dissector *dissector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			      struct fl_flow_key *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	size_t cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			     FLOW_DISSECTOR_KEY_META, meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			     FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			     FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			     FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			     FLOW_DISSECTOR_KEY_PORTS, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			     FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			     FLOW_DISSECTOR_KEY_IP, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			     FLOW_DISSECTOR_KEY_TCP, tcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			     FLOW_DISSECTOR_KEY_ICMP, icmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			     FLOW_DISSECTOR_KEY_ARP, arp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 			     FLOW_DISSECTOR_KEY_MPLS, mpls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 			     FLOW_DISSECTOR_KEY_VLAN, vlan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 			     FLOW_DISSECTOR_KEY_CVLAN, cvlan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 			     FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 			     FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 			     FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	    FL_KEY_IS_MASKED(mask, enc_ipv6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 			   enc_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 			     FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			     FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			     FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 			     FLOW_DISSECTOR_KEY_CT, ct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			     FLOW_DISSECTOR_KEY_HASH, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	skb_flow_dissector_init(dissector, keys, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 					       struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	struct fl_flow_mask *newmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	if (!newmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	fl_mask_copy(newmask, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if ((newmask->key.tp_range.tp_min.dst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	     newmask->key.tp_range.tp_max.dst) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	    (newmask->key.tp_range.tp_min.src &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	     newmask->key.tp_range.tp_max.src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	err = fl_init_mask_hashtable(newmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	fl_init_dissector(&newmask->dissector, &newmask->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	INIT_LIST_HEAD_RCU(&newmask->filters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	refcount_set(&newmask->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 				      &newmask->ht_node, mask_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		goto errout_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	spin_lock(&head->masks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	list_add_tail_rcu(&newmask->list, &head->masks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	spin_unlock(&head->masks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	return newmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) errout_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	rhashtable_destroy(&newmask->ht);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) errout_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	kfree(newmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) static int fl_check_assign_mask(struct cls_fl_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 				struct cls_fl_filter *fnew,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 				struct cls_fl_filter *fold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 				struct fl_flow_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	struct fl_flow_mask *newmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	/* Insert mask as temporary node to prevent concurrent creation of mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	 * with same key. Any concurrent lookups with same key will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	 * -EAGAIN because mask's refcnt is zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 						       &mask->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 						       mask_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	if (!fnew->mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		if (fold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			goto errout_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		newmask = fl_create_new_mask(head, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		if (IS_ERR(newmask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 			ret = PTR_ERR(newmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			goto errout_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		fnew->mask = newmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	} else if (IS_ERR(fnew->mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		ret = PTR_ERR(fnew->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	} else if (fold && fold->mask != fnew->mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	} else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		/* Mask was deleted concurrently, try again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) errout_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	rhashtable_remove_fast(&head->ht, &mask->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			       mask_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) static int fl_set_parms(struct net *net, struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 			struct cls_fl_filter *f, struct fl_flow_mask *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 			unsigned long base, struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 			struct nlattr *est, bool ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			struct fl_flow_tmplt *tmplt, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 				extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	if (tb[TCA_FLOWER_CLASSID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		if (!rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 			rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		tcf_bind_filter(tp, &f->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		if (!rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 			rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	err = fl_set_key(net, tb, &f->key, &mask->key, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	fl_mask_update_range(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	fl_set_masked_key(&f->mkey, &f->key, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	if (!fl_mask_fits_tmplt(tmplt, mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 			       struct cls_fl_filter *fold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			       bool *in_ht)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	struct fl_flow_mask *mask = fnew->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	err = rhashtable_lookup_insert_fast(&mask->ht,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 					    &fnew->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 					    mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		*in_ht = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		/* It is okay if filter with same key exists when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		 * overwriting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		return fold && err == -EEXIST ? 0 : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	*in_ht = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) static int fl_change(struct net *net, struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		     struct tcf_proto *tp, unsigned long base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		     u32 handle, struct nlattr **tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		     void **arg, bool ovr, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	struct cls_fl_filter *fold = *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	struct cls_fl_filter *fnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	struct fl_flow_mask *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	struct nlattr **tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	bool in_ht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	if (!tca[TCA_OPTIONS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		goto errout_fold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	if (!mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		goto errout_fold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	if (!tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		goto errout_mask_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 					  tca[TCA_OPTIONS], fl_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		goto errout_tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	if (fold && handle && fold->handle != handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		goto errout_tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	if (!fnew) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		goto errout_tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	INIT_LIST_HEAD(&fnew->hw_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	refcount_set(&fnew->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	if (tb[TCA_FLOWER_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		if (!tc_flags_valid(fnew->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			   tp->chain->tmplt_priv, rtnl_held, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	err = fl_check_assign_mask(head, fnew, fold, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	err = fl_ht_insert_unique(fnew, fold, &in_ht);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		goto errout_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	if (!tc_skip_hw(fnew->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			goto errout_ht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	if (!tc_in_hw(fnew->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	/* tp was deleted concurrently. -EAGAIN will cause caller to lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	 * proto again or create new one, if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	if (tp->deleting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		goto errout_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	if (fold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		/* Fold filter was deleted concurrently. Retry lookup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		if (fold->deleted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			goto errout_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		fnew->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		if (!in_ht) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 			struct rhashtable_params params =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 				fnew->mask->filter_ht_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			err = rhashtable_insert_fast(&fnew->mask->ht,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 						     &fnew->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 						     params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 				goto errout_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 			in_ht = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		refcount_inc(&fnew->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		rhashtable_remove_fast(&fold->mask->ht,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 				       &fold->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 				       fold->mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		idr_replace(&head->handle_idr, fnew, fnew->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		list_replace_rcu(&fold->list, &fnew->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		fold->deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		fl_mask_put(head, fold->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		if (!tc_skip_hw(fold->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		tcf_unbind_filter(tp, &fold->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		/* Caller holds reference to fold, so refcnt is always > 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		 * after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		refcount_dec(&fold->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		__fl_put(fold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		if (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			/* user specifies a handle and it doesn't exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 					    handle, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 			/* Filter with specified handle was concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 			 * inserted after initial check in cls_api. This is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 			 * necessarily an error if NLM_F_EXCL is not set in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 			 * message flags. Returning EAGAIN will cause cls_api to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 			 * try to update concurrently inserted rule.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 			if (err == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 				err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 			handle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 					    INT_MAX, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 			goto errout_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		refcount_inc(&fnew->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		fnew->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	*arg = fnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	kfree(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) errout_ht:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) errout_hw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	fnew->deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	if (!tc_skip_hw(fnew->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	if (in_ht)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 				       fnew->mask->filter_ht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) errout_mask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	fl_mask_put(head, fnew->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	__fl_put(fnew);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) errout_tb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	kfree(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) errout_mask_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) errout_fold:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (fold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		__fl_put(fold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		     bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	struct cls_fl_filter *f = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	bool last_on_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	*last = list_empty(&head->masks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		    bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	unsigned long id = arg->cookie, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	struct cls_fl_filter *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	arg->count = arg->skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		/* don't return filters that are being deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 		if (!refcount_inc_not_zero(&f->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		if (arg->fn(tp, f, arg) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 			__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 			arg->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 			rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	arg->cookie = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) static struct cls_fl_filter *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	if (list_empty(&head->hw_filters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 		f = list_entry(&head->hw_filters, struct cls_fl_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 			       hw_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 			spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 			return f;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 			void *cb_priv, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	struct cls_fl_filter *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	/* hw_filters list can only be changed by hw offload functions after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	 * obtaining rtnl lock. Make sure it is not changed while reoffload is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	 * iterating it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	while ((f = fl_get_next_hw_filter(tp, f, add))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		cls_flower.rule =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 			flow_rule_alloc(tcf_exts_num_actions(&f->exts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 		if (!cls_flower.rule) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 			__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 					   extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		cls_flower.command = add ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 			FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 		cls_flower.cookie = (unsigned long)f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 		cls_flower.rule->match.dissector = &f->mask->dissector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		cls_flower.rule->match.mask = &f->mask->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		cls_flower.rule->match.key = &f->mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			kfree(cls_flower.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 			if (tc_skip_sw(f->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 				NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 				__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 			goto next_flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		cls_flower.classid = f->res.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		err = tc_setup_cb_reoffload(block, tp, add, cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 					    TC_SETUP_CLSFLOWER, &cls_flower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 					    cb_priv, &f->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 					    &f->in_hw_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		tc_cleanup_flow_action(&cls_flower.rule->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		kfree(cls_flower.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) next_flow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		__fl_put(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) static void fl_hw_add(struct tcf_proto *tp, void *type_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	struct flow_cls_offload *cls_flower = type_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	struct cls_fl_filter *f =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 		(struct cls_fl_filter *) cls_flower->cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	list_add(&f->hw_list, &head->hw_filters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) static void fl_hw_del(struct tcf_proto *tp, void *type_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	struct flow_cls_offload *cls_flower = type_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	struct cls_fl_filter *f =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		(struct cls_fl_filter *) cls_flower->cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	if (!list_empty(&f->hw_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		list_del_init(&f->hw_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) static int fl_hw_create_tmplt(struct tcf_chain *chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 			      struct fl_flow_tmplt *tmplt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	struct tcf_block *block = chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	cls_flower.rule = flow_rule_alloc(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	if (!cls_flower.rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	cls_flower.common.chain_index = chain->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	cls_flower.command = FLOW_CLS_TMPLT_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	cls_flower.cookie = (unsigned long) tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	cls_flower.rule->match.dissector = &tmplt->dissector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	cls_flower.rule->match.mask = &tmplt->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	cls_flower.rule->match.key = &tmplt->dummy_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	/* We don't care if driver (any of them) fails to handle this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	 * call. It serves just as a hint for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	kfree(cls_flower.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 				struct fl_flow_tmplt *tmplt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	struct flow_cls_offload cls_flower = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	struct tcf_block *block = chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	cls_flower.common.chain_index = chain->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	cls_flower.cookie = (unsigned long) tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 			     struct nlattr **tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 			     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	struct fl_flow_tmplt *tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	struct nlattr **tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	if (!tca[TCA_OPTIONS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	if (!tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		return ERR_PTR(-ENOBUFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 					  tca[TCA_OPTIONS], fl_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		goto errout_tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	if (!tmplt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		goto errout_tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	tmplt->chain = chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		goto errout_tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	fl_init_dissector(&tmplt->dissector, &tmplt->mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	err = fl_hw_create_tmplt(chain, tmplt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		goto errout_tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	kfree(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	return tmplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) errout_tmplt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	kfree(tmplt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) errout_tb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	kfree(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) static void fl_tmplt_destroy(void *tmplt_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	struct fl_flow_tmplt *tmplt = tmplt_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	fl_hw_destroy_tmplt(tmplt->chain, tmplt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	kfree(tmplt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) static int fl_dump_key_val(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 			   void *val, int val_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 			   void *mask, int mask_type, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	if (!memchr_inv(mask, 0, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	err = nla_put(skb, val_type, len, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	if (mask_type != TCA_FLOWER_UNSPEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		err = nla_put(skb, mask_type, len, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	return 0;
^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) static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 				  struct fl_flow_key *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 			    TCA_FLOWER_KEY_PORT_DST_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 			    &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 			    sizeof(key->tp_range.tp_min.dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	    fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 			    TCA_FLOWER_KEY_PORT_DST_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			    &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 			    sizeof(key->tp_range.tp_max.dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	    fl_dump_key_val(skb, &key->tp_range.tp_min.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 			    TCA_FLOWER_KEY_PORT_SRC_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 			    &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 			    sizeof(key->tp_range.tp_min.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	    fl_dump_key_val(skb, &key->tp_range.tp_max.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 			    TCA_FLOWER_KEY_PORT_SRC_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 			    &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 			    sizeof(key->tp_range.tp_max.src)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 				    struct flow_dissector_key_mpls *mpls_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 				    struct flow_dissector_key_mpls *mpls_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 				    u8 lse_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 			 lse_index + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	if (lse_mask->mpls_ttl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 				 lse_key->mpls_ttl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	if (lse_mask->mpls_bos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 				 lse_key->mpls_bos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	if (lse_mask->mpls_tc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 				 lse_key->mpls_tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	if (lse_mask->mpls_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 				  lse_key->mpls_label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) static int fl_dump_key_mpls_opts(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 				 struct flow_dissector_key_mpls *mpls_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 				 struct flow_dissector_key_mpls *mpls_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	struct nlattr *opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	struct nlattr *lse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	u8 lse_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	if (!opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		if (!(mpls_mask->used_lses & 1 << lse_index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		if (!lse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 			err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 			goto err_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 					       lse_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 			goto err_opts_lse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 		nla_nest_end(skb, lse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	nla_nest_end(skb, opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) err_opts_lse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	nla_nest_cancel(skb, lse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) err_opts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	nla_nest_cancel(skb, opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) static int fl_dump_key_mpls(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 			    struct flow_dissector_key_mpls *mpls_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 			    struct flow_dissector_key_mpls *mpls_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	struct flow_dissector_mpls_lse *lse_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	struct flow_dissector_mpls_lse *lse_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	if (!mpls_mask->used_lses)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	lse_mask = &mpls_mask->ls[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	lse_key = &mpls_key->ls[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	/* For backward compatibility, don't use the MPLS nested attributes if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	 * the rule can be expressed using the old attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	if (mpls_mask->used_lses & ~1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	    (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	     !lse_mask->mpls_tc && !lse_mask->mpls_label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	if (lse_mask->mpls_ttl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 				 lse_key->mpls_ttl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	if (lse_mask->mpls_tc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 				 lse_key->mpls_tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	if (lse_mask->mpls_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 		err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 				  lse_key->mpls_label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	if (lse_mask->mpls_bos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 				 lse_key->mpls_bos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			  struct flow_dissector_key_ip *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			  struct flow_dissector_key_ip *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	    fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) static int fl_dump_key_vlan(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 			    int vlan_id_key, int vlan_prio_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 			    struct flow_dissector_key_vlan *vlan_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 			    struct flow_dissector_key_vlan *vlan_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	if (vlan_mask->vlan_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 		err = nla_put_u16(skb, vlan_id_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 				  vlan_key->vlan_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	if (vlan_mask->vlan_priority) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 		err = nla_put_u8(skb, vlan_prio_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 				 vlan_key->vlan_priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			    u32 *flower_key, u32 *flower_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 			    u32 flower_flag_bit, u32 dissector_flag_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	if (dissector_mask & dissector_flag_bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 		*flower_mask |= flower_flag_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		if (dissector_key & dissector_flag_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 			*flower_key |= flower_flag_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	u32 key, mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	__be32 _key, _mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			FLOW_DIS_FIRST_FRAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	_key = cpu_to_be32(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	_mask = cpu_to_be32(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) static int fl_dump_key_geneve_opt(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 				  struct flow_dissector_key_enc_opts *enc_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	struct geneve_opt *opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	int opt_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	while (enc_opts->len > opt_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 		opt = (struct geneve_opt *)&enc_opts->data[opt_off];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 		if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 				 opt->opt_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 		if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 			       opt->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 		if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 			    opt->length * 4, opt->opt_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 		opt_off += sizeof(struct geneve_opt) + opt->length * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 				 struct flow_dissector_key_enc_opts *enc_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	struct vxlan_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	md = (struct vxlan_metadata *)&enc_opts->data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) static int fl_dump_key_erspan_opt(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 				  struct flow_dissector_key_enc_opts *enc_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	struct erspan_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	md = (struct erspan_metadata *)&enc_opts->data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	if (md->version == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	    nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	if (md->version == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	    (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 			md->u.md2.dir) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	     nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 			get_hwid(&md->u.md2))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) static int fl_dump_key_ct(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 			  struct flow_dissector_key_ct *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 			  struct flow_dissector_key_ct *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	    fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 			    &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 			    sizeof(key->ct_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	    fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 			    &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 			    sizeof(key->ct_zone)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	    fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 			    &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 			    sizeof(key->ct_mark)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	    fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 			    &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 			    sizeof(key->ct_labels)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 			       struct flow_dissector_key_enc_opts *enc_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	if (!enc_opts->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	nest = nla_nest_start_noflag(skb, enc_opt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	switch (enc_opts->dst_opt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	case TUNNEL_GENEVE_OPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 		err = fl_dump_key_geneve_opt(skb, enc_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	case TUNNEL_VXLAN_OPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 		err = fl_dump_key_vxlan_opt(skb, enc_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	case TUNNEL_ERSPAN_OPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 		err = fl_dump_key_erspan_opt(skb, enc_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) static int fl_dump_key_enc_opt(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 			       struct flow_dissector_key_enc_opts *key_opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 			       struct flow_dissector_key_enc_opts *msk_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) static int fl_dump_key(struct sk_buff *skb, struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 		       struct fl_flow_key *key, struct fl_flow_key *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	if (mask->meta.ingress_ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 			goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 			    mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 			    sizeof(key->eth.dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	    fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 			    mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 			    sizeof(key->eth.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	    fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 			    &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 			    sizeof(key->basic.n_proto)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 			     TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 			     TCA_FLOWER_KEY_CVLAN_PRIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 			     &key->cvlan, &mask->cvlan) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	    (mask->cvlan.vlan_tpid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	     nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 			  key->cvlan.vlan_tpid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	if (mask->basic.n_proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		if (mask->cvlan.vlan_tpid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 			if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 					 key->basic.n_proto))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 				goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		} else if (mask->vlan.vlan_tpid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 			if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 					 key->basic.n_proto))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 				goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	if ((key->basic.n_proto == htons(ETH_P_IP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	     key->basic.n_proto == htons(ETH_P_IPV6)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	    (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 			    &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 			    sizeof(key->basic.ip_proto)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	    fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	    (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 			     &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 			     sizeof(key->ipv4.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	     fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 			     &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 			     sizeof(key->ipv4.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 				  &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 				  sizeof(key->ipv6.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		  fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 				  &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 				  sizeof(key->ipv6.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	if (key->basic.ip_proto == IPPROTO_TCP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 	    (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 			     &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 			     sizeof(key->tp.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 	     fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 			     &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 			     sizeof(key->tp.dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	     fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 			     &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 			     sizeof(key->tcp.flags))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	else if (key->basic.ip_proto == IPPROTO_UDP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 				  &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 				  sizeof(key->tp.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 				  &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 				  sizeof(key->tp.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	else if (key->basic.ip_proto == IPPROTO_SCTP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 				  &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 				  sizeof(key->tp.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 				  &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 				  sizeof(key->tp.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	else if (key->basic.n_proto == htons(ETH_P_IP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		 key->basic.ip_proto == IPPROTO_ICMP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		 (fl_dump_key_val(skb, &key->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 				  TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 				  TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 				  sizeof(key->icmp.type)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 		  fl_dump_key_val(skb, &key->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 				  TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 				  TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 				  sizeof(key->icmp.code))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 		 key->basic.ip_proto == IPPROTO_ICMPV6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		 (fl_dump_key_val(skb, &key->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 				  TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 				  TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 				  sizeof(key->icmp.type)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 		  fl_dump_key_val(skb, &key->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 				  TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 				  TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 				  sizeof(key->icmp.code))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 	else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 		  key->basic.n_proto == htons(ETH_P_RARP)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 		 (fl_dump_key_val(skb, &key->arp.sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 				  TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 				  TCA_FLOWER_KEY_ARP_SIP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 				  sizeof(key->arp.sip)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 		  fl_dump_key_val(skb, &key->arp.tip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 				  TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 				  TCA_FLOWER_KEY_ARP_TIP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 				  sizeof(key->arp.tip)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 		  fl_dump_key_val(skb, &key->arp.op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 				  TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 				  TCA_FLOWER_KEY_ARP_OP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 				  sizeof(key->arp.op)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		  fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 				  mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 				  sizeof(key->arp.sha)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		  fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 				  mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 				  sizeof(key->arp.tha))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	if ((key->basic.ip_proto == IPPROTO_TCP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	     key->basic.ip_proto == IPPROTO_UDP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	     key->basic.ip_proto == IPPROTO_SCTP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	     fl_dump_key_port_range(skb, key, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	    (fl_dump_key_val(skb, &key->enc_ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 			    TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 			    TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 			    sizeof(key->enc_ipv4.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	     fl_dump_key_val(skb, &key->enc_ipv4.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 			     TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 			     TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 			     sizeof(key->enc_ipv4.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 		 (fl_dump_key_val(skb, &key->enc_ipv6.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 			    TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 			    TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 			    sizeof(key->enc_ipv6.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 		 fl_dump_key_val(skb, &key->enc_ipv6.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 				 TCA_FLOWER_KEY_ENC_IPV6_DST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 				 &mask->enc_ipv6.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 				 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 			    sizeof(key->enc_ipv6.dst))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 			    &mask->enc_key_id, TCA_FLOWER_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 			    sizeof(key->enc_key_id)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	    fl_dump_key_val(skb, &key->enc_tp.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 			    &mask->enc_tp.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 			    sizeof(key->enc_tp.src)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 	    fl_dump_key_val(skb, &key->enc_tp.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 			    &mask->enc_tp.dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 			    sizeof(key->enc_tp.dst)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	    fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 	    fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 			     &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 			     sizeof(key->hash.hash)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 		   struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	struct cls_fl_filter *f = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	struct fl_flow_key *key, *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	bool skip_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 		return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	t->tcm_handle = f->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	if (f->res.classid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	    nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		goto nla_put_failure_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	key = &f->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	mask = &f->mask->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	skip_hw = tc_skip_hw(f->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	if (fl_dump_key(skb, net, key, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 		goto nla_put_failure_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		goto nla_put_failure_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	if (!skip_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 		fl_hw_update_stats(tp, f, rtnl_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 	if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	if (tcf_exts_dump(skb, &f->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) nla_put_failure_locked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 			 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	struct cls_fl_filter *f = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	bool skip_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 		return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	t->tcm_handle = f->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	skip_hw = tc_skip_hw(f->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 		goto nla_put_failure_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	if (!skip_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		fl_hw_update_stats(tp, f, rtnl_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	if (tcf_exts_terse_dump(skb, &f->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) nla_put_failure_locked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	struct fl_flow_tmplt *tmplt = tmplt_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	struct fl_flow_key *key, *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	key = &tmplt->dummy_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	mask = &tmplt->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	if (fl_dump_key(skb, net, key, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 			  unsigned long base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 	struct cls_fl_filter *f = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 	if (f && f->res.classid == classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 		if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 			__tcf_bind_filter(q, &f->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 			__tcf_unbind_filter(q, &f->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) static bool fl_delete_empty(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 	struct cls_fl_head *head = fl_head_dereference(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 	spin_lock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 	tp->deleting = idr_is_empty(&head->handle_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 	spin_unlock(&tp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 	return tp->deleting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) static struct tcf_proto_ops cls_fl_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 	.kind		= "flower",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 	.classify	= fl_classify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 	.init		= fl_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 	.destroy	= fl_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 	.get		= fl_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 	.put		= fl_put,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 	.change		= fl_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 	.delete		= fl_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	.delete_empty	= fl_delete_empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	.walk		= fl_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 	.reoffload	= fl_reoffload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	.hw_add		= fl_hw_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	.hw_del		= fl_hw_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	.dump		= fl_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	.terse_dump	= fl_terse_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	.bind_class	= fl_bind_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	.tmplt_create	= fl_tmplt_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	.tmplt_destroy	= fl_tmplt_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	.tmplt_dump	= fl_tmplt_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	.flags		= TCF_PROTO_OPS_DOIT_UNLOCKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) static int __init cls_fl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	return register_tcf_proto_ops(&cls_fl_ops);
^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 __exit cls_fl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	unregister_tcf_proto_ops(&cls_fl_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) module_init(cls_fl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) module_exit(cls_fl_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) MODULE_DESCRIPTION("Flower classifier");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) MODULE_LICENSE("GPL v2");