act_ife.c 20.3 KB
Newer Older
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * net/sched/ife.c	Inter-FE action based on ForCES WG InterFE LFB
 *
 *		Refer to:
 *		draft-ietf-forces-interfelfb-03
 *		and
 *		netdev01 paper:
 *		"Distributing Linux Traffic Control Classifier-Action
 *		Subsystem"
 *		Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
 *
 *		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.
 *
 * copyright Jamal Hadi Salim (2015)
 *
*/

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/module.h>
#include <linux/init.h>
#include <net/net_namespace.h>
#include <net/netlink.h>
#include <net/pkt_sched.h>
#include <uapi/linux/tc_act/tc_ife.h>
#include <net/tc_act/tc_ife.h>
#include <linux/etherdevice.h>
35
#include <net/ife.h>
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
36

37
static unsigned int ife_net_id;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
38
static int max_metacnt = IFE_META_MAX + 1;
39
static struct tc_action_ops act_ife_ops;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
40 41 42 43 44 45 46 47

static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
	[TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
	[TCA_IFE_DMAC] = { .len = ETH_ALEN},
	[TCA_IFE_SMAC] = { .len = ETH_ALEN},
	[TCA_IFE_TYPE] = { .type = NLA_U16},
};

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
{
	u16 edata = 0;

	if (mi->metaval)
		edata = *(u16 *)mi->metaval;
	else if (metaval)
		edata = metaval;

	if (!edata) /* will not encode */
		return 0;

	edata = htons(edata);
	return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
}
EXPORT_SYMBOL_GPL(ife_encode_meta_u16);

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
{
	if (mi->metaval)
		return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
	else
		return nla_put(skb, mi->metaid, 0, NULL);
}
EXPORT_SYMBOL_GPL(ife_get_meta_u32);

int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
{
	if (metaval || mi->metaval)
		return 8; /* T+L+V == 2+2+4 */

	return 0;
}
EXPORT_SYMBOL_GPL(ife_check_meta_u32);

83 84 85 86 87 88 89 90 91
int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
{
	if (metaval || mi->metaval)
		return 8; /* T+L+(V) == 2+2+(2+2bytepad) */

	return 0;
}
EXPORT_SYMBOL_GPL(ife_check_meta_u16);

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
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
int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
{
	u32 edata = metaval;

	if (mi->metaval)
		edata = *(u32 *)mi->metaval;
	else if (metaval)
		edata = metaval;

	if (!edata) /* will not encode */
		return 0;

	edata = htonl(edata);
	return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
}
EXPORT_SYMBOL_GPL(ife_encode_meta_u32);

int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
{
	if (mi->metaval)
		return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
	else
		return nla_put(skb, mi->metaid, 0, NULL);
}
EXPORT_SYMBOL_GPL(ife_get_meta_u16);

118
int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
119
{
120
	mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
121 122 123 124 125 126 127
	if (!mi->metaval)
		return -ENOMEM;

	return 0;
}
EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);

128
int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
129
{
130
	mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	if (!mi->metaval)
		return -ENOMEM;

	return 0;
}
EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);

void ife_release_meta_gen(struct tcf_meta_info *mi)
{
	kfree(mi->metaval);
}
EXPORT_SYMBOL_GPL(ife_release_meta_gen);

