em_meta.c 23.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * net/sched/em_meta.c	Metadata ematch
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Thomas Graf <tgraf@suug.ch>
 *
 * ==========================================================================
12
 *
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * 	The metadata ematch compares two meta objects where each object
 * 	represents either a meta value stored in the kernel or a static
 * 	value provided by userspace. The objects are not provided by
 * 	userspace itself but rather a definition providing the information
 * 	to build them. Every object is of a certain type which must be
 * 	equal to the object it is being compared to.
 *
 * 	The definition of a objects conists of the type (meta type), a
 * 	identifier (meta id) and additional type specific information.
 * 	The meta id is either TCF_META_TYPE_VALUE for values provided by
 * 	userspace or a index to the meta operations table consisting of
 * 	function pointers to type specific meta data collectors returning
 * 	the value of the requested meta value.
 *
 * 	         lvalue                                   rvalue
 * 	      +-----------+                           +-----------+
 * 	      | type: INT |                           | type: INT |
30
 * 	 def  | id: DEV   |                           | id: VALUE |
L
Linus Torvalds 已提交
31 32 33
 * 	      | data:     |                           | data: 3   |
 * 	      +-----------+                           +-----------+
 * 	            |                                       |
34
 * 	            ---> meta_ops[INT][DEV](...)            |
35
 *	                      |                             |
L
Linus Torvalds 已提交
36 37 38 39
 * 	            -----------                             |
 * 	            V                                       V
 * 	      +-----------+                           +-----------+
 * 	      | type: INT |                           | type: INT |
40
 * 	 obj  | id: DEV |                             | id: VALUE |
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49
 * 	      | data: 2   |<--data got filled out     | data: 3   |
 * 	      +-----------+                           +-----------+
 * 	            |                                         |
 * 	            --------------> 2  equals 3 <--------------
 *
 * 	This is a simplified schema, the complexity varies depending
 * 	on the meta type. Obviously, the length of the data must also
 * 	be provided for non-numeric types.
 *
L
Lucas De Marchi 已提交
50
 * 	Additionally, type dependent modifiers such as shift operators
L
Linus Torvalds 已提交
51 52 53 54 55 56 57
 * 	or mask may be applied to extend the functionaliy. As of now,
 * 	the variable length type supports shifting the byte string to
 * 	the right, eating up any number of octets and thus supporting
 * 	wildcard interface name comparisons such as "ppp%" matching
 * 	ppp0..9.
 *
 * 	NOTE: Certain meta values depend on other subsystems and are
58
 * 	      only available if that subsystem is enabled in the kernel.
L
Linus Torvalds 已提交
59 60
 */

61
#include <linux/slab.h>
L
Linus Torvalds 已提交
62 63 64 65
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
66
#include <linux/sched/loadavg.h>
L
Linus Torvalds 已提交
67 68 69
#include <linux/string.h>
#include <linux/skbuff.h>
#include <linux/random.h>
S
Stephen Hemminger 已提交
70
#include <linux/if_vlan.h>
L
Linus Torvalds 已提交
71 72 73 74
#include <linux/tc_ematch/tc_em_meta.h>
#include <net/dst.h>
#include <net/route.h>
#include <net/pkt_cls.h>
75
#include <net/sock.h>
L
Linus Torvalds 已提交
76

E
Eric Dumazet 已提交
77
struct meta_obj {
L
Linus Torvalds 已提交
78 79 80 81
	unsigned long		value;
	unsigned int		len;
};

E
Eric Dumazet 已提交
82
struct meta_value {
L
Linus Torvalds 已提交
83 84 85 86 87
	struct tcf_meta_val	hdr;
	unsigned long		val;
	unsigned int		len;
};

E
Eric Dumazet 已提交
88
struct meta_match {
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	struct meta_value	lvalue;
	struct meta_value	rvalue;
};

static inline int meta_id(struct meta_value *v)
{
	return TCF_META_ID(v->hdr.kind);
}

