Linux Audio

Check our new training course

Loading...
v4.17
 
   1/*
   2 * net/core/fib_rules.c		Generic Routing Rules
   3 *
   4 *	This program is free software; you can redistribute it and/or
   5 *	modify it under the terms of the GNU General Public License as
   6 *	published by the Free Software Foundation, version 2.
   7 *
   8 * Authors:	Thomas Graf <tgraf@suug.ch>
   9 */
  10
  11#include <linux/types.h>
  12#include <linux/kernel.h>
  13#include <linux/slab.h>
  14#include <linux/list.h>
  15#include <linux/module.h>
  16#include <net/net_namespace.h>
 
  17#include <net/sock.h>
  18#include <net/fib_rules.h>
  19#include <net/ip_tunnels.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  20
  21static const struct fib_kuid_range fib_kuid_range_unset = {
  22	KUIDT_INIT(0),
  23	KUIDT_INIT(~0),
  24};
  25
  26bool fib_rule_matchall(const struct fib_rule *rule)
  27{
  28	if (rule->iifindex || rule->oifindex || rule->mark || rule->tun_id ||
  29	    rule->flags)
  30		return false;
  31	if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1)
  32		return false;
  33	if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) ||
  34	    !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end))
  35		return false;
  36	if (fib_rule_port_range_set(&rule->sport_range))
  37		return false;
  38	if (fib_rule_port_range_set(&rule->dport_range))
  39		return false;
  40	return true;
  41}
  42EXPORT_SYMBOL_GPL(fib_rule_matchall);
  43
  44int fib_default_rule_add(struct fib_rules_ops *ops,
  45			 u32 pref, u32 table, u32 flags)
  46{
  47	struct fib_rule *r;
  48
  49	r = kzalloc(ops->rule_size, GFP_KERNEL);
  50	if (r == NULL)
  51		return -ENOMEM;
  52
  53	refcount_set(&r->refcnt, 1);
  54	r->action = FR_ACT_TO_TBL;
  55	r->pref = pref;
  56	r->table = table;
  57	r->flags = flags;
  58	r->proto = RTPROT_KERNEL;
  59	r->fr_net = ops->fro_net;
  60	r->uid_range = fib_kuid_range_unset;
  61
  62	r->suppress_prefixlen = -1;
  63	r->suppress_ifgroup = -1;
  64
  65	/* The lock is not required here, the list in unreacheable
  66	 * at the moment this function is called */
  67	list_add_tail(&r->list, &ops->rules_list);
  68	return 0;
  69}
  70EXPORT_SYMBOL(fib_default_rule_add);
  71
  72static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
  73{
  74	struct list_head *pos;
  75	struct fib_rule *rule;
  76
  77	if (!list_empty(&ops->rules_list)) {
  78		pos = ops->rules_list.next;
  79		if (pos->next != &ops->rules_list) {
  80			rule = list_entry(pos->next, struct fib_rule, list);
  81			if (rule->pref)
  82				return rule->pref - 1;
  83		}
  84	}
  85
  86	return 0;
  87}
  88
  89static void notify_rule_change(int event, struct fib_rule *rule,
  90			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  91			       u32 pid);
  92
  93static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
 
  94{
  95	struct fib_rules_ops *ops;
  96
  97	rcu_read_lock();
  98	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  99		if (ops->family == family) {
 100			if (!try_module_get(ops->owner))
 101				ops = NULL;
 102			rcu_read_unlock();
 103			return ops;
 104		}
 105	}
 106	rcu_read_unlock();
 107
 108	return NULL;
 109}
 110
 111static void rules_ops_put(struct fib_rules_ops *ops)
 112{
 113	if (ops)
 114		module_put(ops->owner);
 115}
 116
 117static void flush_route_cache(struct fib_rules_ops *ops)
 118{
 119	if (ops->flush_cache)
 120		ops->flush_cache(ops);
 121}
 122
 123static int __fib_rules_register(struct fib_rules_ops *ops)
 124{
 125	int err = -EEXIST;
 126	struct fib_rules_ops *o;
 127	struct net *net;
 128
 129	net = ops->fro_net;
 130
 131	if (ops->rule_size < sizeof(struct fib_rule))
 132		return -EINVAL;
 133
 134	if (ops->match == NULL || ops->configure == NULL ||
 135	    ops->compare == NULL || ops->fill == NULL ||
 136	    ops->action == NULL)
 137		return -EINVAL;
 138
 139	spin_lock(&net->rules_mod_lock);
 140	list_for_each_entry(o, &net->rules_ops, list)
 141		if (ops->family == o->family)
 142			goto errout;
 143
 144	list_add_tail_rcu(&ops->list, &net->rules_ops);
 145	err = 0;
 146errout:
 147	spin_unlock(&net->rules_mod_lock);
 148
 149	return err;
 150}
 151
 152struct fib_rules_ops *
 153fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
 154{
 155	struct fib_rules_ops *ops;
 156	int err;
 157
 158	ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
 159	if (ops == NULL)
 160		return ERR_PTR(-ENOMEM);
 161
 162	INIT_LIST_HEAD(&ops->rules_list);
 163	ops->fro_net = net;
 164
 165	err = __fib_rules_register(ops);
 166	if (err) {
 167		kfree(ops);
 168		ops = ERR_PTR(err);
 169	}
 170
 171	return ops;
 172}
 173EXPORT_SYMBOL_GPL(fib_rules_register);
 174
 175static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
 176{
 177	struct fib_rule *rule, *tmp;
 178
 179	list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
 180		list_del_rcu(&rule->list);
 181		if (ops->delete)
 182			ops->delete(rule);
 183		fib_rule_put(rule);
 184	}
 185}
 186
 187void fib_rules_unregister(struct fib_rules_ops *ops)
 188{
 189	struct net *net = ops->fro_net;
 190
 191	spin_lock(&net->rules_mod_lock);
 192	list_del_rcu(&ops->list);
 193	spin_unlock(&net->rules_mod_lock);
 194
 195	fib_rules_cleanup_ops(ops);
 196	kfree_rcu(ops, rcu);
 197}
 198EXPORT_SYMBOL_GPL(fib_rules_unregister);
 199
 200static int uid_range_set(struct fib_kuid_range *range)
 201{
 202	return uid_valid(range->start) && uid_valid(range->end);
 203}
 204
 205static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
 206{
 207	struct fib_rule_uid_range *in;
 208	struct fib_kuid_range out;
 209
 210	in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
 211
 212	out.start = make_kuid(current_user_ns(), in->start);
 213	out.end = make_kuid(current_user_ns(), in->end);
 214
 215	return out;
 216}
 217
 218static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
 219{
 220	struct fib_rule_uid_range out = {
 221		from_kuid_munged(current_user_ns(), range->start),
 222		from_kuid_munged(current_user_ns(), range->end)
 223	};
 224
 225	return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
 226}
 227
 228static int nla_get_port_range(struct nlattr *pattr,
 229			      struct fib_rule_port_range *port_range)
 230{
 231	const struct fib_rule_port_range *pr = nla_data(pattr);
 232
 233	if (!fib_rule_port_range_valid(pr))
 234		return -EINVAL;
 235
 236	port_range->start = pr->start;
 237	port_range->end = pr->end;
 238
 239	return 0;
 240}
 241
 242static int nla_put_port_range(struct sk_buff *skb, int attrtype,
 243			      struct fib_rule_port_range *range)
 244{
 245	return nla_put(skb, attrtype, sizeof(*range), range);
 246}
 247
 248static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
 249			  struct flowi *fl, int flags,
 250			  struct fib_lookup_arg *arg)
 251{
 252	int ret = 0;
 253
 254	if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
 
 255		goto out;
 256
 257	if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
 
 258		goto out;
 259
 260	if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
 261		goto out;
 262
 263	if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
 264		goto out;
 265
 266	if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
 267		goto out;
 268
 269	if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
 270	    uid_gt(fl->flowi_uid, rule->uid_range.end))
 271		goto out;
 272
 273	ret = ops->match(rule, fl, flags);
 
 
 
 274out:
 275	return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
 276}
 277
 278int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
 279		     int flags, struct fib_lookup_arg *arg)
 280{
 281	struct fib_rule *rule;
 282	int err;
 283
 284	rcu_read_lock();
 285
 286	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
 287jumped:
 288		if (!fib_rule_match(rule, ops, fl, flags, arg))
 289			continue;
 290
 291		if (rule->action == FR_ACT_GOTO) {
 292			struct fib_rule *target;
 293
 294			target = rcu_dereference(rule->ctarget);
 295			if (target == NULL) {
 296				continue;
 297			} else {
 298				rule = target;
 299				goto jumped;
 300			}
 301		} else if (rule->action == FR_ACT_NOP)
 302			continue;
 303		else
 304			err = ops->action(rule, fl, flags, arg);
 305
 306		if (!err && ops->suppress && ops->suppress(rule, arg))
 
 
 
 
 
 
 307			continue;
 308
 309		if (err != -EAGAIN) {
 310			if ((arg->flags & FIB_LOOKUP_NOREF) ||
 311			    likely(refcount_inc_not_zero(&rule->refcnt))) {
 312				arg->rule = rule;
 313				goto out;
 314			}
 315			break;
 316		}
 317	}
 318
 319	err = -ESRCH;
 320out:
 321	rcu_read_unlock();
 322
 323	return err;
 324}
 325EXPORT_SYMBOL_GPL(fib_rules_lookup);
 326
 327static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
 328				  enum fib_event_type event_type,
 329				  struct fib_rule *rule, int family)
 
 330{
 331	struct fib_rule_notifier_info info = {
 332		.info.family = family,
 
 333		.rule = rule,
 334	};
 335
 336	return call_fib_notifier(nb, net, event_type, &info.info);
 337}
 338
 339static int call_fib_rule_notifiers(struct net *net,
 340				   enum fib_event_type event_type,
 341				   struct fib_rule *rule,
 342				   struct fib_rules_ops *ops,
 343				   struct netlink_ext_ack *extack)
 344{
 345	struct fib_rule_notifier_info info = {
 346		.info.family = ops->family,
 347		.info.extack = extack,
 348		.rule = rule,
 349	};
 350
 351	ops->fib_rules_seq++;
 
 
 352	return call_fib_notifiers(net, event_type, &info.info);
 353}
 354
 355/* Called with rcu_read_lock() */
 356int fib_rules_dump(struct net *net, struct notifier_block *nb, int family)
 
 357{
 358	struct fib_rules_ops *ops;
 359	struct fib_rule *rule;
 
 360
 361	ops = lookup_rules_ops(net, family);
 362	if (!ops)
 363		return -EAFNOSUPPORT;
 364	list_for_each_entry_rcu(rule, &ops->rules_list, list)
 365		call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule,
 366				       family);
 
 
 
 367	rules_ops_put(ops);
 368
 369	return 0;
 370}
 371EXPORT_SYMBOL_GPL(fib_rules_dump);
 372
 373unsigned int fib_rules_seq_read(struct net *net, int family)
 374{
 375	unsigned int fib_rules_seq;
 376	struct fib_rules_ops *ops;
 377
 378	ASSERT_RTNL();
 379
 380	ops = lookup_rules_ops(net, family);
 381	if (!ops)
 382		return 0;
 383	fib_rules_seq = ops->fib_rules_seq;
 
 384	rules_ops_put(ops);
 385
 386	return fib_rules_seq;
 387}
 388EXPORT_SYMBOL_GPL(fib_rules_seq_read);
 389
 390static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
 391			    struct fib_rules_ops *ops)
 392{
 393	int err = -EINVAL;
 394
 395	if (frh->src_len)
 396		if (tb[FRA_SRC] == NULL ||
 397		    frh->src_len > (ops->addr_size * 8) ||
 398		    nla_len(tb[FRA_SRC]) != ops->addr_size)
 399			goto errout;
 400
 401	if (frh->dst_len)
 402		if (tb[FRA_DST] == NULL ||
 403		    frh->dst_len > (ops->addr_size * 8) ||
 404		    nla_len(tb[FRA_DST]) != ops->addr_size)
 405			goto errout;
 406
 407	err = 0;
 408errout:
 409	return err;
 410}
 411
 412static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
 413		       struct nlattr **tb, struct fib_rule *rule)
 414{
 415	struct fib_rule *r;
 416
 417	list_for_each_entry(r, &ops->rules_list, list) {
 418		if (r->action != rule->action)
 419			continue;
 420
 421		if (r->table != rule->table)
 422			continue;
 423
 424		if (r->pref != rule->pref)
 425			continue;
 426
 427		if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
 
 428			continue;
 429
 430		if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
 
 431			continue;
 432
 433		if (r->mark != rule->mark)
 434			continue;
 435
 436		if (r->mark_mask != rule->mark_mask)
 
 437			continue;
 438
 439		if (r->tun_id != rule->tun_id)
 
 
 
 
 
 
 
 440			continue;
 441
 442		if (r->fr_net != rule->fr_net)
 443			continue;
 444
 445		if (r->l3mdev != rule->l3mdev)
 446			continue;
 447
 448		if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
 449		    !uid_eq(r->uid_range.end, rule->uid_range.end))
 
 450			continue;
 451
 452		if (r->ip_proto != rule->ip_proto)
 453			continue;
 454
 455		if (!fib_rule_port_range_compare(&r->sport_range,
 
 
 
 
 456						 &rule->sport_range))
 457			continue;
 458
 459		if (!fib_rule_port_range_compare(&r->dport_range,
 
 460						 &rule->dport_range))
 461			continue;
 462
 463		if (!ops->compare(r, frh, tb))
 464			continue;
 465		return 1;
 466	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 467	return 0;
 468}
 
 
 
 
 
 
 
 
 469
 470int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 471		   struct netlink_ext_ack *extack)
 
 
 
 
 472{
 473	struct net *net = sock_net(skb->sk);
 474	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 475	struct fib_rules_ops *ops = NULL;
 476	struct fib_rule *rule, *r, *last = NULL;
 477	struct nlattr *tb[FRA_MAX+1];
 478	int err = -EINVAL, unresolved = 0;
 479
 480	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
 481		goto errout;
 482
 483	ops = lookup_rules_ops(net, frh->family);
 484	if (ops == NULL) {
 485		err = -EAFNOSUPPORT;
 486		goto errout;
 
 
 487	}
 488
 489	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
 490	if (err < 0)
 491		goto errout;
 492
 493	err = validate_rulemsg(frh, tb, ops);
 494	if (err < 0)
 495		goto errout;
 496
 497	rule = kzalloc(ops->rule_size, GFP_KERNEL);
 498	if (rule == NULL) {
 499		err = -ENOMEM;
 500		goto errout;
 501	}
 502	refcount_set(&rule->refcnt, 1);
 503	rule->fr_net = net;
 504
 505	rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
 506	                              : fib_default_rule_pref(ops);
 
 
 
 
 507
 508	rule->proto = tb[FRA_PROTOCOL] ?
 509		nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
 510
 511	if (tb[FRA_IIFNAME]) {
 512		struct net_device *dev;
 513
 514		rule->iifindex = -1;
 515		nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
 516		dev = __dev_get_by_name(net, rule->iifname);
 517		if (dev)
 518			rule->iifindex = dev->ifindex;
 519	}
 520
 521	if (tb[FRA_OIFNAME]) {
 522		struct net_device *dev;
 523
 524		rule->oifindex = -1;
 525		nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
 526		dev = __dev_get_by_name(net, rule->oifname);
 527		if (dev)
 528			rule->oifindex = dev->ifindex;
 529	}
 530
 531	if (tb[FRA_FWMARK]) {
 532		rule->mark = nla_get_u32(tb[FRA_FWMARK]);
 533		if (rule->mark)
 534			/* compatibility: if the mark value is non-zero all bits
 535			 * are compared unless a mask is explicitly specified.
 536			 */
 537			rule->mark_mask = 0xFFFFFFFF;
 538	}
 539
 540	if (tb[FRA_FWMASK])
 541		rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
 542
 543	if (tb[FRA_TUN_ID])
 544		rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
 545
 546	err = -EINVAL;
 547	if (tb[FRA_L3MDEV]) {
 548#ifdef CONFIG_NET_L3_MASTER_DEV
 549		rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
 550		if (rule->l3mdev != 1)
 551#endif
 552			goto errout_free;
 553	}
 554
 555	rule->action = frh->action;
 556	rule->flags = frh->flags;
 557	rule->table = frh_get_table(frh, tb);
 558	if (tb[FRA_SUPPRESS_PREFIXLEN])
 559		rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
 560	else
 561		rule->suppress_prefixlen = -1;
 562
 563	if (tb[FRA_SUPPRESS_IFGROUP])
 564		rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
 565	else
 566		rule->suppress_ifgroup = -1;
 567
 568	if (tb[FRA_GOTO]) {
 569		if (rule->action != FR_ACT_GOTO)
 
 570			goto errout_free;
 
 571
 572		rule->target = nla_get_u32(tb[FRA_GOTO]);
 573		/* Backward jumps are prohibited to avoid endless loops */
 574		if (rule->target <= rule->pref)
 
 575			goto errout_free;
 576
 577		list_for_each_entry(r, &ops->rules_list, list) {
 578			if (r->pref == rule->target) {
 579				RCU_INIT_POINTER(rule->ctarget, r);
 580				break;
 581			}
 582		}
 583
 584		if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
 585			unresolved = 1;
 586	} else if (rule->action == FR_ACT_GOTO)
 587		goto errout_free;
 
 588
 589	if (rule->l3mdev && rule->table)
 
 590		goto errout_free;
 
 591
 592	if (tb[FRA_UID_RANGE]) {
 593		if (current_user_ns() != net->user_ns) {
 594			err = -EPERM;
 
 595			goto errout_free;
 596		}
 597
 598		rule->uid_range = nla_get_kuid_range(tb);
 599
 600		if (!uid_range_set(&rule->uid_range) ||
 601		    !uid_lte(rule->uid_range.start, rule->uid_range.end))
 
 602			goto errout_free;
 
 603	} else {
 604		rule->uid_range = fib_kuid_range_unset;
 605	}
 606
 607	if (tb[FRA_IP_PROTO])
 608		rule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
 609
 610	if (tb[FRA_SPORT_RANGE]) {
 611		err = nla_get_port_range(tb[FRA_SPORT_RANGE],
 612					 &rule->sport_range);
 613		if (err)
 
 614			goto errout_free;
 
 615	}
 616
 617	if (tb[FRA_DPORT_RANGE]) {
 618		err = nla_get_port_range(tb[FRA_DPORT_RANGE],
 619					 &rule->dport_range);
 620		if (err)
 
 621			goto errout_free;
 
 622	}
 623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 624	if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
 625	    rule_exists(ops, frh, tb, rule)) {
 626		err = -EEXIST;
 627		goto errout_free;
 628	}
 629
 630	err = ops->configure(rule, skb, frh, tb);
 631	if (err < 0)
 632		goto errout_free;
 633
 634	err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
 635				      extack);
 636	if (err < 0)
 637		goto errout_free;
 638
 639	list_for_each_entry(r, &ops->rules_list, list) {
 
 
 
 
 
 
 
 
 
 
 640		if (r->pref > rule->pref)
 641			break;
 642		last = r;
 643	}
 644
 645	if (last)
 646		list_add_rcu(&rule->list, &last->list);
 647	else
 648		list_add_rcu(&rule->list, &ops->rules_list);
 649
 650	if (ops->unresolved_rules) {
 651		/*
 652		 * There are unresolved goto rules in the list, check if
 653		 * any of them are pointing to this new rule.
 654		 */
 655		list_for_each_entry(r, &ops->rules_list, list) {
 656			if (r->action == FR_ACT_GOTO &&
 657			    r->target == rule->pref &&
 658			    rtnl_dereference(r->ctarget) == NULL) {
 659				rcu_assign_pointer(r->ctarget, rule);
 660				if (--ops->unresolved_rules == 0)
 661					break;
 662			}
 663		}
 664	}
 665
 666	if (rule->action == FR_ACT_GOTO)
 667		ops->nr_goto_rules++;
 668
 669	if (unresolved)
 670		ops->unresolved_rules++;
 671
 672	if (rule->tun_id)
 673		ip_tunnel_need_metadata();
 674
 675	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
 676	flush_route_cache(ops);
 677	rules_ops_put(ops);
 678	return 0;
 679
 680errout_free:
 681	kfree(rule);
 682errout:
 683	rules_ops_put(ops);
 684	return err;
 685}
 686EXPORT_SYMBOL_GPL(fib_nl_newrule);
 687
 688int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 689		   struct netlink_ext_ack *extack)
 690{
 691	struct net *net = sock_net(skb->sk);
 692	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 693	struct fib_rule_port_range sprange = {0, 0};
 694	struct fib_rule_port_range dprange = {0, 0};
 695	struct fib_rules_ops *ops = NULL;
 696	struct fib_rule *rule, *r;
 697	struct nlattr *tb[FRA_MAX+1];
 698	struct fib_kuid_range range;
 699	int err = -EINVAL;
 
 700
 701	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
 
 702		goto errout;
 
 703
 704	ops = lookup_rules_ops(net, frh->family);
 705	if (ops == NULL) {
 706		err = -EAFNOSUPPORT;
 
 707		goto errout;
 708	}
 709
 710	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
 711	if (err < 0)
 
 
 712		goto errout;
 
 713
 714	err = validate_rulemsg(frh, tb, ops);
 715	if (err < 0)
 716		goto errout;
 717
 718	if (tb[FRA_UID_RANGE]) {
 719		range = nla_get_kuid_range(tb);
 720		if (!uid_range_set(&range)) {
 721			err = -EINVAL;
 722			goto errout;
 723		}
 724	} else {
 725		range = fib_kuid_range_unset;
 726	}
 727
 728	if (tb[FRA_SPORT_RANGE]) {
 729		err = nla_get_port_range(tb[FRA_SPORT_RANGE],
 730					 &sprange);
 731		if (err)
 732			goto errout;
 733	}
 734
 735	if (tb[FRA_DPORT_RANGE]) {
 736		err = nla_get_port_range(tb[FRA_DPORT_RANGE],
 737					 &dprange);
 738		if (err)
 739			goto errout;
 740	}
 741
 742	list_for_each_entry(rule, &ops->rules_list, list) {
 743		if (tb[FRA_PROTOCOL] &&
 744		    (rule->proto != nla_get_u8(tb[FRA_PROTOCOL])))
 745			continue;
 746
 747		if (frh->action && (frh->action != rule->action))
 748			continue;
 749
 750		if (frh_get_table(frh, tb) &&
 751		    (frh_get_table(frh, tb) != rule->table))
 752			continue;
 753
 754		if (tb[FRA_PRIORITY] &&
 755		    (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
 756			continue;
 757
 758		if (tb[FRA_IIFNAME] &&
 759		    nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
 760			continue;
 761
 762		if (tb[FRA_OIFNAME] &&
 763		    nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
 764			continue;
 765
 766		if (tb[FRA_FWMARK] &&
 767		    (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
 768			continue;
 769
 770		if (tb[FRA_FWMASK] &&
 771		    (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
 772			continue;
 773
 774		if (tb[FRA_TUN_ID] &&
 775		    (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
 776			continue;
 777
 778		if (tb[FRA_L3MDEV] &&
 779		    (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
 780			continue;
 781
 782		if (uid_range_set(&range) &&
 783		    (!uid_eq(rule->uid_range.start, range.start) ||
 784		     !uid_eq(rule->uid_range.end, range.end)))
 785			continue;
 786
 787		if (tb[FRA_IP_PROTO] &&
 788		    (rule->ip_proto != nla_get_u8(tb[FRA_IP_PROTO])))
 789			continue;
 790
 791		if (fib_rule_port_range_set(&sprange) &&
 792		    !fib_rule_port_range_compare(&rule->sport_range, &sprange))
 793			continue;
 794
 795		if (fib_rule_port_range_set(&dprange) &&
 796		    !fib_rule_port_range_compare(&rule->dport_range, &dprange))
 797			continue;
 798
 799		if (!ops->compare(rule, frh, tb))
 800			continue;
 801
 802		if (rule->flags & FIB_RULE_PERMANENT) {
 803			err = -EPERM;
 804			goto errout;
 805		}
 806
 807		if (ops->delete) {
 808			err = ops->delete(rule);
 809			if (err)
 810				goto errout;
 811		}
 812
 813		if (rule->tun_id)
 814			ip_tunnel_unneed_metadata();
 815
 816		list_del_rcu(&rule->list);
 817
 818		if (rule->action == FR_ACT_GOTO) {
 819			ops->nr_goto_rules--;
 820			if (rtnl_dereference(rule->ctarget) == NULL)
 821				ops->unresolved_rules--;
 822		}
 823
 824		/*
 825		 * Check if this rule is a target to any of them. If so,
 826		 * adjust to the next one with the same preference or
 827		 * disable them. As this operation is eventually very
 828		 * expensive, it is only performed if goto rules, except
 829		 * current if it is goto rule, have actually been added.
 830		 */
 831		if (ops->nr_goto_rules > 0) {
 832			struct fib_rule *n;
 833
 834			n = list_next_entry(rule, list);
 835			if (&n->list == &ops->rules_list || n->pref != rule->pref)
 836				n = NULL;
 837			list_for_each_entry(r, &ops->rules_list, list) {
 838				if (rtnl_dereference(r->ctarget) != rule)
 839					continue;
 840				rcu_assign_pointer(r->ctarget, n);
 841				if (!n)
 842					ops->unresolved_rules++;
 843			}
 844		}
 845
 846		call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
 847					NULL);
 848		notify_rule_change(RTM_DELRULE, rule, ops, nlh,
 849				   NETLINK_CB(skb).portid);
 850		fib_rule_put(rule);
 851		flush_route_cache(ops);
 852		rules_ops_put(ops);
 853		return 0;
 854	}
 855
 856	err = -ENOENT;
 
 
 
 
 
 
 
 
 
 857errout:
 
 858	rules_ops_put(ops);
 859	return err;
 860}
 861EXPORT_SYMBOL_GPL(fib_nl_delrule);
 862
 863static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
 864					 struct fib_rule *rule)
 865{
 866	size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
 867			 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
 868			 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
 869			 + nla_total_size(4) /* FRA_PRIORITY */
 870			 + nla_total_size(4) /* FRA_TABLE */
 871			 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
 872			 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
 873			 + nla_total_size(4) /* FRA_FWMARK */
 874			 + nla_total_size(4) /* FRA_FWMASK */
 875			 + nla_total_size_64bit(8) /* FRA_TUN_ID */
 876			 + nla_total_size(sizeof(struct fib_kuid_range))
 877			 + nla_total_size(1) /* FRA_PROTOCOL */
 878			 + nla_total_size(1) /* FRA_IP_PROTO */
 879			 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
 880			 + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
 881
 882	if (ops->nlmsg_payload)
 883		payload += ops->nlmsg_payload(rule);
 884
 885	return payload;
 886}
 887
 888static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
 889			    u32 pid, u32 seq, int type, int flags,
 890			    struct fib_rules_ops *ops)
 891{
 892	struct nlmsghdr *nlh;
 893	struct fib_rule_hdr *frh;
 894
 895	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
 896	if (nlh == NULL)
 897		return -EMSGSIZE;
 898
 899	frh = nlmsg_data(nlh);
 900	frh->family = ops->family;
 901	frh->table = rule->table;
 902	if (nla_put_u32(skb, FRA_TABLE, rule->table))
 903		goto nla_put_failure;
 904	if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
 905		goto nla_put_failure;
 906	frh->res1 = 0;
 907	frh->res2 = 0;
 908	frh->action = rule->action;
 909	frh->flags = rule->flags;
 910
 911	if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
 912		goto nla_put_failure;
 913
 914	if (rule->action == FR_ACT_GOTO &&
 915	    rcu_access_pointer(rule->ctarget) == NULL)
 916		frh->flags |= FIB_RULE_UNRESOLVED;
 917
 918	if (rule->iifname[0]) {
 919		if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
 920			goto nla_put_failure;
 921		if (rule->iifindex == -1)
 922			frh->flags |= FIB_RULE_IIF_DETACHED;
 923	}
 924
 925	if (rule->oifname[0]) {
 926		if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
 927			goto nla_put_failure;
 928		if (rule->oifindex == -1)
 929			frh->flags |= FIB_RULE_OIF_DETACHED;
 930	}
 931
 932	if ((rule->pref &&
 933	     nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
 934	    (rule->mark &&
 935	     nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
 936	    ((rule->mark_mask || rule->mark) &&
 937	     nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
 938	    (rule->target &&
 939	     nla_put_u32(skb, FRA_GOTO, rule->target)) ||
 940	    (rule->tun_id &&
 941	     nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
 942	    (rule->l3mdev &&
 943	     nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
 944	    (uid_range_set(&rule->uid_range) &&
 945	     nla_put_uid_range(skb, &rule->uid_range)) ||
 946	    (fib_rule_port_range_set(&rule->sport_range) &&
 947	     nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
 948	    (fib_rule_port_range_set(&rule->dport_range) &&
 949	     nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
 950	    (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
 951		goto nla_put_failure;
 952
 953	if (rule->suppress_ifgroup != -1) {
 954		if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
 955			goto nla_put_failure;
 956	}
 957
 958	if (ops->fill(rule, skb, frh) < 0)
 959		goto nla_put_failure;
 960
 961	nlmsg_end(skb, nlh);
 962	return 0;
 963
 964nla_put_failure:
 965	nlmsg_cancel(skb, nlh);
 966	return -EMSGSIZE;
 967}
 968
 969static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
 970		      struct fib_rules_ops *ops)
 971{
 972	int idx = 0;
 973	struct fib_rule *rule;
 974	int err = 0;
 975
 976	rcu_read_lock();
 977	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
 978		if (idx < cb->args[1])
 979			goto skip;
 980
 981		err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
 982				       cb->nlh->nlmsg_seq, RTM_NEWRULE,
 983				       NLM_F_MULTI, ops);
 984		if (err)
 985			break;
 986skip:
 987		idx++;
 988	}
 989	rcu_read_unlock();
 990	cb->args[1] = idx;
 991	rules_ops_put(ops);
 992
 993	return err;
 994}
 995
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 996static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
 997{
 
 998	struct net *net = sock_net(skb->sk);
 999	struct fib_rules_ops *ops;
1000	int idx = 0, family;
 
 
 
1001
1002	family = rtnl_msg_family(cb->nlh);
 
 
 
 
1003	if (family != AF_UNSPEC) {
1004		/* Protocol specific dump request */
1005		ops = lookup_rules_ops(net, family);
1006		if (ops == NULL)
1007			return -EAFNOSUPPORT;
1008
1009		dump_rules(skb, cb, ops);
1010
1011		return skb->len;
1012	}
1013
 
1014	rcu_read_lock();
1015	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
1016		if (idx < cb->args[0] || !try_module_get(ops->owner))
1017			goto skip;
1018
1019		if (dump_rules(skb, cb, ops) < 0)
 
1020			break;
1021
1022		cb->args[1] = 0;
1023skip:
1024		idx++;
1025	}
1026	rcu_read_unlock();
1027	cb->args[0] = idx;
1028
1029	return skb->len;
1030}
1031
1032static void notify_rule_change(int event, struct fib_rule *rule,
1033			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1034			       u32 pid)
1035{
1036	struct net *net;
1037	struct sk_buff *skb;
1038	int err = -ENOBUFS;
1039
1040	net = ops->fro_net;
1041	skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
1042	if (skb == NULL)
1043		goto errout;
1044
1045	err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
1046	if (err < 0) {
1047		/* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1048		WARN_ON(err == -EMSGSIZE);
1049		kfree_skb(skb);
1050		goto errout;
1051	}
1052
1053	rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1054	return;
1055errout:
1056	if (err < 0)
1057		rtnl_set_sk_err(net, ops->nlgroup, err);
1058}
1059
1060static void attach_rules(struct list_head *rules, struct net_device *dev)
1061{
1062	struct fib_rule *rule;
1063
1064	list_for_each_entry(rule, rules, list) {
1065		if (rule->iifindex == -1 &&
1066		    strcmp(dev->name, rule->iifname) == 0)
1067			rule->iifindex = dev->ifindex;
1068		if (rule->oifindex == -1 &&
1069		    strcmp(dev->name, rule->oifname) == 0)
1070			rule->oifindex = dev->ifindex;
1071	}
1072}
1073
1074static void detach_rules(struct list_head *rules, struct net_device *dev)
1075{
1076	struct fib_rule *rule;
1077
1078	list_for_each_entry(rule, rules, list) {
1079		if (rule->iifindex == dev->ifindex)
1080			rule->iifindex = -1;
1081		if (rule->oifindex == dev->ifindex)
1082			rule->oifindex = -1;
1083	}
1084}
1085
1086
1087static int fib_rules_event(struct notifier_block *this, unsigned long event,
1088			   void *ptr)
1089{
1090	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1091	struct net *net = dev_net(dev);
1092	struct fib_rules_ops *ops;
1093
1094	ASSERT_RTNL();
1095
1096	switch (event) {
1097	case NETDEV_REGISTER:
1098		list_for_each_entry(ops, &net->rules_ops, list)
1099			attach_rules(&ops->rules_list, dev);
1100		break;
1101
1102	case NETDEV_CHANGENAME:
1103		list_for_each_entry(ops, &net->rules_ops, list) {
1104			detach_rules(&ops->rules_list, dev);
1105			attach_rules(&ops->rules_list, dev);
1106		}
1107		break;
1108
1109	case NETDEV_UNREGISTER:
1110		list_for_each_entry(ops, &net->rules_ops, list)
1111			detach_rules(&ops->rules_list, dev);
1112		break;
1113	}
1114
1115	return NOTIFY_DONE;
1116}
1117
1118static struct notifier_block fib_rules_notifier = {
1119	.notifier_call = fib_rules_event,
1120};
1121
1122static int __net_init fib_rules_net_init(struct net *net)
1123{
1124	INIT_LIST_HEAD(&net->rules_ops);
1125	spin_lock_init(&net->rules_mod_lock);
1126	return 0;
1127}
1128
1129static void __net_exit fib_rules_net_exit(struct net *net)
1130{
1131	WARN_ON_ONCE(!list_empty(&net->rules_ops));
1132}
1133
1134static struct pernet_operations fib_rules_net_ops = {
1135	.init = fib_rules_net_init,
1136	.exit = fib_rules_net_exit,
1137};
1138
 
 
 
 
 
 
 
1139static int __init fib_rules_init(void)
1140{
1141	int err;
1142	rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1143	rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1144	rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
1145
1146	err = register_pernet_subsys(&fib_rules_net_ops);
1147	if (err < 0)
1148		goto fail;
1149
1150	err = register_netdevice_notifier(&fib_rules_notifier);
1151	if (err < 0)
1152		goto fail_unregister;
1153
1154	return 0;
1155
1156fail_unregister:
1157	unregister_pernet_subsys(&fib_rules_net_ops);
1158fail:
1159	rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1160	rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1161	rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1162	return err;
1163}
1164
1165subsys_initcall(fib_rules_init);
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * net/core/fib_rules.c		Generic Routing Rules
   4 *
 
 
 
 
   5 * Authors:	Thomas Graf <tgraf@suug.ch>
   6 */
   7
   8#include <linux/types.h>
   9#include <linux/kernel.h>
  10#include <linux/slab.h>
  11#include <linux/list.h>
  12#include <linux/module.h>
  13#include <net/net_namespace.h>
  14#include <net/inet_dscp.h>
  15#include <net/sock.h>
  16#include <net/fib_rules.h>
  17#include <net/ip_tunnels.h>
  18#include <linux/indirect_call_wrapper.h>
  19
  20#if defined(CONFIG_IPV6) && defined(CONFIG_IPV6_MULTIPLE_TABLES)
  21#ifdef CONFIG_IP_MULTIPLE_TABLES
  22#define INDIRECT_CALL_MT(f, f2, f1, ...) \
  23	INDIRECT_CALL_INET(f, f2, f1, __VA_ARGS__)
  24#else
  25#define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f2, __VA_ARGS__)
  26#endif
  27#elif defined(CONFIG_IP_MULTIPLE_TABLES)
  28#define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
  29#else
  30#define INDIRECT_CALL_MT(f, f2, f1, ...) f(__VA_ARGS__)
  31#endif
  32
  33static const struct fib_kuid_range fib_kuid_range_unset = {
  34	KUIDT_INIT(0),
  35	KUIDT_INIT(~0),
  36};
  37
  38bool fib_rule_matchall(const struct fib_rule *rule)
  39{
  40	if (READ_ONCE(rule->iifindex) || READ_ONCE(rule->oifindex) ||
  41	    rule->mark || rule->tun_id || rule->flags)
  42		return false;
  43	if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1)
  44		return false;
  45	if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) ||
  46	    !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end))
  47		return false;
  48	if (fib_rule_port_range_set(&rule->sport_range))
  49		return false;
  50	if (fib_rule_port_range_set(&rule->dport_range))
  51		return false;
  52	return true;
  53}
  54EXPORT_SYMBOL_GPL(fib_rule_matchall);
  55
  56int fib_default_rule_add(struct fib_rules_ops *ops,
  57			 u32 pref, u32 table)
  58{
  59	struct fib_rule *r;
  60
  61	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
  62	if (r == NULL)
  63		return -ENOMEM;
  64
  65	refcount_set(&r->refcnt, 1);
  66	r->action = FR_ACT_TO_TBL;
  67	r->pref = pref;
  68	r->table = table;
 
  69	r->proto = RTPROT_KERNEL;
  70	r->fr_net = ops->fro_net;
  71	r->uid_range = fib_kuid_range_unset;
  72
  73	r->suppress_prefixlen = -1;
  74	r->suppress_ifgroup = -1;
  75
  76	/* The lock is not required here, the list in unreachable
  77	 * at the moment this function is called */
  78	list_add_tail(&r->list, &ops->rules_list);
  79	return 0;
  80}
  81EXPORT_SYMBOL(fib_default_rule_add);
  82
  83static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
  84{
  85	struct list_head *pos;
  86	struct fib_rule *rule;
  87
  88	if (!list_empty(&ops->rules_list)) {
  89		pos = ops->rules_list.next;
  90		if (pos->next != &ops->rules_list) {
  91			rule = list_entry(pos->next, struct fib_rule, list);
  92			if (rule->pref)
  93				return rule->pref - 1;
  94		}
  95	}
  96
  97	return 0;
  98}
  99
 100static void notify_rule_change(int event, struct fib_rule *rule,
 101			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
 102			       u32 pid);
 103
 104static struct fib_rules_ops *lookup_rules_ops(const struct net *net,
 105					      int family)
 106{
 107	struct fib_rules_ops *ops;
 108
 109	rcu_read_lock();
 110	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
 111		if (ops->family == family) {
 112			if (!try_module_get(ops->owner))
 113				ops = NULL;
 114			rcu_read_unlock();
 115			return ops;
 116		}
 117	}
 118	rcu_read_unlock();
 119
 120	return NULL;
 121}
 122
 123static void rules_ops_put(struct fib_rules_ops *ops)
 124{
 125	if (ops)
 126		module_put(ops->owner);
 127}
 128
 129static void flush_route_cache(struct fib_rules_ops *ops)
 130{
 131	if (ops->flush_cache)
 132		ops->flush_cache(ops);
 133}
 134
 135static int __fib_rules_register(struct fib_rules_ops *ops)
 136{
 137	int err = -EEXIST;
 138	struct fib_rules_ops *o;
 139	struct net *net;
 140
 141	net = ops->fro_net;
 142
 143	if (ops->rule_size < sizeof(struct fib_rule))
 144		return -EINVAL;
 145
 146	if (ops->match == NULL || ops->configure == NULL ||
 147	    ops->compare == NULL || ops->fill == NULL ||
 148	    ops->action == NULL)
 149		return -EINVAL;
 150
 151	spin_lock(&net->rules_mod_lock);
 152	list_for_each_entry(o, &net->rules_ops, list)
 153		if (ops->family == o->family)
 154			goto errout;
 155
 156	list_add_tail_rcu(&ops->list, &net->rules_ops);
 157	err = 0;
 158errout:
 159	spin_unlock(&net->rules_mod_lock);
 160
 161	return err;
 162}
 163
 164struct fib_rules_ops *
 165fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
 166{
 167	struct fib_rules_ops *ops;
 168	int err;
 169
 170	ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
 171	if (ops == NULL)
 172		return ERR_PTR(-ENOMEM);
 173
 174	INIT_LIST_HEAD(&ops->rules_list);
 175	ops->fro_net = net;
 176
 177	err = __fib_rules_register(ops);
 178	if (err) {
 179		kfree(ops);
 180		ops = ERR_PTR(err);
 181	}
 182
 183	return ops;
 184}
 185EXPORT_SYMBOL_GPL(fib_rules_register);
 186
 187static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
 188{
 189	struct fib_rule *rule, *tmp;
 190
 191	list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
 192		list_del_rcu(&rule->list);
 193		if (ops->delete)
 194			ops->delete(rule);
 195		fib_rule_put(rule);
 196	}
 197}
 198
 199void fib_rules_unregister(struct fib_rules_ops *ops)
 200{
 201	struct net *net = ops->fro_net;
 202
 203	spin_lock(&net->rules_mod_lock);
 204	list_del_rcu(&ops->list);
 205	spin_unlock(&net->rules_mod_lock);
 206
 207	fib_rules_cleanup_ops(ops);
 208	kfree_rcu(ops, rcu);
 209}
 210EXPORT_SYMBOL_GPL(fib_rules_unregister);
 211
 212static int uid_range_set(struct fib_kuid_range *range)
 213{
 214	return uid_valid(range->start) && uid_valid(range->end);
 215}
 216
 217static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
 218{
 219	struct fib_rule_uid_range *in;
 220	struct fib_kuid_range out;
 221
 222	in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
 223
 224	out.start = make_kuid(current_user_ns(), in->start);
 225	out.end = make_kuid(current_user_ns(), in->end);
 226
 227	return out;
 228}
 229
 230static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
 231{
 232	struct fib_rule_uid_range out = {
 233		from_kuid_munged(current_user_ns(), range->start),
 234		from_kuid_munged(current_user_ns(), range->end)
 235	};
 236
 237	return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
 238}
 239
 240static int nla_get_port_range(struct nlattr *pattr,
 241			      struct fib_rule_port_range *port_range)
 242{
 243	const struct fib_rule_port_range *pr = nla_data(pattr);
 244
 245	if (!fib_rule_port_range_valid(pr))
 246		return -EINVAL;
 247
 248	port_range->start = pr->start;
 249	port_range->end = pr->end;
 250
 251	return 0;
 252}
 253
 254static int nla_put_port_range(struct sk_buff *skb, int attrtype,
 255			      struct fib_rule_port_range *range)
 256{
 257	return nla_put(skb, attrtype, sizeof(*range), range);
 258}
 259
 260static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
 261			  struct flowi *fl, int flags,
 262			  struct fib_lookup_arg *arg)
 263{
 264	int iifindex, oifindex, ret = 0;
 265
 266	iifindex = READ_ONCE(rule->iifindex);
 267	if (iifindex && (iifindex != fl->flowi_iif))
 268		goto out;
 269
 270	oifindex = READ_ONCE(rule->oifindex);
 271	if (oifindex && (oifindex != fl->flowi_oif))
 272		goto out;
 273
 274	if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
 275		goto out;
 276
 277	if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
 278		goto out;
 279
 280	if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
 281		goto out;
 282
 283	if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
 284	    uid_gt(fl->flowi_uid, rule->uid_range.end))
 285		goto out;
 286
 287	ret = INDIRECT_CALL_MT(ops->match,
 288			       fib6_rule_match,
 289			       fib4_rule_match,
 290			       rule, fl, flags);
 291out:
 292	return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
 293}
 294
 295int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
 296		     int flags, struct fib_lookup_arg *arg)
 297{
 298	struct fib_rule *rule;
 299	int err;
 300
 301	rcu_read_lock();
 302
 303	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
 304jumped:
 305		if (!fib_rule_match(rule, ops, fl, flags, arg))
 306			continue;
 307
 308		if (rule->action == FR_ACT_GOTO) {
 309			struct fib_rule *target;
 310
 311			target = rcu_dereference(rule->ctarget);
 312			if (target == NULL) {
 313				continue;
 314			} else {
 315				rule = target;
 316				goto jumped;
 317			}
 318		} else if (rule->action == FR_ACT_NOP)
 319			continue;
 320		else
 321			err = INDIRECT_CALL_MT(ops->action,
 322					       fib6_rule_action,
 323					       fib4_rule_action,
 324					       rule, fl, flags, arg);
 325
 326		if (!err && ops->suppress && INDIRECT_CALL_MT(ops->suppress,
 327							      fib6_rule_suppress,
 328							      fib4_rule_suppress,
 329							      rule, flags, arg))
 330			continue;
 331
 332		if (err != -EAGAIN) {
 333			if ((arg->flags & FIB_LOOKUP_NOREF) ||
 334			    likely(refcount_inc_not_zero(&rule->refcnt))) {
 335				arg->rule = rule;
 336				goto out;
 337			}
 338			break;
 339		}
 340	}
 341
 342	err = -ESRCH;
 343out:
 344	rcu_read_unlock();
 345
 346	return err;
 347}
 348EXPORT_SYMBOL_GPL(fib_rules_lookup);
 349
 350static int call_fib_rule_notifier(struct notifier_block *nb,
 351				  enum fib_event_type event_type,
 352				  struct fib_rule *rule, int family,
 353				  struct netlink_ext_ack *extack)
 354{
 355	struct fib_rule_notifier_info info = {
 356		.info.family = family,
 357		.info.extack = extack,
 358		.rule = rule,
 359	};
 360
 361	return call_fib_notifier(nb, event_type, &info.info);
 362}
 363
 364static int call_fib_rule_notifiers(struct net *net,
 365				   enum fib_event_type event_type,
 366				   struct fib_rule *rule,
 367				   struct fib_rules_ops *ops,
 368				   struct netlink_ext_ack *extack)
 369{
 370	struct fib_rule_notifier_info info = {
 371		.info.family = ops->family,
 372		.info.extack = extack,
 373		.rule = rule,
 374	};
 375
 376	ASSERT_RTNL();
 377	/* Paired with READ_ONCE() in fib_rules_seq() */
 378	WRITE_ONCE(ops->fib_rules_seq, ops->fib_rules_seq + 1);
 379	return call_fib_notifiers(net, event_type, &info.info);
 380}
 381
 382/* Called with rcu_read_lock() */
 383int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
 384		   struct netlink_ext_ack *extack)
 385{
 386	struct fib_rules_ops *ops;
 387	struct fib_rule *rule;
 388	int err = 0;
 389
 390	ops = lookup_rules_ops(net, family);
 391	if (!ops)
 392		return -EAFNOSUPPORT;
 393	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
 394		err = call_fib_rule_notifier(nb, FIB_EVENT_RULE_ADD,
 395					     rule, family, extack);
 396		if (err)
 397			break;
 398	}
 399	rules_ops_put(ops);
 400
 401	return err;
 402}
 403EXPORT_SYMBOL_GPL(fib_rules_dump);
 404
 405unsigned int fib_rules_seq_read(const struct net *net, int family)
 406{
 407	unsigned int fib_rules_seq;
 408	struct fib_rules_ops *ops;
 409
 
 
 410	ops = lookup_rules_ops(net, family);
 411	if (!ops)
 412		return 0;
 413	/* Paired with WRITE_ONCE() in call_fib_rule_notifiers() */
 414	fib_rules_seq = READ_ONCE(ops->fib_rules_seq);
 415	rules_ops_put(ops);
 416
 417	return fib_rules_seq;
 418}
 419EXPORT_SYMBOL_GPL(fib_rules_seq_read);
 420
 421static struct fib_rule *rule_find(struct fib_rules_ops *ops,
 422				  struct fib_rule_hdr *frh,
 423				  struct nlattr **tb,
 424				  struct fib_rule *rule,
 425				  bool user_priority)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 426{
 427	struct fib_rule *r;
 428
 429	list_for_each_entry(r, &ops->rules_list, list) {
 430		if (rule->action && r->action != rule->action)
 431			continue;
 432
 433		if (rule->table && r->table != rule->table)
 434			continue;
 435
 436		if (user_priority && r->pref != rule->pref)
 437			continue;
 438
 439		if (rule->iifname[0] &&
 440		    memcmp(r->iifname, rule->iifname, IFNAMSIZ))
 441			continue;
 442
 443		if (rule->oifname[0] &&
 444		    memcmp(r->oifname, rule->oifname, IFNAMSIZ))
 445			continue;
 446
 447		if (rule->mark && r->mark != rule->mark)
 448			continue;
 449
 450		if (rule->suppress_ifgroup != -1 &&
 451		    r->suppress_ifgroup != rule->suppress_ifgroup)
 452			continue;
 453
 454		if (rule->suppress_prefixlen != -1 &&
 455		    r->suppress_prefixlen != rule->suppress_prefixlen)
 456			continue;
 457
 458		if (rule->mark_mask && r->mark_mask != rule->mark_mask)
 459			continue;
 460
 461		if (rule->tun_id && r->tun_id != rule->tun_id)
 462			continue;
 463
 464		if (r->fr_net != rule->fr_net)
 465			continue;
 466
 467		if (rule->l3mdev && r->l3mdev != rule->l3mdev)
 468			continue;
 469
 470		if (uid_range_set(&rule->uid_range) &&
 471		    (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
 472		    !uid_eq(r->uid_range.end, rule->uid_range.end)))
 473			continue;
 474
 475		if (rule->ip_proto && r->ip_proto != rule->ip_proto)
 476			continue;
 477
 478		if (rule->proto && r->proto != rule->proto)
 479			continue;
 480
 481		if (fib_rule_port_range_set(&rule->sport_range) &&
 482		    !fib_rule_port_range_compare(&r->sport_range,
 483						 &rule->sport_range))
 484			continue;
 485
 486		if (fib_rule_port_range_set(&rule->dport_range) &&
 487		    !fib_rule_port_range_compare(&r->dport_range,
 488						 &rule->dport_range))
 489			continue;
 490
 491		if (!ops->compare(r, frh, tb))
 492			continue;
 493		return r;
 494	}
 495
 496	return NULL;
 497}
 498
 499#ifdef CONFIG_NET_L3_MASTER_DEV
 500static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
 501			      struct netlink_ext_ack *extack)
 502{
 503	nlrule->l3mdev = nla_get_u8(nla);
 504	if (nlrule->l3mdev != 1) {
 505		NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
 506		return -1;
 507	}
 508
 509	return 0;
 510}
 511#else
 512static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
 513			      struct netlink_ext_ack *extack)
 514{
 515	NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
 516	return -1;
 517}
 518#endif
 519
 520static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 521		       struct netlink_ext_ack *extack,
 522		       struct fib_rules_ops *ops,
 523		       struct nlattr *tb[],
 524		       struct fib_rule **rule,
 525		       bool *user_priority)
 526{
 527	struct net *net = sock_net(skb->sk);
 528	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 529	struct fib_rule *nlrule = NULL;
 530	int err = -EINVAL;
 
 
 
 
 
 531
 532	if (frh->src_len)
 533		if (!tb[FRA_SRC] ||
 534		    frh->src_len > (ops->addr_size * 8) ||
 535		    nla_len(tb[FRA_SRC]) != ops->addr_size) {
 536			NL_SET_ERR_MSG(extack, "Invalid source address");
 537			goto errout;
 538	}
 539
 540	if (frh->dst_len)
 541		if (!tb[FRA_DST] ||
 542		    frh->dst_len > (ops->addr_size * 8) ||
 543		    nla_len(tb[FRA_DST]) != ops->addr_size) {
 544			NL_SET_ERR_MSG(extack, "Invalid dst address");
 545			goto errout;
 546	}
 547
 548	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 549	if (!nlrule) {
 550		err = -ENOMEM;
 551		goto errout;
 552	}
 553	refcount_set(&nlrule->refcnt, 1);
 554	nlrule->fr_net = net;
 555
 556	if (tb[FRA_PRIORITY]) {
 557		nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
 558		*user_priority = true;
 559	} else {
 560		nlrule->pref = fib_default_rule_pref(ops);
 561	}
 562
 563	nlrule->proto = nla_get_u8_default(tb[FRA_PROTOCOL], RTPROT_UNSPEC);
 
 564
 565	if (tb[FRA_IIFNAME]) {
 566		struct net_device *dev;
 567
 568		nlrule->iifindex = -1;
 569		nla_strscpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
 570		dev = __dev_get_by_name(net, nlrule->iifname);
 571		if (dev)
 572			nlrule->iifindex = dev->ifindex;
 573	}
 574
 575	if (tb[FRA_OIFNAME]) {
 576		struct net_device *dev;
 577
 578		nlrule->oifindex = -1;
 579		nla_strscpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
 580		dev = __dev_get_by_name(net, nlrule->oifname);
 581		if (dev)
 582			nlrule->oifindex = dev->ifindex;
 583	}
 584
 585	if (tb[FRA_FWMARK]) {
 586		nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
 587		if (nlrule->mark)
 588			/* compatibility: if the mark value is non-zero all bits
 589			 * are compared unless a mask is explicitly specified.
 590			 */
 591			nlrule->mark_mask = 0xFFFFFFFF;
 592	}
 593
 594	if (tb[FRA_FWMASK])
 595		nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
 596
 597	if (tb[FRA_TUN_ID])
 598		nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
 599
 600	if (tb[FRA_L3MDEV] &&
 601	    fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
 602		goto errout_free;
 
 
 
 
 
 603
 604	nlrule->action = frh->action;
 605	nlrule->flags = frh->flags;
 606	nlrule->table = frh_get_table(frh, tb);
 607	if (tb[FRA_SUPPRESS_PREFIXLEN])
 608		nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
 609	else
 610		nlrule->suppress_prefixlen = -1;
 611
 612	if (tb[FRA_SUPPRESS_IFGROUP])
 613		nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
 614	else
 615		nlrule->suppress_ifgroup = -1;
 616
 617	if (tb[FRA_GOTO]) {
 618		if (nlrule->action != FR_ACT_GOTO) {
 619			NL_SET_ERR_MSG(extack, "Unexpected goto");
 620			goto errout_free;
 621		}
 622
 623		nlrule->target = nla_get_u32(tb[FRA_GOTO]);
 624		/* Backward jumps are prohibited to avoid endless loops */
 625		if (nlrule->target <= nlrule->pref) {
 626			NL_SET_ERR_MSG(extack, "Backward goto not supported");
 627			goto errout_free;
 
 
 
 
 
 
 628		}
 629	} else if (nlrule->action == FR_ACT_GOTO) {
 630		NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
 
 
 631		goto errout_free;
 632	}
 633
 634	if (nlrule->l3mdev && nlrule->table) {
 635		NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
 636		goto errout_free;
 637	}
 638
 639	if (tb[FRA_UID_RANGE]) {
 640		if (current_user_ns() != net->user_ns) {
 641			err = -EPERM;
 642			NL_SET_ERR_MSG(extack, "No permission to set uid");
 643			goto errout_free;
 644		}
 645
 646		nlrule->uid_range = nla_get_kuid_range(tb);
 647
 648		if (!uid_range_set(&nlrule->uid_range) ||
 649		    !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
 650			NL_SET_ERR_MSG(extack, "Invalid uid range");
 651			goto errout_free;
 652		}
 653	} else {
 654		nlrule->uid_range = fib_kuid_range_unset;
 655	}
 656
 657	if (tb[FRA_IP_PROTO])
 658		nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
 659
 660	if (tb[FRA_SPORT_RANGE]) {
 661		err = nla_get_port_range(tb[FRA_SPORT_RANGE],
 662					 &nlrule->sport_range);
 663		if (err) {
 664			NL_SET_ERR_MSG(extack, "Invalid sport range");
 665			goto errout_free;
 666		}
 667	}
 668
 669	if (tb[FRA_DPORT_RANGE]) {
 670		err = nla_get_port_range(tb[FRA_DPORT_RANGE],
 671					 &nlrule->dport_range);
 672		if (err) {
 673			NL_SET_ERR_MSG(extack, "Invalid dport range");
 674			goto errout_free;
 675		}
 676	}
 677
 678	*rule = nlrule;
 679
 680	return 0;
 681
 682errout_free:
 683	kfree(nlrule);
 684errout:
 685	return err;
 686}
 687
 688static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
 689		       struct nlattr **tb, struct fib_rule *rule)
 690{
 691	struct fib_rule *r;
 692
 693	list_for_each_entry(r, &ops->rules_list, list) {
 694		if (r->action != rule->action)
 695			continue;
 696
 697		if (r->table != rule->table)
 698			continue;
 699
 700		if (r->pref != rule->pref)
 701			continue;
 702
 703		if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
 704			continue;
 705
 706		if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
 707			continue;
 708
 709		if (r->mark != rule->mark)
 710			continue;
 711
 712		if (r->suppress_ifgroup != rule->suppress_ifgroup)
 713			continue;
 714
 715		if (r->suppress_prefixlen != rule->suppress_prefixlen)
 716			continue;
 717
 718		if (r->mark_mask != rule->mark_mask)
 719			continue;
 720
 721		if (r->tun_id != rule->tun_id)
 722			continue;
 723
 724		if (r->fr_net != rule->fr_net)
 725			continue;
 726
 727		if (r->l3mdev != rule->l3mdev)
 728			continue;
 729
 730		if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
 731		    !uid_eq(r->uid_range.end, rule->uid_range.end))
 732			continue;
 733
 734		if (r->ip_proto != rule->ip_proto)
 735			continue;
 736
 737		if (r->proto != rule->proto)
 738			continue;
 739
 740		if (!fib_rule_port_range_compare(&r->sport_range,
 741						 &rule->sport_range))
 742			continue;
 743
 744		if (!fib_rule_port_range_compare(&r->dport_range,
 745						 &rule->dport_range))
 746			continue;
 747
 748		if (!ops->compare(r, frh, tb))
 749			continue;
 750		return 1;
 751	}
 752	return 0;
 753}
 754
 755static const struct nla_policy fib_rule_policy[FRA_MAX + 1] = {
 756	[FRA_UNSPEC]	= { .strict_start_type = FRA_DPORT_RANGE + 1 },
 757	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
 758	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
 759	[FRA_PRIORITY]	= { .type = NLA_U32 },
 760	[FRA_FWMARK]	= { .type = NLA_U32 },
 761	[FRA_FLOW]	= { .type = NLA_U32 },
 762	[FRA_TUN_ID]	= { .type = NLA_U64 },
 763	[FRA_FWMASK]	= { .type = NLA_U32 },
 764	[FRA_TABLE]     = { .type = NLA_U32 },
 765	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 },
 766	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 },
 767	[FRA_GOTO]	= { .type = NLA_U32 },
 768	[FRA_L3MDEV]	= { .type = NLA_U8 },
 769	[FRA_UID_RANGE]	= { .len = sizeof(struct fib_rule_uid_range) },
 770	[FRA_PROTOCOL]  = { .type = NLA_U8 },
 771	[FRA_IP_PROTO]  = { .type = NLA_U8 },
 772	[FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) },
 773	[FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) },
 774	[FRA_DSCP]	= NLA_POLICY_MAX(NLA_U8, INET_DSCP_MASK >> 2),
 775};
 776
 777int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 778		   struct netlink_ext_ack *extack)
 779{
 780	struct net *net = sock_net(skb->sk);
 781	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 782	struct fib_rules_ops *ops = NULL;
 783	struct fib_rule *rule = NULL, *r, *last = NULL;
 784	struct nlattr *tb[FRA_MAX + 1];
 785	int err = -EINVAL, unresolved = 0;
 786	bool user_priority = false;
 787
 788	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
 789		NL_SET_ERR_MSG(extack, "Invalid msg length");
 790		goto errout;
 791	}
 792
 793	ops = lookup_rules_ops(net, frh->family);
 794	if (!ops) {
 795		err = -EAFNOSUPPORT;
 796		NL_SET_ERR_MSG(extack, "Rule family not supported");
 797		goto errout;
 798	}
 799
 800	err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
 801				     fib_rule_policy, extack);
 802	if (err < 0) {
 803		NL_SET_ERR_MSG(extack, "Error parsing msg");
 804		goto errout;
 805	}
 806
 807	err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
 808	if (err)
 809		goto errout;
 810
 811	if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
 812	    rule_exists(ops, frh, tb, rule)) {
 813		err = -EEXIST;
 814		goto errout_free;
 815	}
 816
 817	err = ops->configure(rule, skb, frh, tb, extack);
 818	if (err < 0)
 819		goto errout_free;
 820
 821	err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
 822				      extack);
 823	if (err < 0)
 824		goto errout_free;
 825
 826	list_for_each_entry(r, &ops->rules_list, list) {
 827		if (r->pref == rule->target) {
 828			RCU_INIT_POINTER(rule->ctarget, r);
 829			break;
 830		}
 831	}
 832
 833	if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
 834		unresolved = 1;
 835
 836	list_for_each_entry(r, &ops->rules_list, list) {
 837		if (r->pref > rule->pref)
 838			break;
 839		last = r;
 840	}
 841
 842	if (last)
 843		list_add_rcu(&rule->list, &last->list);
 844	else
 845		list_add_rcu(&rule->list, &ops->rules_list);
 846
 847	if (ops->unresolved_rules) {
 848		/*
 849		 * There are unresolved goto rules in the list, check if
 850		 * any of them are pointing to this new rule.
 851		 */
 852		list_for_each_entry(r, &ops->rules_list, list) {
 853			if (r->action == FR_ACT_GOTO &&
 854			    r->target == rule->pref &&
 855			    rtnl_dereference(r->ctarget) == NULL) {
 856				rcu_assign_pointer(r->ctarget, rule);
 857				if (--ops->unresolved_rules == 0)
 858					break;
 859			}
 860		}
 861	}
 862
 863	if (rule->action == FR_ACT_GOTO)
 864		ops->nr_goto_rules++;
 865
 866	if (unresolved)
 867		ops->unresolved_rules++;
 868
 869	if (rule->tun_id)
 870		ip_tunnel_need_metadata();
 871
 872	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
 873	flush_route_cache(ops);
 874	rules_ops_put(ops);
 875	return 0;
 876
 877errout_free:
 878	kfree(rule);
 879errout:
 880	rules_ops_put(ops);
 881	return err;
 882}
 883EXPORT_SYMBOL_GPL(fib_nl_newrule);
 884
 885int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 886		   struct netlink_ext_ack *extack)
 887{
 888	struct net *net = sock_net(skb->sk);
 889	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 
 
 890	struct fib_rules_ops *ops = NULL;
 891	struct fib_rule *rule = NULL, *r, *nlrule = NULL;
 892	struct nlattr *tb[FRA_MAX+1];
 
 893	int err = -EINVAL;
 894	bool user_priority = false;
 895
 896	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
 897		NL_SET_ERR_MSG(extack, "Invalid msg length");
 898		goto errout;
 899	}
 900
 901	ops = lookup_rules_ops(net, frh->family);
 902	if (ops == NULL) {
 903		err = -EAFNOSUPPORT;
 904		NL_SET_ERR_MSG(extack, "Rule family not supported");
 905		goto errout;
 906	}
 907
 908	err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
 909				     fib_rule_policy, extack);
 910	if (err < 0) {
 911		NL_SET_ERR_MSG(extack, "Error parsing msg");
 912		goto errout;
 913	}
 914
 915	err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
 916	if (err)
 917		goto errout;
 918
 919	rule = rule_find(ops, frh, tb, nlrule, user_priority);
 920	if (!rule) {
 921		err = -ENOENT;
 922		goto errout;
 
 
 
 
 923	}
 924
 925	if (rule->flags & FIB_RULE_PERMANENT) {
 926		err = -EPERM;
 927		goto errout;
 
 
 928	}
 929
 930	if (ops->delete) {
 931		err = ops->delete(rule);
 
 932		if (err)
 933			goto errout;
 934	}
 935
 936	if (rule->tun_id)
 937		ip_tunnel_unneed_metadata();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 938
 939	list_del_rcu(&rule->list);
 
 940
 941	if (rule->action == FR_ACT_GOTO) {
 942		ops->nr_goto_rules--;
 943		if (rtnl_dereference(rule->ctarget) == NULL)
 944			ops->unresolved_rules--;
 945	}
 946
 947	/*
 948	 * Check if this rule is a target to any of them. If so,
 949	 * adjust to the next one with the same preference or
 950	 * disable them. As this operation is eventually very
 951	 * expensive, it is only performed if goto rules, except
 952	 * current if it is goto rule, have actually been added.
 953	 */
 954	if (ops->nr_goto_rules > 0) {
 955		struct fib_rule *n;
 956
 957		n = list_next_entry(rule, list);
 958		if (&n->list == &ops->rules_list || n->pref != rule->pref)
 959			n = NULL;
 960		list_for_each_entry(r, &ops->rules_list, list) {
 961			if (rtnl_dereference(r->ctarget) != rule)
 962				continue;
 963			rcu_assign_pointer(r->ctarget, n);
 964			if (!n)
 965				ops->unresolved_rules++;
 
 
 
 966		}
 
 
 
 
 
 
 
 
 
 967	}
 968
 969	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
 970				NULL);
 971	notify_rule_change(RTM_DELRULE, rule, ops, nlh,
 972			   NETLINK_CB(skb).portid);
 973	fib_rule_put(rule);
 974	flush_route_cache(ops);
 975	rules_ops_put(ops);
 976	kfree(nlrule);
 977	return 0;
 978
 979errout:
 980	kfree(nlrule);
 981	rules_ops_put(ops);
 982	return err;
 983}
 984EXPORT_SYMBOL_GPL(fib_nl_delrule);
 985
 986static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
 987					 struct fib_rule *rule)
 988{
 989	size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
 990			 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
 991			 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
 992			 + nla_total_size(4) /* FRA_PRIORITY */
 993			 + nla_total_size(4) /* FRA_TABLE */
 994			 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
 995			 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
 996			 + nla_total_size(4) /* FRA_FWMARK */
 997			 + nla_total_size(4) /* FRA_FWMASK */
 998			 + nla_total_size_64bit(8) /* FRA_TUN_ID */
 999			 + nla_total_size(sizeof(struct fib_kuid_range))
1000			 + nla_total_size(1) /* FRA_PROTOCOL */
1001			 + nla_total_size(1) /* FRA_IP_PROTO */
1002			 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */
1003			 + nla_total_size(sizeof(struct fib_rule_port_range)); /* FRA_DPORT_RANGE */
1004
1005	if (ops->nlmsg_payload)
1006		payload += ops->nlmsg_payload(rule);
1007
1008	return payload;
1009}
1010
1011static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
1012			    u32 pid, u32 seq, int type, int flags,
1013			    struct fib_rules_ops *ops)
1014{
1015	struct nlmsghdr *nlh;
1016	struct fib_rule_hdr *frh;
1017
1018	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
1019	if (nlh == NULL)
1020		return -EMSGSIZE;
1021
1022	frh = nlmsg_data(nlh);
1023	frh->family = ops->family;
1024	frh->table = rule->table < 256 ? rule->table : RT_TABLE_COMPAT;
1025	if (nla_put_u32(skb, FRA_TABLE, rule->table))
1026		goto nla_put_failure;
1027	if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
1028		goto nla_put_failure;
1029	frh->res1 = 0;
1030	frh->res2 = 0;
1031	frh->action = rule->action;
1032	frh->flags = rule->flags;
1033
1034	if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
1035		goto nla_put_failure;
1036
1037	if (rule->action == FR_ACT_GOTO &&
1038	    rcu_access_pointer(rule->ctarget) == NULL)
1039		frh->flags |= FIB_RULE_UNRESOLVED;
1040
1041	if (rule->iifname[0]) {
1042		if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
1043			goto nla_put_failure;
1044		if (READ_ONCE(rule->iifindex) == -1)
1045			frh->flags |= FIB_RULE_IIF_DETACHED;
1046	}
1047
1048	if (rule->oifname[0]) {
1049		if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
1050			goto nla_put_failure;
1051		if (READ_ONCE(rule->oifindex) == -1)
1052			frh->flags |= FIB_RULE_OIF_DETACHED;
1053	}
1054
1055	if ((rule->pref &&
1056	     nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
1057	    (rule->mark &&
1058	     nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
1059	    ((rule->mark_mask || rule->mark) &&
1060	     nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
1061	    (rule->target &&
1062	     nla_put_u32(skb, FRA_GOTO, rule->target)) ||
1063	    (rule->tun_id &&
1064	     nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
1065	    (rule->l3mdev &&
1066	     nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
1067	    (uid_range_set(&rule->uid_range) &&
1068	     nla_put_uid_range(skb, &rule->uid_range)) ||
1069	    (fib_rule_port_range_set(&rule->sport_range) &&
1070	     nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
1071	    (fib_rule_port_range_set(&rule->dport_range) &&
1072	     nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
1073	    (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
1074		goto nla_put_failure;
1075
1076	if (rule->suppress_ifgroup != -1) {
1077		if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
1078			goto nla_put_failure;
1079	}
1080
1081	if (ops->fill(rule, skb, frh) < 0)
1082		goto nla_put_failure;
1083
1084	nlmsg_end(skb, nlh);
1085	return 0;
1086
1087nla_put_failure:
1088	nlmsg_cancel(skb, nlh);
1089	return -EMSGSIZE;
1090}
1091
1092static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
1093		      struct fib_rules_ops *ops)
1094{
1095	int idx = 0;
1096	struct fib_rule *rule;
1097	int err = 0;
1098
1099	rcu_read_lock();
1100	list_for_each_entry_rcu(rule, &ops->rules_list, list) {
1101		if (idx < cb->args[1])
1102			goto skip;
1103
1104		err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
1105				       cb->nlh->nlmsg_seq, RTM_NEWRULE,
1106				       NLM_F_MULTI, ops);
1107		if (err)
1108			break;
1109skip:
1110		idx++;
1111	}
1112	rcu_read_unlock();
1113	cb->args[1] = idx;
1114	rules_ops_put(ops);
1115
1116	return err;
1117}
1118
1119static int fib_valid_dumprule_req(const struct nlmsghdr *nlh,
1120				   struct netlink_ext_ack *extack)
1121{
1122	struct fib_rule_hdr *frh;
1123
1124	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
1125		NL_SET_ERR_MSG(extack, "Invalid header for fib rule dump request");
1126		return -EINVAL;
1127	}
1128
1129	frh = nlmsg_data(nlh);
1130	if (frh->dst_len || frh->src_len || frh->tos || frh->table ||
1131	    frh->res1 || frh->res2 || frh->action || frh->flags) {
1132		NL_SET_ERR_MSG(extack,
1133			       "Invalid values in header for fib rule dump request");
1134		return -EINVAL;
1135	}
1136
1137	if (nlmsg_attrlen(nlh, sizeof(*frh))) {
1138		NL_SET_ERR_MSG(extack, "Invalid data after header in fib rule dump request");
1139		return -EINVAL;
1140	}
1141
1142	return 0;
1143}
1144
1145static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
1146{
1147	const struct nlmsghdr *nlh = cb->nlh;
1148	struct net *net = sock_net(skb->sk);
1149	struct fib_rules_ops *ops;
1150	int err, idx = 0, family;
1151
1152	if (cb->strict_check) {
1153		err = fib_valid_dumprule_req(nlh, cb->extack);
1154
1155		if (err < 0)
1156			return err;
1157	}
1158
1159	family = rtnl_msg_family(nlh);
1160	if (family != AF_UNSPEC) {
1161		/* Protocol specific dump request */
1162		ops = lookup_rules_ops(net, family);
1163		if (ops == NULL)
1164			return -EAFNOSUPPORT;
1165
1166		return dump_rules(skb, cb, ops);
 
 
1167	}
1168
1169	err = 0;
1170	rcu_read_lock();
1171	list_for_each_entry_rcu(ops, &net->rules_ops, list) {
1172		if (idx < cb->args[0] || !try_module_get(ops->owner))
1173			goto skip;
1174
1175		err = dump_rules(skb, cb, ops);
1176		if (err < 0)
1177			break;
1178
1179		cb->args[1] = 0;
1180skip:
1181		idx++;
1182	}
1183	rcu_read_unlock();
1184	cb->args[0] = idx;
1185
1186	return err;
1187}
1188
1189static void notify_rule_change(int event, struct fib_rule *rule,
1190			       struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1191			       u32 pid)
1192{
1193	struct net *net;
1194	struct sk_buff *skb;
1195	int err = -ENOMEM;
1196
1197	net = ops->fro_net;
1198	skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
1199	if (skb == NULL)
1200		goto errout;
1201
1202	err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
1203	if (err < 0) {
1204		/* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1205		WARN_ON(err == -EMSGSIZE);
1206		kfree_skb(skb);
1207		goto errout;
1208	}
1209
1210	rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1211	return;
1212errout:
1213	rtnl_set_sk_err(net, ops->nlgroup, err);
 
1214}
1215
1216static void attach_rules(struct list_head *rules, struct net_device *dev)
1217{
1218	struct fib_rule *rule;
1219
1220	list_for_each_entry(rule, rules, list) {
1221		if (rule->iifindex == -1 &&
1222		    strcmp(dev->name, rule->iifname) == 0)
1223			WRITE_ONCE(rule->iifindex, dev->ifindex);
1224		if (rule->oifindex == -1 &&
1225		    strcmp(dev->name, rule->oifname) == 0)
1226			WRITE_ONCE(rule->oifindex, dev->ifindex);
1227	}
1228}
1229
1230static void detach_rules(struct list_head *rules, struct net_device *dev)
1231{
1232	struct fib_rule *rule;
1233
1234	list_for_each_entry(rule, rules, list) {
1235		if (rule->iifindex == dev->ifindex)
1236			WRITE_ONCE(rule->iifindex, -1);
1237		if (rule->oifindex == dev->ifindex)
1238			WRITE_ONCE(rule->oifindex, -1);
1239	}
1240}
1241
1242
1243static int fib_rules_event(struct notifier_block *this, unsigned long event,
1244			   void *ptr)
1245{
1246	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1247	struct net *net = dev_net(dev);
1248	struct fib_rules_ops *ops;
1249
1250	ASSERT_RTNL();
1251
1252	switch (event) {
1253	case NETDEV_REGISTER:
1254		list_for_each_entry(ops, &net->rules_ops, list)
1255			attach_rules(&ops->rules_list, dev);
1256		break;
1257
1258	case NETDEV_CHANGENAME:
1259		list_for_each_entry(ops, &net->rules_ops, list) {
1260			detach_rules(&ops->rules_list, dev);
1261			attach_rules(&ops->rules_list, dev);
1262		}
1263		break;
1264
1265	case NETDEV_UNREGISTER:
1266		list_for_each_entry(ops, &net->rules_ops, list)
1267			detach_rules(&ops->rules_list, dev);
1268		break;
1269	}
1270
1271	return NOTIFY_DONE;
1272}
1273
1274static struct notifier_block fib_rules_notifier = {
1275	.notifier_call = fib_rules_event,
1276};
1277
1278static int __net_init fib_rules_net_init(struct net *net)
1279{
1280	INIT_LIST_HEAD(&net->rules_ops);
1281	spin_lock_init(&net->rules_mod_lock);
1282	return 0;
1283}
1284
1285static void __net_exit fib_rules_net_exit(struct net *net)
1286{
1287	WARN_ON_ONCE(!list_empty(&net->rules_ops));
1288}
1289
1290static struct pernet_operations fib_rules_net_ops = {
1291	.init = fib_rules_net_init,
1292	.exit = fib_rules_net_exit,
1293};
1294
1295static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
1296	{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule},
1297	{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
1298	{.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
1299	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
1300};
1301
1302static int __init fib_rules_init(void)
1303{
1304	int err;
1305
1306	rtnl_register_many(fib_rules_rtnl_msg_handlers);
 
1307
1308	err = register_pernet_subsys(&fib_rules_net_ops);
1309	if (err < 0)
1310		goto fail;
1311
1312	err = register_netdevice_notifier(&fib_rules_notifier);
1313	if (err < 0)
1314		goto fail_unregister;
1315
1316	return 0;
1317
1318fail_unregister:
1319	unregister_pernet_subsys(&fib_rules_net_ops);
1320fail:
1321	rtnl_unregister_many(fib_rules_rtnl_msg_handlers);
 
 
1322	return err;
1323}
1324
1325subsys_initcall(fib_rules_init);