int ife_validate_meta_u32(void *val, int len)
{
146
	if (len == sizeof(u32))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
147 148 149 150 151 152 153 154
		return 0;

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(ife_validate_meta_u32);

int ife_validate_meta_u16(void *val, int len)
{
155 156
	/* length will not include padding */
	if (len == sizeof(u16))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
157 158 159 160 161 162 163 164 165 166 167 168 169
		return 0;

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(ife_validate_meta_u16);

static LIST_HEAD(ifeoplist);
static DEFINE_RWLOCK(ife_mod_lock);

static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
{
	struct tcf_meta_ops *o;

170
	read_lock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
171 172 173 174
	list_for_each_entry(o, &ifeoplist, list) {
		if (o->metaid == metaid) {
			if (!try_module_get(o->owner))
				o = NULL;
175
			read_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
176 177 178
			return o;
		}
	}
179
	read_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
180 181 182 183 184 185 186 187 188 189 190 191 192

	return NULL;
}

int register_ife_op(struct tcf_meta_ops *mops)
{
	struct tcf_meta_ops *m;

	if (!mops->metaid || !mops->metatype || !mops->name ||
	    !mops->check_presence || !mops->encode || !mops->decode ||
	    !mops->get || !mops->alloc)
		return -EINVAL;

193
	write_lock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
194 195 196 197

	list_for_each_entry(m, &ifeoplist, list) {
		if (m->metaid == mops->metaid ||
		    (strcmp(mops->name, m->name) == 0)) {
198
			write_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
199 200 201 202 203 204 205 206
			return -EEXIST;
		}
	}

	if (!mops->release)
		mops->release = ife_release_meta_gen;

	list_add_tail(&mops->list, &ifeoplist);
207
	write_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
208 209 210 211 212 213 214 215 216
	return 0;
}
EXPORT_SYMBOL_GPL(unregister_ife_op);

int unregister_ife_op(struct tcf_meta_ops *mops)
{
	struct tcf_meta_ops *m;
	int err = -ENOENT;

217
	write_lock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
218 219 220 221 222 223 224
	list_for_each_entry(m, &ifeoplist, list) {
		if (m->metaid == mops->metaid) {
			list_del(&mops->list);
			err = 0;
			break;
		}
	}
225
	write_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	return err;
}
EXPORT_SYMBOL_GPL(register_ife_op);

static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
{
	int ret = 0;
	/* XXX: unfortunately cant use nla_policy at this point
	* because a length of 0 is valid in the case of
	* "allow". "use" semantics do enforce for proper
	* length and i couldve use nla_policy but it makes it hard
	* to use it just for that..
	*/
	if (ops->validate)
		return ops->validate(val, len);

	if (ops->metatype == NLA_U32)
		ret = ife_validate_meta_u32(val, len);
	else if (ops->metatype == NLA_U16)
		ret = ife_validate_meta_u16(val, len);

	return ret;
}

251
#ifdef CONFIG_MODULES
252 253 254 255 256 257 258 259 260 261 262 263 264
static const char *ife_meta_id2name(u32 metaid)
{
	switch (metaid) {
	case IFE_META_SKBMARK:
		return "skbmark";
	case IFE_META_PRIO:
		return "skbprio";
	case IFE_META_TCINDEX:
		return "tcindex";
	default:
		return "unknown";
	}
}
265
#endif
266

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
267 268
/* called when adding new meta information
*/
269
static int load_metaops_and_vet(u32 metaid, void *val, int len, bool rtnl_held)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
270 271 272 273 274 275 276
{
	struct tcf_meta_ops *ops = find_ife_oplist(metaid);
	int ret = 0;

	if (!ops) {
		ret = -ENOENT;
#ifdef CONFIG_MODULES
277 278
		if (rtnl_held)
			rtnl_unlock();
279
		request_module("ife-meta-%s", ife_meta_id2name(metaid));
280 281
		if (rtnl_held)
			rtnl_lock();
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		ops = find_ife_oplist(metaid);
#endif
	}

	if (ops) {
		ret = 0;
		if (len)
			ret = ife_validate_metatype(ops, val, len);

		module_put(ops->owner);
	}

	return ret;
}

/* called when adding new meta information
*/
299 300 301
static int __add_metainfo(const struct tcf_meta_ops *ops,
			  struct tcf_ife_info *ife, u32 metaid, void *metaval,
			  int len, bool atomic, bool exists)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
302 303 304 305
{
	struct tcf_meta_info *mi = NULL;
	int ret = 0;

306
	mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
307
	if (!mi)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
308 309 310 311 312
		return -ENOMEM;

	mi->metaid = metaid;
	mi->ops = ops;
	if (len > 0) {
313
		ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
314 315 316 317 318 319
		if (ret != 0) {
			kfree(mi);
			return ret;
		}
	}

320 321
	if (exists)
		spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
322
	list_add_tail(&mi->metalist, &ife->metalist);
323 324
	if (exists)
		spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
325 326 327 328

	return ret;
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342
static int add_metainfo_and_get_ops(const struct tcf_meta_ops *ops,
				    struct tcf_ife_info *ife, u32 metaid,
				    bool exists)
{
	int ret;

	if (!try_module_get(ops->owner))
		return -ENOENT;
	ret = __add_metainfo(ops, ife, metaid, NULL, 0, true, exists);
	if (ret)
		module_put(ops->owner);
	return ret;
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
			int len, bool exists)
{
	const struct tcf_meta_ops *ops = find_ife_oplist(metaid);
	int ret;

	if (!ops)
		return -ENOENT;
	ret = __add_metainfo(ops, ife, metaid, metaval, len, false, exists);
	if (ret)
		/*put back what find_ife_oplist took */
		module_put(ops->owner);
	return ret;
}

358
static int use_all_metadata(struct tcf_ife_info *ife, bool exists)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
359 360 361 362 363
{
	struct tcf_meta_ops *o;
	int rc = 0;
	int installed = 0;

364
	read_lock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
365
	list_for_each_entry(o, &ifeoplist, list) {
366
		rc = add_metainfo_and_get_ops(o, ife, o->metaid, exists);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
367 368 369
		if (rc == 0)
			installed += 1;
	}
370
	read_unlock(&ife_mod_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

	if (installed)
		return 0;
	else
		return -EINVAL;
}

static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
{
	struct tcf_meta_info *e;
	struct nlattr *nest;
	unsigned char *b = skb_tail_pointer(skb);
	int total_encoded = 0;

	/*can only happen on decode */
	if (list_empty(&ife->metalist))
		return 0;

	nest = nla_nest_start(skb, TCA_IFE_METALST);
	if (!nest)
		goto out_nlmsg_trim;

	list_for_each_entry(e, &ife->metalist, metalist) {
		if (!e->ops->get(skb, e))
			total_encoded += 1;
	}

	if (!total_encoded)
		goto out_nlmsg_trim;

	nla_nest_end(skb, nest);

	return 0;

out_nlmsg_trim:
	nlmsg_trim(skb, b);
	return -1;
}

/* under ife->tcf_lock */
411
static void _tcf_ife_cleanup(struct tc_action *a)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
412
{
413
	struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
414 415 416 417 418 419 420 421 422 423
	struct tcf_meta_info *e, *n;

	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
		list_del(&e->metalist);
		if (e->metaval) {
			if (e->ops->release)
				e->ops->release(e);
			else
				kfree(e->metaval);
		}
424
		module_put(e->ops->owner);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
425 426 427 428
		kfree(e);
	}
}

429
static void tcf_ife_cleanup(struct tc_action *a)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
430
{
431
	struct tcf_ife_info *ife = to_ife(a);
432
	struct tcf_ife_params *p;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
433 434

	spin_lock_bh(&ife->tcf_lock);
435
	_tcf_ife_cleanup(a);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
436
	spin_unlock_bh(&ife->tcf_lock);
437 438

	p = rcu_dereference_protected(ife->params, 1);
439 440
	if (p)
		kfree_rcu(p, rcu);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
441 442
}

443
static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
444
			     bool exists, bool rtnl_held)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
445 446 447 448 449 450 451 452 453 454 455
{
	int len = 0;
	int rc = 0;
	int i = 0;
	void *val;

	for (i = 1; i < max_metacnt; i++) {
		if (tb[i]) {
			val = nla_data(tb[i]);
			len = nla_len(tb[i]);

456
			rc = load_metaops_and_vet(i, val, len, rtnl_held);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
457 458 459
			if (rc != 0)
				return rc;

460
			rc = add_metainfo(ife, i, val, len, exists);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
461 462 463 464 465 466 467 468 469
			if (rc)
				return rc;
		}
	}

	return rc;
}

static int tcf_ife_init(struct net *net, struct nlattr *nla,
470
			struct nlattr *est, struct tc_action **a,
471 472
			int ovr, int bind, bool rtnl_held,
			struct netlink_ext_ack *extack)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
473 474 475 476
{
	struct tc_action_net *tn = net_generic(net, ife_net_id);
	struct nlattr *tb[TCA_IFE_MAX + 1];
	struct nlattr *tb2[IFE_META_MAX + 1];
477
	struct tcf_ife_params *p;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
478
	struct tcf_ife_info *ife;
479
	u16 ife_type = ETH_P_IFE;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
480 481 482
	struct tc_ife *parm;
	u8 *daddr = NULL;
	u8 *saddr = NULL;
483 484
	bool exists = false;
	int ret = 0;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
485 486
	int err;

487
	err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy, NULL);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
488 489 490 491 492 493 494 495
	if (err < 0)
		return err;

	if (!tb[TCA_IFE_PARMS])
		return -EINVAL;

	parm = nla_data(tb[TCA_IFE_PARMS]);

496 497 498 499 500 501 502
	/* IFE_DECODE is 0 and indicates the opposite of IFE_ENCODE because
	 * they cannot run as the same time. Check on all other values which
	 * are not supported right now.
	 */
	if (parm->flags & ~IFE_ENCODE)
		return -EINVAL;

503 504 505 506
	p = kzalloc(sizeof(*p), GFP_KERNEL);
	if (!p)
		return -ENOMEM;

507
	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
508 509
	if (err < 0) {
		kfree(p);
510
		return err;
511
	}
512
	exists = err;
513 514
	if (exists && bind) {
		kfree(p);
515
		return 0;
516
	}
517 518

	if (!exists) {
519
		ret = tcf_idr_create(tn, parm->index, est, a, &act_ife_ops,
520
				     bind, true);
521
		if (ret) {
522
			tcf_idr_cleanup(tn, parm->index);
523
			kfree(p);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
524
			return ret;
525
		}
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
526
		ret = ACT_P_CREATED;
527
	} else if (!ovr) {
528
		tcf_idr_release(*a, bind);
529 530
		kfree(p);
		return -EEXIST;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
531 532
	}

533
	ife = to_ife(*a);
534
	p->flags = parm->flags;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
535 536

	if (parm->flags & IFE_ENCODE) {
537 538
		if (tb[TCA_IFE_TYPE])
			ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
539 540 541 542 543 544 545 546
		if (tb[TCA_IFE_DMAC])
			daddr = nla_data(tb[TCA_IFE_DMAC]);
		if (tb[TCA_IFE_SMAC])
			saddr = nla_data(tb[TCA_IFE_SMAC]);
	}

	if (parm->flags & IFE_ENCODE) {
		if (daddr)
547
			ether_addr_copy(p->eth_dst, daddr);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
548
		else
549
			eth_zero_addr(p->eth_dst);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
550 551

		if (saddr)
552
			ether_addr_copy(p->eth_src, saddr);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
553
		else
554
			eth_zero_addr(p->eth_src);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
555

556
		p->eth_type = ife_type;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
557 558
	}

559

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
560 561 562 563 564
	if (ret == ACT_P_CREATED)
		INIT_LIST_HEAD(&ife->metalist);

	if (tb[TCA_IFE_METALST]) {
		err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
565
				       NULL, NULL);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
566 567
		if (err) {
metadata_parse_err:
568
			tcf_idr_release(*a, bind);
569
			kfree(p);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
570 571 572
			return err;
		}

573
		err = populate_metalist(ife, tb2, exists, rtnl_held);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
574 575 576 577 578 579 580 581 582
		if (err)
			goto metadata_parse_err;

	} else {
		/* if no passed metadata allow list or passed allow-all
		 * then here we process by adding as many supported metadatum
		 * as we can. You better have at least one else we are
		 * going to bail out
		 */
583
		err = use_all_metadata(ife, exists);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
584
		if (err) {
585
			tcf_idr_release(*a, bind);
586
			kfree(p);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
587 588 589 590
			return err;
		}
	}

591 592
	if (exists)
		spin_lock_bh(&ife->tcf_lock);
593
	ife->tcf_action = parm->action;
594 595 596
	/* protected by tcf_lock when modifying existing action */
	rcu_swap_protected(ife->params, p, 1);

597 598
	if (exists)
		spin_unlock_bh(&ife->tcf_lock);
599 600
	if (p)
		kfree_rcu(p, rcu);
601

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
602
	if (ret == ACT_P_CREATED)
603
		tcf_idr_insert(tn, *a);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
604 605 606 607 608 609 610 611

	return ret;
}