static inline int meta_type(struct meta_value *v)
{
	return TCF_META_TYPE(v->hdr.kind);
}

#define META_COLLECTOR(FUNC) static void meta_##FUNC(struct sk_buff *skb, \
	struct tcf_pkt_info *info, struct meta_value *v, \
	struct meta_obj *dst, int *err)

/**************************************************************************
 * System status & misc
 **************************************************************************/

META_COLLECTOR(int_random)
{
	get_random_bytes(&dst->value, sizeof(dst->value));
}

static inline unsigned long fixed_loadavg(int load)
{
	int rnd_load = load + (FIXED_1/200);
	int rnd_frac = ((rnd_load & (FIXED_1-1)) * 100) >> FSHIFT;

	return ((rnd_load >> FSHIFT) * 100) + rnd_frac;
}

META_COLLECTOR(int_loadavg_0)
{
	dst->value = fixed_loadavg(avenrun[0]);
}

META_COLLECTOR(int_loadavg_1)
{
	dst->value = fixed_loadavg(avenrun[1]);
}

META_COLLECTOR(int_loadavg_2)
{
	dst->value = fixed_loadavg(avenrun[2]);
}

/**************************************************************************
 * Device names & indices
 **************************************************************************/

static inline int int_dev(struct net_device *dev, struct meta_obj *dst)
{
	if (unlikely(dev == NULL))
		return -1;

	dst->value = dev->ifindex;
	return 0;
}

static inline int var_dev(struct net_device *dev, struct meta_obj *dst)
{
	if (unlikely(dev == NULL))
		return -1;

	dst->value = (unsigned long) dev->name;
	dst->len = strlen(dev->name);
	return 0;
}

META_COLLECTOR(int_dev)
{
	*err = int_dev(skb->dev, dst);
}

META_COLLECTOR(var_dev)
{
	*err = var_dev(skb->dev, dst);
}

S
Stephen Hemminger 已提交
172 173 174 175 176 177
/**************************************************************************
 * vlan tag
 **************************************************************************/

META_COLLECTOR(int_vlan_tag)
{
178 179
	unsigned short tag;

180 181 182
	if (skb_vlan_tag_present(skb))
		dst->value = skb_vlan_tag_get(skb);
	else if (!__vlan_get_tag(skb, &tag))
S
Stephen Hemminger 已提交
183
		dst->value = tag;
184 185
	else
		*err = -1;
S
Stephen Hemminger 已提交
186 187 188 189
}



L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199 200 201
/**************************************************************************
 * skb attributes
 **************************************************************************/

META_COLLECTOR(int_priority)
{
	dst->value = skb->priority;
}

META_COLLECTOR(int_protocol)
{
	/* Let userspace take care of the byte ordering */
202
	dst->value = tc_skb_protocol(skb);
L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
}

META_COLLECTOR(int_pkttype)
{
	dst->value = skb->pkt_type;
}

META_COLLECTOR(int_pktlen)
{
	dst->value = skb->len;
}

META_COLLECTOR(int_datalen)
{
	dst->value = skb->data_len;
}

META_COLLECTOR(int_maclen)
{
	dst->value = skb->mac_len;
}

225 226
META_COLLECTOR(int_rxhash)
{
227
	dst->value = skb_get_hash(skb);
228 229
}

L
Linus Torvalds 已提交
230 231 232 233
/**************************************************************************
 * Netfilter
 **************************************************************************/

T
Thomas Graf 已提交
234
META_COLLECTOR(int_mark)
L
Linus Torvalds 已提交
235
{
T
Thomas Graf 已提交
236
	dst->value = skb->mark;
237
}
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

/**************************************************************************
 * Traffic Control
 **************************************************************************/

META_COLLECTOR(int_tcindex)
{
	dst->value = skb->tc_index;
}

/**************************************************************************
 * Routing
 **************************************************************************/

