LCOV - code coverage report
Current view: top level - net/ipv6 - route.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 619 2352 26.3 %
Date: 2020-09-30 20:25:40 Functions: 61 185 33.0 %
Branches: 266 2351 11.3 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  *      Linux INET6 implementation
       4                 :            :  *      FIB front-end.
       5                 :            :  *
       6                 :            :  *      Authors:
       7                 :            :  *      Pedro Roque             <roque@di.fc.ul.pt>
       8                 :            :  */
       9                 :            : 
      10                 :            : /*      Changes:
      11                 :            :  *
      12                 :            :  *      YOSHIFUJI Hideaki @USAGI
      13                 :            :  *              reworked default router selection.
      14                 :            :  *              - respect outgoing interface
      15                 :            :  *              - select from (probably) reachable routers (i.e.
      16                 :            :  *              routers in REACHABLE, STALE, DELAY or PROBE states).
      17                 :            :  *              - always select the same router if it is (probably)
      18                 :            :  *              reachable.  otherwise, round-robin the list.
      19                 :            :  *      Ville Nuorvala
      20                 :            :  *              Fixed routing subtrees.
      21                 :            :  */
      22                 :            : 
      23                 :            : #define pr_fmt(fmt) "IPv6: " fmt
      24                 :            : 
      25                 :            : #include <linux/capability.h>
      26                 :            : #include <linux/errno.h>
      27                 :            : #include <linux/export.h>
      28                 :            : #include <linux/types.h>
      29                 :            : #include <linux/times.h>
      30                 :            : #include <linux/socket.h>
      31                 :            : #include <linux/sockios.h>
      32                 :            : #include <linux/net.h>
      33                 :            : #include <linux/route.h>
      34                 :            : #include <linux/netdevice.h>
      35                 :            : #include <linux/in6.h>
      36                 :            : #include <linux/mroute6.h>
      37                 :            : #include <linux/init.h>
      38                 :            : #include <linux/if_arp.h>
      39                 :            : #include <linux/proc_fs.h>
      40                 :            : #include <linux/seq_file.h>
      41                 :            : #include <linux/nsproxy.h>
      42                 :            : #include <linux/slab.h>
      43                 :            : #include <linux/jhash.h>
      44                 :            : #include <net/net_namespace.h>
      45                 :            : #include <net/snmp.h>
      46                 :            : #include <net/ipv6.h>
      47                 :            : #include <net/ip6_fib.h>
      48                 :            : #include <net/ip6_route.h>
      49                 :            : #include <net/ndisc.h>
      50                 :            : #include <net/addrconf.h>
      51                 :            : #include <net/tcp.h>
      52                 :            : #include <linux/rtnetlink.h>
      53                 :            : #include <net/dst.h>
      54                 :            : #include <net/dst_metadata.h>
      55                 :            : #include <net/xfrm.h>
      56                 :            : #include <net/netevent.h>
      57                 :            : #include <net/netlink.h>
      58                 :            : #include <net/rtnh.h>
      59                 :            : #include <net/lwtunnel.h>
      60                 :            : #include <net/ip_tunnels.h>
      61                 :            : #include <net/l3mdev.h>
      62                 :            : #include <net/ip.h>
      63                 :            : #include <linux/uaccess.h>
      64                 :            : 
      65                 :            : #ifdef CONFIG_SYSCTL
      66                 :            : #include <linux/sysctl.h>
      67                 :            : #endif
      68                 :            : 
      69                 :            : static int ip6_rt_type_to_error(u8 fib6_type);
      70                 :            : 
      71                 :            : #define CREATE_TRACE_POINTS
      72                 :            : #include <trace/events/fib6.h>
      73                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
      74                 :            : #undef CREATE_TRACE_POINTS
      75                 :            : 
      76                 :            : enum rt6_nud_state {
      77                 :            :         RT6_NUD_FAIL_HARD = -3,
      78                 :            :         RT6_NUD_FAIL_PROBE = -2,
      79                 :            :         RT6_NUD_FAIL_DO_RR = -1,
      80                 :            :         RT6_NUD_SUCCEED = 1
      81                 :            : };
      82                 :            : 
      83                 :            : static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
      84                 :            : static unsigned int      ip6_default_advmss(const struct dst_entry *dst);
      85                 :            : static unsigned int      ip6_mtu(const struct dst_entry *dst);
      86                 :            : static struct dst_entry *ip6_negative_advice(struct dst_entry *);
      87                 :            : static void             ip6_dst_destroy(struct dst_entry *);
      88                 :            : static void             ip6_dst_ifdown(struct dst_entry *,
      89                 :            :                                        struct net_device *dev, int how);
      90                 :            : static int               ip6_dst_gc(struct dst_ops *ops);
      91                 :            : 
      92                 :            : static int              ip6_pkt_discard(struct sk_buff *skb);
      93                 :            : static int              ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
      94                 :            : static int              ip6_pkt_prohibit(struct sk_buff *skb);
      95                 :            : static int              ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
      96                 :            : static void             ip6_link_failure(struct sk_buff *skb);
      97                 :            : static void             ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
      98                 :            :                                            struct sk_buff *skb, u32 mtu,
      99                 :            :                                            bool confirm_neigh);
     100                 :            : static void             rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
     101                 :            :                                         struct sk_buff *skb);
     102                 :            : static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
     103                 :            :                            int strict);
     104                 :            : static size_t rt6_nlmsg_size(struct fib6_info *f6i);
     105                 :            : static int rt6_fill_node(struct net *net, struct sk_buff *skb,
     106                 :            :                          struct fib6_info *rt, struct dst_entry *dst,
     107                 :            :                          struct in6_addr *dest, struct in6_addr *src,
     108                 :            :                          int iif, int type, u32 portid, u32 seq,
     109                 :            :                          unsigned int flags);
     110                 :            : static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
     111                 :            :                                            const struct in6_addr *daddr,
     112                 :            :                                            const struct in6_addr *saddr);
     113                 :            : 
     114                 :            : #ifdef CONFIG_IPV6_ROUTE_INFO
     115                 :            : static struct fib6_info *rt6_add_route_info(struct net *net,
     116                 :            :                                            const struct in6_addr *prefix, int prefixlen,
     117                 :            :                                            const struct in6_addr *gwaddr,
     118                 :            :                                            struct net_device *dev,
     119                 :            :                                            unsigned int pref);
     120                 :            : static struct fib6_info *rt6_get_route_info(struct net *net,
     121                 :            :                                            const struct in6_addr *prefix, int prefixlen,
     122                 :            :                                            const struct in6_addr *gwaddr,
     123                 :            :                                            struct net_device *dev);
     124                 :            : #endif
     125                 :            : 
     126                 :            : struct uncached_list {
     127                 :            :         spinlock_t              lock;
     128                 :            :         struct list_head        head;
     129                 :            : };
     130                 :            : 
     131                 :            : static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
     132                 :            : 
     133                 :       2001 : void rt6_uncached_list_add(struct rt6_info *rt)
     134                 :            : {
     135                 :       4002 :         struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
     136                 :            : 
     137                 :       2001 :         rt->rt6i_uncached_list = ul;
     138                 :            : 
     139                 :            :         spin_lock_bh(&ul->lock);
     140                 :       2001 :         list_add_tail(&rt->rt6i_uncached, &ul->head);
     141                 :            :         spin_unlock_bh(&ul->lock);
     142                 :       2001 : }
     143                 :            : 
     144                 :       2001 : void rt6_uncached_list_del(struct rt6_info *rt)
     145                 :            : {
     146         [ +  - ]:       4002 :         if (!list_empty(&rt->rt6i_uncached)) {
     147                 :       2001 :                 struct uncached_list *ul = rt->rt6i_uncached_list;
     148                 :       2001 :                 struct net *net = dev_net(rt->dst.dev);
     149                 :            : 
     150                 :            :                 spin_lock_bh(&ul->lock);
     151                 :            :                 list_del(&rt->rt6i_uncached);
     152                 :       2001 :                 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
     153                 :            :                 spin_unlock_bh(&ul->lock);
     154                 :            :         }
     155                 :       2001 : }
     156                 :            : 
     157                 :          0 : static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
     158                 :            : {
     159                 :          0 :         struct net_device *loopback_dev = net->loopback_dev;
     160                 :            :         int cpu;
     161                 :            : 
     162         [ #  # ]:          0 :         if (dev == loopback_dev)
     163                 :          0 :                 return;
     164                 :            : 
     165         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
     166                 :          0 :                 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
     167                 :            :                 struct rt6_info *rt;
     168                 :            : 
     169                 :            :                 spin_lock_bh(&ul->lock);
     170         [ #  # ]:          0 :                 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
     171                 :          0 :                         struct inet6_dev *rt_idev = rt->rt6i_idev;
     172                 :          0 :                         struct net_device *rt_dev = rt->dst.dev;
     173                 :            : 
     174         [ #  # ]:          0 :                         if (rt_idev->dev == dev) {
     175                 :          0 :                                 rt->rt6i_idev = in6_dev_get(loopback_dev);
     176                 :          0 :                                 in6_dev_put(rt_idev);
     177                 :            :                         }
     178                 :            : 
     179         [ #  # ]:          0 :                         if (rt_dev == dev) {
     180                 :          0 :                                 rt->dst.dev = blackhole_netdev;
     181                 :          0 :                                 dev_hold(rt->dst.dev);
     182                 :          0 :                                 dev_put(rt_dev);
     183                 :            :                         }
     184                 :            :                 }
     185                 :            :                 spin_unlock_bh(&ul->lock);
     186                 :            :         }
     187                 :            : }
     188                 :            : 
     189                 :            : static inline const void *choose_neigh_daddr(const struct in6_addr *p,
     190                 :            :                                              struct sk_buff *skb,
     191                 :            :                                              const void *daddr)
     192                 :            : {
     193   [ #  #  #  # ]:          0 :         if (!ipv6_addr_any(p))
     194                 :            :                 return (const void *) p;
     195         [ #  # ]:          0 :         else if (skb)
     196                 :          0 :                 return &ipv6_hdr(skb)->daddr;
     197                 :            :         return daddr;
     198                 :            : }
     199                 :            : 
     200                 :          0 : struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
     201                 :            :                                    struct net_device *dev,
     202                 :            :                                    struct sk_buff *skb,
     203                 :            :                                    const void *daddr)
     204                 :            : {
     205                 :            :         struct neighbour *n;
     206                 :            : 
     207                 :            :         daddr = choose_neigh_daddr(gw, skb, daddr);
     208                 :          0 :         n = __ipv6_neigh_lookup(dev, daddr);
     209         [ #  # ]:          0 :         if (n)
     210                 :            :                 return n;
     211                 :            : 
     212                 :            :         n = neigh_create(&nd_tbl, daddr, dev);
     213         [ #  # ]:          0 :         return IS_ERR(n) ? NULL : n;
     214                 :            : }
     215                 :            : 
     216                 :          0 : static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
     217                 :            :                                               struct sk_buff *skb,
     218                 :            :                                               const void *daddr)
     219                 :            : {
     220                 :            :         const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
     221                 :            : 
     222                 :          0 :         return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
     223                 :            :                                 dst->dev, skb, daddr);
     224                 :            : }
     225                 :            : 
     226                 :          0 : static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
     227                 :            : {
     228                 :          0 :         struct net_device *dev = dst->dev;
     229                 :            :         struct rt6_info *rt = (struct rt6_info *)dst;
     230                 :            : 
     231                 :            :         daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
     232         [ #  # ]:          0 :         if (!daddr)
     233                 :            :                 return;
     234         [ #  # ]:          0 :         if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
     235                 :            :                 return;
     236         [ #  # ]:          0 :         if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
     237                 :            :                 return;
     238                 :          0 :         __ipv6_confirm_neigh(dev, daddr);
     239                 :            : }
     240                 :            : 
     241                 :            : static struct dst_ops ip6_dst_ops_template = {
     242                 :            :         .family                 =       AF_INET6,
     243                 :            :         .gc                     =       ip6_dst_gc,
     244                 :            :         .gc_thresh              =       1024,
     245                 :            :         .check                  =       ip6_dst_check,
     246                 :            :         .default_advmss         =       ip6_default_advmss,
     247                 :            :         .mtu                    =       ip6_mtu,
     248                 :            :         .cow_metrics            =       dst_cow_metrics_generic,
     249                 :            :         .destroy                =       ip6_dst_destroy,
     250                 :            :         .ifdown                 =       ip6_dst_ifdown,
     251                 :            :         .negative_advice        =       ip6_negative_advice,
     252                 :            :         .link_failure           =       ip6_link_failure,
     253                 :            :         .update_pmtu            =       ip6_rt_update_pmtu,
     254                 :            :         .redirect               =       rt6_do_redirect,
     255                 :            :         .local_out              =       __ip6_local_out,
     256                 :            :         .neigh_lookup           =       ip6_dst_neigh_lookup,
     257                 :            :         .confirm_neigh          =       ip6_confirm_neigh,
     258                 :            : };
     259                 :            : 
     260                 :          0 : static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
     261                 :            : {
     262                 :            :         unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
     263                 :            : 
     264         [ #  # ]:          0 :         return mtu ? : dst->dev->mtu;
     265                 :            : }
     266                 :            : 
     267                 :          0 : static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
     268                 :            :                                          struct sk_buff *skb, u32 mtu,
     269                 :            :                                          bool confirm_neigh)
     270                 :            : {
     271                 :          0 : }
     272                 :            : 
     273                 :          0 : static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
     274                 :            :                                       struct sk_buff *skb)
     275                 :            : {
     276                 :          0 : }
     277                 :            : 
     278                 :            : static struct dst_ops ip6_dst_blackhole_ops = {
     279                 :            :         .family                 =       AF_INET6,
     280                 :            :         .destroy                =       ip6_dst_destroy,
     281                 :            :         .check                  =       ip6_dst_check,
     282                 :            :         .mtu                    =       ip6_blackhole_mtu,
     283                 :            :         .default_advmss         =       ip6_default_advmss,
     284                 :            :         .update_pmtu            =       ip6_rt_blackhole_update_pmtu,
     285                 :            :         .redirect               =       ip6_rt_blackhole_redirect,
     286                 :            :         .cow_metrics            =       dst_cow_metrics_generic,
     287                 :            :         .neigh_lookup           =       ip6_dst_neigh_lookup,
     288                 :            : };
     289                 :            : 
     290                 :            : static const u32 ip6_template_metrics[RTAX_MAX] = {
     291                 :            :         [RTAX_HOPLIMIT - 1] = 0,
     292                 :            : };
     293                 :            : 
     294                 :            : static const struct fib6_info fib6_null_entry_template = {
     295                 :            :         .fib6_flags     = (RTF_REJECT | RTF_NONEXTHOP),
     296                 :            :         .fib6_protocol  = RTPROT_KERNEL,
     297                 :            :         .fib6_metric    = ~(u32)0,
     298                 :            :         .fib6_ref       = REFCOUNT_INIT(1),
     299                 :            :         .fib6_type      = RTN_UNREACHABLE,
     300                 :            :         .fib6_metrics   = (struct dst_metrics *)&dst_default_metrics,
     301                 :            : };
     302                 :            : 
     303                 :            : static const struct rt6_info ip6_null_entry_template = {
     304                 :            :         .dst = {
     305                 :            :                 .__refcnt       = ATOMIC_INIT(1),
     306                 :            :                 .__use          = 1,
     307                 :            :                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
     308                 :            :                 .error          = -ENETUNREACH,
     309                 :            :                 .input          = ip6_pkt_discard,
     310                 :            :                 .output         = ip6_pkt_discard_out,
     311                 :            :         },
     312                 :            :         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
     313                 :            : };
     314                 :            : 
     315                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
     316                 :            : 
     317                 :            : static const struct rt6_info ip6_prohibit_entry_template = {
     318                 :            :         .dst = {
     319                 :            :                 .__refcnt       = ATOMIC_INIT(1),
     320                 :            :                 .__use          = 1,
     321                 :            :                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
     322                 :            :                 .error          = -EACCES,
     323                 :            :                 .input          = ip6_pkt_prohibit,
     324                 :            :                 .output         = ip6_pkt_prohibit_out,
     325                 :            :         },
     326                 :            :         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
     327                 :            : };
     328                 :            : 
     329                 :            : static const struct rt6_info ip6_blk_hole_entry_template = {
     330                 :            :         .dst = {
     331                 :            :                 .__refcnt       = ATOMIC_INIT(1),
     332                 :            :                 .__use          = 1,
     333                 :            :                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
     334                 :            :                 .error          = -EINVAL,
     335                 :            :                 .input          = dst_discard,
     336                 :            :                 .output         = dst_discard_out,
     337                 :            :         },
     338                 :            :         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
     339                 :            : };
     340                 :            : 
     341                 :            : #endif
     342                 :            : 
     343                 :            : static void rt6_info_init(struct rt6_info *rt)
     344                 :            : {
     345                 :            :         struct dst_entry *dst = &rt->dst;
     346                 :            : 
     347                 :       2511 :         memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
     348                 :       2511 :         INIT_LIST_HEAD(&rt->rt6i_uncached);
     349                 :            : }
     350                 :            : 
     351                 :            : /* allocate dst with ip6_dst_ops */
     352                 :       2511 : struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
     353                 :            :                                int flags)
     354                 :            : {
     355                 :       2511 :         struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
     356                 :            :                                         1, DST_OBSOLETE_FORCE_CHK, flags);
     357                 :            : 
     358         [ +  - ]:       2511 :         if (rt) {
     359                 :            :                 rt6_info_init(rt);
     360                 :       2511 :                 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
     361                 :            :         }
     362                 :            : 
     363                 :       2511 :         return rt;
     364                 :            : }
     365                 :            : EXPORT_SYMBOL(ip6_dst_alloc);
     366                 :            : 
     367                 :       2001 : static void ip6_dst_destroy(struct dst_entry *dst)
     368                 :            : {
     369                 :            :         struct rt6_info *rt = (struct rt6_info *)dst;
     370                 :            :         struct fib6_info *from;
     371                 :            :         struct inet6_dev *idev;
     372                 :            : 
     373                 :       2001 :         ip_dst_metrics_put(dst);
     374                 :       2001 :         rt6_uncached_list_del(rt);
     375                 :            : 
     376                 :       2001 :         idev = rt->rt6i_idev;
     377         [ +  - ]:       2001 :         if (idev) {
     378                 :       2001 :                 rt->rt6i_idev = NULL;
     379                 :       2001 :                 in6_dev_put(idev);
     380                 :            :         }
     381                 :            : 
     382                 :       2001 :         from = xchg((__force struct fib6_info **)&rt->from, NULL);
     383                 :       2001 :         fib6_info_release(from);
     384                 :       2001 : }
     385                 :            : 
     386                 :          0 : static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
     387                 :            :                            int how)
     388                 :            : {
     389                 :            :         struct rt6_info *rt = (struct rt6_info *)dst;
     390                 :          0 :         struct inet6_dev *idev = rt->rt6i_idev;
     391                 :          0 :         struct net_device *loopback_dev =
     392                 :            :                 dev_net(dev)->loopback_dev;
     393                 :            : 
     394   [ #  #  #  # ]:          0 :         if (idev && idev->dev != loopback_dev) {
     395                 :            :                 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
     396         [ #  # ]:          0 :                 if (loopback_idev) {
     397                 :          0 :                         rt->rt6i_idev = loopback_idev;
     398                 :          0 :                         in6_dev_put(idev);
     399                 :            :                 }
     400                 :            :         }
     401                 :          0 : }
     402                 :            : 
     403                 :            : static bool __rt6_check_expired(const struct rt6_info *rt)
     404                 :            : {
     405         [ #  # ]:          0 :         if (rt->rt6i_flags & RTF_EXPIRES)
     406         [ #  # ]:          0 :                 return time_after(jiffies, rt->dst.expires);
     407                 :            :         else
     408                 :            :                 return false;
     409                 :            : }
     410                 :            : 
     411                 :          0 : static bool rt6_check_expired(const struct rt6_info *rt)
     412                 :            : {
     413                 :            :         struct fib6_info *from;
     414                 :            : 
     415                 :          0 :         from = rcu_dereference(rt->from);
     416                 :            : 
     417         [ #  # ]:          0 :         if (rt->rt6i_flags & RTF_EXPIRES) {
     418         [ #  # ]:          0 :                 if (time_after(jiffies, rt->dst.expires))
     419                 :            :                         return true;
     420         [ #  # ]:          0 :         } else if (from) {
     421   [ #  #  #  # ]:          0 :                 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
     422                 :            :                         fib6_check_expired(from);
     423                 :            :         }
     424                 :            :         return false;
     425                 :            : }
     426                 :            : 
     427                 :       1780 : void fib6_select_path(const struct net *net, struct fib6_result *res,
     428                 :            :                       struct flowi6 *fl6, int oif, bool have_oif_match,
     429                 :            :                       const struct sk_buff *skb, int strict)
     430                 :            : {
     431                 :            :         struct fib6_info *sibling, *next_sibling;
     432                 :       1780 :         struct fib6_info *match = res->f6i;
     433                 :            : 
     434   [ +  -  -  +  :       1780 :         if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
                   #  # ]
     435                 :            :                 goto out;
     436                 :            : 
     437   [ #  #  #  #  :          0 :         if (match->nh && have_oif_match && res->nh)
                   #  # ]
     438                 :            :                 return;
     439                 :            : 
     440                 :            :         /* We might have already computed the hash for ICMPv6 errors. In such
     441                 :            :          * case it will always be non-zero. Otherwise now is the time to do it.
     442                 :            :          */
     443   [ #  #  #  # ]:          0 :         if (!fl6->mp_hash &&
     444         [ #  # ]:          0 :             (!match->nh || nexthop_is_multipath(match->nh)))
     445                 :          0 :                 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
     446                 :            : 
     447         [ #  # ]:          0 :         if (unlikely(match->nh)) {
     448                 :          0 :                 nexthop_path_fib6_result(res, fl6->mp_hash);
     449                 :          0 :                 return;
     450                 :            :         }
     451                 :            : 
     452         [ #  # ]:          0 :         if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
     453                 :            :                 goto out;
     454                 :            : 
     455         [ #  # ]:          0 :         list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
     456                 :            :                                  fib6_siblings) {
     457                 :          0 :                 const struct fib6_nh *nh = sibling->fib6_nh;
     458                 :            :                 int nh_upper_bound;
     459                 :            : 
     460                 :            :                 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
     461         [ #  # ]:          0 :                 if (fl6->mp_hash > nh_upper_bound)
     462                 :          0 :                         continue;
     463         [ #  # ]:          0 :                 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
     464                 :            :                         break;
     465                 :          0 :                 match = sibling;
     466                 :          0 :                 break;
     467                 :            :         }
     468                 :            : 
     469                 :            : out:
     470                 :       1780 :         res->f6i = match;
     471                 :       1780 :         res->nh = match->fib6_nh;
     472                 :            : }
     473                 :            : 
     474                 :            : /*
     475                 :            :  *      Route lookup. rcu_read_lock() should be held.
     476                 :            :  */
     477                 :            : 
     478                 :          0 : static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
     479                 :            :                                const struct in6_addr *saddr, int oif, int flags)
     480                 :            : {
     481                 :            :         const struct net_device *dev;
     482                 :            : 
     483         [ #  # ]:          0 :         if (nh->fib_nh_flags & RTNH_F_DEAD)
     484                 :            :                 return false;
     485                 :            : 
     486                 :          0 :         dev = nh->fib_nh_dev;
     487         [ #  # ]:          0 :         if (oif) {
     488         [ #  # ]:          0 :                 if (dev->ifindex == oif)
     489                 :            :                         return true;
     490                 :            :         } else {
     491         [ #  # ]:          0 :                 if (ipv6_chk_addr(net, saddr, dev,
     492                 :            :                                   flags & RT6_LOOKUP_F_IFACE))
     493                 :            :                         return true;
     494                 :            :         }
     495                 :            : 
     496                 :            :         return false;
     497                 :            : }
     498                 :            : 
     499                 :            : struct fib6_nh_dm_arg {
     500                 :            :         struct net              *net;
     501                 :            :         const struct in6_addr   *saddr;
     502                 :            :         int                     oif;
     503                 :            :         int                     flags;
     504                 :            :         struct fib6_nh          *nh;
     505                 :            : };
     506                 :            : 
     507                 :          0 : static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
     508                 :            : {
     509                 :            :         struct fib6_nh_dm_arg *arg = _arg;
     510                 :            : 
     511                 :          0 :         arg->nh = nh;
     512                 :          0 :         return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
     513                 :            :                                   arg->flags);
     514                 :            : }
     515                 :            : 
     516                 :            : /* returns fib6_nh from nexthop or NULL */
     517                 :          0 : static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
     518                 :            :                                         struct fib6_result *res,
     519                 :            :                                         const struct in6_addr *saddr,
     520                 :            :                                         int oif, int flags)
     521                 :            : {
     522                 :          0 :         struct fib6_nh_dm_arg arg = {
     523                 :            :                 .net   = net,
     524                 :            :                 .saddr = saddr,
     525                 :            :                 .oif   = oif,
     526                 :            :                 .flags = flags,
     527                 :            :         };
     528                 :            : 
     529         [ #  # ]:          0 :         if (nexthop_is_blackhole(nh))
     530                 :            :                 return NULL;
     531                 :            : 
     532         [ #  # ]:          0 :         if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
     533                 :          0 :                 return arg.nh;
     534                 :            : 
     535                 :            :         return NULL;
     536                 :            : }
     537                 :            : 
     538                 :          0 : static void rt6_device_match(struct net *net, struct fib6_result *res,
     539                 :            :                              const struct in6_addr *saddr, int oif, int flags)
     540                 :            : {
     541                 :          0 :         struct fib6_info *f6i = res->f6i;
     542                 :            :         struct fib6_info *spf6i;
     543                 :            :         struct fib6_nh *nh;
     544                 :            : 
     545   [ #  #  #  # ]:          0 :         if (!oif && ipv6_addr_any(saddr)) {
     546         [ #  # ]:          0 :                 if (unlikely(f6i->nh)) {
     547                 :            :                         nh = nexthop_fib6_nh(f6i->nh);
     548         [ #  # ]:          0 :                         if (nexthop_is_blackhole(f6i->nh))
     549                 :            :                                 goto out_blackhole;
     550                 :            :                 } else {
     551                 :          0 :                         nh = f6i->fib6_nh;
     552                 :            :                 }
     553         [ #  # ]:          0 :                 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
     554                 :            :                         goto out;
     555                 :            :         }
     556                 :            : 
     557         [ #  # ]:          0 :         for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
     558                 :            :                 bool matched = false;
     559                 :            : 
     560         [ #  # ]:          0 :                 if (unlikely(spf6i->nh)) {
     561                 :          0 :                         nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
     562                 :            :                                               oif, flags);
     563         [ #  # ]:          0 :                         if (nh)
     564                 :            :                                 matched = true;
     565                 :            :                 } else {
     566                 :          0 :                         nh = spf6i->fib6_nh;
     567         [ #  # ]:          0 :                         if (__rt6_device_match(net, nh, saddr, oif, flags))
     568                 :            :                                 matched = true;
     569                 :            :                 }
     570         [ #  # ]:          0 :                 if (matched) {
     571                 :          0 :                         res->f6i = spf6i;
     572                 :          0 :                         goto out;
     573                 :            :                 }
     574                 :            :         }
     575                 :            : 
     576   [ #  #  #  # ]:          0 :         if (oif && flags & RT6_LOOKUP_F_IFACE) {
     577                 :          0 :                 res->f6i = net->ipv6.fib6_null_entry;
     578                 :          0 :                 nh = res->f6i->fib6_nh;
     579                 :          0 :                 goto out;
     580                 :            :         }
     581                 :            : 
     582         [ #  # ]:          0 :         if (unlikely(f6i->nh)) {
     583                 :            :                 nh = nexthop_fib6_nh(f6i->nh);
     584         [ #  # ]:          0 :                 if (nexthop_is_blackhole(f6i->nh))
     585                 :            :                         goto out_blackhole;
     586                 :            :         } else {
     587                 :          0 :                 nh = f6i->fib6_nh;
     588                 :            :         }
     589                 :            : 
     590         [ #  # ]:          0 :         if (nh->fib_nh_flags & RTNH_F_DEAD) {
     591                 :          0 :                 res->f6i = net->ipv6.fib6_null_entry;
     592                 :          0 :                 nh = res->f6i->fib6_nh;
     593                 :            :         }
     594                 :            : out:
     595                 :          0 :         res->nh = nh;
     596                 :          0 :         res->fib6_type = res->f6i->fib6_type;
     597                 :          0 :         res->fib6_flags = res->f6i->fib6_flags;
     598                 :          0 :         return;
     599                 :            : 
     600                 :            : out_blackhole:
     601                 :          0 :         res->fib6_flags |= RTF_REJECT;
     602                 :          0 :         res->fib6_type = RTN_BLACKHOLE;
     603                 :          0 :         res->nh = nh;
     604                 :            : }
     605                 :            : 
     606                 :            : #ifdef CONFIG_IPV6_ROUTER_PREF
     607                 :            : struct __rt6_probe_work {
     608                 :            :         struct work_struct work;
     609                 :            :         struct in6_addr target;
     610                 :            :         struct net_device *dev;
     611                 :            : };
     612                 :            : 
     613                 :          0 : static void rt6_probe_deferred(struct work_struct *w)
     614                 :            : {
     615                 :            :         struct in6_addr mcaddr;
     616                 :            :         struct __rt6_probe_work *work =
     617                 :            :                 container_of(w, struct __rt6_probe_work, work);
     618                 :            : 
     619                 :            :         addrconf_addr_solict_mult(&work->target, &mcaddr);
     620                 :          0 :         ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
     621                 :          0 :         dev_put(work->dev);
     622                 :          0 :         kfree(work);
     623                 :          0 : }
     624                 :            : 
     625                 :       1780 : static void rt6_probe(struct fib6_nh *fib6_nh)
     626                 :            : {
     627                 :            :         struct __rt6_probe_work *work = NULL;
     628                 :            :         const struct in6_addr *nh_gw;
     629                 :            :         unsigned long last_probe;
     630                 :            :         struct neighbour *neigh;
     631                 :            :         struct net_device *dev;
     632                 :            :         struct inet6_dev *idev;
     633                 :            : 
     634                 :            :         /*
     635                 :            :          * Okay, this does not seem to be appropriate
     636                 :            :          * for now, however, we need to check if it
     637                 :            :          * is really so; aka Router Reachability Probing.
     638                 :            :          *
     639                 :            :          * Router Reachability Probe MUST be rate-limited
     640                 :            :          * to no more than one per minute.
     641                 :            :          */
     642         [ -  + ]:       1780 :         if (!fib6_nh->fib_nh_gw_family)
     643                 :       1780 :                 return;
     644                 :            : 
     645                 :          0 :         nh_gw = &fib6_nh->fib_nh_gw6;
     646                 :          0 :         dev = fib6_nh->fib_nh_dev;
     647                 :            :         rcu_read_lock_bh();
     648                 :            :         last_probe = READ_ONCE(fib6_nh->last_probe);
     649                 :            :         idev = __in6_dev_get(dev);
     650                 :            :         neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
     651         [ #  # ]:          0 :         if (neigh) {
     652         [ #  # ]:          0 :                 if (neigh->nud_state & NUD_VALID)
     653                 :            :                         goto out;
     654                 :            : 
     655                 :          0 :                 write_lock(&neigh->lock);
     656         [ #  # ]:          0 :                 if (!(neigh->nud_state & NUD_VALID) &&
     657         [ #  # ]:          0 :                     time_after(jiffies,
     658                 :            :                                neigh->updated + idev->cnf.rtr_probe_interval)) {
     659                 :            :                         work = kmalloc(sizeof(*work), GFP_ATOMIC);
     660         [ #  # ]:          0 :                         if (work)
     661                 :          0 :                                 __neigh_set_probe_once(neigh);
     662                 :            :                 }
     663                 :            :                 write_unlock(&neigh->lock);
     664         [ #  # ]:          0 :         } else if (time_after(jiffies, last_probe +
     665                 :            :                                        idev->cnf.rtr_probe_interval)) {
     666                 :            :                 work = kmalloc(sizeof(*work), GFP_ATOMIC);
     667                 :            :         }
     668                 :            : 
     669   [ #  #  #  # ]:          0 :         if (!work || cmpxchg(&fib6_nh->last_probe,
     670                 :            :                              last_probe, jiffies) != last_probe) {
     671                 :          0 :                 kfree(work);
     672                 :            :         } else {
     673                 :          0 :                 INIT_WORK(&work->work, rt6_probe_deferred);
     674                 :          0 :                 work->target = *nh_gw;
     675                 :          0 :                 dev_hold(dev);
     676                 :          0 :                 work->dev = dev;
     677                 :          0 :                 schedule_work(&work->work);
     678                 :            :         }
     679                 :            : 
     680                 :            : out:
     681                 :            :         rcu_read_unlock_bh();
     682                 :            : }
     683                 :            : #else
     684                 :            : static inline void rt6_probe(struct fib6_nh *fib6_nh)
     685                 :            : {
     686                 :            : }
     687                 :            : #endif
     688                 :            : 
     689                 :            : /*
     690                 :            :  * Default Router Selection (RFC 2461 6.3.6)
     691                 :            :  */
     692                 :          0 : static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
     693                 :            : {
     694                 :            :         enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
     695                 :            :         struct neighbour *neigh;
     696                 :            : 
     697                 :            :         rcu_read_lock_bh();
     698                 :          0 :         neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
     699                 :          0 :                                           &fib6_nh->fib_nh_gw6);
     700         [ #  # ]:          0 :         if (neigh) {
     701                 :          0 :                 read_lock(&neigh->lock);
     702         [ #  # ]:          0 :                 if (neigh->nud_state & NUD_VALID)
     703                 :            :                         ret = RT6_NUD_SUCCEED;
     704                 :            : #ifdef CONFIG_IPV6_ROUTER_PREF
     705         [ #  # ]:          0 :                 else if (!(neigh->nud_state & NUD_FAILED))
     706                 :            :                         ret = RT6_NUD_SUCCEED;
     707                 :            :                 else
     708                 :            :                         ret = RT6_NUD_FAIL_PROBE;
     709                 :            : #endif
     710                 :            :                 read_unlock(&neigh->lock);
     711                 :            :         } else {
     712                 :            :                 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
     713                 :            :                       RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
     714                 :            :         }
     715                 :            :         rcu_read_unlock_bh();
     716                 :            : 
     717                 :          0 :         return ret;
     718                 :            : }
     719                 :            : 
     720                 :       1780 : static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
     721                 :            :                            int strict)
     722                 :            : {
     723                 :            :         int m = 0;
     724                 :            : 
     725   [ +  -  +  - ]:       1780 :         if (!oif || nh->fib_nh_dev->ifindex == oif)
     726                 :            :                 m = 2;
     727                 :            : 
     728   [ -  +  #  # ]:       1780 :         if (!m && (strict & RT6_LOOKUP_F_IFACE))
     729                 :            :                 return RT6_NUD_FAIL_HARD;
     730                 :            : #ifdef CONFIG_IPV6_ROUTER_PREF
     731                 :       1780 :         m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
     732                 :            : #endif
     733   [ +  -  +  - ]:       3560 :         if ((strict & RT6_LOOKUP_F_REACHABLE) &&
     734         [ -  + ]:       3560 :             !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
     735                 :          0 :                 int n = rt6_check_neigh(nh);
     736         [ #  # ]:          0 :                 if (n < 0)
     737                 :            :                         return n;
     738                 :            :         }
     739                 :       1780 :         return m;
     740                 :            : }
     741                 :            : 
     742                 :       1780 : static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
     743                 :            :                        int oif, int strict, int *mpri, bool *do_rr)
     744                 :            : {
     745                 :            :         bool match_do_rr = false;
     746                 :            :         bool rc = false;
     747                 :            :         int m;
     748                 :            : 
     749         [ +  - ]:       1780 :         if (nh->fib_nh_flags & RTNH_F_DEAD)
     750                 :            :                 goto out;
     751                 :            : 
     752   [ -  +  #  # ]:       3560 :         if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
     753         [ #  # ]:          0 :             nh->fib_nh_flags & RTNH_F_LINKDOWN &&
     754                 :          0 :             !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
     755                 :            :                 goto out;
     756                 :            : 
     757                 :       1780 :         m = rt6_score_route(nh, fib6_flags, oif, strict);
     758         [ +  - ]:       1780 :         if (m == RT6_NUD_FAIL_DO_RR) {
     759                 :            :                 match_do_rr = true;
     760                 :            :                 m = 0; /* lowest valid score */
     761         [ +  - ]:       1780 :         } else if (m == RT6_NUD_FAIL_HARD) {
     762                 :            :                 goto out;
     763                 :            :         }
     764                 :            : 
     765         [ +  - ]:       1780 :         if (strict & RT6_LOOKUP_F_REACHABLE)
     766                 :       1780 :                 rt6_probe(nh);
     767                 :            : 
     768                 :            :         /* note that m can be RT6_NUD_FAIL_PROBE at this point */
     769         [ +  - ]:       1780 :         if (m > *mpri) {
     770                 :       1780 :                 *do_rr = match_do_rr;
     771                 :       1780 :                 *mpri = m;
     772                 :            :                 rc = true;
     773                 :            :         }
     774                 :            : out:
     775                 :       1780 :         return rc;
     776                 :            : }
     777                 :            : 
     778                 :            : struct fib6_nh_frl_arg {
     779                 :            :         u32             flags;
     780                 :            :         int             oif;
     781                 :            :         int             strict;
     782                 :            :         int             *mpri;
     783                 :            :         bool            *do_rr;
     784                 :            :         struct fib6_nh  *nh;
     785                 :            : };
     786                 :            : 
     787                 :          0 : static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
     788                 :            : {
     789                 :            :         struct fib6_nh_frl_arg *arg = _arg;
     790                 :            : 
     791                 :          0 :         arg->nh = nh;
     792                 :          0 :         return find_match(nh, arg->flags, arg->oif, arg->strict,
     793                 :            :                           arg->mpri, arg->do_rr);
     794                 :            : }
     795                 :            : 
     796                 :       3560 : static void __find_rr_leaf(struct fib6_info *f6i_start,
     797                 :            :                            struct fib6_info *nomatch, u32 metric,
     798                 :            :                            struct fib6_result *res, struct fib6_info **cont,
     799                 :            :                            int oif, int strict, bool *do_rr, int *mpri)
     800                 :            : {
     801                 :            :         struct fib6_info *f6i;
     802                 :            : 
     803         [ +  + ]:       8900 :         for (f6i = f6i_start;
     804                 :       5340 :              f6i && f6i != nomatch;
     805                 :       1780 :              f6i = rcu_dereference(f6i->fib6_next)) {
     806                 :            :                 bool matched = false;
     807                 :            :                 struct fib6_nh *nh;
     808                 :            : 
     809   [ +  -  -  + ]:       1780 :                 if (cont && f6i->fib6_metric != metric) {
     810                 :          0 :                         *cont = f6i;
     811                 :          0 :                         return;
     812                 :            :                 }
     813                 :            : 
     814         [ -  + ]:       1780 :                 if (fib6_check_expired(f6i))
     815                 :          0 :                         continue;
     816                 :            : 
     817         [ -  + ]:       1780 :                 if (unlikely(f6i->nh)) {
     818                 :          0 :                         struct fib6_nh_frl_arg arg = {
     819                 :            :                                 .flags  = f6i->fib6_flags,
     820                 :            :                                 .oif    = oif,
     821                 :            :                                 .strict = strict,
     822                 :            :                                 .mpri   = mpri,
     823                 :            :                                 .do_rr  = do_rr
     824                 :            :                         };
     825                 :            : 
     826         [ #  # ]:          0 :                         if (nexthop_is_blackhole(f6i->nh)) {
     827                 :          0 :                                 res->fib6_flags = RTF_REJECT;
     828                 :          0 :                                 res->fib6_type = RTN_BLACKHOLE;
     829                 :          0 :                                 res->f6i = f6i;
     830                 :          0 :                                 res->nh = nexthop_fib6_nh(f6i->nh);
     831                 :          0 :                                 return;
     832                 :            :                         }
     833         [ #  # ]:          0 :                         if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
     834                 :            :                                                      &arg)) {
     835                 :            :                                 matched = true;
     836                 :          0 :                                 nh = arg.nh;
     837                 :            :                         }
     838                 :            :                 } else {
     839                 :       1780 :                         nh = f6i->fib6_nh;
     840         [ +  - ]:       1780 :                         if (find_match(nh, f6i->fib6_flags, oif, strict,
     841                 :            :                                        mpri, do_rr))
     842                 :            :                                 matched = true;
     843                 :            :                 }
     844         [ +  - ]:       1780 :                 if (matched) {
     845                 :       1780 :                         res->f6i = f6i;
     846                 :       1780 :                         res->nh = nh;
     847                 :       1780 :                         res->fib6_flags = f6i->fib6_flags;
     848                 :       1780 :                         res->fib6_type = f6i->fib6_type;
     849                 :            :                 }
     850                 :            :         }
     851                 :            : }
     852                 :            : 
     853                 :       1780 : static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
     854                 :            :                          struct fib6_info *rr_head, int oif, int strict,
     855                 :            :                          bool *do_rr, struct fib6_result *res)
     856                 :            : {
     857                 :       1780 :         u32 metric = rr_head->fib6_metric;
     858                 :       1780 :         struct fib6_info *cont = NULL;
     859                 :       1780 :         int mpri = -1;
     860                 :            : 
     861                 :       1780 :         __find_rr_leaf(rr_head, NULL, metric, res, &cont,
     862                 :            :                        oif, strict, do_rr, &mpri);
     863                 :            : 
     864                 :       1780 :         __find_rr_leaf(leaf, rr_head, metric, res, &cont,
     865                 :            :                        oif, strict, do_rr, &mpri);
     866                 :            : 
     867   [ -  +  #  # ]:       1780 :         if (res->f6i || !cont)
     868                 :       1780 :                 return;
     869                 :            : 
     870                 :          0 :         __find_rr_leaf(cont, NULL, metric, res, NULL,
     871                 :            :                        oif, strict, do_rr, &mpri);
     872                 :            : }
     873                 :            : 
     874                 :       8404 : static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
     875                 :            :                        struct fib6_result *res, int strict)
     876                 :            : {
     877                 :       8404 :         struct fib6_info *leaf = rcu_dereference(fn->leaf);
     878                 :            :         struct fib6_info *rt0;
     879                 :       8404 :         bool do_rr = false;
     880                 :            :         int key_plen;
     881                 :            : 
     882                 :            :         /* make sure this function or its helpers sets f6i */
     883                 :       8404 :         res->f6i = NULL;
     884                 :            : 
     885   [ +  -  +  + ]:       8404 :         if (!leaf || leaf == net->ipv6.fib6_null_entry)
     886                 :            :                 goto out;
     887                 :            : 
     888                 :       1780 :         rt0 = rcu_dereference(fn->rr_ptr);
     889         [ +  - ]:       1780 :         if (!rt0)
     890                 :            :                 rt0 = leaf;
     891                 :            : 
     892                 :            :         /* Double check to make sure fn is not an intermediate node
     893                 :            :          * and fn->leaf does not points to its child's leaf
     894                 :            :          * (This might happen if all routes under fn are deleted from
     895                 :            :          * the tree and fib6_repair_tree() is called on the node.)
     896                 :            :          */
     897                 :       1780 :         key_plen = rt0->fib6_dst.plen;
     898                 :            : #ifdef CONFIG_IPV6_SUBTREES
     899         [ -  + ]:       1780 :         if (rt0->fib6_src.plen)
     900                 :            :                 key_plen = rt0->fib6_src.plen;
     901                 :            : #endif
     902         [ +  - ]:       1780 :         if (fn->fn_bit != key_plen)
     903                 :            :                 goto out;
     904                 :            : 
     905                 :       1780 :         find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
     906         [ -  + ]:       1780 :         if (do_rr) {
     907                 :          0 :                 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
     908                 :            : 
     909                 :            :                 /* no entries matched; do round-robin */
     910   [ #  #  #  # ]:          0 :                 if (!next || next->fib6_metric != rt0->fib6_metric)
     911                 :            :                         next = leaf;
     912                 :            : 
     913         [ #  # ]:          0 :                 if (next != rt0) {
     914                 :          0 :                         spin_lock_bh(&leaf->fib6_table->tb6_lock);
     915                 :            :                         /* make sure next is not being deleted from the tree */
     916         [ #  # ]:          0 :                         if (next->fib6_node)
     917                 :          0 :                                 rcu_assign_pointer(fn->rr_ptr, next);
     918                 :          0 :                         spin_unlock_bh(&leaf->fib6_table->tb6_lock);
     919                 :            :                 }
     920                 :            :         }
     921                 :            : 
     922                 :            : out:
     923         [ +  + ]:       8404 :         if (!res->f6i) {
     924                 :       6624 :                 res->f6i = net->ipv6.fib6_null_entry;
     925                 :       6624 :                 res->nh = res->f6i->fib6_nh;
     926                 :       6624 :                 res->fib6_flags = res->f6i->fib6_flags;
     927                 :       6624 :                 res->fib6_type = res->f6i->fib6_type;
     928                 :            :         }
     929                 :       8404 : }
     930                 :            : 
     931                 :            : static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
     932                 :            : {
     933   [ #  #  #  # ]:          0 :         return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
     934                 :          0 :                res->nh->fib_nh_gw_family;
     935                 :            : }
     936                 :            : 
     937                 :            : #ifdef CONFIG_IPV6_ROUTE_INFO
     938                 :          0 : int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
     939                 :            :                   const struct in6_addr *gwaddr)
     940                 :            : {
     941                 :            :         struct net *net = dev_net(dev);
     942                 :            :         struct route_info *rinfo = (struct route_info *) opt;
     943                 :            :         struct in6_addr prefix_buf, *prefix;
     944                 :            :         unsigned int pref;
     945                 :            :         unsigned long lifetime;
     946                 :            :         struct fib6_info *rt;
     947                 :            : 
     948         [ #  # ]:          0 :         if (len < sizeof(struct route_info)) {
     949                 :            :                 return -EINVAL;
     950                 :            :         }
     951                 :            : 
     952                 :            :         /* Sanity check for prefix_len and length */
     953         [ #  # ]:          0 :         if (rinfo->length > 3) {
     954                 :            :                 return -EINVAL;
     955         [ #  # ]:          0 :         } else if (rinfo->prefix_len > 128) {
     956                 :            :                 return -EINVAL;
     957         [ #  # ]:          0 :         } else if (rinfo->prefix_len > 64) {
     958         [ #  # ]:          0 :                 if (rinfo->length < 2) {
     959                 :            :                         return -EINVAL;
     960                 :            :                 }
     961         [ #  # ]:          0 :         } else if (rinfo->prefix_len > 0) {
     962         [ #  # ]:          0 :                 if (rinfo->length < 1) {
     963                 :            :                         return -EINVAL;
     964                 :            :                 }
     965                 :            :         }
     966                 :            : 
     967                 :          0 :         pref = rinfo->route_pref;
     968         [ #  # ]:          0 :         if (pref == ICMPV6_ROUTER_PREF_INVALID)
     969                 :            :                 return -EINVAL;
     970                 :            : 
     971                 :          0 :         lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
     972                 :            : 
     973         [ #  # ]:          0 :         if (rinfo->length == 3)
     974                 :          0 :                 prefix = (struct in6_addr *)rinfo->prefix;
     975                 :            :         else {
     976                 :            :                 /* this function is safe */
     977                 :          0 :                 ipv6_addr_prefix(&prefix_buf,
     978                 :          0 :                                  (struct in6_addr *)rinfo->prefix,
     979                 :            :                                  rinfo->prefix_len);
     980                 :            :                 prefix = &prefix_buf;
     981                 :            :         }
     982                 :            : 
     983         [ #  # ]:          0 :         if (rinfo->prefix_len == 0)
     984                 :          0 :                 rt = rt6_get_dflt_router(net, gwaddr, dev);
     985                 :            :         else
     986                 :          0 :                 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
     987                 :            :                                         gwaddr, dev);
     988                 :            : 
     989         [ #  # ]:          0 :         if (rt && !lifetime) {
     990                 :          0 :                 ip6_del_rt(net, rt);
     991                 :            :                 rt = NULL;
     992                 :            :         }
     993                 :            : 
     994         [ #  # ]:          0 :         if (!rt && lifetime)
     995                 :          0 :                 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
     996                 :            :                                         dev, pref);
     997         [ #  # ]:          0 :         else if (rt)
     998                 :          0 :                 rt->fib6_flags = RTF_ROUTEINFO |
     999                 :          0 :                                  (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
    1000                 :            : 
    1001         [ #  # ]:          0 :         if (rt) {
    1002         [ #  # ]:          0 :                 if (!addrconf_finite_timeout(lifetime))
    1003                 :            :                         fib6_clean_expires(rt);
    1004                 :            :                 else
    1005                 :          0 :                         fib6_set_expires(rt, jiffies + HZ * lifetime);
    1006                 :            : 
    1007                 :          0 :                 fib6_info_release(rt);
    1008                 :            :         }
    1009                 :            :         return 0;
    1010                 :            : }
    1011                 :            : #endif
    1012                 :            : 
    1013                 :            : /*
    1014                 :            :  *      Misc support functions
    1015                 :            :  */
    1016                 :            : 
    1017                 :            : /* called with rcu_lock held */
    1018                 :        510 : static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
    1019                 :            : {
    1020                 :        510 :         struct net_device *dev = res->nh->fib_nh_dev;
    1021                 :            : 
    1022         [ -  + ]:        510 :         if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
    1023                 :            :                 /* for copies of local routes, dst->dev needs to be the
    1024                 :            :                  * device if it is a master device, the master device if
    1025                 :            :                  * device is enslaved, and the loopback as the default
    1026                 :            :                  */
    1027   [ #  #  #  # ]:          0 :                 if (netif_is_l3_slave(dev) &&
    1028                 :          0 :                     !rt6_need_strict(&res->f6i->fib6_dst.addr))
    1029                 :          0 :                         dev = l3mdev_master_dev_rcu(dev);
    1030         [ #  # ]:          0 :                 else if (!netif_is_l3_master(dev))
    1031                 :          0 :                         dev = dev_net(dev)->loopback_dev;
    1032                 :            :                 /* last case is netif_is_l3_master(dev) is true in which
    1033                 :            :                  * case we want dev returned to be dev
    1034                 :            :                  */
    1035                 :            :         }
    1036                 :            : 
    1037                 :        510 :         return dev;
    1038                 :            : }
    1039                 :            : 
    1040                 :            : static const int fib6_prop[RTN_MAX + 1] = {
    1041                 :            :         [RTN_UNSPEC]    = 0,
    1042                 :            :         [RTN_UNICAST]   = 0,
    1043                 :            :         [RTN_LOCAL]     = 0,
    1044                 :            :         [RTN_BROADCAST] = 0,
    1045                 :            :         [RTN_ANYCAST]   = 0,
    1046                 :            :         [RTN_MULTICAST] = 0,
    1047                 :            :         [RTN_BLACKHOLE] = -EINVAL,
    1048                 :            :         [RTN_UNREACHABLE] = -EHOSTUNREACH,
    1049                 :            :         [RTN_PROHIBIT]  = -EACCES,
    1050                 :            :         [RTN_THROW]     = -EAGAIN,
    1051                 :            :         [RTN_NAT]       = -EINVAL,
    1052                 :            :         [RTN_XRESOLVE]  = -EINVAL,
    1053                 :            : };
    1054                 :            : 
    1055                 :            : static int ip6_rt_type_to_error(u8 fib6_type)
    1056                 :            : {
    1057                 :          0 :         return fib6_prop[fib6_type];
    1058                 :            : }
    1059                 :            : 
    1060                 :            : static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
    1061                 :            : {
    1062                 :            :         unsigned short flags = 0;
    1063                 :            : 
    1064   [ -  +  #  # ]:        510 :         if (rt->dst_nocount)
    1065                 :            :                 flags |= DST_NOCOUNT;
    1066   [ -  +  #  # ]:        510 :         if (rt->dst_nopolicy)
    1067                 :          0 :                 flags |= DST_NOPOLICY;
    1068   [ -  +  #  # ]:        510 :         if (rt->dst_host)
    1069                 :          0 :                 flags |= DST_HOST;
    1070                 :            : 
    1071                 :            :         return flags;
    1072                 :            : }
    1073                 :            : 
    1074                 :            : static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
    1075                 :            : {
    1076                 :          0 :         rt->dst.error = ip6_rt_type_to_error(fib6_type);
    1077                 :            : 
    1078      [ #  #  # ]:          0 :         switch (fib6_type) {
    1079                 :            :         case RTN_BLACKHOLE:
    1080                 :          0 :                 rt->dst.output = dst_discard_out;
    1081                 :          0 :                 rt->dst.input = dst_discard;
    1082                 :            :                 break;
    1083                 :            :         case RTN_PROHIBIT:
    1084                 :          0 :                 rt->dst.output = ip6_pkt_prohibit_out;
    1085                 :          0 :                 rt->dst.input = ip6_pkt_prohibit;
    1086                 :            :                 break;
    1087                 :            :         case RTN_THROW:
    1088                 :            :         case RTN_UNREACHABLE:
    1089                 :            :         default:
    1090                 :          0 :                 rt->dst.output = ip6_pkt_discard_out;
    1091                 :          0 :                 rt->dst.input = ip6_pkt_discard;
    1092                 :            :                 break;
    1093                 :            :         }
    1094                 :            : }
    1095                 :            : 
    1096                 :        510 : static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
    1097                 :            : {
    1098                 :        510 :         struct fib6_info *f6i = res->f6i;
    1099                 :            : 
    1100         [ -  + ]:        510 :         if (res->fib6_flags & RTF_REJECT) {
    1101                 :          0 :                 ip6_rt_init_dst_reject(rt, res->fib6_type);
    1102                 :        510 :                 return;
    1103                 :            :         }
    1104                 :            : 
    1105                 :        510 :         rt->dst.error = 0;
    1106                 :        510 :         rt->dst.output = ip6_output;
    1107                 :            : 
    1108         [ -  + ]:        510 :         if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
    1109                 :          0 :                 rt->dst.input = ip6_input;
    1110         [ +  - ]:       1020 :         } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
    1111                 :        510 :                 rt->dst.input = ip6_mc_input;
    1112                 :            :         } else {
    1113                 :          0 :                 rt->dst.input = ip6_forward;
    1114                 :            :         }
    1115                 :            : 
    1116         [ -  + ]:        510 :         if (res->nh->fib_nh_lws) {
    1117                 :          0 :                 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
    1118                 :            :                 lwtunnel_set_redirect(&rt->dst);
    1119                 :            :         }
    1120                 :            : 
    1121                 :        510 :         rt->dst.lastuse = jiffies;
    1122                 :            : }
    1123                 :            : 
    1124                 :            : /* Caller must already hold reference to @from */
    1125                 :        510 : static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
    1126                 :            : {
    1127                 :        510 :         rt->rt6i_flags &= ~RTF_EXPIRES;
    1128                 :        510 :         rcu_assign_pointer(rt->from, from);
    1129                 :        510 :         ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
    1130                 :        510 : }
    1131                 :            : 
    1132                 :            : /* Caller must already hold reference to f6i in result */
    1133                 :        510 : static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
    1134                 :            : {
    1135                 :        510 :         const struct fib6_nh *nh = res->nh;
    1136                 :        510 :         const struct net_device *dev = nh->fib_nh_dev;
    1137                 :        510 :         struct fib6_info *f6i = res->f6i;
    1138                 :            : 
    1139                 :        510 :         ip6_rt_init_dst(rt, res);
    1140                 :            : 
    1141                 :        510 :         rt->rt6i_dst = f6i->fib6_dst;
    1142         [ +  - ]:       1020 :         rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
    1143                 :        510 :         rt->rt6i_flags = res->fib6_flags;
    1144         [ -  + ]:        510 :         if (nh->fib_nh_gw_family) {
    1145                 :          0 :                 rt->rt6i_gateway = nh->fib_nh_gw6;
    1146                 :          0 :                 rt->rt6i_flags |= RTF_GATEWAY;
    1147                 :            :         }
    1148                 :        510 :         rt6_set_from(rt, f6i);
    1149                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1150                 :        510 :         rt->rt6i_src = f6i->fib6_src;
    1151                 :            : #endif
    1152                 :        510 : }
    1153                 :            : 
    1154                 :       6624 : static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
    1155                 :            :                                         struct in6_addr *saddr)
    1156                 :            : {
    1157                 :            :         struct fib6_node *pn, *sn;
    1158                 :            :         while (1) {
    1159         [ -  + ]:       6624 :                 if (fn->fn_flags & RTN_TL_ROOT)
    1160                 :            :                         return NULL;
    1161                 :          0 :                 pn = rcu_dereference(fn->parent);
    1162                 :          0 :                 sn = FIB6_SUBTREE(pn);
    1163         [ #  # ]:          0 :                 if (sn && sn != fn)
    1164                 :          0 :                         fn = fib6_node_lookup(sn, NULL, saddr);
    1165                 :            :                 else
    1166                 :            :                         fn = pn;
    1167         [ #  # ]:          0 :                 if (fn->fn_flags & RTN_RTINFO)
    1168                 :          0 :                         return fn;
    1169                 :            :         }
    1170                 :            : }
    1171                 :            : 
    1172                 :          0 : static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
    1173                 :            : {
    1174                 :          0 :         struct rt6_info *rt = *prt;
    1175                 :            : 
    1176         [ #  # ]:          0 :         if (dst_hold_safe(&rt->dst))
    1177                 :            :                 return true;
    1178         [ #  # ]:          0 :         if (net) {
    1179                 :          0 :                 rt = net->ipv6.ip6_null_entry;
    1180                 :          0 :                 dst_hold(&rt->dst);
    1181                 :            :         } else {
    1182                 :            :                 rt = NULL;
    1183                 :            :         }
    1184                 :          0 :         *prt = rt;
    1185                 :          0 :         return false;
    1186                 :            : }
    1187                 :            : 
    1188                 :            : /* called with rcu_lock held */
    1189                 :          0 : static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
    1190                 :            : {
    1191                 :          0 :         struct net_device *dev = res->nh->fib_nh_dev;
    1192                 :          0 :         struct fib6_info *f6i = res->f6i;
    1193                 :            :         unsigned short flags;
    1194                 :            :         struct rt6_info *nrt;
    1195                 :            : 
    1196         [ #  # ]:          0 :         if (!fib6_info_hold_safe(f6i))
    1197                 :            :                 goto fallback;
    1198                 :            : 
    1199                 :            :         flags = fib6_info_dst_flags(f6i);
    1200                 :          0 :         nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
    1201         [ #  # ]:          0 :         if (!nrt) {
    1202                 :          0 :                 fib6_info_release(f6i);
    1203                 :          0 :                 goto fallback;
    1204                 :            :         }
    1205                 :            : 
    1206                 :          0 :         ip6_rt_copy_init(nrt, res);
    1207                 :          0 :         return nrt;
    1208                 :            : 
    1209                 :            : fallback:
    1210                 :          0 :         nrt = dev_net(dev)->ipv6.ip6_null_entry;
    1211                 :          0 :         dst_hold(&nrt->dst);
    1212                 :          0 :         return nrt;
    1213                 :            : }
    1214                 :            : 
    1215                 :          0 : static struct rt6_info *ip6_pol_route_lookup(struct net *net,
    1216                 :            :                                              struct fib6_table *table,
    1217                 :            :                                              struct flowi6 *fl6,
    1218                 :            :                                              const struct sk_buff *skb,
    1219                 :            :                                              int flags)
    1220                 :            : {
    1221                 :          0 :         struct fib6_result res = {};
    1222                 :            :         struct fib6_node *fn;
    1223                 :            :         struct rt6_info *rt;
    1224                 :            : 
    1225         [ #  # ]:          0 :         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
    1226                 :          0 :                 flags &= ~RT6_LOOKUP_F_IFACE;
    1227                 :            : 
    1228                 :            :         rcu_read_lock();
    1229                 :          0 :         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
    1230                 :            : restart:
    1231                 :          0 :         res.f6i = rcu_dereference(fn->leaf);
    1232         [ #  # ]:          0 :         if (!res.f6i)
    1233                 :          0 :                 res.f6i = net->ipv6.fib6_null_entry;
    1234                 :            :         else
    1235                 :          0 :                 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
    1236                 :            :                                  flags);
    1237                 :            : 
    1238         [ #  # ]:          0 :         if (res.f6i == net->ipv6.fib6_null_entry) {
    1239                 :          0 :                 fn = fib6_backtrack(fn, &fl6->saddr);
    1240         [ #  # ]:          0 :                 if (fn)
    1241                 :            :                         goto restart;
    1242                 :            : 
    1243                 :          0 :                 rt = net->ipv6.ip6_null_entry;
    1244                 :          0 :                 dst_hold(&rt->dst);
    1245                 :          0 :                 goto out;
    1246         [ #  # ]:          0 :         } else if (res.fib6_flags & RTF_REJECT) {
    1247                 :            :                 goto do_create;
    1248                 :            :         }
    1249                 :            : 
    1250                 :          0 :         fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
    1251                 :          0 :                          fl6->flowi6_oif != 0, skb, flags);
    1252                 :            : 
    1253                 :            :         /* Search through exception table */
    1254                 :          0 :         rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
    1255         [ #  # ]:          0 :         if (rt) {
    1256         [ #  # ]:          0 :                 if (ip6_hold_safe(net, &rt))
    1257                 :          0 :                         dst_use_noref(&rt->dst, jiffies);
    1258                 :            :         } else {
    1259                 :            : do_create:
    1260                 :          0 :                 rt = ip6_create_rt_rcu(&res);
    1261                 :            :         }
    1262                 :            : 
    1263                 :            : out:
    1264                 :          0 :         trace_fib6_table_lookup(net, &res, table, fl6);
    1265                 :            : 
    1266                 :            :         rcu_read_unlock();
    1267                 :            : 
    1268                 :          0 :         return rt;
    1269                 :            : }
    1270                 :            : 
    1271                 :          0 : struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
    1272                 :            :                                    const struct sk_buff *skb, int flags)
    1273                 :            : {
    1274                 :          0 :         return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
    1275                 :            : }
    1276                 :            : EXPORT_SYMBOL_GPL(ip6_route_lookup);
    1277                 :            : 
    1278                 :          0 : struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
    1279                 :            :                             const struct in6_addr *saddr, int oif,
    1280                 :            :                             const struct sk_buff *skb, int strict)
    1281                 :            : {
    1282                 :          0 :         struct flowi6 fl6 = {
    1283                 :            :                 .flowi6_oif = oif,
    1284                 :            :                 .daddr = *daddr,
    1285                 :            :         };
    1286                 :            :         struct dst_entry *dst;
    1287                 :          0 :         int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
    1288                 :            : 
    1289         [ #  # ]:          0 :         if (saddr) {
    1290                 :          0 :                 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
    1291                 :          0 :                 flags |= RT6_LOOKUP_F_HAS_SADDR;
    1292                 :            :         }
    1293                 :            : 
    1294                 :          0 :         dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
    1295         [ #  # ]:          0 :         if (dst->error == 0)
    1296                 :            :                 return (struct rt6_info *) dst;
    1297                 :            : 
    1298                 :          0 :         dst_release(dst);
    1299                 :            : 
    1300                 :          0 :         return NULL;
    1301                 :            : }
    1302                 :            : EXPORT_SYMBOL(rt6_lookup);
    1303                 :            : 
    1304                 :            : /* ip6_ins_rt is called with FREE table->tb6_lock.
    1305                 :            :  * It takes new route entry, the addition fails by any reason the
    1306                 :            :  * route is released.
    1307                 :            :  * Caller must hold dst before calling it.
    1308                 :            :  */
    1309                 :            : 
    1310                 :       1667 : static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
    1311                 :            :                         struct netlink_ext_ack *extack)
    1312                 :            : {
    1313                 :            :         int err;
    1314                 :            :         struct fib6_table *table;
    1315                 :            : 
    1316                 :       1667 :         table = rt->fib6_table;
    1317                 :            :         spin_lock_bh(&table->tb6_lock);
    1318                 :       1667 :         err = fib6_add(&table->tb6_root, rt, info, extack);
    1319                 :            :         spin_unlock_bh(&table->tb6_lock);
    1320                 :            : 
    1321                 :       1667 :         return err;
    1322                 :            : }
    1323                 :            : 
    1324                 :        425 : int ip6_ins_rt(struct net *net, struct fib6_info *rt)
    1325                 :            : {
    1326                 :        425 :         struct nl_info info = { .nl_net = net, };
    1327                 :            : 
    1328                 :        425 :         return __ip6_ins_rt(rt, &info, NULL);
    1329                 :            : }
    1330                 :            : 
    1331                 :          0 : static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
    1332                 :            :                                            const struct in6_addr *daddr,
    1333                 :            :                                            const struct in6_addr *saddr)
    1334                 :            : {
    1335                 :          0 :         struct fib6_info *f6i = res->f6i;
    1336                 :            :         struct net_device *dev;
    1337                 :            :         struct rt6_info *rt;
    1338                 :            : 
    1339                 :            :         /*
    1340                 :            :          *      Clone the route.
    1341                 :            :          */
    1342                 :            : 
    1343         [ #  # ]:          0 :         if (!fib6_info_hold_safe(f6i))
    1344                 :            :                 return NULL;
    1345                 :            : 
    1346                 :          0 :         dev = ip6_rt_get_dev_rcu(res);
    1347                 :          0 :         rt = ip6_dst_alloc(dev_net(dev), dev, 0);
    1348         [ #  # ]:          0 :         if (!rt) {
    1349                 :          0 :                 fib6_info_release(f6i);
    1350                 :          0 :                 return NULL;
    1351                 :            :         }
    1352                 :            : 
    1353                 :          0 :         ip6_rt_copy_init(rt, res);
    1354                 :          0 :         rt->rt6i_flags |= RTF_CACHE;
    1355                 :          0 :         rt->dst.flags |= DST_HOST;
    1356                 :          0 :         rt->rt6i_dst.addr = *daddr;
    1357                 :          0 :         rt->rt6i_dst.plen = 128;
    1358                 :            : 
    1359         [ #  # ]:          0 :         if (!rt6_is_gw_or_nonexthop(res)) {
    1360   [ #  #  #  # ]:          0 :                 if (f6i->fib6_dst.plen != 128 &&
    1361                 :            :                     ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
    1362                 :          0 :                         rt->rt6i_flags |= RTF_ANYCAST;
    1363                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1364   [ #  #  #  # ]:          0 :                 if (rt->rt6i_src.plen && saddr) {
    1365                 :          0 :                         rt->rt6i_src.addr = *saddr;
    1366                 :          0 :                         rt->rt6i_src.plen = 128;
    1367                 :            :                 }
    1368                 :            : #endif
    1369                 :            :         }
    1370                 :            : 
    1371                 :          0 :         return rt;
    1372                 :            : }
    1373                 :            : 
    1374                 :        510 : static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
    1375                 :            : {
    1376                 :        510 :         struct fib6_info *f6i = res->f6i;
    1377                 :            :         unsigned short flags = fib6_info_dst_flags(f6i);
    1378                 :            :         struct net_device *dev;
    1379                 :            :         struct rt6_info *pcpu_rt;
    1380                 :            : 
    1381         [ +  - ]:        510 :         if (!fib6_info_hold_safe(f6i))
    1382                 :            :                 return NULL;
    1383                 :            : 
    1384                 :            :         rcu_read_lock();
    1385                 :        510 :         dev = ip6_rt_get_dev_rcu(res);
    1386                 :        510 :         pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
    1387                 :            :         rcu_read_unlock();
    1388         [ -  + ]:        510 :         if (!pcpu_rt) {
    1389                 :          0 :                 fib6_info_release(f6i);
    1390                 :          0 :                 return NULL;
    1391                 :            :         }
    1392                 :        510 :         ip6_rt_copy_init(pcpu_rt, res);
    1393                 :        510 :         pcpu_rt->rt6i_flags |= RTF_PCPU;
    1394                 :            : 
    1395         [ -  + ]:        510 :         if (f6i->nh)
    1396                 :          0 :                 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
    1397                 :            : 
    1398                 :        510 :         return pcpu_rt;
    1399                 :            : }
    1400                 :            : 
    1401                 :            : static bool rt6_is_valid(const struct rt6_info *rt6)
    1402                 :            : {
    1403                 :          0 :         return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
    1404                 :            : }
    1405                 :            : 
    1406                 :            : /* It should be called with rcu_read_lock() acquired */
    1407                 :       1780 : static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
    1408                 :            : {
    1409                 :            :         struct rt6_info *pcpu_rt;
    1410                 :            : 
    1411                 :       5340 :         pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
    1412                 :            : 
    1413   [ +  +  -  +  :       1780 :         if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
                   #  # ]
    1414                 :            :                 struct rt6_info *prev, **p;
    1415                 :            : 
    1416                 :          0 :                 p = this_cpu_ptr(res->nh->rt6i_pcpu);
    1417                 :          0 :                 prev = xchg(p, NULL);
    1418         [ #  # ]:          0 :                 if (prev) {
    1419                 :          0 :                         dst_dev_put(&prev->dst);
    1420                 :          0 :                         dst_release(&prev->dst);
    1421                 :            :                 }
    1422                 :            : 
    1423                 :            :                 pcpu_rt = NULL;
    1424                 :            :         }
    1425                 :            : 
    1426                 :       1780 :         return pcpu_rt;
    1427                 :            : }
    1428                 :            : 
    1429                 :        510 : static struct rt6_info *rt6_make_pcpu_route(struct net *net,
    1430                 :            :                                             const struct fib6_result *res)
    1431                 :            : {
    1432                 :            :         struct rt6_info *pcpu_rt, *prev, **p;
    1433                 :            : 
    1434                 :        510 :         pcpu_rt = ip6_rt_pcpu_alloc(res);
    1435         [ +  - ]:        510 :         if (!pcpu_rt)
    1436                 :            :                 return NULL;
    1437                 :            : 
    1438                 :       1020 :         p = this_cpu_ptr(res->nh->rt6i_pcpu);
    1439                 :        510 :         prev = cmpxchg(p, NULL, pcpu_rt);
    1440         [ -  + ]:        510 :         BUG_ON(prev);
    1441                 :            : 
    1442         [ -  + ]:        510 :         if (res->f6i->fib6_destroying) {
    1443                 :            :                 struct fib6_info *from;
    1444                 :            : 
    1445                 :          0 :                 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
    1446                 :          0 :                 fib6_info_release(from);
    1447                 :            :         }
    1448                 :            : 
    1449                 :        510 :         return pcpu_rt;
    1450                 :            : }
    1451                 :            : 
    1452                 :            : /* exception hash table implementation
    1453                 :            :  */
    1454                 :            : static DEFINE_SPINLOCK(rt6_exception_lock);
    1455                 :            : 
    1456                 :            : /* Remove rt6_ex from hash table and free the memory
    1457                 :            :  * Caller must hold rt6_exception_lock
    1458                 :            :  */
    1459                 :          0 : static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
    1460                 :            :                                  struct rt6_exception *rt6_ex)
    1461                 :            : {
    1462                 :            :         struct fib6_info *from;
    1463                 :            :         struct net *net;
    1464                 :            : 
    1465         [ #  # ]:          0 :         if (!bucket || !rt6_ex)
    1466                 :          0 :                 return;
    1467                 :            : 
    1468                 :          0 :         net = dev_net(rt6_ex->rt6i->dst.dev);
    1469                 :          0 :         net->ipv6.rt6_stats->fib_rt_cache--;
    1470                 :            : 
    1471                 :            :         /* purge completely the exception to allow releasing the held resources:
    1472                 :            :          * some [sk] cache may keep the dst around for unlimited time
    1473                 :            :          */
    1474                 :          0 :         from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
    1475                 :          0 :         fib6_info_release(from);
    1476                 :          0 :         dst_dev_put(&rt6_ex->rt6i->dst);
    1477                 :            : 
    1478                 :            :         hlist_del_rcu(&rt6_ex->hlist);
    1479                 :          0 :         dst_release(&rt6_ex->rt6i->dst);
    1480         [ #  # ]:          0 :         kfree_rcu(rt6_ex, rcu);
    1481   [ #  #  #  # ]:          0 :         WARN_ON_ONCE(!bucket->depth);
    1482                 :          0 :         bucket->depth--;
    1483                 :            : }
    1484                 :            : 
    1485                 :            : /* Remove oldest rt6_ex in bucket and free the memory
    1486                 :            :  * Caller must hold rt6_exception_lock
    1487                 :            :  */
    1488                 :          0 : static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
    1489                 :            : {
    1490                 :            :         struct rt6_exception *rt6_ex, *oldest = NULL;
    1491                 :            : 
    1492         [ #  # ]:          0 :         if (!bucket)
    1493                 :          0 :                 return;
    1494                 :            : 
    1495   [ #  #  #  #  :          0 :         hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
                   #  # ]
    1496   [ #  #  #  # ]:          0 :                 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
    1497                 :            :                         oldest = rt6_ex;
    1498                 :            :         }
    1499                 :          0 :         rt6_remove_exception(bucket, oldest);
    1500                 :            : }
    1501                 :            : 
    1502                 :          0 : static u32 rt6_exception_hash(const struct in6_addr *dst,
    1503                 :            :                               const struct in6_addr *src)
    1504                 :            : {
    1505                 :            :         static u32 seed __read_mostly;
    1506                 :            :         u32 val;
    1507                 :            : 
    1508   [ #  #  #  # ]:          0 :         net_get_random_once(&seed, sizeof(seed));
    1509                 :          0 :         val = jhash(dst, sizeof(*dst), seed);
    1510                 :            : 
    1511                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1512         [ #  # ]:          0 :         if (src)
    1513                 :          0 :                 val = jhash(src, sizeof(*src), val);
    1514                 :            : #endif
    1515                 :          0 :         return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
    1516                 :            : }
    1517                 :            : 
    1518                 :            : /* Helper function to find the cached rt in the hash table
    1519                 :            :  * and update bucket pointer to point to the bucket for this
    1520                 :            :  * (daddr, saddr) pair
    1521                 :            :  * Caller must hold rt6_exception_lock
    1522                 :            :  */
    1523                 :            : static struct rt6_exception *
    1524                 :          0 : __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
    1525                 :            :                               const struct in6_addr *daddr,
    1526                 :            :                               const struct in6_addr *saddr)
    1527                 :            : {
    1528                 :            :         struct rt6_exception *rt6_ex;
    1529                 :            :         u32 hval;
    1530                 :            : 
    1531   [ #  #  #  # ]:          0 :         if (!(*bucket) || !daddr)
    1532                 :            :                 return NULL;
    1533                 :            : 
    1534                 :          0 :         hval = rt6_exception_hash(daddr, saddr);
    1535                 :          0 :         *bucket += hval;
    1536                 :            : 
    1537   [ #  #  #  #  :          0 :         hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
                   #  # ]
    1538                 :          0 :                 struct rt6_info *rt6 = rt6_ex->rt6i;
    1539                 :            :                 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
    1540                 :            : 
    1541                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1542         [ #  # ]:          0 :                 if (matched && saddr)
    1543                 :            :                         matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
    1544                 :            : #endif
    1545         [ #  # ]:          0 :                 if (matched)
    1546                 :          0 :                         return rt6_ex;
    1547                 :            :         }
    1548                 :            :         return NULL;
    1549                 :            : }
    1550                 :            : 
    1551                 :            : /* Helper function to find the cached rt in the hash table
    1552                 :            :  * and update bucket pointer to point to the bucket for this
    1553                 :            :  * (daddr, saddr) pair
    1554                 :            :  * Caller must hold rcu_read_lock()
    1555                 :            :  */
    1556                 :            : static struct rt6_exception *
    1557                 :       1780 : __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
    1558                 :            :                          const struct in6_addr *daddr,
    1559                 :            :                          const struct in6_addr *saddr)
    1560                 :            : {
    1561                 :            :         struct rt6_exception *rt6_ex;
    1562                 :            :         u32 hval;
    1563                 :            : 
    1564                 :            :         WARN_ON_ONCE(!rcu_read_lock_held());
    1565                 :            : 
    1566   [ -  +  #  # ]:       1780 :         if (!(*bucket) || !daddr)
    1567                 :            :                 return NULL;
    1568                 :            : 
    1569                 :          0 :         hval = rt6_exception_hash(daddr, saddr);
    1570                 :          0 :         *bucket += hval;
    1571                 :            : 
    1572   [ #  #  #  #  :          0 :         hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
                   #  # ]
    1573                 :          0 :                 struct rt6_info *rt6 = rt6_ex->rt6i;
    1574                 :            :                 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
    1575                 :            : 
    1576                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1577         [ #  # ]:          0 :                 if (matched && saddr)
    1578                 :            :                         matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
    1579                 :            : #endif
    1580         [ #  # ]:          0 :                 if (matched)
    1581                 :          0 :                         return rt6_ex;
    1582                 :            :         }
    1583                 :            :         return NULL;
    1584                 :            : }
    1585                 :            : 
    1586                 :          0 : static unsigned int fib6_mtu(const struct fib6_result *res)
    1587                 :            : {
    1588                 :          0 :         const struct fib6_nh *nh = res->nh;
    1589                 :            :         unsigned int mtu;
    1590                 :            : 
    1591         [ #  # ]:          0 :         if (res->f6i->fib6_pmtu) {
    1592                 :            :                 mtu = res->f6i->fib6_pmtu;
    1593                 :            :         } else {
    1594                 :          0 :                 struct net_device *dev = nh->fib_nh_dev;
    1595                 :            :                 struct inet6_dev *idev;
    1596                 :            : 
    1597                 :            :                 rcu_read_lock();
    1598                 :            :                 idev = __in6_dev_get(dev);
    1599                 :          0 :                 mtu = idev->cnf.mtu6;
    1600                 :            :                 rcu_read_unlock();
    1601                 :            :         }
    1602                 :            : 
    1603                 :          0 :         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
    1604                 :            : 
    1605                 :          0 :         return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
    1606                 :            : }
    1607                 :            : 
    1608                 :            : #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
    1609                 :            : 
    1610                 :            : /* used when the flushed bit is not relevant, only access to the bucket
    1611                 :            :  * (ie., all bucket users except rt6_insert_exception);
    1612                 :            :  *
    1613                 :            :  * called under rcu lock; sometimes called with rt6_exception_lock held
    1614                 :            :  */
    1615                 :            : static
    1616                 :            : struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
    1617                 :            :                                                        spinlock_t *lock)
    1618                 :            : {
    1619                 :            :         struct rt6_exception_bucket *bucket;
    1620                 :            : 
    1621   [ #  #  #  #  :       1482 :         if (lock)
          #  #  #  #  +  
                      - ]
    1622                 :       1482 :                 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
    1623                 :            :                                                    lockdep_is_held(lock));
    1624                 :            :         else
    1625                 :       3665 :                 bucket = rcu_dereference(nh->rt6i_exception_bucket);
    1626                 :            : 
    1627                 :            :         /* remove bucket flushed bit if set */
    1628   [ -  +  -  +  :       5147 :         if (bucket) {
          #  #  #  #  #  
          #  #  #  #  #  
             -  +  -  + ]
    1629                 :          0 :                 unsigned long p = (unsigned long)bucket;
    1630                 :            : 
    1631                 :          0 :                 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
    1632                 :          0 :                 bucket = (struct rt6_exception_bucket *)p;
    1633                 :            :         }
    1634                 :            : 
    1635                 :            :         return bucket;
    1636                 :            : }
    1637                 :            : 
    1638                 :            : static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
    1639                 :            : {
    1640                 :          0 :         unsigned long p = (unsigned long)bucket;
    1641                 :            : 
    1642                 :          0 :         return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
    1643                 :            : }
    1644                 :            : 
    1645                 :            : /* called with rt6_exception_lock held */
    1646                 :            : static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
    1647                 :            :                                               spinlock_t *lock)
    1648                 :            : {
    1649                 :            :         struct rt6_exception_bucket *bucket;
    1650                 :            :         unsigned long p;
    1651                 :            : 
    1652                 :          0 :         bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
    1653                 :            :                                            lockdep_is_held(lock));
    1654                 :            : 
    1655                 :          0 :         p = (unsigned long)bucket;
    1656                 :          0 :         p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
    1657                 :            :         bucket = (struct rt6_exception_bucket *)p;
    1658                 :          0 :         rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
    1659                 :            : }
    1660                 :            : 
    1661                 :          0 : static int rt6_insert_exception(struct rt6_info *nrt,
    1662                 :            :                                 const struct fib6_result *res)
    1663                 :            : {
    1664                 :          0 :         struct net *net = dev_net(nrt->dst.dev);
    1665                 :            :         struct rt6_exception_bucket *bucket;
    1666                 :          0 :         struct fib6_info *f6i = res->f6i;
    1667                 :            :         struct in6_addr *src_key = NULL;
    1668                 :            :         struct rt6_exception *rt6_ex;
    1669                 :          0 :         struct fib6_nh *nh = res->nh;
    1670                 :            :         int err = 0;
    1671                 :            : 
    1672                 :            :         spin_lock_bh(&rt6_exception_lock);
    1673                 :            : 
    1674                 :          0 :         bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
    1675                 :            :                                           lockdep_is_held(&rt6_exception_lock));
    1676         [ #  # ]:          0 :         if (!bucket) {
    1677                 :          0 :                 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
    1678                 :            :                                  GFP_ATOMIC);
    1679         [ #  # ]:          0 :                 if (!bucket) {
    1680                 :            :                         err = -ENOMEM;
    1681                 :            :                         goto out;
    1682                 :            :                 }
    1683                 :          0 :                 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
    1684         [ #  # ]:          0 :         } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
    1685                 :            :                 err = -EINVAL;
    1686                 :            :                 goto out;
    1687                 :            :         }
    1688                 :            : 
    1689                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1690                 :            :         /* fib6_src.plen != 0 indicates f6i is in subtree
    1691                 :            :          * and exception table is indexed by a hash of
    1692                 :            :          * both fib6_dst and fib6_src.
    1693                 :            :          * Otherwise, the exception table is indexed by
    1694                 :            :          * a hash of only fib6_dst.
    1695                 :            :          */
    1696         [ #  # ]:          0 :         if (f6i->fib6_src.plen)
    1697                 :          0 :                 src_key = &nrt->rt6i_src.addr;
    1698                 :            : #endif
    1699                 :            :         /* rt6_mtu_change() might lower mtu on f6i.
    1700                 :            :          * Only insert this exception route if its mtu
    1701                 :            :          * is less than f6i's mtu value.
    1702                 :            :          */
    1703         [ #  # ]:          0 :         if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
    1704                 :            :                 err = -EINVAL;
    1705                 :            :                 goto out;
    1706                 :            :         }
    1707                 :            : 
    1708                 :          0 :         rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
    1709                 :            :                                                src_key);
    1710         [ #  # ]:          0 :         if (rt6_ex)
    1711                 :          0 :                 rt6_remove_exception(bucket, rt6_ex);
    1712                 :            : 
    1713                 :          0 :         rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
    1714         [ #  # ]:          0 :         if (!rt6_ex) {
    1715                 :            :                 err = -ENOMEM;
    1716                 :            :                 goto out;
    1717                 :            :         }
    1718                 :          0 :         rt6_ex->rt6i = nrt;
    1719                 :          0 :         rt6_ex->stamp = jiffies;
    1720                 :          0 :         hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
    1721                 :          0 :         bucket->depth++;
    1722                 :          0 :         net->ipv6.rt6_stats->fib_rt_cache++;
    1723                 :            : 
    1724         [ #  # ]:          0 :         if (bucket->depth > FIB6_MAX_DEPTH)
    1725                 :          0 :                 rt6_exception_remove_oldest(bucket);
    1726                 :            : 
    1727                 :            : out:
    1728                 :            :         spin_unlock_bh(&rt6_exception_lock);
    1729                 :            : 
    1730                 :            :         /* Update fn->fn_sernum to invalidate all cached dst */
    1731         [ #  # ]:          0 :         if (!err) {
    1732                 :          0 :                 spin_lock_bh(&f6i->fib6_table->tb6_lock);
    1733                 :          0 :                 fib6_update_sernum(net, f6i);
    1734                 :          0 :                 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
    1735                 :          0 :                 fib6_force_start_gc(net);
    1736                 :            :         }
    1737                 :            : 
    1738                 :          0 :         return err;
    1739                 :            : }
    1740                 :            : 
    1741                 :       1482 : static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
    1742                 :            : {
    1743                 :            :         struct rt6_exception_bucket *bucket;
    1744                 :            :         struct rt6_exception *rt6_ex;
    1745                 :            :         struct hlist_node *tmp;
    1746                 :            :         int i;
    1747                 :            : 
    1748                 :            :         spin_lock_bh(&rt6_exception_lock);
    1749                 :            : 
    1750                 :            :         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
    1751         [ -  + ]:       1482 :         if (!bucket)
    1752                 :            :                 goto out;
    1753                 :            : 
    1754                 :            :         /* Prevent rt6_insert_exception() to recreate the bucket list */
    1755         [ #  # ]:          0 :         if (!from)
    1756                 :            :                 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
    1757                 :            : 
    1758         [ #  # ]:          0 :         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
    1759   [ #  #  #  #  :          0 :                 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
                   #  # ]
    1760   [ #  #  #  # ]:          0 :                         if (!from ||
    1761                 :          0 :                             rcu_access_pointer(rt6_ex->rt6i->from) == from)
    1762                 :          0 :                                 rt6_remove_exception(bucket, rt6_ex);
    1763                 :            :                 }
    1764   [ #  #  #  #  :          0 :                 WARN_ON_ONCE(!from && bucket->depth);
             #  #  #  # ]
    1765                 :          0 :                 bucket++;
    1766                 :            :         }
    1767                 :            : out:
    1768                 :            :         spin_unlock_bh(&rt6_exception_lock);
    1769                 :       1482 : }
    1770                 :            : 
    1771                 :          0 : static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
    1772                 :            : {
    1773                 :            :         struct fib6_info *f6i = arg;
    1774                 :            : 
    1775                 :          0 :         fib6_nh_flush_exceptions(nh, f6i);
    1776                 :            : 
    1777                 :          0 :         return 0;
    1778                 :            : }
    1779                 :            : 
    1780                 :        218 : void rt6_flush_exceptions(struct fib6_info *f6i)
    1781                 :            : {
    1782         [ -  + ]:        218 :         if (f6i->nh)
    1783                 :          0 :                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
    1784                 :            :                                          f6i);
    1785                 :            :         else
    1786                 :        218 :                 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
    1787                 :        218 : }
    1788                 :            : 
    1789                 :            : /* Find cached rt in the hash table inside passed in rt
    1790                 :            :  * Caller has to hold rcu_read_lock()
    1791                 :            :  */
    1792                 :       1780 : static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
    1793                 :            :                                            const struct in6_addr *daddr,
    1794                 :            :                                            const struct in6_addr *saddr)
    1795                 :            : {
    1796                 :            :         const struct in6_addr *src_key = NULL;
    1797                 :            :         struct rt6_exception_bucket *bucket;
    1798                 :            :         struct rt6_exception *rt6_ex;
    1799                 :            :         struct rt6_info *ret = NULL;
    1800                 :            : 
    1801                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1802                 :            :         /* fib6i_src.plen != 0 indicates f6i is in subtree
    1803                 :            :          * and exception table is indexed by a hash of
    1804                 :            :          * both fib6_dst and fib6_src.
    1805                 :            :          * However, the src addr used to create the hash
    1806                 :            :          * might not be exactly the passed in saddr which
    1807                 :            :          * is a /128 addr from the flow.
    1808                 :            :          * So we need to use f6i->fib6_src to redo lookup
    1809                 :            :          * if the passed in saddr does not find anything.
    1810                 :            :          * (See the logic in ip6_rt_cache_alloc() on how
    1811                 :            :          * rt->rt6i_src is updated.)
    1812                 :            :          */
    1813         [ -  + ]:       1780 :         if (res->f6i->fib6_src.plen)
    1814                 :            :                 src_key = saddr;
    1815                 :            : find_ex:
    1816                 :            : #endif
    1817                 :       3560 :         bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
    1818                 :       1780 :         rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
    1819                 :            : 
    1820   [ -  +  #  # ]:       1780 :         if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
    1821                 :          0 :                 ret = rt6_ex->rt6i;
    1822                 :            : 
    1823                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1824                 :            :         /* Use fib6_src as src_key and redo lookup */
    1825   [ -  +  #  # ]:       1780 :         if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
    1826                 :            :                 src_key = &res->f6i->fib6_src.addr;
    1827                 :            :                 goto find_ex;
    1828                 :            :         }
    1829                 :            : #endif
    1830                 :            : 
    1831                 :       1780 :         return ret;
    1832                 :            : }
    1833                 :            : 
    1834                 :            : /* Remove the passed in cached rt from the hash table that contains it */
    1835                 :          0 : static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
    1836                 :            :                                     const struct rt6_info *rt)
    1837                 :            : {
    1838                 :            :         const struct in6_addr *src_key = NULL;
    1839                 :            :         struct rt6_exception_bucket *bucket;
    1840                 :            :         struct rt6_exception *rt6_ex;
    1841                 :            :         int err;
    1842                 :            : 
    1843         [ #  # ]:          0 :         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
    1844                 :            :                 return -ENOENT;
    1845                 :            : 
    1846                 :            :         spin_lock_bh(&rt6_exception_lock);
    1847                 :          0 :         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
    1848                 :            : 
    1849                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1850                 :            :         /* rt6i_src.plen != 0 indicates 'from' is in subtree
    1851                 :            :          * and exception table is indexed by a hash of
    1852                 :            :          * both rt6i_dst and rt6i_src.
    1853                 :            :          * Otherwise, the exception table is indexed by
    1854                 :            :          * a hash of only rt6i_dst.
    1855                 :            :          */
    1856         [ #  # ]:          0 :         if (plen)
    1857                 :          0 :                 src_key = &rt->rt6i_src.addr;
    1858                 :            : #endif
    1859                 :          0 :         rt6_ex = __rt6_find_exception_spinlock(&bucket,
    1860                 :            :                                                &rt->rt6i_dst.addr,
    1861                 :            :                                                src_key);
    1862         [ #  # ]:          0 :         if (rt6_ex) {
    1863                 :          0 :                 rt6_remove_exception(bucket, rt6_ex);
    1864                 :            :                 err = 0;
    1865                 :            :         } else {
    1866                 :            :                 err = -ENOENT;
    1867                 :            :         }
    1868                 :            : 
    1869                 :            :         spin_unlock_bh(&rt6_exception_lock);
    1870                 :          0 :         return err;
    1871                 :            : }
    1872                 :            : 
    1873                 :            : struct fib6_nh_excptn_arg {
    1874                 :            :         struct rt6_info *rt;
    1875                 :            :         int             plen;
    1876                 :            : };
    1877                 :            : 
    1878                 :          0 : static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
    1879                 :            : {
    1880                 :            :         struct fib6_nh_excptn_arg *arg = _arg;
    1881                 :            :         int err;
    1882                 :            : 
    1883                 :          0 :         err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
    1884         [ #  # ]:          0 :         if (err == 0)
    1885                 :            :                 return 1;
    1886                 :            : 
    1887                 :          0 :         return 0;
    1888                 :            : }
    1889                 :            : 
    1890                 :          0 : static int rt6_remove_exception_rt(struct rt6_info *rt)
    1891                 :            : {
    1892                 :            :         struct fib6_info *from;
    1893                 :            : 
    1894                 :          0 :         from = rcu_dereference(rt->from);
    1895   [ #  #  #  # ]:          0 :         if (!from || !(rt->rt6i_flags & RTF_CACHE))
    1896                 :            :                 return -EINVAL;
    1897                 :            : 
    1898         [ #  # ]:          0 :         if (from->nh) {
    1899                 :          0 :                 struct fib6_nh_excptn_arg arg = {
    1900                 :            :                         .rt = rt,
    1901                 :          0 :                         .plen = from->fib6_src.plen
    1902                 :            :                 };
    1903                 :            :                 int rc;
    1904                 :            : 
    1905                 :            :                 /* rc = 1 means an entry was found */
    1906                 :          0 :                 rc = nexthop_for_each_fib6_nh(from->nh,
    1907                 :            :                                               rt6_nh_remove_exception_rt,
    1908                 :            :                                               &arg);
    1909         [ #  # ]:          0 :                 return rc ? 0 : -ENOENT;
    1910                 :            :         }
    1911                 :            : 
    1912                 :          0 :         return fib6_nh_remove_exception(from->fib6_nh,
    1913                 :            :                                         from->fib6_src.plen, rt);
    1914                 :            : }
    1915                 :            : 
    1916                 :            : /* Find rt6_ex which contains the passed in rt cache and
    1917                 :            :  * refresh its stamp
    1918                 :            :  */
    1919                 :          0 : static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
    1920                 :            :                                      const struct rt6_info *rt)
    1921                 :            : {
    1922                 :            :         const struct in6_addr *src_key = NULL;
    1923                 :            :         struct rt6_exception_bucket *bucket;
    1924                 :            :         struct rt6_exception *rt6_ex;
    1925                 :            : 
    1926                 :          0 :         bucket = fib6_nh_get_excptn_bucket(nh, NULL);
    1927                 :            : #ifdef CONFIG_IPV6_SUBTREES
    1928                 :            :         /* rt6i_src.plen != 0 indicates 'from' is in subtree
    1929                 :            :          * and exception table is indexed by a hash of
    1930                 :            :          * both rt6i_dst and rt6i_src.
    1931                 :            :          * Otherwise, the exception table is indexed by
    1932                 :            :          * a hash of only rt6i_dst.
    1933                 :            :          */
    1934         [ #  # ]:          0 :         if (plen)
    1935                 :          0 :                 src_key = &rt->rt6i_src.addr;
    1936                 :            : #endif
    1937                 :          0 :         rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
    1938         [ #  # ]:          0 :         if (rt6_ex)
    1939                 :          0 :                 rt6_ex->stamp = jiffies;
    1940                 :          0 : }
    1941                 :            : 
    1942                 :            : struct fib6_nh_match_arg {
    1943                 :            :         const struct net_device *dev;
    1944                 :            :         const struct in6_addr   *gw;
    1945                 :            :         struct fib6_nh          *match;
    1946                 :            : };
    1947                 :            : 
    1948                 :            : /* determine if fib6_nh has given device and gateway */
    1949                 :          0 : static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
    1950                 :            : {
    1951                 :            :         struct fib6_nh_match_arg *arg = _arg;
    1952                 :            : 
    1953   [ #  #  #  # ]:          0 :         if (arg->dev != nh->fib_nh_dev ||
    1954   [ #  #  #  # ]:          0 :             (arg->gw && !nh->fib_nh_gw_family) ||
    1955   [ #  #  #  # ]:          0 :             (!arg->gw && nh->fib_nh_gw_family) ||
    1956         [ #  # ]:          0 :             (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
    1957                 :            :                 return 0;
    1958                 :            : 
    1959                 :          0 :         arg->match = nh;
    1960                 :            : 
    1961                 :            :         /* found a match, break the loop */
    1962                 :          0 :         return 1;
    1963                 :            : }
    1964                 :            : 
    1965                 :          0 : static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
    1966                 :            : {
    1967                 :            :         struct fib6_info *from;
    1968                 :            :         struct fib6_nh *fib6_nh;
    1969                 :            : 
    1970                 :            :         rcu_read_lock();
    1971                 :            : 
    1972                 :          0 :         from = rcu_dereference(rt->from);
    1973   [ #  #  #  # ]:          0 :         if (!from || !(rt->rt6i_flags & RTF_CACHE))
    1974                 :            :                 goto unlock;
    1975                 :            : 
    1976         [ #  # ]:          0 :         if (from->nh) {
    1977                 :          0 :                 struct fib6_nh_match_arg arg = {
    1978                 :          0 :                         .dev = rt->dst.dev,
    1979                 :          0 :                         .gw = &rt->rt6i_gateway,
    1980                 :            :                 };
    1981                 :            : 
    1982                 :          0 :                 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
    1983                 :            : 
    1984         [ #  # ]:          0 :                 if (!arg.match)
    1985                 :            :                         goto unlock;
    1986                 :            :                 fib6_nh = arg.match;
    1987                 :            :         } else {
    1988                 :          0 :                 fib6_nh = from->fib6_nh;
    1989                 :            :         }
    1990                 :          0 :         fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
    1991                 :            : unlock:
    1992                 :            :         rcu_read_unlock();
    1993                 :          0 : }
    1994                 :            : 
    1995                 :          0 : static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
    1996                 :            :                                          struct rt6_info *rt, int mtu)
    1997                 :            : {
    1998                 :            :         /* If the new MTU is lower than the route PMTU, this new MTU will be the
    1999                 :            :          * lowest MTU in the path: always allow updating the route PMTU to
    2000                 :            :          * reflect PMTU decreases.
    2001                 :            :          *
    2002                 :            :          * If the new MTU is higher, and the route PMTU is equal to the local
    2003                 :            :          * MTU, this means the old MTU is the lowest in the path, so allow
    2004                 :            :          * updating it: if other nodes now have lower MTUs, PMTU discovery will
    2005                 :            :          * handle this.
    2006                 :            :          */
    2007                 :            : 
    2008         [ #  # ]:          0 :         if (dst_mtu(&rt->dst) >= mtu)
    2009                 :            :                 return true;
    2010                 :            : 
    2011         [ #  # ]:          0 :         if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
    2012                 :            :                 return true;
    2013                 :            : 
    2014                 :          0 :         return false;
    2015                 :            : }
    2016                 :            : 
    2017                 :          0 : static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
    2018                 :            :                                        const struct fib6_nh *nh, int mtu)
    2019                 :            : {
    2020                 :            :         struct rt6_exception_bucket *bucket;
    2021                 :            :         struct rt6_exception *rt6_ex;
    2022                 :            :         int i;
    2023                 :            : 
    2024                 :            :         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
    2025         [ #  # ]:          0 :         if (!bucket)
    2026                 :          0 :                 return;
    2027                 :            : 
    2028         [ #  # ]:          0 :         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
    2029   [ #  #  #  #  :          0 :                 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
                   #  # ]
    2030                 :          0 :                         struct rt6_info *entry = rt6_ex->rt6i;
    2031                 :            : 
    2032                 :            :                         /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
    2033                 :            :                          * route), the metrics of its rt->from have already
    2034                 :            :                          * been updated.
    2035                 :            :                          */
    2036   [ #  #  #  # ]:          0 :                         if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
    2037                 :          0 :                             rt6_mtu_change_route_allowed(idev, entry, mtu))
    2038                 :          0 :                                 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
    2039                 :            :                 }
    2040                 :          0 :                 bucket++;
    2041                 :            :         }
    2042                 :            : }
    2043                 :            : 
    2044                 :            : #define RTF_CACHE_GATEWAY       (RTF_GATEWAY | RTF_CACHE)
    2045                 :            : 
    2046                 :          0 : static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
    2047                 :            :                                             const struct in6_addr *gateway)
    2048                 :            : {
    2049                 :            :         struct rt6_exception_bucket *bucket;
    2050                 :            :         struct rt6_exception *rt6_ex;
    2051                 :            :         struct hlist_node *tmp;
    2052                 :            :         int i;
    2053                 :            : 
    2054         [ #  # ]:          0 :         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
    2055                 :          0 :                 return;
    2056                 :            : 
    2057                 :            :         spin_lock_bh(&rt6_exception_lock);
    2058                 :            :         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
    2059         [ #  # ]:          0 :         if (bucket) {
    2060         [ #  # ]:          0 :                 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
    2061   [ #  #  #  #  :          0 :                         hlist_for_each_entry_safe(rt6_ex, tmp,
                   #  # ]
    2062                 :            :                                                   &bucket->chain, hlist) {
    2063                 :          0 :                                 struct rt6_info *entry = rt6_ex->rt6i;
    2064                 :            : 
    2065         [ #  # ]:          0 :                                 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
    2066         [ #  # ]:          0 :                                     RTF_CACHE_GATEWAY &&
    2067                 :            :                                     ipv6_addr_equal(gateway,
    2068                 :            :                                                     &entry->rt6i_gateway)) {
    2069                 :          0 :                                         rt6_remove_exception(bucket, rt6_ex);
    2070                 :            :                                 }
    2071                 :            :                         }
    2072                 :          0 :                         bucket++;
    2073                 :            :                 }
    2074                 :            :         }
    2075                 :            : 
    2076                 :            :         spin_unlock_bh(&rt6_exception_lock);
    2077                 :            : }
    2078                 :            : 
    2079                 :          0 : static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
    2080                 :            :                                       struct rt6_exception *rt6_ex,
    2081                 :            :                                       struct fib6_gc_args *gc_args,
    2082                 :            :                                       unsigned long now)
    2083                 :            : {
    2084                 :          0 :         struct rt6_info *rt = rt6_ex->rt6i;
    2085                 :            : 
    2086                 :            :         /* we are pruning and obsoleting aged-out and non gateway exceptions
    2087                 :            :          * even if others have still references to them, so that on next
    2088                 :            :          * dst_check() such references can be dropped.
    2089                 :            :          * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
    2090                 :            :          * expired, independently from their aging, as per RFC 8201 section 4
    2091                 :            :          */
    2092         [ #  # ]:          0 :         if (!(rt->rt6i_flags & RTF_EXPIRES)) {
    2093         [ #  # ]:          0 :                 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
    2094                 :            :                         RT6_TRACE("aging clone %p\n", rt);
    2095                 :          0 :                         rt6_remove_exception(bucket, rt6_ex);
    2096                 :          0 :                         return;
    2097                 :            :                 }
    2098         [ #  # ]:          0 :         } else if (time_after(jiffies, rt->dst.expires)) {
    2099                 :            :                 RT6_TRACE("purging expired route %p\n", rt);
    2100                 :          0 :                 rt6_remove_exception(bucket, rt6_ex);
    2101                 :          0 :                 return;
    2102                 :            :         }
    2103                 :            : 
    2104         [ #  # ]:          0 :         if (rt->rt6i_flags & RTF_GATEWAY) {
    2105                 :            :                 struct neighbour *neigh;
    2106                 :            :                 __u8 neigh_flags = 0;
    2107                 :            : 
    2108                 :          0 :                 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
    2109         [ #  # ]:          0 :                 if (neigh)
    2110                 :          0 :                         neigh_flags = neigh->flags;
    2111                 :            : 
    2112         [ #  # ]:          0 :                 if (!(neigh_flags & NTF_ROUTER)) {
    2113                 :            :                         RT6_TRACE("purging route %p via non-router but gateway\n",
    2114                 :            :                                   rt);
    2115                 :          0 :                         rt6_remove_exception(bucket, rt6_ex);
    2116                 :          0 :                         return;
    2117                 :            :                 }
    2118                 :            :         }
    2119                 :            : 
    2120                 :          0 :         gc_args->more++;
    2121                 :            : }
    2122                 :            : 
    2123                 :          0 : static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
    2124                 :            :                                    struct fib6_gc_args *gc_args,
    2125                 :            :                                    unsigned long now)
    2126                 :            : {
    2127                 :            :         struct rt6_exception_bucket *bucket;
    2128                 :            :         struct rt6_exception *rt6_ex;
    2129                 :            :         struct hlist_node *tmp;
    2130                 :            :         int i;
    2131                 :            : 
    2132         [ #  # ]:          0 :         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
    2133                 :          0 :                 return;
    2134                 :            : 
    2135                 :            :         rcu_read_lock_bh();
    2136                 :            :         spin_lock(&rt6_exception_lock);
    2137                 :            :         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
    2138         [ #  # ]:          0 :         if (bucket) {
    2139         [ #  # ]:          0 :                 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
    2140   [ #  #  #  #  :          0 :                         hlist_for_each_entry_safe(rt6_ex, tmp,
                   #  # ]
    2141                 :            :                                                   &bucket->chain, hlist) {
    2142                 :          0 :                                 rt6_age_examine_exception(bucket, rt6_ex,
    2143                 :            :                                                           gc_args, now);
    2144                 :            :                         }
    2145                 :          0 :                         bucket++;
    2146                 :            :                 }
    2147                 :            :         }
    2148                 :            :         spin_unlock(&rt6_exception_lock);
    2149                 :            :         rcu_read_unlock_bh();
    2150                 :            : }
    2151                 :            : 
    2152                 :            : struct fib6_nh_age_excptn_arg {
    2153                 :            :         struct fib6_gc_args     *gc_args;
    2154                 :            :         unsigned long           now;
    2155                 :            : };
    2156                 :            : 
    2157                 :          0 : static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
    2158                 :            : {
    2159                 :            :         struct fib6_nh_age_excptn_arg *arg = _arg;
    2160                 :            : 
    2161                 :          0 :         fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
    2162                 :          0 :         return 0;
    2163                 :            : }
    2164                 :            : 
    2165                 :          0 : void rt6_age_exceptions(struct fib6_info *f6i,
    2166                 :            :                         struct fib6_gc_args *gc_args,
    2167                 :            :                         unsigned long now)
    2168                 :            : {
    2169         [ #  # ]:          0 :         if (f6i->nh) {
    2170                 :          0 :                 struct fib6_nh_age_excptn_arg arg = {
    2171                 :            :                         .gc_args = gc_args,
    2172                 :            :                         .now = now
    2173                 :            :                 };
    2174                 :            : 
    2175                 :          0 :                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
    2176                 :            :                                          &arg);
    2177                 :            :         } else {
    2178                 :          0 :                 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
    2179                 :            :         }
    2180                 :          0 : }
    2181                 :            : 
    2182                 :            : /* must be called with rcu lock held */
    2183                 :       5092 : int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
    2184                 :            :                       struct flowi6 *fl6, struct fib6_result *res, int strict)
    2185                 :            : {
    2186                 :            :         struct fib6_node *fn, *saved_fn;
    2187                 :            : 
    2188                 :       5092 :         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
    2189                 :            :         saved_fn = fn;
    2190                 :            : 
    2191         [ -  + ]:       5092 :         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
    2192                 :            :                 oif = 0;
    2193                 :            : 
    2194                 :            : redo_rt6_select:
    2195                 :       8404 :         rt6_select(net, fn, oif, res, strict);
    2196         [ +  + ]:       8404 :         if (res->f6i == net->ipv6.fib6_null_entry) {
    2197                 :       6624 :                 fn = fib6_backtrack(fn, &fl6->saddr);
    2198         [ -  + ]:       6624 :                 if (fn)
    2199                 :            :                         goto redo_rt6_select;
    2200         [ +  + ]:       6624 :                 else if (strict & RT6_LOOKUP_F_REACHABLE) {
    2201                 :            :                         /* also consider unreachable route */
    2202                 :       3312 :                         strict &= ~RT6_LOOKUP_F_REACHABLE;
    2203                 :            :                         fn = saved_fn;
    2204                 :       3312 :                         goto redo_rt6_select;
    2205                 :            :                 }
    2206                 :            :         }
    2207                 :            : 
    2208                 :       5092 :         trace_fib6_table_lookup(net, res, table, fl6);
    2209                 :            : 
    2210                 :       5092 :         return 0;
    2211                 :            : }
    2212                 :            : 
    2213                 :       5092 : struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
    2214                 :            :                                int oif, struct flowi6 *fl6,
    2215                 :            :                                const struct sk_buff *skb, int flags)
    2216                 :            : {
    2217                 :       5092 :         struct fib6_result res = {};
    2218                 :       5092 :         struct rt6_info *rt = NULL;
    2219                 :            :         int strict = 0;
    2220                 :            : 
    2221                 :            :         WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
    2222                 :            :                      !rcu_read_lock_held());
    2223                 :            : 
    2224                 :       5092 :         strict |= flags & RT6_LOOKUP_F_IFACE;
    2225                 :       5092 :         strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
    2226         [ +  - ]:       5092 :         if (net->ipv6.devconf_all->forwarding == 0)
    2227                 :       5092 :                 strict |= RT6_LOOKUP_F_REACHABLE;
    2228                 :            : 
    2229                 :            :         rcu_read_lock();
    2230                 :            : 
    2231                 :       5092 :         fib6_table_lookup(net, table, oif, fl6, &res, strict);
    2232         [ +  + ]:       5092 :         if (res.f6i == net->ipv6.fib6_null_entry)
    2233                 :            :                 goto out;
    2234                 :            : 
    2235                 :       1780 :         fib6_select_path(net, &res, fl6, oif, false, skb, strict);
    2236                 :            : 
    2237                 :            :         /*Search through exception table */
    2238                 :       1780 :         rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
    2239         [ +  - ]:       1780 :         if (rt) {
    2240                 :            :                 goto out;
    2241   [ -  +  #  # ]:       1780 :         } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
    2242                 :            :                             !res.nh->fib_nh_gw_family)) {
    2243                 :            :                 /* Create a RTF_CACHE clone which will not be
    2244                 :            :                  * owned by the fib6 tree.  It is for the special case where
    2245                 :            :                  * the daddr in the skb during the neighbor look-up is different
    2246                 :            :                  * from the fl6->daddr used to look-up route here.
    2247                 :            :                  */
    2248                 :          0 :                 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
    2249                 :            : 
    2250         [ #  # ]:          0 :                 if (rt) {
    2251                 :            :                         /* 1 refcnt is taken during ip6_rt_cache_alloc().
    2252                 :            :                          * As rt6_uncached_list_add() does not consume refcnt,
    2253                 :            :                          * this refcnt is always returned to the caller even
    2254                 :            :                          * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
    2255                 :            :                          */
    2256                 :          0 :                         rt6_uncached_list_add(rt);
    2257                 :          0 :                         atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
    2258                 :            :                         rcu_read_unlock();
    2259                 :            : 
    2260                 :          0 :                         return rt;
    2261                 :            :                 }
    2262                 :            :         } else {
    2263                 :            :                 /* Get a percpu copy */
    2264                 :            :                 local_bh_disable();
    2265                 :       1780 :                 rt = rt6_get_pcpu_route(&res);
    2266                 :            : 
    2267         [ +  + ]:       1780 :                 if (!rt)
    2268                 :        510 :                         rt = rt6_make_pcpu_route(net, &res);
    2269                 :            : 
    2270                 :            :                 local_bh_enable();
    2271                 :            :         }
    2272                 :            : out:
    2273         [ +  + ]:       5092 :         if (!rt)
    2274                 :       3312 :                 rt = net->ipv6.ip6_null_entry;
    2275         [ -  + ]:       5092 :         if (!(flags & RT6_LOOKUP_F_DST_NOREF))
    2276                 :          0 :                 ip6_hold_safe(net, &rt);
    2277                 :            :         rcu_read_unlock();
    2278                 :            : 
    2279                 :       5092 :         return rt;
    2280                 :            : }
    2281                 :            : EXPORT_SYMBOL_GPL(ip6_pol_route);
    2282                 :            : 
    2283                 :        561 : static struct rt6_info *ip6_pol_route_input(struct net *net,
    2284                 :            :                                             struct fib6_table *table,
    2285                 :            :                                             struct flowi6 *fl6,
    2286                 :            :                                             const struct sk_buff *skb,
    2287                 :            :                                             int flags)
    2288                 :            : {
    2289                 :        561 :         return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
    2290                 :            : }
    2291                 :            : 
    2292                 :        561 : struct dst_entry *ip6_route_input_lookup(struct net *net,
    2293                 :            :                                          struct net_device *dev,
    2294                 :            :                                          struct flowi6 *fl6,
    2295                 :            :                                          const struct sk_buff *skb,
    2296                 :            :                                          int flags)
    2297                 :            : {
    2298   [ +  -  +  - ]:       1122 :         if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
    2299                 :        561 :                 flags |= RT6_LOOKUP_F_IFACE;
    2300                 :            : 
    2301                 :        561 :         return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
    2302                 :            : }
    2303                 :            : EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
    2304                 :            : 
    2305                 :          0 : static void ip6_multipath_l3_keys(const struct sk_buff *skb,
    2306                 :            :                                   struct flow_keys *keys,
    2307                 :            :                                   struct flow_keys *flkeys)
    2308                 :            : {
    2309                 :            :         const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
    2310                 :            :         const struct ipv6hdr *key_iph = outer_iph;
    2311                 :            :         struct flow_keys *_flkeys = flkeys;
    2312                 :            :         const struct ipv6hdr *inner_iph;
    2313                 :            :         const struct icmp6hdr *icmph;
    2314                 :            :         struct ipv6hdr _inner_iph;
    2315                 :            :         struct icmp6hdr _icmph;
    2316                 :            : 
    2317         [ #  # ]:          0 :         if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
    2318                 :            :                 goto out;
    2319                 :            : 
    2320                 :            :         icmph = skb_header_pointer(skb, skb_transport_offset(skb),
    2321                 :            :                                    sizeof(_icmph), &_icmph);
    2322         [ #  # ]:          0 :         if (!icmph)
    2323                 :            :                 goto out;
    2324                 :            : 
    2325         [ #  # ]:          0 :         if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
    2326                 :            :             icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
    2327                 :          0 :             icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
    2328                 :            :             icmph->icmp6_type != ICMPV6_PARAMPROB)
    2329                 :            :                 goto out;
    2330                 :            : 
    2331                 :          0 :         inner_iph = skb_header_pointer(skb,
    2332                 :          0 :                                        skb_transport_offset(skb) + sizeof(*icmph),
    2333                 :            :                                        sizeof(_inner_iph), &_inner_iph);
    2334         [ #  # ]:          0 :         if (!inner_iph)
    2335                 :            :                 goto out;
    2336                 :            : 
    2337                 :            :         key_iph = inner_iph;
    2338                 :            :         _flkeys = NULL;
    2339                 :            : out:
    2340         [ #  # ]:          0 :         if (_flkeys) {
    2341                 :          0 :                 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
    2342                 :          0 :                 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
    2343                 :          0 :                 keys->tags.flow_label = _flkeys->tags.flow_label;
    2344                 :          0 :                 keys->basic.ip_proto = _flkeys->basic.ip_proto;
    2345                 :            :         } else {
    2346                 :          0 :                 keys->addrs.v6addrs.src = key_iph->saddr;
    2347                 :          0 :                 keys->addrs.v6addrs.dst = key_iph->daddr;
    2348                 :          0 :                 keys->tags.flow_label = ip6_flowlabel(key_iph);
    2349                 :          0 :                 keys->basic.ip_proto = key_iph->nexthdr;
    2350                 :            :         }
    2351                 :          0 : }
    2352                 :            : 
    2353                 :            : /* if skb is set it will be used and fl6 can be NULL */
    2354                 :          0 : u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
    2355                 :            :                        const struct sk_buff *skb, struct flow_keys *flkeys)
    2356                 :            : {
    2357                 :            :         struct flow_keys hash_keys;
    2358                 :            :         u32 mhash;
    2359                 :            : 
    2360   [ #  #  #  # ]:          0 :         switch (ip6_multipath_hash_policy(net)) {
    2361                 :            :         case 0:
    2362                 :          0 :                 memset(&hash_keys, 0, sizeof(hash_keys));
    2363                 :          0 :                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2364         [ #  # ]:          0 :                 if (skb) {
    2365                 :          0 :                         ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
    2366                 :            :                 } else {
    2367                 :          0 :                         hash_keys.addrs.v6addrs.src = fl6->saddr;
    2368                 :          0 :                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
    2369                 :          0 :                         hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
    2370                 :          0 :                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
    2371                 :            :                 }
    2372                 :            :                 break;
    2373                 :            :         case 1:
    2374         [ #  # ]:          0 :                 if (skb) {
    2375                 :            :                         unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
    2376                 :            :                         struct flow_keys keys;
    2377                 :            : 
    2378                 :            :                         /* short-circuit if we already have L4 hash present */
    2379         [ #  # ]:          0 :                         if (skb->l4_hash)
    2380                 :          0 :                                 return skb_get_hash_raw(skb) >> 1;
    2381                 :            : 
    2382                 :          0 :                         memset(&hash_keys, 0, sizeof(hash_keys));
    2383                 :            : 
    2384         [ #  # ]:          0 :                         if (!flkeys) {
    2385                 :          0 :                                 skb_flow_dissect_flow_keys(skb, &keys, flag);
    2386                 :            :                                 flkeys = &keys;
    2387                 :            :                         }
    2388                 :          0 :                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2389                 :          0 :                         hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
    2390                 :          0 :                         hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
    2391                 :          0 :                         hash_keys.ports.src = flkeys->ports.src;
    2392                 :          0 :                         hash_keys.ports.dst = flkeys->ports.dst;
    2393                 :          0 :                         hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
    2394                 :            :                 } else {
    2395                 :          0 :                         memset(&hash_keys, 0, sizeof(hash_keys));
    2396                 :          0 :                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2397                 :          0 :                         hash_keys.addrs.v6addrs.src = fl6->saddr;
    2398                 :          0 :                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
    2399                 :          0 :                         hash_keys.ports.src = fl6->fl6_sport;
    2400                 :          0 :                         hash_keys.ports.dst = fl6->fl6_dport;
    2401                 :          0 :                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
    2402                 :            :                 }
    2403                 :            :                 break;
    2404                 :            :         case 2:
    2405                 :          0 :                 memset(&hash_keys, 0, sizeof(hash_keys));
    2406                 :          0 :                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2407         [ #  # ]:          0 :                 if (skb) {
    2408                 :            :                         struct flow_keys keys;
    2409                 :            : 
    2410         [ #  # ]:          0 :                         if (!flkeys) {
    2411                 :          0 :                                 skb_flow_dissect_flow_keys(skb, &keys, 0);
    2412                 :            :                                 flkeys = &keys;
    2413                 :            :                         }
    2414                 :            : 
    2415                 :            :                         /* Inner can be v4 or v6 */
    2416         [ #  # ]:          0 :                         if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
    2417                 :          0 :                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
    2418                 :          0 :                                 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
    2419                 :          0 :                                 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
    2420         [ #  # ]:          0 :                         } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
    2421                 :          0 :                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2422                 :          0 :                                 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
    2423                 :          0 :                                 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
    2424                 :          0 :                                 hash_keys.tags.flow_label = flkeys->tags.flow_label;
    2425                 :          0 :                                 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
    2426                 :            :                         } else {
    2427                 :            :                                 /* Same as case 0 */
    2428                 :          0 :                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2429                 :          0 :                                 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
    2430                 :            :                         }
    2431                 :            :                 } else {
    2432                 :            :                         /* Same as case 0 */
    2433                 :            :                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
    2434                 :          0 :                         hash_keys.addrs.v6addrs.src = fl6->saddr;
    2435                 :          0 :                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
    2436                 :          0 :                         hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
    2437                 :          0 :                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
    2438                 :            :                 }
    2439                 :            :                 break;
    2440                 :            :         }
    2441                 :          0 :         mhash = flow_hash_from_keys(&hash_keys);
    2442                 :            : 
    2443                 :          0 :         return mhash >> 1;
    2444                 :            : }
    2445                 :            : 
    2446                 :            : /* Called with rcu held */
    2447                 :        561 : void ip6_route_input(struct sk_buff *skb)
    2448                 :            : {
    2449                 :            :         const struct ipv6hdr *iph = ipv6_hdr(skb);
    2450                 :        561 :         struct net *net = dev_net(skb->dev);
    2451                 :            :         int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
    2452                 :            :         struct ip_tunnel_info *tun_info;
    2453                 :       2805 :         struct flowi6 fl6 = {
    2454                 :        561 :                 .flowi6_iif = skb->dev->ifindex,
    2455                 :            :                 .daddr = iph->daddr,
    2456                 :            :                 .saddr = iph->saddr,
    2457                 :            :                 .flowlabel = ip6_flowinfo(iph),
    2458                 :        561 :                 .flowi6_mark = skb->mark,
    2459                 :        561 :                 .flowi6_proto = iph->nexthdr,
    2460                 :            :         };
    2461                 :            :         struct flow_keys *flkeys = NULL, _flkeys;
    2462                 :            : 
    2463                 :        561 :         tun_info = skb_tunnel_info(skb);
    2464   [ -  +  #  # ]:        561 :         if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
    2465                 :          0 :                 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
    2466                 :            : 
    2467         [ -  + ]:        561 :         if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
    2468                 :            :                 flkeys = &_flkeys;
    2469                 :            : 
    2470         [ -  + ]:        561 :         if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
    2471                 :          0 :                 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
    2472                 :        561 :         skb_dst_drop(skb);
    2473                 :        561 :         skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
    2474                 :            :                                                       &fl6, skb, flags));
    2475                 :        561 : }
    2476                 :            : 
    2477                 :       4531 : static struct rt6_info *ip6_pol_route_output(struct net *net,
    2478                 :            :                                              struct fib6_table *table,
    2479                 :            :                                              struct flowi6 *fl6,
    2480                 :            :                                              const struct sk_buff *skb,
    2481                 :            :                                              int flags)
    2482                 :            : {
    2483                 :       4531 :         return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
    2484                 :            : }
    2485                 :            : 
    2486                 :       2875 : struct dst_entry *ip6_route_output_flags_noref(struct net *net,
    2487                 :            :                                                const struct sock *sk,
    2488                 :            :                                                struct flowi6 *fl6, int flags)
    2489                 :            : {
    2490                 :            :         bool any_src;
    2491                 :            : 
    2492         [ +  + ]:       5750 :         if (ipv6_addr_type(&fl6->daddr) &
    2493                 :            :             (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
    2494                 :            :                 struct dst_entry *dst;
    2495                 :            : 
    2496                 :            :                 /* This function does not take refcnt on the dst */
    2497                 :       1219 :                 dst = l3mdev_link_scope_lookup(net, fl6);
    2498         [ +  - ]:       1219 :                 if (dst)
    2499                 :            :                         return dst;
    2500                 :            :         }
    2501                 :            : 
    2502                 :       2875 :         fl6->flowi6_iif = LOOPBACK_IFINDEX;
    2503                 :            : 
    2504                 :       2875 :         flags |= RT6_LOOKUP_F_DST_NOREF;
    2505                 :            :         any_src = ipv6_addr_any(&fl6->saddr);
    2506   [ +  -  +  -  :       7406 :         if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
             +  +  -  + ]
    2507         [ #  # ]:       1656 :             (fl6->flowi6_oif && any_src))
    2508                 :       1219 :                 flags |= RT6_LOOKUP_F_IFACE;
    2509                 :            : 
    2510         [ +  + ]:       2875 :         if (!any_src)
    2511                 :       1216 :                 flags |= RT6_LOOKUP_F_HAS_SADDR;
    2512         [ +  - ]:       1659 :         else if (sk)
    2513                 :       3318 :                 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
    2514                 :            : 
    2515                 :       2875 :         return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
    2516                 :            : }
    2517                 :            : EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
    2518                 :            : 
    2519                 :       2875 : struct dst_entry *ip6_route_output_flags(struct net *net,
    2520                 :            :                                          const struct sock *sk,
    2521                 :            :                                          struct flowi6 *fl6,
    2522                 :            :                                          int flags)
    2523                 :            : {
    2524                 :            :         struct dst_entry *dst;
    2525                 :            :         struct rt6_info *rt6;
    2526                 :            : 
    2527                 :            :         rcu_read_lock();
    2528                 :       2875 :         dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
    2529                 :            :         rt6 = (struct rt6_info *)dst;
    2530                 :            :         /* For dst cached in uncached_list, refcnt is already taken. */
    2531   [ +  -  -  + ]:       8625 :         if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
    2532                 :          0 :                 dst = &net->ipv6.ip6_null_entry->dst;
    2533                 :          0 :                 dst_hold(dst);
    2534                 :            :         }
    2535                 :            :         rcu_read_unlock();
    2536                 :            : 
    2537                 :       2875 :         return dst;
    2538                 :            : }
    2539                 :            : EXPORT_SYMBOL_GPL(ip6_route_output_flags);
    2540                 :            : 
    2541                 :          0 : struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
    2542                 :            : {
    2543                 :            :         struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
    2544                 :          0 :         struct net_device *loopback_dev = net->loopback_dev;
    2545                 :            :         struct dst_entry *new = NULL;
    2546                 :            : 
    2547                 :          0 :         rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
    2548                 :            :                        DST_OBSOLETE_DEAD, 0);
    2549         [ #  # ]:          0 :         if (rt) {
    2550                 :            :                 rt6_info_init(rt);
    2551                 :          0 :                 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
    2552                 :            : 
    2553                 :          0 :                 new = &rt->dst;
    2554                 :          0 :                 new->__use = 1;
    2555                 :          0 :                 new->input = dst_discard;
    2556                 :          0 :                 new->output = dst_discard_out;
    2557                 :            : 
    2558                 :          0 :                 dst_copy_metrics(new, &ort->dst);
    2559                 :            : 
    2560                 :          0 :                 rt->rt6i_idev = in6_dev_get(loopback_dev);
    2561                 :          0 :                 rt->rt6i_gateway = ort->rt6i_gateway;
    2562                 :          0 :                 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
    2563                 :            : 
    2564                 :          0 :                 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
    2565                 :            : #ifdef CONFIG_IPV6_SUBTREES
    2566                 :          0 :                 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
    2567                 :            : #endif
    2568                 :            :         }
    2569                 :            : 
    2570                 :          0 :         dst_release(dst_orig);
    2571         [ #  # ]:          0 :         return new ? new : ERR_PTR(-ENOMEM);
    2572                 :            : }
    2573                 :            : 
    2574                 :            : /*
    2575                 :            :  *      Destination cache support functions
    2576                 :            :  */
    2577                 :            : 
    2578                 :          0 : static bool fib6_check(struct fib6_info *f6i, u32 cookie)
    2579                 :            : {
    2580                 :            :         u32 rt_cookie = 0;
    2581                 :            : 
    2582   [ #  #  #  # ]:          0 :         if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
    2583                 :            :                 return false;
    2584                 :            : 
    2585         [ #  # ]:          0 :         if (fib6_check_expired(f6i))
    2586                 :            :                 return false;
    2587                 :            : 
    2588                 :          0 :         return true;
    2589                 :            : }
    2590                 :            : 
    2591                 :          0 : static struct dst_entry *rt6_check(struct rt6_info *rt,
    2592                 :            :                                    struct fib6_info *from,
    2593                 :            :                                    u32 cookie)
    2594                 :            : {
    2595                 :            :         u32 rt_cookie = 0;
    2596                 :            : 
    2597   [ #  #  #  #  :          0 :         if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
                   #  # ]
    2598                 :            :             rt_cookie != cookie)
    2599                 :            :                 return NULL;
    2600                 :            : 
    2601         [ #  # ]:          0 :         if (rt6_check_expired(rt))
    2602                 :            :                 return NULL;
    2603                 :            : 
    2604                 :          0 :         return &rt->dst;
    2605                 :            : }
    2606                 :            : 
    2607                 :          0 : static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
    2608                 :            :                                             struct fib6_info *from,
    2609                 :            :                                             u32 cookie)
    2610                 :            : {
    2611   [ #  #  #  # ]:          0 :         if (!__rt6_check_expired(rt) &&
    2612         [ #  # ]:          0 :             rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
    2613                 :          0 :             fib6_check(from, cookie))
    2614                 :          0 :                 return &rt->dst;
    2615                 :            :         else
    2616                 :            :                 return NULL;
    2617                 :            : }
    2618                 :            : 
    2619                 :          0 : static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
    2620                 :            : {
    2621                 :            :         struct dst_entry *dst_ret;
    2622                 :            :         struct fib6_info *from;
    2623                 :            :         struct rt6_info *rt;
    2624                 :            : 
    2625                 :            :         rt = container_of(dst, struct rt6_info, dst);
    2626                 :            : 
    2627         [ #  # ]:          0 :         if (rt->sernum)
    2628         [ #  # ]:          0 :                 return rt6_is_valid(rt) ? dst : NULL;
    2629                 :            : 
    2630                 :            :         rcu_read_lock();
    2631                 :            : 
    2632                 :            :         /* All IPV6 dsts are created with ->obsolete set to the value
    2633                 :            :          * DST_OBSOLETE_FORCE_CHK which forces validation calls down
    2634                 :            :          * into this function always.
    2635                 :            :          */
    2636                 :            : 
    2637                 :          0 :         from = rcu_dereference(rt->from);
    2638                 :            : 
    2639   [ #  #  #  #  :          0 :         if (from && (rt->rt6i_flags & RTF_PCPU ||
                   #  # ]
    2640                 :          0 :             unlikely(!list_empty(&rt->rt6i_uncached))))
    2641                 :          0 :                 dst_ret = rt6_dst_from_check(rt, from, cookie);
    2642                 :            :         else
    2643                 :          0 :                 dst_ret = rt6_check(rt, from, cookie);
    2644                 :            : 
    2645                 :            :         rcu_read_unlock();
    2646                 :            : 
    2647                 :          0 :         return dst_ret;
    2648                 :            : }
    2649                 :            : 
    2650                 :          0 : static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
    2651                 :            : {
    2652                 :            :         struct rt6_info *rt = (struct rt6_info *) dst;
    2653                 :            : 
    2654         [ #  # ]:          0 :         if (rt) {
    2655         [ #  # ]:          0 :                 if (rt->rt6i_flags & RTF_CACHE) {
    2656                 :            :                         rcu_read_lock();
    2657         [ #  # ]:          0 :                         if (rt6_check_expired(rt)) {
    2658                 :          0 :                                 rt6_remove_exception_rt(rt);
    2659                 :            :                                 dst = NULL;
    2660                 :            :                         }
    2661                 :            :                         rcu_read_unlock();
    2662                 :            :                 } else {
    2663                 :          0 :                         dst_release(dst);
    2664                 :            :                         dst = NULL;
    2665                 :            :                 }
    2666                 :            :         }
    2667                 :          0 :         return dst;
    2668                 :            : }
    2669                 :            : 
    2670                 :          0 : static void ip6_link_failure(struct sk_buff *skb)
    2671                 :            : {
    2672                 :            :         struct rt6_info *rt;
    2673                 :            : 
    2674                 :          0 :         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
    2675                 :            : 
    2676                 :            :         rt = (struct rt6_info *) skb_dst(skb);
    2677         [ #  # ]:          0 :         if (rt) {
    2678                 :            :                 rcu_read_lock();
    2679         [ #  # ]:          0 :                 if (rt->rt6i_flags & RTF_CACHE) {
    2680                 :          0 :                         rt6_remove_exception_rt(rt);
    2681                 :            :                 } else {
    2682                 :            :                         struct fib6_info *from;
    2683                 :            :                         struct fib6_node *fn;
    2684                 :            : 
    2685                 :          0 :                         from = rcu_dereference(rt->from);
    2686         [ #  # ]:          0 :                         if (from) {
    2687                 :          0 :                                 fn = rcu_dereference(from->fib6_node);
    2688   [ #  #  #  # ]:          0 :                                 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
    2689                 :          0 :                                         fn->fn_sernum = -1;
    2690                 :            :                         }
    2691                 :            :                 }
    2692                 :            :                 rcu_read_unlock();
    2693                 :            :         }
    2694                 :          0 : }
    2695                 :            : 
    2696                 :          0 : static void rt6_update_expires(struct rt6_info *rt0, int timeout)
    2697                 :            : {
    2698         [ #  # ]:          0 :         if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
    2699                 :            :                 struct fib6_info *from;
    2700                 :            : 
    2701                 :            :                 rcu_read_lock();
    2702                 :          0 :                 from = rcu_dereference(rt0->from);
    2703         [ #  # ]:          0 :                 if (from)
    2704                 :          0 :                         rt0->dst.expires = from->expires;
    2705                 :            :                 rcu_read_unlock();
    2706                 :            :         }
    2707                 :            : 
    2708                 :            :         dst_set_expires(&rt0->dst, timeout);
    2709                 :          0 :         rt0->rt6i_flags |= RTF_EXPIRES;
    2710                 :          0 : }
    2711                 :            : 
    2712                 :          0 : static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
    2713                 :            : {
    2714                 :          0 :         struct net *net = dev_net(rt->dst.dev);
    2715                 :            : 
    2716                 :          0 :         dst_metric_set(&rt->dst, RTAX_MTU, mtu);
    2717                 :          0 :         rt->rt6i_flags |= RTF_MODIFIED;
    2718                 :          0 :         rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
    2719                 :          0 : }
    2720                 :            : 
    2721                 :            : static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
    2722                 :            : {
    2723   [ #  #  #  # ]:          0 :         return !(rt->rt6i_flags & RTF_CACHE) &&
    2724         [ #  # ]:          0 :                 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
    2725                 :            : }
    2726                 :            : 
    2727                 :          0 : static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
    2728                 :            :                                  const struct ipv6hdr *iph, u32 mtu,
    2729                 :            :                                  bool confirm_neigh)
    2730                 :            : {
    2731                 :            :         const struct in6_addr *daddr, *saddr;
    2732                 :            :         struct rt6_info *rt6 = (struct rt6_info *)dst;
    2733                 :            : 
    2734                 :            :         /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
    2735                 :            :          * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
    2736                 :            :          * [see also comment in rt6_mtu_change_route()]
    2737                 :            :          */
    2738                 :            : 
    2739         [ #  # ]:          0 :         if (iph) {
    2740                 :          0 :                 daddr = &iph->daddr;
    2741                 :          0 :                 saddr = &iph->saddr;
    2742         [ #  # ]:          0 :         } else if (sk) {
    2743                 :          0 :                 daddr = &sk->sk_v6_daddr;
    2744                 :          0 :                 saddr = &inet6_sk(sk)->saddr;
    2745                 :            :         } else {
    2746                 :            :                 daddr = NULL;
    2747                 :            :                 saddr = NULL;
    2748                 :            :         }
    2749                 :            : 
    2750         [ #  # ]:          0 :         if (confirm_neigh)
    2751                 :            :                 dst_confirm_neigh(dst, daddr);
    2752                 :            : 
    2753                 :          0 :         mtu = max_t(u32, mtu, IPV6_MIN_MTU);
    2754         [ #  # ]:          0 :         if (mtu >= dst_mtu(dst))
    2755                 :          0 :                 return;
    2756                 :            : 
    2757         [ #  # ]:          0 :         if (!rt6_cache_allowed_for_pmtu(rt6)) {
    2758                 :          0 :                 rt6_do_update_pmtu(rt6, mtu);
    2759                 :            :                 /* update rt6_ex->stamp for cache */
    2760         [ #  # ]:          0 :                 if (rt6->rt6i_flags & RTF_CACHE)
    2761                 :          0 :                         rt6_update_exception_stamp_rt(rt6);
    2762         [ #  # ]:          0 :         } else if (daddr) {
    2763                 :          0 :                 struct fib6_result res = {};
    2764                 :            :                 struct rt6_info *nrt6;
    2765                 :            : 
    2766                 :            :                 rcu_read_lock();
    2767                 :          0 :                 res.f6i = rcu_dereference(rt6->from);
    2768         [ #  # ]:          0 :                 if (!res.f6i)
    2769                 :            :                         goto out_unlock;
    2770                 :            : 
    2771                 :          0 :                 res.fib6_flags = res.f6i->fib6_flags;
    2772                 :          0 :                 res.fib6_type = res.f6i->fib6_type;
    2773                 :            : 
    2774         [ #  # ]:          0 :                 if (res.f6i->nh) {
    2775                 :          0 :                         struct fib6_nh_match_arg arg = {
    2776                 :          0 :                                 .dev = dst->dev,
    2777                 :          0 :                                 .gw = &rt6->rt6i_gateway,
    2778                 :            :                         };
    2779                 :            : 
    2780                 :          0 :                         nexthop_for_each_fib6_nh(res.f6i->nh,
    2781                 :            :                                                  fib6_nh_find_match, &arg);
    2782                 :            : 
    2783                 :            :                         /* fib6_info uses a nexthop that does not have fib6_nh
    2784                 :            :                          * using the dst->dev + gw. Should be impossible.
    2785                 :            :                          */
    2786         [ #  # ]:          0 :                         if (!arg.match)
    2787                 :            :                                 goto out_unlock;
    2788                 :            : 
    2789                 :          0 :                         res.nh = arg.match;
    2790                 :            :                 } else {
    2791                 :          0 :                         res.nh = res.f6i->fib6_nh;
    2792                 :            :                 }
    2793                 :            : 
    2794                 :          0 :                 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
    2795         [ #  # ]:          0 :                 if (nrt6) {
    2796                 :          0 :                         rt6_do_update_pmtu(nrt6, mtu);
    2797         [ #  # ]:          0 :                         if (rt6_insert_exception(nrt6, &res))
    2798                 :          0 :                                 dst_release_immediate(&nrt6->dst);
    2799                 :            :                 }
    2800                 :            : out_unlock:
    2801                 :            :                 rcu_read_unlock();
    2802                 :            :         }
    2803                 :            : }
    2804                 :            : 
    2805                 :          0 : static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
    2806                 :            :                                struct sk_buff *skb, u32 mtu,
    2807                 :            :                                bool confirm_neigh)
    2808                 :            : {
    2809         [ #  # ]:          0 :         __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
    2810                 :            :                              confirm_neigh);
    2811                 :          0 : }
    2812                 :            : 
    2813                 :          0 : void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
    2814                 :            :                      int oif, u32 mark, kuid_t uid)
    2815                 :            : {
    2816                 :          0 :         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
    2817                 :            :         struct dst_entry *dst;
    2818                 :          0 :         struct flowi6 fl6 = {
    2819                 :            :                 .flowi6_oif = oif,
    2820   [ #  #  #  # ]:          0 :                 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
    2821                 :            :                 .daddr = iph->daddr,
    2822                 :            :                 .saddr = iph->saddr,
    2823                 :            :                 .flowlabel = ip6_flowinfo(iph),
    2824                 :            :                 .flowi6_uid = uid,
    2825                 :            :         };
    2826                 :            : 
    2827                 :            :         dst = ip6_route_output(net, NULL, &fl6);
    2828         [ #  # ]:          0 :         if (!dst->error)
    2829                 :          0 :                 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
    2830                 :          0 :         dst_release(dst);
    2831                 :          0 : }
    2832                 :            : EXPORT_SYMBOL_GPL(ip6_update_pmtu);
    2833                 :            : 
    2834                 :          0 : void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
    2835                 :            : {
    2836                 :          0 :         int oif = sk->sk_bound_dev_if;
    2837                 :            :         struct dst_entry *dst;
    2838                 :            : 
    2839   [ #  #  #  # ]:          0 :         if (!oif && skb->dev)
    2840                 :            :                 oif = l3mdev_master_ifindex(skb->dev);
    2841                 :            : 
    2842                 :          0 :         ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
    2843                 :            : 
    2844                 :            :         dst = __sk_dst_get(sk);
    2845   [ #  #  #  #  :          0 :         if (!dst || !dst->obsolete ||
                   #  # ]
    2846                 :          0 :             dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
    2847                 :          0 :                 return;
    2848                 :            : 
    2849                 :            :         bh_lock_sock(sk);
    2850   [ #  #  #  # ]:          0 :         if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
    2851                 :          0 :                 ip6_datagram_dst_update(sk, false);
    2852                 :            :         bh_unlock_sock(sk);
    2853                 :            : }
    2854                 :            : EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
    2855                 :            : 
    2856                 :          0 : void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
    2857                 :            :                            const struct flowi6 *fl6)
    2858                 :            : {
    2859                 :            : #ifdef CONFIG_IPV6_SUBTREES
    2860                 :            :         struct ipv6_pinfo *np = inet6_sk(sk);
    2861                 :            : #endif
    2862                 :            : 
    2863   [ #  #  #  # ]:          0 :         ip6_dst_store(sk, dst,
    2864                 :            :                       ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
    2865                 :            :                       &sk->sk_v6_daddr : NULL,
    2866                 :            : #ifdef CONFIG_IPV6_SUBTREES
    2867                 :            :                       ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
    2868                 :            :                       &np->saddr :
    2869                 :            : #endif
    2870                 :            :                       NULL);
    2871                 :          0 : }
    2872                 :            : 
    2873                 :          0 : static bool ip6_redirect_nh_match(const struct fib6_result *res,
    2874                 :            :                                   struct flowi6 *fl6,
    2875                 :            :                                   const struct in6_addr *gw,
    2876                 :            :                                   struct rt6_info **ret)
    2877                 :            : {
    2878                 :          0 :         const struct fib6_nh *nh = res->nh;
    2879                 :            : 
    2880   [ #  #  #  #  :          0 :         if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
                   #  # ]
    2881                 :          0 :             fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
    2882                 :            :                 return false;
    2883                 :            : 
    2884                 :            :         /* rt_cache's gateway might be different from its 'parent'
    2885                 :            :          * in the case of an ip redirect.
    2886                 :            :          * So we keep searching in the exception table if the gateway
    2887                 :            :          * is different.
    2888                 :            :          */
    2889         [ #  # ]:          0 :         if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
    2890                 :            :                 struct rt6_info *rt_cache;
    2891                 :            : 
    2892                 :          0 :                 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
    2893   [ #  #  #  # ]:          0 :                 if (rt_cache &&
    2894                 :            :                     ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
    2895                 :          0 :                         *ret = rt_cache;
    2896                 :          0 :                         return true;
    2897                 :            :                 }
    2898                 :            :                 return false;
    2899                 :            :         }
    2900                 :            :         return true;
    2901                 :            : }
    2902                 :            : 
    2903                 :            : struct fib6_nh_rd_arg {
    2904                 :            :         struct fib6_result      *res;
    2905                 :            :         struct flowi6           *fl6;
    2906                 :            :         const struct in6_addr   *gw;
    2907                 :            :         struct rt6_info         **ret;
    2908                 :            : };
    2909                 :            : 
    2910                 :          0 : static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
    2911                 :            : {
    2912                 :            :         struct fib6_nh_rd_arg *arg = _arg;
    2913                 :            : 
    2914                 :          0 :         arg->res->nh = nh;
    2915                 :          0 :         return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
    2916                 :            : }
    2917                 :            : 
    2918                 :            : /* Handle redirects */
    2919                 :            : struct ip6rd_flowi {
    2920                 :            :         struct flowi6 fl6;
    2921                 :            :         struct in6_addr gateway;
    2922                 :            : };
    2923                 :            : 
    2924                 :          0 : static struct rt6_info *__ip6_route_redirect(struct net *net,
    2925                 :            :                                              struct fib6_table *table,
    2926                 :            :                                              struct flowi6 *fl6,
    2927                 :            :                                              const struct sk_buff *skb,
    2928                 :            :                                              int flags)
    2929                 :            : {
    2930                 :            :         struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
    2931                 :          0 :         struct rt6_info *ret = NULL;
    2932                 :          0 :         struct fib6_result res = {};
    2933                 :          0 :         struct fib6_nh_rd_arg arg = {
    2934                 :            :                 .res = &res,
    2935                 :            :                 .fl6 = fl6,
    2936                 :          0 :                 .gw  = &rdfl->gateway,
    2937                 :            :                 .ret = &ret
    2938                 :            :         };
    2939                 :            :         struct fib6_info *rt;
    2940                 :            :         struct fib6_node *fn;
    2941                 :            : 
    2942                 :            :         /* l3mdev_update_flow overrides oif if the device is enslaved; in
    2943                 :            :          * this case we must match on the real ingress device, so reset it
    2944                 :            :          */
    2945         [ #  # ]:          0 :         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
    2946                 :          0 :                 fl6->flowi6_oif = skb->dev->ifindex;
    2947                 :            : 
    2948                 :            :         /* Get the "current" route for this destination and
    2949                 :            :          * check if the redirect has come from appropriate router.
    2950                 :            :          *
    2951                 :            :          * RFC 4861 specifies that redirects should only be
    2952                 :            :          * accepted if they come from the nexthop to the target.
    2953                 :            :          * Due to the way the routes are chosen, this notion
    2954                 :            :          * is a bit fuzzy and one might need to check all possible
    2955                 :            :          * routes.
    2956                 :            :          */
    2957                 :            : 
    2958                 :            :         rcu_read_lock();
    2959                 :          0 :         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
    2960                 :            : restart:
    2961         [ #  # ]:          0 :         for_each_fib6_node_rt_rcu(fn) {
    2962                 :          0 :                 res.f6i = rt;
    2963         [ #  # ]:          0 :                 if (fib6_check_expired(rt))
    2964                 :          0 :                         continue;
    2965         [ #  # ]:          0 :                 if (rt->fib6_flags & RTF_REJECT)
    2966                 :            :                         break;
    2967         [ #  # ]:          0 :                 if (unlikely(rt->nh)) {
    2968         [ #  # ]:          0 :                         if (nexthop_is_blackhole(rt->nh))
    2969                 :          0 :                                 continue;
    2970                 :            :                         /* on match, res->nh is filled in and potentially ret */
    2971         [ #  # ]:          0 :                         if (nexthop_for_each_fib6_nh(rt->nh,
    2972                 :            :                                                      fib6_nh_redirect_match,
    2973                 :            :                                                      &arg))
    2974                 :            :                                 goto out;
    2975                 :            :                 } else {
    2976                 :          0 :                         res.nh = rt->fib6_nh;
    2977         [ #  # ]:          0 :                         if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
    2978                 :            :                                                   &ret))
    2979                 :            :                                 goto out;
    2980                 :            :                 }
    2981                 :            :         }
    2982                 :            : 
    2983         [ #  # ]:          0 :         if (!rt)
    2984                 :          0 :                 rt = net->ipv6.fib6_null_entry;
    2985         [ #  # ]:          0 :         else if (rt->fib6_flags & RTF_REJECT) {
    2986                 :          0 :                 ret = net->ipv6.ip6_null_entry;
    2987                 :          0 :                 goto out;
    2988                 :            :         }
    2989                 :            : 
    2990         [ #  # ]:          0 :         if (rt == net->ipv6.fib6_null_entry) {
    2991                 :          0 :                 fn = fib6_backtrack(fn, &fl6->saddr);
    2992         [ #  # ]:          0 :                 if (fn)
    2993                 :            :                         goto restart;
    2994                 :            :         }
    2995                 :            : 
    2996                 :          0 :         res.f6i = rt;
    2997                 :          0 :         res.nh = rt->fib6_nh;
    2998                 :            : out:
    2999         [ #  # ]:          0 :         if (ret) {
    3000                 :          0 :                 ip6_hold_safe(net, &ret);
    3001                 :            :         } else {
    3002                 :          0 :                 res.fib6_flags = res.f6i->fib6_flags;
    3003                 :          0 :                 res.fib6_type = res.f6i->fib6_type;
    3004                 :          0 :                 ret = ip6_create_rt_rcu(&res);
    3005                 :            :         }
    3006                 :            : 
    3007                 :            :         rcu_read_unlock();
    3008                 :            : 
    3009                 :          0 :         trace_fib6_table_lookup(net, &res, table, fl6);
    3010                 :          0 :         return ret;
    3011                 :            : };
    3012                 :            : 
    3013                 :          0 : static struct dst_entry *ip6_route_redirect(struct net *net,
    3014                 :            :                                             const struct flowi6 *fl6,
    3015                 :            :                                             const struct sk_buff *skb,
    3016                 :            :                                             const struct in6_addr *gateway)
    3017                 :            : {
    3018                 :            :         int flags = RT6_LOOKUP_F_HAS_SADDR;
    3019                 :            :         struct ip6rd_flowi rdfl;
    3020                 :            : 
    3021                 :          0 :         rdfl.fl6 = *fl6;
    3022                 :          0 :         rdfl.gateway = *gateway;
    3023                 :            : 
    3024                 :          0 :         return fib6_rule_lookup(net, &rdfl.fl6, skb,
    3025                 :            :                                 flags, __ip6_route_redirect);
    3026                 :            : }
    3027                 :            : 
    3028                 :          0 : void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
    3029                 :            :                   kuid_t uid)
    3030                 :            : {
    3031                 :          0 :         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
    3032                 :            :         struct dst_entry *dst;
    3033                 :          0 :         struct flowi6 fl6 = {
    3034                 :            :                 .flowi6_iif = LOOPBACK_IFINDEX,
    3035                 :            :                 .flowi6_oif = oif,
    3036                 :            :                 .flowi6_mark = mark,
    3037                 :            :                 .daddr = iph->daddr,
    3038                 :            :                 .saddr = iph->saddr,
    3039                 :            :                 .flowlabel = ip6_flowinfo(iph),
    3040                 :            :                 .flowi6_uid = uid,
    3041                 :            :         };
    3042                 :            : 
    3043                 :          0 :         dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
    3044                 :          0 :         rt6_do_redirect(dst, NULL, skb);
    3045                 :          0 :         dst_release(dst);
    3046                 :          0 : }
    3047                 :            : EXPORT_SYMBOL_GPL(ip6_redirect);
    3048                 :            : 
    3049                 :          0 : void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
    3050                 :            : {
    3051                 :            :         const struct ipv6hdr *iph = ipv6_hdr(skb);
    3052                 :            :         const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
    3053                 :            :         struct dst_entry *dst;
    3054                 :          0 :         struct flowi6 fl6 = {
    3055                 :            :                 .flowi6_iif = LOOPBACK_IFINDEX,
    3056                 :            :                 .flowi6_oif = oif,
    3057                 :            :                 .daddr = msg->dest,
    3058                 :            :                 .saddr = iph->daddr,
    3059                 :            :                 .flowi6_uid = sock_net_uid(net, NULL),
    3060                 :            :         };
    3061                 :            : 
    3062                 :          0 :         dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
    3063                 :          0 :         rt6_do_redirect(dst, NULL, skb);
    3064                 :          0 :         dst_release(dst);
    3065                 :          0 : }
    3066                 :            : 
    3067                 :          0 : void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
    3068                 :            : {
    3069                 :          0 :         ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
    3070                 :            :                      sk->sk_uid);
    3071                 :          0 : }
    3072                 :            : EXPORT_SYMBOL_GPL(ip6_sk_redirect);
    3073                 :            : 
    3074                 :          0 : static unsigned int ip6_default_advmss(const struct dst_entry *dst)
    3075                 :            : {
    3076                 :          0 :         struct net_device *dev = dst->dev;
    3077                 :            :         unsigned int mtu = dst_mtu(dst);
    3078                 :            :         struct net *net = dev_net(dev);
    3079                 :            : 
    3080                 :          0 :         mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
    3081                 :            : 
    3082         [ #  # ]:          0 :         if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
    3083                 :            :                 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
    3084                 :            : 
    3085                 :            :         /*
    3086                 :            :          * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
    3087                 :            :          * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
    3088                 :            :          * IPV6_MAXPLEN is also valid and means: "any MSS,
    3089                 :            :          * rely only on pmtu discovery"
    3090                 :            :          */
    3091         [ #  # ]:          0 :         if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
    3092                 :            :                 mtu = IPV6_MAXPLEN;
    3093                 :          0 :         return mtu;
    3094                 :            : }
    3095                 :            : 
    3096                 :       4439 : static unsigned int ip6_mtu(const struct dst_entry *dst)
    3097                 :            : {
    3098                 :            :         struct inet6_dev *idev;
    3099                 :            :         unsigned int mtu;
    3100                 :            : 
    3101                 :            :         mtu = dst_metric_raw(dst, RTAX_MTU);
    3102         [ +  - ]:       4439 :         if (mtu)
    3103                 :            :                 goto out;
    3104                 :            : 
    3105                 :            :         mtu = IPV6_MIN_MTU;
    3106                 :            : 
    3107                 :            :         rcu_read_lock();
    3108                 :       4439 :         idev = __in6_dev_get(dst->dev);
    3109         [ +  - ]:       4439 :         if (idev)
    3110                 :       4439 :                 mtu = idev->cnf.mtu6;
    3111                 :            :         rcu_read_unlock();
    3112                 :            : 
    3113                 :            : out:
    3114                 :       4439 :         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
    3115                 :            : 
    3116                 :       4439 :         return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
    3117                 :            : }
    3118                 :            : 
    3119                 :            : /* MTU selection:
    3120                 :            :  * 1. mtu on route is locked - use it
    3121                 :            :  * 2. mtu from nexthop exception
    3122                 :            :  * 3. mtu from egress device
    3123                 :            :  *
    3124                 :            :  * based on ip6_dst_mtu_forward and exception logic of
    3125                 :            :  * rt6_find_cached_rt; called with rcu_read_lock
    3126                 :            :  */
    3127                 :          0 : u32 ip6_mtu_from_fib6(const struct fib6_result *res,
    3128                 :            :                       const struct in6_addr *daddr,
    3129                 :            :                       const struct in6_addr *saddr)
    3130                 :            : {
    3131                 :          0 :         const struct fib6_nh *nh = res->nh;
    3132                 :          0 :         struct fib6_info *f6i = res->f6i;
    3133                 :            :         struct inet6_dev *idev;
    3134                 :            :         struct rt6_info *rt;
    3135                 :            :         u32 mtu = 0;
    3136                 :            : 
    3137         [ #  # ]:          0 :         if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
    3138                 :          0 :                 mtu = f6i->fib6_pmtu;
    3139         [ #  # ]:          0 :                 if (mtu)
    3140                 :            :                         goto out;
    3141                 :            :         }
    3142                 :            : 
    3143                 :          0 :         rt = rt6_find_cached_rt(res, daddr, saddr);
    3144         [ #  # ]:          0 :         if (unlikely(rt)) {
    3145                 :            :                 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
    3146                 :            :         } else {
    3147                 :          0 :                 struct net_device *dev = nh->fib_nh_dev;
    3148                 :            : 
    3149                 :            :                 mtu = IPV6_MIN_MTU;
    3150                 :            :                 idev = __in6_dev_get(dev);
    3151   [ #  #  #  # ]:          0 :                 if (idev && idev->cnf.mtu6 > mtu)
    3152                 :            :                         mtu = idev->cnf.mtu6;
    3153                 :            :         }
    3154                 :            : 
    3155                 :          0 :         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
    3156                 :            : out:
    3157                 :          0 :         return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
    3158                 :            : }
    3159                 :            : 
    3160                 :       2001 : struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
    3161                 :            :                                   struct flowi6 *fl6)
    3162                 :            : {
    3163                 :            :         struct dst_entry *dst;
    3164                 :            :         struct rt6_info *rt;
    3165                 :            :         struct inet6_dev *idev = in6_dev_get(dev);
    3166                 :            :         struct net *net = dev_net(dev);
    3167                 :            : 
    3168         [ +  - ]:       2001 :         if (unlikely(!idev))
    3169                 :            :                 return ERR_PTR(-ENODEV);
    3170                 :            : 
    3171                 :       2001 :         rt = ip6_dst_alloc(net, dev, 0);
    3172         [ -  + ]:       2001 :         if (unlikely(!rt)) {
    3173                 :          0 :                 in6_dev_put(idev);
    3174                 :            :                 dst = ERR_PTR(-ENOMEM);
    3175                 :          0 :                 goto out;
    3176                 :            :         }
    3177                 :            : 
    3178                 :       2001 :         rt->dst.flags |= DST_HOST;
    3179                 :       2001 :         rt->dst.input = ip6_input;
    3180                 :       2001 :         rt->dst.output  = ip6_output;
    3181                 :       2001 :         rt->rt6i_gateway  = fl6->daddr;
    3182                 :       2001 :         rt->rt6i_dst.addr = fl6->daddr;
    3183                 :       2001 :         rt->rt6i_dst.plen = 128;
    3184                 :       2001 :         rt->rt6i_idev     = idev;
    3185                 :       2001 :         dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
    3186                 :            : 
    3187                 :            :         /* Add this dst into uncached_list so that rt6_disable_ip() can
    3188                 :            :          * do proper release of the net_device
    3189                 :            :          */
    3190                 :       2001 :         rt6_uncached_list_add(rt);
    3191                 :       2001 :         atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
    3192                 :            : 
    3193                 :       2001 :         dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
    3194                 :            : 
    3195                 :            : out:
    3196                 :       2001 :         return dst;
    3197                 :            : }
    3198                 :            : 
    3199                 :          0 : static int ip6_dst_gc(struct dst_ops *ops)
    3200                 :            : {
    3201                 :          0 :         struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
    3202                 :          0 :         int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
    3203                 :          0 :         int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
    3204                 :          0 :         int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
    3205                 :          0 :         int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
    3206                 :          0 :         unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
    3207                 :            :         int entries;
    3208                 :            : 
    3209                 :            :         entries = dst_entries_get_fast(ops);
    3210   [ #  #  #  # ]:          0 :         if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
    3211                 :            :             entries <= rt_max_size)
    3212                 :            :                 goto out;
    3213                 :            : 
    3214                 :          0 :         net->ipv6.ip6_rt_gc_expire++;
    3215                 :          0 :         fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
    3216                 :            :         entries = dst_entries_get_slow(ops);
    3217         [ #  # ]:          0 :         if (entries < ops->gc_thresh)
    3218                 :          0 :                 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
    3219                 :            : out:
    3220                 :          0 :         net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
    3221                 :          0 :         return entries > rt_max_size;
    3222                 :            : }
    3223                 :            : 
    3224                 :          0 : static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
    3225                 :            :                                const struct in6_addr *gw_addr, u32 tbid,
    3226                 :            :                                int flags, struct fib6_result *res)
    3227                 :            : {
    3228                 :          0 :         struct flowi6 fl6 = {
    3229                 :          0 :                 .flowi6_oif = cfg->fc_ifindex,
    3230                 :            :                 .daddr = *gw_addr,
    3231                 :            :                 .saddr = cfg->fc_prefsrc,
    3232                 :            :         };
    3233                 :            :         struct fib6_table *table;
    3234                 :            :         int err;
    3235                 :            : 
    3236                 :          0 :         table = fib6_get_table(net, tbid);
    3237         [ #  # ]:          0 :         if (!table)
    3238                 :            :                 return -EINVAL;
    3239                 :            : 
    3240         [ #  # ]:          0 :         if (!ipv6_addr_any(&cfg->fc_prefsrc))
    3241                 :          0 :                 flags |= RT6_LOOKUP_F_HAS_SADDR;
    3242                 :            : 
    3243                 :          0 :         flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
    3244                 :            : 
    3245                 :          0 :         err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
    3246   [ #  #  #  # ]:          0 :         if (!err && res->f6i != net->ipv6.fib6_null_entry)
    3247                 :          0 :                 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
    3248                 :          0 :                                  cfg->fc_ifindex != 0, NULL, flags);
    3249                 :            : 
    3250                 :          0 :         return err;
    3251                 :            : }
    3252                 :            : 
    3253                 :          0 : static int ip6_route_check_nh_onlink(struct net *net,
    3254                 :            :                                      struct fib6_config *cfg,
    3255                 :            :                                      const struct net_device *dev,
    3256                 :            :                                      struct netlink_ext_ack *extack)
    3257                 :            : {
    3258         [ #  # ]:          0 :         u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
    3259                 :          0 :         const struct in6_addr *gw_addr = &cfg->fc_gateway;
    3260                 :          0 :         struct fib6_result res = {};
    3261                 :            :         int err;
    3262                 :            : 
    3263                 :          0 :         err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
    3264   [ #  #  #  #  :          0 :         if (!err && !(res.fib6_flags & RTF_REJECT) &&
                   #  # ]
    3265                 :            :             /* ignore match if it is the default route */
    3266         [ #  # ]:          0 :             !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
    3267         [ #  # ]:          0 :             (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
    3268         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack,
    3269                 :            :                                "Nexthop has invalid gateway or device mismatch");
    3270                 :            :                 err = -EINVAL;
    3271                 :            :         }
    3272                 :            : 
    3273                 :          0 :         return err;
    3274                 :            : }
    3275                 :            : 
    3276                 :          0 : static int ip6_route_check_nh(struct net *net,
    3277                 :            :                               struct fib6_config *cfg,
    3278                 :            :                               struct net_device **_dev,
    3279                 :            :                               struct inet6_dev **idev)
    3280                 :            : {
    3281                 :          0 :         const struct in6_addr *gw_addr = &cfg->fc_gateway;
    3282         [ #  # ]:          0 :         struct net_device *dev = _dev ? *_dev : NULL;
    3283                 :            :         int flags = RT6_LOOKUP_F_IFACE;
    3284                 :          0 :         struct fib6_result res = {};
    3285                 :            :         int err = -EHOSTUNREACH;
    3286                 :            : 
    3287         [ #  # ]:          0 :         if (cfg->fc_table) {
    3288                 :          0 :                 err = ip6_nh_lookup_table(net, cfg, gw_addr,
    3289                 :            :                                           cfg->fc_table, flags, &res);
    3290                 :            :                 /* gw_addr can not require a gateway or resolve to a reject
    3291                 :            :                  * route. If a device is given, it must match the result.
    3292                 :            :                  */
    3293   [ #  #  #  #  :          0 :                 if (err || res.fib6_flags & RTF_REJECT ||
                   #  # ]
    3294         [ #  # ]:          0 :                     res.nh->fib_nh_gw_family ||
    3295         [ #  # ]:          0 :                     (dev && dev != res.nh->fib_nh_dev))
    3296                 :            :                         err = -EHOSTUNREACH;
    3297                 :            :         }
    3298                 :            : 
    3299         [ #  # ]:          0 :         if (err < 0) {
    3300                 :          0 :                 struct flowi6 fl6 = {
    3301                 :          0 :                         .flowi6_oif = cfg->fc_ifindex,
    3302                 :            :                         .daddr = *gw_addr,
    3303                 :            :                 };
    3304                 :            : 
    3305                 :          0 :                 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
    3306   [ #  #  #  #  :          0 :                 if (err || res.fib6_flags & RTF_REJECT ||
                   #  # ]
    3307                 :          0 :                     res.nh->fib_nh_gw_family)
    3308                 :            :                         err = -EHOSTUNREACH;
    3309                 :            : 
    3310         [ #  # ]:          0 :                 if (err)
    3311                 :          0 :                         return err;
    3312                 :            : 
    3313                 :          0 :                 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
    3314                 :          0 :                                  cfg->fc_ifindex != 0, NULL, flags);
    3315                 :            :         }
    3316                 :            : 
    3317                 :            :         err = 0;
    3318         [ #  # ]:          0 :         if (dev) {
    3319         [ #  # ]:          0 :                 if (dev != res.nh->fib_nh_dev)
    3320                 :            :                         err = -EHOSTUNREACH;
    3321                 :            :         } else {
    3322                 :          0 :                 *_dev = dev = res.nh->fib_nh_dev;
    3323                 :          0 :                 dev_hold(dev);
    3324                 :          0 :                 *idev = in6_dev_get(dev);
    3325                 :            :         }
    3326                 :            : 
    3327                 :          0 :         return err;
    3328                 :            : }
    3329                 :            : 
    3330                 :          0 : static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
    3331                 :            :                            struct net_device **_dev, struct inet6_dev **idev,
    3332                 :            :                            struct netlink_ext_ack *extack)
    3333                 :            : {
    3334                 :          0 :         const struct in6_addr *gw_addr = &cfg->fc_gateway;
    3335                 :            :         int gwa_type = ipv6_addr_type(gw_addr);
    3336                 :          0 :         bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
    3337                 :          0 :         const struct net_device *dev = *_dev;
    3338                 :            :         bool need_addr_check = !dev;
    3339                 :            :         int err = -EINVAL;
    3340                 :            : 
    3341                 :            :         /* if gw_addr is local we will fail to detect this in case
    3342                 :            :          * address is still TENTATIVE (DAD in progress). rt6_lookup()
    3343                 :            :          * will return already-added prefix route via interface that
    3344                 :            :          * prefix route was assigned to, which might be non-loopback.
    3345                 :            :          */
    3346   [ #  #  #  # ]:          0 :         if (dev &&
    3347                 :          0 :             ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
    3348         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
    3349                 :            :                 goto out;
    3350                 :            :         }
    3351                 :            : 
    3352         [ #  # ]:          0 :         if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
    3353                 :            :                 /* IPv6 strictly inhibits using not link-local
    3354                 :            :                  * addresses as nexthop address.
    3355                 :            :                  * Otherwise, router will not able to send redirects.
    3356                 :            :                  * It is very good, but in some (rare!) circumstances
    3357                 :            :                  * (SIT, PtP, NBMA NOARP links) it is handy to allow
    3358                 :            :                  * some exceptions. --ANK
    3359                 :            :                  * We allow IPv4-mapped nexthops to support RFC4798-type
    3360                 :            :                  * addressing
    3361                 :            :                  */
    3362         [ #  # ]:          0 :                 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
    3363         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Invalid gateway address");
    3364                 :            :                         goto out;
    3365                 :            :                 }
    3366                 :            : 
    3367                 :            :                 rcu_read_lock();
    3368                 :            : 
    3369         [ #  # ]:          0 :                 if (cfg->fc_flags & RTNH_F_ONLINK)
    3370                 :          0 :                         err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
    3371                 :            :                 else
    3372                 :          0 :                         err = ip6_route_check_nh(net, cfg, _dev, idev);
    3373                 :            : 
    3374                 :            :                 rcu_read_unlock();
    3375                 :            : 
    3376         [ #  # ]:          0 :                 if (err)
    3377                 :            :                         goto out;
    3378                 :            :         }
    3379                 :            : 
    3380                 :            :         /* reload in case device was changed */
    3381                 :          0 :         dev = *_dev;
    3382                 :            : 
    3383                 :            :         err = -EINVAL;
    3384         [ #  # ]:          0 :         if (!dev) {
    3385         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Egress device not specified");
    3386                 :            :                 goto out;
    3387         [ #  # ]:          0 :         } else if (dev->flags & IFF_LOOPBACK) {
    3388         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack,
    3389                 :            :                                "Egress device can not be loopback device for this route");
    3390                 :            :                 goto out;
    3391                 :            :         }
    3392                 :            : 
    3393                 :            :         /* if we did not check gw_addr above, do so now that the
    3394                 :            :          * egress device has been resolved.
    3395                 :            :          */
    3396   [ #  #  #  # ]:          0 :         if (need_addr_check &&
    3397                 :          0 :             ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
    3398         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
    3399                 :            :                 goto out;
    3400                 :            :         }
    3401                 :            : 
    3402                 :            :         err = 0;
    3403                 :            : out:
    3404                 :          0 :         return err;
    3405                 :            : }
    3406                 :            : 
    3407                 :            : static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
    3408                 :            : {
    3409   [ +  -  +  -  :       4391 :         if ((flags & RTF_REJECT) ||
             +  -  +  - ]
    3410   [ +  +  -  +  :       5884 :             (dev && (dev->flags & IFF_LOOPBACK) &&
             +  +  -  + ]
    3411   [ #  #  #  # ]:       1493 :              !(addr_type & IPV6_ADDR_LOOPBACK) &&
    3412                 :          0 :              !(flags & (RTF_ANYCAST | RTF_LOCAL))))
    3413                 :            :                 return true;
    3414                 :            : 
    3415                 :            :         return false;
    3416                 :            : }
    3417                 :            : 
    3418                 :       2299 : int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
    3419                 :            :                  struct fib6_config *cfg, gfp_t gfp_flags,
    3420                 :            :                  struct netlink_ext_ack *extack)
    3421                 :            : {
    3422                 :       2299 :         struct net_device *dev = NULL;
    3423                 :       2299 :         struct inet6_dev *idev = NULL;
    3424                 :            :         int addr_type;
    3425                 :            :         int err;
    3426                 :            : 
    3427                 :       2299 :         fib6_nh->fib_nh_family = AF_INET6;
    3428                 :            : #ifdef CONFIG_IPV6_ROUTER_PREF
    3429                 :       2299 :         fib6_nh->last_probe = jiffies;
    3430                 :            : #endif
    3431                 :            : 
    3432                 :            :         err = -ENODEV;
    3433         [ +  - ]:       2299 :         if (cfg->fc_ifindex) {
    3434                 :       2299 :                 dev = dev_get_by_index(net, cfg->fc_ifindex);
    3435         [ +  - ]:       2299 :                 if (!dev)
    3436                 :            :                         goto out;
    3437                 :       2299 :                 idev = in6_dev_get(dev);
    3438         [ +  - ]:       2299 :                 if (!idev)
    3439                 :            :                         goto out;
    3440                 :            :         }
    3441                 :            : 
    3442         [ -  + ]:       2299 :         if (cfg->fc_flags & RTNH_F_ONLINK) {
    3443         [ #  # ]:          0 :                 if (!dev) {
    3444         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack,
    3445                 :            :                                        "Nexthop device required for onlink");
    3446                 :            :                         goto out;
    3447                 :            :                 }
    3448                 :            : 
    3449         [ #  # ]:          0 :                 if (!(dev->flags & IFF_UP)) {
    3450         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Nexthop device is not up");
    3451                 :            :                         err = -ENETDOWN;
    3452                 :            :                         goto out;
    3453                 :            :                 }
    3454                 :            : 
    3455                 :          0 :                 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
    3456                 :            :         }
    3457                 :            : 
    3458                 :       2299 :         fib6_nh->fib_nh_weight = 1;
    3459                 :            : 
    3460                 :            :         /* We cannot add true routes via loopback here,
    3461                 :            :          * they would result in kernel looping; promote them to reject routes
    3462                 :            :          */
    3463                 :       2299 :         addr_type = ipv6_addr_type(&cfg->fc_dst);
    3464         [ -  + ]:       4598 :         if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
    3465                 :            :                 /* hold loopback dev/idev if we haven't done so. */
    3466         [ #  # ]:          0 :                 if (dev != net->loopback_dev) {
    3467         [ #  # ]:          0 :                         if (dev) {
    3468                 :          0 :                                 dev_put(dev);
    3469                 :          0 :                                 in6_dev_put(idev);
    3470                 :            :                         }
    3471                 :          0 :                         dev = net->loopback_dev;
    3472                 :          0 :                         dev_hold(dev);
    3473                 :          0 :                         idev = in6_dev_get(dev);
    3474         [ #  # ]:          0 :                         if (!idev) {
    3475                 :            :                                 err = -ENODEV;
    3476                 :            :                                 goto out;
    3477                 :            :                         }
    3478                 :            :                 }
    3479                 :            :                 goto pcpu_alloc;
    3480                 :            :         }
    3481                 :            : 
    3482         [ -  + ]:       2299 :         if (cfg->fc_flags & RTF_GATEWAY) {
    3483                 :          0 :                 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
    3484         [ #  # ]:          0 :                 if (err)
    3485                 :            :                         goto out;
    3486                 :            : 
    3487                 :          0 :                 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
    3488                 :          0 :                 fib6_nh->fib_nh_gw_family = AF_INET6;
    3489                 :            :         }
    3490                 :            : 
    3491                 :            :         err = -ENODEV;
    3492         [ +  - ]:       2299 :         if (!dev)
    3493                 :            :                 goto out;
    3494                 :            : 
    3495         [ -  + ]:       2299 :         if (idev->cnf.disable_ipv6) {
    3496         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
    3497                 :            :                 err = -EACCES;
    3498                 :            :                 goto out;
    3499                 :            :         }
    3500                 :            : 
    3501   [ +  +  +  + ]:       2299 :         if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
    3502         [ -  + ]:        207 :                 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
    3503                 :            :                 err = -ENETDOWN;
    3504                 :            :                 goto out;
    3505                 :            :         }
    3506                 :            : 
    3507   [ +  +  -  + ]:       3334 :         if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
    3508                 :            :             !netif_carrier_ok(dev))
    3509                 :          0 :                 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
    3510                 :            : 
    3511                 :       2092 :         err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
    3512                 :            :                                  cfg->fc_encap_type, cfg, gfp_flags, extack);
    3513         [ +  - ]:       2092 :         if (err)
    3514                 :            :                 goto out;
    3515                 :            : 
    3516                 :            : pcpu_alloc:
    3517                 :       2092 :         fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
    3518         [ +  - ]:       2092 :         if (!fib6_nh->rt6i_pcpu) {
    3519                 :            :                 err = -ENOMEM;
    3520                 :            :                 goto out;
    3521                 :            :         }
    3522                 :            : 
    3523                 :       2092 :         fib6_nh->fib_nh_dev = dev;
    3524                 :       2092 :         fib6_nh->fib_nh_oif = dev->ifindex;
    3525                 :            :         err = 0;
    3526                 :            : out:
    3527         [ +  - ]:       2299 :         if (idev)
    3528                 :       2299 :                 in6_dev_put(idev);
    3529                 :            : 
    3530         [ +  + ]:       2299 :         if (err) {
    3531                 :            :                 lwtstate_put(fib6_nh->fib_nh_lws);
    3532                 :        207 :                 fib6_nh->fib_nh_lws = NULL;
    3533         [ +  - ]:        207 :                 if (dev)
    3534                 :        207 :                         dev_put(dev);
    3535                 :            :         }
    3536                 :            : 
    3537                 :       2299 :         return err;
    3538                 :            : }
    3539                 :            : 
    3540                 :       1264 : void fib6_nh_release(struct fib6_nh *fib6_nh)
    3541                 :            : {
    3542                 :            :         struct rt6_exception_bucket *bucket;
    3543                 :            : 
    3544                 :            :         rcu_read_lock();
    3545                 :            : 
    3546                 :       1264 :         fib6_nh_flush_exceptions(fib6_nh, NULL);
    3547                 :            :         bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
    3548         [ -  + ]:       1264 :         if (bucket) {
    3549                 :            :                 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
    3550                 :          0 :                 kfree(bucket);
    3551                 :            :         }
    3552                 :            : 
    3553                 :            :         rcu_read_unlock();
    3554                 :            : 
    3555         [ +  + ]:       1264 :         if (fib6_nh->rt6i_pcpu) {
    3556                 :            :                 int cpu;
    3557                 :            : 
    3558         [ +  + ]:       5285 :                 for_each_possible_cpu(cpu) {
    3559                 :            :                         struct rt6_info **ppcpu_rt;
    3560                 :            :                         struct rt6_info *pcpu_rt;
    3561                 :            : 
    3562                 :       4228 :                         ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
    3563                 :       4228 :                         pcpu_rt = *ppcpu_rt;
    3564         [ -  + ]:       4228 :                         if (pcpu_rt) {
    3565                 :          0 :                                 dst_dev_put(&pcpu_rt->dst);
    3566                 :          0 :                                 dst_release(&pcpu_rt->dst);
    3567                 :          0 :                                 *ppcpu_rt = NULL;
    3568                 :            :                         }
    3569                 :            :                 }
    3570                 :            : 
    3571                 :       1057 :                 free_percpu(fib6_nh->rt6i_pcpu);
    3572                 :            :         }
    3573                 :            : 
    3574                 :       1264 :         fib_nh_common_release(&fib6_nh->nh_common);
    3575                 :       1264 : }
    3576                 :            : 
    3577                 :       2299 : static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
    3578                 :            :                                               gfp_t gfp_flags,
    3579                 :            :                                               struct netlink_ext_ack *extack)
    3580                 :            : {
    3581                 :       2299 :         struct net *net = cfg->fc_nlinfo.nl_net;
    3582                 :            :         struct fib6_info *rt = NULL;
    3583                 :            :         struct nexthop *nh = NULL;
    3584                 :            :         struct fib6_table *table;
    3585                 :            :         struct fib6_nh *fib6_nh;
    3586                 :            :         int err = -EINVAL;
    3587                 :            :         int addr_type;
    3588                 :            : 
    3589                 :            :         /* RTF_PCPU is an internal flag; can not be set by userspace */
    3590         [ -  + ]:       2299 :         if (cfg->fc_flags & RTF_PCPU) {
    3591         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
    3592                 :            :                 goto out;
    3593                 :            :         }
    3594                 :            : 
    3595                 :            :         /* RTF_CACHE is an internal flag; can not be set by userspace */
    3596         [ -  + ]:       2299 :         if (cfg->fc_flags & RTF_CACHE) {
    3597         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
    3598                 :            :                 goto out;
    3599                 :            :         }
    3600                 :            : 
    3601         [ -  + ]:       2299 :         if (cfg->fc_type > RTN_MAX) {
    3602         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Invalid route type");
    3603                 :            :                 goto out;
    3604                 :            :         }
    3605                 :            : 
    3606         [ -  + ]:       2299 :         if (cfg->fc_dst_len > 128) {
    3607         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Invalid prefix length");
    3608                 :            :                 goto out;
    3609                 :            :         }
    3610         [ -  + ]:       2299 :         if (cfg->fc_src_len > 128) {
    3611         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Invalid source address length");
    3612                 :            :                 goto out;
    3613                 :            :         }
    3614                 :            : #ifndef CONFIG_IPV6_SUBTREES
    3615                 :            :         if (cfg->fc_src_len) {
    3616                 :            :                 NL_SET_ERR_MSG(extack,
    3617                 :            :                                "Specifying source address requires IPV6_SUBTREES to be enabled");
    3618                 :            :                 goto out;
    3619                 :            :         }
    3620                 :            : #endif
    3621         [ -  + ]:       2299 :         if (cfg->fc_nh_id) {
    3622                 :          0 :                 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
    3623         [ #  # ]:          0 :                 if (!nh) {
    3624         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
    3625                 :            :                         goto out;
    3626                 :            :                 }
    3627                 :          0 :                 err = fib6_check_nexthop(nh, cfg, extack);
    3628         [ #  # ]:          0 :                 if (err)
    3629                 :            :                         goto out;
    3630                 :            :         }
    3631                 :            : 
    3632                 :            :         err = -ENOBUFS;
    3633   [ -  +  #  # ]:       2299 :         if (cfg->fc_nlinfo.nlh &&
    3634                 :          0 :             !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
    3635                 :          0 :                 table = fib6_get_table(net, cfg->fc_table);
    3636         [ #  # ]:          0 :                 if (!table) {
    3637                 :          0 :                         pr_warn("NLM_F_CREATE should be specified when creating new route\n");
    3638                 :          0 :                         table = fib6_new_table(net, cfg->fc_table);
    3639                 :            :                 }
    3640                 :            :         } else {
    3641                 :       2299 :                 table = fib6_new_table(net, cfg->fc_table);
    3642                 :            :         }
    3643                 :            : 
    3644         [ +  - ]:       2299 :         if (!table)
    3645                 :            :                 goto out;
    3646                 :            : 
    3647                 :            :         err = -ENOMEM;
    3648                 :       2299 :         rt = fib6_info_alloc(gfp_flags, !nh);
    3649         [ +  - ]:       2299 :         if (!rt)
    3650                 :            :                 goto out;
    3651                 :            : 
    3652                 :       2299 :         rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
    3653                 :            :                                                extack);
    3654         [ -  + ]:       2299 :         if (IS_ERR(rt->fib6_metrics)) {
    3655                 :            :                 err = PTR_ERR(rt->fib6_metrics);
    3656                 :            :                 /* Do not leave garbage there. */
    3657                 :          0 :                 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
    3658                 :          0 :                 goto out;
    3659                 :            :         }
    3660                 :            : 
    3661         [ -  + ]:       2299 :         if (cfg->fc_flags & RTF_ADDRCONF)
    3662                 :          0 :                 rt->dst_nocount = true;
    3663                 :            : 
    3664         [ -  + ]:       2299 :         if (cfg->fc_flags & RTF_EXPIRES)
    3665                 :          0 :                 fib6_set_expires(rt, jiffies +
    3666                 :          0 :                                 clock_t_to_jiffies(cfg->fc_expires));
    3667                 :            :         else
    3668                 :            :                 fib6_clean_expires(rt);
    3669                 :            : 
    3670         [ +  + ]:       2299 :         if (cfg->fc_protocol == RTPROT_UNSPEC)
    3671                 :        621 :                 cfg->fc_protocol = RTPROT_BOOT;
    3672                 :       2299 :         rt->fib6_protocol = cfg->fc_protocol;
    3673                 :            : 
    3674                 :       2299 :         rt->fib6_table = table;
    3675                 :       2299 :         rt->fib6_metric = cfg->fc_metric;
    3676         [ +  - ]:       2299 :         rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
    3677                 :       2299 :         rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
    3678                 :            : 
    3679                 :       2299 :         ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
    3680                 :       2299 :         rt->fib6_dst.plen = cfg->fc_dst_len;
    3681         [ +  + ]:       2299 :         if (rt->fib6_dst.plen == 128)
    3682                 :       1264 :                 rt->dst_host = true;
    3683                 :            : 
    3684                 :            : #ifdef CONFIG_IPV6_SUBTREES
    3685                 :       2299 :         ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
    3686                 :       2299 :         rt->fib6_src.plen = cfg->fc_src_len;
    3687                 :            : #endif
    3688         [ -  + ]:       2299 :         if (nh) {
    3689         [ #  # ]:          0 :                 if (rt->fib6_src.plen) {
    3690         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
    3691                 :            :                         goto out;
    3692                 :            :                 }
    3693         [ #  # ]:          0 :                 if (!nexthop_get(nh)) {
    3694         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
    3695                 :            :                         goto out;
    3696                 :            :                 }
    3697                 :          0 :                 rt->nh = nh;
    3698                 :            :                 fib6_nh = nexthop_fib6_nh(rt->nh);
    3699                 :            :         } else {
    3700                 :       2299 :                 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
    3701         [ +  + ]:       2299 :                 if (err)
    3702                 :            :                         goto out;
    3703                 :            : 
    3704                 :            :                 fib6_nh = rt->fib6_nh;
    3705                 :            : 
    3706                 :            :                 /* We cannot add true routes via loopback here, they would
    3707                 :            :                  * result in kernel looping; promote them to reject routes
    3708                 :            :                  */
    3709                 :            :                 addr_type = ipv6_addr_type(&cfg->fc_dst);
    3710         [ -  + ]:       4184 :                 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
    3711                 :            :                                    addr_type))
    3712                 :          0 :                         rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
    3713                 :            :         }
    3714                 :            : 
    3715         [ -  + ]:       2092 :         if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
    3716                 :          0 :                 struct net_device *dev = fib6_nh->fib_nh_dev;
    3717                 :            : 
    3718         [ #  # ]:          0 :                 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
    3719         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack, "Invalid source address");
    3720                 :            :                         err = -EINVAL;
    3721                 :            :                         goto out;
    3722                 :            :                 }
    3723                 :          0 :                 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
    3724                 :          0 :                 rt->fib6_prefsrc.plen = 128;
    3725                 :            :         } else
    3726                 :       2092 :                 rt->fib6_prefsrc.plen = 0;
    3727                 :            : 
    3728                 :       2092 :         return rt;
    3729                 :            : out:
    3730                 :        207 :         fib6_info_release(rt);
    3731                 :        207 :         return ERR_PTR(err);
    3732                 :            : }
    3733                 :            : 
    3734                 :       1449 : int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
    3735                 :            :                   struct netlink_ext_ack *extack)
    3736                 :            : {
    3737                 :            :         struct fib6_info *rt;
    3738                 :            :         int err;
    3739                 :            : 
    3740                 :       1449 :         rt = ip6_route_info_create(cfg, gfp_flags, extack);
    3741         [ +  + ]:       1449 :         if (IS_ERR(rt))
    3742                 :        207 :                 return PTR_ERR(rt);
    3743                 :            : 
    3744                 :       1242 :         err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
    3745                 :       1242 :         fib6_info_release(rt);
    3746                 :            : 
    3747                 :       1242 :         return err;
    3748                 :            : }
    3749                 :            : 
    3750                 :        414 : static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
    3751                 :            : {
    3752                 :        414 :         struct net *net = info->nl_net;
    3753                 :            :         struct fib6_table *table;
    3754                 :            :         int err;
    3755                 :            : 
    3756         [ +  - ]:        414 :         if (rt == net->ipv6.fib6_null_entry) {
    3757                 :            :                 err = -ENOENT;
    3758                 :            :                 goto out;
    3759                 :            :         }
    3760                 :            : 
    3761                 :        414 :         table = rt->fib6_table;
    3762                 :            :         spin_lock_bh(&table->tb6_lock);
    3763                 :        414 :         err = fib6_del(rt, info);
    3764                 :            :         spin_unlock_bh(&table->tb6_lock);
    3765                 :            : 
    3766                 :            : out:
    3767                 :        414 :         fib6_info_release(rt);
    3768                 :        414 :         return err;
    3769                 :            : }
    3770                 :            : 
    3771                 :        414 : int ip6_del_rt(struct net *net, struct fib6_info *rt)
    3772                 :            : {
    3773                 :        414 :         struct nl_info info = { .nl_net = net };
    3774                 :            : 
    3775                 :        414 :         return __ip6_del_rt(rt, &info);
    3776                 :            : }
    3777                 :            : 
    3778                 :          0 : static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
    3779                 :            : {
    3780                 :          0 :         struct nl_info *info = &cfg->fc_nlinfo;
    3781                 :          0 :         struct net *net = info->nl_net;
    3782                 :            :         struct sk_buff *skb = NULL;
    3783                 :            :         struct fib6_table *table;
    3784                 :            :         int err = -ENOENT;
    3785                 :            : 
    3786         [ #  # ]:          0 :         if (rt == net->ipv6.fib6_null_entry)
    3787                 :            :                 goto out_put;
    3788                 :          0 :         table = rt->fib6_table;
    3789                 :            :         spin_lock_bh(&table->tb6_lock);
    3790                 :            : 
    3791   [ #  #  #  # ]:          0 :         if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
    3792                 :            :                 struct fib6_info *sibling, *next_sibling;
    3793                 :            : 
    3794                 :            :                 /* prefer to send a single notification with all hops */
    3795                 :          0 :                 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
    3796         [ #  # ]:          0 :                 if (skb) {
    3797         [ #  # ]:          0 :                         u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
    3798                 :            : 
    3799         [ #  # ]:          0 :                         if (rt6_fill_node(net, skb, rt, NULL,
    3800                 :            :                                           NULL, NULL, 0, RTM_DELROUTE,
    3801                 :            :                                           info->portid, seq, 0) < 0) {
    3802                 :          0 :                                 kfree_skb(skb);
    3803                 :            :                                 skb = NULL;
    3804                 :            :                         } else
    3805                 :          0 :                                 info->skip_notify = 1;
    3806                 :            :                 }
    3807                 :            : 
    3808                 :          0 :                 info->skip_notify_kernel = 1;
    3809                 :          0 :                 call_fib6_multipath_entry_notifiers(net,
    3810                 :            :                                                     FIB_EVENT_ENTRY_DEL,
    3811                 :            :                                                     rt,
    3812                 :            :                                                     rt->fib6_nsiblings,
    3813                 :            :                                                     NULL);
    3814         [ #  # ]:          0 :                 list_for_each_entry_safe(sibling, next_sibling,
    3815                 :            :                                          &rt->fib6_siblings,
    3816                 :            :                                          fib6_siblings) {
    3817                 :          0 :                         err = fib6_del(sibling, info);
    3818         [ #  # ]:          0 :                         if (err)
    3819                 :            :                                 goto out_unlock;
    3820                 :            :                 }
    3821                 :            :         }
    3822                 :            : 
    3823                 :          0 :         err = fib6_del(rt, info);
    3824                 :            : out_unlock:
    3825                 :            :         spin_unlock_bh(&table->tb6_lock);
    3826                 :            : out_put:
    3827                 :          0 :         fib6_info_release(rt);
    3828                 :            : 
    3829         [ #  # ]:          0 :         if (skb) {
    3830                 :          0 :                 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
    3831                 :            :                             info->nlh, gfp_any());
    3832                 :            :         }
    3833                 :          0 :         return err;
    3834                 :            : }
    3835                 :            : 
    3836                 :          0 : static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
    3837                 :            : {
    3838                 :            :         int rc = -ESRCH;
    3839                 :            : 
    3840   [ #  #  #  # ]:          0 :         if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
    3841                 :            :                 goto out;
    3842                 :            : 
    3843   [ #  #  #  # ]:          0 :         if (cfg->fc_flags & RTF_GATEWAY &&
    3844                 :            :             !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
    3845                 :            :                 goto out;
    3846                 :            : 
    3847                 :          0 :         rc = rt6_remove_exception_rt(rt);
    3848                 :            : out:
    3849                 :          0 :         return rc;
    3850                 :            : }
    3851                 :            : 
    3852                 :          0 : static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
    3853                 :            :                              struct fib6_nh *nh)
    3854                 :            : {
    3855                 :          0 :         struct fib6_result res = {
    3856                 :            :                 .f6i = rt,
    3857                 :            :                 .nh = nh,
    3858                 :            :         };
    3859                 :            :         struct rt6_info *rt_cache;
    3860                 :            : 
    3861                 :          0 :         rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
    3862         [ #  # ]:          0 :         if (rt_cache)
    3863                 :          0 :                 return __ip6_del_cached_rt(rt_cache, cfg);
    3864                 :            : 
    3865                 :            :         return 0;
    3866                 :            : }
    3867                 :            : 
    3868                 :            : struct fib6_nh_del_cached_rt_arg {
    3869                 :            :         struct fib6_config *cfg;
    3870                 :            :         struct fib6_info *f6i;
    3871                 :            : };
    3872                 :            : 
    3873                 :          0 : static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
    3874                 :            : {
    3875                 :            :         struct fib6_nh_del_cached_rt_arg *arg = _arg;
    3876                 :            :         int rc;
    3877                 :            : 
    3878                 :          0 :         rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
    3879         [ #  # ]:          0 :         return rc != -ESRCH ? rc : 0;
    3880                 :            : }
    3881                 :            : 
    3882                 :            : static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
    3883                 :            : {
    3884                 :          0 :         struct fib6_nh_del_cached_rt_arg arg = {
    3885                 :            :                 .cfg = cfg,
    3886                 :            :                 .f6i = f6i
    3887                 :            :         };
    3888                 :            : 
    3889                 :          0 :         return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
    3890                 :            : }
    3891                 :            : 
    3892                 :          0 : static int ip6_route_del(struct fib6_config *cfg,
    3893                 :            :                          struct netlink_ext_ack *extack)
    3894                 :            : {
    3895                 :            :         struct fib6_table *table;
    3896                 :            :         struct fib6_info *rt;
    3897                 :            :         struct fib6_node *fn;
    3898                 :            :         int err = -ESRCH;
    3899                 :            : 
    3900                 :          0 :         table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
    3901         [ #  # ]:          0 :         if (!table) {
    3902         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "FIB table does not exist");
    3903                 :            :                 return err;
    3904                 :            :         }
    3905                 :            : 
    3906                 :            :         rcu_read_lock();
    3907                 :            : 
    3908                 :          0 :         fn = fib6_locate(&table->tb6_root,
    3909                 :          0 :                          &cfg->fc_dst, cfg->fc_dst_len,
    3910                 :          0 :                          &cfg->fc_src, cfg->fc_src_len,
    3911                 :          0 :                          !(cfg->fc_flags & RTF_CACHE));
    3912                 :            : 
    3913         [ #  # ]:          0 :         if (fn) {
    3914         [ #  # ]:          0 :                 for_each_fib6_node_rt_rcu(fn) {
    3915                 :            :                         struct fib6_nh *nh;
    3916                 :            : 
    3917   [ #  #  #  #  :          0 :                         if (rt->nh && cfg->fc_nh_id &&
                   #  # ]
    3918                 :          0 :                             rt->nh->id != cfg->fc_nh_id)
    3919                 :          0 :                                 continue;
    3920                 :            : 
    3921         [ #  # ]:          0 :                         if (cfg->fc_flags & RTF_CACHE) {
    3922                 :            :                                 int rc = 0;
    3923                 :            : 
    3924         [ #  # ]:          0 :                                 if (rt->nh) {
    3925                 :            :                                         rc = ip6_del_cached_rt_nh(cfg, rt);
    3926         [ #  # ]:          0 :                                 } else if (cfg->fc_nh_id) {
    3927                 :          0 :                                         continue;
    3928                 :            :                                 } else {
    3929                 :          0 :                                         nh = rt->fib6_nh;
    3930                 :          0 :                                         rc = ip6_del_cached_rt(cfg, rt, nh);
    3931                 :            :                                 }
    3932         [ #  # ]:          0 :                                 if (rc != -ESRCH) {
    3933                 :            :                                         rcu_read_unlock();
    3934                 :          0 :                                         return rc;
    3935                 :            :                                 }
    3936                 :          0 :                                 continue;
    3937                 :            :                         }
    3938                 :            : 
    3939   [ #  #  #  # ]:          0 :                         if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
    3940                 :          0 :                                 continue;
    3941   [ #  #  #  # ]:          0 :                         if (cfg->fc_protocol &&
    3942                 :          0 :                             cfg->fc_protocol != rt->fib6_protocol)
    3943                 :          0 :                                 continue;
    3944                 :            : 
    3945         [ #  # ]:          0 :                         if (rt->nh) {
    3946         [ #  # ]:          0 :                                 if (!fib6_info_hold_safe(rt))
    3947                 :          0 :                                         continue;
    3948                 :            :                                 rcu_read_unlock();
    3949                 :            : 
    3950                 :          0 :                                 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
    3951                 :            :                         }
    3952         [ #  # ]:          0 :                         if (cfg->fc_nh_id)
    3953                 :          0 :                                 continue;
    3954                 :            : 
    3955                 :            :                         nh = rt->fib6_nh;
    3956   [ #  #  #  # ]:          0 :                         if (cfg->fc_ifindex &&
    3957         [ #  # ]:          0 :                             (!nh->fib_nh_dev ||
    3958                 :          0 :                              nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
    3959                 :          0 :                                 continue;
    3960   [ #  #  #  # ]:          0 :                         if (cfg->fc_flags & RTF_GATEWAY &&
    3961                 :            :                             !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
    3962                 :          0 :                                 continue;
    3963         [ #  # ]:          0 :                         if (!fib6_info_hold_safe(rt))
    3964                 :          0 :                                 continue;
    3965                 :            :                         rcu_read_unlock();
    3966                 :            : 
    3967                 :            :                         /* if gateway was specified only delete the one hop */
    3968         [ #  # ]:          0 :                         if (cfg->fc_flags & RTF_GATEWAY)
    3969                 :          0 :                                 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
    3970                 :            : 
    3971                 :          0 :                         return __ip6_del_rt_siblings(rt, cfg);
    3972                 :            :                 }
    3973                 :            :         }
    3974                 :            :         rcu_read_unlock();
    3975                 :            : 
    3976                 :          0 :         return err;
    3977                 :            : }
    3978                 :            : 
    3979                 :          0 : static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
    3980                 :            : {
    3981                 :            :         struct netevent_redirect netevent;
    3982                 :            :         struct rt6_info *rt, *nrt = NULL;
    3983                 :          0 :         struct fib6_result res = {};
    3984                 :            :         struct ndisc_options ndopts;
    3985                 :            :         struct inet6_dev *in6_dev;
    3986                 :            :         struct neighbour *neigh;
    3987                 :            :         struct rd_msg *msg;
    3988                 :            :         int optlen, on_link;
    3989                 :            :         u8 *lladdr;
    3990                 :            : 
    3991                 :          0 :         optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
    3992                 :          0 :         optlen -= sizeof(*msg);
    3993                 :            : 
    3994         [ #  # ]:          0 :         if (optlen < 0) {
    3995                 :            :                 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
    3996                 :          0 :                 return;
    3997                 :            :         }
    3998                 :            : 
    3999                 :            :         msg = (struct rd_msg *)icmp6_hdr(skb);
    4000                 :            : 
    4001         [ #  # ]:          0 :         if (ipv6_addr_is_multicast(&msg->dest)) {
    4002                 :            :                 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
    4003                 :            :                 return;
    4004                 :            :         }
    4005                 :            : 
    4006                 :            :         on_link = 0;
    4007         [ #  # ]:          0 :         if (ipv6_addr_equal(&msg->dest, &msg->target)) {
    4008                 :            :                 on_link = 1;
    4009         [ #  # ]:          0 :         } else if (ipv6_addr_type(&msg->target) !=
    4010                 :            :                    (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
    4011                 :            :                 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
    4012                 :            :                 return;
    4013                 :            :         }
    4014                 :            : 
    4015                 :          0 :         in6_dev = __in6_dev_get(skb->dev);
    4016         [ #  # ]:          0 :         if (!in6_dev)
    4017                 :            :                 return;
    4018   [ #  #  #  # ]:          0 :         if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
    4019                 :            :                 return;
    4020                 :            : 
    4021                 :            :         /* RFC2461 8.1:
    4022                 :            :          *      The IP source address of the Redirect MUST be the same as the current
    4023                 :            :          *      first-hop router for the specified ICMP Destination Address.
    4024                 :            :          */
    4025                 :            : 
    4026         [ #  # ]:          0 :         if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
    4027                 :            :                 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
    4028                 :            :                 return;
    4029                 :            :         }
    4030                 :            : 
    4031                 :            :         lladdr = NULL;
    4032         [ #  # ]:          0 :         if (ndopts.nd_opts_tgt_lladdr) {
    4033                 :          0 :                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
    4034                 :            :                                              skb->dev);
    4035         [ #  # ]:          0 :                 if (!lladdr) {
    4036                 :            :                         net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
    4037                 :            :                         return;
    4038                 :            :                 }
    4039                 :            :         }
    4040                 :            : 
    4041                 :            :         rt = (struct rt6_info *) dst;
    4042         [ #  # ]:          0 :         if (rt->rt6i_flags & RTF_REJECT) {
    4043                 :            :                 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
    4044                 :            :                 return;
    4045                 :            :         }
    4046                 :            : 
    4047                 :            :         /* Redirect received -> path was valid.
    4048                 :            :          * Look, redirects are sent only in response to data packets,
    4049                 :            :          * so that this nexthop apparently is reachable. --ANK
    4050                 :            :          */
    4051                 :          0 :         dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
    4052                 :            : 
    4053                 :          0 :         neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
    4054         [ #  # ]:          0 :         if (!neigh)
    4055                 :            :                 return;
    4056                 :            : 
    4057                 :            :         /*
    4058                 :            :          *      We have finally decided to accept it.
    4059                 :            :          */
    4060                 :            : 
    4061         [ #  # ]:          0 :         ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
    4062                 :            :                      NEIGH_UPDATE_F_WEAK_OVERRIDE|
    4063                 :            :                      NEIGH_UPDATE_F_OVERRIDE|
    4064                 :            :                      (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
    4065                 :            :                                      NEIGH_UPDATE_F_ISROUTER)),
    4066                 :            :                      NDISC_REDIRECT, &ndopts);
    4067                 :            : 
    4068                 :            :         rcu_read_lock();
    4069                 :          0 :         res.f6i = rcu_dereference(rt->from);
    4070         [ #  # ]:          0 :         if (!res.f6i)
    4071                 :            :                 goto out;
    4072                 :            : 
    4073         [ #  # ]:          0 :         if (res.f6i->nh) {
    4074                 :          0 :                 struct fib6_nh_match_arg arg = {
    4075                 :          0 :                         .dev = dst->dev,
    4076                 :          0 :                         .gw = &rt->rt6i_gateway,
    4077                 :            :                 };
    4078                 :            : 
    4079                 :          0 :                 nexthop_for_each_fib6_nh(res.f6i->nh,
    4080                 :            :                                          fib6_nh_find_match, &arg);
    4081                 :            : 
    4082                 :            :                 /* fib6_info uses a nexthop that does not have fib6_nh
    4083                 :            :                  * using the dst->dev. Should be impossible
    4084                 :            :                  */
    4085         [ #  # ]:          0 :                 if (!arg.match)
    4086                 :            :                         goto out;
    4087                 :          0 :                 res.nh = arg.match;
    4088                 :            :         } else {
    4089                 :          0 :                 res.nh = res.f6i->fib6_nh;
    4090                 :            :         }
    4091                 :            : 
    4092                 :          0 :         res.fib6_flags = res.f6i->fib6_flags;
    4093                 :          0 :         res.fib6_type = res.f6i->fib6_type;
    4094                 :          0 :         nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
    4095         [ #  # ]:          0 :         if (!nrt)
    4096                 :            :                 goto out;
    4097                 :            : 
    4098                 :          0 :         nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
    4099         [ #  # ]:          0 :         if (on_link)
    4100                 :          0 :                 nrt->rt6i_flags &= ~RTF_GATEWAY;
    4101                 :            : 
    4102                 :          0 :         nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
    4103                 :            : 
    4104                 :            :         /* rt6_insert_exception() will take care of duplicated exceptions */
    4105         [ #  # ]:          0 :         if (rt6_insert_exception(nrt, &res)) {
    4106                 :          0 :                 dst_release_immediate(&nrt->dst);
    4107                 :          0 :                 goto out;
    4108                 :            :         }
    4109                 :            : 
    4110                 :          0 :         netevent.old = &rt->dst;
    4111                 :          0 :         netevent.new = &nrt->dst;
    4112                 :          0 :         netevent.daddr = &msg->dest;
    4113                 :          0 :         netevent.neigh = neigh;
    4114                 :          0 :         call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
    4115                 :            : 
    4116                 :            : out:
    4117                 :            :         rcu_read_unlock();
    4118                 :          0 :         neigh_release(neigh);
    4119                 :            : }
    4120                 :            : 
    4121                 :            : #ifdef CONFIG_IPV6_ROUTE_INFO
    4122                 :          0 : static struct fib6_info *rt6_get_route_info(struct net *net,
    4123                 :            :                                            const struct in6_addr *prefix, int prefixlen,
    4124                 :            :                                            const struct in6_addr *gwaddr,
    4125                 :            :                                            struct net_device *dev)
    4126                 :            : {
    4127         [ #  # ]:          0 :         u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
    4128                 :          0 :         int ifindex = dev->ifindex;
    4129                 :            :         struct fib6_node *fn;
    4130                 :            :         struct fib6_info *rt = NULL;
    4131                 :            :         struct fib6_table *table;
    4132                 :            : 
    4133                 :          0 :         table = fib6_get_table(net, tb_id);
    4134         [ #  # ]:          0 :         if (!table)
    4135                 :            :                 return NULL;
    4136                 :            : 
    4137                 :            :         rcu_read_lock();
    4138                 :          0 :         fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
    4139         [ #  # ]:          0 :         if (!fn)
    4140                 :            :                 goto out;
    4141                 :            : 
    4142         [ #  # ]:          0 :         for_each_fib6_node_rt_rcu(fn) {
    4143                 :            :                 /* these routes do not use nexthops */
    4144         [ #  # ]:          0 :                 if (rt->nh)
    4145                 :          0 :                         continue;
    4146         [ #  # ]:          0 :                 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
    4147                 :          0 :                         continue;
    4148   [ #  #  #  # ]:          0 :                 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
    4149                 :          0 :                     !rt->fib6_nh->fib_nh_gw_family)
    4150                 :          0 :                         continue;
    4151         [ #  # ]:          0 :                 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
    4152                 :          0 :                         continue;
    4153         [ #  # ]:          0 :                 if (!fib6_info_hold_safe(rt))
    4154                 :          0 :                         continue;
    4155                 :            :                 break;
    4156                 :            :         }
    4157                 :            : out:
    4158                 :            :         rcu_read_unlock();
    4159                 :          0 :         return rt;
    4160                 :            : }
    4161                 :            : 
    4162                 :          0 : static struct fib6_info *rt6_add_route_info(struct net *net,
    4163                 :            :                                            const struct in6_addr *prefix, int prefixlen,
    4164                 :            :                                            const struct in6_addr *gwaddr,
    4165                 :            :                                            struct net_device *dev,
    4166                 :            :                                            unsigned int pref)
    4167                 :            : {
    4168                 :          0 :         struct fib6_config cfg = {
    4169                 :            :                 .fc_metric      = IP6_RT_PRIO_USER,
    4170                 :          0 :                 .fc_ifindex     = dev->ifindex,
    4171                 :            :                 .fc_dst_len     = prefixlen,
    4172                 :            :                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
    4173                 :          0 :                                   RTF_UP | RTF_PREF(pref),
    4174                 :            :                 .fc_protocol = RTPROT_RA,
    4175                 :            :                 .fc_type = RTN_UNICAST,
    4176                 :            :                 .fc_nlinfo.portid = 0,
    4177                 :            :                 .fc_nlinfo.nlh = NULL,
    4178                 :            :                 .fc_nlinfo.nl_net = net,
    4179                 :            :         };
    4180                 :            : 
    4181         [ #  # ]:          0 :         cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
    4182                 :          0 :         cfg.fc_dst = *prefix;
    4183                 :          0 :         cfg.fc_gateway = *gwaddr;
    4184                 :            : 
    4185                 :            :         /* We should treat it as a default route if prefix length is 0. */
    4186         [ #  # ]:          0 :         if (!prefixlen)
    4187                 :          0 :                 cfg.fc_flags |= RTF_DEFAULT;
    4188                 :            : 
    4189                 :          0 :         ip6_route_add(&cfg, GFP_ATOMIC, NULL);
    4190                 :            : 
    4191                 :          0 :         return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
    4192                 :            : }
    4193                 :            : #endif
    4194                 :            : 
    4195                 :          0 : struct fib6_info *rt6_get_dflt_router(struct net *net,
    4196                 :            :                                      const struct in6_addr *addr,
    4197                 :            :                                      struct net_device *dev)
    4198                 :            : {
    4199         [ #  # ]:          0 :         u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
    4200                 :            :         struct fib6_info *rt;
    4201                 :            :         struct fib6_table *table;
    4202                 :            : 
    4203                 :          0 :         table = fib6_get_table(net, tb_id);
    4204         [ #  # ]:          0 :         if (!table)
    4205                 :            :                 return NULL;
    4206                 :            : 
    4207                 :            :         rcu_read_lock();
    4208         [ #  # ]:          0 :         for_each_fib6_node_rt_rcu(&table->tb6_root) {
    4209                 :            :                 struct fib6_nh *nh;
    4210                 :            : 
    4211                 :            :                 /* RA routes do not use nexthops */
    4212         [ #  # ]:          0 :                 if (rt->nh)
    4213                 :          0 :                         continue;
    4214                 :            : 
    4215                 :            :                 nh = rt->fib6_nh;
    4216   [ #  #  #  # ]:          0 :                 if (dev == nh->fib_nh_dev &&
    4217         [ #  # ]:          0 :                     ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
    4218                 :            :                     ipv6_addr_equal(&nh->fib_nh_gw6, addr))
    4219                 :            :                         break;
    4220                 :            :         }
    4221   [ #  #  #  # ]:          0 :         if (rt && !fib6_info_hold_safe(rt))
    4222                 :            :                 rt = NULL;
    4223                 :            :         rcu_read_unlock();
    4224                 :          0 :         return rt;
    4225                 :            : }
    4226                 :            : 
    4227                 :          0 : struct fib6_info *rt6_add_dflt_router(struct net *net,
    4228                 :            :                                      const struct in6_addr *gwaddr,
    4229                 :            :                                      struct net_device *dev,
    4230                 :            :                                      unsigned int pref)
    4231                 :            : {
    4232                 :          0 :         struct fib6_config cfg = {
    4233         [ #  # ]:          0 :                 .fc_table       = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
    4234                 :            :                 .fc_metric      = IP6_RT_PRIO_USER,
    4235                 :          0 :                 .fc_ifindex     = dev->ifindex,
    4236                 :            :                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
    4237                 :          0 :                                   RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
    4238                 :            :                 .fc_protocol = RTPROT_RA,
    4239                 :            :                 .fc_type = RTN_UNICAST,
    4240                 :            :                 .fc_nlinfo.portid = 0,
    4241                 :            :                 .fc_nlinfo.nlh = NULL,
    4242                 :            :                 .fc_nlinfo.nl_net = net,
    4243                 :            :         };
    4244                 :            : 
    4245                 :          0 :         cfg.fc_gateway = *gwaddr;
    4246                 :            : 
    4247         [ #  # ]:          0 :         if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
    4248                 :            :                 struct fib6_table *table;
    4249                 :            : 
    4250                 :          0 :                 table = fib6_get_table(dev_net(dev), cfg.fc_table);
    4251         [ #  # ]:          0 :                 if (table)
    4252                 :          0 :                         table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
    4253                 :            :         }
    4254                 :            : 
    4255                 :          0 :         return rt6_get_dflt_router(net, gwaddr, dev);
    4256                 :            : }
    4257                 :            : 
    4258                 :          0 : static void __rt6_purge_dflt_routers(struct net *net,
    4259                 :            :                                      struct fib6_table *table)
    4260                 :            : {
    4261                 :            :         struct fib6_info *rt;
    4262                 :            : 
    4263                 :            : restart:
    4264                 :            :         rcu_read_lock();
    4265         [ #  # ]:          0 :         for_each_fib6_node_rt_rcu(&table->tb6_root) {
    4266                 :          0 :                 struct net_device *dev = fib6_info_nh_dev(rt);
    4267         [ #  # ]:          0 :                 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
    4268                 :            : 
    4269   [ #  #  #  # ]:          0 :                 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
    4270   [ #  #  #  # ]:          0 :                     (!idev || idev->cnf.accept_ra != 2) &&
    4271                 :            :                     fib6_info_hold_safe(rt)) {
    4272                 :            :                         rcu_read_unlock();
    4273                 :          0 :                         ip6_del_rt(net, rt);
    4274                 :          0 :                         goto restart;
    4275                 :            :                 }
    4276                 :            :         }
    4277                 :            :         rcu_read_unlock();
    4278                 :            : 
    4279                 :          0 :         table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
    4280                 :          0 : }
    4281                 :            : 
    4282                 :          0 : void rt6_purge_dflt_routers(struct net *net)
    4283                 :            : {
    4284                 :            :         struct fib6_table *table;
    4285                 :            :         struct hlist_head *head;
    4286                 :            :         unsigned int h;
    4287                 :            : 
    4288                 :            :         rcu_read_lock();
    4289                 :            : 
    4290         [ #  # ]:          0 :         for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
    4291                 :          0 :                 head = &net->ipv6.fib_table_hash[h];
    4292   [ #  #  #  #  :          0 :                 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
                   #  # ]
    4293         [ #  # ]:          0 :                         if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
    4294                 :          0 :                                 __rt6_purge_dflt_routers(net, table);
    4295                 :            :                 }
    4296                 :            :         }
    4297                 :            : 
    4298                 :            :         rcu_read_unlock();
    4299                 :          0 : }
    4300                 :            : 
    4301                 :          0 : static void rtmsg_to_fib6_config(struct net *net,
    4302                 :            :                                  struct in6_rtmsg *rtmsg,
    4303                 :            :                                  struct fib6_config *cfg)
    4304                 :            : {
    4305                 :          0 :         *cfg = (struct fib6_config){
    4306                 :          0 :                 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
    4307         [ #  # ]:          0 :                          : RT6_TABLE_MAIN,
    4308                 :          0 :                 .fc_ifindex = rtmsg->rtmsg_ifindex,
    4309         [ #  # ]:          0 :                 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
    4310                 :          0 :                 .fc_expires = rtmsg->rtmsg_info,
    4311                 :          0 :                 .fc_dst_len = rtmsg->rtmsg_dst_len,
    4312                 :          0 :                 .fc_src_len = rtmsg->rtmsg_src_len,
    4313                 :          0 :                 .fc_flags = rtmsg->rtmsg_flags,
    4314                 :          0 :                 .fc_type = rtmsg->rtmsg_type,
    4315                 :            : 
    4316                 :            :                 .fc_nlinfo.nl_net = net,
    4317                 :            : 
    4318                 :          0 :                 .fc_dst = rtmsg->rtmsg_dst,
    4319                 :          0 :                 .fc_src = rtmsg->rtmsg_src,
    4320                 :          0 :                 .fc_gateway = rtmsg->rtmsg_gateway,
    4321                 :            :         };
    4322                 :          0 : }
    4323                 :            : 
    4324                 :          0 : int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
    4325                 :            : {
    4326                 :            :         struct fib6_config cfg;
    4327                 :            :         struct in6_rtmsg rtmsg;
    4328                 :            :         int err;
    4329                 :            : 
    4330         [ #  # ]:          0 :         switch (cmd) {
    4331                 :            :         case SIOCADDRT:         /* Add a route */
    4332                 :            :         case SIOCDELRT:         /* Delete a route */
    4333         [ #  # ]:          0 :                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    4334                 :            :                         return -EPERM;
    4335                 :            :                 err = copy_from_user(&rtmsg, arg,
    4336                 :            :                                      sizeof(struct in6_rtmsg));
    4337         [ #  # ]:          0 :                 if (err)
    4338                 :            :                         return -EFAULT;
    4339                 :            : 
    4340                 :          0 :                 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
    4341                 :            : 
    4342                 :          0 :                 rtnl_lock();
    4343      [ #  #  # ]:          0 :                 switch (cmd) {
    4344                 :            :                 case SIOCADDRT:
    4345                 :          0 :                         err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
    4346                 :          0 :                         break;
    4347                 :            :                 case SIOCDELRT:
    4348                 :          0 :                         err = ip6_route_del(&cfg, NULL);
    4349                 :          0 :                         break;
    4350                 :            :                 default:
    4351                 :            :                         err = -EINVAL;
    4352                 :            :                 }
    4353                 :          0 :                 rtnl_unlock();
    4354                 :            : 
    4355                 :          0 :                 return err;
    4356                 :            :         }
    4357                 :            : 
    4358                 :            :         return -EINVAL;
    4359                 :            : }
    4360                 :            : 
    4361                 :            : /*
    4362                 :            :  *      Drop the packet on the floor
    4363                 :            :  */
    4364                 :            : 
    4365                 :          0 : static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
    4366                 :            : {
    4367                 :            :         struct dst_entry *dst = skb_dst(skb);
    4368                 :          0 :         struct net *net = dev_net(dst->dev);
    4369                 :            :         struct inet6_dev *idev;
    4370                 :            :         int type;
    4371                 :            : 
    4372   [ #  #  #  # ]:          0 :         if (netif_is_l3_master(skb->dev) &&
    4373                 :          0 :             dst->dev == net->loopback_dev)
    4374                 :          0 :                 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
    4375                 :            :         else
    4376                 :            :                 idev = ip6_dst_idev(dst);
    4377                 :            : 
    4378      [ #  #  # ]:          0 :         switch (ipstats_mib_noroutes) {
    4379                 :            :         case IPSTATS_MIB_INNOROUTES:
    4380                 :          0 :                 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
    4381         [ #  # ]:          0 :                 if (type == IPV6_ADDR_ANY) {
    4382         [ #  # ]:          0 :                         IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
    4383                 :            :                         break;
    4384                 :            :                 }
    4385                 :            :                 /* FALLTHROUGH */
    4386                 :            :         case IPSTATS_MIB_OUTNOROUTES:
    4387         [ #  # ]:          0 :                 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
    4388                 :            :                 break;
    4389                 :            :         }
    4390                 :            : 
    4391                 :            :         /* Start over by dropping the dst for l3mdev case */
    4392         [ #  # ]:          0 :         if (netif_is_l3_master(skb->dev))
    4393                 :          0 :                 skb_dst_drop(skb);
    4394                 :            : 
    4395                 :          0 :         icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
    4396                 :          0 :         kfree_skb(skb);
    4397                 :          0 :         return 0;
    4398                 :            : }
    4399                 :            : 
    4400                 :          0 : static int ip6_pkt_discard(struct sk_buff *skb)
    4401                 :            : {
    4402                 :          0 :         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
    4403                 :            : }
    4404                 :            : 
    4405                 :          0 : static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
    4406                 :            : {
    4407                 :          0 :         skb->dev = skb_dst(skb)->dev;
    4408                 :          0 :         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
    4409                 :            : }
    4410                 :            : 
    4411                 :          0 : static int ip6_pkt_prohibit(struct sk_buff *skb)
    4412                 :            : {
    4413                 :          0 :         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
    4414                 :            : }
    4415                 :            : 
    4416                 :          0 : static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
    4417                 :            : {
    4418                 :          0 :         skb->dev = skb_dst(skb)->dev;
    4419                 :          0 :         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
    4420                 :            : }
    4421                 :            : 
    4422                 :            : /*
    4423                 :            :  *      Allocate a dst for local (unicast / anycast) address.
    4424                 :            :  */
    4425                 :            : 
    4426                 :        850 : struct fib6_info *addrconf_f6i_alloc(struct net *net,
    4427                 :            :                                      struct inet6_dev *idev,
    4428                 :            :                                      const struct in6_addr *addr,
    4429                 :            :                                      bool anycast, gfp_t gfp_flags)
    4430                 :            : {
    4431                 :       2550 :         struct fib6_config cfg = {
    4432         [ -  + ]:       1700 :                 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
    4433                 :        850 :                 .fc_ifindex = idev->dev->ifindex,
    4434                 :            :                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
    4435                 :            :                 .fc_dst = *addr,
    4436                 :            :                 .fc_dst_len = 128,
    4437                 :            :                 .fc_protocol = RTPROT_KERNEL,
    4438                 :            :                 .fc_nlinfo.nl_net = net,
    4439                 :            :                 .fc_ignore_dev_down = true,
    4440                 :            :         };
    4441                 :            :         struct fib6_info *f6i;
    4442                 :            : 
    4443         [ -  + ]:        850 :         if (anycast) {
    4444                 :          0 :                 cfg.fc_type = RTN_ANYCAST;
    4445                 :          0 :                 cfg.fc_flags |= RTF_ANYCAST;
    4446                 :            :         } else {
    4447                 :        850 :                 cfg.fc_type = RTN_LOCAL;
    4448                 :        850 :                 cfg.fc_flags |= RTF_LOCAL;
    4449                 :            :         }
    4450                 :            : 
    4451                 :        850 :         f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
    4452         [ +  - ]:        850 :         if (!IS_ERR(f6i))
    4453                 :        850 :                 f6i->dst_nocount = true;
    4454                 :        850 :         return f6i;
    4455                 :            : }
    4456                 :            : 
    4457                 :            : /* remove deleted ip from prefsrc entries */
    4458                 :            : struct arg_dev_net_ip {
    4459                 :            :         struct net_device *dev;
    4460                 :            :         struct net *net;
    4461                 :            :         struct in6_addr *addr;
    4462                 :            : };
    4463                 :            : 
    4464                 :       1035 : static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
    4465                 :            : {
    4466                 :       1035 :         struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
    4467                 :       1035 :         struct net *net = ((struct arg_dev_net_ip *)arg)->net;
    4468                 :       1035 :         struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
    4469                 :            : 
    4470   [ +  -  +  + ]:       2070 :         if (!rt->nh &&
    4471   [ -  +  +  - ]:       2070 :             ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
    4472         [ -  + ]:        414 :             rt != net->ipv6.fib6_null_entry &&
    4473                 :            :             ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
    4474                 :            :                 spin_lock_bh(&rt6_exception_lock);
    4475                 :            :                 /* remove prefsrc entry */
    4476                 :          0 :                 rt->fib6_prefsrc.plen = 0;
    4477                 :            :                 spin_unlock_bh(&rt6_exception_lock);
    4478                 :            :         }
    4479                 :       1035 :         return 0;
    4480                 :            : }
    4481                 :            : 
    4482                 :        207 : void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
    4483                 :            : {
    4484                 :        207 :         struct net *net = dev_net(ifp->idev->dev);
    4485                 :        414 :         struct arg_dev_net_ip adni = {
    4486                 :            :                 .dev = ifp->idev->dev,
    4487                 :            :                 .net = net,
    4488                 :        207 :                 .addr = &ifp->addr,
    4489                 :            :         };
    4490                 :        207 :         fib6_clean_all(net, fib6_remove_prefsrc, &adni);
    4491                 :        207 : }
    4492                 :            : 
    4493                 :            : #define RTF_RA_ROUTER           (RTF_ADDRCONF | RTF_DEFAULT)
    4494                 :            : 
    4495                 :            : /* Remove routers and update dst entries when gateway turn into host. */
    4496                 :          0 : static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
    4497                 :            : {
    4498                 :            :         struct in6_addr *gateway = (struct in6_addr *)arg;
    4499                 :            :         struct fib6_nh *nh;
    4500                 :            : 
    4501                 :            :         /* RA routes do not use nexthops */
    4502         [ #  # ]:          0 :         if (rt->nh)
    4503                 :            :                 return 0;
    4504                 :            : 
    4505                 :          0 :         nh = rt->fib6_nh;
    4506   [ #  #  #  # ]:          0 :         if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
    4507         [ #  # ]:          0 :             nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
    4508                 :            :                 return -1;
    4509                 :            : 
    4510                 :            :         /* Further clean up cached routes in exception table.
    4511                 :            :          * This is needed because cached route may have a different
    4512                 :            :          * gateway than its 'parent' in the case of an ip redirect.
    4513                 :            :          */
    4514                 :          0 :         fib6_nh_exceptions_clean_tohost(nh, gateway);
    4515                 :            : 
    4516                 :          0 :         return 0;
    4517                 :            : }
    4518                 :            : 
    4519                 :          0 : void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
    4520                 :            : {
    4521                 :          0 :         fib6_clean_all(net, fib6_clean_tohost, gateway);
    4522                 :          0 : }
    4523                 :            : 
    4524                 :            : struct arg_netdev_event {
    4525                 :            :         const struct net_device *dev;
    4526                 :            :         union {
    4527                 :            :                 unsigned char nh_flags;
    4528                 :            :                 unsigned long event;
    4529                 :            :         };
    4530                 :            : };
    4531                 :            : 
    4532                 :          0 : static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
    4533                 :            : {
    4534                 :            :         struct fib6_info *iter;
    4535                 :            :         struct fib6_node *fn;
    4536                 :            : 
    4537                 :          0 :         fn = rcu_dereference_protected(rt->fib6_node,
    4538                 :            :                         lockdep_is_held(&rt->fib6_table->tb6_lock));
    4539                 :          0 :         iter = rcu_dereference_protected(fn->leaf,
    4540                 :            :                         lockdep_is_held(&rt->fib6_table->tb6_lock));
    4541         [ #  # ]:          0 :         while (iter) {
    4542   [ #  #  #  # ]:          0 :                 if (iter->fib6_metric == rt->fib6_metric &&
    4543                 :            :                     rt6_qualify_for_ecmp(iter))
    4544                 :          0 :                         return iter;
    4545                 :          0 :                 iter = rcu_dereference_protected(iter->fib6_next,
    4546                 :            :                                 lockdep_is_held(&rt->fib6_table->tb6_lock));
    4547                 :            :         }
    4548                 :            : 
    4549                 :            :         return NULL;
    4550                 :            : }
    4551                 :            : 
    4552                 :            : /* only called for fib entries with builtin fib6_nh */
    4553                 :            : static bool rt6_is_dead(const struct fib6_info *rt)
    4554                 :            : {
    4555   [ #  #  #  #  :          0 :         if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
          #  #  #  #  #  
                #  #  # ]
    4556   [ #  #  #  #  :          0 :             (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
                   #  # ]
    4557                 :          0 :              ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
    4558                 :            :                 return true;
    4559                 :            : 
    4560                 :            :         return false;
    4561                 :            : }
    4562                 :            : 
    4563                 :          0 : static int rt6_multipath_total_weight(const struct fib6_info *rt)
    4564                 :            : {
    4565                 :            :         struct fib6_info *iter;
    4566                 :            :         int total = 0;
    4567                 :            : 
    4568         [ #  # ]:          0 :         if (!rt6_is_dead(rt))
    4569                 :          0 :                 total += rt->fib6_nh->fib_nh_weight;
    4570                 :            : 
    4571         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
    4572         [ #  # ]:          0 :                 if (!rt6_is_dead(iter))
    4573                 :          0 :                         total += iter->fib6_nh->fib_nh_weight;
    4574                 :            :         }
    4575                 :            : 
    4576                 :          0 :         return total;
    4577                 :            : }
    4578                 :            : 
    4579                 :          0 : static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
    4580                 :            : {
    4581                 :            :         int upper_bound = -1;
    4582                 :            : 
    4583         [ #  # ]:          0 :         if (!rt6_is_dead(rt)) {
    4584                 :          0 :                 *weight += rt->fib6_nh->fib_nh_weight;
    4585   [ #  #  #  #  :          0 :                 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4586                 :            :                                                     total) - 1;
    4587                 :            :         }
    4588                 :            :         atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
    4589                 :          0 : }
    4590                 :            : 
    4591                 :          0 : static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
    4592                 :            : {
    4593                 :            :         struct fib6_info *iter;
    4594                 :          0 :         int weight = 0;
    4595                 :            : 
    4596                 :          0 :         rt6_upper_bound_set(rt, &weight, total);
    4597                 :            : 
    4598         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
    4599                 :          0 :                 rt6_upper_bound_set(iter, &weight, total);
    4600                 :          0 : }
    4601                 :            : 
    4602                 :       1634 : void rt6_multipath_rebalance(struct fib6_info *rt)
    4603                 :            : {
    4604                 :            :         struct fib6_info *first;
    4605                 :            :         int total;
    4606                 :            : 
    4607                 :            :         /* In case the entire multipath route was marked for flushing,
    4608                 :            :          * then there is no need to rebalance upon the removal of every
    4609                 :            :          * sibling route.
    4610                 :            :          */
    4611   [ -  +  #  # ]:       1634 :         if (!rt->fib6_nsiblings || rt->should_flush)
    4612                 :            :                 return;
    4613                 :            : 
    4614                 :            :         /* During lookup routes are evaluated in order, so we need to
    4615                 :            :          * make sure upper bounds are assigned from the first sibling
    4616                 :            :          * onwards.
    4617                 :            :          */
    4618                 :          0 :         first = rt6_multipath_first_sibling(rt);
    4619   [ #  #  #  #  :          0 :         if (WARN_ON_ONCE(!first))
                   #  # ]
    4620                 :            :                 return;
    4621                 :            : 
    4622                 :          0 :         total = rt6_multipath_total_weight(first);
    4623                 :          0 :         rt6_multipath_upper_bound_set(first, total);
    4624                 :            : }
    4625                 :            : 
    4626                 :       4946 : static int fib6_ifup(struct fib6_info *rt, void *p_arg)
    4627                 :            : {
    4628                 :            :         const struct arg_netdev_event *arg = p_arg;
    4629                 :       4946 :         struct net *net = dev_net(arg->dev);
    4630                 :            : 
    4631   [ +  +  +  -  :       7822 :         if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
                   +  + ]
    4632                 :       2876 :             rt->fib6_nh->fib_nh_dev == arg->dev) {
    4633                 :       1220 :                 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
    4634                 :       1220 :                 fib6_update_sernum_upto_root(net, rt);
    4635                 :       1220 :                 rt6_multipath_rebalance(rt);
    4636                 :            :         }
    4637                 :            : 
    4638                 :       4946 :         return 0;
    4639                 :            : }
    4640                 :            : 
    4641                 :       1035 : void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
    4642                 :            : {
    4643                 :       1035 :         struct arg_netdev_event arg = {
    4644                 :            :                 .dev = dev,
    4645                 :            :                 {
    4646                 :            :                         .nh_flags = nh_flags,
    4647                 :            :                 },
    4648                 :            :         };
    4649                 :            : 
    4650   [ +  +  +  - ]:       1449 :         if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
    4651                 :        414 :                 arg.nh_flags |= RTNH_F_LINKDOWN;
    4652                 :            : 
    4653                 :       1035 :         fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
    4654                 :       1035 : }
    4655                 :            : 
    4656                 :            : /* only called for fib entries with inline fib6_nh */
    4657                 :            : static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
    4658                 :            :                                    const struct net_device *dev)
    4659                 :            : {
    4660                 :            :         struct fib6_info *iter;
    4661                 :            : 
    4662         [ #  # ]:          0 :         if (rt->fib6_nh->fib_nh_dev == dev)
    4663                 :            :                 return true;
    4664         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
    4665         [ #  # ]:          0 :                 if (iter->fib6_nh->fib_nh_dev == dev)
    4666                 :            :                         return true;
    4667                 :            : 
    4668                 :            :         return false;
    4669                 :            : }
    4670                 :            : 
    4671                 :            : static void rt6_multipath_flush(struct fib6_info *rt)
    4672                 :            : {
    4673                 :            :         struct fib6_info *iter;
    4674                 :            : 
    4675                 :          0 :         rt->should_flush = 1;
    4676         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
    4677                 :          0 :                 iter->should_flush = 1;
    4678                 :            : }
    4679                 :            : 
    4680                 :          0 : static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
    4681                 :            :                                              const struct net_device *down_dev)
    4682                 :            : {
    4683                 :            :         struct fib6_info *iter;
    4684                 :            :         unsigned int dead = 0;
    4685                 :            : 
    4686   [ #  #  #  # ]:          0 :         if (rt->fib6_nh->fib_nh_dev == down_dev ||
    4687                 :          0 :             rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
    4688                 :            :                 dead++;
    4689         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
    4690   [ #  #  #  # ]:          0 :                 if (iter->fib6_nh->fib_nh_dev == down_dev ||
    4691                 :          0 :                     iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
    4692                 :          0 :                         dead++;
    4693                 :            : 
    4694                 :          0 :         return dead;
    4695                 :            : }
    4696                 :            : 
    4697                 :          0 : static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
    4698                 :            :                                        const struct net_device *dev,
    4699                 :            :                                        unsigned char nh_flags)
    4700                 :            : {
    4701                 :            :         struct fib6_info *iter;
    4702                 :            : 
    4703         [ #  # ]:          0 :         if (rt->fib6_nh->fib_nh_dev == dev)
    4704                 :          0 :                 rt->fib6_nh->fib_nh_flags |= nh_flags;
    4705         [ #  # ]:          0 :         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
    4706         [ #  # ]:          0 :                 if (iter->fib6_nh->fib_nh_dev == dev)
    4707                 :          0 :                         iter->fib6_nh->fib_nh_flags |= nh_flags;
    4708                 :          0 : }
    4709                 :            : 
    4710                 :            : /* called with write lock held for table with rt */
    4711                 :       1245 : static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
    4712                 :            : {
    4713                 :            :         const struct arg_netdev_event *arg = p_arg;
    4714                 :       1245 :         const struct net_device *dev = arg->dev;
    4715                 :            :         struct net *net = dev_net(dev);
    4716                 :            : 
    4717   [ +  +  +  - ]:       1245 :         if (rt == net->ipv6.fib6_null_entry || rt->nh)
    4718                 :            :                 return 0;
    4719                 :            : 
    4720   [ -  -  +  - ]:        831 :         switch (arg->event) {
    4721                 :            :         case NETDEV_UNREGISTER:
    4722         [ #  # ]:          0 :                 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
    4723                 :            :         case NETDEV_DOWN:
    4724         [ #  # ]:          0 :                 if (rt->should_flush)
    4725                 :            :                         return -1;
    4726         [ #  # ]:          0 :                 if (!rt->fib6_nsiblings)
    4727         [ #  # ]:          0 :                         return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
    4728         [ #  # ]:          0 :                 if (rt6_multipath_uses_dev(rt, dev)) {
    4729                 :            :                         unsigned int count;
    4730                 :            : 
    4731                 :          0 :                         count = rt6_multipath_dead_count(rt, dev);
    4732         [ #  # ]:          0 :                         if (rt->fib6_nsiblings + 1 == count) {
    4733                 :            :                                 rt6_multipath_flush(rt);
    4734                 :            :                                 return -1;
    4735                 :            :                         }
    4736                 :          0 :                         rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
    4737                 :            :                                                    RTNH_F_LINKDOWN);
    4738                 :          0 :                         fib6_update_sernum(net, rt);
    4739                 :          0 :                         rt6_multipath_rebalance(rt);
    4740                 :            :                 }
    4741                 :            :                 return -2;
    4742                 :            :         case NETDEV_CHANGE:
    4743   [ +  +  +  + ]:       1248 :                 if (rt->fib6_nh->fib_nh_dev != dev ||
    4744                 :        417 :                     rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
    4745                 :            :                         break;
    4746                 :        414 :                 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
    4747                 :        414 :                 rt6_multipath_rebalance(rt);
    4748                 :        414 :                 break;
    4749                 :            :         }
    4750                 :            : 
    4751                 :            :         return 0;
    4752                 :            : }
    4753                 :            : 
    4754                 :        207 : void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
    4755                 :            : {
    4756                 :        207 :         struct arg_netdev_event arg = {
    4757                 :            :                 .dev = dev,
    4758                 :            :                 {
    4759                 :            :                         .event = event,
    4760                 :            :                 },
    4761                 :            :         };
    4762                 :            :         struct net *net = dev_net(dev);
    4763                 :            : 
    4764         [ -  + ]:        207 :         if (net->ipv6.sysctl.skip_notify_on_dev_down)
    4765                 :          0 :                 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
    4766                 :            :         else
    4767                 :        207 :                 fib6_clean_all(net, fib6_ifdown, &arg);
    4768                 :        207 : }
    4769                 :            : 
    4770                 :          0 : void rt6_disable_ip(struct net_device *dev, unsigned long event)
    4771                 :            : {
    4772                 :          0 :         rt6_sync_down_dev(dev, event);
    4773                 :          0 :         rt6_uncached_list_flush_dev(dev_net(dev), dev);
    4774                 :          0 :         neigh_ifdown(&nd_tbl, dev);
    4775                 :          0 : }
    4776                 :            : 
    4777                 :            : struct rt6_mtu_change_arg {
    4778                 :            :         struct net_device *dev;
    4779                 :            :         unsigned int mtu;
    4780                 :            :         struct fib6_info *f6i;
    4781                 :            : };
    4782                 :            : 
    4783                 :          0 : static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
    4784                 :            : {
    4785                 :            :         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
    4786                 :          0 :         struct fib6_info *f6i = arg->f6i;
    4787                 :            : 
    4788                 :            :         /* For administrative MTU increase, there is no way to discover
    4789                 :            :          * IPv6 PMTU increase, so PMTU increase should be updated here.
    4790                 :            :          * Since RFC 1981 doesn't include administrative MTU increase
    4791                 :            :          * update PMTU increase is a MUST. (i.e. jumbo frame)
    4792                 :            :          */
    4793         [ #  # ]:          0 :         if (nh->fib_nh_dev == arg->dev) {
    4794                 :            :                 struct inet6_dev *idev = __in6_dev_get(arg->dev);
    4795                 :          0 :                 u32 mtu = f6i->fib6_pmtu;
    4796                 :            : 
    4797   [ #  #  #  # ]:          0 :                 if (mtu >= arg->mtu ||
    4798                 :          0 :                     (mtu < arg->mtu && mtu == idev->cnf.mtu6))
    4799                 :          0 :                         fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
    4800                 :            : 
    4801                 :            :                 spin_lock_bh(&rt6_exception_lock);
    4802                 :          0 :                 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
    4803                 :            :                 spin_unlock_bh(&rt6_exception_lock);
    4804                 :            :         }
    4805                 :            : 
    4806                 :          0 :         return 0;
    4807                 :            : }
    4808                 :            : 
    4809                 :          0 : static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
    4810                 :            : {
    4811                 :            :         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
    4812                 :            :         struct inet6_dev *idev;
    4813                 :            : 
    4814                 :            :         /* In IPv6 pmtu discovery is not optional,
    4815                 :            :            so that RTAX_MTU lock cannot disable it.
    4816                 :            :            We still use this lock to block changes
    4817                 :            :            caused by addrconf/ndisc.
    4818                 :            :         */
    4819                 :            : 
    4820                 :          0 :         idev = __in6_dev_get(arg->dev);
    4821         [ #  # ]:          0 :         if (!idev)
    4822                 :            :                 return 0;
    4823                 :            : 
    4824         [ #  # ]:          0 :         if (fib6_metric_locked(f6i, RTAX_MTU))
    4825                 :            :                 return 0;
    4826                 :            : 
    4827                 :          0 :         arg->f6i = f6i;
    4828         [ #  # ]:          0 :         if (f6i->nh) {
    4829                 :            :                 /* fib6_nh_mtu_change only returns 0, so this is safe */
    4830                 :          0 :                 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
    4831                 :            :                                                 arg);
    4832                 :            :         }
    4833                 :            : 
    4834                 :          0 :         return fib6_nh_mtu_change(f6i->fib6_nh, arg);
    4835                 :            : }
    4836                 :            : 
    4837                 :          0 : void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
    4838                 :            : {
    4839                 :          0 :         struct rt6_mtu_change_arg arg = {
    4840                 :            :                 .dev = dev,
    4841                 :            :                 .mtu = mtu,
    4842                 :            :         };
    4843                 :            : 
    4844                 :          0 :         fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
    4845                 :          0 : }
    4846                 :            : 
    4847                 :            : static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
    4848                 :            :         [RTA_UNSPEC]            = { .strict_start_type = RTA_DPORT + 1 },
    4849                 :            :         [RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
    4850                 :            :         [RTA_PREFSRC]           = { .len = sizeof(struct in6_addr) },
    4851                 :            :         [RTA_OIF]               = { .type = NLA_U32 },
    4852                 :            :         [RTA_IIF]               = { .type = NLA_U32 },
    4853                 :            :         [RTA_PRIORITY]          = { .type = NLA_U32 },
    4854                 :            :         [RTA_METRICS]           = { .type = NLA_NESTED },
    4855                 :            :         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
    4856                 :            :         [RTA_PREF]              = { .type = NLA_U8 },
    4857                 :            :         [RTA_ENCAP_TYPE]        = { .type = NLA_U16 },
    4858                 :            :         [RTA_ENCAP]             = { .type = NLA_NESTED },
    4859                 :            :         [RTA_EXPIRES]           = { .type = NLA_U32 },
    4860                 :            :         [RTA_UID]               = { .type = NLA_U32 },
    4861                 :            :         [RTA_MARK]              = { .type = NLA_U32 },
    4862                 :            :         [RTA_TABLE]             = { .type = NLA_U32 },
    4863                 :            :         [RTA_IP_PROTO]          = { .type = NLA_U8 },
    4864                 :            :         [RTA_SPORT]             = { .type = NLA_U16 },
    4865                 :            :         [RTA_DPORT]             = { .type = NLA_U16 },
    4866                 :            :         [RTA_NH_ID]             = { .type = NLA_U32 },
    4867                 :            : };
    4868                 :            : 
    4869                 :          0 : static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
    4870                 :            :                               struct fib6_config *cfg,
    4871                 :            :                               struct netlink_ext_ack *extack)
    4872                 :            : {
    4873                 :            :         struct rtmsg *rtm;
    4874                 :            :         struct nlattr *tb[RTA_MAX+1];
    4875                 :            :         unsigned int pref;
    4876                 :            :         int err;
    4877                 :            : 
    4878                 :            :         err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
    4879                 :            :                                      rtm_ipv6_policy, extack);
    4880         [ #  # ]:          0 :         if (err < 0)
    4881                 :            :                 goto errout;
    4882                 :            : 
    4883                 :            :         err = -EINVAL;
    4884                 :            :         rtm = nlmsg_data(nlh);
    4885                 :            : 
    4886                 :          0 :         *cfg = (struct fib6_config){
    4887                 :          0 :                 .fc_table = rtm->rtm_table,
    4888                 :          0 :                 .fc_dst_len = rtm->rtm_dst_len,
    4889                 :          0 :                 .fc_src_len = rtm->rtm_src_len,
    4890                 :            :                 .fc_flags = RTF_UP,
    4891                 :          0 :                 .fc_protocol = rtm->rtm_protocol,
    4892                 :          0 :                 .fc_type = rtm->rtm_type,
    4893                 :            : 
    4894                 :          0 :                 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
    4895                 :            :                 .fc_nlinfo.nlh = nlh,
    4896                 :          0 :                 .fc_nlinfo.nl_net = sock_net(skb->sk),
    4897                 :            :         };
    4898                 :            : 
    4899         [ #  # ]:          0 :         if (rtm->rtm_type == RTN_UNREACHABLE ||
    4900                 :            :             rtm->rtm_type == RTN_BLACKHOLE ||
    4901                 :          0 :             rtm->rtm_type == RTN_PROHIBIT ||
    4902                 :            :             rtm->rtm_type == RTN_THROW)
    4903                 :          0 :                 cfg->fc_flags |= RTF_REJECT;
    4904                 :            : 
    4905         [ #  # ]:          0 :         if (rtm->rtm_type == RTN_LOCAL)
    4906                 :          0 :                 cfg->fc_flags |= RTF_LOCAL;
    4907                 :            : 
    4908         [ #  # ]:          0 :         if (rtm->rtm_flags & RTM_F_CLONED)
    4909                 :          0 :                 cfg->fc_flags |= RTF_CACHE;
    4910                 :            : 
    4911                 :          0 :         cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
    4912                 :            : 
    4913         [ #  # ]:          0 :         if (tb[RTA_NH_ID]) {
    4914   [ #  #  #  #  :          0 :                 if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
                   #  # ]
    4915         [ #  # ]:          0 :                     tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
    4916         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack,
    4917                 :            :                                        "Nexthop specification and nexthop id are mutually exclusive");
    4918                 :            :                         goto errout;
    4919                 :            :                 }
    4920                 :          0 :                 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
    4921                 :            :         }
    4922                 :            : 
    4923         [ #  # ]:          0 :         if (tb[RTA_GATEWAY]) {
    4924                 :          0 :                 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
    4925                 :          0 :                 cfg->fc_flags |= RTF_GATEWAY;
    4926                 :            :         }
    4927         [ #  # ]:          0 :         if (tb[RTA_VIA]) {
    4928         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
    4929                 :            :                 goto errout;
    4930                 :            :         }
    4931                 :            : 
    4932         [ #  # ]:          0 :         if (tb[RTA_DST]) {
    4933                 :          0 :                 int plen = (rtm->rtm_dst_len + 7) >> 3;
    4934                 :            : 
    4935         [ #  # ]:          0 :                 if (nla_len(tb[RTA_DST]) < plen)
    4936                 :            :                         goto errout;
    4937                 :            : 
    4938                 :          0 :                 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
    4939                 :            :         }
    4940                 :            : 
    4941         [ #  # ]:          0 :         if (tb[RTA_SRC]) {
    4942                 :          0 :                 int plen = (rtm->rtm_src_len + 7) >> 3;
    4943                 :            : 
    4944         [ #  # ]:          0 :                 if (nla_len(tb[RTA_SRC]) < plen)
    4945                 :            :                         goto errout;
    4946                 :            : 
    4947                 :          0 :                 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
    4948                 :            :         }
    4949                 :            : 
    4950         [ #  # ]:          0 :         if (tb[RTA_PREFSRC])
    4951                 :          0 :                 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
    4952                 :            : 
    4953         [ #  # ]:          0 :         if (tb[RTA_OIF])
    4954                 :          0 :                 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
    4955                 :            : 
    4956         [ #  # ]:          0 :         if (tb[RTA_PRIORITY])
    4957                 :          0 :                 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
    4958                 :            : 
    4959         [ #  # ]:          0 :         if (tb[RTA_METRICS]) {
    4960                 :          0 :                 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
    4961                 :          0 :                 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
    4962                 :            :         }
    4963                 :            : 
    4964         [ #  # ]:          0 :         if (tb[RTA_TABLE])
    4965                 :          0 :                 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
    4966                 :            : 
    4967         [ #  # ]:          0 :         if (tb[RTA_MULTIPATH]) {
    4968                 :          0 :                 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
    4969                 :          0 :                 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
    4970                 :            : 
    4971                 :            :                 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
    4972                 :            :                                                      cfg->fc_mp_len, extack);
    4973                 :            :                 if (err < 0)
    4974                 :            :                         goto errout;
    4975                 :            :         }
    4976                 :            : 
    4977         [ #  # ]:          0 :         if (tb[RTA_PREF]) {
    4978                 :          0 :                 pref = nla_get_u8(tb[RTA_PREF]);
    4979         [ #  # ]:          0 :                 if (pref != ICMPV6_ROUTER_PREF_LOW &&
    4980                 :          0 :                     pref != ICMPV6_ROUTER_PREF_HIGH)
    4981                 :            :                         pref = ICMPV6_ROUTER_PREF_MEDIUM;
    4982                 :          0 :                 cfg->fc_flags |= RTF_PREF(pref);
    4983                 :            :         }
    4984                 :            : 
    4985         [ #  # ]:          0 :         if (tb[RTA_ENCAP])
    4986                 :          0 :                 cfg->fc_encap = tb[RTA_ENCAP];
    4987                 :            : 
    4988         [ #  # ]:          0 :         if (tb[RTA_ENCAP_TYPE]) {
    4989                 :          0 :                 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
    4990                 :            : 
    4991                 :            :                 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
    4992                 :            :                 if (err < 0)
    4993                 :            :                         goto errout;
    4994                 :            :         }
    4995                 :            : 
    4996         [ #  # ]:          0 :         if (tb[RTA_EXPIRES]) {
    4997                 :            :                 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
    4998                 :            : 
    4999         [ #  # ]:          0 :                 if (addrconf_finite_timeout(timeout)) {
    5000                 :          0 :                         cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
    5001                 :          0 :                         cfg->fc_flags |= RTF_EXPIRES;
    5002                 :            :                 }
    5003                 :            :         }
    5004                 :            : 
    5005                 :            :         err = 0;
    5006                 :            : errout:
    5007                 :          0 :         return err;
    5008                 :            : }
    5009                 :            : 
    5010                 :            : struct rt6_nh {
    5011                 :            :         struct fib6_info *fib6_info;
    5012                 :            :         struct fib6_config r_cfg;
    5013                 :            :         struct list_head next;
    5014                 :            : };
    5015                 :            : 
    5016                 :          0 : static int ip6_route_info_append(struct net *net,
    5017                 :            :                                  struct list_head *rt6_nh_list,
    5018                 :            :                                  struct fib6_info *rt,
    5019                 :            :                                  struct fib6_config *r_cfg)
    5020                 :            : {
    5021                 :            :         struct rt6_nh *nh;
    5022                 :            :         int err = -EEXIST;
    5023                 :            : 
    5024         [ #  # ]:          0 :         list_for_each_entry(nh, rt6_nh_list, next) {
    5025                 :            :                 /* check if fib6_info already exists */
    5026         [ #  # ]:          0 :                 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
    5027                 :            :                         return err;
    5028                 :            :         }
    5029                 :            : 
    5030                 :          0 :         nh = kzalloc(sizeof(*nh), GFP_KERNEL);
    5031         [ #  # ]:          0 :         if (!nh)
    5032                 :            :                 return -ENOMEM;
    5033                 :          0 :         nh->fib6_info = rt;
    5034                 :          0 :         memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
    5035                 :          0 :         list_add_tail(&nh->next, rt6_nh_list);
    5036                 :            : 
    5037                 :          0 :         return 0;
    5038                 :            : }
    5039                 :            : 
    5040                 :          0 : static void ip6_route_mpath_notify(struct fib6_info *rt,
    5041                 :            :                                    struct fib6_info *rt_last,
    5042                 :            :                                    struct nl_info *info,
    5043                 :            :                                    __u16 nlflags)
    5044                 :            : {
    5045                 :            :         /* if this is an APPEND route, then rt points to the first route
    5046                 :            :          * inserted and rt_last points to last route inserted. Userspace
    5047                 :            :          * wants a consistent dump of the route which starts at the first
    5048                 :            :          * nexthop. Since sibling routes are always added at the end of
    5049                 :            :          * the list, find the first sibling of the last route appended
    5050                 :            :          */
    5051   [ #  #  #  #  :          0 :         if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
                   #  # ]
    5052                 :          0 :                 rt = list_first_entry(&rt_last->fib6_siblings,
    5053                 :            :                                       struct fib6_info,
    5054                 :            :                                       fib6_siblings);
    5055                 :            :         }
    5056                 :            : 
    5057         [ #  # ]:          0 :         if (rt)
    5058                 :          0 :                 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
    5059                 :          0 : }
    5060                 :            : 
    5061                 :          0 : static int ip6_route_multipath_add(struct fib6_config *cfg,
    5062                 :            :                                    struct netlink_ext_ack *extack)
    5063                 :            : {
    5064                 :            :         struct fib6_info *rt_notif = NULL, *rt_last = NULL;
    5065                 :          0 :         struct nl_info *info = &cfg->fc_nlinfo;
    5066                 :            :         enum fib_event_type event_type;
    5067                 :            :         struct fib6_config r_cfg;
    5068                 :            :         struct rtnexthop *rtnh;
    5069                 :            :         struct fib6_info *rt;
    5070                 :            :         struct rt6_nh *err_nh;
    5071                 :            :         struct rt6_nh *nh, *nh_safe;
    5072                 :            :         __u16 nlflags;
    5073                 :            :         int remaining;
    5074                 :            :         int attrlen;
    5075                 :            :         int err = 1;
    5076                 :            :         int nhn = 0;
    5077   [ #  #  #  # ]:          0 :         int replace = (cfg->fc_nlinfo.nlh &&
    5078                 :          0 :                        (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
    5079                 :          0 :         LIST_HEAD(rt6_nh_list);
    5080                 :            : 
    5081         [ #  # ]:          0 :         nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
    5082   [ #  #  #  # ]:          0 :         if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
    5083                 :          0 :                 nlflags |= NLM_F_APPEND;
    5084                 :            : 
    5085                 :          0 :         remaining = cfg->fc_mp_len;
    5086                 :          0 :         rtnh = (struct rtnexthop *)cfg->fc_mp;
    5087                 :            : 
    5088                 :            :         /* Parse a Multipath Entry and build a list (rt6_nh_list) of
    5089                 :            :          * fib6_info structs per nexthop
    5090                 :            :          */
    5091         [ #  # ]:          0 :         while (rtnh_ok(rtnh, remaining)) {
    5092                 :          0 :                 memcpy(&r_cfg, cfg, sizeof(*cfg));
    5093         [ #  # ]:          0 :                 if (rtnh->rtnh_ifindex)
    5094                 :          0 :                         r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
    5095                 :            : 
    5096                 :            :                 attrlen = rtnh_attrlen(rtnh);
    5097         [ #  # ]:          0 :                 if (attrlen > 0) {
    5098                 :            :                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
    5099                 :            : 
    5100                 :          0 :                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
    5101         [ #  # ]:          0 :                         if (nla) {
    5102                 :          0 :                                 r_cfg.fc_gateway = nla_get_in6_addr(nla);
    5103                 :          0 :                                 r_cfg.fc_flags |= RTF_GATEWAY;
    5104                 :            :                         }
    5105                 :          0 :                         r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
    5106                 :          0 :                         nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
    5107         [ #  # ]:          0 :                         if (nla)
    5108                 :          0 :                                 r_cfg.fc_encap_type = nla_get_u16(nla);
    5109                 :            :                 }
    5110                 :            : 
    5111                 :          0 :                 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
    5112                 :          0 :                 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
    5113         [ #  # ]:          0 :                 if (IS_ERR(rt)) {
    5114                 :            :                         err = PTR_ERR(rt);
    5115                 :            :                         rt = NULL;
    5116                 :          0 :                         goto cleanup;
    5117                 :            :                 }
    5118         [ #  # ]:          0 :                 if (!rt6_qualify_for_ecmp(rt)) {
    5119                 :            :                         err = -EINVAL;
    5120         [ #  # ]:          0 :                         NL_SET_ERR_MSG(extack,
    5121                 :            :                                        "Device only routes can not be added for IPv6 using the multipath API.");
    5122                 :          0 :                         fib6_info_release(rt);
    5123                 :          0 :                         goto cleanup;
    5124                 :            :                 }
    5125                 :            : 
    5126                 :          0 :                 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
    5127                 :            : 
    5128                 :          0 :                 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
    5129                 :            :                                             rt, &r_cfg);
    5130         [ #  # ]:          0 :                 if (err) {
    5131                 :          0 :                         fib6_info_release(rt);
    5132                 :          0 :                         goto cleanup;
    5133                 :            :                 }
    5134                 :            : 
    5135                 :            :                 rtnh = rtnh_next(rtnh, &remaining);
    5136                 :            :         }
    5137                 :            : 
    5138         [ #  # ]:          0 :         if (list_empty(&rt6_nh_list)) {
    5139         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack,
    5140                 :            :                                "Invalid nexthop configuration - no valid nexthops");
    5141                 :            :                 return -EINVAL;
    5142                 :            :         }
    5143                 :            : 
    5144                 :            :         /* for add and replace send one notification with all nexthops.
    5145                 :            :          * Skip the notification in fib6_add_rt2node and send one with
    5146                 :            :          * the full route when done
    5147                 :            :          */
    5148                 :          0 :         info->skip_notify = 1;
    5149                 :            : 
    5150                 :            :         /* For add and replace, send one notification with all nexthops. For
    5151                 :            :          * append, send one notification with all appended nexthops.
    5152                 :            :          */
    5153                 :          0 :         info->skip_notify_kernel = 1;
    5154                 :            : 
    5155                 :            :         err_nh = NULL;
    5156         [ #  # ]:          0 :         list_for_each_entry(nh, &rt6_nh_list, next) {
    5157                 :          0 :                 err = __ip6_ins_rt(nh->fib6_info, info, extack);
    5158                 :          0 :                 fib6_info_release(nh->fib6_info);
    5159                 :            : 
    5160         [ #  # ]:          0 :                 if (!err) {
    5161                 :            :                         /* save reference to last route successfully inserted */
    5162                 :          0 :                         rt_last = nh->fib6_info;
    5163                 :            : 
    5164                 :            :                         /* save reference to first route for notification */
    5165         [ #  # ]:          0 :                         if (!rt_notif)
    5166                 :            :                                 rt_notif = nh->fib6_info;
    5167                 :            :                 }
    5168                 :            : 
    5169                 :            :                 /* nh->fib6_info is used or freed at this point, reset to NULL*/
    5170                 :          0 :                 nh->fib6_info = NULL;
    5171         [ #  # ]:          0 :                 if (err) {
    5172         [ #  # ]:          0 :                         if (replace && nhn)
    5173         [ #  # ]:          0 :                                 NL_SET_ERR_MSG_MOD(extack,
    5174                 :            :                                                    "multipath route replace failed (check consistency of installed routes)");
    5175                 :          0 :                         err_nh = nh;
    5176                 :          0 :                         goto add_errout;
    5177                 :            :                 }
    5178                 :            : 
    5179                 :            :                 /* Because each route is added like a single route we remove
    5180                 :            :                  * these flags after the first nexthop: if there is a collision,
    5181                 :            :                  * we have already failed to add the first nexthop:
    5182                 :            :                  * fib6_add_rt2node() has rejected it; when replacing, old
    5183                 :            :                  * nexthops have been replaced by first new, the rest should
    5184                 :            :                  * be added to it.
    5185                 :            :                  */
    5186                 :          0 :                 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
    5187                 :            :                                                      NLM_F_REPLACE);
    5188                 :          0 :                 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
    5189                 :          0 :                 nhn++;
    5190                 :            :         }
    5191                 :            : 
    5192         [ #  # ]:          0 :         event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
    5193                 :          0 :         err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
    5194                 :          0 :                                                   rt_notif, nhn - 1, extack);
    5195         [ #  # ]:          0 :         if (err) {
    5196                 :            :                 /* Delete all the siblings that were just added */
    5197                 :            :                 err_nh = NULL;
    5198                 :            :                 goto add_errout;
    5199                 :            :         }
    5200                 :            : 
    5201                 :            :         /* success ... tell user about new route */
    5202                 :          0 :         ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
    5203                 :          0 :         goto cleanup;
    5204                 :            : 
    5205                 :            : add_errout:
    5206                 :            :         /* send notification for routes that were added so that
    5207                 :            :          * the delete notifications sent by ip6_route_del are
    5208                 :            :          * coherent
    5209                 :            :          */
    5210         [ #  # ]:          0 :         if (rt_notif)
    5211                 :          0 :                 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
    5212                 :            : 
    5213                 :            :         /* Delete routes that were already added */
    5214         [ #  # ]:          0 :         list_for_each_entry(nh, &rt6_nh_list, next) {
    5215         [ #  # ]:          0 :                 if (err_nh == nh)
    5216                 :            :                         break;
    5217                 :          0 :                 ip6_route_del(&nh->r_cfg, extack);
    5218                 :            :         }
    5219                 :            : 
    5220                 :            : cleanup:
    5221         [ #  # ]:          0 :         list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
    5222         [ #  # ]:          0 :                 if (nh->fib6_info)
    5223                 :          0 :                         fib6_info_release(nh->fib6_info);
    5224                 :            :                 list_del(&nh->next);
    5225                 :          0 :                 kfree(nh);
    5226                 :            :         }
    5227                 :            : 
    5228                 :            :         return err;
    5229                 :            : }
    5230                 :            : 
    5231                 :          0 : static int ip6_route_multipath_del(struct fib6_config *cfg,
    5232                 :            :                                    struct netlink_ext_ack *extack)
    5233                 :            : {
    5234                 :            :         struct fib6_config r_cfg;
    5235                 :            :         struct rtnexthop *rtnh;
    5236                 :            :         int remaining;
    5237                 :            :         int attrlen;
    5238                 :            :         int err = 1, last_err = 0;
    5239                 :            : 
    5240                 :          0 :         remaining = cfg->fc_mp_len;
    5241                 :          0 :         rtnh = (struct rtnexthop *)cfg->fc_mp;
    5242                 :            : 
    5243                 :            :         /* Parse a Multipath Entry */
    5244         [ #  # ]:          0 :         while (rtnh_ok(rtnh, remaining)) {
    5245                 :          0 :                 memcpy(&r_cfg, cfg, sizeof(*cfg));
    5246         [ #  # ]:          0 :                 if (rtnh->rtnh_ifindex)
    5247                 :          0 :                         r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
    5248                 :            : 
    5249                 :            :                 attrlen = rtnh_attrlen(rtnh);
    5250         [ #  # ]:          0 :                 if (attrlen > 0) {
    5251                 :            :                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
    5252                 :            : 
    5253                 :          0 :                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
    5254         [ #  # ]:          0 :                         if (nla) {
    5255                 :          0 :                                 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
    5256                 :          0 :                                 r_cfg.fc_flags |= RTF_GATEWAY;
    5257                 :            :                         }
    5258                 :            :                 }
    5259                 :          0 :                 err = ip6_route_del(&r_cfg, extack);
    5260         [ #  # ]:          0 :                 if (err)
    5261                 :            :                         last_err = err;
    5262                 :            : 
    5263                 :            :                 rtnh = rtnh_next(rtnh, &remaining);
    5264                 :            :         }
    5265                 :            : 
    5266                 :          0 :         return last_err;
    5267                 :            : }
    5268                 :            : 
    5269                 :          0 : static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
    5270                 :            :                               struct netlink_ext_ack *extack)
    5271                 :            : {
    5272                 :            :         struct fib6_config cfg;
    5273                 :            :         int err;
    5274                 :            : 
    5275                 :          0 :         err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
    5276         [ #  # ]:          0 :         if (err < 0)
    5277                 :            :                 return err;
    5278                 :            : 
    5279   [ #  #  #  # ]:          0 :         if (cfg.fc_nh_id &&
    5280                 :          0 :             !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
    5281         [ #  # ]:          0 :                 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
    5282                 :            :                 return -EINVAL;
    5283                 :            :         }
    5284                 :            : 
    5285         [ #  # ]:          0 :         if (cfg.fc_mp)
    5286                 :          0 :                 return ip6_route_multipath_del(&cfg, extack);
    5287                 :            :         else {
    5288                 :          0 :                 cfg.fc_delete_all_nh = 1;
    5289                 :          0 :                 return ip6_route_del(&cfg, extack);
    5290                 :            :         }
    5291                 :            : }
    5292                 :            : 
    5293                 :          0 : static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
    5294                 :            :                               struct netlink_ext_ack *extack)
    5295                 :            : {
    5296                 :            :         struct fib6_config cfg;
    5297                 :            :         int err;
    5298                 :            : 
    5299                 :          0 :         err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
    5300         [ #  # ]:          0 :         if (err < 0)
    5301                 :            :                 return err;
    5302                 :            : 
    5303         [ #  # ]:          0 :         if (cfg.fc_metric == 0)
    5304                 :          0 :                 cfg.fc_metric = IP6_RT_PRIO_USER;
    5305                 :            : 
    5306         [ #  # ]:          0 :         if (cfg.fc_mp)
    5307                 :          0 :                 return ip6_route_multipath_add(&cfg, extack);
    5308                 :            :         else
    5309                 :          0 :                 return ip6_route_add(&cfg, GFP_KERNEL, extack);
    5310                 :            : }
    5311                 :            : 
    5312                 :            : /* add the overhead of this fib6_nh to nexthop_len */
    5313                 :          0 : static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
    5314                 :            : {
    5315                 :            :         int *nexthop_len = arg;
    5316                 :            : 
    5317                 :          0 :         *nexthop_len += nla_total_size(0)        /* RTA_MULTIPATH */
    5318                 :            :                      + NLA_ALIGN(sizeof(struct rtnexthop))
    5319                 :            :                      + nla_total_size(16); /* RTA_GATEWAY */
    5320                 :            : 
    5321         [ #  # ]:          0 :         if (nh->fib_nh_lws) {
    5322                 :            :                 /* RTA_ENCAP_TYPE */
    5323                 :            :                 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
    5324                 :            :                 /* RTA_ENCAP */
    5325                 :          0 :                 *nexthop_len += nla_total_size(2);
    5326                 :            :         }
    5327                 :            : 
    5328                 :          0 :         return 0;
    5329                 :            : }
    5330                 :            : 
    5331                 :       1471 : static size_t rt6_nlmsg_size(struct fib6_info *f6i)
    5332                 :            : {
    5333                 :            :         int nexthop_len;
    5334                 :            : 
    5335         [ -  + ]:       1471 :         if (f6i->nh) {
    5336                 :          0 :                 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
    5337                 :          0 :                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
    5338                 :            :                                          &nexthop_len);
    5339                 :            :         } else {
    5340                 :            :                 struct fib6_nh *nh = f6i->fib6_nh;
    5341                 :            : 
    5342                 :       1471 :                 nexthop_len = 0;
    5343         [ -  + ]:       1471 :                 if (f6i->fib6_nsiblings) {
    5344                 :          0 :                         nexthop_len = nla_total_size(0)  /* RTA_MULTIPATH */
    5345                 :            :                                     + NLA_ALIGN(sizeof(struct rtnexthop))
    5346                 :            :                                     + nla_total_size(16) /* RTA_GATEWAY */
    5347                 :            :                                     + lwtunnel_get_encap_size(nh->fib_nh_lws);
    5348                 :            : 
    5349                 :          0 :                         nexthop_len *= f6i->fib6_nsiblings;
    5350                 :            :                 }
    5351                 :            :                 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
    5352                 :            :         }
    5353                 :            : 
    5354                 :       1471 :         return NLMSG_ALIGN(sizeof(struct rtmsg))
    5355                 :            :                + nla_total_size(16) /* RTA_SRC */
    5356                 :            :                + nla_total_size(16) /* RTA_DST */
    5357                 :            :                + nla_total_size(16) /* RTA_GATEWAY */
    5358                 :            :                + nla_total_size(16) /* RTA_PREFSRC */
    5359                 :            :                + nla_total_size(4) /* RTA_TABLE */
    5360                 :            :                + nla_total_size(4) /* RTA_IIF */
    5361                 :            :                + nla_total_size(4) /* RTA_OIF */
    5362                 :            :                + nla_total_size(4) /* RTA_PRIORITY */
    5363                 :            :                + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
    5364                 :            :                + nla_total_size(sizeof(struct rta_cacheinfo))
    5365                 :            :                + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
    5366                 :            :                + nla_total_size(1) /* RTA_PREF */
    5367                 :       1471 :                + nexthop_len;
    5368                 :            : }
    5369                 :            : 
    5370                 :          0 : static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
    5371                 :            :                                  unsigned char *flags)
    5372                 :            : {
    5373         [ #  # ]:          0 :         if (nexthop_is_multipath(nh)) {
    5374                 :            :                 struct nlattr *mp;
    5375                 :            : 
    5376                 :            :                 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
    5377         [ #  # ]:          0 :                 if (!mp)
    5378                 :            :                         goto nla_put_failure;
    5379                 :            : 
    5380         [ #  # ]:          0 :                 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
    5381                 :            :                         goto nla_put_failure;
    5382                 :            : 
    5383                 :            :                 nla_nest_end(skb, mp);
    5384                 :            :         } else {
    5385                 :            :                 struct fib6_nh *fib6_nh;
    5386                 :            : 
    5387                 :            :                 fib6_nh = nexthop_fib6_nh(nh);
    5388         [ #  # ]:          0 :                 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
    5389                 :            :                                      flags, false) < 0)
    5390                 :            :                         goto nla_put_failure;
    5391                 :            :         }
    5392                 :            : 
    5393                 :            :         return 0;
    5394                 :            : 
    5395                 :            : nla_put_failure:
    5396                 :            :         return -EMSGSIZE;
    5397                 :            : }
    5398                 :            : 
    5399                 :       2092 : static int rt6_fill_node(struct net *net, struct sk_buff *skb,
    5400                 :            :                          struct fib6_info *rt, struct dst_entry *dst,
    5401                 :            :                          struct in6_addr *dest, struct in6_addr *src,
    5402                 :            :                          int iif, int type, u32 portid, u32 seq,
    5403                 :            :                          unsigned int flags)
    5404                 :            : {
    5405                 :            :         struct rt6_info *rt6 = (struct rt6_info *)dst;
    5406                 :            :         struct rt6key *rt6_dst, *rt6_src;
    5407                 :            :         u32 *pmetrics, table, rt6_flags;
    5408                 :       2092 :         unsigned char nh_flags = 0;
    5409                 :            :         struct nlmsghdr *nlh;
    5410                 :            :         struct rtmsg *rtm;
    5411                 :            :         long expires = 0;
    5412                 :            : 
    5413                 :       2092 :         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
    5414         [ +  - ]:       2092 :         if (!nlh)
    5415                 :            :                 return -EMSGSIZE;
    5416                 :            : 
    5417         [ -  + ]:       2092 :         if (rt6) {
    5418                 :          0 :                 rt6_dst = &rt6->rt6i_dst;
    5419                 :          0 :                 rt6_src = &rt6->rt6i_src;
    5420                 :          0 :                 rt6_flags = rt6->rt6i_flags;
    5421                 :            :         } else {
    5422                 :       2092 :                 rt6_dst = &rt->fib6_dst;
    5423                 :       2092 :                 rt6_src = &rt->fib6_src;
    5424                 :       2092 :                 rt6_flags = rt->fib6_flags;
    5425                 :            :         }
    5426                 :            : 
    5427                 :            :         rtm = nlmsg_data(nlh);
    5428                 :       2092 :         rtm->rtm_family = AF_INET6;
    5429                 :       2092 :         rtm->rtm_dst_len = rt6_dst->plen;
    5430                 :       2092 :         rtm->rtm_src_len = rt6_src->plen;
    5431                 :       2092 :         rtm->rtm_tos = 0;
    5432         [ +  - ]:       2092 :         if (rt->fib6_table)
    5433                 :       2092 :                 table = rt->fib6_table->tb6_id;
    5434                 :            :         else
    5435                 :            :                 table = RT6_TABLE_UNSPEC;
    5436         [ +  - ]:       2092 :         rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
    5437         [ +  - ]:       2092 :         if (nla_put_u32(skb, RTA_TABLE, table))
    5438                 :            :                 goto nla_put_failure;
    5439                 :            : 
    5440                 :       2092 :         rtm->rtm_type = rt->fib6_type;
    5441                 :       2092 :         rtm->rtm_flags = 0;
    5442                 :       2092 :         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
    5443                 :       2092 :         rtm->rtm_protocol = rt->fib6_protocol;
    5444                 :            : 
    5445         [ -  + ]:       2092 :         if (rt6_flags & RTF_CACHE)
    5446                 :          0 :                 rtm->rtm_flags |= RTM_F_CLONED;
    5447                 :            : 
    5448         [ -  + ]:       2092 :         if (dest) {
    5449         [ #  # ]:          0 :                 if (nla_put_in6_addr(skb, RTA_DST, dest))
    5450                 :            :                         goto nla_put_failure;
    5451                 :          0 :                 rtm->rtm_dst_len = 128;
    5452         [ +  - ]:       2092 :         } else if (rtm->rtm_dst_len)
    5453         [ +  - ]:       4184 :                 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
    5454                 :            :                         goto nla_put_failure;
    5455                 :            : #ifdef CONFIG_IPV6_SUBTREES
    5456         [ -  + ]:       2092 :         if (src) {
    5457         [ #  # ]:          0 :                 if (nla_put_in6_addr(skb, RTA_SRC, src))
    5458                 :            :                         goto nla_put_failure;
    5459                 :          0 :                 rtm->rtm_src_len = 128;
    5460   [ -  +  #  # ]:       2092 :         } else if (rtm->rtm_src_len &&
    5461                 :          0 :                    nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
    5462                 :            :                 goto nla_put_failure;
    5463                 :            : #endif
    5464         [ -  + ]:       2092 :         if (iif) {
    5465                 :            : #ifdef CONFIG_IPV6_MROUTE
    5466         [ #  # ]:          0 :                 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
    5467                 :          0 :                         int err = ip6mr_get_route(net, skb, rtm, portid);
    5468                 :            : 
    5469         [ #  # ]:          0 :                         if (err == 0)
    5470                 :            :                                 return 0;
    5471         [ #  # ]:          0 :                         if (err < 0)
    5472                 :            :                                 goto nla_put_failure;
    5473                 :            :                 } else
    5474                 :            : #endif
    5475         [ #  # ]:          0 :                         if (nla_put_u32(skb, RTA_IIF, iif))
    5476                 :            :                                 goto nla_put_failure;
    5477         [ -  + ]:       2092 :         } else if (dest) {
    5478                 :            :                 struct in6_addr saddr_buf;
    5479   [ #  #  #  # ]:          0 :                 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
    5480                 :            :                     nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
    5481                 :            :                         goto nla_put_failure;
    5482                 :            :         }
    5483                 :            : 
    5484         [ -  + ]:       2092 :         if (rt->fib6_prefsrc.plen) {
    5485                 :            :                 struct in6_addr saddr_buf;
    5486                 :          0 :                 saddr_buf = rt->fib6_prefsrc.addr;
    5487         [ #  # ]:          0 :                 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
    5488                 :            :                         goto nla_put_failure;
    5489                 :            :         }
    5490                 :            : 
    5491         [ -  + ]:       4184 :         pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
    5492         [ +  - ]:       2092 :         if (rtnetlink_put_metrics(skb, pmetrics) < 0)
    5493                 :            :                 goto nla_put_failure;
    5494                 :            : 
    5495         [ +  - ]:       4184 :         if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
    5496                 :            :                 goto nla_put_failure;
    5497                 :            : 
    5498                 :            :         /* For multipath routes, walk the siblings list and add
    5499                 :            :          * each as a nexthop within RTA_MULTIPATH.
    5500                 :            :          */
    5501         [ -  + ]:       2092 :         if (rt6) {
    5502   [ #  #  #  # ]:          0 :                 if (rt6_flags & RTF_GATEWAY &&
    5503                 :          0 :                     nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
    5504                 :            :                         goto nla_put_failure;
    5505                 :            : 
    5506   [ #  #  #  # ]:          0 :                 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
    5507                 :            :                         goto nla_put_failure;
    5508         [ -  + ]:       2092 :         } else if (rt->fib6_nsiblings) {
    5509                 :            :                 struct fib6_info *sibling, *next_sibling;
    5510                 :            :                 struct nlattr *mp;
    5511                 :            : 
    5512                 :            :                 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
    5513         [ #  # ]:          0 :                 if (!mp)
    5514                 :            :                         goto nla_put_failure;
    5515                 :            : 
    5516         [ #  # ]:          0 :                 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
    5517                 :            :                                     rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
    5518                 :            :                         goto nla_put_failure;
    5519                 :            : 
    5520         [ #  # ]:          0 :                 list_for_each_entry_safe(sibling, next_sibling,
    5521                 :            :                                          &rt->fib6_siblings, fib6_siblings) {
    5522         [ #  # ]:          0 :                         if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
    5523                 :            :                                             sibling->fib6_nh->fib_nh_weight,
    5524                 :            :                                             AF_INET6) < 0)
    5525                 :            :                                 goto nla_put_failure;
    5526                 :            :                 }
    5527                 :            : 
    5528                 :            :                 nla_nest_end(skb, mp);
    5529         [ -  + ]:       2092 :         } else if (rt->nh) {
    5530         [ #  # ]:          0 :                 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
    5531                 :            :                         goto nla_put_failure;
    5532                 :            : 
    5533         [ #  # ]:          0 :                 if (nexthop_is_blackhole(rt->nh))
    5534                 :          0 :                         rtm->rtm_type = RTN_BLACKHOLE;
    5535                 :            : 
    5536         [ #  # ]:          0 :                 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
    5537                 :            :                         goto nla_put_failure;
    5538                 :            : 
    5539                 :          0 :                 rtm->rtm_flags |= nh_flags;
    5540                 :            :         } else {
    5541         [ +  - ]:       2092 :                 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
    5542                 :            :                                      &nh_flags, false) < 0)
    5543                 :            :                         goto nla_put_failure;
    5544                 :            : 
    5545                 :       2092 :                 rtm->rtm_flags |= nh_flags;
    5546                 :            :         }
    5547                 :            : 
    5548         [ -  + ]:       2092 :         if (rt6_flags & RTF_EXPIRES) {
    5549         [ #  # ]:          0 :                 expires = dst ? dst->expires : rt->expires;
    5550                 :          0 :                 expires -= jiffies;
    5551                 :            :         }
    5552                 :            : 
    5553   [ -  +  +  - ]:       2092 :         if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
    5554                 :            :                 goto nla_put_failure;
    5555                 :            : 
    5556         [ +  - ]:       4184 :         if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
    5557                 :            :                 goto nla_put_failure;
    5558                 :            : 
    5559                 :            : 
    5560                 :            :         nlmsg_end(skb, nlh);
    5561                 :       2092 :         return 0;
    5562                 :            : 
    5563                 :            : nla_put_failure:
    5564                 :            :         nlmsg_cancel(skb, nlh);
    5565                 :          0 :         return -EMSGSIZE;
    5566                 :            : }
    5567                 :            : 
    5568                 :          0 : static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
    5569                 :            : {
    5570                 :            :         const struct net_device *dev = arg;
    5571                 :            : 
    5572         [ #  # ]:          0 :         if (nh->fib_nh_dev == dev)
    5573                 :            :                 return 1;
    5574                 :            : 
    5575                 :          0 :         return 0;
    5576                 :            : }
    5577                 :            : 
    5578                 :          0 : static bool fib6_info_uses_dev(const struct fib6_info *f6i,
    5579                 :            :                                const struct net_device *dev)
    5580                 :            : {
    5581         [ #  # ]:          0 :         if (f6i->nh) {
    5582                 :            :                 struct net_device *_dev = (struct net_device *)dev;
    5583                 :            : 
    5584                 :          0 :                 return !!nexthop_for_each_fib6_nh(f6i->nh,
    5585                 :            :                                                   fib6_info_nh_uses_dev,
    5586                 :            :                                                   _dev);
    5587                 :            :         }
    5588                 :            : 
    5589         [ #  # ]:          0 :         if (f6i->fib6_nh->fib_nh_dev == dev)
    5590                 :            :                 return true;
    5591                 :            : 
    5592         [ #  # ]:          0 :         if (f6i->fib6_nsiblings) {
    5593                 :            :                 struct fib6_info *sibling, *next_sibling;
    5594                 :            : 
    5595         [ #  # ]:          0 :                 list_for_each_entry_safe(sibling, next_sibling,
    5596                 :            :                                          &f6i->fib6_siblings, fib6_siblings) {
    5597         [ #  # ]:          0 :                         if (sibling->fib6_nh->fib_nh_dev == dev)
    5598                 :            :                                 return true;
    5599                 :            :                 }
    5600                 :            :         }
    5601                 :            : 
    5602                 :            :         return false;
    5603                 :            : }
    5604                 :            : 
    5605                 :            : struct fib6_nh_exception_dump_walker {
    5606                 :            :         struct rt6_rtnl_dump_arg *dump;
    5607                 :            :         struct fib6_info *rt;
    5608                 :            :         unsigned int flags;
    5609                 :            :         unsigned int skip;
    5610                 :            :         unsigned int count;
    5611                 :            : };
    5612                 :            : 
    5613                 :        621 : static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
    5614                 :            : {
    5615                 :            :         struct fib6_nh_exception_dump_walker *w = arg;
    5616                 :        621 :         struct rt6_rtnl_dump_arg *dump = w->dump;
    5617                 :            :         struct rt6_exception_bucket *bucket;
    5618                 :            :         struct rt6_exception *rt6_ex;
    5619                 :            :         int i, err;
    5620                 :            : 
    5621                 :            :         bucket = fib6_nh_get_excptn_bucket(nh, NULL);
    5622         [ -  + ]:        621 :         if (!bucket)
    5623                 :            :                 return 0;
    5624                 :            : 
    5625         [ #  # ]:          0 :         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
    5626   [ #  #  #  #  :          0 :                 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
                   #  # ]
    5627         [ #  # ]:          0 :                         if (w->skip) {
    5628                 :          0 :                                 w->skip--;
    5629                 :          0 :                                 continue;
    5630                 :            :                         }
    5631                 :            : 
    5632                 :            :                         /* Expiration of entries doesn't bump sernum, insertion
    5633                 :            :                          * does. Removal is triggered by insertion, so we can
    5634                 :            :                          * rely on the fact that if entries change between two
    5635                 :            :                          * partial dumps, this node is scanned again completely,
    5636                 :            :                          * see rt6_insert_exception() and fib6_dump_table().
    5637                 :            :                          *
    5638                 :            :                          * Count expired entries we go through as handled
    5639                 :            :                          * entries that we'll skip next time, in case of partial
    5640                 :            :                          * node dump. Otherwise, if entries expire meanwhile,
    5641                 :            :                          * we'll skip the wrong amount.
    5642                 :            :                          */
    5643         [ #  # ]:          0 :                         if (rt6_check_expired(rt6_ex->rt6i)) {
    5644                 :          0 :                                 w->count++;
    5645                 :          0 :                                 continue;
    5646                 :            :                         }
    5647                 :            : 
    5648                 :          0 :                         err = rt6_fill_node(dump->net, dump->skb, w->rt,
    5649                 :          0 :                                             &rt6_ex->rt6i->dst, NULL, NULL, 0,
    5650                 :            :                                             RTM_NEWROUTE,
    5651                 :          0 :                                             NETLINK_CB(dump->cb->skb).portid,
    5652                 :          0 :                                             dump->cb->nlh->nlmsg_seq, w->flags);
    5653         [ #  # ]:          0 :                         if (err)
    5654                 :          0 :                                 return err;
    5655                 :            : 
    5656                 :          0 :                         w->count++;
    5657                 :            :                 }
    5658                 :          0 :                 bucket++;
    5659                 :            :         }
    5660                 :            : 
    5661                 :            :         return 0;
    5662                 :            : }
    5663                 :            : 
    5664                 :            : /* Return -1 if done with node, number of handled routes on partial dump */
    5665                 :       1035 : int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
    5666                 :            : {
    5667                 :            :         struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
    5668                 :            :         struct fib_dump_filter *filter = &arg->filter;
    5669                 :            :         unsigned int flags = NLM_F_MULTI;
    5670                 :       1035 :         struct net *net = arg->net;
    5671                 :            :         int count = 0;
    5672                 :            : 
    5673         [ +  + ]:       1035 :         if (rt == net->ipv6.fib6_null_entry)
    5674                 :            :                 return -1;
    5675                 :            : 
    5676   [ -  +  #  # ]:        621 :         if ((filter->flags & RTM_F_PREFIX) &&
    5677                 :          0 :             !(rt->fib6_flags & RTF_PREFIX_RT)) {
    5678                 :            :                 /* success since this is not a prefix route */
    5679                 :            :                 return -1;
    5680                 :            :         }
    5681   [ -  +  #  # ]:        621 :         if (filter->filter_set &&
    5682   [ #  #  #  # ]:          0 :             ((filter->rt_type  && rt->fib6_type != filter->rt_type) ||
    5683   [ #  #  #  # ]:          0 :              (filter->dev      && !fib6_info_uses_dev(rt, filter->dev)) ||
    5684         [ #  # ]:          0 :              (filter->protocol && rt->fib6_protocol != filter->protocol))) {
    5685                 :            :                 return -1;
    5686                 :            :         }
    5687                 :            : 
    5688   [ +  -  +  - ]:       1242 :         if (filter->filter_set ||
    5689         [ -  + ]:       1242 :             !filter->dump_routes || !filter->dump_exceptions) {
    5690                 :            :                 flags |= NLM_F_DUMP_FILTERED;
    5691                 :            :         }
    5692                 :            : 
    5693         [ +  - ]:        621 :         if (filter->dump_routes) {
    5694         [ -  + ]:        621 :                 if (skip) {
    5695                 :          0 :                         skip--;
    5696                 :            :                 } else {
    5697         [ +  - ]:       1863 :                         if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
    5698                 :            :                                           0, RTM_NEWROUTE,
    5699                 :        621 :                                           NETLINK_CB(arg->cb->skb).portid,
    5700                 :        621 :                                           arg->cb->nlh->nlmsg_seq, flags)) {
    5701                 :            :                                 return 0;
    5702                 :            :                         }
    5703                 :            :                         count++;
    5704                 :            :                 }
    5705                 :            :         }
    5706                 :            : 
    5707         [ +  - ]:        621 :         if (filter->dump_exceptions) {
    5708                 :        621 :                 struct fib6_nh_exception_dump_walker w = { .dump = arg,
    5709                 :            :                                                            .rt = rt,
    5710                 :            :                                                            .flags = flags,
    5711                 :            :                                                            .skip = skip,
    5712                 :            :                                                            .count = 0 };
    5713                 :            :                 int err;
    5714                 :            : 
    5715                 :            :                 rcu_read_lock();
    5716         [ -  + ]:        621 :                 if (rt->nh) {
    5717                 :          0 :                         err = nexthop_for_each_fib6_nh(rt->nh,
    5718                 :            :                                                        rt6_nh_dump_exceptions,
    5719                 :            :                                                        &w);
    5720                 :            :                 } else {
    5721                 :        621 :                         err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
    5722                 :            :                 }
    5723                 :            :                 rcu_read_unlock();
    5724                 :            : 
    5725         [ -  + ]:        621 :                 if (err)
    5726                 :          0 :                         return count += w.count;
    5727                 :            :         }
    5728                 :            : 
    5729                 :            :         return -1;
    5730                 :            : }
    5731                 :            : 
    5732                 :          0 : static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
    5733                 :            :                                         const struct nlmsghdr *nlh,
    5734                 :            :                                         struct nlattr **tb,
    5735                 :            :                                         struct netlink_ext_ack *extack)
    5736                 :            : {
    5737                 :            :         struct rtmsg *rtm;
    5738                 :            :         int i, err;
    5739                 :            : 
    5740         [ #  # ]:          0 :         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
    5741         [ #  # ]:          0 :                 NL_SET_ERR_MSG_MOD(extack,
    5742                 :            :                                    "Invalid header for get route request");
    5743                 :            :                 return -EINVAL;
    5744                 :            :         }
    5745                 :            : 
    5746         [ #  # ]:          0 :         if (!netlink_strict_get_check(skb))
    5747                 :          0 :                 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
    5748                 :            :                                               rtm_ipv6_policy, extack);
    5749                 :            : 
    5750                 :            :         rtm = nlmsg_data(nlh);
    5751   [ #  #  #  # ]:          0 :         if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
    5752                 :          0 :             (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
    5753         [ #  # ]:          0 :             rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
    5754                 :            :             rtm->rtm_type) {
    5755         [ #  # ]:          0 :                 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
    5756                 :            :                 return -EINVAL;
    5757                 :            :         }
    5758         [ #  # ]:          0 :         if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
    5759         [ #  # ]:          0 :                 NL_SET_ERR_MSG_MOD(extack,
    5760                 :            :                                    "Invalid flags for get route request");
    5761                 :            :                 return -EINVAL;
    5762                 :            :         }
    5763                 :            : 
    5764                 :            :         err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
    5765                 :            :                                             rtm_ipv6_policy, extack);
    5766         [ #  # ]:          0 :         if (err)
    5767                 :            :                 return err;
    5768                 :            : 
    5769   [ #  #  #  #  :          0 :         if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
                   #  # ]
    5770         [ #  # ]:          0 :             (tb[RTA_DST] && !rtm->rtm_dst_len)) {
    5771         [ #  # ]:          0 :                 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
    5772                 :            :                 return -EINVAL;
    5773                 :            :         }
    5774                 :            : 
    5775         [ #  # ]:          0 :         for (i = 0; i <= RTA_MAX; i++) {
    5776         [ #  # ]:          0 :                 if (!tb[i])
    5777                 :          0 :                         continue;
    5778                 :            : 
    5779         [ #  # ]:          0 :                 switch (i) {
    5780                 :            :                 case RTA_SRC:
    5781                 :            :                 case RTA_DST:
    5782                 :            :                 case RTA_IIF:
    5783                 :            :                 case RTA_OIF:
    5784                 :            :                 case RTA_MARK:
    5785                 :            :                 case RTA_UID:
    5786                 :            :                 case RTA_SPORT:
    5787                 :            :                 case RTA_DPORT:
    5788                 :            :                 case RTA_IP_PROTO:
    5789                 :            :                         break;
    5790                 :            :                 default:
    5791         [ #  # ]:          0 :                         NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
    5792                 :            :                         return -EINVAL;
    5793                 :            :                 }
    5794                 :            :         }
    5795                 :            : 
    5796                 :            :         return 0;
    5797                 :            : }
    5798                 :            : 
    5799                 :          0 : static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
    5800                 :            :                               struct netlink_ext_ack *extack)
    5801                 :            : {
    5802                 :          0 :         struct net *net = sock_net(in_skb->sk);
    5803                 :            :         struct nlattr *tb[RTA_MAX+1];
    5804                 :            :         int err, iif = 0, oif = 0;
    5805                 :            :         struct fib6_info *from;
    5806                 :            :         struct dst_entry *dst;
    5807                 :            :         struct rt6_info *rt;
    5808                 :            :         struct sk_buff *skb;
    5809                 :            :         struct rtmsg *rtm;
    5810                 :          0 :         struct flowi6 fl6 = {};
    5811                 :            :         bool fibmatch;
    5812                 :            : 
    5813                 :          0 :         err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
    5814         [ #  # ]:          0 :         if (err < 0)
    5815                 :            :                 goto errout;
    5816                 :            : 
    5817                 :            :         err = -EINVAL;
    5818                 :            :         rtm = nlmsg_data(nlh);
    5819                 :          0 :         fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
    5820                 :          0 :         fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
    5821                 :            : 
    5822         [ #  # ]:          0 :         if (tb[RTA_SRC]) {
    5823         [ #  # ]:          0 :                 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
    5824                 :            :                         goto errout;
    5825                 :            : 
    5826                 :          0 :                 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
    5827                 :            :         }
    5828                 :            : 
    5829         [ #  # ]:          0 :         if (tb[RTA_DST]) {
    5830         [ #  # ]:          0 :                 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
    5831                 :            :                         goto errout;
    5832                 :            : 
    5833                 :          0 :                 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
    5834                 :            :         }
    5835                 :            : 
    5836         [ #  # ]:          0 :         if (tb[RTA_IIF])
    5837                 :          0 :                 iif = nla_get_u32(tb[RTA_IIF]);
    5838                 :            : 
    5839         [ #  # ]:          0 :         if (tb[RTA_OIF])
    5840                 :          0 :                 oif = nla_get_u32(tb[RTA_OIF]);
    5841                 :            : 
    5842         [ #  # ]:          0 :         if (tb[RTA_MARK])
    5843                 :          0 :                 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
    5844                 :            : 
    5845         [ #  # ]:          0 :         if (tb[RTA_UID])
    5846                 :          0 :                 fl6.flowi6_uid = make_kuid(current_user_ns(),
    5847                 :            :                                            nla_get_u32(tb[RTA_UID]));
    5848                 :            :         else
    5849         [ #  # ]:          0 :                 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
    5850                 :            : 
    5851         [ #  # ]:          0 :         if (tb[RTA_SPORT])
    5852                 :          0 :                 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
    5853                 :            : 
    5854         [ #  # ]:          0 :         if (tb[RTA_DPORT])
    5855                 :          0 :                 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
    5856                 :            : 
    5857         [ #  # ]:          0 :         if (tb[RTA_IP_PROTO]) {
    5858                 :          0 :                 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
    5859                 :            :                                                   &fl6.flowi6_proto, AF_INET6,
    5860                 :            :                                                   extack);
    5861         [ #  # ]:          0 :                 if (err)
    5862                 :            :                         goto errout;
    5863                 :            :         }
    5864                 :            : 
    5865         [ #  # ]:          0 :         if (iif) {
    5866                 :            :                 struct net_device *dev;
    5867                 :            :                 int flags = 0;
    5868                 :            : 
    5869                 :            :                 rcu_read_lock();
    5870                 :            : 
    5871                 :          0 :                 dev = dev_get_by_index_rcu(net, iif);
    5872         [ #  # ]:          0 :                 if (!dev) {
    5873                 :            :                         rcu_read_unlock();
    5874                 :            :                         err = -ENODEV;
    5875                 :          0 :                         goto errout;
    5876                 :            :                 }
    5877                 :            : 
    5878                 :          0 :                 fl6.flowi6_iif = iif;
    5879                 :            : 
    5880         [ #  # ]:          0 :                 if (!ipv6_addr_any(&fl6.saddr))
    5881                 :            :                         flags |= RT6_LOOKUP_F_HAS_SADDR;
    5882                 :            : 
    5883                 :          0 :                 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
    5884                 :            : 
    5885                 :            :                 rcu_read_unlock();
    5886                 :            :         } else {
    5887                 :          0 :                 fl6.flowi6_oif = oif;
    5888                 :            : 
    5889                 :            :                 dst = ip6_route_output(net, NULL, &fl6);
    5890                 :            :         }
    5891                 :            : 
    5892                 :            : 
    5893                 :            :         rt = container_of(dst, struct rt6_info, dst);
    5894         [ #  # ]:          0 :         if (rt->dst.error) {
    5895                 :          0 :                 err = rt->dst.error;
    5896                 :            :                 ip6_rt_put(rt);
    5897                 :            :                 goto errout;
    5898                 :            :         }
    5899                 :            : 
    5900         [ #  # ]:          0 :         if (rt == net->ipv6.ip6_null_entry) {
    5901                 :          0 :                 err = rt->dst.error;
    5902                 :            :                 ip6_rt_put(rt);
    5903                 :            :                 goto errout;
    5904                 :            :         }
    5905                 :            : 
    5906                 :            :         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
    5907         [ #  # ]:          0 :         if (!skb) {
    5908                 :            :                 ip6_rt_put(rt);
    5909                 :            :                 err = -ENOBUFS;
    5910                 :          0 :                 goto errout;
    5911                 :            :         }
    5912                 :            : 
    5913                 :          0 :         skb_dst_set(skb, &rt->dst);
    5914                 :            : 
    5915                 :            :         rcu_read_lock();
    5916                 :          0 :         from = rcu_dereference(rt->from);
    5917         [ #  # ]:          0 :         if (from) {
    5918         [ #  # ]:          0 :                 if (fibmatch)
    5919                 :          0 :                         err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
    5920                 :            :                                             iif, RTM_NEWROUTE,
    5921                 :            :                                             NETLINK_CB(in_skb).portid,
    5922                 :            :                                             nlh->nlmsg_seq, 0);
    5923                 :            :                 else
    5924                 :          0 :                         err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
    5925                 :            :                                             &fl6.saddr, iif, RTM_NEWROUTE,
    5926                 :            :                                             NETLINK_CB(in_skb).portid,
    5927                 :            :                                             nlh->nlmsg_seq, 0);
    5928                 :            :         } else {
    5929                 :            :                 err = -ENETUNREACH;
    5930                 :            :         }
    5931                 :            :         rcu_read_unlock();
    5932                 :            : 
    5933         [ #  # ]:          0 :         if (err < 0) {
    5934                 :          0 :                 kfree_skb(skb);
    5935                 :          0 :                 goto errout;
    5936                 :            :         }
    5937                 :            : 
    5938                 :          0 :         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
    5939                 :            : errout:
    5940                 :          0 :         return err;
    5941                 :            : }
    5942                 :            : 
    5943                 :       1471 : void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
    5944                 :            :                      unsigned int nlm_flags)
    5945                 :            : {
    5946                 :            :         struct sk_buff *skb;
    5947                 :       1471 :         struct net *net = info->nl_net;
    5948                 :            :         u32 seq;
    5949                 :            :         int err;
    5950                 :            : 
    5951                 :            :         err = -ENOBUFS;
    5952         [ -  + ]:       1471 :         seq = info->nlh ? info->nlh->nlmsg_seq : 0;
    5953                 :            : 
    5954                 :       1471 :         skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
    5955         [ +  - ]:       1471 :         if (!skb)
    5956                 :            :                 goto errout;
    5957                 :            : 
    5958                 :       1471 :         err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
    5959                 :            :                             event, info->portid, seq, nlm_flags);
    5960         [ -  + ]:       1471 :         if (err < 0) {
    5961                 :            :                 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
    5962         [ #  # ]:          0 :                 WARN_ON(err == -EMSGSIZE);
    5963                 :          0 :                 kfree_skb(skb);
    5964                 :          0 :                 goto errout;
    5965                 :            :         }
    5966                 :       2942 :         rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
    5967                 :            :                     info->nlh, gfp_any());
    5968                 :       2942 :         return;
    5969                 :            : errout:
    5970         [ #  # ]:          0 :         if (err < 0)
    5971                 :          0 :                 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
    5972                 :            : }
    5973                 :            : 
    5974                 :          0 : void fib6_rt_update(struct net *net, struct fib6_info *rt,
    5975                 :            :                     struct nl_info *info)
    5976                 :            : {
    5977         [ #  # ]:          0 :         u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
    5978                 :            :         struct sk_buff *skb;
    5979                 :            :         int err = -ENOBUFS;
    5980                 :            : 
    5981                 :            :         /* call_fib6_entry_notifiers will be removed when in-kernel notifier
    5982                 :            :          * is implemented and supported for nexthop objects
    5983                 :            :          */
    5984                 :          0 :         call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
    5985                 :            : 
    5986                 :          0 :         skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
    5987         [ #  # ]:          0 :         if (!skb)
    5988                 :            :                 goto errout;
    5989                 :            : 
    5990                 :          0 :         err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
    5991                 :            :                             RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
    5992         [ #  # ]:          0 :         if (err < 0) {
    5993                 :            :                 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
    5994         [ #  # ]:          0 :                 WARN_ON(err == -EMSGSIZE);
    5995                 :          0 :                 kfree_skb(skb);
    5996                 :          0 :                 goto errout;
    5997                 :            :         }
    5998                 :          0 :         rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
    5999                 :            :                     info->nlh, gfp_any());
    6000                 :          0 :         return;
    6001                 :            : errout:
    6002         [ #  # ]:          0 :         if (err < 0)
    6003                 :          0 :                 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
    6004                 :            : }
    6005                 :            : 
    6006                 :       2070 : static int ip6_route_dev_notify(struct notifier_block *this,
    6007                 :            :                                 unsigned long event, void *ptr)
    6008                 :            : {
    6009                 :            :         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
    6010                 :            :         struct net *net = dev_net(dev);
    6011                 :            : 
    6012         [ +  + ]:       2070 :         if (!(dev->flags & IFF_LOOPBACK))
    6013                 :            :                 return NOTIFY_OK;
    6014                 :            : 
    6015         [ +  + ]:        621 :         if (event == NETDEV_REGISTER) {
    6016                 :        207 :                 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
    6017                 :        207 :                 net->ipv6.ip6_null_entry->dst.dev = dev;
    6018                 :        414 :                 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
    6019                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6020                 :        207 :                 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
    6021                 :        414 :                 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
    6022                 :        207 :                 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
    6023                 :        414 :                 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
    6024                 :            : #endif
    6025   [ -  +  #  # ]:        414 :          } else if (event == NETDEV_UNREGISTER &&
    6026                 :          0 :                     dev->reg_state != NETREG_UNREGISTERED) {
    6027                 :            :                 /* NETDEV_UNREGISTER could be fired for multiple times by
    6028                 :            :                  * netdev_wait_allrefs(). Make sure we only call this once.
    6029                 :            :                  */
    6030                 :          0 :                 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
    6031                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6032                 :          0 :                 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
    6033                 :          0 :                 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
    6034                 :            : #endif
    6035                 :            :         }
    6036                 :            : 
    6037                 :            :         return NOTIFY_OK;
    6038                 :            : }
    6039                 :            : 
    6040                 :            : /*
    6041                 :            :  *      /proc
    6042                 :            :  */
    6043                 :            : 
    6044                 :            : #ifdef CONFIG_PROC_FS
    6045                 :          0 : static int rt6_stats_seq_show(struct seq_file *seq, void *v)
    6046                 :            : {
    6047                 :          0 :         struct net *net = (struct net *)seq->private;
    6048                 :          0 :         seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
    6049                 :          0 :                    net->ipv6.rt6_stats->fib_nodes,
    6050                 :            :                    net->ipv6.rt6_stats->fib_route_nodes,
    6051                 :          0 :                    atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
    6052                 :            :                    net->ipv6.rt6_stats->fib_rt_entries,
    6053                 :            :                    net->ipv6.rt6_stats->fib_rt_cache,
    6054                 :            :                    dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
    6055                 :          0 :                    net->ipv6.rt6_stats->fib_discarded_routes);
    6056                 :            : 
    6057                 :          0 :         return 0;
    6058                 :            : }
    6059                 :            : #endif  /* CONFIG_PROC_FS */
    6060                 :            : 
    6061                 :            : #ifdef CONFIG_SYSCTL
    6062                 :            : 
    6063                 :            : static
    6064                 :          0 : int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
    6065                 :            :                               void __user *buffer, size_t *lenp, loff_t *ppos)
    6066                 :            : {
    6067                 :            :         struct net *net;
    6068                 :            :         int delay;
    6069                 :            :         int ret;
    6070         [ #  # ]:          0 :         if (!write)
    6071                 :            :                 return -EINVAL;
    6072                 :            : 
    6073                 :          0 :         net = (struct net *)ctl->extra1;
    6074                 :          0 :         delay = net->ipv6.sysctl.flush_delay;
    6075                 :          0 :         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
    6076         [ #  # ]:          0 :         if (ret)
    6077                 :            :                 return ret;
    6078                 :            : 
    6079                 :          0 :         fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
    6080                 :          0 :         return 0;
    6081                 :            : }
    6082                 :            : 
    6083                 :            : static struct ctl_table ipv6_route_table_template[] = {
    6084                 :            :         {
    6085                 :            :                 .procname       =       "flush",
    6086                 :            :                 .data           =       &init_net.ipv6.sysctl.flush_delay,
    6087                 :            :                 .maxlen         =       sizeof(int),
    6088                 :            :                 .mode           =       0200,
    6089                 :            :                 .proc_handler   =       ipv6_sysctl_rtcache_flush
    6090                 :            :         },
    6091                 :            :         {
    6092                 :            :                 .procname       =       "gc_thresh",
    6093                 :            :                 .data           =       &ip6_dst_ops_template.gc_thresh,
    6094                 :            :                 .maxlen         =       sizeof(int),
    6095                 :            :                 .mode           =       0644,
    6096                 :            :                 .proc_handler   =       proc_dointvec,
    6097                 :            :         },
    6098                 :            :         {
    6099                 :            :                 .procname       =       "max_size",
    6100                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_max_size,
    6101                 :            :                 .maxlen         =       sizeof(int),
    6102                 :            :                 .mode           =       0644,
    6103                 :            :                 .proc_handler   =       proc_dointvec,
    6104                 :            :         },
    6105                 :            :         {
    6106                 :            :                 .procname       =       "gc_min_interval",
    6107                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
    6108                 :            :                 .maxlen         =       sizeof(int),
    6109                 :            :                 .mode           =       0644,
    6110                 :            :                 .proc_handler   =       proc_dointvec_jiffies,
    6111                 :            :         },
    6112                 :            :         {
    6113                 :            :                 .procname       =       "gc_timeout",
    6114                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
    6115                 :            :                 .maxlen         =       sizeof(int),
    6116                 :            :                 .mode           =       0644,
    6117                 :            :                 .proc_handler   =       proc_dointvec_jiffies,
    6118                 :            :         },
    6119                 :            :         {
    6120                 :            :                 .procname       =       "gc_interval",
    6121                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_interval,
    6122                 :            :                 .maxlen         =       sizeof(int),
    6123                 :            :                 .mode           =       0644,
    6124                 :            :                 .proc_handler   =       proc_dointvec_jiffies,
    6125                 :            :         },
    6126                 :            :         {
    6127                 :            :                 .procname       =       "gc_elasticity",
    6128                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
    6129                 :            :                 .maxlen         =       sizeof(int),
    6130                 :            :                 .mode           =       0644,
    6131                 :            :                 .proc_handler   =       proc_dointvec,
    6132                 :            :         },
    6133                 :            :         {
    6134                 :            :                 .procname       =       "mtu_expires",
    6135                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
    6136                 :            :                 .maxlen         =       sizeof(int),
    6137                 :            :                 .mode           =       0644,
    6138                 :            :                 .proc_handler   =       proc_dointvec_jiffies,
    6139                 :            :         },
    6140                 :            :         {
    6141                 :            :                 .procname       =       "min_adv_mss",
    6142                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_min_advmss,
    6143                 :            :                 .maxlen         =       sizeof(int),
    6144                 :            :                 .mode           =       0644,
    6145                 :            :                 .proc_handler   =       proc_dointvec,
    6146                 :            :         },
    6147                 :            :         {
    6148                 :            :                 .procname       =       "gc_min_interval_ms",
    6149                 :            :                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
    6150                 :            :                 .maxlen         =       sizeof(int),
    6151                 :            :                 .mode           =       0644,
    6152                 :            :                 .proc_handler   =       proc_dointvec_ms_jiffies,
    6153                 :            :         },
    6154                 :            :         {
    6155                 :            :                 .procname       =       "skip_notify_on_dev_down",
    6156                 :            :                 .data           =       &init_net.ipv6.sysctl.skip_notify_on_dev_down,
    6157                 :            :                 .maxlen         =       sizeof(int),
    6158                 :            :                 .mode           =       0644,
    6159                 :            :                 .proc_handler   =       proc_dointvec_minmax,
    6160                 :            :                 .extra1         =       SYSCTL_ZERO,
    6161                 :            :                 .extra2         =       SYSCTL_ONE,
    6162                 :            :         },
    6163                 :            :         { }
    6164                 :            : };
    6165                 :            : 
    6166                 :        207 : struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
    6167                 :            : {
    6168                 :            :         struct ctl_table *table;
    6169                 :            : 
    6170                 :        207 :         table = kmemdup(ipv6_route_table_template,
    6171                 :            :                         sizeof(ipv6_route_table_template),
    6172                 :            :                         GFP_KERNEL);
    6173                 :            : 
    6174         [ +  - ]:        207 :         if (table) {
    6175                 :        207 :                 table[0].data = &net->ipv6.sysctl.flush_delay;
    6176                 :        207 :                 table[0].extra1 = net;
    6177                 :        207 :                 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
    6178                 :        207 :                 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
    6179                 :        207 :                 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
    6180                 :        207 :                 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
    6181                 :        207 :                 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
    6182                 :        207 :                 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
    6183                 :        207 :                 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
    6184                 :        207 :                 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
    6185                 :        207 :                 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
    6186                 :        207 :                 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
    6187                 :            : 
    6188                 :            :                 /* Don't export sysctls to unprivileged users */
    6189         [ -  + ]:        207 :                 if (net->user_ns != &init_user_ns)
    6190                 :          0 :                         table[0].procname = NULL;
    6191                 :            :         }
    6192                 :            : 
    6193                 :        207 :         return table;
    6194                 :            : }
    6195                 :            : #endif
    6196                 :            : 
    6197                 :        207 : static int __net_init ip6_route_net_init(struct net *net)
    6198                 :            : {
    6199                 :            :         int ret = -ENOMEM;
    6200                 :            : 
    6201                 :        207 :         memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
    6202                 :            :                sizeof(net->ipv6.ip6_dst_ops));
    6203                 :            : 
    6204         [ +  - ]:        207 :         if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
    6205                 :            :                 goto out_ip6_dst_ops;
    6206                 :            : 
    6207                 :        207 :         net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
    6208         [ +  - ]:        207 :         if (!net->ipv6.fib6_null_entry)
    6209                 :            :                 goto out_ip6_dst_entries;
    6210                 :        207 :         memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
    6211                 :            :                sizeof(*net->ipv6.fib6_null_entry));
    6212                 :            : 
    6213                 :        207 :         net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
    6214                 :            :                                            sizeof(*net->ipv6.ip6_null_entry),
    6215                 :            :                                            GFP_KERNEL);
    6216         [ +  - ]:        207 :         if (!net->ipv6.ip6_null_entry)
    6217                 :            :                 goto out_fib6_null_entry;
    6218                 :        207 :         net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
    6219                 :        207 :         dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
    6220                 :            :                          ip6_template_metrics, true);
    6221                 :        207 :         INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
    6222                 :            : 
    6223                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6224                 :        207 :         net->ipv6.fib6_has_custom_rules = false;
    6225                 :        207 :         net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
    6226                 :            :                                                sizeof(*net->ipv6.ip6_prohibit_entry),
    6227                 :            :                                                GFP_KERNEL);
    6228         [ +  - ]:        207 :         if (!net->ipv6.ip6_prohibit_entry)
    6229                 :            :                 goto out_ip6_null_entry;
    6230                 :        207 :         net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
    6231                 :        207 :         dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
    6232                 :            :                          ip6_template_metrics, true);
    6233                 :        207 :         INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
    6234                 :            : 
    6235                 :        207 :         net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
    6236                 :            :                                                sizeof(*net->ipv6.ip6_blk_hole_entry),
    6237                 :            :                                                GFP_KERNEL);
    6238         [ +  - ]:        207 :         if (!net->ipv6.ip6_blk_hole_entry)
    6239                 :            :                 goto out_ip6_prohibit_entry;
    6240                 :        207 :         net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
    6241                 :        207 :         dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
    6242                 :            :                          ip6_template_metrics, true);
    6243                 :        207 :         INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
    6244                 :            : #endif
    6245                 :            : 
    6246                 :        207 :         net->ipv6.sysctl.flush_delay = 0;
    6247                 :        207 :         net->ipv6.sysctl.ip6_rt_max_size = 4096;
    6248                 :        207 :         net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
    6249                 :        207 :         net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
    6250                 :        207 :         net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
    6251                 :        207 :         net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
    6252                 :        207 :         net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
    6253                 :        207 :         net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
    6254                 :        207 :         net->ipv6.sysctl.skip_notify_on_dev_down = 0;
    6255                 :            : 
    6256                 :        207 :         net->ipv6.ip6_rt_gc_expire = 30*HZ;
    6257                 :            : 
    6258                 :            :         ret = 0;
    6259                 :            : out:
    6260                 :        207 :         return ret;
    6261                 :            : 
    6262                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6263                 :            : out_ip6_prohibit_entry:
    6264                 :          0 :         kfree(net->ipv6.ip6_prohibit_entry);
    6265                 :            : out_ip6_null_entry:
    6266                 :          0 :         kfree(net->ipv6.ip6_null_entry);
    6267                 :            : #endif
    6268                 :            : out_fib6_null_entry:
    6269                 :          0 :         kfree(net->ipv6.fib6_null_entry);
    6270                 :            : out_ip6_dst_entries:
    6271                 :            :         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
    6272                 :            : out_ip6_dst_ops:
    6273                 :            :         goto out;
    6274                 :            : }
    6275                 :            : 
    6276                 :          0 : static void __net_exit ip6_route_net_exit(struct net *net)
    6277                 :            : {
    6278                 :          0 :         kfree(net->ipv6.fib6_null_entry);
    6279                 :          0 :         kfree(net->ipv6.ip6_null_entry);
    6280                 :            : #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6281                 :          0 :         kfree(net->ipv6.ip6_prohibit_entry);
    6282                 :          0 :         kfree(net->ipv6.ip6_blk_hole_entry);
    6283                 :            : #endif
    6284                 :            :         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
    6285                 :          0 : }
    6286                 :            : 
    6287                 :        207 : static int __net_init ip6_route_net_init_late(struct net *net)
    6288                 :            : {
    6289                 :            : #ifdef CONFIG_PROC_FS
    6290                 :        207 :         proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
    6291                 :            :                         sizeof(struct ipv6_route_iter));
    6292                 :        207 :         proc_create_net_single("rt6_stats", 0444, net->proc_net,
    6293                 :            :                         rt6_stats_seq_show, NULL);
    6294                 :            : #endif
    6295                 :        207 :         return 0;
    6296                 :            : }
    6297                 :            : 
    6298                 :          0 : static void __net_exit ip6_route_net_exit_late(struct net *net)
    6299                 :            : {
    6300                 :            : #ifdef CONFIG_PROC_FS
    6301                 :          0 :         remove_proc_entry("ipv6_route", net->proc_net);
    6302                 :          0 :         remove_proc_entry("rt6_stats", net->proc_net);
    6303                 :            : #endif
    6304                 :          0 : }
    6305                 :            : 
    6306                 :            : static struct pernet_operations ip6_route_net_ops = {
    6307                 :            :         .init = ip6_route_net_init,
    6308                 :            :         .exit = ip6_route_net_exit,
    6309                 :            : };
    6310                 :            : 
    6311                 :        207 : static int __net_init ipv6_inetpeer_init(struct net *net)
    6312                 :            : {
    6313                 :            :         struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
    6314                 :            : 
    6315         [ +  - ]:        207 :         if (!bp)
    6316                 :            :                 return -ENOMEM;
    6317                 :        207 :         inet_peer_base_init(bp);
    6318                 :        207 :         net->ipv6.peers = bp;
    6319                 :        207 :         return 0;
    6320                 :            : }
    6321                 :            : 
    6322                 :          0 : static void __net_exit ipv6_inetpeer_exit(struct net *net)
    6323                 :            : {
    6324                 :          0 :         struct inet_peer_base *bp = net->ipv6.peers;
    6325                 :            : 
    6326                 :          0 :         net->ipv6.peers = NULL;
    6327                 :          0 :         inetpeer_invalidate_tree(bp);
    6328                 :          0 :         kfree(bp);
    6329                 :          0 : }
    6330                 :            : 
    6331                 :            : static struct pernet_operations ipv6_inetpeer_ops = {
    6332                 :            :         .init   =       ipv6_inetpeer_init,
    6333                 :            :         .exit   =       ipv6_inetpeer_exit,
    6334                 :            : };
    6335                 :            : 
    6336                 :            : static struct pernet_operations ip6_route_net_late_ops = {
    6337                 :            :         .init = ip6_route_net_init_late,
    6338                 :            :         .exit = ip6_route_net_exit_late,
    6339                 :            : };
    6340                 :            : 
    6341                 :            : static struct notifier_block ip6_route_dev_notifier = {
    6342                 :            :         .notifier_call = ip6_route_dev_notify,
    6343                 :            :         .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
    6344                 :            : };
    6345                 :            : 
    6346                 :        207 : void __init ip6_route_init_special_entries(void)
    6347                 :            : {
    6348                 :            :         /* Registering of the loopback is done before this portion of code,
    6349                 :            :          * the loopback reference in rt6_info will not be taken, do it
    6350                 :            :          * manually for init_net */
    6351                 :        207 :         init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
    6352                 :        207 :         init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
    6353                 :        414 :         init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
    6354                 :            :   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
    6355                 :        207 :         init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
    6356                 :        414 :         init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
    6357                 :        207 :         init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
    6358                 :        414 :         init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
    6359                 :            :   #endif
    6360                 :        207 : }
    6361                 :            : 
    6362                 :        207 : int __init ip6_route_init(void)
    6363                 :            : {
    6364                 :            :         int ret;
    6365                 :            :         int cpu;
    6366                 :            : 
    6367                 :            :         ret = -ENOMEM;
    6368                 :        207 :         ip6_dst_ops_template.kmem_cachep =
    6369                 :        207 :                 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
    6370                 :            :                                   SLAB_HWCACHE_ALIGN, NULL);
    6371         [ +  - ]:        207 :         if (!ip6_dst_ops_template.kmem_cachep)
    6372                 :            :                 goto out;
    6373                 :            : 
    6374                 :            :         ret = dst_entries_init(&ip6_dst_blackhole_ops);
    6375         [ +  - ]:        207 :         if (ret)
    6376                 :            :                 goto out_kmem_cache;
    6377                 :            : 
    6378                 :        207 :         ret = register_pernet_subsys(&ipv6_inetpeer_ops);
    6379         [ +  - ]:        207 :         if (ret)
    6380                 :            :                 goto out_dst_entries;
    6381                 :            : 
    6382                 :        207 :         ret = register_pernet_subsys(&ip6_route_net_ops);
    6383         [ +  - ]:        207 :         if (ret)
    6384                 :            :                 goto out_register_inetpeer;
    6385                 :            : 
    6386                 :        207 :         ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
    6387                 :            : 
    6388                 :        207 :         ret = fib6_init();
    6389         [ +  - ]:        207 :         if (ret)
    6390                 :            :                 goto out_register_subsys;
    6391                 :            : 
    6392                 :        207 :         ret = xfrm6_init();
    6393         [ +  - ]:        207 :         if (ret)
    6394                 :            :                 goto out_fib6_init;
    6395                 :            : 
    6396                 :        207 :         ret = fib6_rules_init();
    6397         [ +  - ]:        207 :         if (ret)
    6398                 :            :                 goto xfrm6_init;
    6399                 :            : 
    6400                 :        207 :         ret = register_pernet_subsys(&ip6_route_net_late_ops);
    6401         [ +  - ]:        207 :         if (ret)
    6402                 :            :                 goto fib6_rules_init;
    6403                 :            : 
    6404                 :        207 :         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
    6405                 :            :                                    inet6_rtm_newroute, NULL, 0);
    6406         [ +  - ]:        207 :         if (ret < 0)
    6407                 :            :                 goto out_register_late_subsys;
    6408                 :            : 
    6409                 :        207 :         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
    6410                 :            :                                    inet6_rtm_delroute, NULL, 0);
    6411         [ +  - ]:        207 :         if (ret < 0)
    6412                 :            :                 goto out_register_late_subsys;
    6413                 :            : 
    6414                 :        207 :         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
    6415                 :            :                                    inet6_rtm_getroute, NULL,
    6416                 :            :                                    RTNL_FLAG_DOIT_UNLOCKED);
    6417         [ +  - ]:        207 :         if (ret < 0)
    6418                 :            :                 goto out_register_late_subsys;
    6419                 :            : 
    6420                 :        207 :         ret = register_netdevice_notifier(&ip6_route_dev_notifier);
    6421         [ +  - ]:        207 :         if (ret)
    6422                 :            :                 goto out_register_late_subsys;
    6423                 :            : 
    6424         [ +  + ]:       1035 :         for_each_possible_cpu(cpu) {
    6425                 :        828 :                 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
    6426                 :            : 
    6427                 :        828 :                 INIT_LIST_HEAD(&ul->head);
    6428                 :        828 :                 spin_lock_init(&ul->lock);
    6429                 :            :         }
    6430                 :            : 
    6431                 :            : out:
    6432                 :        207 :         return ret;
    6433                 :            : 
    6434                 :            : out_register_late_subsys:
    6435                 :          0 :         rtnl_unregister_all(PF_INET6);
    6436                 :          0 :         unregister_pernet_subsys(&ip6_route_net_late_ops);
    6437                 :            : fib6_rules_init:
    6438                 :          0 :         fib6_rules_cleanup();
    6439                 :            : xfrm6_init:
    6440                 :          0 :         xfrm6_fini();
    6441                 :            : out_fib6_init:
    6442                 :          0 :         fib6_gc_cleanup();
    6443                 :            : out_register_subsys:
    6444                 :          0 :         unregister_pernet_subsys(&ip6_route_net_ops);
    6445                 :            : out_register_inetpeer:
    6446                 :          0 :         unregister_pernet_subsys(&ipv6_inetpeer_ops);
    6447                 :            : out_dst_entries:
    6448                 :            :         dst_entries_destroy(&ip6_dst_blackhole_ops);
    6449                 :            : out_kmem_cache:
    6450                 :          0 :         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
    6451                 :          0 :         goto out;
    6452                 :            : }
    6453                 :            : 
    6454                 :          0 : void ip6_route_cleanup(void)
    6455                 :            : {
    6456                 :          0 :         unregister_netdevice_notifier(&ip6_route_dev_notifier);
    6457                 :          0 :         unregister_pernet_subsys(&ip6_route_net_late_ops);
    6458                 :          0 :         fib6_rules_cleanup();
    6459                 :          0 :         xfrm6_fini();
    6460                 :          0 :         fib6_gc_cleanup();
    6461                 :          0 :         unregister_pernet_subsys(&ipv6_inetpeer_ops);
    6462                 :          0 :         unregister_pernet_subsys(&ip6_route_net_ops);
    6463                 :            :         dst_entries_destroy(&ip6_dst_blackhole_ops);
    6464                 :          0 :         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
    6465                 :          0 : }

Generated by: LCOV version 1.14