static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
			int ref)
{
	unsigned char *b = skb_tail_pointer(skb);
612
	struct tcf_ife_info *ife = to_ife(a);
613
	struct tcf_ife_params *p;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
614 615
	struct tc_ife opt = {
		.index = ife->tcf_index,
616 617
		.refcnt = refcount_read(&ife->tcf_refcnt) - ref,
		.bindcnt = atomic_read(&ife->tcf_bindcnt) - bind,
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
618 619 620
	};
	struct tcf_t t;

621 622 623 624 625 626
	spin_lock_bh(&ife->tcf_lock);
	opt.action = ife->tcf_action;
	p = rcu_dereference_protected(ife->params,
				      lockdep_is_held(&ife->tcf_lock));
	opt.flags = p->flags;

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
627 628 629
	if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
		goto nla_put_failure;

630
	tcf_tm_dump(&t, &ife->tcf_tm);
631
	if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
632 633
		goto nla_put_failure;

634 635
	if (!is_zero_ether_addr(p->eth_dst)) {
		if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
636 637 638
			goto nla_put_failure;
	}

639 640
	if (!is_zero_ether_addr(p->eth_src)) {
		if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
641 642 643
			goto nla_put_failure;
	}

644
	if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
645 646 647 648 649 650 651
		goto nla_put_failure;

	if (dump_metalist(skb, ife)) {
		/*ignore failure to dump metalist */
		pr_info("Failed to dump metalist\n");
	}

652
	spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
653 654 655
	return skb->len;

nla_put_failure:
656
	spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
657 658 659 660
	nlmsg_trim(skb, b);
	return -1;
}