META_COLLECTOR(int_rtclassid)
{
E
Eric Dumazet 已提交
254
	if (unlikely(skb_dst(skb) == NULL))
L
Linus Torvalds 已提交
255 256
		*err = -1;
	else
257
#ifdef CONFIG_IP_ROUTE_CLASSID
E
Eric Dumazet 已提交
258
		dst->value = skb_dst(skb)->tclassid;
259 260
#else
		dst->value = 0;
L
Linus Torvalds 已提交
261
#endif
262
}
L
Linus Torvalds 已提交
263 264 265

META_COLLECTOR(int_rtiif)
{
E
Eric Dumazet 已提交
266
	if (unlikely(skb_rtable(skb) == NULL))
L
Linus Torvalds 已提交
267 268
		*err = -1;
	else
269
		dst->value = inet_iif(skb);
L
Linus Torvalds 已提交
270 271
}

272 273 274 275
/**************************************************************************
 * Socket Attributes
 **************************************************************************/

276 277
#define skip_nonlocal(skb) \
	(unlikely(skb->sk == NULL))
278 279 280

META_COLLECTOR(int_sk_family)
{
281 282 283 284
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
285 286 287 288 289
	dst->value = skb->sk->sk_family;
}

META_COLLECTOR(int_sk_state)
{
290 291 292 293
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
294 295 296 297 298
	dst->value = skb->sk->sk_state;
}

META_COLLECTOR(int_sk_reuse)
{
299 300 301 302
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
303 304 305 306 307
	dst->value = skb->sk->sk_reuse;
}

META_COLLECTOR(int_sk_bound_if)
{
308 309 310 311
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
312 313 314 315 316 317
	/* No error if bound_dev_if is 0, legal userspace check */
	dst->value = skb->sk->sk_bound_dev_if;
}

META_COLLECTOR(var_sk_bound_if)
{
318 319 320 321
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
322

E
Eric Dumazet 已提交
323
	if (skb->sk->sk_bound_dev_if == 0) {
324 325
		dst->value = (unsigned long) "any";
		dst->len = 3;
E
Eric Dumazet 已提交
326
	} else {
327
		struct net_device *dev;
328

E
Eric Dumazet 已提交
329
		rcu_read_lock();
330 331
		dev = dev_get_by_index_rcu(sock_net(skb->sk),
					   skb->sk->sk_bound_dev_if);
332
		*err = var_dev(dev, dst);
E
Eric Dumazet 已提交
333 334
		rcu_read_unlock();
	}
335 336 337 338
}

META_COLLECTOR(int_sk_refcnt)
{
339 340 341 342
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
343 344 345 346 347
	dst->value = atomic_read(&skb->sk->sk_refcnt);
}

META_COLLECTOR(int_sk_rcvbuf)
{
348 349 350
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
351 352 353
		*err = -1;
		return;
	}
354
	dst->value = sk->sk_rcvbuf;
355 356 357 358
}

META_COLLECTOR(int_sk_shutdown)
{
359 360 361
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
362 363 364
		*err = -1;
		return;
	}
365
	dst->value = sk->sk_shutdown;
366 367 368 369
}

META_COLLECTOR(int_sk_proto)
{
370 371 372
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
373 374 375
		*err = -1;
		return;
	}
376
	dst->value = sk->sk_protocol;
377 378 379 380
}

META_COLLECTOR(int_sk_type)
{
381 382 383
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
384 385 386
		*err = -1;
		return;
	}
387
	dst->value = sk->sk_type;
388 389 390 391
}

META_COLLECTOR(int_sk_rmem_alloc)
{
392 393 394
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
395 396 397
		*err = -1;
		return;
	}
398
	dst->value = sk_rmem_alloc_get(sk);
399 400 401 402
}

META_COLLECTOR(int_sk_wmem_alloc)
{
403 404 405
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
406 407 408
		*err = -1;
		return;
	}
409
	dst->value = sk_wmem_alloc_get(sk);
410 411 412 413
}

META_COLLECTOR(int_sk_omem_alloc)
{
414 415 416
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
417 418 419
		*err = -1;
		return;
	}
