1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * IPv4 Forwarding Information Base: FIB frontend. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/uaccess.h> 14 #include <linux/bitops.h> 15 #include <linux/capability.h> 16 #include <linux/types.h> 17 #include <linux/kernel.h> 18 #include <linux/mm.h> 19 #include <linux/string.h> 20 #include <linux/socket.h> 21 #include <linux/sockios.h> 22 #include <linux/errno.h> 23 #include <linux/in.h> 24 #include <linux/inet.h> 25 #include <linux/inetdevice.h> 26 #include <linux/netdevice.h> 27 #include <linux/if_addr.h> 28 #include <linux/if_arp.h> 29 #include <linux/skbuff.h> 30 #include <linux/cache.h> 31 #include <linux/init.h> 32 #include <linux/list.h> 33 #include <linux/slab.h> 34 35 #include <net/flow.h> 36 #include <net/inet_dscp.h> 37 #include <net/ip.h> 38 #include <net/protocol.h> 39 #include <net/route.h> 40 #include <net/tcp.h> 41 #include <net/sock.h> 42 #include <net/arp.h> 43 #include <net/ip_fib.h> 44 #include <net/nexthop.h> 45 #include <net/rtnetlink.h> 46 #include <net/xfrm.h> 47 #include <net/l3mdev.h> 48 #include <net/lwtunnel.h> 49 #include <trace/events/fib.h> 50 51 #ifndef CONFIG_IP_MULTIPLE_TABLES 52 53 static int __net_init fib4_rules_init(struct net *net) 54 { 55 struct fib_table *local_table, *main_table; 56 57 main_table = fib_trie_table(RT_TABLE_MAIN, NULL); 58 if (!main_table) 59 return -ENOMEM; 60 61 local_table = fib_trie_table(RT_TABLE_LOCAL, main_table); 62 if (!local_table) 63 goto fail; 64 65 hlist_add_head_rcu(&local_table->tb_hlist, 66 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]); 67 hlist_add_head_rcu(&main_table->tb_hlist, 68 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]); 69 return 0; 70 71 fail: 72 fib_free_table(main_table); 73 return -ENOMEM; 74 } 75 #else 76 77 struct fib_table *fib_new_table(struct net *net, u32 id) 78 { 79 struct fib_table *tb, *alias = NULL; 80 unsigned int h; 81 82 if (id == 0) 83 id = RT_TABLE_MAIN; 84 tb = fib_get_table(net, id); 85 if (tb) 86 return tb; 87 88 if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules) 89 alias = fib_new_table(net, RT_TABLE_MAIN); 90 91 tb = fib_trie_table(id, alias); 92 if (!tb) 93 return NULL; 94 95 switch (id) { 96 case RT_TABLE_MAIN: 97 rcu_assign_pointer(net->ipv4.fib_main, tb); 98 break; 99 case RT_TABLE_DEFAULT: 100 rcu_assign_pointer(net->ipv4.fib_default, tb); 101 break; 102 default: 103 break; 104 } 105 106 h = id & (FIB_TABLE_HASHSZ - 1); 107 hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]); 108 return tb; 109 } 110 EXPORT_SYMBOL_GPL(fib_new_table); 111 112 /* caller must hold either rtnl or rcu read lock */ 113 struct fib_table *fib_get_table(struct net *net, u32 id) 114 { 115 struct fib_table *tb; 116 struct hlist_head *head; 117 unsigned int h; 118 119 if (id == 0) 120 id = RT_TABLE_MAIN; 121 h = id & (FIB_TABLE_HASHSZ - 1); 122 123 head = &net->ipv4.fib_table_hash[h]; 124 hlist_for_each_entry_rcu(tb, head, tb_hlist, 125 lockdep_rtnl_is_held()) { 126 if (tb->tb_id == id) 127 return tb; 128 } 129 return NULL; 130 } 131 #endif /* CONFIG_IP_MULTIPLE_TABLES */ 132 133 static void fib_replace_table(struct net *net, struct fib_table *old, 134 struct fib_table *new) 135 { 136 #ifdef CONFIG_IP_MULTIPLE_TABLES 137 switch (new->tb_id) { 138 case RT_TABLE_MAIN: 139 rcu_assign_pointer(net->ipv4.fib_main, new); 140 break; 141 case RT_TABLE_DEFAULT: 142 rcu_assign_pointer(net->ipv4.fib_default, new); 143 break; 144 default: 145 break; 146 } 147 148 #endif 149 /* replace the old table in the hlist */ 150 hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist); 151 } 152 153 int fib_unmerge(struct net *net) 154 { 155 struct fib_table *old, *new, *main_table; 156 157 /* attempt to fetch local table if it has been allocated */ 158 old = fib_get_table(net, RT_TABLE_LOCAL); 159 if (!old) 160 return 0; 161 162 new = fib_trie_unmerge(old); 163 if (!new) 164 return -ENOMEM; 165 166 /* table is already unmerged */ 167 if (new == old) 168 return 0; 169 170 /* replace merged table with clean table */ 171 fib_replace_table(net, old, new); 172 fib_free_table(old); 173 174 /* attempt to fetch main table if it has been allocated */ 175 main_table = fib_get_table(net, RT_TABLE_MAIN); 176 if (!main_table) 177 return 0; 178 179 /* flush local entries from main table */ 180 fib_table_flush_external(main_table); 181 182 return 0; 183 } 184 185 void fib_flush(struct net *net) 186 { 187 int flushed = 0; 188 unsigned int h; 189 190 for (h = 0; h < FIB_TABLE_HASHSZ; h++) { 191 struct hlist_head *head = &net->ipv4.fib_table_hash[h]; 192 struct hlist_node *tmp; 193 struct fib_table *tb; 194 195 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) 196 flushed += fib_table_flush(net, tb, false); 197 } 198 199 if (flushed) 200 rt_cache_flush(net); 201 } 202 203 /* 204 * Find address type as if only "dev" was present in the system. If 205 * on_dev is NULL then all interfaces are taken into consideration. 206 */ 207 static inline unsigned int __inet_dev_addr_type(struct net *net, 208 const struct net_device *dev, 209 __be32 addr, u32 tb_id) 210 { 211 struct flowi4 fl4 = { .daddr = addr }; 212 struct fib_result res; 213 unsigned int ret = RTN_BROADCAST; 214 struct fib_table *table; 215 216 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) 217 return RTN_BROADCAST; 218 if (ipv4_is_multicast(addr)) 219 return RTN_MULTICAST; 220 221 rcu_read_lock(); 222 223 table = fib_get_table(net, tb_id); 224 if (table) { 225 ret = RTN_UNICAST; 226 if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) { 227 struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0); 228 229 if (!dev || dev == nhc->nhc_dev) 230 ret = res.type; 231 } 232 } 233 234 rcu_read_unlock(); 235 return ret; 236 } 237 238 unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id) 239 { 240 return __inet_dev_addr_type(net, NULL, addr, tb_id); 241 } 242 EXPORT_SYMBOL(inet_addr_type_table); 243 244 unsigned int inet_addr_type(struct net *net, __be32 addr) 245 { 246 return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL); 247 } 248 EXPORT_SYMBOL(inet_addr_type); 249 250 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, 251 __be32 addr) 252 { 253 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; 254 255 return __inet_dev_addr_type(net, dev, addr, rt_table); 256 } 257 EXPORT_SYMBOL(inet_dev_addr_type); 258 259 /* inet_addr_type with dev == NULL but using the table from a dev 260 * if one is associated 261 */ 262 unsigned int inet_addr_type_dev_table(struct net *net, 263 const struct net_device *dev, 264 __be32 addr) 265 { 266 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; 267 268 return __inet_dev_addr_type(net, NULL, addr, rt_table); 269 } 270 EXPORT_SYMBOL(inet_addr_type_dev_table); 271 272 __be32 fib_compute_spec_dst(struct sk_buff *skb) 273 { 274 struct net_device *dev = skb->dev; 275 struct in_device *in_dev; 276 struct fib_result res; 277 struct rtable *rt; 278 struct net *net; 279 int scope; 280 281 rt = skb_rtable(skb); 282 if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) == 283 RTCF_LOCAL) 284 return ip_hdr(skb)->daddr; 285 286 in_dev = __in_dev_get_rcu(dev); 287 288 net = dev_net(dev); 289 290 scope = RT_SCOPE_UNIVERSE; 291 if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) { 292 bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev); 293 struct flowi4 fl4 = { 294 .flowi4_iif = LOOPBACK_IFINDEX, 295 .flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev), 296 .daddr = ip_hdr(skb)->saddr, 297 .flowi4_dscp = ip4h_dscp(ip_hdr(skb)), 298 .flowi4_scope = scope, 299 .flowi4_mark = vmark ? skb->mark : 0, 300 }; 301 if (!fib_lookup(net, &fl4, &res, 0)) 302 return fib_result_prefsrc(net, &res); 303 } else { 304 scope = RT_SCOPE_LINK; 305 } 306 307 return inet_select_addr(dev, ip_hdr(skb)->saddr, scope); 308 } 309 310 bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev) 311 { 312 bool dev_match = false; 313 #ifdef CONFIG_IP_ROUTE_MULTIPATH 314 if (unlikely(fi->nh)) { 315 dev_match = nexthop_uses_dev(fi->nh, dev); 316 } else { 317 int ret; 318 319 for (ret = 0; ret < fib_info_num_path(fi); ret++) { 320 const struct fib_nh_common *nhc = fib_info_nhc(fi, ret); 321 322 if (nhc_l3mdev_matches_dev(nhc, dev)) { 323 dev_match = true; 324 break; 325 } 326 } 327 } 328 #else 329 if (fib_info_nhc(fi, 0)->nhc_dev == dev) 330 dev_match = true; 331 #endif 332 333 return dev_match; 334 } 335 EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev); 336 337 /* Given (packet source, input interface) and optional (dst, oif, tos): 338 * - (main) check, that source is valid i.e. not broadcast or our local 339 * address. 340 * - figure out what "logical" interface this packet arrived 341 * and calculate "specific destination" address. 342 * - check, that packet arrived from expected physical interface. 343 * called with rcu_read_lock() 344 */ 345 static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, 346 dscp_t dscp, int oif, struct net_device *dev, 347 int rpf, struct in_device *idev, u32 *itag) 348 { 349 struct net *net = dev_net(dev); 350 enum skb_drop_reason reason; 351 struct flow_keys flkeys; 352 int ret, no_addr; 353 struct fib_result res; 354 struct flowi4 fl4; 355 bool dev_match; 356 357 fl4.flowi4_oif = 0; 358 fl4.flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev); 359 fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX; 360 fl4.daddr = src; 361 fl4.saddr = dst; 362 fl4.flowi4_dscp = dscp; 363 fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 364 fl4.flowi4_tun_key.tun_id = 0; 365 fl4.flowi4_flags = 0; 366 fl4.flowi4_uid = sock_net_uid(net, NULL); 367 fl4.flowi4_multipath_hash = 0; 368 369 no_addr = idev->ifa_list == NULL; 370 371 fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0; 372 if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) { 373 fl4.flowi4_proto = 0; 374 fl4.fl4_sport = 0; 375 fl4.fl4_dport = 0; 376 } else { 377 swap(fl4.fl4_sport, fl4.fl4_dport); 378 } 379 380 if (fib_lookup(net, &fl4, &res, 0)) 381 goto last_resort; 382 if (res.type != RTN_UNICAST) { 383 if (res.type != RTN_LOCAL) { 384 reason = SKB_DROP_REASON_IP_INVALID_SOURCE; 385 goto e_inval; 386 } else if (!IN_DEV_ACCEPT_LOCAL(idev)) { 387 reason = SKB_DROP_REASON_IP_LOCAL_SOURCE; 388 goto e_inval; 389 } 390 } 391 fib_combine_itag(itag, &res); 392 393 dev_match = fib_info_nh_uses_dev(res.fi, dev); 394 /* This is not common, loopback packets retain skb_dst so normally they 395 * would not even hit this slow path. 396 */ 397 dev_match = dev_match || (res.type == RTN_LOCAL && 398 dev == net->loopback_dev); 399 if (dev_match) { 400 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; 401 return ret; 402 } 403 if (no_addr) 404 goto last_resort; 405 if (rpf == 1) 406 goto e_rpf; 407 fl4.flowi4_oif = dev->ifindex; 408 409 ret = 0; 410 if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) { 411 if (res.type == RTN_UNICAST) 412 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; 413 } 414 return ret; 415 416 last_resort: 417 if (rpf) 418 goto e_rpf; 419 *itag = 0; 420 return 0; 421 422 e_inval: 423 return -reason; 424 e_rpf: 425 return -SKB_DROP_REASON_IP_RPFILTER; 426 } 427 428 /* Ignore rp_filter for packets protected by IPsec. */ 429 int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, 430 dscp_t dscp, int oif, struct net_device *dev, 431 struct in_device *idev, u32 *itag) 432 { 433 int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev); 434 struct net *net = dev_net(dev); 435 436 if (!r && !fib_num_tclassid_users(net) && 437 (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) { 438 if (IN_DEV_ACCEPT_LOCAL(idev)) 439 goto ok; 440 /* with custom local routes in place, checking local addresses 441 * only will be too optimistic, with custom rules, checking 442 * local addresses only can be too strict, e.g. due to vrf 443 */ 444 if (net->ipv4.fib_has_custom_local_routes || 445 fib4_has_custom_rules(net)) 446 goto full_check; 447 /* Within the same container, it is regarded as a martian source, 448 * and the same host but different containers are not. 449 */ 450 if (inet_lookup_ifaddr_rcu(net, src)) 451 return -SKB_DROP_REASON_IP_LOCAL_SOURCE; 452 453 ok: 454 *itag = 0; 455 return 0; 456 } 457 458 full_check: 459 return __fib_validate_source(skb, src, dst, dscp, oif, dev, r, idev, 460 itag); 461 } 462 463 static inline __be32 sk_extract_addr(struct sockaddr *addr) 464 { 465 return ((struct sockaddr_in *) addr)->sin_addr.s_addr; 466 } 467 468 static int put_rtax(struct nlattr *mx, int len, int type, u32 value) 469 { 470 struct nlattr *nla; 471 472 nla = (struct nlattr *) ((char *) mx + len); 473 nla->nla_type = type; 474 nla->nla_len = nla_attr_size(4); 475 *(u32 *) nla_data(nla) = value; 476 477 return len + nla_total_size(4); 478 } 479 480 static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt, 481 struct fib_config *cfg) 482 { 483 __be32 addr; 484 int plen; 485 486 memset(cfg, 0, sizeof(*cfg)); 487 cfg->fc_nlinfo.nl_net = net; 488 489 if (rt->rt_dst.sa_family != AF_INET) 490 return -EAFNOSUPPORT; 491 492 /* 493 * Check mask for validity: 494 * a) it must be contiguous. 495 * b) destination must have all host bits clear. 496 * c) if application forgot to set correct family (AF_INET), 497 * reject request unless it is absolutely clear i.e. 498 * both family and mask are zero. 499 */ 500 plen = 32; 501 addr = sk_extract_addr(&rt->rt_dst); 502 if (!(rt->rt_flags & RTF_HOST)) { 503 __be32 mask = sk_extract_addr(&rt->rt_genmask); 504 505 if (rt->rt_genmask.sa_family != AF_INET) { 506 if (mask || rt->rt_genmask.sa_family) 507 return -EAFNOSUPPORT; 508 } 509 510 if (bad_mask(mask, addr)) 511 return -EINVAL; 512 513 plen = inet_mask_len(mask); 514 } 515 516 cfg->fc_dst_len = plen; 517 cfg->fc_dst = addr; 518 519 if (cmd != SIOCDELRT) { 520 cfg->fc_nlflags = NLM_F_CREATE; 521 cfg->fc_protocol = RTPROT_BOOT; 522 } 523 524 if (rt->rt_metric) 525 cfg->fc_priority = rt->rt_metric - 1; 526 527 if (rt->rt_flags & RTF_REJECT) { 528 cfg->fc_scope = RT_SCOPE_HOST; 529 cfg->fc_type = RTN_UNREACHABLE; 530 return 0; 531 } 532 533 cfg->fc_scope = RT_SCOPE_NOWHERE; 534 cfg->fc_type = RTN_UNICAST; 535 536 if (rt->rt_dev) { 537 char *colon; 538 struct net_device *dev; 539 char devname[IFNAMSIZ]; 540 541 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1)) 542 return -EFAULT; 543 544 devname[IFNAMSIZ-1] = 0; 545 colon = strchr(devname, ':'); 546 if (colon) 547 *colon = 0; 548 dev = __dev_get_by_name(net, devname); 549 if (!dev) 550 return -ENODEV; 551 cfg->fc_oif = dev->ifindex; 552 cfg->fc_table = l3mdev_fib_table(dev); 553 if (colon) { 554 const struct in_ifaddr *ifa; 555 struct in_device *in_dev; 556 557 in_dev = __in_dev_get_rtnl_net(dev); 558 if (!in_dev) 559 return -ENODEV; 560 561 *colon = ':'; 562 563 in_dev_for_each_ifa_rtnl_net(net, ifa, in_dev) { 564 if (strcmp(ifa->ifa_label, devname) == 0) 565 break; 566 } 567 568 if (!ifa) 569 return -ENODEV; 570 cfg->fc_prefsrc = ifa->ifa_local; 571 } 572 } 573 574 addr = sk_extract_addr(&rt->rt_gateway); 575 if (rt->rt_gateway.sa_family == AF_INET && addr) { 576 unsigned int addr_type; 577 578 cfg->fc_gw4 = addr; 579 cfg->fc_gw_family = AF_INET; 580 addr_type = inet_addr_type_table(net, addr, cfg->fc_table); 581 if (rt->rt_flags & RTF_GATEWAY && 582 addr_type == RTN_UNICAST) 583 cfg->fc_scope = RT_SCOPE_UNIVERSE; 584 } 585 586 if (!cfg->fc_table) 587 cfg->fc_table = RT_TABLE_MAIN; 588 589 if (cmd == SIOCDELRT) 590 return 0; 591 592 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family) 593 return -EINVAL; 594 595 if (cfg->fc_scope == RT_SCOPE_NOWHERE) 596 cfg->fc_scope = RT_SCOPE_LINK; 597 598 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) { 599 struct nlattr *mx; 600 int len = 0; 601 602 mx = kcalloc(3, nla_total_size(4), GFP_KERNEL); 603 if (!mx) 604 return -ENOMEM; 605 606 if (rt->rt_flags & RTF_MTU) 607 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40); 608 609 if (rt->rt_flags & RTF_WINDOW) 610 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window); 611 612 if (rt->rt_flags & RTF_IRTT) 613 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3); 614 615 cfg->fc_mx = mx; 616 cfg->fc_mx_len = len; 617 } 618 619 return 0; 620 } 621 622 /* 623 * Handle IP routing ioctl calls. 624 * These are used to manipulate the routing tables 625 */ 626 int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt) 627 { 628 struct fib_config cfg; 629 int err; 630 631 switch (cmd) { 632 case SIOCADDRT: /* Add a route */ 633 case SIOCDELRT: /* Delete a route */ 634 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 635 return -EPERM; 636 637 rtnl_net_lock(net); 638 err = rtentry_to_fib_config(net, cmd, rt, &cfg); 639 if (err == 0) { 640 struct fib_table *tb; 641 642 if (cmd == SIOCDELRT) { 643 tb = fib_get_table(net, cfg.fc_table); 644 if (tb) 645 err = fib_table_delete(net, tb, &cfg, 646 NULL); 647 else 648 err = -ESRCH; 649 } else { 650 tb = fib_new_table(net, cfg.fc_table); 651 if (tb) 652 err = fib_table_insert(net, tb, 653 &cfg, NULL); 654 else 655 err = -ENOBUFS; 656 } 657 658 /* allocated by rtentry_to_fib_config() */ 659 kfree(cfg.fc_mx); 660 } 661 rtnl_net_unlock(net); 662 return err; 663 } 664 return -EINVAL; 665 } 666 667 const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { 668 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, 669 [RTA_DST] = { .type = NLA_U32 }, 670 [RTA_SRC] = { .type = NLA_U32 }, 671 [RTA_IIF] = { .type = NLA_U32 }, 672 [RTA_OIF] = { .type = NLA_U32 }, 673 [RTA_GATEWAY] = { .type = NLA_U32 }, 674 [RTA_PRIORITY] = { .type = NLA_U32 }, 675 [RTA_PREFSRC] = { .type = NLA_U32 }, 676 [RTA_METRICS] = { .type = NLA_NESTED }, 677 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 678 [RTA_FLOW] = { .type = NLA_U32 }, 679 [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 680 [RTA_ENCAP] = { .type = NLA_NESTED }, 681 [RTA_UID] = { .type = NLA_U32 }, 682 [RTA_MARK] = { .type = NLA_U32 }, 683 [RTA_TABLE] = { .type = NLA_U32 }, 684 [RTA_IP_PROTO] = { .type = NLA_U8 }, 685 [RTA_SPORT] = { .type = NLA_U16 }, 686 [RTA_DPORT] = { .type = NLA_U16 }, 687 [RTA_NH_ID] = { .type = NLA_U32 }, 688 }; 689 690 int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla, 691 struct netlink_ext_ack *extack) 692 { 693 struct rtvia *via; 694 int alen; 695 696 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) { 697 NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA"); 698 return -EINVAL; 699 } 700 701 via = nla_data(nla); 702 alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr); 703 704 switch (via->rtvia_family) { 705 case AF_INET: 706 if (alen != sizeof(__be32)) { 707 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA"); 708 return -EINVAL; 709 } 710 cfg->fc_gw_family = AF_INET; 711 cfg->fc_gw4 = *((__be32 *)via->rtvia_addr); 712 break; 713 case AF_INET6: 714 #if IS_ENABLED(CONFIG_IPV6) 715 if (alen != sizeof(struct in6_addr)) { 716 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA"); 717 return -EINVAL; 718 } 719 cfg->fc_gw_family = AF_INET6; 720 cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr); 721 #else 722 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel"); 723 return -EINVAL; 724 #endif 725 break; 726 default: 727 NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA"); 728 return -EINVAL; 729 } 730 731 return 0; 732 } 733 734 static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, 735 struct nlmsghdr *nlh, struct fib_config *cfg, 736 struct netlink_ext_ack *extack) 737 { 738 bool has_gw = false, has_via = false; 739 struct nlattr *attr; 740 int err, remaining; 741 struct rtmsg *rtm; 742 743 err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX, 744 rtm_ipv4_policy, extack); 745 if (err < 0) 746 goto errout; 747 748 memset(cfg, 0, sizeof(*cfg)); 749 750 rtm = nlmsg_data(nlh); 751 752 if (!inet_validate_dscp(rtm->rtm_tos)) { 753 NL_SET_ERR_MSG(extack, 754 "Invalid dsfield (tos): ECN bits must be 0"); 755 err = -EINVAL; 756 goto errout; 757 } 758 cfg->fc_dscp = inet_dsfield_to_dscp(rtm->rtm_tos); 759 760 cfg->fc_dst_len = rtm->rtm_dst_len; 761 cfg->fc_table = rtm->rtm_table; 762 cfg->fc_protocol = rtm->rtm_protocol; 763 cfg->fc_scope = rtm->rtm_scope; 764 cfg->fc_type = rtm->rtm_type; 765 cfg->fc_flags = rtm->rtm_flags; 766 cfg->fc_nlflags = nlh->nlmsg_flags; 767 768 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 769 cfg->fc_nlinfo.nlh = nlh; 770 cfg->fc_nlinfo.nl_net = net; 771 772 if (cfg->fc_type > RTN_MAX) { 773 NL_SET_ERR_MSG(extack, "Invalid route type"); 774 err = -EINVAL; 775 goto errout; 776 } 777 778 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) { 779 switch (nla_type(attr)) { 780 case RTA_DST: 781 cfg->fc_dst = nla_get_be32(attr); 782 break; 783 case RTA_OIF: 784 cfg->fc_oif = nla_get_u32(attr); 785 break; 786 case RTA_GATEWAY: 787 has_gw = true; 788 cfg->fc_gw4 = nla_get_be32(attr); 789 if (cfg->fc_gw4) 790 cfg->fc_gw_family = AF_INET; 791 break; 792 case RTA_VIA: 793 has_via = true; 794 err = fib_gw_from_via(cfg, attr, extack); 795 if (err) 796 goto errout; 797 break; 798 case RTA_PRIORITY: 799 cfg->fc_priority = nla_get_u32(attr); 800 break; 801 case RTA_PREFSRC: 802 cfg->fc_prefsrc = nla_get_be32(attr); 803 break; 804 case RTA_METRICS: 805 cfg->fc_mx = nla_data(attr); 806 cfg->fc_mx_len = nla_len(attr); 807 break; 808 case RTA_MULTIPATH: 809 err = lwtunnel_valid_encap_type_attr(nla_data(attr), 810 nla_len(attr), 811 extack); 812 if (err < 0) 813 goto errout; 814 cfg->fc_mp = nla_data(attr); 815 cfg->fc_mp_len = nla_len(attr); 816 break; 817 case RTA_FLOW: 818 cfg->fc_flow = nla_get_u32(attr); 819 break; 820 case RTA_TABLE: 821 cfg->fc_table = nla_get_u32(attr); 822 break; 823 case RTA_ENCAP: 824 cfg->fc_encap = attr; 825 break; 826 case RTA_ENCAP_TYPE: 827 cfg->fc_encap_type = nla_get_u16(attr); 828 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, 829 extack); 830 if (err < 0) 831 goto errout; 832 break; 833 case RTA_NH_ID: 834 cfg->fc_nh_id = nla_get_u32(attr); 835 break; 836 } 837 } 838 839 if (cfg->fc_dst_len > 32) { 840 NL_SET_ERR_MSG(extack, "Invalid prefix length"); 841 err = -EINVAL; 842 goto errout; 843 } 844 845 if (cfg->fc_dst_len < 32 && (ntohl(cfg->fc_dst) << cfg->fc_dst_len)) { 846 NL_SET_ERR_MSG(extack, "Invalid prefix for given prefix length"); 847 err = -EINVAL; 848 goto errout; 849 } 850 851 if (cfg->fc_nh_id) { 852 if (cfg->fc_oif || cfg->fc_gw_family || 853 cfg->fc_encap || cfg->fc_mp) { 854 NL_SET_ERR_MSG(extack, 855 "Nexthop specification and nexthop id are mutually exclusive"); 856 err = -EINVAL; 857 goto errout; 858 } 859 } 860 861 if (has_gw && has_via) { 862 NL_SET_ERR_MSG(extack, 863 "Nexthop configuration can not contain both GATEWAY and VIA"); 864 err = -EINVAL; 865 goto errout; 866 } 867 868 if (!cfg->fc_table) 869 cfg->fc_table = RT_TABLE_MAIN; 870 871 return 0; 872 errout: 873 return err; 874 } 875 876 static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 877 struct netlink_ext_ack *extack) 878 { 879 struct net *net = sock_net(skb->sk); 880 struct fib_config cfg; 881 struct fib_table *tb; 882 int err; 883 884 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); 885 if (err < 0) 886 goto errout; 887 888 rtnl_net_lock(net); 889 890 if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) { 891 NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); 892 err = -EINVAL; 893 goto unlock; 894 } 895 896 tb = fib_get_table(net, cfg.fc_table); 897 if (!tb) { 898 NL_SET_ERR_MSG(extack, "FIB table does not exist"); 899 err = -ESRCH; 900 goto unlock; 901 } 902 903 err = fib_table_delete(net, tb, &cfg, extack); 904 unlock: 905 rtnl_net_unlock(net); 906 errout: 907 return err; 908 } 909 910 static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 911 struct netlink_ext_ack *extack) 912 { 913 struct net *net = sock_net(skb->sk); 914 struct fib_config cfg; 915 struct fib_table *tb; 916 int err; 917 918 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); 919 if (err < 0) 920 goto errout; 921 922 rtnl_net_lock(net); 923 924 tb = fib_new_table(net, cfg.fc_table); 925 if (!tb) { 926 err = -ENOBUFS; 927 goto unlock; 928 } 929 930 err = fib_table_insert(net, tb, &cfg, extack); 931 if (!err && cfg.fc_type == RTN_LOCAL) 932 net->ipv4.fib_has_custom_local_routes = true; 933 934 unlock: 935 rtnl_net_unlock(net); 936 errout: 937 return err; 938 } 939 940 int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh, 941 struct fib_dump_filter *filter, 942 struct netlink_callback *cb) 943 { 944 struct netlink_ext_ack *extack = cb->extack; 945 struct nlattr *tb[RTA_MAX + 1]; 946 struct rtmsg *rtm; 947 int err, i; 948 949 rtm = nlmsg_payload(nlh, sizeof(*rtm)); 950 if (!rtm) { 951 NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request"); 952 return -EINVAL; 953 } 954 955 if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos || 956 rtm->rtm_scope) { 957 NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request"); 958 return -EINVAL; 959 } 960 961 if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) { 962 NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request"); 963 return -EINVAL; 964 } 965 if (rtm->rtm_flags & RTM_F_CLONED) 966 filter->dump_routes = false; 967 else 968 filter->dump_exceptions = false; 969 970 filter->flags = rtm->rtm_flags; 971 filter->protocol = rtm->rtm_protocol; 972 filter->rt_type = rtm->rtm_type; 973 filter->table_id = rtm->rtm_table; 974 975 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 976 rtm_ipv4_policy, extack); 977 if (err < 0) 978 return err; 979 980 for (i = 0; i <= RTA_MAX; ++i) { 981 int ifindex; 982 983 if (!tb[i]) 984 continue; 985 986 switch (i) { 987 case RTA_TABLE: 988 filter->table_id = nla_get_u32(tb[i]); 989 break; 990 case RTA_OIF: 991 ifindex = nla_get_u32(tb[i]); 992 993 filter->dev = dev_get_by_index_rcu(net, ifindex); 994 if (!filter->dev) 995 return -ENODEV; 996 break; 997 default: 998 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); 999 return -EINVAL; 1000 } 1001 } 1002 1003 if (filter->flags || filter->protocol || filter->rt_type || 1004 filter->table_id || filter->dev) { 1005 filter->filter_set = 1; 1006 cb->answer_flags = NLM_F_DUMP_FILTERED; 1007 } 1008 1009 return 0; 1010 } 1011 EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req); 1012 1013 static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) 1014 { 1015 const struct nlmsghdr *nlh = cb->nlh; 1016 struct net *net = sock_net(skb->sk); 1017 struct fib_dump_filter filter = { 1018 .dump_routes = true, 1019 .dump_exceptions = true, 1020 }; 1021 unsigned int e = 0, s_e, h, s_h; 1022 struct hlist_head *head; 1023 int dumped = 0, err = 0; 1024 struct fib_table *tb; 1025 1026 rcu_read_lock(); 1027 if (cb->strict_check) { 1028 err = ip_valid_fib_dump_req(net, nlh, &filter, cb); 1029 if (err < 0) 1030 goto unlock; 1031 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) { 1032 struct rtmsg *rtm = nlmsg_data(nlh); 1033 1034 filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED); 1035 } 1036 1037 /* ipv4 does not use prefix flag */ 1038 if (filter.flags & RTM_F_PREFIX) 1039 goto unlock; 1040 1041 if (filter.table_id) { 1042 tb = fib_get_table(net, filter.table_id); 1043 if (!tb) { 1044 if (rtnl_msg_family(cb->nlh) != PF_INET) 1045 goto unlock; 1046 1047 NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist"); 1048 err = -ENOENT; 1049 goto unlock; 1050 } 1051 err = fib_table_dump(tb, skb, cb, &filter); 1052 goto unlock; 1053 } 1054 1055 s_h = cb->args[0]; 1056 s_e = cb->args[1]; 1057 1058 err = 0; 1059 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { 1060 e = 0; 1061 head = &net->ipv4.fib_table_hash[h]; 1062 hlist_for_each_entry_rcu(tb, head, tb_hlist) { 1063 if (e < s_e) 1064 goto next; 1065 if (dumped) 1066 memset(&cb->args[2], 0, sizeof(cb->args) - 1067 2 * sizeof(cb->args[0])); 1068 err = fib_table_dump(tb, skb, cb, &filter); 1069 if (err < 0) 1070 goto out; 1071 dumped = 1; 1072 next: 1073 e++; 1074 } 1075 } 1076 out: 1077 1078 cb->args[1] = e; 1079 cb->args[0] = h; 1080 1081 unlock: 1082 rcu_read_unlock(); 1083 return err; 1084 } 1085 1086 /* Prepare and feed intra-kernel routing request. 1087 * Really, it should be netlink message, but :-( netlink 1088 * can be not configured, so that we feed it directly 1089 * to fib engine. It is legal, because all events occur 1090 * only when netlink is already locked. 1091 */ 1092 static void fib_magic(int cmd, int type, __be32 dst, int dst_len, 1093 struct in_ifaddr *ifa, u32 rt_priority) 1094 { 1095 struct net *net = dev_net(ifa->ifa_dev->dev); 1096 u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev); 1097 struct fib_table *tb; 1098 struct fib_config cfg = { 1099 .fc_protocol = RTPROT_KERNEL, 1100 .fc_type = type, 1101 .fc_dst = dst, 1102 .fc_dst_len = dst_len, 1103 .fc_priority = rt_priority, 1104 .fc_prefsrc = ifa->ifa_local, 1105 .fc_oif = ifa->ifa_dev->dev->ifindex, 1106 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND, 1107 .fc_nlinfo = { 1108 .nl_net = net, 1109 }, 1110 }; 1111 1112 if (!tb_id) 1113 tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL; 1114 1115 tb = fib_new_table(net, tb_id); 1116 if (!tb) 1117 return; 1118 1119 cfg.fc_table = tb->tb_id; 1120 1121 if (type != RTN_LOCAL) 1122 cfg.fc_scope = RT_SCOPE_LINK; 1123 else 1124 cfg.fc_scope = RT_SCOPE_HOST; 1125 1126 if (cmd == RTM_NEWROUTE) 1127 fib_table_insert(net, tb, &cfg, NULL); 1128 else 1129 fib_table_delete(net, tb, &cfg, NULL); 1130 } 1131 1132 void fib_add_ifaddr(struct in_ifaddr *ifa) 1133 { 1134 struct in_device *in_dev = ifa->ifa_dev; 1135 struct net_device *dev = in_dev->dev; 1136 struct in_ifaddr *prim = ifa; 1137 __be32 mask = ifa->ifa_mask; 1138 __be32 addr = ifa->ifa_local; 1139 __be32 prefix = ifa->ifa_address & mask; 1140 1141 if (ifa->ifa_flags & IFA_F_SECONDARY) { 1142 prim = inet_ifa_byprefix(in_dev, prefix, mask); 1143 if (!prim) { 1144 pr_warn("%s: bug: prim == NULL\n", __func__); 1145 return; 1146 } 1147 } 1148 1149 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0); 1150 1151 if (!(dev->flags & IFF_UP)) 1152 return; 1153 1154 /* Add broadcast address, if it is explicitly assigned. */ 1155 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) { 1156 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, 1157 prim, 0); 1158 arp_invalidate(dev, ifa->ifa_broadcast, false); 1159 } 1160 1161 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) && 1162 (prefix != addr || ifa->ifa_prefixlen < 32)) { 1163 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) 1164 fib_magic(RTM_NEWROUTE, 1165 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, 1166 prefix, ifa->ifa_prefixlen, prim, 1167 ifa->ifa_rt_priority); 1168 1169 /* Add the network broadcast address, when it makes sense */ 1170 if (ifa->ifa_prefixlen < 31) { 1171 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask, 1172 32, prim, 0); 1173 arp_invalidate(dev, prefix | ~mask, false); 1174 } 1175 } 1176 } 1177 1178 void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric) 1179 { 1180 __be32 prefix = ifa->ifa_address & ifa->ifa_mask; 1181 struct in_device *in_dev = ifa->ifa_dev; 1182 struct net_device *dev = in_dev->dev; 1183 1184 if (!(dev->flags & IFF_UP) || 1185 ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) || 1186 ipv4_is_zeronet(prefix) || 1187 (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32)) 1188 return; 1189 1190 /* add the new */ 1191 fib_magic(RTM_NEWROUTE, 1192 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, 1193 prefix, ifa->ifa_prefixlen, ifa, new_metric); 1194 1195 /* delete the old */ 1196 fib_magic(RTM_DELROUTE, 1197 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, 1198 prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority); 1199 } 1200 1201 /* Delete primary or secondary address. 1202 * Optionally, on secondary address promotion consider the addresses 1203 * from subnet iprim as deleted, even if they are in device list. 1204 * In this case the secondary ifa can be in device list. 1205 */ 1206 void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim) 1207 { 1208 struct in_device *in_dev = ifa->ifa_dev; 1209 struct net_device *dev = in_dev->dev; 1210 struct in_ifaddr *ifa1; 1211 struct in_ifaddr *prim = ifa, *prim1 = NULL; 1212 __be32 brd = ifa->ifa_address | ~ifa->ifa_mask; 1213 __be32 any = ifa->ifa_address & ifa->ifa_mask; 1214 #define LOCAL_OK 1 1215 #define BRD_OK 2 1216 #define BRD0_OK 4 1217 #define BRD1_OK 8 1218 unsigned int ok = 0; 1219 int subnet = 0; /* Primary network */ 1220 int gone = 1; /* Address is missing */ 1221 int same_prefsrc = 0; /* Another primary with same IP */ 1222 1223 if (ifa->ifa_flags & IFA_F_SECONDARY) { 1224 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask); 1225 if (!prim) { 1226 /* if the device has been deleted, we don't perform 1227 * address promotion 1228 */ 1229 if (!in_dev->dead) 1230 pr_warn("%s: bug: prim == NULL\n", __func__); 1231 return; 1232 } 1233 if (iprim && iprim != prim) { 1234 pr_warn("%s: bug: iprim != prim\n", __func__); 1235 return; 1236 } 1237 } else if (!ipv4_is_zeronet(any) && 1238 (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) { 1239 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) 1240 fib_magic(RTM_DELROUTE, 1241 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, 1242 any, ifa->ifa_prefixlen, prim, 0); 1243 subnet = 1; 1244 } 1245 1246 if (in_dev->dead) 1247 goto no_promotions; 1248 1249 /* Deletion is more complicated than add. 1250 * We should take care of not to delete too much :-) 1251 * 1252 * Scan address list to be sure that addresses are really gone. 1253 */ 1254 rcu_read_lock(); 1255 in_dev_for_each_ifa_rcu(ifa1, in_dev) { 1256 if (ifa1 == ifa) { 1257 /* promotion, keep the IP */ 1258 gone = 0; 1259 continue; 1260 } 1261 /* Ignore IFAs from our subnet */ 1262 if (iprim && ifa1->ifa_mask == iprim->ifa_mask && 1263 inet_ifa_match(ifa1->ifa_address, iprim)) 1264 continue; 1265 1266 /* Ignore ifa1 if it uses different primary IP (prefsrc) */ 1267 if (ifa1->ifa_flags & IFA_F_SECONDARY) { 1268 /* Another address from our subnet? */ 1269 if (ifa1->ifa_mask == prim->ifa_mask && 1270 inet_ifa_match(ifa1->ifa_address, prim)) 1271 prim1 = prim; 1272 else { 1273 /* We reached the secondaries, so 1274 * same_prefsrc should be determined. 1275 */ 1276 if (!same_prefsrc) 1277 continue; 1278 /* Search new prim1 if ifa1 is not 1279 * using the current prim1 1280 */ 1281 if (!prim1 || 1282 ifa1->ifa_mask != prim1->ifa_mask || 1283 !inet_ifa_match(ifa1->ifa_address, prim1)) 1284 prim1 = inet_ifa_byprefix(in_dev, 1285 ifa1->ifa_address, 1286 ifa1->ifa_mask); 1287 if (!prim1) 1288 continue; 1289 if (prim1->ifa_local != prim->ifa_local) 1290 continue; 1291 } 1292 } else { 1293 if (prim->ifa_local != ifa1->ifa_local) 1294 continue; 1295 prim1 = ifa1; 1296 if (prim != prim1) 1297 same_prefsrc = 1; 1298 } 1299 if (ifa->ifa_local == ifa1->ifa_local) 1300 ok |= LOCAL_OK; 1301 if (ifa->ifa_broadcast == ifa1->ifa_broadcast) 1302 ok |= BRD_OK; 1303 if (brd == ifa1->ifa_broadcast) 1304 ok |= BRD1_OK; 1305 if (any == ifa1->ifa_broadcast) 1306 ok |= BRD0_OK; 1307 /* primary has network specific broadcasts */ 1308 if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) { 1309 __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask; 1310 __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask; 1311 1312 if (!ipv4_is_zeronet(any1)) { 1313 if (ifa->ifa_broadcast == brd1 || 1314 ifa->ifa_broadcast == any1) 1315 ok |= BRD_OK; 1316 if (brd == brd1 || brd == any1) 1317 ok |= BRD1_OK; 1318 if (any == brd1 || any == any1) 1319 ok |= BRD0_OK; 1320 } 1321 } 1322 } 1323 rcu_read_unlock(); 1324 1325 no_promotions: 1326 if (!(ok & BRD_OK)) 1327 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, 1328 prim, 0); 1329 if (subnet && ifa->ifa_prefixlen < 31) { 1330 if (!(ok & BRD1_OK)) 1331 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, 1332 prim, 0); 1333 if (!(ok & BRD0_OK)) 1334 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, 1335 prim, 0); 1336 } 1337 if (!(ok & LOCAL_OK)) { 1338 unsigned int addr_type; 1339 1340 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0); 1341 1342 /* Check, that this local address finally disappeared. */ 1343 addr_type = inet_addr_type_dev_table(dev_net(dev), dev, 1344 ifa->ifa_local); 1345 if (gone && addr_type != RTN_LOCAL) { 1346 /* And the last, but not the least thing. 1347 * We must flush stray FIB entries. 1348 * 1349 * First of all, we scan fib_info list searching 1350 * for stray nexthop entries, then ignite fib_flush. 1351 */ 1352 if (fib_sync_down_addr(dev, ifa->ifa_local)) 1353 fib_flush(dev_net(dev)); 1354 } 1355 } 1356 #undef LOCAL_OK 1357 #undef BRD_OK 1358 #undef BRD0_OK 1359 #undef BRD1_OK 1360 } 1361 1362 static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn) 1363 { 1364 1365 struct fib_result res; 1366 struct flowi4 fl4 = { 1367 .flowi4_mark = frn->fl_mark, 1368 .daddr = frn->fl_addr, 1369 .flowi4_dscp = inet_dsfield_to_dscp(frn->fl_tos), 1370 .flowi4_scope = frn->fl_scope, 1371 }; 1372 struct fib_table *tb; 1373 1374 rcu_read_lock(); 1375 1376 tb = fib_get_table(net, frn->tb_id_in); 1377 1378 frn->err = -ENOENT; 1379 if (tb) { 1380 local_bh_disable(); 1381 1382 frn->tb_id = tb->tb_id; 1383 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); 1384 1385 if (!frn->err) { 1386 frn->prefixlen = res.prefixlen; 1387 frn->nh_sel = res.nh_sel; 1388 frn->type = res.type; 1389 frn->scope = res.scope; 1390 } 1391 local_bh_enable(); 1392 } 1393 1394 rcu_read_unlock(); 1395 } 1396 1397 static void nl_fib_input(struct sk_buff *skb) 1398 { 1399 struct net *net; 1400 struct fib_result_nl *frn; 1401 struct nlmsghdr *nlh; 1402 u32 portid; 1403 1404 net = sock_net(skb->sk); 1405 nlh = nlmsg_hdr(skb); 1406 if (skb->len < nlmsg_total_size(sizeof(*frn)) || 1407 skb->len < nlh->nlmsg_len || 1408 nlmsg_len(nlh) < sizeof(*frn)) 1409 return; 1410 1411 skb = netlink_skb_clone(skb, GFP_KERNEL); 1412 if (!skb) 1413 return; 1414 nlh = nlmsg_hdr(skb); 1415 1416 frn = nlmsg_data(nlh); 1417 nl_fib_lookup(net, frn); 1418 1419 portid = NETLINK_CB(skb).portid; /* netlink portid */ 1420 NETLINK_CB(skb).portid = 0; /* from kernel */ 1421 NETLINK_CB(skb).dst_group = 0; /* unicast */ 1422 nlmsg_unicast(net->ipv4.fibnl, skb, portid); 1423 } 1424 1425 static int __net_init nl_fib_lookup_init(struct net *net) 1426 { 1427 struct sock *sk; 1428 struct netlink_kernel_cfg cfg = { 1429 .input = nl_fib_input, 1430 }; 1431 1432 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg); 1433 if (!sk) 1434 return -EAFNOSUPPORT; 1435 net->ipv4.fibnl = sk; 1436 return 0; 1437 } 1438 1439 static void nl_fib_lookup_exit(struct net *net) 1440 { 1441 netlink_kernel_release(net->ipv4.fibnl); 1442 net->ipv4.fibnl = NULL; 1443 } 1444 1445 static void fib_disable_ip(struct net_device *dev, unsigned long event, 1446 bool force) 1447 { 1448 if (fib_sync_down_dev(dev, event, force)) 1449 fib_flush(dev_net(dev)); 1450 else 1451 rt_cache_flush(dev_net(dev)); 1452 arp_ifdown(dev); 1453 } 1454 1455 static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr) 1456 { 1457 struct in_ifaddr *ifa = ptr; 1458 struct net_device *dev = ifa->ifa_dev->dev; 1459 struct net *net = dev_net(dev); 1460 1461 switch (event) { 1462 case NETDEV_UP: 1463 fib_add_ifaddr(ifa); 1464 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1465 fib_sync_up(dev, RTNH_F_DEAD); 1466 #endif 1467 atomic_inc(&net->ipv4.dev_addr_genid); 1468 rt_cache_flush(net); 1469 break; 1470 case NETDEV_DOWN: 1471 fib_del_ifaddr(ifa, NULL); 1472 atomic_inc(&net->ipv4.dev_addr_genid); 1473 if (!ifa->ifa_dev->ifa_list) { 1474 /* Last address was deleted from this interface. 1475 * Disable IP. 1476 */ 1477 fib_disable_ip(dev, event, true); 1478 } else { 1479 rt_cache_flush(net); 1480 } 1481 break; 1482 } 1483 return NOTIFY_DONE; 1484 } 1485 1486 static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) 1487 { 1488 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1489 struct netdev_notifier_changeupper_info *upper_info = ptr; 1490 struct netdev_notifier_info_ext *info_ext = ptr; 1491 struct in_device *in_dev; 1492 struct net *net = dev_net(dev); 1493 struct in_ifaddr *ifa; 1494 unsigned int flags; 1495 1496 if (event == NETDEV_UNREGISTER) { 1497 fib_disable_ip(dev, event, true); 1498 rt_flush_dev(dev); 1499 return NOTIFY_DONE; 1500 } 1501 1502 in_dev = __in_dev_get_rtnl(dev); 1503 if (!in_dev) 1504 return NOTIFY_DONE; 1505 1506 switch (event) { 1507 case NETDEV_UP: 1508 in_dev_for_each_ifa_rtnl(ifa, in_dev) { 1509 fib_add_ifaddr(ifa); 1510 } 1511 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1512 fib_sync_up(dev, RTNH_F_DEAD); 1513 #endif 1514 atomic_inc(&net->ipv4.dev_addr_genid); 1515 rt_cache_flush(net); 1516 break; 1517 case NETDEV_DOWN: 1518 fib_disable_ip(dev, event, false); 1519 break; 1520 case NETDEV_CHANGE: 1521 flags = netif_get_flags(dev); 1522 if (flags & (IFF_RUNNING | IFF_LOWER_UP)) 1523 fib_sync_up(dev, RTNH_F_LINKDOWN); 1524 else 1525 fib_sync_down_dev(dev, event, false); 1526 rt_cache_flush(net); 1527 break; 1528 case NETDEV_CHANGEMTU: 1529 fib_sync_mtu(dev, info_ext->ext.mtu); 1530 rt_cache_flush(net); 1531 break; 1532 case NETDEV_CHANGEUPPER: 1533 upper_info = ptr; 1534 /* flush all routes if dev is linked to or unlinked from 1535 * an L3 master device (e.g., VRF) 1536 */ 1537 if (upper_info->upper_dev && 1538 netif_is_l3_master(upper_info->upper_dev)) 1539 fib_disable_ip(dev, NETDEV_DOWN, true); 1540 break; 1541 } 1542 return NOTIFY_DONE; 1543 } 1544 1545 static struct notifier_block fib_inetaddr_notifier = { 1546 .notifier_call = fib_inetaddr_event, 1547 }; 1548 1549 static struct notifier_block fib_netdev_notifier = { 1550 .notifier_call = fib_netdev_event, 1551 }; 1552 1553 static int __net_init ip_fib_net_init(struct net *net) 1554 { 1555 int err; 1556 size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ; 1557 1558 err = fib4_notifier_init(net); 1559 if (err) 1560 return err; 1561 1562 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1563 /* Default to 3-tuple */ 1564 net->ipv4.sysctl_fib_multipath_hash_fields = 1565 FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK; 1566 #endif 1567 1568 /* Avoid false sharing : Use at least a full cache line */ 1569 size = max_t(size_t, size, L1_CACHE_BYTES); 1570 1571 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL); 1572 if (!net->ipv4.fib_table_hash) { 1573 err = -ENOMEM; 1574 goto err_table_hash_alloc; 1575 } 1576 1577 err = fib4_rules_init(net); 1578 if (err < 0) 1579 goto err_rules_init; 1580 return 0; 1581 1582 err_rules_init: 1583 kfree(net->ipv4.fib_table_hash); 1584 err_table_hash_alloc: 1585 fib4_notifier_exit(net); 1586 return err; 1587 } 1588 1589 static void ip_fib_net_exit(struct net *net) 1590 { 1591 int i; 1592 1593 ASSERT_RTNL_NET(net); 1594 #ifdef CONFIG_IP_MULTIPLE_TABLES 1595 RCU_INIT_POINTER(net->ipv4.fib_main, NULL); 1596 RCU_INIT_POINTER(net->ipv4.fib_default, NULL); 1597 #endif 1598 /* Destroy the tables in reverse order to guarantee that the 1599 * local table, ID 255, is destroyed before the main table, ID 1600 * 254. This is necessary as the local table may contain 1601 * references to data contained in the main table. 1602 */ 1603 for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) { 1604 struct hlist_head *head = &net->ipv4.fib_table_hash[i]; 1605 struct hlist_node *tmp; 1606 struct fib_table *tb; 1607 1608 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) { 1609 hlist_del(&tb->tb_hlist); 1610 fib_table_flush(net, tb, true); 1611 fib_free_table(tb); 1612 } 1613 } 1614 1615 #ifdef CONFIG_IP_MULTIPLE_TABLES 1616 fib4_rules_exit(net); 1617 #endif 1618 1619 kfree(net->ipv4.fib_table_hash); 1620 fib4_notifier_exit(net); 1621 } 1622 1623 static int __net_init fib_net_init(struct net *net) 1624 { 1625 int error; 1626 1627 #ifdef CONFIG_IP_ROUTE_CLASSID 1628 atomic_set(&net->ipv4.fib_num_tclassid_users, 0); 1629 #endif 1630 error = ip_fib_net_init(net); 1631 if (error < 0) 1632 goto out; 1633 1634 error = fib4_semantics_init(net); 1635 if (error) 1636 goto out_semantics; 1637 1638 error = nl_fib_lookup_init(net); 1639 if (error < 0) 1640 goto out_nlfl; 1641 1642 error = fib_proc_init(net); 1643 if (error < 0) 1644 goto out_proc; 1645 out: 1646 return error; 1647 1648 out_proc: 1649 nl_fib_lookup_exit(net); 1650 out_nlfl: 1651 fib4_semantics_exit(net); 1652 out_semantics: 1653 rtnl_net_lock(net); 1654 ip_fib_net_exit(net); 1655 rtnl_net_unlock(net); 1656 goto out; 1657 } 1658 1659 static void __net_exit fib_net_exit(struct net *net) 1660 { 1661 fib_proc_exit(net); 1662 nl_fib_lookup_exit(net); 1663 } 1664 1665 static void __net_exit fib_net_exit_batch(struct list_head *net_list) 1666 { 1667 struct net *net; 1668 1669 rtnl_lock(); 1670 list_for_each_entry(net, net_list, exit_list) { 1671 __rtnl_net_lock(net); 1672 ip_fib_net_exit(net); 1673 __rtnl_net_unlock(net); 1674 } 1675 rtnl_unlock(); 1676 1677 list_for_each_entry(net, net_list, exit_list) 1678 fib4_semantics_exit(net); 1679 } 1680 1681 static struct pernet_operations fib_net_ops = { 1682 .init = fib_net_init, 1683 .exit = fib_net_exit, 1684 .exit_batch = fib_net_exit_batch, 1685 }; 1686 1687 static const struct rtnl_msg_handler fib_rtnl_msg_handlers[] __initconst = { 1688 {.protocol = PF_INET, .msgtype = RTM_NEWROUTE, 1689 .doit = inet_rtm_newroute, .flags = RTNL_FLAG_DOIT_PERNET}, 1690 {.protocol = PF_INET, .msgtype = RTM_DELROUTE, 1691 .doit = inet_rtm_delroute, .flags = RTNL_FLAG_DOIT_PERNET}, 1692 {.protocol = PF_INET, .msgtype = RTM_GETROUTE, .dumpit = inet_dump_fib, 1693 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE}, 1694 }; 1695 1696 void __init ip_fib_init(void) 1697 { 1698 fib_trie_init(); 1699 1700 register_pernet_subsys(&fib_net_ops); 1701 1702 register_netdevice_notifier(&fib_netdev_notifier); 1703 register_inetaddr_notifier(&fib_inetaddr_notifier); 1704 1705 rtnl_register_many(fib_rtnl_msg_handlers); 1706 } 1707