661 662
static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
			      u16 metaid, u16 mlen, void *mdata)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
663 664 665 666 667 668 669 670 671 672 673 674 675
{
	struct tcf_meta_info *e;

	/* XXX: use hash to speed up */
	list_for_each_entry(e, &ife->metalist, metalist) {
		if (metaid == e->metaid) {
			if (e->ops) {
				/* We check for decode presence already */
				return e->ops->decode(skb, mdata, mlen);
			}
		}
	}

676
	return -ENOENT;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
677 678 679 680 681
}

static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
			  struct tcf_result *res)
{
682
	struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
683
	int action = ife->tcf_action;
684 685 686
	u8 *ifehdr_end;
	u8 *tlv_data;
	u16 metalen;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
687

688
	bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
689
	tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
690

691 692 693 694 695
	if (skb_at_tc_ingress(skb))
		skb_push(skb, skb->dev->hard_header_len);

	tlv_data = ife_decode(skb, &metalen);
	if (unlikely(!tlv_data)) {
696
		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
697 698 699
		return TC_ACT_SHOT;
	}

700 701 702 703 704
	ifehdr_end = tlv_data + metalen;
	for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
		u8 *curr_data;
		u16 mtype;
		u16 dlen;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
705

706 707 708 709 710 711
		curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype,
						&dlen, NULL);
		if (!curr_data) {
			qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
			return TC_ACT_SHOT;
		}
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
712