420
	dst->value = atomic_read(&sk->sk_omem_alloc);
421 422 423 424
}

META_COLLECTOR(int_sk_rcv_qlen)
{
425 426 427
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
428 429 430
		*err = -1;
		return;
	}
431
	dst->value = sk->sk_receive_queue.qlen;
432 433 434 435
}

META_COLLECTOR(int_sk_snd_qlen)
{
436 437 438
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
439 440 441
		*err = -1;
		return;
	}
442
	dst->value = sk->sk_write_queue.qlen;
443 444 445 446
}

META_COLLECTOR(int_sk_wmem_queued)
{
447 448 449
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
450 451 452
		*err = -1;
		return;
	}
453
	dst->value = sk->sk_wmem_queued;
454 455 456 457
}

META_COLLECTOR(int_sk_fwd_alloc)
{
458 459 460
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
461 462 463
		*err = -1;
		return;
	}
464
	dst->value = sk->sk_forward_alloc;
465 466 467 468
}

META_COLLECTOR(int_sk_sndbuf)
{
469 470 471
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
472 473 474
		*err = -1;
		return;
	}
475
	dst->value = sk->sk_sndbuf;
476 477 478 479
}

META_COLLECTOR(int_sk_alloc)
{
480 481 482
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
483 484 485
		*err = -1;
		return;
	}
486
	dst->value = (__force int) sk->sk_allocation;
487 488
}

489
META_COLLECTOR(int_sk_hash)
490
{
491 492 493 494
	if (skip_nonlocal(skb)) {
		*err = -1;
		return;
	}
495
	dst->value = skb->sk->sk_hash;
496 497 498 499
}

META_COLLECTOR(int_sk_lingertime)
{
500 501 502
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
503 504 505
		*err = -1;
		return;
	}
506
	dst->value = sk->sk_lingertime / HZ;
507 508 509 510
}

META_COLLECTOR(int_sk_err_qlen)
{
511 512 513
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
514 515 516
		*err = -1;
		return;
	}
517
	dst->value = sk->sk_error_queue.qlen;
518 519 520 521
}

META_COLLECTOR(int_sk_ack_bl)
{
522 523 524
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
525 526 527
		*err = -1;
		return;
	}
528
	dst->value = sk->sk_ack_backlog;
529 530 531 532
}

META_COLLECTOR(int_sk_max_ack_bl)
{
533 534 535
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
536 537 538
		*err = -1;
		return;
	}
539
	dst->value = sk->sk_max_ack_backlog;
540 541 542 543
}

META_COLLECTOR(int_sk_prio)
{
544 545 546
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
547 548 549
		*err = -1;
		return;
	}
550
	dst->value = sk->sk_priority;
551 552 553 554
}

META_COLLECTOR(int_sk_rcvlowat)
{
555 556 557
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
558 559 560
		*err = -1;
		return;
	}
561
	dst->value = sk->sk_rcvlowat;
562 563 564 565
}

META_COLLECTOR(int_sk_rcvtimeo)
{
566 567 568
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
569 570 571
		*err = -1;
		return;
	}
572
	dst->value = sk->sk_rcvtimeo / HZ;
573 574 575 576
}

META_COLLECTOR(int_sk_sndtimeo)
{
577 578 579
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
580 581 582
		*err = -1;
		return;
	}
583
	dst->value = sk->sk_sndtimeo / HZ;
584 585 586 587
}

META_COLLECTOR(int_sk_sendmsg_off)
{
588 589 590
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
591 592 593
		*err = -1;
		return;
	}
594
	dst->value = sk->sk_frag.offset;
595 596 597 598
}

META_COLLECTOR(int_sk_write_pend)
{
599 600 601
	const struct sock *sk = skb_to_full_sk(skb);

	if (!sk) {
602 603 604
		*err = -1;
		return;
	}
605
	dst->value = sk->sk_write_pending;
606 607
}

L
Linus Torvalds 已提交
608 609 610 611
/**************************************************************************
 * Meta value collectors assignment table
 **************************************************************************/

