David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Generic nexthop implementation |
| 4 | * |
| 5 | * Copyright (c) 2017-19 Cumulus Networks |
| 6 | * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> |
| 7 | */ |
| 8 | |
| 9 | #ifndef __LINUX_NEXTHOP_H |
| 10 | #define __LINUX_NEXTHOP_H |
| 11 | |
| 12 | #include <linux/netdevice.h> |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 13 | #include <linux/notifier.h> |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 14 | #include <linux/route.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <net/ip_fib.h> |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 17 | #include <net/ip6_fib.h> |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 18 | #include <net/netlink.h> |
| 19 | |
| 20 | #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK |
| 21 | |
| 22 | struct nexthop; |
| 23 | |
| 24 | struct nh_config { |
| 25 | u32 nh_id; |
| 26 | |
| 27 | u8 nh_family; |
| 28 | u8 nh_protocol; |
| 29 | u8 nh_blackhole; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 30 | u8 nh_fdb; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 31 | u32 nh_flags; |
| 32 | |
| 33 | int nh_ifindex; |
| 34 | struct net_device *dev; |
| 35 | |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 36 | union { |
| 37 | __be32 ipv4; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 38 | struct in6_addr ipv6; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 39 | } gw; |
| 40 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 41 | struct nlattr *nh_grp; |
| 42 | u16 nh_grp_type; |
| 43 | |
David Ahern | b513bd0 | 2019-05-24 14:43:07 -0700 | [diff] [blame] | 44 | struct nlattr *nh_encap; |
| 45 | u16 nh_encap_type; |
| 46 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 47 | u32 nlflags; |
| 48 | struct nl_info nlinfo; |
| 49 | }; |
| 50 | |
| 51 | struct nh_info { |
| 52 | struct hlist_node dev_hash; /* entry on netns devhash */ |
| 53 | struct nexthop *nh_parent; |
| 54 | |
| 55 | u8 family; |
| 56 | bool reject_nh; |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 57 | bool fdb_nh; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 58 | |
| 59 | union { |
| 60 | struct fib_nh_common fib_nhc; |
David Ahern | 597cfe4f | 2019-05-24 14:43:05 -0700 | [diff] [blame] | 61 | struct fib_nh fib_nh; |
David Ahern | 53010f9 | 2019-05-24 14:43:06 -0700 | [diff] [blame] | 62 | struct fib6_nh fib6_nh; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 63 | }; |
| 64 | }; |
| 65 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 66 | struct nh_grp_entry { |
| 67 | struct nexthop *nh; |
| 68 | u8 weight; |
| 69 | atomic_t upper_bound; |
| 70 | |
| 71 | struct list_head nh_list; |
| 72 | struct nexthop *nh_parent; /* nexthop of group with this entry */ |
| 73 | }; |
| 74 | |
| 75 | struct nh_group { |
Nikolay Aleksandrov | 90f33bf | 2020-05-26 12:56:15 -0600 | [diff] [blame] | 76 | struct nh_group *spare; /* spare group for removals */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 77 | u16 num_nh; |
| 78 | bool mpath; |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 79 | bool fdb_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 80 | bool has_v4; |
Gustavo A. R. Silva | 97a888c | 2020-02-28 18:14:11 -0600 | [diff] [blame] | 81 | struct nh_grp_entry nh_entries[]; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 84 | struct nexthop { |
| 85 | struct rb_node rb_node; /* entry on netns rbtree */ |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 86 | struct list_head fi_list; /* v4 entries using nh */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 87 | struct list_head f6i_list; /* v6 entries using nh */ |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 88 | struct list_head fdb_list; /* fdb entries using this nh */ |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 89 | struct list_head grp_list; /* nh group entries using this nh */ |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 90 | struct net *net; |
| 91 | |
| 92 | u32 id; |
| 93 | |
| 94 | u8 protocol; /* app managing this nh */ |
| 95 | u8 nh_flags; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 96 | bool is_group; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 97 | |
| 98 | refcount_t refcnt; |
| 99 | struct rcu_head rcu; |
| 100 | |
| 101 | union { |
| 102 | struct nh_info __rcu *nh_info; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 103 | struct nh_group __rcu *nh_grp; |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 104 | }; |
| 105 | }; |
| 106 | |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 107 | enum nexthop_event_type { |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 108 | NEXTHOP_EVENT_DEL |
| 109 | }; |
| 110 | |
Ido Schimmel | 1c9cac6 | 2020-11-04 15:30:23 +0200 | [diff] [blame^] | 111 | struct nh_notifier_single_info { |
| 112 | struct net_device *dev; |
| 113 | u8 gw_family; |
| 114 | union { |
| 115 | __be32 ipv4; |
| 116 | struct in6_addr ipv6; |
| 117 | }; |
| 118 | u8 is_reject:1, |
| 119 | is_fdb:1, |
| 120 | has_encap:1; |
| 121 | }; |
| 122 | |
| 123 | struct nh_notifier_grp_entry_info { |
| 124 | u8 weight; |
| 125 | u32 id; |
| 126 | struct nh_notifier_single_info nh; |
| 127 | }; |
| 128 | |
| 129 | struct nh_notifier_grp_info { |
| 130 | u16 num_nh; |
| 131 | bool is_fdb; |
| 132 | struct nh_notifier_grp_entry_info nh_entries[]; |
| 133 | }; |
| 134 | |
| 135 | struct nh_notifier_info { |
| 136 | struct net *net; |
| 137 | struct netlink_ext_ack *extack; |
| 138 | u32 id; |
| 139 | bool is_grp; |
| 140 | union { |
| 141 | struct nh_notifier_single_info *nh; |
| 142 | struct nh_notifier_grp_info *nh_grp; |
| 143 | }; |
| 144 | }; |
| 145 | |
Roopa Prabhu | 8590ceed | 2020-05-21 22:26:15 -0700 | [diff] [blame] | 146 | int register_nexthop_notifier(struct net *net, struct notifier_block *nb); |
| 147 | int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb); |
| 148 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 149 | /* caller is holding rcu or rtnl; no reference taken to nexthop */ |
| 150 | struct nexthop *nexthop_find_by_id(struct net *net, u32 id); |
| 151 | void nexthop_free_rcu(struct rcu_head *head); |
| 152 | |
| 153 | static inline bool nexthop_get(struct nexthop *nh) |
| 154 | { |
| 155 | return refcount_inc_not_zero(&nh->refcnt); |
| 156 | } |
| 157 | |
| 158 | static inline void nexthop_put(struct nexthop *nh) |
| 159 | { |
| 160 | if (refcount_dec_and_test(&nh->refcnt)) |
| 161 | call_rcu(&nh->rcu, nexthop_free_rcu); |
| 162 | } |
| 163 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 164 | static inline bool nexthop_cmp(const struct nexthop *nh1, |
| 165 | const struct nexthop *nh2) |
| 166 | { |
| 167 | return nh1 == nh2; |
| 168 | } |
| 169 | |
David Ahern | ce9ac05 | 2020-06-08 20:54:43 -0600 | [diff] [blame] | 170 | static inline bool nexthop_is_fdb(const struct nexthop *nh) |
| 171 | { |
| 172 | if (nh->is_group) { |
| 173 | const struct nh_group *nh_grp; |
| 174 | |
| 175 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 176 | return nh_grp->fdb_nh; |
| 177 | } else { |
| 178 | const struct nh_info *nhi; |
| 179 | |
| 180 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 181 | return nhi->fdb_nh; |
| 182 | } |
| 183 | } |
| 184 | |
David Ahern | 50cb876 | 2020-06-09 17:27:28 -0600 | [diff] [blame] | 185 | static inline bool nexthop_has_v4(const struct nexthop *nh) |
| 186 | { |
| 187 | if (nh->is_group) { |
| 188 | struct nh_group *nh_grp; |
| 189 | |
| 190 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 191 | return nh_grp->has_v4; |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 196 | static inline bool nexthop_is_multipath(const struct nexthop *nh) |
| 197 | { |
| 198 | if (nh->is_group) { |
| 199 | struct nh_group *nh_grp; |
| 200 | |
| 201 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 202 | return nh_grp->mpath; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | struct nexthop *nexthop_select_path(struct nexthop *nh, int hash); |
| 208 | |
| 209 | static inline unsigned int nexthop_num_path(const struct nexthop *nh) |
| 210 | { |
| 211 | unsigned int rc = 1; |
| 212 | |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 213 | if (nh->is_group) { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 214 | struct nh_group *nh_grp; |
| 215 | |
| 216 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 217 | if (nh_grp->mpath) |
| 218 | rc = nh_grp->num_nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | static inline |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 225 | struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 226 | { |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 227 | /* for_nexthops macros in fib_semantics.c grabs a pointer to |
| 228 | * the nexthop before checking nhsel |
| 229 | */ |
Dan Carpenter | 5270041 | 2019-06-07 18:31:07 +0300 | [diff] [blame] | 230 | if (nhsel >= nhg->num_nh) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 231 | return NULL; |
| 232 | |
| 233 | return nhg->nh_entries[nhsel].nh; |
| 234 | } |
| 235 | |
| 236 | static inline |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 237 | int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh, |
| 238 | u8 rt_family) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 239 | { |
| 240 | struct nh_group *nhg = rtnl_dereference(nh->nh_grp); |
| 241 | int i; |
| 242 | |
| 243 | for (i = 0; i < nhg->num_nh; i++) { |
| 244 | struct nexthop *nhe = nhg->nh_entries[i].nh; |
| 245 | struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info); |
| 246 | struct fib_nh_common *nhc = &nhi->fib_nhc; |
| 247 | int weight = nhg->nh_entries[i].weight; |
| 248 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 249 | if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 250 | return -EMSGSIZE; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 256 | /* called with rcu lock */ |
| 257 | static inline bool nexthop_is_blackhole(const struct nexthop *nh) |
| 258 | { |
| 259 | const struct nh_info *nhi; |
| 260 | |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 261 | if (nh->is_group) { |
| 262 | struct nh_group *nh_grp; |
| 263 | |
| 264 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 265 | if (nh_grp->num_nh > 1) |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 266 | return false; |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 267 | |
| 268 | nh = nh_grp->nh_entries[0].nh; |
David Ahern | 430a049 | 2019-05-24 14:43:08 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | nhi = rcu_dereference_rtnl(nh->nh_info); |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 272 | return nhi->reject_nh; |
| 273 | } |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 274 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 275 | static inline void nexthop_path_fib_result(struct fib_result *res, int hash) |
| 276 | { |
| 277 | struct nh_info *nhi; |
| 278 | struct nexthop *nh; |
| 279 | |
| 280 | nh = nexthop_select_path(res->fi->nh, hash); |
| 281 | nhi = rcu_dereference(nh->nh_info); |
| 282 | res->nhc = &nhi->fib_nhc; |
| 283 | } |
| 284 | |
| 285 | /* called with rcu read lock or rtnl held */ |
| 286 | static inline |
| 287 | struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel) |
| 288 | { |
| 289 | struct nh_info *nhi; |
| 290 | |
| 291 | BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0); |
| 292 | BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0); |
| 293 | |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 294 | if (nh->is_group) { |
| 295 | struct nh_group *nh_grp; |
| 296 | |
| 297 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 298 | if (nh_grp->mpath) { |
| 299 | nh = nexthop_mpath_select(nh_grp, nhsel); |
| 300 | if (!nh) |
| 301 | return NULL; |
| 302 | } |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 306 | return &nhi->fib_nhc; |
| 307 | } |
| 308 | |
David Ahern | af7888a | 2020-05-26 12:56:17 -0600 | [diff] [blame] | 309 | /* called from fib_table_lookup with rcu_lock */ |
| 310 | static inline |
| 311 | struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh, |
| 312 | int fib_flags, |
| 313 | const struct flowi4 *flp, |
| 314 | int *nhsel) |
| 315 | { |
| 316 | struct nh_info *nhi; |
| 317 | |
| 318 | if (nh->is_group) { |
| 319 | struct nh_group *nhg = rcu_dereference(nh->nh_grp); |
| 320 | int i; |
| 321 | |
| 322 | for (i = 0; i < nhg->num_nh; i++) { |
| 323 | struct nexthop *nhe = nhg->nh_entries[i].nh; |
| 324 | |
| 325 | nhi = rcu_dereference(nhe->nh_info); |
| 326 | if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) { |
| 327 | *nhsel = i; |
| 328 | return &nhi->fib_nhc; |
| 329 | } |
| 330 | } |
| 331 | } else { |
| 332 | nhi = rcu_dereference(nh->nh_info); |
| 333 | if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) { |
| 334 | *nhsel = 0; |
| 335 | return &nhi->fib_nhc; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return NULL; |
| 340 | } |
| 341 | |
David Ahern | 1fd1c76 | 2020-05-26 12:56:18 -0600 | [diff] [blame] | 342 | static inline bool nexthop_uses_dev(const struct nexthop *nh, |
| 343 | const struct net_device *dev) |
| 344 | { |
| 345 | struct nh_info *nhi; |
| 346 | |
| 347 | if (nh->is_group) { |
| 348 | struct nh_group *nhg = rcu_dereference(nh->nh_grp); |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < nhg->num_nh; i++) { |
| 352 | struct nexthop *nhe = nhg->nh_entries[i].nh; |
| 353 | |
| 354 | nhi = rcu_dereference(nhe->nh_info); |
| 355 | if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev)) |
| 356 | return true; |
| 357 | } |
| 358 | } else { |
| 359 | nhi = rcu_dereference(nh->nh_info); |
| 360 | if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev)) |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 367 | static inline unsigned int fib_info_num_path(const struct fib_info *fi) |
| 368 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 369 | if (unlikely(fi->nh)) |
| 370 | return nexthop_num_path(fi->nh); |
| 371 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 372 | return fi->fib_nhs; |
| 373 | } |
| 374 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 375 | int fib_check_nexthop(struct nexthop *nh, u8 scope, |
| 376 | struct netlink_ext_ack *extack); |
| 377 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 378 | static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel) |
| 379 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 380 | if (unlikely(fi->nh)) |
| 381 | return nexthop_fib_nhc(fi->nh, nhsel); |
| 382 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 383 | return &fi->fib_nh[nhsel].nh_common; |
| 384 | } |
| 385 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 386 | /* only used when fib_nh is built into fib_info */ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 387 | static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel) |
| 388 | { |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 389 | WARN_ON(fi->nh); |
| 390 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 391 | return &fi->fib_nh[nhsel]; |
| 392 | } |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * IPv6 variants |
| 396 | */ |
| 397 | int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, |
| 398 | struct netlink_ext_ack *extack); |
| 399 | |
| 400 | static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh) |
| 401 | { |
| 402 | struct nh_info *nhi; |
| 403 | |
David Ahern | 0b5e2e3 | 2020-05-26 12:56:16 -0600 | [diff] [blame] | 404 | if (nh->is_group) { |
| 405 | struct nh_group *nh_grp; |
| 406 | |
| 407 | nh_grp = rcu_dereference_rtnl(nh->nh_grp); |
| 408 | nh = nexthop_mpath_select(nh_grp, 0); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 409 | if (!nh) |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 414 | if (nhi->family == AF_INET6) |
| 415 | return &nhi->fib6_nh; |
| 416 | |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i) |
| 421 | { |
| 422 | struct fib6_nh *fib6_nh; |
| 423 | |
| 424 | fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh; |
| 425 | return fib6_nh->fib_nh_dev; |
| 426 | } |
| 427 | |
| 428 | static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash) |
| 429 | { |
| 430 | struct nexthop *nh = res->f6i->nh; |
| 431 | struct nh_info *nhi; |
| 432 | |
| 433 | nh = nexthop_select_path(nh, hash); |
| 434 | |
| 435 | nhi = rcu_dereference_rtnl(nh->nh_info); |
| 436 | if (nhi->reject_nh) { |
| 437 | res->fib6_type = RTN_BLACKHOLE; |
| 438 | res->fib6_flags |= RTF_REJECT; |
| 439 | res->nh = nexthop_fib6_nh(nh); |
| 440 | } else { |
| 441 | res->nh = &nhi->fib6_nh; |
| 442 | } |
| 443 | } |
David Ahern | f88c9aa | 2019-06-08 14:53:22 -0700 | [diff] [blame] | 444 | |
| 445 | int nexthop_for_each_fib6_nh(struct nexthop *nh, |
| 446 | int (*cb)(struct fib6_nh *nh, void *arg), |
| 447 | void *arg); |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 448 | |
| 449 | static inline int nexthop_get_family(struct nexthop *nh) |
| 450 | { |
| 451 | struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info); |
| 452 | |
| 453 | return nhi->family; |
| 454 | } |
| 455 | |
| 456 | static inline |
| 457 | struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh) |
| 458 | { |
| 459 | struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info); |
| 460 | |
| 461 | return &nhi->fib_nhc; |
| 462 | } |
| 463 | |
| 464 | static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh, |
| 465 | int hash) |
| 466 | { |
| 467 | struct nh_info *nhi; |
| 468 | struct nexthop *nhp; |
| 469 | |
| 470 | nhp = nexthop_select_path(nh, hash); |
| 471 | if (unlikely(!nhp)) |
| 472 | return NULL; |
| 473 | nhi = rcu_dereference(nhp->nh_info); |
| 474 | return &nhi->fib_nhc; |
| 475 | } |
David Ahern | ab84be7 | 2019-05-24 14:43:04 -0700 | [diff] [blame] | 476 | #endif |