713
		if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) {
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
714 715 716
			/* abuse overlimits to count when we receive metadata
			 * but dont have an ops for it
			 */
717 718
			pr_info_ratelimited("Unknown metaid %d dlen %d\n",
					    mtype, dlen);
719
			qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
720
		}
721
	}
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
722

723
	if (WARN_ON(tlv_data != ifehdr_end)) {
724
		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
725
		return TC_ACT_SHOT;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
726 727
	}

728
	skb->protocol = eth_type_trans(skb, skb->dev);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
729
	skb_reset_network_header(skb);
730

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	return action;
}

/*XXX: check if we can do this at install time instead of current
 * send data path
**/
static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
{
	struct tcf_meta_info *e, *n;
	int tot_run_sz = 0, run_sz = 0;

	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
		if (e->ops->check_presence) {
			run_sz = e->ops->check_presence(skb, e);
			tot_run_sz += run_sz;
		}
	}

	return tot_run_sz;
}

static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
753
			  struct tcf_result *res, struct tcf_ife_params *p)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
754
{
755
	struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
756 757 758 759 760 761 762 763 764
	int action = ife->tcf_action;
	struct ethhdr *oethh;	/* outer ether header */
	struct tcf_meta_info *e;
	/*
	   OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
	   where ORIGDATA = original ethernet header ...
	 */
	u16 metalen = ife_get_sz(skb, ife);
	int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
765
	unsigned int skboff = 0;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
766 767
	int new_len = skb->len + hdrm;
	bool exceed_mtu = false;
768 769
	void *ife_meta;
	int err = 0;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
770

771
	if (!skb_at_tc_ingress(skb)) {
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
772 773 774 775
		if (new_len > skb->dev->mtu)
			exceed_mtu = true;
	}

776
	bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
777
	tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
778 779 780 781 782

	if (!metalen) {		/* no metadata to send */
		/* abuse overlimits to count when we allow packet
		 * with no metadata
		 */
783
		qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
784 785 786 787 788
		return action;
	}
	/* could be stupid policy setup or mtu config
	 * so lets be conservative.. */
	if ((action == TC_ACT_SHOT) || exceed_mtu) {
789
		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
790 791 792
		return TC_ACT_SHOT;
	}

793
	if (skb_at_tc_ingress(skb))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
794 795
		skb_push(skb, skb->dev->hard_header_len);

796
	ife_meta = ife_encode(skb, metalen);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
797

798 799
	spin_lock(&ife->tcf_lock);

Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
800 801 802 803 804 805
	/* XXX: we dont have a clever way of telling encode to
	 * not repeat some of the computations that are done by
	 * ops->presence_check...
	 */
	list_for_each_entry(e, &ife->metalist, metalist) {
		if (e->ops->encode) {
806
			err = e->ops->encode(skb, (void *)(ife_meta + skboff),
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
807 808 809 810 811
					     e);
		}
		if (err < 0) {
			/* too corrupt to keep around if overwritten */
			spin_unlock(&ife->tcf_lock);
812
			qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
813 814 815 816
			return TC_ACT_SHOT;
		}
		skboff += err;
	}
817
	spin_unlock(&ife->tcf_lock);
818
	oethh = (struct ethhdr *)skb->data;
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
819

820 821 822 823 824
	if (!is_zero_ether_addr(p->eth_src))
		ether_addr_copy(oethh->h_source, p->eth_src);
	if (!is_zero_ether_addr(p->eth_dst))
		ether_addr_copy(oethh->h_dest, p->eth_dst);
	oethh->h_proto = htons(p->eth_type);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
825

826
	if (skb_at_tc_ingress(skb))
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
827 828 829 830 831 832 833 834
		skb_pull(skb, skb->dev->hard_header_len);

	return action;
}