E
Eric Dumazet 已提交
612
struct meta_ops {
L
Linus Torvalds 已提交
613 614 615 616
	void		(*get)(struct sk_buff *, struct tcf_pkt_info *,
			       struct meta_value *, struct meta_obj *, int *);
};

617 618 619
#define META_ID(name) TCF_META_ID_##name
#define META_FUNC(name) { .get = meta_##name }

L
Linus Torvalds 已提交
620 621
/* Meta value operations table listing all meta value collectors and
 * assigns them to a type and meta id. */
E
Eric Dumazet 已提交
622
static struct meta_ops __meta_ops[TCF_META_TYPE_MAX + 1][TCF_META_ID_MAX + 1] = {
L
Linus Torvalds 已提交
623
	[TCF_META_TYPE_VAR] = {
624 625
		[META_ID(DEV)]			= META_FUNC(var_dev),
		[META_ID(SK_BOUND_IF)] 		= META_FUNC(var_sk_bound_if),
L
Linus Torvalds 已提交
626 627
	},
	[TCF_META_TYPE_INT] = {
628 629 630 631 632 633 634 635 636 637 638
		[META_ID(RANDOM)]		= META_FUNC(int_random),
		[META_ID(LOADAVG_0)]		= META_FUNC(int_loadavg_0),
		[META_ID(LOADAVG_1)]		= META_FUNC(int_loadavg_1),
		[META_ID(LOADAVG_2)]		= META_FUNC(int_loadavg_2),
		[META_ID(DEV)]			= META_FUNC(int_dev),
		[META_ID(PRIORITY)]		= META_FUNC(int_priority),
		[META_ID(PROTOCOL)]		= META_FUNC(int_protocol),
		[META_ID(PKTTYPE)]		= META_FUNC(int_pkttype),
		[META_ID(PKTLEN)]		= META_FUNC(int_pktlen),
		[META_ID(DATALEN)]		= META_FUNC(int_datalen),
		[META_ID(MACLEN)]		= META_FUNC(int_maclen),
T
Thomas Graf 已提交
639
		[META_ID(NFMARK)]		= META_FUNC(int_mark),
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		[META_ID(TCINDEX)]		= META_FUNC(int_tcindex),
		[META_ID(RTCLASSID)]		= META_FUNC(int_rtclassid),
		[META_ID(RTIIF)]		= META_FUNC(int_rtiif),
		[META_ID(SK_FAMILY)]		= META_FUNC(int_sk_family),
		[META_ID(SK_STATE)]		= META_FUNC(int_sk_state),
		[META_ID(SK_REUSE)]		= META_FUNC(int_sk_reuse),
		[META_ID(SK_BOUND_IF)]		= META_FUNC(int_sk_bound_if),
		[META_ID(SK_REFCNT)]		= META_FUNC(int_sk_refcnt),
		[META_ID(SK_RCVBUF)]		= META_FUNC(int_sk_rcvbuf),
		[META_ID(SK_SNDBUF)]		= META_FUNC(int_sk_sndbuf),
		[META_ID(SK_SHUTDOWN)]		= META_FUNC(int_sk_shutdown),
		[META_ID(SK_PROTO)]		= META_FUNC(int_sk_proto),
		[META_ID(SK_TYPE)]		= META_FUNC(int_sk_type),
		[META_ID(SK_RMEM_ALLOC)]	= META_FUNC(int_sk_rmem_alloc),
		[META_ID(SK_WMEM_ALLOC)]	= META_FUNC(int_sk_wmem_alloc),
		[META_ID(SK_OMEM_ALLOC)]	= META_FUNC(int_sk_omem_alloc),
		[META_ID(SK_WMEM_QUEUED)]	= META_FUNC(int_sk_wmem_queued),
		[META_ID(SK_RCV_QLEN)]		= META_FUNC(int_sk_rcv_qlen),
		[META_ID(SK_SND_QLEN)]		= META_FUNC(int_sk_snd_qlen),
		[META_ID(SK_ERR_QLEN)]		= META_FUNC(int_sk_err_qlen),
		[META_ID(SK_FORWARD_ALLOCS)]	= META_FUNC(int_sk_fwd_alloc),
		[META_ID(SK_ALLOCS)]		= META_FUNC(int_sk_alloc),
662
		[META_ID(SK_HASH)]		= META_FUNC(int_sk_hash),
663 664 665 666 667 668 669 670 671
		[META_ID(SK_LINGERTIME)]	= META_FUNC(int_sk_lingertime),
		[META_ID(SK_ACK_BACKLOG)]	= META_FUNC(int_sk_ack_bl),
		[META_ID(SK_MAX_ACK_BACKLOG)]	= META_FUNC(int_sk_max_ack_bl),
		[META_ID(SK_PRIO)]		= META_FUNC(int_sk_prio),
		[META_ID(SK_RCVLOWAT)]		= META_FUNC(int_sk_rcvlowat),
		[META_ID(SK_RCVTIMEO)]		= META_FUNC(int_sk_rcvtimeo),
		[META_ID(SK_SNDTIMEO)]		= META_FUNC(int_sk_sndtimeo),
		[META_ID(SK_SENDMSG_OFF)]	= META_FUNC(int_sk_sendmsg_off),
		[META_ID(SK_WRITE_PENDING)]	= META_FUNC(int_sk_write_pend),
S
Stephen Hemminger 已提交
672
		[META_ID(VLAN_TAG)]		= META_FUNC(int_vlan_tag),
673
		[META_ID(RXHASH)]		= META_FUNC(int_rxhash),
L
Linus Torvalds 已提交
674 675 676
	}
};

