root/net/ipv6/route.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rt6_uncached_list_add
  2. rt6_uncached_list_del
  3. rt6_uncached_list_flush_dev
  4. choose_neigh_daddr
  5. ip6_neigh_lookup
  6. ip6_dst_neigh_lookup
  7. ip6_confirm_neigh
  8. ip6_blackhole_mtu
  9. ip6_rt_blackhole_update_pmtu
  10. ip6_rt_blackhole_redirect
  11. rt6_info_init
  12. ip6_dst_alloc
  13. ip6_dst_destroy
  14. ip6_dst_ifdown
  15. __rt6_check_expired
  16. rt6_check_expired
  17. fib6_select_path
  18. __rt6_device_match
  19. __rt6_nh_dev_match
  20. rt6_nh_dev_match
  21. rt6_device_match
  22. rt6_probe_deferred
  23. rt6_probe
  24. rt6_probe
  25. rt6_check_neigh
  26. rt6_score_route
  27. find_match
  28. rt6_nh_find_match
  29. __find_rr_leaf
  30. find_rr_leaf
  31. rt6_select
  32. rt6_is_gw_or_nonexthop
  33. rt6_route_rcv
  34. ip6_rt_get_dev_rcu
  35. ip6_rt_type_to_error
  36. fib6_info_dst_flags
  37. ip6_rt_init_dst_reject
  38. ip6_rt_init_dst
  39. rt6_set_from
  40. ip6_rt_copy_init
  41. fib6_backtrack
  42. ip6_hold_safe
  43. ip6_create_rt_rcu
  44. ip6_pol_route_lookup
  45. ip6_route_lookup
  46. rt6_lookup
  47. __ip6_ins_rt
  48. ip6_ins_rt
  49. ip6_rt_cache_alloc
  50. ip6_rt_pcpu_alloc
  51. rt6_is_valid
  52. rt6_get_pcpu_route
  53. rt6_make_pcpu_route
  54. rt6_remove_exception
  55. rt6_exception_remove_oldest
  56. rt6_exception_hash
  57. __rt6_find_exception_spinlock
  58. __rt6_find_exception_rcu
  59. fib6_mtu
  60. fib6_nh_get_excptn_bucket
  61. fib6_nh_excptn_bucket_flushed
  62. fib6_nh_excptn_bucket_set_flushed
  63. rt6_insert_exception
  64. fib6_nh_flush_exceptions
  65. rt6_nh_flush_exceptions
  66. rt6_flush_exceptions
  67. rt6_find_cached_rt
  68. fib6_nh_remove_exception
  69. rt6_nh_remove_exception_rt
  70. rt6_remove_exception_rt
  71. fib6_nh_update_exception
  72. fib6_nh_find_match
  73. rt6_update_exception_stamp_rt
  74. rt6_mtu_change_route_allowed
  75. rt6_exceptions_update_pmtu
  76. fib6_nh_exceptions_clean_tohost
  77. rt6_age_examine_exception
  78. fib6_nh_age_exceptions
  79. rt6_nh_age_exceptions
  80. rt6_age_exceptions
  81. fib6_table_lookup
  82. ip6_pol_route
  83. ip6_pol_route_input
  84. ip6_route_input_lookup
  85. ip6_multipath_l3_keys
  86. rt6_multipath_hash
  87. ip6_route_input
  88. ip6_pol_route_output
  89. ip6_route_output_flags_noref
  90. ip6_route_output_flags
  91. ip6_blackhole_route
  92. fib6_check
  93. rt6_check
  94. rt6_dst_from_check
  95. ip6_dst_check
  96. ip6_negative_advice
  97. ip6_link_failure
  98. rt6_update_expires
  99. rt6_do_update_pmtu
  100. rt6_cache_allowed_for_pmtu
  101. __ip6_rt_update_pmtu
  102. ip6_rt_update_pmtu
  103. ip6_update_pmtu
  104. ip6_sk_update_pmtu
  105. ip6_sk_dst_store_flow
  106. ip6_redirect_nh_match
  107. fib6_nh_redirect_match
  108. __ip6_route_redirect
  109. ip6_route_redirect
  110. ip6_redirect
  111. ip6_redirect_no_header
  112. ip6_sk_redirect
  113. ip6_default_advmss
  114. ip6_mtu
  115. ip6_mtu_from_fib6
  116. icmp6_dst_alloc
  117. ip6_dst_gc
  118. ip6_nh_lookup_table
  119. ip6_route_check_nh_onlink
  120. ip6_route_check_nh
  121. ip6_validate_gw
  122. fib6_is_reject
  123. fib6_nh_init
  124. fib6_nh_release
  125. ip6_route_info_create
  126. ip6_route_add
  127. __ip6_del_rt
  128. ip6_del_rt
  129. __ip6_del_rt_siblings
  130. __ip6_del_cached_rt
  131. ip6_del_cached_rt
  132. fib6_nh_del_cached_rt
  133. ip6_del_cached_rt_nh
  134. ip6_route_del
  135. rt6_do_redirect
  136. rt6_get_route_info
  137. rt6_add_route_info
  138. rt6_get_dflt_router
  139. rt6_add_dflt_router
  140. __rt6_purge_dflt_routers
  141. rt6_purge_dflt_routers
  142. rtmsg_to_fib6_config
  143. ipv6_route_ioctl
  144. ip6_pkt_drop
  145. ip6_pkt_discard
  146. ip6_pkt_discard_out
  147. ip6_pkt_prohibit
  148. ip6_pkt_prohibit_out
  149. addrconf_f6i_alloc
  150. fib6_remove_prefsrc
  151. rt6_remove_prefsrc
  152. fib6_clean_tohost
  153. rt6_clean_tohost
  154. rt6_multipath_first_sibling
  155. rt6_is_dead
  156. rt6_multipath_total_weight
  157. rt6_upper_bound_set
  158. rt6_multipath_upper_bound_set
  159. rt6_multipath_rebalance
  160. fib6_ifup
  161. rt6_sync_up
  162. rt6_multipath_uses_dev
  163. rt6_multipath_flush
  164. rt6_multipath_dead_count
  165. rt6_multipath_nh_flags_set
  166. fib6_ifdown
  167. rt6_sync_down_dev
  168. rt6_disable_ip
  169. fib6_nh_mtu_change
  170. rt6_mtu_change_route
  171. rt6_mtu_change
  172. rtm_to_fib6_config
  173. ip6_route_info_append
  174. ip6_route_mpath_notify
  175. ip6_route_multipath_add
  176. ip6_route_multipath_del
  177. inet6_rtm_delroute
  178. inet6_rtm_newroute
  179. rt6_nh_nlmsg_size
  180. rt6_nlmsg_size
  181. rt6_fill_node_nexthop
  182. rt6_fill_node
  183. fib6_info_nh_uses_dev
  184. fib6_info_uses_dev
  185. rt6_nh_dump_exceptions
  186. rt6_dump_route
  187. inet6_rtm_valid_getroute_req
  188. inet6_rtm_getroute
  189. inet6_rt_notify
  190. fib6_rt_update
  191. ip6_route_dev_notify
  192. rt6_stats_seq_show
  193. ipv6_sysctl_rtcache_flush
  194. ipv6_route_sysctl_init
  195. ip6_route_net_init
  196. ip6_route_net_exit
  197. ip6_route_net_init_late
  198. ip6_route_net_exit_late
  199. ipv6_inetpeer_init
  200. ipv6_inetpeer_exit
  201. ip6_route_init_special_entries
  202. ip6_route_init
  203. ip6_route_cleanup

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

/* [<][>][^][v][top][bottom][index][help] */