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