E
Eric Dumazet 已提交
677
static inline struct meta_ops *meta_ops(struct meta_value *val)
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
{
	return &__meta_ops[meta_type(val)][meta_id(val)];
}

/**************************************************************************
 * Type specific operations for TCF_META_TYPE_VAR
 **************************************************************************/

static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
{
	int r = a->len - b->len;

	if (r == 0)
		r = memcmp((void *) a->value, (void *) b->value, a->len);

	return r;
}

696
static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
L
Linus Torvalds 已提交
697
{
698
	int len = nla_len(nla);
L
Linus Torvalds 已提交
699

700
	dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708
	if (dst->val == 0UL)
		return -ENOMEM;
	dst->len = len;
	return 0;
}

static void meta_var_destroy(struct meta_value *v)
{
J
Jesper Juhl 已提交
709
	kfree((void *) v->val);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722
}

static void meta_var_apply_extras(struct meta_value *v,
				  struct meta_obj *dst)
{
	int shift = v->hdr.shift;

	if (shift && shift < dst->len)
		dst->len -= shift;
}

static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
{
723 724 725
	if (v->val && v->len &&
	    nla_put(skb, tlv, v->len, (void *) v->val))
		goto nla_put_failure;
L
Linus Torvalds 已提交
726 727
	return 0;

728
nla_put_failure:
L
Linus Torvalds 已提交
729 730 731 732 733 734 735 736 737 738 739 740
	return -1;
}

/**************************************************************************
 * Type specific operations for TCF_META_TYPE_INT
 **************************************************************************/

static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
{
	/* Let gcc optimize it, the unlikely is not really based on
	 * some numbers but jump free code for mismatches seems
	 * more logical. */
741
	if (unlikely(a->value == b->value))
L
Linus Torvalds 已提交
742
		return 0;
743
	else if (a->value < b->value)
L
Linus Torvalds 已提交
744 745 746 747 748
		return -1;
	else
		return 1;
}

749
static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
L
Linus Torvalds 已提交
750
{
751 752
	if (nla_len(nla) >= sizeof(unsigned long)) {
		dst->val = *(unsigned long *) nla_data(nla);
L
Linus Torvalds 已提交
753
		dst->len = sizeof(unsigned long);
754
	} else if (nla_len(nla) == sizeof(u32)) {
755
		dst->val = nla_get_u32(nla);
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
		dst->len = sizeof(u32);
	} else
		return -EINVAL;

	return 0;
}

