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 33 static const struct fib_kuid_range fib_kuid_range_unset = { 34 KUIDT_INIT(0), 35 KUIDT_INIT(~0), 36 }; 37 38 bool 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 } 54 EXPORT_SYMBOL_GPL(fib_rule_matchall); 55 56 int 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 } 81 EXPORT_SYMBOL(fib_default_rule_add); 82 83 static 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 100 static void notify_rule_change(int event, struct fib_rule *rule, 101 struct fib_rules_ops *ops, struct nlmsghdr *nlh, 102 u32 pid); 103 104 static 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 123 static void rules_ops_put(struct fib_rules_ops *ops) 124 { 125 if (ops) 126 module_put(ops->owner); 127 } 128 129 static void flush_route_cache(struct fib_rules_ops *ops) 130 { 131 if (ops->flush_cache) 132 ops->flush_cache(ops); 133 } 134 135 static 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; 158 errout: 159 spin_unlock(&net->rules_mod_lock); 160 161 return err; 162 } 163 164 struct fib_rules_ops * 165 fib_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 } 185 EXPORT_SYMBOL_GPL(fib_rules_register); 186 187 static 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 199 void 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 } 210 EXPORT_SYMBOL_GPL(fib_rules_unregister); 211 212 static int uid_range_set(struct fib_kuid_range *range) 213 { 214 return uid_valid(range->start) && uid_valid(range->end); 215 } 216 217 static 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 230 static 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 240 static 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 254 static 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 260 static bool fib_rule_iif_match(const struct fib_rule *rule, int iifindex, 261 const struct flowi *fl) 262 { 263 u8 iif_is_l3_master = READ_ONCE(rule->iif_is_l3_master); 264 265 return iif_is_l3_master ? l3mdev_fib_rule_iif_match(fl, iifindex) : 266 fl->flowi_iif == iifindex; 267 } 268 269 static bool fib_rule_oif_match(const struct fib_rule *rule, int oifindex, 270 const struct flowi *fl) 271 { 272 u8 oif_is_l3_master = READ_ONCE(rule->oif_is_l3_master); 273 274 return oif_is_l3_master ? l3mdev_fib_rule_oif_match(fl, oifindex) : 275 fl->flowi_oif == oifindex; 276 } 277 278 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops, 279 struct flowi *fl, int flags, 280 struct fib_lookup_arg *arg) 281 { 282 int iifindex, oifindex, ret = 0; 283 284 iifindex = READ_ONCE(rule->iifindex); 285 if (iifindex && !fib_rule_iif_match(rule, iifindex, fl)) 286 goto out; 287 288 oifindex = READ_ONCE(rule->oifindex); 289 if (oifindex && !fib_rule_oif_match(rule, oifindex, fl)) 290 goto out; 291 292 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask) 293 goto out; 294 295 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id)) 296 goto out; 297 298 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg)) 299 goto out; 300 301 if (uid_lt(fl->flowi_uid, rule->uid_range.start) || 302 uid_gt(fl->flowi_uid, rule->uid_range.end)) 303 goto out; 304 305 ret = INDIRECT_CALL_MT(ops->match, 306 fib6_rule_match, 307 fib4_rule_match, 308 rule, fl, flags); 309 out: 310 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret; 311 } 312 313 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl, 314 int flags, struct fib_lookup_arg *arg) 315 { 316 struct fib_rule *rule; 317 int err; 318 319 rcu_read_lock(); 320 321 list_for_each_entry_rcu(rule, &ops->rules_list, list) { 322 jumped: 323 if (!fib_rule_match(rule, ops, fl, flags, arg)) 324 continue; 325 326 if (rule->action == FR_ACT_GOTO) { 327 struct fib_rule *target; 328 329 target = rcu_dereference(rule->ctarget); 330 if (target == NULL) { 331 continue; 332 } else { 333 rule = target; 334 goto jumped; 335 } 336 } else if (rule->action == FR_ACT_NOP) 337 continue; 338 else 339 err = INDIRECT_CALL_MT(ops->action, 340 fib6_rule_action, 341 fib4_rule_action, 342 rule, fl, flags, arg); 343 344 if (!err && ops->suppress && INDIRECT_CALL_MT(ops->suppress, 345 fib6_rule_suppress, 346 fib4_rule_suppress, 347 rule, flags, arg)) 348 continue; 349 350 if (err != -EAGAIN) { 351 if ((arg->flags & FIB_LOOKUP_NOREF) || 352 likely(refcount_inc_not_zero(&rule->refcnt))) { 353 arg->rule = rule; 354 goto out; 355 } 356 break; 357 } 358 } 359 360 err = -ESRCH; 361 out: 362 rcu_read_unlock(); 363 364 return err; 365 } 366 EXPORT_SYMBOL_GPL(fib_rules_lookup); 367 368 static int call_fib_rule_notifier(struct notifier_block *nb, 369 enum fib_event_type event_type, 370 struct fib_rule *rule, int family, 371 struct netlink_ext_ack *extack) 372 { 373 struct fib_rule_notifier_info info = { 374 .info.family = family, 375 .info.extack = extack, 376 .rule = rule, 377 }; 378 379 return call_fib_notifier(nb, event_type, &info.info); 380 } 381 382 static int call_fib_rule_notifiers(struct net *net, 383 enum fib_event_type event_type, 384 struct fib_rule *rule, 385 struct fib_rules_ops *ops, 386 struct netlink_ext_ack *extack) 387 { 388 struct fib_rule_notifier_info info = { 389 .info.family = ops->family, 390 .info.extack = extack, 391 .rule = rule, 392 }; 393 394 ASSERT_RTNL_NET(net); 395 396 /* Paired with READ_ONCE() in fib_rules_seq() */ 397 WRITE_ONCE(ops->fib_rules_seq, ops->fib_rules_seq + 1); 398 return call_fib_notifiers(net, event_type, &info.info); 399 } 400 401 /* Called with rcu_read_lock() */ 402 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family, 403 struct netlink_ext_ack *extack) 404 { 405 struct fib_rules_ops *ops; 406 struct fib_rule *rule; 407 int err = 0; 408 409 ops = lookup_rules_ops(net, family); 410 if (!ops) 411 return -EAFNOSUPPORT; 412 list_for_each_entry_rcu(rule, &ops->rules_list, list) { 413 err = call_fib_rule_notifier(nb, FIB_EVENT_RULE_ADD, 414 rule, family, extack); 415 if (err) 416 break; 417 } 418 rules_ops_put(ops); 419 420 return err; 421 } 422 EXPORT_SYMBOL_GPL(fib_rules_dump); 423 424 unsigned int fib_rules_seq_read(const struct net *net, int family) 425 { 426 unsigned int fib_rules_seq; 427 struct fib_rules_ops *ops; 428 429 ops = lookup_rules_ops(net, family); 430 if (!ops) 431 return 0; 432 /* Paired with WRITE_ONCE() in call_fib_rule_notifiers() */ 433 fib_rules_seq = READ_ONCE(ops->fib_rules_seq); 434 rules_ops_put(ops); 435 436 return fib_rules_seq; 437 } 438 EXPORT_SYMBOL_GPL(fib_rules_seq_read); 439 440 static struct fib_rule *rule_find(struct fib_rules_ops *ops, 441 struct fib_rule_hdr *frh, 442 struct nlattr **tb, 443 struct fib_rule *rule, 444 bool user_priority) 445 { 446 struct fib_rule *r; 447 448 list_for_each_entry(r, &ops->rules_list, list) { 449 if (rule->action && r->action != rule->action) 450 continue; 451 452 if (rule->table && r->table != rule->table) 453 continue; 454 455 if (user_priority && r->pref != rule->pref) 456 continue; 457 458 if (rule->iifname[0] && 459 memcmp(r->iifname, rule->iifname, IFNAMSIZ)) 460 continue; 461 462 if (rule->oifname[0] && 463 memcmp(r->oifname, rule->oifname, IFNAMSIZ)) 464 continue; 465 466 if (rule->mark && r->mark != rule->mark) 467 continue; 468 469 if (rule->suppress_ifgroup != -1 && 470 r->suppress_ifgroup != rule->suppress_ifgroup) 471 continue; 472 473 if (rule->suppress_prefixlen != -1 && 474 r->suppress_prefixlen != rule->suppress_prefixlen) 475 continue; 476 477 if (rule->mark_mask && r->mark_mask != rule->mark_mask) 478 continue; 479 480 if (rule->tun_id && r->tun_id != rule->tun_id) 481 continue; 482 483 if (rule->l3mdev && r->l3mdev != rule->l3mdev) 484 continue; 485 486 if (uid_range_set(&rule->uid_range) && 487 (!uid_eq(r->uid_range.start, rule->uid_range.start) || 488 !uid_eq(r->uid_range.end, rule->uid_range.end))) 489 continue; 490 491 if (rule->ip_proto && r->ip_proto != rule->ip_proto) 492 continue; 493 494 if (rule->proto && r->proto != rule->proto) 495 continue; 496 497 if (fib_rule_port_range_set(&rule->sport_range) && 498 !fib_rule_port_range_compare(&r->sport_range, 499 &rule->sport_range)) 500 continue; 501 502 if (rule->sport_mask && r->sport_mask != rule->sport_mask) 503 continue; 504 505 if (fib_rule_port_range_set(&rule->dport_range) && 506 !fib_rule_port_range_compare(&r->dport_range, 507 &rule->dport_range)) 508 continue; 509 510 if (rule->dport_mask && r->dport_mask != rule->dport_mask) 511 continue; 512 513 if (!ops->compare(r, frh, tb)) 514 continue; 515 return r; 516 } 517 518 return NULL; 519 } 520 521 #ifdef CONFIG_NET_L3_MASTER_DEV 522 static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule, 523 struct netlink_ext_ack *extack) 524 { 525 nlrule->l3mdev = nla_get_u8(nla); 526 if (nlrule->l3mdev != 1) { 527 NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute"); 528 return -1; 529 } 530 531 return 0; 532 } 533 #else 534 static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule, 535 struct netlink_ext_ack *extack) 536 { 537 NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel"); 538 return -1; 539 } 540 #endif 541 542 static int fib_nl2rule_port_mask(const struct nlattr *mask_attr, 543 const struct fib_rule_port_range *range, 544 u16 *port_mask, 545 struct netlink_ext_ack *extack) 546 { 547 if (!fib_rule_port_range_valid(range)) { 548 NL_SET_ERR_MSG_ATTR(extack, mask_attr, 549 "Cannot specify port mask without port value"); 550 return -EINVAL; 551 } 552 553 if (fib_rule_port_is_range(range)) { 554 NL_SET_ERR_MSG_ATTR(extack, mask_attr, 555 "Cannot specify port mask for port range"); 556 return -EINVAL; 557 } 558 559 if (range->start & ~nla_get_u16(mask_attr)) { 560 NL_SET_ERR_MSG_ATTR(extack, mask_attr, "Invalid port mask"); 561 return -EINVAL; 562 } 563 564 *port_mask = nla_get_u16(mask_attr); 565 566 return 0; 567 } 568 569 static int fib_nl2rule(struct net *net, struct nlmsghdr *nlh, 570 struct netlink_ext_ack *extack, 571 struct fib_rules_ops *ops, 572 struct nlattr *tb[], 573 struct fib_rule **rule, 574 bool *user_priority) 575 { 576 struct fib_rule_hdr *frh = nlmsg_data(nlh); 577 struct fib_rule *nlrule = NULL; 578 int err = -EINVAL; 579 580 if (frh->src_len) 581 if (!tb[FRA_SRC] || 582 frh->src_len > (ops->addr_size * 8) || 583 nla_len(tb[FRA_SRC]) != ops->addr_size) { 584 NL_SET_ERR_MSG(extack, "Invalid source address"); 585 goto errout; 586 } 587 588 if (frh->dst_len) 589 if (!tb[FRA_DST] || 590 frh->dst_len > (ops->addr_size * 8) || 591 nla_len(tb[FRA_DST]) != ops->addr_size) { 592 NL_SET_ERR_MSG(extack, "Invalid dst address"); 593 goto errout; 594 } 595 596 nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT); 597 if (!nlrule) { 598 err = -ENOMEM; 599 goto errout; 600 } 601 refcount_set(&nlrule->refcnt, 1); 602 nlrule->fr_net = net; 603 604 if (tb[FRA_PRIORITY]) { 605 nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]); 606 *user_priority = true; 607 } 608 609 nlrule->proto = nla_get_u8_default(tb[FRA_PROTOCOL], RTPROT_UNSPEC); 610 611 if (tb[FRA_IIFNAME]) { 612 nlrule->iifindex = -1; 613 nla_strscpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ); 614 } 615 616 if (tb[FRA_OIFNAME]) { 617 nlrule->oifindex = -1; 618 nla_strscpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ); 619 } 620 621 if (tb[FRA_FWMARK]) { 622 nlrule->mark = nla_get_u32(tb[FRA_FWMARK]); 623 if (nlrule->mark) 624 /* compatibility: if the mark value is non-zero all bits 625 * are compared unless a mask is explicitly specified. 626 */ 627 nlrule->mark_mask = 0xFFFFFFFF; 628 } 629 630 if (tb[FRA_FWMASK]) 631 nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]); 632 633 if (tb[FRA_TUN_ID]) 634 nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]); 635 636 if (tb[FRA_L3MDEV] && 637 fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0) 638 goto errout_free; 639 640 nlrule->action = frh->action; 641 nlrule->flags = frh->flags; 642 nlrule->table = frh_get_table(frh, tb); 643 if (tb[FRA_SUPPRESS_PREFIXLEN]) 644 nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]); 645 else 646 nlrule->suppress_prefixlen = -1; 647 648 if (tb[FRA_SUPPRESS_IFGROUP]) 649 nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]); 650 else 651 nlrule->suppress_ifgroup = -1; 652 653 if (tb[FRA_GOTO]) { 654 if (nlrule->action != FR_ACT_GOTO) { 655 NL_SET_ERR_MSG(extack, "Unexpected goto"); 656 goto errout_free; 657 } 658 659 nlrule->target = nla_get_u32(tb[FRA_GOTO]); 660 } else if (nlrule->action == FR_ACT_GOTO) { 661 NL_SET_ERR_MSG(extack, "Missing goto target for action goto"); 662 goto errout_free; 663 } 664 665 if (nlrule->l3mdev && nlrule->table) { 666 NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive"); 667 goto errout_free; 668 } 669 670 if (tb[FRA_UID_RANGE]) { 671 if (current_user_ns() != net->user_ns) { 672 err = -EPERM; 673 NL_SET_ERR_MSG(extack, "No permission to set uid"); 674 goto errout_free; 675 } 676 677 nlrule->uid_range = nla_get_kuid_range(tb); 678 679 if (!uid_range_set(&nlrule->uid_range) || 680 !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) { 681 NL_SET_ERR_MSG(extack, "Invalid uid range"); 682 goto errout_free; 683 } 684 } else { 685 nlrule->uid_range = fib_kuid_range_unset; 686 } 687 688 if (tb[FRA_IP_PROTO]) 689 nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]); 690 691 if (tb[FRA_SPORT_RANGE]) { 692 err = nla_get_port_range(tb[FRA_SPORT_RANGE], 693 &nlrule->sport_range); 694 if (err) { 695 NL_SET_ERR_MSG(extack, "Invalid sport range"); 696 goto errout_free; 697 } 698 if (!fib_rule_port_is_range(&nlrule->sport_range)) 699 nlrule->sport_mask = U16_MAX; 700 } 701 702 if (tb[FRA_SPORT_MASK]) { 703 err = fib_nl2rule_port_mask(tb[FRA_SPORT_MASK], 704 &nlrule->sport_range, 705 &nlrule->sport_mask, extack); 706 if (err) 707 goto errout_free; 708 } 709 710 if (tb[FRA_DPORT_RANGE]) { 711 err = nla_get_port_range(tb[FRA_DPORT_RANGE], 712 &nlrule->dport_range); 713 if (err) { 714 NL_SET_ERR_MSG(extack, "Invalid dport range"); 715 goto errout_free; 716 } 717 if (!fib_rule_port_is_range(&nlrule->dport_range)) 718 nlrule->dport_mask = U16_MAX; 719 } 720 721 if (tb[FRA_DPORT_MASK]) { 722 err = fib_nl2rule_port_mask(tb[FRA_DPORT_MASK], 723 &nlrule->dport_range, 724 &nlrule->dport_mask, extack); 725 if (err) 726 goto errout_free; 727 } 728 729 *rule = nlrule; 730 731 return 0; 732 733 errout_free: 734 kfree(nlrule); 735 errout: 736 return err; 737 } 738 739 static int fib_nl2rule_rtnl(struct fib_rule *nlrule, 740 struct fib_rules_ops *ops, 741 struct nlattr *tb[], 742 struct netlink_ext_ack *extack) 743 { 744 if (!tb[FRA_PRIORITY]) 745 nlrule->pref = fib_default_rule_pref(ops); 746 747 /* Backward jumps are prohibited to avoid endless loops */ 748 if (tb[FRA_GOTO] && nlrule->target <= nlrule->pref) { 749 NL_SET_ERR_MSG(extack, "Backward goto not supported"); 750 return -EINVAL; 751 } 752 753 if (tb[FRA_IIFNAME]) { 754 struct net_device *dev; 755 756 dev = __dev_get_by_name(nlrule->fr_net, nlrule->iifname); 757 if (dev) { 758 nlrule->iifindex = dev->ifindex; 759 nlrule->iif_is_l3_master = netif_is_l3_master(dev); 760 } 761 } 762 763 if (tb[FRA_OIFNAME]) { 764 struct net_device *dev; 765 766 dev = __dev_get_by_name(nlrule->fr_net, nlrule->oifname); 767 if (dev) { 768 nlrule->oifindex = dev->ifindex; 769 nlrule->oif_is_l3_master = netif_is_l3_master(dev); 770 } 771 } 772 773 return 0; 774 } 775 776 static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh, 777 struct nlattr **tb, struct fib_rule *rule) 778 { 779 struct fib_rule *r; 780 781 list_for_each_entry(r, &ops->rules_list, list) { 782 if (r->action != rule->action) 783 continue; 784 785 if (r->table != rule->table) 786 continue; 787 788 if (r->pref != rule->pref) 789 continue; 790 791 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ)) 792 continue; 793 794 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ)) 795 continue; 796 797 if (r->mark != rule->mark) 798 continue; 799 800 if (r->suppress_ifgroup != rule->suppress_ifgroup) 801 continue; 802 803 if (r->suppress_prefixlen != rule->suppress_prefixlen) 804 continue; 805 806 if (r->mark_mask != rule->mark_mask) 807 continue; 808 809 if (r->tun_id != rule->tun_id) 810 continue; 811 812 if (r->l3mdev != rule->l3mdev) 813 continue; 814 815 if (!uid_eq(r->uid_range.start, rule->uid_range.start) || 816 !uid_eq(r->uid_range.end, rule->uid_range.end)) 817 continue; 818 819 if (r->ip_proto != rule->ip_proto) 820 continue; 821 822 if (r->proto != rule->proto) 823 continue; 824 825 if (!fib_rule_port_range_compare(&r->sport_range, 826 &rule->sport_range)) 827 continue; 828 829 if (r->sport_mask != rule->sport_mask) 830 continue; 831 832 if (!fib_rule_port_range_compare(&r->dport_range, 833 &rule->dport_range)) 834 continue; 835 836 if (r->dport_mask != rule->dport_mask) 837 continue; 838 839 if (!ops->compare(r, frh, tb)) 840 continue; 841 return 1; 842 } 843 return 0; 844 } 845 846 static const struct nla_policy fib_rule_policy[FRA_MAX + 1] = { 847 [FRA_UNSPEC] = { .strict_start_type = FRA_DPORT_RANGE + 1 }, 848 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, 849 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, 850 [FRA_PRIORITY] = { .type = NLA_U32 }, 851 [FRA_FWMARK] = { .type = NLA_U32 }, 852 [FRA_FLOW] = { .type = NLA_U32 }, 853 [FRA_TUN_ID] = { .type = NLA_U64 }, 854 [FRA_FWMASK] = { .type = NLA_U32 }, 855 [FRA_TABLE] = { .type = NLA_U32 }, 856 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, 857 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, 858 [FRA_GOTO] = { .type = NLA_U32 }, 859 [FRA_L3MDEV] = { .type = NLA_U8 }, 860 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }, 861 [FRA_PROTOCOL] = { .type = NLA_U8 }, 862 [FRA_IP_PROTO] = { .type = NLA_U8 }, 863 [FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, 864 [FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, 865 [FRA_DSCP] = NLA_POLICY_MAX(NLA_U8, INET_DSCP_MASK >> 2), 866 [FRA_FLOWLABEL] = { .type = NLA_BE32 }, 867 [FRA_FLOWLABEL_MASK] = { .type = NLA_BE32 }, 868 [FRA_SPORT_MASK] = { .type = NLA_U16 }, 869 [FRA_DPORT_MASK] = { .type = NLA_U16 }, 870 [FRA_DSCP_MASK] = NLA_POLICY_MASK(NLA_U8, INET_DSCP_MASK >> 2), 871 }; 872 873 int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh, 874 struct netlink_ext_ack *extack, bool rtnl_held) 875 { 876 struct fib_rule *rule = NULL, *r, *last = NULL; 877 struct fib_rule_hdr *frh = nlmsg_data(nlh); 878 int err = -EINVAL, unresolved = 0; 879 struct fib_rules_ops *ops = NULL; 880 struct nlattr *tb[FRA_MAX + 1]; 881 bool user_priority = false; 882 883 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) { 884 NL_SET_ERR_MSG(extack, "Invalid msg length"); 885 goto errout; 886 } 887 888 ops = lookup_rules_ops(net, frh->family); 889 if (!ops) { 890 err = -EAFNOSUPPORT; 891 NL_SET_ERR_MSG(extack, "Rule family not supported"); 892 goto errout; 893 } 894 895 err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX, 896 fib_rule_policy, extack); 897 if (err < 0) { 898 NL_SET_ERR_MSG(extack, "Error parsing msg"); 899 goto errout; 900 } 901 902 err = fib_nl2rule(net, nlh, extack, ops, tb, &rule, &user_priority); 903 if (err) 904 goto errout; 905 906 if (!rtnl_held) 907 rtnl_net_lock(net); 908 909 err = fib_nl2rule_rtnl(rule, ops, tb, extack); 910 if (err) 911 goto errout_free; 912 913 if ((nlh->nlmsg_flags & NLM_F_EXCL) && 914 rule_exists(ops, frh, tb, rule)) { 915 err = -EEXIST; 916 goto errout_free; 917 } 918 919 err = ops->configure(rule, skb, frh, tb, extack); 920 if (err < 0) 921 goto errout_free; 922 923 err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops, 924 extack); 925 if (err < 0) 926 goto errout_free; 927 928 list_for_each_entry(r, &ops->rules_list, list) { 929 if (r->pref == rule->target) { 930 RCU_INIT_POINTER(rule->ctarget, r); 931 break; 932 } 933 } 934 935 if (rcu_dereference_protected(rule->ctarget, 1) == NULL) 936 unresolved = 1; 937 938 list_for_each_entry(r, &ops->rules_list, list) { 939 if (r->pref > rule->pref) 940 break; 941 last = r; 942 } 943 944 if (last) 945 list_add_rcu(&rule->list, &last->list); 946 else 947 list_add_rcu(&rule->list, &ops->rules_list); 948 949 if (ops->unresolved_rules) { 950 /* 951 * There are unresolved goto rules in the list, check if 952 * any of them are pointing to this new rule. 953 */ 954 list_for_each_entry(r, &ops->rules_list, list) { 955 if (r->action == FR_ACT_GOTO && 956 r->target == rule->pref && 957 rtnl_dereference(r->ctarget) == NULL) { 958 rcu_assign_pointer(r->ctarget, rule); 959 if (--ops->unresolved_rules == 0) 960 break; 961 } 962 } 963 } 964 965 if (rule->action == FR_ACT_GOTO) 966 ops->nr_goto_rules++; 967 968 if (unresolved) 969 ops->unresolved_rules++; 970 971 if (rule->tun_id) 972 ip_tunnel_need_metadata(); 973 974 fib_rule_get(rule); 975 976 if (!rtnl_held) 977 rtnl_net_unlock(net); 978 979 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid); 980 fib_rule_put(rule); 981 flush_route_cache(ops); 982 rules_ops_put(ops); 983 return 0; 984 985 errout_free: 986 if (!rtnl_held) 987 rtnl_net_unlock(net); 988 kfree(rule); 989 errout: 990 rules_ops_put(ops); 991 return err; 992 } 993 EXPORT_SYMBOL_GPL(fib_newrule); 994 995 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, 996 struct netlink_ext_ack *extack) 997 { 998 return fib_newrule(sock_net(skb->sk), skb, nlh, extack, false); 999 } 1000 1001 int fib_delrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh, 1002 struct netlink_ext_ack *extack, bool rtnl_held) 1003 { 1004 struct fib_rule *rule = NULL, *nlrule = NULL; 1005 struct fib_rule_hdr *frh = nlmsg_data(nlh); 1006 struct fib_rules_ops *ops = NULL; 1007 struct nlattr *tb[FRA_MAX+1]; 1008 bool user_priority = false; 1009 int err = -EINVAL; 1010 1011 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) { 1012 NL_SET_ERR_MSG(extack, "Invalid msg length"); 1013 goto errout; 1014 } 1015 1016 ops = lookup_rules_ops(net, frh->family); 1017 if (ops == NULL) { 1018 err = -EAFNOSUPPORT; 1019 NL_SET_ERR_MSG(extack, "Rule family not supported"); 1020 goto errout; 1021 } 1022 1023 err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX, 1024 fib_rule_policy, extack); 1025 if (err < 0) { 1026 NL_SET_ERR_MSG(extack, "Error parsing msg"); 1027 goto errout; 1028 } 1029 1030 err = fib_nl2rule(net, nlh, extack, ops, tb, &nlrule, &user_priority); 1031 if (err) 1032 goto errout; 1033 1034 if (!rtnl_held) 1035 rtnl_net_lock(net); 1036 1037 err = fib_nl2rule_rtnl(nlrule, ops, tb, extack); 1038 if (err) 1039 goto errout_free; 1040 1041 rule = rule_find(ops, frh, tb, nlrule, user_priority); 1042 if (!rule) { 1043 err = -ENOENT; 1044 goto errout_free; 1045 } 1046 1047 if (rule->flags & FIB_RULE_PERMANENT) { 1048 err = -EPERM; 1049 goto errout_free; 1050 } 1051 1052 if (ops->delete) { 1053 err = ops->delete(rule); 1054 if (err) 1055 goto errout_free; 1056 } 1057 1058 if (rule->tun_id) 1059 ip_tunnel_unneed_metadata(); 1060 1061 list_del_rcu(&rule->list); 1062 1063 if (rule->action == FR_ACT_GOTO) { 1064 ops->nr_goto_rules--; 1065 if (rtnl_dereference(rule->ctarget) == NULL) 1066 ops->unresolved_rules--; 1067 } 1068 1069 /* 1070 * Check if this rule is a target to any of them. If so, 1071 * adjust to the next one with the same preference or 1072 * disable them. As this operation is eventually very 1073 * expensive, it is only performed if goto rules, except 1074 * current if it is goto rule, have actually been added. 1075 */ 1076 if (ops->nr_goto_rules > 0) { 1077 struct fib_rule *n, *r; 1078 1079 n = list_next_entry(rule, list); 1080 if (&n->list == &ops->rules_list || n->pref != rule->pref) 1081 n = NULL; 1082 list_for_each_entry(r, &ops->rules_list, list) { 1083 if (rtnl_dereference(r->ctarget) != rule) 1084 continue; 1085 rcu_assign_pointer(r->ctarget, n); 1086 if (!n) 1087 ops->unresolved_rules++; 1088 } 1089 } 1090 1091 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops, NULL); 1092 1093 if (!rtnl_held) 1094 rtnl_net_unlock(net); 1095 1096 notify_rule_change(RTM_DELRULE, rule, ops, nlh, NETLINK_CB(skb).portid); 1097 fib_rule_put(rule); 1098 flush_route_cache(ops); 1099 rules_ops_put(ops); 1100 kfree(nlrule); 1101 return 0; 1102 1103 errout_free: 1104 if (!rtnl_held) 1105 rtnl_net_unlock(net); 1106 kfree(nlrule); 1107 errout: 1108 rules_ops_put(ops); 1109 return err; 1110 } 1111 EXPORT_SYMBOL_GPL(fib_delrule); 1112 1113 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, 1114 struct netlink_ext_ack *extack) 1115 { 1116 return fib_delrule(sock_net(skb->sk), skb, nlh, extack, false); 1117 } 1118 1119 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops, 1120 struct fib_rule *rule) 1121 { 1122 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)) 1123 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */ 1124 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */ 1125 + nla_total_size(4) /* FRA_PRIORITY */ 1126 + nla_total_size(4) /* FRA_TABLE */ 1127 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */ 1128 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */ 1129 + nla_total_size(4) /* FRA_FWMARK */ 1130 + nla_total_size(4) /* FRA_FWMASK */ 1131 + nla_total_size_64bit(8) /* FRA_TUN_ID */ 1132 + nla_total_size(sizeof(struct fib_kuid_range)) 1133 + nla_total_size(1) /* FRA_PROTOCOL */ 1134 + nla_total_size(1) /* FRA_IP_PROTO */ 1135 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_SPORT_RANGE */ 1136 + nla_total_size(sizeof(struct fib_rule_port_range)) /* FRA_DPORT_RANGE */ 1137 + nla_total_size(2) /* FRA_SPORT_MASK */ 1138 + nla_total_size(2); /* FRA_DPORT_MASK */ 1139 1140 if (ops->nlmsg_payload) 1141 payload += ops->nlmsg_payload(rule); 1142 1143 return payload; 1144 } 1145 1146 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule, 1147 u32 pid, u32 seq, int type, int flags, 1148 struct fib_rules_ops *ops) 1149 { 1150 struct nlmsghdr *nlh; 1151 struct fib_rule_hdr *frh; 1152 1153 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags); 1154 if (nlh == NULL) 1155 return -EMSGSIZE; 1156 1157 frh = nlmsg_data(nlh); 1158 frh->family = ops->family; 1159 frh->table = rule->table < 256 ? rule->table : RT_TABLE_COMPAT; 1160 if (nla_put_u32(skb, FRA_TABLE, rule->table)) 1161 goto nla_put_failure; 1162 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen)) 1163 goto nla_put_failure; 1164 frh->res1 = 0; 1165 frh->res2 = 0; 1166 frh->action = rule->action; 1167 frh->flags = rule->flags; 1168 1169 if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto)) 1170 goto nla_put_failure; 1171 1172 if (rule->action == FR_ACT_GOTO && 1173 rcu_access_pointer(rule->ctarget) == NULL) 1174 frh->flags |= FIB_RULE_UNRESOLVED; 1175 1176 if (rule->iifname[0]) { 1177 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname)) 1178 goto nla_put_failure; 1179 if (READ_ONCE(rule->iifindex) == -1) 1180 frh->flags |= FIB_RULE_IIF_DETACHED; 1181 } 1182 1183 if (rule->oifname[0]) { 1184 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname)) 1185 goto nla_put_failure; 1186 if (READ_ONCE(rule->oifindex) == -1) 1187 frh->flags |= FIB_RULE_OIF_DETACHED; 1188 } 1189 1190 if ((rule->pref && 1191 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) || 1192 (rule->mark && 1193 nla_put_u32(skb, FRA_FWMARK, rule->mark)) || 1194 ((rule->mark_mask || rule->mark) && 1195 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) || 1196 (rule->target && 1197 nla_put_u32(skb, FRA_GOTO, rule->target)) || 1198 (rule->tun_id && 1199 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) || 1200 (rule->l3mdev && 1201 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) || 1202 (uid_range_set(&rule->uid_range) && 1203 nla_put_uid_range(skb, &rule->uid_range)) || 1204 (fib_rule_port_range_set(&rule->sport_range) && 1205 nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) || 1206 (rule->sport_mask && nla_put_u16(skb, FRA_SPORT_MASK, 1207 rule->sport_mask)) || 1208 (fib_rule_port_range_set(&rule->dport_range) && 1209 nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) || 1210 (rule->dport_mask && nla_put_u16(skb, FRA_DPORT_MASK, 1211 rule->dport_mask)) || 1212 (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto))) 1213 goto nla_put_failure; 1214 1215 if (rule->suppress_ifgroup != -1) { 1216 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup)) 1217 goto nla_put_failure; 1218 } 1219 1220 if (ops->fill(rule, skb, frh) < 0) 1221 goto nla_put_failure; 1222 1223 nlmsg_end(skb, nlh); 1224 return 0; 1225 1226 nla_put_failure: 1227 nlmsg_cancel(skb, nlh); 1228 return -EMSGSIZE; 1229 } 1230 1231 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb, 1232 struct fib_rules_ops *ops) 1233 { 1234 int idx = 0; 1235 struct fib_rule *rule; 1236 int err = 0; 1237 1238 rcu_read_lock(); 1239 list_for_each_entry_rcu(rule, &ops->rules_list, list) { 1240 if (idx < cb->args[1]) 1241 goto skip; 1242 1243 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid, 1244 cb->nlh->nlmsg_seq, RTM_NEWRULE, 1245 NLM_F_MULTI, ops); 1246 if (err) 1247 break; 1248 skip: 1249 idx++; 1250 } 1251 rcu_read_unlock(); 1252 cb->args[1] = idx; 1253 rules_ops_put(ops); 1254 1255 return err; 1256 } 1257 1258 static int fib_valid_dumprule_req(const struct nlmsghdr *nlh, 1259 struct netlink_ext_ack *extack) 1260 { 1261 struct fib_rule_hdr *frh; 1262 1263 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) { 1264 NL_SET_ERR_MSG(extack, "Invalid header for fib rule dump request"); 1265 return -EINVAL; 1266 } 1267 1268 frh = nlmsg_data(nlh); 1269 if (frh->dst_len || frh->src_len || frh->tos || frh->table || 1270 frh->res1 || frh->res2 || frh->action || frh->flags) { 1271 NL_SET_ERR_MSG(extack, 1272 "Invalid values in header for fib rule dump request"); 1273 return -EINVAL; 1274 } 1275 1276 if (nlmsg_attrlen(nlh, sizeof(*frh))) { 1277 NL_SET_ERR_MSG(extack, "Invalid data after header in fib rule dump request"); 1278 return -EINVAL; 1279 } 1280 1281 return 0; 1282 } 1283 1284 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb) 1285 { 1286 const struct nlmsghdr *nlh = cb->nlh; 1287 struct net *net = sock_net(skb->sk); 1288 struct fib_rules_ops *ops; 1289 int err, idx = 0, family; 1290 1291 if (cb->strict_check) { 1292 err = fib_valid_dumprule_req(nlh, cb->extack); 1293 1294 if (err < 0) 1295 return err; 1296 } 1297 1298 family = rtnl_msg_family(nlh); 1299 if (family != AF_UNSPEC) { 1300 /* Protocol specific dump request */ 1301 ops = lookup_rules_ops(net, family); 1302 if (ops == NULL) 1303 return -EAFNOSUPPORT; 1304 1305 return dump_rules(skb, cb, ops); 1306 } 1307 1308 err = 0; 1309 rcu_read_lock(); 1310 list_for_each_entry_rcu(ops, &net->rules_ops, list) { 1311 if (idx < cb->args[0] || !try_module_get(ops->owner)) 1312 goto skip; 1313 1314 err = dump_rules(skb, cb, ops); 1315 if (err < 0) 1316 break; 1317 1318 cb->args[1] = 0; 1319 skip: 1320 idx++; 1321 } 1322 rcu_read_unlock(); 1323 cb->args[0] = idx; 1324 1325 return err; 1326 } 1327 1328 static void notify_rule_change(int event, struct fib_rule *rule, 1329 struct fib_rules_ops *ops, struct nlmsghdr *nlh, 1330 u32 pid) 1331 { 1332 struct net *net; 1333 struct sk_buff *skb; 1334 int err = -ENOMEM; 1335 1336 net = ops->fro_net; 1337 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL); 1338 if (skb == NULL) 1339 goto errout; 1340 1341 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops); 1342 if (err < 0) { 1343 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */ 1344 WARN_ON(err == -EMSGSIZE); 1345 kfree_skb(skb); 1346 goto errout; 1347 } 1348 1349 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL); 1350 return; 1351 errout: 1352 rtnl_set_sk_err(net, ops->nlgroup, err); 1353 } 1354 1355 static void attach_rules(struct list_head *rules, struct net_device *dev) 1356 { 1357 struct fib_rule *rule; 1358 1359 list_for_each_entry(rule, rules, list) { 1360 if (rule->iifindex == -1 && 1361 strcmp(dev->name, rule->iifname) == 0) { 1362 WRITE_ONCE(rule->iifindex, dev->ifindex); 1363 WRITE_ONCE(rule->iif_is_l3_master, 1364 netif_is_l3_master(dev)); 1365 } 1366 if (rule->oifindex == -1 && 1367 strcmp(dev->name, rule->oifname) == 0) { 1368 WRITE_ONCE(rule->oifindex, dev->ifindex); 1369 WRITE_ONCE(rule->oif_is_l3_master, 1370 netif_is_l3_master(dev)); 1371 } 1372 } 1373 } 1374 1375 static void detach_rules(struct list_head *rules, struct net_device *dev) 1376 { 1377 struct fib_rule *rule; 1378 1379 list_for_each_entry(rule, rules, list) { 1380 if (rule->iifindex == dev->ifindex) { 1381 WRITE_ONCE(rule->iifindex, -1); 1382 WRITE_ONCE(rule->iif_is_l3_master, false); 1383 } 1384 if (rule->oifindex == dev->ifindex) { 1385 WRITE_ONCE(rule->oifindex, -1); 1386 WRITE_ONCE(rule->oif_is_l3_master, false); 1387 } 1388 } 1389 } 1390 1391 1392 static int fib_rules_event(struct notifier_block *this, unsigned long event, 1393 void *ptr) 1394 { 1395 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1396 struct net *net = dev_net(dev); 1397 struct fib_rules_ops *ops; 1398 1399 ASSERT_RTNL(); 1400 1401 switch (event) { 1402 case NETDEV_REGISTER: 1403 list_for_each_entry(ops, &net->rules_ops, list) 1404 attach_rules(&ops->rules_list, dev); 1405 break; 1406 1407 case NETDEV_CHANGENAME: 1408 list_for_each_entry(ops, &net->rules_ops, list) { 1409 detach_rules(&ops->rules_list, dev); 1410 attach_rules(&ops->rules_list, dev); 1411 } 1412 break; 1413 1414 case NETDEV_UNREGISTER: 1415 list_for_each_entry(ops, &net->rules_ops, list) 1416 detach_rules(&ops->rules_list, dev); 1417 break; 1418 } 1419 1420 return NOTIFY_DONE; 1421 } 1422 1423 static struct notifier_block fib_rules_notifier = { 1424 .notifier_call = fib_rules_event, 1425 }; 1426 1427 static int __net_init fib_rules_net_init(struct net *net) 1428 { 1429 INIT_LIST_HEAD(&net->rules_ops); 1430 spin_lock_init(&net->rules_mod_lock); 1431 return 0; 1432 } 1433 1434 static void __net_exit fib_rules_net_exit(struct net *net) 1435 { 1436 WARN_ON_ONCE(!list_empty(&net->rules_ops)); 1437 } 1438 1439 static struct pernet_operations fib_rules_net_ops = { 1440 .init = fib_rules_net_init, 1441 .exit = fib_rules_net_exit, 1442 }; 1443 1444 static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = { 1445 {.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule, 1446 .flags = RTNL_FLAG_DOIT_PERNET}, 1447 {.msgtype = RTM_DELRULE, .doit = fib_nl_delrule, 1448 .flags = RTNL_FLAG_DOIT_PERNET}, 1449 {.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule, 1450 .flags = RTNL_FLAG_DUMP_UNLOCKED}, 1451 }; 1452 1453 static int __init fib_rules_init(void) 1454 { 1455 int err; 1456 1457 rtnl_register_many(fib_rules_rtnl_msg_handlers); 1458 1459 err = register_pernet_subsys(&fib_rules_net_ops); 1460 if (err < 0) 1461 goto fail; 1462 1463 err = register_netdevice_notifier(&fib_rules_notifier); 1464 if (err < 0) 1465 goto fail_unregister; 1466 1467 return 0; 1468 1469 fail_unregister: 1470 unregister_pernet_subsys(&fib_rules_net_ops); 1471 fail: 1472 rtnl_unregister_many(fib_rules_rtnl_msg_handlers); 1473 return err; 1474 } 1475 1476 subsys_initcall(fib_rules_init); 1477