static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
		       struct tcf_result *res)
{
835
	struct tcf_ife_info *ife = to_ife(a);
836 837 838
	struct tcf_ife_params *p;
	int ret;

839
	p = rcu_dereference_bh(ife->params);
840 841 842 843
	if (p->flags & IFE_ENCODE) {
		ret = tcf_ife_encode(skb, a, res, p);
		return ret;
	}
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
844

845
	return tcf_ife_decode(skb, a, res);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
846 847 848 849
}

static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
			  struct netlink_callback *cb, int type,
850 851
			  const struct tc_action_ops *ops,
			  struct netlink_ext_ack *extack)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
852 853 854
{
	struct tc_action_net *tn = net_generic(net, ife_net_id);

855
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
856 857
}

858
static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
859 860 861
{
	struct tc_action_net *tn = net_generic(net, ife_net_id);

862
	return tcf_idr_search(tn, a, index);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
863 864 865 866 867 868 869 870 871 872 873 874
}

static struct tc_action_ops act_ife_ops = {
	.kind = "ife",
	.type = TCA_ACT_IFE,
	.owner = THIS_MODULE,
	.act = tcf_ife_act,
	.dump = tcf_ife_dump,
	.cleanup = tcf_ife_cleanup,
	.init = tcf_ife_init,
	.walk = tcf_ife_walker,
	.lookup = tcf_ife_search,
875
	.size =	sizeof(struct tcf_ife_info),
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
876 877 878 879 880 881
};

static __net_init int ife_init_net(struct net *net)
{
	struct tc_action_net *tn = net_generic(net, ife_net_id);

882
	return tc_action_net_init(tn, &act_ife_ops);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
883 884
}

885
static void __net_exit ife_exit_net(struct list_head *net_list)
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
886
{
887
	tc_action_net_exit(net_list, ife_net_id);
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
888 889 890 891
}

static struct pernet_operations ife_net_ops = {
	.init = ife_init_net,
892
	.exit_batch = ife_exit_net,
Jamal Hadi Salim's avatar
Jamal Hadi Salim committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	.id   = &ife_net_id,
	.size = sizeof(struct tc_action_net),
};

static int __init ife_init_module(void)
{
	return tcf_register_action(&act_ife_ops, &ife_net_ops);
}

static void __exit ife_cleanup_module(void)
{
	tcf_unregister_action(&act_ife_ops, &ife_net_ops);
}

module_init(ife_init_module);
module_exit(ife_cleanup_module);

MODULE_AUTHOR("Jamal Hadi Salim(2015)");
MODULE_DESCRIPTION("Inter-FE LFB action");
MODULE_LICENSE("GPL");