static void meta_int_apply_extras(struct meta_value *v,
				  struct meta_obj *dst)
{
	if (v->hdr.shift)
		dst->value >>= v->hdr.shift;

	if (v->val)
		dst->value &= v->val;
}

static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
{
775 776 777 778 779 780 781
	if (v->len == sizeof(unsigned long)) {
		if (nla_put(skb, tlv, sizeof(unsigned long), &v->val))
			goto nla_put_failure;
	} else if (v->len == sizeof(u32)) {
		if (nla_put_u32(skb, tlv, v->val))
			goto nla_put_failure;
	}
L
Linus Torvalds 已提交
782 783 784

	return 0;

785
nla_put_failure:
L
Linus Torvalds 已提交
786 787 788 789 790 791 792
	return -1;
}

/**************************************************************************
 * Type specific operations table
 **************************************************************************/

E
Eric Dumazet 已提交
793
struct meta_type_ops {
L
Linus Torvalds 已提交
794 795
	void	(*destroy)(struct meta_value *);
	int	(*compare)(struct meta_obj *, struct meta_obj *);
796
	int	(*change)(struct meta_value *, struct nlattr *);
L
Linus Torvalds 已提交
797 798 799 800
	void	(*apply_extras)(struct meta_value *, struct meta_obj *);
	int	(*dump)(struct sk_buff *, struct meta_value *, int);
};

801
static const struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX + 1] = {
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	[TCF_META_TYPE_VAR] = {
		.destroy = meta_var_destroy,
		.compare = meta_var_compare,
		.change = meta_var_change,
		.apply_extras = meta_var_apply_extras,
		.dump = meta_var_dump
	},
	[TCF_META_TYPE_INT] = {
		.compare = meta_int_compare,
		.change = meta_int_change,
		.apply_extras = meta_int_apply_extras,
		.dump = meta_int_dump
	}
};

817
static inline const struct meta_type_ops *meta_type_ops(struct meta_value *v)
L
Linus Torvalds 已提交
818 819 820 821 822 823 824 825
{
	return &__meta_type_ops[meta_type(v)];
}

/**************************************************************************
 * Core
 **************************************************************************/

826 827
static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
		    struct meta_value *v, struct meta_obj *dst)
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
	int err = 0;

	if (meta_id(v) == TCF_META_ID_VALUE) {
		dst->value = v->val;
		dst->len = v->len;
		return 0;
	}

	meta_ops(v)->get(skb, info, v, dst, &err);
	if (err < 0)
		return err;

	if (meta_type_ops(v)->apply_extras)
E
Eric Dumazet 已提交
842
		meta_type_ops(v)->apply_extras(v, dst);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

	return 0;
}

static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
			 struct tcf_pkt_info *info)
{
	int r;
	struct meta_match *meta = (struct meta_match *) m->data;
	struct meta_obj l_value, r_value;

	if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
	    meta_get(skb, info, &meta->rvalue, &r_value) < 0)
		return 0;

	r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);

	switch (meta->lvalue.hdr.op) {
E
Eric Dumazet 已提交
861 862 863 864 865 866
	case TCF_EM_OPND_EQ:
		return !r;
	case TCF_EM_OPND_LT:
		return r < 0;
	case TCF_EM_OPND_GT:
		return r > 0;
L
Linus Torvalds 已提交
867 868 869 870 871
	}

	return 0;
}

872
static void meta_delete(struct meta_match *meta)
L
Linus Torvalds 已提交
873
{
S
Stephen Hemminger 已提交
874
	if (meta) {
875
		const struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
L
Linus Torvalds 已提交
876

S
Stephen Hemminger 已提交
877 878 879 880
		if (ops && ops->destroy) {
			ops->destroy(&meta->lvalue);
			ops->destroy(&meta->rvalue);
		}
L
Linus Torvalds 已提交
881 882 883 884 885
	}

	kfree(meta);
}

