xref: /openbmc/linux/net/sched/em_meta.c (revision 41c6d650)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sched/em_meta.c	Metadata ematch
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
51da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
61da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
71da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Thomas Graf <tgraf@suug.ch>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * ==========================================================================
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * 	The metadata ematch compares two meta objects where each object
141da177e4SLinus Torvalds  * 	represents either a meta value stored in the kernel or a static
151da177e4SLinus Torvalds  * 	value provided by userspace. The objects are not provided by
161da177e4SLinus Torvalds  * 	userspace itself but rather a definition providing the information
171da177e4SLinus Torvalds  * 	to build them. Every object is of a certain type which must be
181da177e4SLinus Torvalds  * 	equal to the object it is being compared to.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * 	The definition of a objects conists of the type (meta type), a
211da177e4SLinus Torvalds  * 	identifier (meta id) and additional type specific information.
221da177e4SLinus Torvalds  * 	The meta id is either TCF_META_TYPE_VALUE for values provided by
231da177e4SLinus Torvalds  * 	userspace or a index to the meta operations table consisting of
241da177e4SLinus Torvalds  * 	function pointers to type specific meta data collectors returning
251da177e4SLinus Torvalds  * 	the value of the requested meta value.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * 	         lvalue                                   rvalue
281da177e4SLinus Torvalds  * 	      +-----------+                           +-----------+
291da177e4SLinus Torvalds  * 	      | type: INT |                           | type: INT |
30261688d0SDavid S. Miller  * 	 def  | id: DEV   |                           | id: VALUE |
311da177e4SLinus Torvalds  * 	      | data:     |                           | data: 3   |
321da177e4SLinus Torvalds  * 	      +-----------+                           +-----------+
331da177e4SLinus Torvalds  * 	            |                                       |
34261688d0SDavid S. Miller  * 	            ---> meta_ops[INT][DEV](...)            |
351da177e4SLinus Torvalds  *	                      |                             |
361da177e4SLinus Torvalds  * 	            -----------                             |
371da177e4SLinus Torvalds  * 	            V                                       V
381da177e4SLinus Torvalds  * 	      +-----------+                           +-----------+
391da177e4SLinus Torvalds  * 	      | type: INT |                           | type: INT |
40261688d0SDavid S. Miller  * 	 obj  | id: DEV |                             | id: VALUE |
411da177e4SLinus Torvalds  * 	      | data: 2   |<--data got filled out     | data: 3   |
421da177e4SLinus Torvalds  * 	      +-----------+                           +-----------+
431da177e4SLinus Torvalds  * 	            |                                         |
441da177e4SLinus Torvalds  * 	            --------------> 2  equals 3 <--------------
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * 	This is a simplified schema, the complexity varies depending
471da177e4SLinus Torvalds  * 	on the meta type. Obviously, the length of the data must also
481da177e4SLinus Torvalds  * 	be provided for non-numeric types.
491da177e4SLinus Torvalds  *
5025985edcSLucas De Marchi  * 	Additionally, type dependent modifiers such as shift operators
511da177e4SLinus Torvalds  * 	or mask may be applied to extend the functionaliy. As of now,
521da177e4SLinus Torvalds  * 	the variable length type supports shifting the byte string to
531da177e4SLinus Torvalds  * 	the right, eating up any number of octets and thus supporting
541da177e4SLinus Torvalds  * 	wildcard interface name comparisons such as "ppp%" matching
551da177e4SLinus Torvalds  * 	ppp0..9.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * 	NOTE: Certain meta values depend on other subsystems and are
583a4fa0a2SRobert P. J. Day  * 	      only available if that subsystem is enabled in the kernel.
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
615a0e3ad6STejun Heo #include <linux/slab.h>
621da177e4SLinus Torvalds #include <linux/module.h>
631da177e4SLinus Torvalds #include <linux/types.h>
641da177e4SLinus Torvalds #include <linux/kernel.h>
651da177e4SLinus Torvalds #include <linux/sched.h>
664f17722cSIngo Molnar #include <linux/sched/loadavg.h>
671da177e4SLinus Torvalds #include <linux/string.h>
681da177e4SLinus Torvalds #include <linux/skbuff.h>
691da177e4SLinus Torvalds #include <linux/random.h>
703113e88cSStephen Hemminger #include <linux/if_vlan.h>
711da177e4SLinus Torvalds #include <linux/tc_ematch/tc_em_meta.h>
721da177e4SLinus Torvalds #include <net/dst.h>
731da177e4SLinus Torvalds #include <net/route.h>
741da177e4SLinus Torvalds #include <net/pkt_cls.h>
7548900629SThomas Graf #include <net/sock.h>
761da177e4SLinus Torvalds 
77cc7ec456SEric Dumazet struct meta_obj {
781da177e4SLinus Torvalds 	unsigned long		value;
791da177e4SLinus Torvalds 	unsigned int		len;
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
82cc7ec456SEric Dumazet struct meta_value {
831da177e4SLinus Torvalds 	struct tcf_meta_val	hdr;
841da177e4SLinus Torvalds 	unsigned long		val;
851da177e4SLinus Torvalds 	unsigned int		len;
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
88cc7ec456SEric Dumazet struct meta_match {
891da177e4SLinus Torvalds 	struct meta_value	lvalue;
901da177e4SLinus Torvalds 	struct meta_value	rvalue;
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static inline int meta_id(struct meta_value *v)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	return TCF_META_ID(v->hdr.kind);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static inline int meta_type(struct meta_value *v)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	return TCF_META_TYPE(v->hdr.kind);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define META_COLLECTOR(FUNC) static void meta_##FUNC(struct sk_buff *skb, \
1041da177e4SLinus Torvalds 	struct tcf_pkt_info *info, struct meta_value *v, \
1051da177e4SLinus Torvalds 	struct meta_obj *dst, int *err)
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /**************************************************************************
1081da177e4SLinus Torvalds  * System status & misc
1091da177e4SLinus Torvalds  **************************************************************************/
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds META_COLLECTOR(int_random)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	get_random_bytes(&dst->value, sizeof(dst->value));
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline unsigned long fixed_loadavg(int load)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	int rnd_load = load + (FIXED_1/200);
1191da177e4SLinus Torvalds 	int rnd_frac = ((rnd_load & (FIXED_1-1)) * 100) >> FSHIFT;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	return ((rnd_load >> FSHIFT) * 100) + rnd_frac;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds META_COLLECTOR(int_loadavg_0)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	dst->value = fixed_loadavg(avenrun[0]);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds META_COLLECTOR(int_loadavg_1)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	dst->value = fixed_loadavg(avenrun[1]);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds META_COLLECTOR(int_loadavg_2)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	dst->value = fixed_loadavg(avenrun[2]);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /**************************************************************************
1401da177e4SLinus Torvalds  * Device names & indices
1411da177e4SLinus Torvalds  **************************************************************************/
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static inline int int_dev(struct net_device *dev, struct meta_obj *dst)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	if (unlikely(dev == NULL))
1461da177e4SLinus Torvalds 		return -1;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	dst->value = dev->ifindex;
1491da177e4SLinus Torvalds 	return 0;
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static inline int var_dev(struct net_device *dev, struct meta_obj *dst)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	if (unlikely(dev == NULL))
1551da177e4SLinus Torvalds 		return -1;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	dst->value = (unsigned long) dev->name;
1581da177e4SLinus Torvalds 	dst->len = strlen(dev->name);
1591da177e4SLinus Torvalds 	return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds META_COLLECTOR(int_dev)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	*err = int_dev(skb->dev, dst);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds META_COLLECTOR(var_dev)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	*err = var_dev(skb->dev, dst);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /**************************************************************************
1733113e88cSStephen Hemminger  * vlan tag
1743113e88cSStephen Hemminger  **************************************************************************/
1753113e88cSStephen Hemminger 
1763113e88cSStephen Hemminger META_COLLECTOR(int_vlan_tag)
1773113e88cSStephen Hemminger {
1781a31f204SStephen Hemminger 	unsigned short tag;
1791a31f204SStephen Hemminger 
180d65f2fa6SShmulik Ladkani 	if (skb_vlan_tag_present(skb))
181d65f2fa6SShmulik Ladkani 		dst->value = skb_vlan_tag_get(skb);
182d65f2fa6SShmulik Ladkani 	else if (!__vlan_get_tag(skb, &tag))
1833113e88cSStephen Hemminger 		dst->value = tag;
184d65f2fa6SShmulik Ladkani 	else
185d65f2fa6SShmulik Ladkani 		*err = -1;
1863113e88cSStephen Hemminger }
1873113e88cSStephen Hemminger 
1883113e88cSStephen Hemminger 
1893113e88cSStephen Hemminger 
1903113e88cSStephen Hemminger /**************************************************************************
1911da177e4SLinus Torvalds  * skb attributes
1921da177e4SLinus Torvalds  **************************************************************************/
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds META_COLLECTOR(int_priority)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	dst->value = skb->priority;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds META_COLLECTOR(int_protocol)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	/* Let userspace take care of the byte ordering */
202d8b9605dSJiri Pirko 	dst->value = tc_skb_protocol(skb);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds META_COLLECTOR(int_pkttype)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	dst->value = skb->pkt_type;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds META_COLLECTOR(int_pktlen)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	dst->value = skb->len;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds META_COLLECTOR(int_datalen)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	dst->value = skb->data_len;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds META_COLLECTOR(int_maclen)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	dst->value = skb->mac_len;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225c2e3143eSStephen Hemminger META_COLLECTOR(int_rxhash)
226c2e3143eSStephen Hemminger {
2273958afa1STom Herbert 	dst->value = skb_get_hash(skb);
228c2e3143eSStephen Hemminger }
229c2e3143eSStephen Hemminger 
2301da177e4SLinus Torvalds /**************************************************************************
2311da177e4SLinus Torvalds  * Netfilter
2321da177e4SLinus Torvalds  **************************************************************************/
2331da177e4SLinus Torvalds 
23482e91ffeSThomas Graf META_COLLECTOR(int_mark)
2351da177e4SLinus Torvalds {
23682e91ffeSThomas Graf 	dst->value = skb->mark;
2377686ee1aSPatrick McHardy }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**************************************************************************
2401da177e4SLinus Torvalds  * Traffic Control
2411da177e4SLinus Torvalds  **************************************************************************/
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds META_COLLECTOR(int_tcindex)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	dst->value = skb->tc_index;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /**************************************************************************
2491da177e4SLinus Torvalds  * Routing
2501da177e4SLinus Torvalds  **************************************************************************/
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds META_COLLECTOR(int_rtclassid)
2531da177e4SLinus Torvalds {
254adf30907SEric Dumazet 	if (unlikely(skb_dst(skb) == NULL))
2551da177e4SLinus Torvalds 		*err = -1;
2561da177e4SLinus Torvalds 	else
257c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
258adf30907SEric Dumazet 		dst->value = skb_dst(skb)->tclassid;
2597686ee1aSPatrick McHardy #else
2607686ee1aSPatrick McHardy 		dst->value = 0;
2611da177e4SLinus Torvalds #endif
2627686ee1aSPatrick McHardy }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds META_COLLECTOR(int_rtiif)
2651da177e4SLinus Torvalds {
266511c3f92SEric Dumazet 	if (unlikely(skb_rtable(skb) == NULL))
2671da177e4SLinus Torvalds 		*err = -1;
2681da177e4SLinus Torvalds 	else
26992101b3bSDavid S. Miller 		dst->value = inet_iif(skb);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /**************************************************************************
27348900629SThomas Graf  * Socket Attributes
27448900629SThomas Graf  **************************************************************************/
27548900629SThomas Graf 
2764f8f61ebSYang Yingliang #define skip_nonlocal(skb) \
2774f8f61ebSYang Yingliang 	(unlikely(skb->sk == NULL))
27848900629SThomas Graf 
27948900629SThomas Graf META_COLLECTOR(int_sk_family)
28048900629SThomas Graf {
2814f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
2824f8f61ebSYang Yingliang 		*err = -1;
2834f8f61ebSYang Yingliang 		return;
2844f8f61ebSYang Yingliang 	}
28548900629SThomas Graf 	dst->value = skb->sk->sk_family;
28648900629SThomas Graf }
28748900629SThomas Graf 
28848900629SThomas Graf META_COLLECTOR(int_sk_state)
28948900629SThomas Graf {
2904f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
2914f8f61ebSYang Yingliang 		*err = -1;
2924f8f61ebSYang Yingliang 		return;
2934f8f61ebSYang Yingliang 	}
29448900629SThomas Graf 	dst->value = skb->sk->sk_state;
29548900629SThomas Graf }
29648900629SThomas Graf 
29748900629SThomas Graf META_COLLECTOR(int_sk_reuse)
29848900629SThomas Graf {
2994f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
3004f8f61ebSYang Yingliang 		*err = -1;
3014f8f61ebSYang Yingliang 		return;
3024f8f61ebSYang Yingliang 	}
30348900629SThomas Graf 	dst->value = skb->sk->sk_reuse;
30448900629SThomas Graf }
30548900629SThomas Graf 
30648900629SThomas Graf META_COLLECTOR(int_sk_bound_if)
30748900629SThomas Graf {
3084f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
3094f8f61ebSYang Yingliang 		*err = -1;
3104f8f61ebSYang Yingliang 		return;
3114f8f61ebSYang Yingliang 	}
31248900629SThomas Graf 	/* No error if bound_dev_if is 0, legal userspace check */
31348900629SThomas Graf 	dst->value = skb->sk->sk_bound_dev_if;
31448900629SThomas Graf }
31548900629SThomas Graf 
31648900629SThomas Graf META_COLLECTOR(var_sk_bound_if)
31748900629SThomas Graf {
3184f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
3194f8f61ebSYang Yingliang 		*err = -1;
3204f8f61ebSYang Yingliang 		return;
3214f8f61ebSYang Yingliang 	}
32248900629SThomas Graf 
32348900629SThomas Graf 	if (skb->sk->sk_bound_dev_if == 0) {
32448900629SThomas Graf 		dst->value = (unsigned long) "any";
32548900629SThomas Graf 		dst->len = 3;
32648900629SThomas Graf 	} else {
32748900629SThomas Graf 		struct net_device *dev;
32848900629SThomas Graf 
329d0075634SEric Dumazet 		rcu_read_lock();
3302939e275SEric Dumazet 		dev = dev_get_by_index_rcu(sock_net(skb->sk),
3312939e275SEric Dumazet 					   skb->sk->sk_bound_dev_if);
33248900629SThomas Graf 		*err = var_dev(dev, dst);
333d0075634SEric Dumazet 		rcu_read_unlock();
33448900629SThomas Graf 	}
33548900629SThomas Graf }
33648900629SThomas Graf 
33748900629SThomas Graf META_COLLECTOR(int_sk_refcnt)
33848900629SThomas Graf {
3394f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
3404f8f61ebSYang Yingliang 		*err = -1;
3414f8f61ebSYang Yingliang 		return;
3424f8f61ebSYang Yingliang 	}
34341c6d650SReshetova, Elena 	dst->value = refcount_read(&skb->sk->sk_refcnt);
34448900629SThomas Graf }
34548900629SThomas Graf 
34648900629SThomas Graf META_COLLECTOR(int_sk_rcvbuf)
34748900629SThomas Graf {
34802a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
34902a56c81SEric Dumazet 
35002a56c81SEric Dumazet 	if (!sk) {
3514f8f61ebSYang Yingliang 		*err = -1;
3524f8f61ebSYang Yingliang 		return;
3534f8f61ebSYang Yingliang 	}
35402a56c81SEric Dumazet 	dst->value = sk->sk_rcvbuf;
35548900629SThomas Graf }
35648900629SThomas Graf 
35748900629SThomas Graf META_COLLECTOR(int_sk_shutdown)
35848900629SThomas Graf {
35902a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
36002a56c81SEric Dumazet 
36102a56c81SEric Dumazet 	if (!sk) {
3624f8f61ebSYang Yingliang 		*err = -1;
3634f8f61ebSYang Yingliang 		return;
3644f8f61ebSYang Yingliang 	}
36502a56c81SEric Dumazet 	dst->value = sk->sk_shutdown;
36648900629SThomas Graf }
36748900629SThomas Graf 
36848900629SThomas Graf META_COLLECTOR(int_sk_proto)
36948900629SThomas Graf {
37002a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
37102a56c81SEric Dumazet 
37202a56c81SEric Dumazet 	if (!sk) {
3734f8f61ebSYang Yingliang 		*err = -1;
3744f8f61ebSYang Yingliang 		return;
3754f8f61ebSYang Yingliang 	}
37602a56c81SEric Dumazet 	dst->value = sk->sk_protocol;
37748900629SThomas Graf }
37848900629SThomas Graf 
37948900629SThomas Graf META_COLLECTOR(int_sk_type)
38048900629SThomas Graf {
38102a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
38202a56c81SEric Dumazet 
38302a56c81SEric Dumazet 	if (!sk) {
3844f8f61ebSYang Yingliang 		*err = -1;
3854f8f61ebSYang Yingliang 		return;
3864f8f61ebSYang Yingliang 	}
38702a56c81SEric Dumazet 	dst->value = sk->sk_type;
38848900629SThomas Graf }
38948900629SThomas Graf 
39048900629SThomas Graf META_COLLECTOR(int_sk_rmem_alloc)
39148900629SThomas Graf {
39202a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
39302a56c81SEric Dumazet 
39402a56c81SEric Dumazet 	if (!sk) {
3954f8f61ebSYang Yingliang 		*err = -1;
3964f8f61ebSYang Yingliang 		return;
3974f8f61ebSYang Yingliang 	}
39802a56c81SEric Dumazet 	dst->value = sk_rmem_alloc_get(sk);
39948900629SThomas Graf }
40048900629SThomas Graf 
40148900629SThomas Graf META_COLLECTOR(int_sk_wmem_alloc)
40248900629SThomas Graf {
40302a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
40402a56c81SEric Dumazet 
40502a56c81SEric Dumazet 	if (!sk) {
4064f8f61ebSYang Yingliang 		*err = -1;
4074f8f61ebSYang Yingliang 		return;
4084f8f61ebSYang Yingliang 	}
40902a56c81SEric Dumazet 	dst->value = sk_wmem_alloc_get(sk);
41048900629SThomas Graf }
41148900629SThomas Graf 
41248900629SThomas Graf META_COLLECTOR(int_sk_omem_alloc)
41348900629SThomas Graf {
41402a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
41502a56c81SEric Dumazet 
41602a56c81SEric Dumazet 	if (!sk) {
4174f8f61ebSYang Yingliang 		*err = -1;
4184f8f61ebSYang Yingliang 		return;
4194f8f61ebSYang Yingliang 	}
42002a56c81SEric Dumazet 	dst->value = atomic_read(&sk->sk_omem_alloc);
42148900629SThomas Graf }
42248900629SThomas Graf 
42348900629SThomas Graf META_COLLECTOR(int_sk_rcv_qlen)
42448900629SThomas Graf {
42502a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
42602a56c81SEric Dumazet 
42702a56c81SEric Dumazet 	if (!sk) {
4284f8f61ebSYang Yingliang 		*err = -1;
4294f8f61ebSYang Yingliang 		return;
4304f8f61ebSYang Yingliang 	}
43102a56c81SEric Dumazet 	dst->value = sk->sk_receive_queue.qlen;
43248900629SThomas Graf }
43348900629SThomas Graf 
43448900629SThomas Graf META_COLLECTOR(int_sk_snd_qlen)
43548900629SThomas Graf {
43602a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
43702a56c81SEric Dumazet 
43802a56c81SEric Dumazet 	if (!sk) {
4394f8f61ebSYang Yingliang 		*err = -1;
4404f8f61ebSYang Yingliang 		return;
4414f8f61ebSYang Yingliang 	}
44202a56c81SEric Dumazet 	dst->value = sk->sk_write_queue.qlen;
44348900629SThomas Graf }
44448900629SThomas Graf 
44548900629SThomas Graf META_COLLECTOR(int_sk_wmem_queued)
44648900629SThomas Graf {
44702a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
44802a56c81SEric Dumazet 
44902a56c81SEric Dumazet 	if (!sk) {
4504f8f61ebSYang Yingliang 		*err = -1;
4514f8f61ebSYang Yingliang 		return;
4524f8f61ebSYang Yingliang 	}
45302a56c81SEric Dumazet 	dst->value = sk->sk_wmem_queued;
45448900629SThomas Graf }
45548900629SThomas Graf 
45648900629SThomas Graf META_COLLECTOR(int_sk_fwd_alloc)
45748900629SThomas Graf {
45802a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
45902a56c81SEric Dumazet 
46002a56c81SEric Dumazet 	if (!sk) {
4614f8f61ebSYang Yingliang 		*err = -1;
4624f8f61ebSYang Yingliang 		return;
4634f8f61ebSYang Yingliang 	}
46402a56c81SEric Dumazet 	dst->value = sk->sk_forward_alloc;
46548900629SThomas Graf }
46648900629SThomas Graf 
46748900629SThomas Graf META_COLLECTOR(int_sk_sndbuf)
46848900629SThomas Graf {
46902a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
47002a56c81SEric Dumazet 
47102a56c81SEric Dumazet 	if (!sk) {
4724f8f61ebSYang Yingliang 		*err = -1;
4734f8f61ebSYang Yingliang 		return;
4744f8f61ebSYang Yingliang 	}
47502a56c81SEric Dumazet 	dst->value = sk->sk_sndbuf;
47648900629SThomas Graf }
47748900629SThomas Graf 
47848900629SThomas Graf META_COLLECTOR(int_sk_alloc)
47948900629SThomas Graf {
48002a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
48102a56c81SEric Dumazet 
48202a56c81SEric Dumazet 	if (!sk) {
4834f8f61ebSYang Yingliang 		*err = -1;
4844f8f61ebSYang Yingliang 		return;
4854f8f61ebSYang Yingliang 	}
48602a56c81SEric Dumazet 	dst->value = (__force int) sk->sk_allocation;
48748900629SThomas Graf }
48848900629SThomas Graf 
48981c3d547SEric Dumazet META_COLLECTOR(int_sk_hash)
49048900629SThomas Graf {
4914f8f61ebSYang Yingliang 	if (skip_nonlocal(skb)) {
4924f8f61ebSYang Yingliang 		*err = -1;
4934f8f61ebSYang Yingliang 		return;
4944f8f61ebSYang Yingliang 	}
49581c3d547SEric Dumazet 	dst->value = skb->sk->sk_hash;
49648900629SThomas Graf }
49748900629SThomas Graf 
49848900629SThomas Graf META_COLLECTOR(int_sk_lingertime)
49948900629SThomas Graf {
50002a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
50102a56c81SEric Dumazet 
50202a56c81SEric Dumazet 	if (!sk) {
5034f8f61ebSYang Yingliang 		*err = -1;
5044f8f61ebSYang Yingliang 		return;
5054f8f61ebSYang Yingliang 	}
50602a56c81SEric Dumazet 	dst->value = sk->sk_lingertime / HZ;
50748900629SThomas Graf }
50848900629SThomas Graf 
50948900629SThomas Graf META_COLLECTOR(int_sk_err_qlen)
51048900629SThomas Graf {
51102a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
51202a56c81SEric Dumazet 
51302a56c81SEric Dumazet 	if (!sk) {
5144f8f61ebSYang Yingliang 		*err = -1;
5154f8f61ebSYang Yingliang 		return;
5164f8f61ebSYang Yingliang 	}
51702a56c81SEric Dumazet 	dst->value = sk->sk_error_queue.qlen;
51848900629SThomas Graf }
51948900629SThomas Graf 
52048900629SThomas Graf META_COLLECTOR(int_sk_ack_bl)
52148900629SThomas Graf {
52202a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
52302a56c81SEric Dumazet 
52402a56c81SEric Dumazet 	if (!sk) {
5254f8f61ebSYang Yingliang 		*err = -1;
5264f8f61ebSYang Yingliang 		return;
5274f8f61ebSYang Yingliang 	}
52802a56c81SEric Dumazet 	dst->value = sk->sk_ack_backlog;
52948900629SThomas Graf }
53048900629SThomas Graf 
53148900629SThomas Graf META_COLLECTOR(int_sk_max_ack_bl)
53248900629SThomas Graf {
53302a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
53402a56c81SEric Dumazet 
53502a56c81SEric Dumazet 	if (!sk) {
5364f8f61ebSYang Yingliang 		*err = -1;
5374f8f61ebSYang Yingliang 		return;
5384f8f61ebSYang Yingliang 	}
53902a56c81SEric Dumazet 	dst->value = sk->sk_max_ack_backlog;
54048900629SThomas Graf }
54148900629SThomas Graf 
54248900629SThomas Graf META_COLLECTOR(int_sk_prio)
54348900629SThomas Graf {
54402a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
54502a56c81SEric Dumazet 
54602a56c81SEric Dumazet 	if (!sk) {
5474f8f61ebSYang Yingliang 		*err = -1;
5484f8f61ebSYang Yingliang 		return;
5494f8f61ebSYang Yingliang 	}
55002a56c81SEric Dumazet 	dst->value = sk->sk_priority;
55148900629SThomas Graf }
55248900629SThomas Graf 
55348900629SThomas Graf META_COLLECTOR(int_sk_rcvlowat)
55448900629SThomas Graf {
55502a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
55602a56c81SEric Dumazet 
55702a56c81SEric Dumazet 	if (!sk) {
5584f8f61ebSYang Yingliang 		*err = -1;
5594f8f61ebSYang Yingliang 		return;
5604f8f61ebSYang Yingliang 	}
56102a56c81SEric Dumazet 	dst->value = sk->sk_rcvlowat;
56248900629SThomas Graf }
56348900629SThomas Graf 
56448900629SThomas Graf META_COLLECTOR(int_sk_rcvtimeo)
56548900629SThomas Graf {
56602a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
56702a56c81SEric Dumazet 
56802a56c81SEric Dumazet 	if (!sk) {
5694f8f61ebSYang Yingliang 		*err = -1;
5704f8f61ebSYang Yingliang 		return;
5714f8f61ebSYang Yingliang 	}
57202a56c81SEric Dumazet 	dst->value = sk->sk_rcvtimeo / HZ;
57348900629SThomas Graf }
57448900629SThomas Graf 
57548900629SThomas Graf META_COLLECTOR(int_sk_sndtimeo)
57648900629SThomas Graf {
57702a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
57802a56c81SEric Dumazet 
57902a56c81SEric Dumazet 	if (!sk) {
5804f8f61ebSYang Yingliang 		*err = -1;
5814f8f61ebSYang Yingliang 		return;
5824f8f61ebSYang Yingliang 	}
58302a56c81SEric Dumazet 	dst->value = sk->sk_sndtimeo / HZ;
58448900629SThomas Graf }
58548900629SThomas Graf 
58648900629SThomas Graf META_COLLECTOR(int_sk_sendmsg_off)
58748900629SThomas Graf {
58802a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
58902a56c81SEric Dumazet 
59002a56c81SEric Dumazet 	if (!sk) {
5914f8f61ebSYang Yingliang 		*err = -1;
5924f8f61ebSYang Yingliang 		return;
5934f8f61ebSYang Yingliang 	}
59402a56c81SEric Dumazet 	dst->value = sk->sk_frag.offset;
59548900629SThomas Graf }
59648900629SThomas Graf 
59748900629SThomas Graf META_COLLECTOR(int_sk_write_pend)
59848900629SThomas Graf {
59902a56c81SEric Dumazet 	const struct sock *sk = skb_to_full_sk(skb);
60002a56c81SEric Dumazet 
60102a56c81SEric Dumazet 	if (!sk) {
6024f8f61ebSYang Yingliang 		*err = -1;
6034f8f61ebSYang Yingliang 		return;
6044f8f61ebSYang Yingliang 	}
60502a56c81SEric Dumazet 	dst->value = sk->sk_write_pending;
60648900629SThomas Graf }
60748900629SThomas Graf 
60848900629SThomas Graf /**************************************************************************
6091da177e4SLinus Torvalds  * Meta value collectors assignment table
6101da177e4SLinus Torvalds  **************************************************************************/
6111da177e4SLinus Torvalds 
612cc7ec456SEric Dumazet struct meta_ops {
6131da177e4SLinus Torvalds 	void		(*get)(struct sk_buff *, struct tcf_pkt_info *,
6141da177e4SLinus Torvalds 			       struct meta_value *, struct meta_obj *, int *);
6151da177e4SLinus Torvalds };
6161da177e4SLinus Torvalds 
61748900629SThomas Graf #define META_ID(name) TCF_META_ID_##name
61848900629SThomas Graf #define META_FUNC(name) { .get = meta_##name }
61948900629SThomas Graf 
6201da177e4SLinus Torvalds /* Meta value operations table listing all meta value collectors and
6211da177e4SLinus Torvalds  * assigns them to a type and meta id. */
6221da177e4SLinus Torvalds static struct meta_ops __meta_ops[TCF_META_TYPE_MAX + 1][TCF_META_ID_MAX + 1] = {
6231da177e4SLinus Torvalds 	[TCF_META_TYPE_VAR] = {
62448900629SThomas Graf 		[META_ID(DEV)]			= META_FUNC(var_dev),
62548900629SThomas Graf 		[META_ID(SK_BOUND_IF)] 		= META_FUNC(var_sk_bound_if),
6261da177e4SLinus Torvalds 	},
6271da177e4SLinus Torvalds 	[TCF_META_TYPE_INT] = {
62848900629SThomas Graf 		[META_ID(RANDOM)]		= META_FUNC(int_random),
62948900629SThomas Graf 		[META_ID(LOADAVG_0)]		= META_FUNC(int_loadavg_0),
63048900629SThomas Graf 		[META_ID(LOADAVG_1)]		= META_FUNC(int_loadavg_1),
63148900629SThomas Graf 		[META_ID(LOADAVG_2)]		= META_FUNC(int_loadavg_2),
63248900629SThomas Graf 		[META_ID(DEV)]			= META_FUNC(int_dev),
63348900629SThomas Graf 		[META_ID(PRIORITY)]		= META_FUNC(int_priority),
63448900629SThomas Graf 		[META_ID(PROTOCOL)]		= META_FUNC(int_protocol),
63548900629SThomas Graf 		[META_ID(PKTTYPE)]		= META_FUNC(int_pkttype),
63648900629SThomas Graf 		[META_ID(PKTLEN)]		= META_FUNC(int_pktlen),
63748900629SThomas Graf 		[META_ID(DATALEN)]		= META_FUNC(int_datalen),
63848900629SThomas Graf 		[META_ID(MACLEN)]		= META_FUNC(int_maclen),
63982e91ffeSThomas Graf 		[META_ID(NFMARK)]		= META_FUNC(int_mark),
64048900629SThomas Graf 		[META_ID(TCINDEX)]		= META_FUNC(int_tcindex),
64148900629SThomas Graf 		[META_ID(RTCLASSID)]		= META_FUNC(int_rtclassid),
64248900629SThomas Graf 		[META_ID(RTIIF)]		= META_FUNC(int_rtiif),
64348900629SThomas Graf 		[META_ID(SK_FAMILY)]		= META_FUNC(int_sk_family),
64448900629SThomas Graf 		[META_ID(SK_STATE)]		= META_FUNC(int_sk_state),
64548900629SThomas Graf 		[META_ID(SK_REUSE)]		= META_FUNC(int_sk_reuse),
64648900629SThomas Graf 		[META_ID(SK_BOUND_IF)]		= META_FUNC(int_sk_bound_if),
64748900629SThomas Graf 		[META_ID(SK_REFCNT)]		= META_FUNC(int_sk_refcnt),
64848900629SThomas Graf 		[META_ID(SK_RCVBUF)]		= META_FUNC(int_sk_rcvbuf),
64948900629SThomas Graf 		[META_ID(SK_SNDBUF)]		= META_FUNC(int_sk_sndbuf),
65048900629SThomas Graf 		[META_ID(SK_SHUTDOWN)]		= META_FUNC(int_sk_shutdown),
65148900629SThomas Graf 		[META_ID(SK_PROTO)]		= META_FUNC(int_sk_proto),
65248900629SThomas Graf 		[META_ID(SK_TYPE)]		= META_FUNC(int_sk_type),
65348900629SThomas Graf 		[META_ID(SK_RMEM_ALLOC)]	= META_FUNC(int_sk_rmem_alloc),
65448900629SThomas Graf 		[META_ID(SK_WMEM_ALLOC)]	= META_FUNC(int_sk_wmem_alloc),
65548900629SThomas Graf 		[META_ID(SK_OMEM_ALLOC)]	= META_FUNC(int_sk_omem_alloc),
65648900629SThomas Graf 		[META_ID(SK_WMEM_QUEUED)]	= META_FUNC(int_sk_wmem_queued),
65748900629SThomas Graf 		[META_ID(SK_RCV_QLEN)]		= META_FUNC(int_sk_rcv_qlen),
65848900629SThomas Graf 		[META_ID(SK_SND_QLEN)]		= META_FUNC(int_sk_snd_qlen),
65948900629SThomas Graf 		[META_ID(SK_ERR_QLEN)]		= META_FUNC(int_sk_err_qlen),
66048900629SThomas Graf 		[META_ID(SK_FORWARD_ALLOCS)]	= META_FUNC(int_sk_fwd_alloc),
66148900629SThomas Graf 		[META_ID(SK_ALLOCS)]		= META_FUNC(int_sk_alloc),
66281c3d547SEric Dumazet 		[META_ID(SK_HASH)]		= META_FUNC(int_sk_hash),
66348900629SThomas Graf 		[META_ID(SK_LINGERTIME)]	= META_FUNC(int_sk_lingertime),
66448900629SThomas Graf 		[META_ID(SK_ACK_BACKLOG)]	= META_FUNC(int_sk_ack_bl),
66548900629SThomas Graf 		[META_ID(SK_MAX_ACK_BACKLOG)]	= META_FUNC(int_sk_max_ack_bl),
66648900629SThomas Graf 		[META_ID(SK_PRIO)]		= META_FUNC(int_sk_prio),
66748900629SThomas Graf 		[META_ID(SK_RCVLOWAT)]		= META_FUNC(int_sk_rcvlowat),
66848900629SThomas Graf 		[META_ID(SK_RCVTIMEO)]		= META_FUNC(int_sk_rcvtimeo),
66948900629SThomas Graf 		[META_ID(SK_SNDTIMEO)]		= META_FUNC(int_sk_sndtimeo),
67048900629SThomas Graf 		[META_ID(SK_SENDMSG_OFF)]	= META_FUNC(int_sk_sendmsg_off),
67148900629SThomas Graf 		[META_ID(SK_WRITE_PENDING)]	= META_FUNC(int_sk_write_pend),
6723113e88cSStephen Hemminger 		[META_ID(VLAN_TAG)]		= META_FUNC(int_vlan_tag),
673c2e3143eSStephen Hemminger 		[META_ID(RXHASH)]		= META_FUNC(int_rxhash),
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds };
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds static inline struct meta_ops *meta_ops(struct meta_value *val)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	return &__meta_ops[meta_type(val)][meta_id(val)];
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds /**************************************************************************
6831da177e4SLinus Torvalds  * Type specific operations for TCF_META_TYPE_VAR
6841da177e4SLinus Torvalds  **************************************************************************/
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	int r = a->len - b->len;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (r == 0)
6911da177e4SLinus Torvalds 		r = memcmp((void *) a->value, (void *) b->value, a->len);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	return r;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696add93b61SPatrick McHardy static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
6971da177e4SLinus Torvalds {
698add93b61SPatrick McHardy 	int len = nla_len(nla);
6991da177e4SLinus Torvalds 
700add93b61SPatrick McHardy 	dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
7011da177e4SLinus Torvalds 	if (dst->val == 0UL)
7021da177e4SLinus Torvalds 		return -ENOMEM;
7031da177e4SLinus Torvalds 	dst->len = len;
7041da177e4SLinus Torvalds 	return 0;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static void meta_var_destroy(struct meta_value *v)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	kfree((void *) v->val);
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static void meta_var_apply_extras(struct meta_value *v,
7131da177e4SLinus Torvalds 				  struct meta_obj *dst)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	int shift = v->hdr.shift;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (shift && shift < dst->len)
7181da177e4SLinus Torvalds 		dst->len -= shift;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
7221da177e4SLinus Torvalds {
7231b34ec43SDavid S. Miller 	if (v->val && v->len &&
7241b34ec43SDavid S. Miller 	    nla_put(skb, tlv, v->len, (void *) v->val))
7251b34ec43SDavid S. Miller 		goto nla_put_failure;
7261da177e4SLinus Torvalds 	return 0;
7271da177e4SLinus Torvalds 
728add93b61SPatrick McHardy nla_put_failure:
7291da177e4SLinus Torvalds 	return -1;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /**************************************************************************
7331da177e4SLinus Torvalds  * Type specific operations for TCF_META_TYPE_INT
7341da177e4SLinus Torvalds  **************************************************************************/
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	/* Let gcc optimize it, the unlikely is not really based on
7391da177e4SLinus Torvalds 	 * some numbers but jump free code for mismatches seems
7401da177e4SLinus Torvalds 	 * more logical. */
74198e56405SThomas Graf 	if (unlikely(a->value == b->value))
7421da177e4SLinus Torvalds 		return 0;
74398e56405SThomas Graf 	else if (a->value < b->value)
7441da177e4SLinus Torvalds 		return -1;
7451da177e4SLinus Torvalds 	else
7461da177e4SLinus Torvalds 		return 1;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
749add93b61SPatrick McHardy static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
7501da177e4SLinus Torvalds {
751add93b61SPatrick McHardy 	if (nla_len(nla) >= sizeof(unsigned long)) {
752add93b61SPatrick McHardy 		dst->val = *(unsigned long *) nla_data(nla);
7531da177e4SLinus Torvalds 		dst->len = sizeof(unsigned long);
754add93b61SPatrick McHardy 	} else if (nla_len(nla) == sizeof(u32)) {
7551587bac4SPatrick McHardy 		dst->val = nla_get_u32(nla);
7561da177e4SLinus Torvalds 		dst->len = sizeof(u32);
7571da177e4SLinus Torvalds 	} else
7581da177e4SLinus Torvalds 		return -EINVAL;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	return 0;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds static void meta_int_apply_extras(struct meta_value *v,
7641da177e4SLinus Torvalds 				  struct meta_obj *dst)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	if (v->hdr.shift)
7671da177e4SLinus Torvalds 		dst->value >>= v->hdr.shift;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (v->val)
7701da177e4SLinus Torvalds 		dst->value &= v->val;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
7741da177e4SLinus Torvalds {
7751b34ec43SDavid S. Miller 	if (v->len == sizeof(unsigned long)) {
7761b34ec43SDavid S. Miller 		if (nla_put(skb, tlv, sizeof(unsigned long), &v->val))
7771b34ec43SDavid S. Miller 			goto nla_put_failure;
7781b34ec43SDavid S. Miller 	} else if (v->len == sizeof(u32)) {
7791b34ec43SDavid S. Miller 		if (nla_put_u32(skb, tlv, v->val))
7801b34ec43SDavid S. Miller 			goto nla_put_failure;
7811b34ec43SDavid S. Miller 	}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	return 0;
7841da177e4SLinus Torvalds 
785add93b61SPatrick McHardy nla_put_failure:
7861da177e4SLinus Torvalds 	return -1;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds /**************************************************************************
7901da177e4SLinus Torvalds  * Type specific operations table
7911da177e4SLinus Torvalds  **************************************************************************/
7921da177e4SLinus Torvalds 
793cc7ec456SEric Dumazet struct meta_type_ops {
7941da177e4SLinus Torvalds 	void	(*destroy)(struct meta_value *);
7951da177e4SLinus Torvalds 	int	(*compare)(struct meta_obj *, struct meta_obj *);
796add93b61SPatrick McHardy 	int	(*change)(struct meta_value *, struct nlattr *);
7971da177e4SLinus Torvalds 	void	(*apply_extras)(struct meta_value *, struct meta_obj *);
7981da177e4SLinus Torvalds 	int	(*dump)(struct sk_buff *, struct meta_value *, int);
7991da177e4SLinus Torvalds };
8001da177e4SLinus Torvalds 
801cfe2f14cSJulia Lawall static const struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX + 1] = {
8021da177e4SLinus Torvalds 	[TCF_META_TYPE_VAR] = {
8031da177e4SLinus Torvalds 		.destroy = meta_var_destroy,
8041da177e4SLinus Torvalds 		.compare = meta_var_compare,
8051da177e4SLinus Torvalds 		.change = meta_var_change,
8061da177e4SLinus Torvalds 		.apply_extras = meta_var_apply_extras,
8071da177e4SLinus Torvalds 		.dump = meta_var_dump
8081da177e4SLinus Torvalds 	},
8091da177e4SLinus Torvalds 	[TCF_META_TYPE_INT] = {
8101da177e4SLinus Torvalds 		.compare = meta_int_compare,
8111da177e4SLinus Torvalds 		.change = meta_int_change,
8121da177e4SLinus Torvalds 		.apply_extras = meta_int_apply_extras,
8131da177e4SLinus Torvalds 		.dump = meta_int_dump
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds };
8161da177e4SLinus Torvalds 
817cfe2f14cSJulia Lawall static inline const struct meta_type_ops *meta_type_ops(struct meta_value *v)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	return &__meta_type_ops[meta_type(v)];
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds /**************************************************************************
8231da177e4SLinus Torvalds  * Core
8241da177e4SLinus Torvalds  **************************************************************************/
8251da177e4SLinus Torvalds 
826ed7af3b3SStephen Hemminger static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
8271da177e4SLinus Torvalds 		    struct meta_value *v, struct meta_obj *dst)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	int err = 0;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if (meta_id(v) == TCF_META_ID_VALUE) {
8321da177e4SLinus Torvalds 		dst->value = v->val;
8331da177e4SLinus Torvalds 		dst->len = v->len;
8341da177e4SLinus Torvalds 		return 0;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	meta_ops(v)->get(skb, info, v, dst, &err);
8381da177e4SLinus Torvalds 	if (err < 0)
8391da177e4SLinus Torvalds 		return err;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (meta_type_ops(v)->apply_extras)
8421da177e4SLinus Torvalds 		meta_type_ops(v)->apply_extras(v, dst);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	return 0;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
8481da177e4SLinus Torvalds 			 struct tcf_pkt_info *info)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	int r;
8511da177e4SLinus Torvalds 	struct meta_match *meta = (struct meta_match *) m->data;
8521da177e4SLinus Torvalds 	struct meta_obj l_value, r_value;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
8551da177e4SLinus Torvalds 	    meta_get(skb, info, &meta->rvalue, &r_value) < 0)
8561da177e4SLinus Torvalds 		return 0;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	switch (meta->lvalue.hdr.op) {
8611da177e4SLinus Torvalds 	case TCF_EM_OPND_EQ:
8621da177e4SLinus Torvalds 		return !r;
8631da177e4SLinus Torvalds 	case TCF_EM_OPND_LT:
8641da177e4SLinus Torvalds 		return r < 0;
8651da177e4SLinus Torvalds 	case TCF_EM_OPND_GT:
8661da177e4SLinus Torvalds 		return r > 0;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	return 0;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
872ed7af3b3SStephen Hemminger static void meta_delete(struct meta_match *meta)
8731da177e4SLinus Torvalds {
87404f217acSStephen Hemminger 	if (meta) {
875cfe2f14cSJulia Lawall 		const struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 		if (ops && ops->destroy) {
8781da177e4SLinus Torvalds 			ops->destroy(&meta->lvalue);
8791da177e4SLinus Torvalds 			ops->destroy(&meta->rvalue);
8801da177e4SLinus Torvalds 		}
88104f217acSStephen Hemminger 	}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	kfree(meta);
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
886add93b61SPatrick McHardy static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
8871da177e4SLinus Torvalds {
888add93b61SPatrick McHardy 	if (nla) {
889add93b61SPatrick McHardy 		if (nla_len(nla) == 0)
8901da177e4SLinus Torvalds 			return -EINVAL;
8911da177e4SLinus Torvalds 
892add93b61SPatrick McHardy 		return meta_type_ops(dst)->change(dst, nla);
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	return 0;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static inline int meta_is_supported(struct meta_value *val)
8991da177e4SLinus Torvalds {
900cc7ec456SEric Dumazet 	return !meta_id(val) || meta_ops(val)->get;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9037a9c1bd4SPatrick McHardy static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
9047a9c1bd4SPatrick McHardy 	[TCA_EM_META_HDR]	= { .len = sizeof(struct tcf_meta_hdr) },
9057a9c1bd4SPatrick McHardy };
9067a9c1bd4SPatrick McHardy 
90782a470f1SJohn Fastabend static int em_meta_change(struct net *net, void *data, int len,
9081da177e4SLinus Torvalds 			  struct tcf_ematch *m)
9091da177e4SLinus Torvalds {
910cee63723SPatrick McHardy 	int err;
911add93b61SPatrick McHardy 	struct nlattr *tb[TCA_EM_META_MAX + 1];
9121da177e4SLinus Torvalds 	struct tcf_meta_hdr *hdr;
9131da177e4SLinus Torvalds 	struct meta_match *meta = NULL;
9141da177e4SLinus Torvalds 
915fceb6435SJohannes Berg 	err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy, NULL);
916cee63723SPatrick McHardy 	if (err < 0)
9171da177e4SLinus Torvalds 		goto errout;
9181da177e4SLinus Torvalds 
919cee63723SPatrick McHardy 	err = -EINVAL;
9207a9c1bd4SPatrick McHardy 	if (tb[TCA_EM_META_HDR] == NULL)
9211da177e4SLinus Torvalds 		goto errout;
922add93b61SPatrick McHardy 	hdr = nla_data(tb[TCA_EM_META_HDR]);
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
9251da177e4SLinus Torvalds 	    TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
9261da177e4SLinus Torvalds 	    TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
9271da177e4SLinus Torvalds 	    TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
9281da177e4SLinus Torvalds 		goto errout;
9291da177e4SLinus Torvalds 
9300da974f4SPanagiotis Issaris 	meta = kzalloc(sizeof(*meta), GFP_KERNEL);
9310c4e4020Sstephen hemminger 	if (meta == NULL) {
9320c4e4020Sstephen hemminger 		err = -ENOMEM;
9331da177e4SLinus Torvalds 		goto errout;
9340c4e4020Sstephen hemminger 	}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
9371da177e4SLinus Torvalds 	memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	if (!meta_is_supported(&meta->lvalue) ||
9401da177e4SLinus Torvalds 	    !meta_is_supported(&meta->rvalue)) {
9411da177e4SLinus Torvalds 		err = -EOPNOTSUPP;
9421da177e4SLinus Torvalds 		goto errout;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
945add93b61SPatrick McHardy 	if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
946add93b61SPatrick McHardy 	    meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
9471da177e4SLinus Torvalds 		goto errout;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	m->datalen = sizeof(*meta);
9501da177e4SLinus Torvalds 	m->data = (unsigned long) meta;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	err = 0;
9531da177e4SLinus Torvalds errout:
9541da177e4SLinus Torvalds 	if (err && meta)
9551da177e4SLinus Torvalds 		meta_delete(meta);
9561da177e4SLinus Torvalds 	return err;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
95982a470f1SJohn Fastabend static void em_meta_destroy(struct tcf_ematch *m)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	if (m)
9621da177e4SLinus Torvalds 		meta_delete((struct meta_match *) m->data);
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
9661da177e4SLinus Torvalds {
9671da177e4SLinus Torvalds 	struct meta_match *meta = (struct meta_match *) em->data;
9681da177e4SLinus Torvalds 	struct tcf_meta_hdr hdr;
969cfe2f14cSJulia Lawall 	const struct meta_type_ops *ops;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	memset(&hdr, 0, sizeof(hdr));
9721da177e4SLinus Torvalds 	memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
9731da177e4SLinus Torvalds 	memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
9741da177e4SLinus Torvalds 
9751b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr))
9761b34ec43SDavid S. Miller 		goto nla_put_failure;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	ops = meta_type_ops(&meta->lvalue);
9791da177e4SLinus Torvalds 	if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
9801da177e4SLinus Torvalds 	    ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
981add93b61SPatrick McHardy 		goto nla_put_failure;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	return 0;
9841da177e4SLinus Torvalds 
985add93b61SPatrick McHardy nla_put_failure:
9861da177e4SLinus Torvalds 	return -1;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static struct tcf_ematch_ops em_meta_ops = {
9901da177e4SLinus Torvalds 	.kind	  = TCF_EM_META,
9911da177e4SLinus Torvalds 	.change	  = em_meta_change,
9921da177e4SLinus Torvalds 	.match	  = em_meta_match,
9931da177e4SLinus Torvalds 	.destroy  = em_meta_destroy,
9941da177e4SLinus Torvalds 	.dump	  = em_meta_dump,
9951da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
9961da177e4SLinus Torvalds 	.link	  = LIST_HEAD_INIT(em_meta_ops.link)
9971da177e4SLinus Torvalds };
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds static int __init init_em_meta(void)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	return tcf_em_register(&em_meta_ops);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds static void __exit exit_em_meta(void)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	tcf_em_unregister(&em_meta_ops);
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds module_init(init_em_meta);
10121da177e4SLinus Torvalds module_exit(exit_em_meta);
1013db3d99c0SPatrick McHardy 
1014db3d99c0SPatrick McHardy MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);
1015