886
static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
L
Linus Torvalds 已提交
887
{
888 889
	if (nla) {
		if (nla_len(nla) == 0)
L
Linus Torvalds 已提交
890 891
			return -EINVAL;

892
		return meta_type_ops(dst)->change(dst, nla);
L
Linus Torvalds 已提交
893 894 895 896 897 898 899
	}

	return 0;
}

static inline int meta_is_supported(struct meta_value *val)
{
E
Eric Dumazet 已提交
900
	return !meta_id(val) || meta_ops(val)->get;
L
Linus Torvalds 已提交
901 902
}

903 904 905 906
static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
	[TCA_EM_META_HDR]	= { .len = sizeof(struct tcf_meta_hdr) },
};

907
static int em_meta_change(struct net *net, void *data, int len,
L
Linus Torvalds 已提交
908 909
			  struct tcf_ematch *m)
{
910
	int err;
911
	struct nlattr *tb[TCA_EM_META_MAX + 1];
L
Linus Torvalds 已提交
912 913
	struct tcf_meta_hdr *hdr;
	struct meta_match *meta = NULL;
914

915
	err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy);
916
	if (err < 0)
L
Linus Torvalds 已提交
917 918
		goto errout;

919
	err = -EINVAL;
920
	if (tb[TCA_EM_META_HDR] == NULL)
L
Linus Torvalds 已提交
921
		goto errout;
922
	hdr = nla_data(tb[TCA_EM_META_HDR]);
L
Linus Torvalds 已提交
923 924 925 926 927 928 929

	if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
	    TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
	    TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
	    TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
		goto errout;

930
	meta = kzalloc(sizeof(*meta), GFP_KERNEL);
931 932
	if (meta == NULL) {
		err = -ENOMEM;
L
Linus Torvalds 已提交
933
		goto errout;
934
	}
L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944

	memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
	memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));

	if (!meta_is_supported(&meta->lvalue) ||
	    !meta_is_supported(&meta->rvalue)) {
		err = -EOPNOTSUPP;
		goto errout;
	}

945 946
	if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
	    meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954 955 956 957 958
		goto errout;

	m->datalen = sizeof(*meta);
	m->data = (unsigned long) meta;

	err = 0;
errout:
	if (err && meta)
		meta_delete(meta);
	return err;
}

959
static void em_meta_destroy(struct tcf_ematch *m)
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967 968
{
	if (m)
		meta_delete((struct meta_match *) m->data);
}

static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
{
	struct meta_match *meta = (struct meta_match *) em->data;
	struct tcf_meta_hdr hdr;
969
	const struct meta_type_ops *ops;
L
Linus Torvalds 已提交
970 971 972 973 974

	memset(&hdr, 0, sizeof(hdr));
	memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
	memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));

975 976
	if (nla_put(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr))
		goto nla_put_failure;
L
Linus Torvalds 已提交
977 978 979 980

	ops = meta_type_ops(&meta->lvalue);
	if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
	    ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
981
		goto nla_put_failure;
L
Linus Torvalds 已提交
982 983 984

	return 0;

985
nla_put_failure:
L
Linus Torvalds 已提交
986
	return -1;
987
}
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

static struct tcf_ematch_ops em_meta_ops = {
	.kind	  = TCF_EM_META,
	.change	  = em_meta_change,
	.match	  = em_meta_match,
	.destroy  = em_meta_destroy,
	.dump	  = em_meta_dump,
	.owner	  = THIS_MODULE,
	.link	  = LIST_HEAD_INIT(em_meta_ops.link)
};

static int __init init_em_meta(void)
{
	return tcf_em_register(&em_meta_ops);
}

1004
static void __exit exit_em_meta(void)
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011 1012
{
	tcf_em_unregister(&em_meta_ops);
}

MODULE_LICENSE("GPL");

module_init(init_em_meta);
module_exit(exit_em_meta);
1013 1014

MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);